blob: 63a65a1a163bff718768fc1908c34740c9be77be [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
Valerio Settibf999cb2023-12-28 17:48:13 +01009#include "mbedtls/psa_util.h"
10
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020011/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
12 * uses mbedtls_ctr_drbg internally. */
13#include "mbedtls/ctr_drbg.h"
14
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020016#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020017
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +010018#include "psa_crypto_core.h"
19
Gilles Peskine8e94efe2021-02-13 00:25:53 +010020#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010021#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010022#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053023#if defined(PSA_CRYPTO_DRIVER_TEST)
24#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053025#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
26#else
27#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053028#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010029
Gilles Peskine4023c012021-05-27 13:21:20 +020030/* If this comes up, it's a bug in the test code or in the test data. */
31#define UNUSED 0xdeadbeef
32
Dave Rodgman647791d2021-06-23 12:49:59 +010033/* Assert that an operation is (not) active.
34 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010035#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
36#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010037
Przemek Stekiel7c795482022-11-15 22:26:12 +010038#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010039int ecjpake_operation_setup(psa_pake_operation_t *operation,
40 psa_pake_cipher_suite_t *cipher_suite,
41 psa_pake_role_t role,
42 mbedtls_svc_key_id_t key,
43 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010044{
Gilles Peskine449bd832023-01-11 14:50:10 +010045 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010046
Gilles Peskine449bd832023-01-11 14:50:10 +010047 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010050
Gilles Peskine449bd832023-01-11 14:50:10 +010051 if (key_available) {
52 PSA_ASSERT(psa_pake_set_password_key(operation, key));
53 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010054 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010055exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010056 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010057}
58#endif
59
Jaeden Amerof24c7f82018-06-27 17:20:43 +010060/** An invalid export length that will never be set by psa_export_key(). */
61static const size_t INVALID_EXPORT_LENGTH = ~0U;
62
Gilles Peskinea7aa4422018-08-14 15:17:54 +020063/** Test if a buffer contains a constant byte value.
64 *
65 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020066 *
67 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020068 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020069 * \param size Size of the buffer in bytes.
70 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020071 * \return 1 if the buffer is all-bits-zero.
72 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020073 */
Gilles Peskine449bd832023-01-11 14:50:10 +010074static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020075{
76 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010077 for (i = 0; i < size; i++) {
78 if (((unsigned char *) buffer)[i] != c) {
79 return 0;
80 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020081 }
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020083}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010084#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020085/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010086static int asn1_write_10x(unsigned char **p,
87 unsigned char *start,
88 size_t bits,
89 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020090{
91 int ret;
92 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (bits == 0) {
94 return MBEDTLS_ERR_ASN1_INVALID_DATA;
95 }
96 if (bits <= 8 && x >= 1 << (bits - 1)) {
97 return MBEDTLS_ERR_ASN1_INVALID_DATA;
98 }
99 if (*p < start || *p - start < (ptrdiff_t) len) {
100 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
101 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200102 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 (*p)[len-1] = x;
104 if (bits % 8 == 0) {
105 (*p)[1] |= 1;
106 } else {
107 (*p)[0] |= 1 << (bits % 8);
108 }
109 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
110 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
111 MBEDTLS_ASN1_INTEGER));
112 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200113}
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115static int construct_fake_rsa_key(unsigned char *buffer,
116 size_t buffer_size,
117 unsigned char **p,
118 size_t bits,
119 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200120{
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200122 int ret;
123 int len = 0;
124 /* Construct something that looks like a DER encoding of
125 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
126 * RSAPrivateKey ::= SEQUENCE {
127 * version Version,
128 * modulus INTEGER, -- n
129 * publicExponent INTEGER, -- e
130 * privateExponent INTEGER, -- d
131 * prime1 INTEGER, -- p
132 * prime2 INTEGER, -- q
133 * exponent1 INTEGER, -- d mod (p-1)
134 * exponent2 INTEGER, -- d mod (q-1)
135 * coefficient INTEGER, -- (inverse of q) mod p
136 * otherPrimeInfos OtherPrimeInfos OPTIONAL
137 * }
138 * Or, for a public key, the same structure with only
139 * version, modulus and publicExponent.
140 */
141 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (keypair) {
143 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
144 asn1_write_10x(p, buffer, half_bits, 1));
145 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
146 asn1_write_10x(p, buffer, half_bits, 1));
147 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
148 asn1_write_10x(p, buffer, half_bits, 1));
149 MBEDTLS_ASN1_CHK_ADD(len, /* q */
150 asn1_write_10x(p, buffer, half_bits, 1));
151 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
152 asn1_write_10x(p, buffer, half_bits, 3));
153 MBEDTLS_ASN1_CHK_ADD(len, /* d */
154 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200155 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
157 asn1_write_10x(p, buffer, 17, 1));
158 MBEDTLS_ASN1_CHK_ADD(len, /* n */
159 asn1_write_10x(p, buffer, bits, 1));
160 if (keypair) {
161 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
162 mbedtls_asn1_write_int(p, buffer, 0));
163 }
164 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200165 {
166 const unsigned char tag =
167 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200169 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200171}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100172#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174int exercise_mac_setup(psa_key_type_t key_type,
175 const unsigned char *key_bytes,
176 size_t key_length,
177 psa_algorithm_t alg,
178 psa_mac_operation_t *operation,
179 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100180{
Ronald Cron5425a212020-08-04 14:58:35 +0200181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
185 psa_set_key_algorithm(&attributes, alg);
186 psa_set_key_type(&attributes, key_type);
187 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100190 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* If setup failed, reproduce the failure, so that the caller can
193 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (*status != PSA_SUCCESS) {
195 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 }
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 psa_destroy_key(key);
199 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200
201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 psa_destroy_key(key);
203 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100204}
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int exercise_cipher_setup(psa_key_type_t key_type,
207 const unsigned char *key_bytes,
208 size_t key_length,
209 psa_algorithm_t alg,
210 psa_cipher_operation_t *operation,
211 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100212{
Ronald Cron5425a212020-08-04 14:58:35 +0200213 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
217 psa_set_key_algorithm(&attributes, alg);
218 psa_set_key_type(&attributes, key_type);
219 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100224 /* If setup failed, reproduce the failure, so that the caller can
225 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (*status != PSA_SUCCESS) {
227 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
228 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100229 }
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 psa_destroy_key(key);
232 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100233
234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 psa_destroy_key(key);
236 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100237}
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240{
241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200243 uint8_t buffer[1];
244 size_t length;
245 int ok = 0;
246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 psa_set_key_id(&attributes, key_id);
248 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
249 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
250 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
251 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
252 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200253 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200255 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
257 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
258 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
259 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
260 TEST_EQUAL(psa_get_key_type(&attributes), 0);
261 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
264 PSA_ERROR_INVALID_HANDLE);
265 TEST_EQUAL(psa_export_public_key(key,
266 buffer, sizeof(buffer), &length),
267 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200268
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200269 ok = 1;
270
271exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100272 /*
273 * Key attributes may have been returned by psa_get_key_attributes()
274 * thus reset them as required.
275 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200279}
280
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200281/* Assert that a key isn't reported as having a slot number. */
282#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100283#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200284 do \
285 { \
286 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 TEST_EQUAL(psa_get_key_slot_number( \
288 attributes, \
289 &ASSERT_NO_SLOT_NUMBER_slot_number), \
290 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200291 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200293#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294#define ASSERT_NO_SLOT_NUMBER(attributes) \
295 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200296#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
297
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530298#define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */
299
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100300/* An overapproximation of the amount of storage needed for a key of the
301 * given type and with the given content. The API doesn't make it easy
302 * to find a good value for the size. The current implementation doesn't
303 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304#define KEY_BITS_FROM_DATA(type, data) \
305 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100306
Darryl Green0c6575a2018-11-07 16:05:30 +0000307typedef enum {
308 IMPORT_KEY = 0,
309 GENERATE_KEY = 1,
310 DERIVE_KEY = 2
311} generate_method;
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100314 DO_NOT_SET_LENGTHS = 0,
315 SET_LENGTHS_BEFORE_NONCE = 1,
316 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100317} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100320 USE_NULL_TAG = 0,
321 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100322} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100323
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530324
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325/*!
326 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 * \param key_type_arg Type of key passed in
328 * \param key_data The encryption / decryption key data
329 * \param alg_arg The type of algorithm used
330 * \param nonce Nonce data
331 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100332 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100333 * feed additional data in to be encrypted /
334 * decrypted. If -1, no chunking.
335 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100336 * \param data_part_len_arg If not -1, the length of chunks to feed
337 * the data in to be encrypted / decrypted. If
338 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100339 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100340 * expected here, this controls whether or not
341 * to set lengths, and in what order with
342 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100343 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100344 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100345 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100346 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100347 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100349static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
350 int alg_arg,
351 data_t *nonce,
352 data_t *additional_data,
353 int ad_part_len_arg,
354 data_t *input_data,
355 int data_part_len_arg,
356 set_lengths_method_t set_lengths_method,
357 data_t *expected_output,
358 int is_encrypt,
359 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100360{
361 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
362 psa_key_type_t key_type = key_type_arg;
363 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100364 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 unsigned char *output_data = NULL;
366 unsigned char *part_data = NULL;
367 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100368 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100369 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100370 size_t output_size = 0;
371 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100372 size_t output_length = 0;
373 size_t key_bits = 0;
374 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100375 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100376 size_t part_length = 0;
377 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100378 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100379 size_t ad_part_len = 0;
380 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
383 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
384
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100386 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (is_encrypt) {
391 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
392 } else {
393 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100394 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100395
396 psa_set_key_algorithm(&attributes, alg);
397 psa_set_key_type(&attributes, key_type);
398
399 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
400 &key));
401
402 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
403 key_bits = psa_get_key_bits(&attributes);
404
405 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
406
407 if (is_encrypt) {
408 /* Tag gets written at end of buffer. */
409 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
410 (input_data->len +
411 tag_length));
412 data_true_size = input_data->len;
413 } else {
414 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
415 (input_data->len -
416 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100417
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100418 /* Do not want to attempt to decrypt tag. */
419 data_true_size = input_data->len - tag_length;
420 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100421
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100422 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (is_encrypt) {
425 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
426 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
427 } else {
428 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
429 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100430 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100431
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100432 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (is_encrypt) {
435 status = psa_aead_encrypt_setup(&operation, key, alg);
436 } else {
437 status = psa_aead_decrypt_setup(&operation, key, alg);
438 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100439
440 /* If the operation is not supported, just skip and not fail in case the
441 * encryption involves a common limitation of cryptography hardwares and
442 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (status == PSA_ERROR_NOT_SUPPORTED) {
444 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
445 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 }
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
451 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
452 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
453 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
454 data_true_size));
455 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
456 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
457 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
460 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100464 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100465 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100468 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 part_offset += part_length, part_count++) {
470 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100471 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100473 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100475 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 }
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 PSA_ASSERT(psa_aead_update_ad(&operation,
479 additional_data->x + part_offset,
480 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100481
Paul Elliottd3f82412021-06-16 16:52:21 +0100482 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100484 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
486 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100487 }
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 data_part_len = (size_t) data_part_len_arg;
492 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
493 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100494
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100495 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100498 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 part_offset += part_length, part_count++) {
500 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100501 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 } else if ((data_true_size - part_offset) < data_part_len) {
503 part_length = (data_true_size - part_offset);
504 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100505 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100506 }
507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 PSA_ASSERT(psa_aead_update(&operation,
509 (input_data->x + part_offset),
510 part_length, part_data,
511 part_data_size,
512 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (output_data && output_part_length) {
515 memcpy((output_data + output_length), part_data,
516 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100517 }
518
Paul Elliottd3f82412021-06-16 16:52:21 +0100519 output_length += output_part_length;
520 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100522 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
524 data_true_size, output_data,
525 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100526 }
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (is_encrypt) {
529 PSA_ASSERT(psa_aead_finish(&operation, final_data,
530 final_output_size,
531 &output_part_length,
532 tag_buffer, tag_length,
533 &tag_size));
534 } else {
535 PSA_ASSERT(psa_aead_verify(&operation, final_data,
536 final_output_size,
537 &output_part_length,
538 (input_data->x + data_true_size),
539 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100540 }
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (output_data && output_part_length) {
543 memcpy((output_data + output_length), final_data,
544 output_part_length);
545 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100546
547 output_length += output_part_length;
548
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100549
550 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
551 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (is_encrypt) {
553 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (output_data && tag_length) {
556 memcpy((output_data + output_length), tag_buffer,
557 tag_length);
558 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100559
560 output_length += tag_length;
561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 TEST_EQUAL(output_length,
563 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
564 input_data->len));
565 TEST_LE_U(output_length,
566 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
567 } else {
568 TEST_EQUAL(output_length,
569 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
570 input_data->len));
571 TEST_LE_U(output_length,
572 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100573 }
574
Paul Elliottd3f82412021-06-16 16:52:21 +0100575
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100576 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100577 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliottd3f82412021-06-16 16:52:21 +0100579
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100580 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100581
582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 psa_destroy_key(key);
584 psa_aead_abort(&operation);
585 mbedtls_free(output_data);
586 mbedtls_free(part_data);
587 mbedtls_free(final_data);
588 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100591}
592
Neil Armstrong4766f992022-02-28 16:23:59 +0100593/*!
594 * \brief Internal Function for MAC multipart tests.
595 * \param key_type_arg Type of key passed in
596 * \param key_data The encryption / decryption key data
597 * \param alg_arg The type of algorithm used
598 * \param input_data Data to encrypt / decrypt
599 * \param data_part_len_arg If not -1, the length of chunks to feed
600 * the data in to be encrypted / decrypted. If
601 * -1, no chunking
602 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000603 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100604 * \param do_zero_parts If non-zero, interleave zero length chunks
605 * with normal length chunks.
606 * \return int Zero on failure, non-zero on success.
607 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
609 int alg_arg,
610 data_t *input_data,
611 int data_part_len_arg,
612 data_t *expected_output,
613 int is_verify,
614 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100615{
616 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
617 psa_key_type_t key_type = key_type_arg;
618 psa_algorithm_t alg = alg_arg;
619 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
620 unsigned char mac[PSA_MAC_MAX_SIZE];
621 size_t part_offset = 0;
622 size_t part_length = 0;
623 size_t data_part_len = 0;
624 size_t mac_len = 0;
625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
626 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
627
628 int test_ok = 0;
629 size_t part_count = 0;
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (is_verify) {
634 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
635 } else {
636 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
637 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 psa_set_key_algorithm(&attributes, alg);
640 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
643 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (is_verify) {
646 status = psa_mac_verify_setup(&operation, key, alg);
647 } else {
648 status = psa_mac_sign_setup(&operation, key, alg);
649 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100654 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100658 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 part_offset += part_length, part_count++) {
660 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100661 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 } else if ((input_data->len - part_offset) < data_part_len) {
663 part_length = (input_data->len - part_offset);
664 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100665 part_length = data_part_len;
666 }
667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 PSA_ASSERT(psa_mac_update(&operation,
669 (input_data->x + part_offset),
670 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100671 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100673 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
675 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100676 }
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (is_verify) {
679 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
680 expected_output->len));
681 } else {
682 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
683 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100684
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100685 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100686 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100687 }
688
689 test_ok = 1;
690
691exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 psa_destroy_key(key);
693 psa_mac_abort(&operation);
694 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100697}
698
Neil Armstrong75673ab2022-06-15 17:39:01 +0200699#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100700static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
701 psa_pake_operation_t *server,
702 psa_pake_operation_t *client,
703 int client_input_first,
704 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200705{
706 unsigned char *buffer0 = NULL, *buffer1 = NULL;
707 size_t buffer_length = (
708 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
709 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
710 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200711 /* The output should be exactly this size according to the spec */
712 const size_t expected_size_key_share =
713 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
714 /* The output should be exactly this size according to the spec */
715 const size_t expected_size_zk_public =
716 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
717 /* The output can be smaller: the spec allows stripping leading zeroes */
718 const size_t max_expected_size_zk_proof =
719 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200720 size_t buffer0_off = 0;
721 size_t buffer1_off = 0;
722 size_t s_g1_len, s_g2_len, s_a_len;
723 size_t s_g1_off, s_g2_off, s_a_off;
724 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
725 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
726 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
727 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
728 size_t c_g1_len, c_g2_len, c_a_len;
729 size_t c_g1_off, c_g2_off, c_a_off;
730 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
731 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
732 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
733 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
734 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200735 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200736
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100737 TEST_CALLOC(buffer0, buffer_length);
738 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200741 case 1:
742 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
744 buffer0 + buffer0_off,
745 512 - buffer0_off, &s_g1_len));
746 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200747 s_g1_off = buffer0_off;
748 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
750 buffer0 + buffer0_off,
751 512 - buffer0_off, &s_x1_pk_len));
752 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200753 s_x1_pk_off = buffer0_off;
754 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
756 buffer0 + buffer0_off,
757 512 - buffer0_off, &s_x1_pr_len));
758 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200759 s_x1_pr_off = buffer0_off;
760 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
762 buffer0 + buffer0_off,
763 512 - buffer0_off, &s_g2_len));
764 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200765 s_g2_off = buffer0_off;
766 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
768 buffer0 + buffer0_off,
769 512 - buffer0_off, &s_x2_pk_len));
770 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200771 s_x2_pk_off = buffer0_off;
772 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
774 buffer0 + buffer0_off,
775 512 - buffer0_off, &s_x2_pr_len));
776 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200777 s_x2_pr_off = buffer0_off;
778 buffer0_off += s_x2_pr_len;
779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500781 buffer0[s_x1_pr_off + 8] ^= 1;
782 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200783 expected_status = PSA_ERROR_DATA_INVALID;
784 }
785
Neil Armstrong51009d72022-09-05 17:59:54 +0200786 /*
787 * When injecting errors in inputs, the implementation is
788 * free to detect it right away of with a delay.
789 * This permits delaying the error until the end of the input
790 * sequence, if no error appears then, this will be treated
791 * as an error.
792 */
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200795 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
797 buffer0 + s_g1_off, s_g1_len);
798 if (inject_error == 1 && status != PSA_SUCCESS) {
799 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200800 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 } else {
802 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200803 }
804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
806 buffer0 + s_x1_pk_off,
807 s_x1_pk_len);
808 if (inject_error == 1 && status != PSA_SUCCESS) {
809 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200810 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 } else {
812 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200813 }
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
816 buffer0 + s_x1_pr_off,
817 s_x1_pr_len);
818 if (inject_error == 1 && status != PSA_SUCCESS) {
819 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200820 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 } else {
822 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200823 }
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
826 buffer0 + s_g2_off,
827 s_g2_len);
828 if (inject_error == 1 && status != PSA_SUCCESS) {
829 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200830 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 } else {
832 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200833 }
834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
836 buffer0 + s_x2_pk_off,
837 s_x2_pk_len);
838 if (inject_error == 1 && status != PSA_SUCCESS) {
839 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200840 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 } else {
842 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200843 }
844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
846 buffer0 + s_x2_pr_off,
847 s_x2_pr_len);
848 if (inject_error == 1 && status != PSA_SUCCESS) {
849 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200850 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 } else {
852 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200853 }
854
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200855 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (inject_error == 1) {
857 TEST_ASSERT(
858 !"One of the last psa_pake_input() calls should have returned the expected error.");
859 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200860 }
861
862 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
864 buffer1 + buffer1_off,
865 512 - buffer1_off, &c_g1_len));
866 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200867 c_g1_off = buffer1_off;
868 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
870 buffer1 + buffer1_off,
871 512 - buffer1_off, &c_x1_pk_len));
872 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200873 c_x1_pk_off = buffer1_off;
874 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
876 buffer1 + buffer1_off,
877 512 - buffer1_off, &c_x1_pr_len));
878 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200879 c_x1_pr_off = buffer1_off;
880 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
882 buffer1 + buffer1_off,
883 512 - buffer1_off, &c_g2_len));
884 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200885 c_g2_off = buffer1_off;
886 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
888 buffer1 + buffer1_off,
889 512 - buffer1_off, &c_x2_pk_len));
890 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200891 c_x2_pk_off = buffer1_off;
892 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
894 buffer1 + buffer1_off,
895 512 - buffer1_off, &c_x2_pr_len));
896 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200897 c_x2_pr_off = buffer1_off;
898 buffer1_off += c_x2_pr_len;
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200901 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
903 buffer0 + s_g1_off, s_g1_len);
904 if (inject_error == 1 && status != PSA_SUCCESS) {
905 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200906 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 } else {
908 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200909 }
910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
912 buffer0 + s_x1_pk_off,
913 s_x1_pk_len);
914 if (inject_error == 1 && status != PSA_SUCCESS) {
915 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200916 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 } else {
918 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200919 }
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
922 buffer0 + s_x1_pr_off,
923 s_x1_pr_len);
924 if (inject_error == 1 && status != PSA_SUCCESS) {
925 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200926 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 } else {
928 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200929 }
930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
932 buffer0 + s_g2_off,
933 s_g2_len);
934 if (inject_error == 1 && status != PSA_SUCCESS) {
935 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200936 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 } else {
938 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200939 }
940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
942 buffer0 + s_x2_pk_off,
943 s_x2_pk_len);
944 if (inject_error == 1 && status != PSA_SUCCESS) {
945 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200946 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 } else {
948 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200949 }
950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
952 buffer0 + s_x2_pr_off,
953 s_x2_pr_len);
954 if (inject_error == 1 && status != PSA_SUCCESS) {
955 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200956 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 } else {
958 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200959 }
960
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200961 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (inject_error == 1) {
963 TEST_ASSERT(
964 !"One of the last psa_pake_input() calls should have returned the expected error.");
965 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200966 }
967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500969 buffer1[c_x1_pr_off + 12] ^= 1;
970 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200971 expected_status = PSA_ERROR_DATA_INVALID;
972 }
973
974 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
976 buffer1 + c_g1_off, c_g1_len);
977 if (inject_error == 2 && status != PSA_SUCCESS) {
978 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200979 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 } else {
981 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200982 }
983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
985 buffer1 + c_x1_pk_off, c_x1_pk_len);
986 if (inject_error == 2 && status != PSA_SUCCESS) {
987 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200988 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 } else {
990 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200991 }
992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
994 buffer1 + c_x1_pr_off, c_x1_pr_len);
995 if (inject_error == 2 && status != PSA_SUCCESS) {
996 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200997 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 } else {
999 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001000 }
1001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1003 buffer1 + c_g2_off, c_g2_len);
1004 if (inject_error == 2 && status != PSA_SUCCESS) {
1005 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001006 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 } else {
1008 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001009 }
1010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1012 buffer1 + c_x2_pk_off, c_x2_pk_len);
1013 if (inject_error == 2 && status != PSA_SUCCESS) {
1014 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001015 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 } else {
1017 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001018 }
1019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1021 buffer1 + c_x2_pr_off, c_x2_pr_len);
1022 if (inject_error == 2 && status != PSA_SUCCESS) {
1023 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001024 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 } else {
1026 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001027 }
1028
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001029 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (inject_error == 2) {
1031 TEST_ASSERT(
1032 !"One of the last psa_pake_input() calls should have returned the expected error.");
1033 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001034
1035 break;
1036
1037 case 2:
1038 /* Server second round Output */
1039 buffer0_off = 0;
1040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1042 buffer0 + buffer0_off,
1043 512 - buffer0_off, &s_a_len));
1044 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001045 s_a_off = buffer0_off;
1046 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1048 buffer0 + buffer0_off,
1049 512 - buffer0_off, &s_x2s_pk_len));
1050 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001051 s_x2s_pk_off = buffer0_off;
1052 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1054 buffer0 + buffer0_off,
1055 512 - buffer0_off, &s_x2s_pr_len));
1056 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001057 s_x2s_pr_off = buffer0_off;
1058 buffer0_off += s_x2s_pr_len;
1059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001061 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001062 expected_status = PSA_ERROR_DATA_INVALID;
1063 }
1064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001066 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1068 buffer0 + s_a_off, s_a_len);
1069 if (inject_error == 3 && status != PSA_SUCCESS) {
1070 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001071 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 } else {
1073 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001074 }
1075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1077 buffer0 + s_x2s_pk_off,
1078 s_x2s_pk_len);
1079 if (inject_error == 3 && status != PSA_SUCCESS) {
1080 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001081 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 } else {
1083 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001084 }
1085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1087 buffer0 + s_x2s_pr_off,
1088 s_x2s_pr_len);
1089 if (inject_error == 3 && status != PSA_SUCCESS) {
1090 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001091 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 } else {
1093 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001094 }
1095
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001096 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (inject_error == 3) {
1098 TEST_ASSERT(
1099 !"One of the last psa_pake_input() calls should have returned the expected error.");
1100 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001101 }
1102
1103 /* Client second round Output */
1104 buffer1_off = 0;
1105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1107 buffer1 + buffer1_off,
1108 512 - buffer1_off, &c_a_len));
1109 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001110 c_a_off = buffer1_off;
1111 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1113 buffer1 + buffer1_off,
1114 512 - buffer1_off, &c_x2s_pk_len));
1115 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001116 c_x2s_pk_off = buffer1_off;
1117 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1119 buffer1 + buffer1_off,
1120 512 - buffer1_off, &c_x2s_pr_len));
1121 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001122 c_x2s_pr_off = buffer1_off;
1123 buffer1_off += c_x2s_pr_len;
1124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001126 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1128 buffer0 + s_a_off, s_a_len);
1129 if (inject_error == 3 && status != PSA_SUCCESS) {
1130 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001131 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 } else {
1133 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001134 }
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1137 buffer0 + s_x2s_pk_off,
1138 s_x2s_pk_len);
1139 if (inject_error == 3 && status != PSA_SUCCESS) {
1140 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001141 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 } else {
1143 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001144 }
1145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1147 buffer0 + s_x2s_pr_off,
1148 s_x2s_pr_len);
1149 if (inject_error == 3 && status != PSA_SUCCESS) {
1150 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001151 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 } else {
1153 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001154 }
1155
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001156 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (inject_error == 3) {
1158 TEST_ASSERT(
1159 !"One of the last psa_pake_input() calls should have returned the expected error.");
1160 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001161 }
1162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001164 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001165 expected_status = PSA_ERROR_DATA_INVALID;
1166 }
1167
1168 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1170 buffer1 + c_a_off, c_a_len);
1171 if (inject_error == 4 && status != PSA_SUCCESS) {
1172 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001173 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 } else {
1175 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001176 }
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1179 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1180 if (inject_error == 4 && status != PSA_SUCCESS) {
1181 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001182 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 } else {
1184 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001185 }
1186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1188 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1189 if (inject_error == 4 && status != PSA_SUCCESS) {
1190 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001191 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 } else {
1193 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001194 }
1195
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001196 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if (inject_error == 4) {
1198 TEST_ASSERT(
1199 !"One of the last psa_pake_input() calls should have returned the expected error.");
1200 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001201
1202 break;
1203
1204 }
1205
Neil Armstrongf983caf2022-06-15 15:27:48 +02001206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 mbedtls_free(buffer0);
1208 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001209}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001210#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001213 INJECT_ERR_NONE = 0,
1214 INJECT_ERR_UNINITIALIZED_ACCESS,
1215 INJECT_ERR_DUPLICATE_SETUP,
1216 INJECT_ERR_INVALID_USER,
1217 INJECT_ERR_INVALID_PEER,
1218 INJECT_ERR_SET_USER,
1219 INJECT_ERR_SET_PEER,
1220 INJECT_EMPTY_IO_BUFFER,
1221 INJECT_UNKNOWN_STEP,
1222 INJECT_INVALID_FIRST_STEP,
1223 INJECT_WRONG_BUFFER_SIZE,
1224 INJECT_VALID_OPERATION_AFTER_FAILURE,
1225 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1226 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1227} ecjpake_injected_failure_t;
1228
Paul Elliott01885fa2023-02-09 12:07:30 +00001229#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001230
Paul Elliott6f600372023-02-06 18:41:05 +00001231static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1232 psa_status_t expected_status,
1233 size_t *min_completes,
1234 size_t *max_completes)
1235{
1236
1237 /* This is slightly contrived, but we only really know that with a minimum
1238 value of max_ops that a successful operation should take more than one op
1239 to complete, and likewise that with a max_ops of
1240 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1241 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001242
Paul Elliott6f600372023-02-06 18:41:05 +00001243 if (expected_status == PSA_SUCCESS) {
1244 *min_completes = 2;
1245 } else {
1246 *min_completes = 1;
1247 }
1248
1249 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1250 } else {
1251 *min_completes = 1;
1252 *max_completes = 1;
1253 }
1254}
Paul Elliott01885fa2023-02-09 12:07:30 +00001255#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001256
Gilles Peskinee59236f2018-01-27 23:32:46 +01001257/* END_HEADER */
1258
1259/* BEGIN_DEPENDENCIES
1260 * depends_on:MBEDTLS_PSA_CRYPTO_C
1261 * END_DEPENDENCIES
1262 */
1263
1264/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001265void psa_can_do_hash()
1266{
1267 /* We can't test that this is specific to drivers until partial init has
1268 * been implemented, but we can at least test before/after full init. */
1269 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1270 PSA_INIT();
1271 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1272 PSA_DONE();
1273}
1274/* END_CASE */
1275
1276/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001278{
1279 size_t max_truncated_mac_size =
1280 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1281
1282 /* Check that the length for a truncated MAC always fits in the algorithm
1283 * encoding. The shifted mask is the maximum truncated value. The
1284 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001286}
1287/* END_CASE */
1288
1289/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001290void import_with_policy(int type_arg,
1291 int usage_arg, int alg_arg,
1292 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001293{
1294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1295 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001296 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001297 psa_key_type_t type = type_arg;
1298 psa_key_usage_t usage = usage_arg;
1299 psa_algorithm_t alg = alg_arg;
1300 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001302 psa_status_t status;
1303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 psa_set_key_type(&attributes, type);
1307 psa_set_key_usage_flags(&attributes, usage);
1308 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 status = psa_import_key(&attributes,
1311 key_material, sizeof(key_material),
1312 &key);
1313 TEST_EQUAL(status, expected_status);
1314 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001315 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1319 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1320 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1321 mbedtls_test_update_key_usage_flags(usage));
1322 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1323 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 PSA_ASSERT(psa_destroy_key(key));
1326 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001327
1328exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001329 /*
1330 * Key attributes may have been returned by psa_get_key_attributes()
1331 * thus reset them as required.
1332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 psa_destroy_key(key);
1336 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001337}
1338/* END_CASE */
1339
1340/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341void import_with_data(data_t *data, int type_arg,
1342 int attr_bits_arg,
1343 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001344{
1345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1346 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001348 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001349 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001350 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001351 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 psa_set_key_type(&attributes, type);
1356 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001359 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1360 *
1361 * This can happen with a type supported only by a driver:
1362 * - the driver sees the invalid data (for example wrong size) and thinks
1363 * "well perhaps this is a key size I don't support" so it returns
1364 * NOT_SUPPORTED which is correct at this point;
1365 * - we fallback to built-ins, which don't support this type, so return
1366 * NOT_SUPPORTED which again is correct at this point.
1367 */
1368 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1369 status == PSA_ERROR_NOT_SUPPORTED) {
1370 ; // OK
1371 } else {
1372 TEST_EQUAL(status, expected_status);
1373 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001375 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1379 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1380 if (attr_bits != 0) {
1381 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1382 }
1383 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 PSA_ASSERT(psa_destroy_key(key));
1386 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001387
1388exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001389 /*
1390 * Key attributes may have been returned by psa_get_key_attributes()
1391 * thus reset them as required.
1392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 psa_destroy_key(key);
1396 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001397}
1398/* END_CASE */
1399
1400/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001401/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402void import_large_key(int type_arg, int byte_size_arg,
1403 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001404{
1405 psa_key_type_t type = type_arg;
1406 size_t byte_size = byte_size_arg;
1407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1408 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001410 psa_status_t status;
1411 uint8_t *buffer = NULL;
1412 size_t buffer_size = byte_size + 1;
1413 size_t n;
1414
Steven Cooreman69967ce2021-01-18 18:01:08 +01001415 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001416 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001417 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001421
1422 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1424 psa_set_key_type(&attributes, type);
1425 status = psa_import_key(&attributes, buffer, byte_size, &key);
1426 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1427 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if (status == PSA_SUCCESS) {
1430 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1431 TEST_EQUAL(psa_get_key_type(&attributes), type);
1432 TEST_EQUAL(psa_get_key_bits(&attributes),
1433 PSA_BYTES_TO_BITS(byte_size));
1434 ASSERT_NO_SLOT_NUMBER(&attributes);
1435 memset(buffer, 0, byte_size + 1);
1436 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1437 for (n = 0; n < byte_size; n++) {
1438 TEST_EQUAL(buffer[n], 'K');
1439 }
1440 for (n = byte_size; n < buffer_size; n++) {
1441 TEST_EQUAL(buffer[n], 0);
1442 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001443 }
1444
1445exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001446 /*
1447 * Key attributes may have been returned by psa_get_key_attributes()
1448 * thus reset them as required.
1449 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 psa_destroy_key(key);
1453 PSA_DONE();
1454 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001455}
1456/* END_CASE */
1457
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001458/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001459/* Import an RSA key with a valid structure (but not valid numbers
1460 * inside, beyond having sensible size and parity). This is expected to
1461 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001463{
Ronald Cron5425a212020-08-04 14:58:35 +02001464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001465 size_t bits = bits_arg;
1466 psa_status_t expected_status = expected_status_arg;
1467 psa_status_t status;
1468 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001469 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001470 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001472 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001473 unsigned char *p;
1474 int ret;
1475 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001479 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1482 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001483 length = ret;
1484
1485 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 psa_set_key_type(&attributes, type);
1487 status = psa_import_key(&attributes, p, length, &key);
1488 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (status == PSA_SUCCESS) {
1491 PSA_ASSERT(psa_destroy_key(key));
1492 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001493
1494exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 mbedtls_free(buffer);
1496 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001497}
1498/* END_CASE */
1499
1500/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001501void import_export(data_t *data,
1502 int type_arg,
1503 int usage_arg, int alg_arg,
1504 int lifetime_arg,
1505 int expected_bits,
1506 int export_size_delta,
1507 int expected_export_status_arg,
1508 /*whether reexport must give the original input exactly*/
1509 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001510{
Ronald Cron5425a212020-08-04 14:58:35 +02001511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001512 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001513 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001514 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001515 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301516 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001517 unsigned char *exported = NULL;
1518 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001519 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001520 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001521 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001522 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001523 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524
Moran Pekercb088e72018-07-17 17:36:59 +03001525 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001526 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001528 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 }
1530 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 psa_set_key_lifetime(&attributes, lifetime);
1533 psa_set_key_usage_flags(&attributes, usage_arg);
1534 psa_set_key_algorithm(&attributes, alg);
1535 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001536
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001537 if (PSA_KEY_TYPE_IS_DH(type) &&
1538 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001539 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1540 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001541 }
1542
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001543 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001544 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001545 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001546
1547 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1549 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1550 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1551 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001552
1553 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 status = psa_export_key(key, exported, export_size, &exported_length);
1555 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001556
1557 /* The exported length must be set by psa_export_key() to a value between 0
1558 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1560 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1561 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1564 export_size - exported_length));
1565 if (status != PSA_SUCCESS) {
1566 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001568 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001569
Gilles Peskineea38a922021-02-13 00:05:16 +01001570 /* Run sanity checks on the exported key. For non-canonical inputs,
1571 * this validates the canonical representations. For canonical inputs,
1572 * this doesn't directly validate the implementation, but it still helps
1573 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (!psa_key_lifetime_is_external(lifetime)) {
1575 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301576 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 }
Archana4d7ae1d2021-07-07 02:50:22 +05301578 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001581 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001583 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1585 &key2));
1586 PSA_ASSERT(psa_export_key(key2,
1587 reexported,
1588 export_size,
1589 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001590 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001591 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001593 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 TEST_LE_U(exported_length,
1595 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1596 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001597 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1598 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1599 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1600 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1601 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001602
1603destroy:
1604 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 PSA_ASSERT(psa_destroy_key(key));
1606 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001607
1608exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001609 /*
1610 * Key attributes may have been returned by psa_get_key_attributes()
1611 * thus reset them as required.
1612 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 psa_reset_key_attributes(&got_attributes);
1614 psa_destroy_key(key);
1615 mbedtls_free(exported);
1616 mbedtls_free(reexported);
1617 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001618}
1619/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001620
Moran Pekerf709f4a2018-06-06 17:26:04 +03001621/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001622void import_export_public_key(data_t *data,
1623 int type_arg, // key pair or public key
1624 int alg_arg,
1625 int lifetime_arg,
1626 int export_size_delta,
1627 int expected_export_status_arg,
1628 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001629{
Ronald Cron5425a212020-08-04 14:58:35 +02001630 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001631 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001632 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001633 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001634 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301635 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001636 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001637 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001638 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001639 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 psa_set_key_lifetime(&attributes, lifetime);
1644 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1645 psa_set_key_algorithm(&attributes, alg);
1646 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001647
1648 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001650
Gilles Peskine49c25912018-10-29 15:15:31 +01001651 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001652 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 status = psa_export_public_key(key,
1654 exported, export_size,
1655 &exported_length);
1656 TEST_EQUAL(status, expected_export_status);
1657 if (status == PSA_SUCCESS) {
1658 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001659 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1661 bits = psa_get_key_bits(&attributes);
1662 TEST_LE_U(expected_public_key->len,
1663 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1664 TEST_LE_U(expected_public_key->len,
1665 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1666 TEST_LE_U(expected_public_key->len,
1667 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001668 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001669 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001670 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001671exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001672 /*
1673 * Key attributes may have been returned by psa_get_key_attributes()
1674 * thus reset them as required.
1675 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 mbedtls_free(exported);
1679 psa_destroy_key(key);
1680 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001681}
1682/* END_CASE */
1683
Gilles Peskine20035e32018-02-03 22:44:14 +01001684/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001685void import_and_exercise_key(data_t *data,
1686 int type_arg,
1687 int bits_arg,
1688 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001689{
Ronald Cron5425a212020-08-04 14:58:35 +02001690 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001691 psa_key_type_t type = type_arg;
1692 size_t bits = bits_arg;
1693 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001696 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 psa_set_key_usage_flags(&attributes, usage);
1701 psa_set_key_algorithm(&attributes, alg);
1702 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001703
1704 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001706
1707 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1709 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1710 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001711
1712 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001714 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 PSA_ASSERT(psa_destroy_key(key));
1718 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001719
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001720exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001721 /*
1722 * Key attributes may have been returned by psa_get_key_attributes()
1723 * thus reset them as required.
1724 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 psa_reset_key_attributes(&attributes);
1728 psa_destroy_key(key);
1729 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001730}
1731/* END_CASE */
1732
1733/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734void effective_key_attributes(int type_arg, int expected_type_arg,
1735 int bits_arg, int expected_bits_arg,
1736 int usage_arg, int expected_usage_arg,
1737 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001738{
Ronald Cron5425a212020-08-04 14:58:35 +02001739 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001740 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001741 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001742 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001743 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001744 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001745 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001746 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001747 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 psa_set_key_usage_flags(&attributes, usage);
1753 psa_set_key_algorithm(&attributes, alg);
1754 psa_set_key_type(&attributes, key_type);
1755 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 PSA_ASSERT(psa_generate_key(&attributes, &key));
1758 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1761 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1762 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1763 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1764 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001765
1766exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001767 /*
1768 * Key attributes may have been returned by psa_get_key_attributes()
1769 * thus reset them as required.
1770 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 psa_destroy_key(key);
1774 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001775}
1776/* END_CASE */
1777
1778/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001779void check_key_policy(int type_arg, int bits_arg,
1780 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001781{
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1783 usage_arg,
1784 mbedtls_test_update_key_usage_flags(usage_arg),
1785 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001786 goto exit;
1787}
1788/* END_CASE */
1789
1790/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001792{
1793 /* Test each valid way of initializing the object, except for `= {0}`, as
1794 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1795 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001796 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001798 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1799 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1804 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1805 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 TEST_EQUAL(psa_get_key_type(&func), 0);
1808 TEST_EQUAL(psa_get_key_type(&init), 0);
1809 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001810
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 TEST_EQUAL(psa_get_key_bits(&func), 0);
1812 TEST_EQUAL(psa_get_key_bits(&init), 0);
1813 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1816 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1817 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1820 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1821 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001822}
1823/* END_CASE */
1824
1825/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826void mac_key_policy(int policy_usage_arg,
1827 int policy_alg_arg,
1828 int key_type_arg,
1829 data_t *key_data,
1830 int exercise_alg_arg,
1831 int expected_status_sign_arg,
1832 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001833{
Ronald Cron5425a212020-08-04 14:58:35 +02001834 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001835 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001836 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001837 psa_key_type_t key_type = key_type_arg;
1838 psa_algorithm_t policy_alg = policy_alg_arg;
1839 psa_algorithm_t exercise_alg = exercise_alg_arg;
1840 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001841 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001842 psa_status_t expected_status_sign = expected_status_sign_arg;
1843 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001844 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 psa_set_key_usage_flags(&attributes, policy_usage);
1849 psa_set_key_algorithm(&attributes, policy_alg);
1850 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1853 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1856 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1859 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001860
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001861 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001863 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1865 input, 128,
1866 mac, PSA_MAC_MAX_SIZE, &mac_len),
1867 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001868
Neil Armstrong3af9b972022-02-07 12:20:21 +01001869 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 PSA_ASSERT(psa_mac_abort(&operation));
1871 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1872 if (status == PSA_SUCCESS) {
1873 status = psa_mac_update(&operation, input, 128);
1874 if (status == PSA_SUCCESS) {
1875 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1876 &mac_len),
1877 expected_status_sign);
1878 } else {
1879 TEST_EQUAL(status, expected_status_sign);
1880 }
1881 } else {
1882 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001883 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001885
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001886 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 status = psa_mac_verify(key, exercise_alg, input, 128,
1888 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1891 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1892 } else {
1893 TEST_EQUAL(status, expected_status_verify);
1894 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001895
Neil Armstrong3af9b972022-02-07 12:20:21 +01001896 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1898 if (status == PSA_SUCCESS) {
1899 status = psa_mac_update(&operation, input, 128);
1900 if (status == PSA_SUCCESS) {
1901 status = psa_mac_verify_finish(&operation, mac, mac_len);
1902 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1903 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1904 } else {
1905 TEST_EQUAL(status, expected_status_verify);
1906 }
1907 } else {
1908 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001909 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 } else {
1911 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001912 }
1913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 memset(mac, 0, sizeof(mac));
1917 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1918 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001919
1920exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 psa_mac_abort(&operation);
1922 psa_destroy_key(key);
1923 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924}
1925/* END_CASE */
1926
1927/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928void cipher_key_policy(int policy_usage_arg,
1929 int policy_alg,
1930 int key_type,
1931 data_t *key_data,
1932 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001933{
Ronald Cron5425a212020-08-04 14:58:35 +02001934 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001935 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001936 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001937 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001938 size_t output_buffer_size = 0;
1939 size_t input_buffer_size = 0;
1940 size_t output_length = 0;
1941 uint8_t *output = NULL;
1942 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001943 psa_status_t status;
1944
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1946 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1947 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001948
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001949 TEST_CALLOC(input, input_buffer_size);
1950 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 psa_set_key_usage_flags(&attributes, policy_usage);
1955 psa_set_key_algorithm(&attributes, policy_alg);
1956 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1959 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001960
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001961 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 TEST_EQUAL(policy_usage,
1963 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001964
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001965 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1967 output, output_buffer_size,
1968 &output_length);
1969 if (policy_alg == exercise_alg &&
1970 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1971 PSA_ASSERT(status);
1972 } else {
1973 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1974 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001975
1976 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1978 if (policy_alg == exercise_alg &&
1979 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1980 PSA_ASSERT(status);
1981 } else {
1982 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1983 }
1984 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001986 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1988 input, input_buffer_size,
1989 &output_length);
1990 if (policy_alg == exercise_alg &&
1991 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1992 PSA_ASSERT(status);
1993 } else {
1994 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1995 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001996
1997 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1999 if (policy_alg == exercise_alg &&
2000 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2001 PSA_ASSERT(status);
2002 } else {
2003 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2004 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002005
2006exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 psa_cipher_abort(&operation);
2008 mbedtls_free(input);
2009 mbedtls_free(output);
2010 psa_destroy_key(key);
2011 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002012}
2013/* END_CASE */
2014
2015/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002016void aead_key_policy(int policy_usage_arg,
2017 int policy_alg,
2018 int key_type,
2019 data_t *key_data,
2020 int nonce_length_arg,
2021 int tag_length_arg,
2022 int exercise_alg,
2023 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024{
Ronald Cron5425a212020-08-04 14:58:35 +02002025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002027 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002028 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002029 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002030 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032 size_t nonce_length = nonce_length_arg;
2033 unsigned char tag[16];
2034 size_t tag_length = tag_length_arg;
2035 size_t output_length;
2036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 TEST_LE_U(nonce_length, sizeof(nonce));
2038 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 psa_set_key_usage_flags(&attributes, policy_usage);
2043 psa_set_key_algorithm(&attributes, policy_alg);
2044 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2047 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002048
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002049 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 TEST_EQUAL(policy_usage,
2051 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002052
Neil Armstrong752d8112022-02-07 14:51:11 +01002053 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 status = psa_aead_encrypt(key, exercise_alg,
2055 nonce, nonce_length,
2056 NULL, 0,
2057 NULL, 0,
2058 tag, tag_length,
2059 &output_length);
2060 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2061 TEST_EQUAL(status, expected_status);
2062 } else {
2063 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2064 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002065
Neil Armstrong752d8112022-02-07 14:51:11 +01002066 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2068 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2069 TEST_EQUAL(status, expected_status);
2070 } else {
2071 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2072 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002073
2074 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 memset(tag, 0, sizeof(tag));
2076 status = psa_aead_decrypt(key, exercise_alg,
2077 nonce, nonce_length,
2078 NULL, 0,
2079 tag, tag_length,
2080 NULL, 0,
2081 &output_length);
2082 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2083 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2084 } else if (expected_status == PSA_SUCCESS) {
2085 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2086 } else {
2087 TEST_EQUAL(status, expected_status);
2088 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002089
Neil Armstrong752d8112022-02-07 14:51:11 +01002090 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 PSA_ASSERT(psa_aead_abort(&operation));
2092 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2093 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2094 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2095 } else {
2096 TEST_EQUAL(status, expected_status);
2097 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002098
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 PSA_ASSERT(psa_aead_abort(&operation));
2101 psa_destroy_key(key);
2102 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103}
2104/* END_CASE */
2105
2106/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002107void asymmetric_encryption_key_policy(int policy_usage_arg,
2108 int policy_alg,
2109 int key_type,
2110 data_t *key_data,
Valerio Settif202c292024-01-15 10:42:37 +01002111 int exercise_alg,
2112 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113{
Ronald Cron5425a212020-08-04 14:58:35 +02002114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002116 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002117 psa_status_t status;
2118 size_t key_bits;
2119 size_t buffer_length;
2120 unsigned char *buffer = NULL;
2121 size_t output_length;
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 psa_set_key_usage_flags(&attributes, policy_usage);
2126 psa_set_key_algorithm(&attributes, policy_alg);
2127 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
Valerio Settif202c292024-01-15 10:42:37 +01002129 if (use_opaque_key) {
2130 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2131 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2132 }
2133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2135 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002136
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002137 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 TEST_EQUAL(policy_usage,
2139 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2142 key_bits = psa_get_key_bits(&attributes);
2143 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2144 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002145 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 status = psa_asymmetric_encrypt(key, exercise_alg,
2148 NULL, 0,
2149 NULL, 0,
2150 buffer, buffer_length,
2151 &output_length);
2152 if (policy_alg == exercise_alg &&
2153 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2154 PSA_ASSERT(status);
2155 } else {
2156 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2157 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (buffer_length != 0) {
2160 memset(buffer, 0, buffer_length);
2161 }
2162 status = psa_asymmetric_decrypt(key, exercise_alg,
2163 buffer, buffer_length,
2164 NULL, 0,
2165 buffer, buffer_length,
2166 &output_length);
2167 if (policy_alg == exercise_alg &&
2168 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2169 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2170 } else {
2171 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2172 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002173
2174exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002175 /*
2176 * Key attributes may have been returned by psa_get_key_attributes()
2177 * thus reset them as required.
2178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 psa_destroy_key(key);
2182 PSA_DONE();
2183 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002184}
2185/* END_CASE */
2186
2187/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002188void asymmetric_signature_key_policy(int policy_usage_arg,
2189 int policy_alg,
2190 int key_type,
2191 data_t *key_data,
2192 int exercise_alg,
2193 int payload_length_arg,
2194 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002195{
Ronald Cron5425a212020-08-04 14:58:35 +02002196 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002198 psa_key_usage_t policy_usage = policy_usage_arg;
2199 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002200 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002202 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2203 * compatible with the policy and `payload_length_arg` is supposed to be
2204 * a valid input length to sign. If `payload_length_arg <= 0`,
2205 * `exercise_alg` is supposed to be forbidden by the policy. */
2206 int compatible_alg = payload_length_arg > 0;
2207 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002209 size_t signature_length;
2210
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002211 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002212 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 TEST_EQUAL(expected_usage,
2214 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 psa_set_key_usage_flags(&attributes, policy_usage);
2219 psa_set_key_algorithm(&attributes, policy_alg);
2220 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2223 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 status = psa_sign_hash(key, exercise_alg,
2228 payload, payload_length,
2229 signature, sizeof(signature),
2230 &signature_length);
2231 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2232 PSA_ASSERT(status);
2233 } else {
2234 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2235 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 memset(signature, 0, sizeof(signature));
2238 status = psa_verify_hash(key, exercise_alg,
2239 payload, payload_length,
2240 signature, sizeof(signature));
2241 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2242 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2243 } else {
2244 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2245 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2248 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2249 status = psa_sign_message(key, exercise_alg,
2250 payload, payload_length,
2251 signature, sizeof(signature),
2252 &signature_length);
2253 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2254 PSA_ASSERT(status);
2255 } else {
2256 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2257 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 memset(signature, 0, sizeof(signature));
2260 status = psa_verify_message(key, exercise_alg,
2261 payload, payload_length,
2262 signature, sizeof(signature));
2263 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2264 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2265 } else {
2266 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2267 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002268 }
2269
Gilles Peskined5b33222018-06-18 22:20:03 +02002270exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 psa_destroy_key(key);
2272 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002273}
2274/* END_CASE */
2275
Janos Follathba3fab92019-06-11 14:50:16 +01002276/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002277void derive_key_policy(int policy_usage,
2278 int policy_alg,
2279 int key_type,
2280 data_t *key_data,
2281 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002282{
Ronald Cron5425a212020-08-04 14:58:35 +02002283 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002284 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002285 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002286 psa_status_t status;
2287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 psa_set_key_usage_flags(&attributes, policy_usage);
2291 psa_set_key_algorithm(&attributes, policy_alg);
2292 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2295 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2300 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2301 PSA_ASSERT(psa_key_derivation_input_bytes(
2302 &operation,
2303 PSA_KEY_DERIVATION_INPUT_SEED,
2304 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002305 }
Janos Follathba3fab92019-06-11 14:50:16 +01002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 status = psa_key_derivation_input_key(&operation,
2308 PSA_KEY_DERIVATION_INPUT_SECRET,
2309 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if (policy_alg == exercise_alg &&
2312 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2313 PSA_ASSERT(status);
2314 } else {
2315 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2316 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002317
2318exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 psa_key_derivation_abort(&operation);
2320 psa_destroy_key(key);
2321 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002322}
2323/* END_CASE */
2324
2325/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326void agreement_key_policy(int policy_usage,
2327 int policy_alg,
2328 int key_type_arg,
2329 data_t *key_data,
2330 int exercise_alg,
2331 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002332{
Ronald Cron5425a212020-08-04 14:58:35 +02002333 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002335 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002336 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002337 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002338 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 psa_set_key_usage_flags(&attributes, policy_usage);
2343 psa_set_key_algorithm(&attributes, policy_alg);
2344 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2347 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2350 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002353
2354exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 psa_key_derivation_abort(&operation);
2356 psa_destroy_key(key);
2357 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002358}
2359/* END_CASE */
2360
2361/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362void key_policy_alg2(int key_type_arg, data_t *key_data,
2363 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002364{
Ronald Cron5425a212020-08-04 14:58:35 +02002365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002366 psa_key_type_t key_type = key_type_arg;
2367 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2368 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2369 psa_key_usage_t usage = usage_arg;
2370 psa_algorithm_t alg = alg_arg;
2371 psa_algorithm_t alg2 = alg2_arg;
2372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 psa_set_key_usage_flags(&attributes, usage);
2376 psa_set_key_algorithm(&attributes, alg);
2377 psa_set_key_enrollment_algorithm(&attributes, alg2);
2378 psa_set_key_type(&attributes, key_type);
2379 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2380 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002381
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002382 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 usage = mbedtls_test_update_key_usage_flags(usage);
2384 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2385 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2386 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2387 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002390 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 }
2392 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002393 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002395
2396exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002397 /*
2398 * Key attributes may have been returned by psa_get_key_attributes()
2399 * thus reset them as required.
2400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 psa_destroy_key(key);
2404 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002405}
2406/* END_CASE */
2407
2408/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002409void raw_agreement_key_policy(int policy_usage,
2410 int policy_alg,
2411 int key_type_arg,
2412 data_t *key_data,
2413 int exercise_alg,
2414 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002415{
Ronald Cron5425a212020-08-04 14:58:35 +02002416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002418 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002419 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002420 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002421 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 psa_set_key_usage_flags(&attributes, policy_usage);
2426 psa_set_key_algorithm(&attributes, policy_alg);
2427 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2430 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002435
2436exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 psa_key_derivation_abort(&operation);
2438 psa_destroy_key(key);
2439 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002440}
2441/* END_CASE */
2442
2443/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002444void copy_success(int source_usage_arg,
2445 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002446 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 int type_arg, data_t *material,
2448 int copy_attributes,
2449 int target_usage_arg,
2450 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002451 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 int expected_usage_arg,
2453 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002454{
Gilles Peskineca25db92019-04-19 11:43:08 +02002455 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2456 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002457 psa_key_usage_t expected_usage = expected_usage_arg;
2458 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002459 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302460 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2461 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002462 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2463 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002464 uint8_t *export_buffer = NULL;
2465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002467
Gilles Peskineca25db92019-04-19 11:43:08 +02002468 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2470 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2471 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2472 psa_set_key_type(&source_attributes, type_arg);
2473 psa_set_key_lifetime(&source_attributes, source_lifetime);
2474 PSA_ASSERT(psa_import_key(&source_attributes,
2475 material->x, material->len,
2476 &source_key));
2477 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002478
Gilles Peskineca25db92019-04-19 11:43:08 +02002479 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002481 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002482 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (target_usage_arg != -1) {
2486 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2487 }
2488 if (target_alg_arg != -1) {
2489 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2490 }
2491 if (target_alg2_arg != -1) {
2492 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2493 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002494
Archana8a180362021-07-05 02:18:48 +05302495
Gilles Peskine57ab7212019-01-28 13:03:09 +01002496 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 PSA_ASSERT(psa_copy_key(source_key,
2498 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002499
2500 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002502
2503 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2505 TEST_EQUAL(psa_get_key_type(&source_attributes),
2506 psa_get_key_type(&target_attributes));
2507 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2508 psa_get_key_bits(&target_attributes));
2509 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2510 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2511 TEST_EQUAL(expected_alg2,
2512 psa_get_key_enrollment_algorithm(&target_attributes));
2513 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002514 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002515 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2517 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002518 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002519 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002520 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (!psa_key_lifetime_is_external(target_lifetime)) {
2523 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302524 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 }
2526 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302527 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 }
Archana8a180362021-07-05 02:18:48 +05302529 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002532
2533exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002534 /*
2535 * Source and target key attributes may have been returned by
2536 * psa_get_key_attributes() thus reset them as required.
2537 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 psa_reset_key_attributes(&source_attributes);
2539 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 PSA_DONE();
2542 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002543}
2544/* END_CASE */
2545
2546/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002547void copy_fail(int source_usage_arg,
2548 int source_alg_arg, int source_alg2_arg,
2549 int source_lifetime_arg,
2550 int type_arg, data_t *material,
2551 int target_type_arg, int target_bits_arg,
2552 int target_usage_arg,
2553 int target_alg_arg, int target_alg2_arg,
2554 int target_id_arg, int target_lifetime_arg,
2555 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002556{
2557 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2558 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002559 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2560 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002564
2565 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2567 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2568 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2569 psa_set_key_type(&source_attributes, type_arg);
2570 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2571 PSA_ASSERT(psa_import_key(&source_attributes,
2572 material->x, material->len,
2573 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002574
2575 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 psa_set_key_id(&target_attributes, key_id);
2577 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2578 psa_set_key_type(&target_attributes, target_type_arg);
2579 psa_set_key_bits(&target_attributes, target_bits_arg);
2580 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2581 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2582 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002583
2584 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 TEST_EQUAL(psa_copy_key(source_key,
2586 &target_attributes, &target_key),
2587 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002590
Gilles Peskine4a644642019-05-03 17:14:08 +02002591exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 psa_reset_key_attributes(&source_attributes);
2593 psa_reset_key_attributes(&target_attributes);
2594 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002595}
2596/* END_CASE */
2597
2598/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002600{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002601 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002602 /* Test each valid way of initializing the object, except for `= {0}`, as
2603 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2604 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002605 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002607 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2608 psa_hash_operation_t zero;
2609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002611
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002612 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2614 PSA_ERROR_BAD_STATE);
2615 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2616 PSA_ERROR_BAD_STATE);
2617 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2618 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002619
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002620 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 PSA_ASSERT(psa_hash_abort(&func));
2622 PSA_ASSERT(psa_hash_abort(&init));
2623 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002624}
2625/* END_CASE */
2626
2627/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002628void hash_setup(int alg_arg,
2629 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002630{
2631 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002632 uint8_t *output = NULL;
2633 size_t output_size = 0;
2634 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002635 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002636 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002637 psa_status_t status;
2638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002640
Neil Armstrongedb20862022-02-07 15:47:44 +01002641 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002643 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 status = psa_hash_compute(alg, NULL, 0,
2646 output, output_size, &output_length);
2647 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002648
2649 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 status = psa_hash_setup(&operation, alg);
2651 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002652
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002653 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002655
2656 /* If setup failed, reproduce the failure, so as to
2657 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (status != PSA_SUCCESS) {
2659 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2660 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002661
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002662 /* Now the operation object should be reusable. */
2663#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2665 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002666#endif
2667
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002668exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 mbedtls_free(output);
2670 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002671}
2672/* END_CASE */
2673
2674/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002675void hash_compute_fail(int alg_arg, data_t *input,
2676 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002677{
2678 psa_algorithm_t alg = alg_arg;
2679 uint8_t *output = NULL;
2680 size_t output_size = output_size_arg;
2681 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002682 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002683 psa_status_t expected_status = expected_status_arg;
2684 psa_status_t status;
2685
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002686 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002689
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002690 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 status = psa_hash_compute(alg, input->x, input->len,
2692 output, output_size, &output_length);
2693 TEST_EQUAL(status, expected_status);
2694 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002695
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002696 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 status = psa_hash_setup(&operation, alg);
2698 if (status == PSA_SUCCESS) {
2699 status = psa_hash_update(&operation, input->x, input->len);
2700 if (status == PSA_SUCCESS) {
2701 status = psa_hash_finish(&operation, output, output_size,
2702 &output_length);
2703 if (status == PSA_SUCCESS) {
2704 TEST_LE_U(output_length, output_size);
2705 } else {
2706 TEST_EQUAL(status, expected_status);
2707 }
2708 } else {
2709 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002710 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 } else {
2712 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002713 }
2714
Gilles Peskine0a749c82019-11-28 19:33:58 +01002715exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 PSA_ASSERT(psa_hash_abort(&operation));
2717 mbedtls_free(output);
2718 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002719}
2720/* END_CASE */
2721
2722/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002723void hash_compare_fail(int alg_arg, data_t *input,
2724 data_t *reference_hash,
2725 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002726{
2727 psa_algorithm_t alg = alg_arg;
2728 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002729 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002730 psa_status_t status;
2731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002733
Neil Armstrong55a1be12022-02-07 11:23:20 +01002734 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 status = psa_hash_compare(alg, input->x, input->len,
2736 reference_hash->x, reference_hash->len);
2737 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002738
Neil Armstrong55a1be12022-02-07 11:23:20 +01002739 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 status = psa_hash_setup(&operation, alg);
2741 if (status == PSA_SUCCESS) {
2742 status = psa_hash_update(&operation, input->x, input->len);
2743 if (status == PSA_SUCCESS) {
2744 status = psa_hash_verify(&operation, reference_hash->x,
2745 reference_hash->len);
2746 TEST_EQUAL(status, expected_status);
2747 } else {
2748 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002749 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 } else {
2751 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002752 }
2753
Gilles Peskine88e08462020-01-28 20:43:00 +01002754exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 PSA_ASSERT(psa_hash_abort(&operation));
2756 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002757}
2758/* END_CASE */
2759
2760/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002761void hash_compute_compare(int alg_arg, data_t *input,
2762 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002763{
2764 psa_algorithm_t alg = alg_arg;
2765 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2766 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002767 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002768 size_t i;
2769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002771
Neil Armstrongca30a002022-02-07 11:40:23 +01002772 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2774 output, PSA_HASH_LENGTH(alg),
2775 &output_length));
2776 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002777 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002778 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002779
Neil Armstrongca30a002022-02-07 11:40:23 +01002780 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 PSA_ASSERT(psa_hash_setup(&operation, alg));
2782 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2783 PSA_ASSERT(psa_hash_finish(&operation, output,
2784 PSA_HASH_LENGTH(alg),
2785 &output_length));
2786 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002787 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002788 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002789
2790 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2792 output, sizeof(output),
2793 &output_length));
2794 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002795 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002796 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002797
Neil Armstrongca30a002022-02-07 11:40:23 +01002798 /* Compute with larger buffer, 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 PSA_ASSERT(psa_hash_finish(&operation, output,
2802 sizeof(output), &output_length));
2803 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002804 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002805 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002806
2807 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2809 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002810
Neil Armstrongca30a002022-02-07 11:40:23 +01002811 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 PSA_ASSERT(psa_hash_setup(&operation, alg));
2813 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2814 PSA_ASSERT(psa_hash_verify(&operation, output,
2815 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002816
2817 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2819 output, output_length + 1),
2820 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002821
Neil Armstrongca30a002022-02-07 11:40:23 +01002822 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 PSA_ASSERT(psa_hash_setup(&operation, alg));
2824 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2825 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2826 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002827
2828 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2830 output, output_length - 1),
2831 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002832
Neil Armstrongca30a002022-02-07 11:40:23 +01002833 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 PSA_ASSERT(psa_hash_setup(&operation, alg));
2835 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2836 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2837 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002838
Gilles Peskine0a749c82019-11-28 19:33:58 +01002839 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 for (i = 0; i < output_length; i++) {
2841 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002842 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002843
2844 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2846 output, output_length),
2847 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002848
2849 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 PSA_ASSERT(psa_hash_setup(&operation, alg));
2851 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2852 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2853 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002854
Gilles Peskine0a749c82019-11-28 19:33:58 +01002855 output[i] ^= 1;
2856 }
2857
2858exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 PSA_ASSERT(psa_hash_abort(&operation));
2860 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002861}
2862/* END_CASE */
2863
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002864/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002865void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002866{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002867 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002868 unsigned char input[] = "";
2869 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002870 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002871 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2872 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2874 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002875 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002876 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002877 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002880
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002881 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 PSA_ASSERT(psa_hash_setup(&operation, alg));
2883 ASSERT_OPERATION_IS_ACTIVE(operation);
2884 TEST_EQUAL(psa_hash_setup(&operation, alg),
2885 PSA_ERROR_BAD_STATE);
2886 ASSERT_OPERATION_IS_INACTIVE(operation);
2887 PSA_ASSERT(psa_hash_abort(&operation));
2888 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002889
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002890 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2892 PSA_ERROR_BAD_STATE);
2893 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002894
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002895 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002897 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 ASSERT_OPERATION_IS_ACTIVE(operation);
2899 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2900 PSA_ERROR_BAD_STATE);
2901 ASSERT_OPERATION_IS_INACTIVE(operation);
2902 PSA_ASSERT(psa_hash_abort(&operation));
2903 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002904
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002905 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 PSA_ASSERT(psa_hash_setup(&operation, alg));
2907 PSA_ASSERT(psa_hash_finish(&operation,
2908 hash, sizeof(hash), &hash_len));
2909 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2910 PSA_ERROR_BAD_STATE);
2911 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002912
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002913 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 TEST_EQUAL(psa_hash_verify(&operation,
2915 valid_hash, sizeof(valid_hash)),
2916 PSA_ERROR_BAD_STATE);
2917 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002918
2919 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 PSA_ASSERT(psa_hash_setup(&operation, alg));
2921 PSA_ASSERT(psa_hash_finish(&operation,
2922 hash, sizeof(hash), &hash_len));
2923 TEST_EQUAL(psa_hash_verify(&operation,
2924 valid_hash, sizeof(valid_hash)),
2925 PSA_ERROR_BAD_STATE);
2926 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002927
2928 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 PSA_ASSERT(psa_hash_setup(&operation, alg));
2930 ASSERT_OPERATION_IS_ACTIVE(operation);
2931 PSA_ASSERT(psa_hash_verify(&operation,
2932 valid_hash, sizeof(valid_hash)));
2933 ASSERT_OPERATION_IS_INACTIVE(operation);
2934 TEST_EQUAL(psa_hash_verify(&operation,
2935 valid_hash, sizeof(valid_hash)),
2936 PSA_ERROR_BAD_STATE);
2937 ASSERT_OPERATION_IS_INACTIVE(operation);
2938 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002939
2940 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 TEST_EQUAL(psa_hash_finish(&operation,
2942 hash, sizeof(hash), &hash_len),
2943 PSA_ERROR_BAD_STATE);
2944 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002945
2946 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 PSA_ASSERT(psa_hash_setup(&operation, alg));
2948 PSA_ASSERT(psa_hash_finish(&operation,
2949 hash, sizeof(hash), &hash_len));
2950 TEST_EQUAL(psa_hash_finish(&operation,
2951 hash, sizeof(hash), &hash_len),
2952 PSA_ERROR_BAD_STATE);
2953 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002954
2955 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 PSA_ASSERT(psa_hash_setup(&operation, alg));
2957 PSA_ASSERT(psa_hash_verify(&operation,
2958 valid_hash, sizeof(valid_hash)));
2959 TEST_EQUAL(psa_hash_finish(&operation,
2960 hash, sizeof(hash), &hash_len),
2961 PSA_ERROR_BAD_STATE);
2962 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002963
2964exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002966}
2967/* END_CASE */
2968
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002969/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002970void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002971{
2972 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002973 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2974 * appended to it */
2975 unsigned char hash[] = {
2976 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2977 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2979 };
2980 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002981 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002984
itayzafrir27e69452018-11-01 14:26:34 +02002985 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 PSA_ASSERT(psa_hash_setup(&operation, alg));
2987 ASSERT_OPERATION_IS_ACTIVE(operation);
2988 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2989 PSA_ERROR_INVALID_SIGNATURE);
2990 ASSERT_OPERATION_IS_INACTIVE(operation);
2991 PSA_ASSERT(psa_hash_abort(&operation));
2992 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002993
itayzafrir27e69452018-11-01 14:26:34 +02002994 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 PSA_ASSERT(psa_hash_setup(&operation, alg));
2996 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2997 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002998
itayzafrir27e69452018-11-01 14:26:34 +02002999 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 PSA_ASSERT(psa_hash_setup(&operation, alg));
3001 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3002 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003003
itayzafrirec93d302018-10-18 18:01:10 +03003004exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003006}
3007/* END_CASE */
3008
Ronald Cronee414c72021-03-18 18:50:08 +01003009/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003010void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003011{
3012 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003013 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003015 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003016 size_t hash_len;
3017
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003019
itayzafrir58028322018-10-25 10:22:01 +03003020 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 PSA_ASSERT(psa_hash_setup(&operation, alg));
3022 TEST_EQUAL(psa_hash_finish(&operation,
3023 hash, expected_size - 1, &hash_len),
3024 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003025
3026exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003028}
3029/* END_CASE */
3030
Ronald Cronee414c72021-03-18 18:50:08 +01003031/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003032void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003033{
3034 psa_algorithm_t alg = PSA_ALG_SHA_256;
3035 unsigned char hash[PSA_HASH_MAX_SIZE];
3036 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3037 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3038 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3039 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3040 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3041 size_t hash_len;
3042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 PSA_ASSERT(psa_crypto_init());
3044 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3047 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3048 PSA_ASSERT(psa_hash_finish(&op_finished,
3049 hash, sizeof(hash), &hash_len));
3050 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3051 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3054 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3057 PSA_ASSERT(psa_hash_finish(&op_init,
3058 hash, sizeof(hash), &hash_len));
3059 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3060 PSA_ASSERT(psa_hash_finish(&op_finished,
3061 hash, sizeof(hash), &hash_len));
3062 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3063 PSA_ASSERT(psa_hash_finish(&op_aborted,
3064 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003065
3066exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 psa_hash_abort(&op_source);
3068 psa_hash_abort(&op_init);
3069 psa_hash_abort(&op_setup);
3070 psa_hash_abort(&op_finished);
3071 psa_hash_abort(&op_aborted);
3072 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003073}
3074/* END_CASE */
3075
Ronald Cronee414c72021-03-18 18:50:08 +01003076/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003077void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003078{
3079 psa_algorithm_t alg = PSA_ALG_SHA_256;
3080 unsigned char hash[PSA_HASH_MAX_SIZE];
3081 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3082 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3083 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3084 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3085 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3086 size_t hash_len;
3087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3091 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3092 PSA_ASSERT(psa_hash_finish(&op_finished,
3093 hash, sizeof(hash), &hash_len));
3094 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3095 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3098 PSA_ASSERT(psa_hash_finish(&op_target,
3099 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3102 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3103 PSA_ERROR_BAD_STATE);
3104 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3105 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003106
3107exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 psa_hash_abort(&op_target);
3109 psa_hash_abort(&op_init);
3110 psa_hash_abort(&op_setup);
3111 psa_hash_abort(&op_finished);
3112 psa_hash_abort(&op_aborted);
3113 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003114}
3115/* END_CASE */
3116
itayzafrir58028322018-10-25 10:22:01 +03003117/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003118void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003119{
Jaeden Amero252ef282019-02-15 14:05:35 +00003120 const uint8_t input[1] = { 0 };
3121
Jaeden Amero769ce272019-01-04 11:48:03 +00003122 /* Test each valid way of initializing the object, except for `= {0}`, as
3123 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3124 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003125 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003127 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3128 psa_mac_operation_t zero;
3129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003131
Jaeden Amero252ef282019-02-15 14:05:35 +00003132 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 TEST_EQUAL(psa_mac_update(&func,
3134 input, sizeof(input)),
3135 PSA_ERROR_BAD_STATE);
3136 TEST_EQUAL(psa_mac_update(&init,
3137 input, sizeof(input)),
3138 PSA_ERROR_BAD_STATE);
3139 TEST_EQUAL(psa_mac_update(&zero,
3140 input, sizeof(input)),
3141 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003142
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003143 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 PSA_ASSERT(psa_mac_abort(&func));
3145 PSA_ASSERT(psa_mac_abort(&init));
3146 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003147}
3148/* END_CASE */
3149
3150/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003151void mac_setup(int key_type_arg,
3152 data_t *key,
3153 int alg_arg,
3154 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003155{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003156 psa_key_type_t key_type = key_type_arg;
3157 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003158 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003159 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003160 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3161#if defined(KNOWN_SUPPORTED_MAC_ALG)
3162 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3163#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003166
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3168 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003169 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 }
3171 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003172
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003173 /* The operation object should be reusable. */
3174#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3176 smoke_test_key_data,
3177 sizeof(smoke_test_key_data),
3178 KNOWN_SUPPORTED_MAC_ALG,
3179 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 }
3182 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003183#endif
3184
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003187}
3188/* END_CASE */
3189
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003190/* 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 +01003191void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003192{
Ronald Cron5425a212020-08-04 14:58:35 +02003193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003194 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3195 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003196 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003197 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3198 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3200 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003202 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3203 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3204 size_t sign_mac_length = 0;
3205 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3206 const uint8_t verify_mac[] = {
3207 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3208 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3210 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 PSA_ASSERT(psa_crypto_init());
3213 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3214 psa_set_key_algorithm(&attributes, alg);
3215 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003216
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3218 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003219
Jaeden Amero252ef282019-02-15 14:05:35 +00003220 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3222 PSA_ERROR_BAD_STATE);
3223 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003224
3225 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3227 &sign_mac_length),
3228 PSA_ERROR_BAD_STATE);
3229 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003230
3231 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 TEST_EQUAL(psa_mac_verify_finish(&operation,
3233 verify_mac, sizeof(verify_mac)),
3234 PSA_ERROR_BAD_STATE);
3235 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003236
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003237 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3239 ASSERT_OPERATION_IS_ACTIVE(operation);
3240 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3241 PSA_ERROR_BAD_STATE);
3242 ASSERT_OPERATION_IS_INACTIVE(operation);
3243 PSA_ASSERT(psa_mac_abort(&operation));
3244 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003245
Jaeden Amero252ef282019-02-15 14:05:35 +00003246 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3248 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3249 PSA_ASSERT(psa_mac_sign_finish(&operation,
3250 sign_mac, sizeof(sign_mac),
3251 &sign_mac_length));
3252 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3253 PSA_ERROR_BAD_STATE);
3254 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003255
3256 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3258 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3259 PSA_ASSERT(psa_mac_verify_finish(&operation,
3260 verify_mac, sizeof(verify_mac)));
3261 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3262 PSA_ERROR_BAD_STATE);
3263 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003264
3265 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3267 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3268 PSA_ASSERT(psa_mac_sign_finish(&operation,
3269 sign_mac, sizeof(sign_mac),
3270 &sign_mac_length));
3271 TEST_EQUAL(psa_mac_sign_finish(&operation,
3272 sign_mac, sizeof(sign_mac),
3273 &sign_mac_length),
3274 PSA_ERROR_BAD_STATE);
3275 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003276
3277 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3279 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3280 PSA_ASSERT(psa_mac_verify_finish(&operation,
3281 verify_mac, sizeof(verify_mac)));
3282 TEST_EQUAL(psa_mac_verify_finish(&operation,
3283 verify_mac, sizeof(verify_mac)),
3284 PSA_ERROR_BAD_STATE);
3285 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003286
3287 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3289 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3290 ASSERT_OPERATION_IS_ACTIVE(operation);
3291 TEST_EQUAL(psa_mac_verify_finish(&operation,
3292 verify_mac, sizeof(verify_mac)),
3293 PSA_ERROR_BAD_STATE);
3294 ASSERT_OPERATION_IS_INACTIVE(operation);
3295 PSA_ASSERT(psa_mac_abort(&operation));
3296 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003297
3298 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3300 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3301 ASSERT_OPERATION_IS_ACTIVE(operation);
3302 TEST_EQUAL(psa_mac_sign_finish(&operation,
3303 sign_mac, sizeof(sign_mac),
3304 &sign_mac_length),
3305 PSA_ERROR_BAD_STATE);
3306 ASSERT_OPERATION_IS_INACTIVE(operation);
3307 PSA_ASSERT(psa_mac_abort(&operation));
3308 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003311
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003312exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003314}
3315/* END_CASE */
3316
3317/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003318void mac_sign_verify_multi(int key_type_arg,
3319 data_t *key_data,
3320 int alg_arg,
3321 data_t *input,
3322 int is_verify,
3323 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003324{
3325 size_t data_part_len = 0;
3326
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003328 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003330
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 if (mac_multipart_internal_func(key_type_arg, key_data,
3332 alg_arg,
3333 input, data_part_len,
3334 expected_mac,
3335 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003336 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003338
3339 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003341
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 if (mac_multipart_internal_func(key_type_arg, key_data,
3343 alg_arg,
3344 input, data_part_len,
3345 expected_mac,
3346 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003347 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003349 }
3350
3351 /* Goto is required to silence warnings about unused labels, as we
3352 * don't actually do any test assertions in this function. */
3353 goto exit;
3354}
3355/* END_CASE */
3356
3357/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003358void mac_sign(int key_type_arg,
3359 data_t *key_data,
3360 int alg_arg,
3361 data_t *input,
3362 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003363{
Ronald Cron5425a212020-08-04 14:58:35 +02003364 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003365 psa_key_type_t key_type = key_type_arg;
3366 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003367 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003369 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003370 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003372 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003373 const size_t output_sizes_to_test[] = {
3374 0,
3375 1,
3376 expected_mac->len - 1,
3377 expected_mac->len,
3378 expected_mac->len + 1,
3379 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003382 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3388 psa_set_key_algorithm(&attributes, alg);
3389 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003390
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3392 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003393
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003395 const size_t output_size = output_sizes_to_test[i];
3396 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 (output_size >= expected_mac->len ? PSA_SUCCESS :
3398 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003399
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003401 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003402
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003403 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 TEST_EQUAL(psa_mac_compute(key, alg,
3405 input->x, input->len,
3406 actual_mac, output_size, &mac_length),
3407 expected_status);
3408 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003409 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003410 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003411 }
3412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 if (output_size > 0) {
3414 memset(actual_mac, 0, output_size);
3415 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003416
3417 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3419 PSA_ASSERT(psa_mac_update(&operation,
3420 input->x, input->len));
3421 TEST_EQUAL(psa_mac_sign_finish(&operation,
3422 actual_mac, output_size,
3423 &mac_length),
3424 expected_status);
3425 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003428 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003429 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003430 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003432 actual_mac = NULL;
3433 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003434
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003435exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 psa_mac_abort(&operation);
3437 psa_destroy_key(key);
3438 PSA_DONE();
3439 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003440}
3441/* END_CASE */
3442
3443/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003444void mac_verify(int key_type_arg,
3445 data_t *key_data,
3446 int alg_arg,
3447 data_t *input,
3448 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003449{
Ronald Cron5425a212020-08-04 14:58:35 +02003450 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003451 psa_key_type_t key_type = key_type_arg;
3452 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003453 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003455 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003456
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003458
Gilles Peskine449bd832023-01-11 14:50:10 +01003459 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003460
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3462 psa_set_key_algorithm(&attributes, alg);
3463 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003464
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3466 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003467
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003468 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3470 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003471
3472 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3474 PSA_ASSERT(psa_mac_update(&operation,
3475 input->x, input->len));
3476 PSA_ASSERT(psa_mac_verify_finish(&operation,
3477 expected_mac->x,
3478 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003479
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003480 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 TEST_EQUAL(psa_mac_verify(key, alg,
3482 input->x, input->len,
3483 expected_mac->x,
3484 expected_mac->len - 1),
3485 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003486
3487 /* Test a MAC that's too short, multi-part case. */
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 expected_mac->x,
3493 expected_mac->len - 1),
3494 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003495
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003496 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003497 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3499 TEST_EQUAL(psa_mac_verify(key, alg,
3500 input->x, input->len,
3501 perturbed_mac, expected_mac->len + 1),
3502 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003503
3504 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3506 PSA_ASSERT(psa_mac_update(&operation,
3507 input->x, input->len));
3508 TEST_EQUAL(psa_mac_verify_finish(&operation,
3509 perturbed_mac,
3510 expected_mac->len + 1),
3511 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003512
3513 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003514 for (size_t i = 0; i < expected_mac->len; i++) {
3515 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003516 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003517
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 TEST_EQUAL(psa_mac_verify(key, alg,
3519 input->x, input->len,
3520 perturbed_mac, expected_mac->len),
3521 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003522
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3524 PSA_ASSERT(psa_mac_update(&operation,
3525 input->x, input->len));
3526 TEST_EQUAL(psa_mac_verify_finish(&operation,
3527 perturbed_mac,
3528 expected_mac->len),
3529 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003530 perturbed_mac[i] ^= 1;
3531 }
3532
Gilles Peskine8c9def32018-02-08 10:02:12 +01003533exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 psa_mac_abort(&operation);
3535 psa_destroy_key(key);
3536 PSA_DONE();
3537 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003538}
3539/* END_CASE */
3540
3541/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003542void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003543{
Jaeden Ameroab439972019-02-15 14:12:05 +00003544 const uint8_t input[1] = { 0 };
3545 unsigned char output[1] = { 0 };
3546 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003547 /* Test each valid way of initializing the object, except for `= {0}`, as
3548 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3549 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003550 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003552 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3553 psa_cipher_operation_t zero;
3554
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003556
Jaeden Ameroab439972019-02-15 14:12:05 +00003557 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 TEST_EQUAL(psa_cipher_update(&func,
3559 input, sizeof(input),
3560 output, sizeof(output),
3561 &output_length),
3562 PSA_ERROR_BAD_STATE);
3563 TEST_EQUAL(psa_cipher_update(&init,
3564 input, sizeof(input),
3565 output, sizeof(output),
3566 &output_length),
3567 PSA_ERROR_BAD_STATE);
3568 TEST_EQUAL(psa_cipher_update(&zero,
3569 input, sizeof(input),
3570 output, sizeof(output),
3571 &output_length),
3572 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003573
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003574 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003575 PSA_ASSERT(psa_cipher_abort(&func));
3576 PSA_ASSERT(psa_cipher_abort(&init));
3577 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003578}
3579/* END_CASE */
3580
3581/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003582void cipher_setup(int key_type_arg,
3583 data_t *key,
3584 int alg_arg,
3585 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003586{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003587 psa_key_type_t key_type = key_type_arg;
3588 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003589 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003590 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003591 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003592#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003593 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3594#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003595
Gilles Peskine449bd832023-01-11 14:50:10 +01003596 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003597
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3599 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003600 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 }
3602 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003603
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003604 /* The operation object should be reusable. */
3605#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003606 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3607 smoke_test_key_data,
3608 sizeof(smoke_test_key_data),
3609 KNOWN_SUPPORTED_CIPHER_ALG,
3610 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003611 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 }
3613 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003614#endif
3615
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003616exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003617 psa_cipher_abort(&operation);
3618 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003619}
3620/* END_CASE */
3621
Ronald Cronee414c72021-03-18 18:50:08 +01003622/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003623void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003624{
Ronald Cron5425a212020-08-04 14:58:35 +02003625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003626 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3627 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003628 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003629 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003630 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003631 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003632 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003633 0xaa, 0xaa, 0xaa, 0xaa
3634 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003635 const uint8_t text[] = {
3636 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003637 0xbb, 0xbb, 0xbb, 0xbb
3638 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003639 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003640 size_t length = 0;
3641
Gilles Peskine449bd832023-01-11 14:50:10 +01003642 PSA_ASSERT(psa_crypto_init());
3643 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3644 psa_set_key_algorithm(&attributes, alg);
3645 psa_set_key_type(&attributes, key_type);
3646 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3647 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003648
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003649 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3651 ASSERT_OPERATION_IS_ACTIVE(operation);
3652 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3653 PSA_ERROR_BAD_STATE);
3654 ASSERT_OPERATION_IS_INACTIVE(operation);
3655 PSA_ASSERT(psa_cipher_abort(&operation));
3656 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003657
3658 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003659 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3660 ASSERT_OPERATION_IS_ACTIVE(operation);
3661 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3662 PSA_ERROR_BAD_STATE);
3663 ASSERT_OPERATION_IS_INACTIVE(operation);
3664 PSA_ASSERT(psa_cipher_abort(&operation));
3665 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003666
Jaeden Ameroab439972019-02-15 14:12:05 +00003667 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003668 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3669 buffer, sizeof(buffer),
3670 &length),
3671 PSA_ERROR_BAD_STATE);
3672 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003673
3674 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003675 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3676 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3677 buffer, sizeof(buffer),
3678 &length));
3679 ASSERT_OPERATION_IS_ACTIVE(operation);
3680 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3681 buffer, sizeof(buffer),
3682 &length),
3683 PSA_ERROR_BAD_STATE);
3684 ASSERT_OPERATION_IS_INACTIVE(operation);
3685 PSA_ASSERT(psa_cipher_abort(&operation));
3686 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003687
3688 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003689 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3690 PSA_ASSERT(psa_cipher_set_iv(&operation,
3691 iv, sizeof(iv)));
3692 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3693 buffer, sizeof(buffer),
3694 &length),
3695 PSA_ERROR_BAD_STATE);
3696 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003697
3698 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003699 TEST_EQUAL(psa_cipher_set_iv(&operation,
3700 iv, sizeof(iv)),
3701 PSA_ERROR_BAD_STATE);
3702 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003703
3704 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003705 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3706 PSA_ASSERT(psa_cipher_set_iv(&operation,
3707 iv, sizeof(iv)));
3708 ASSERT_OPERATION_IS_ACTIVE(operation);
3709 TEST_EQUAL(psa_cipher_set_iv(&operation,
3710 iv, sizeof(iv)),
3711 PSA_ERROR_BAD_STATE);
3712 ASSERT_OPERATION_IS_INACTIVE(operation);
3713 PSA_ASSERT(psa_cipher_abort(&operation));
3714 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003715
3716 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3718 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3719 buffer, sizeof(buffer),
3720 &length));
3721 TEST_EQUAL(psa_cipher_set_iv(&operation,
3722 iv, sizeof(iv)),
3723 PSA_ERROR_BAD_STATE);
3724 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003725
3726 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003727 TEST_EQUAL(psa_cipher_update(&operation,
3728 text, sizeof(text),
3729 buffer, sizeof(buffer),
3730 &length),
3731 PSA_ERROR_BAD_STATE);
3732 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003733
3734 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003735 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3736 ASSERT_OPERATION_IS_ACTIVE(operation);
3737 TEST_EQUAL(psa_cipher_update(&operation,
3738 text, sizeof(text),
3739 buffer, sizeof(buffer),
3740 &length),
3741 PSA_ERROR_BAD_STATE);
3742 ASSERT_OPERATION_IS_INACTIVE(operation);
3743 PSA_ASSERT(psa_cipher_abort(&operation));
3744 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003745
3746 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003747 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3748 PSA_ASSERT(psa_cipher_set_iv(&operation,
3749 iv, sizeof(iv)));
3750 PSA_ASSERT(psa_cipher_finish(&operation,
3751 buffer, sizeof(buffer), &length));
3752 TEST_EQUAL(psa_cipher_update(&operation,
3753 text, sizeof(text),
3754 buffer, sizeof(buffer),
3755 &length),
3756 PSA_ERROR_BAD_STATE);
3757 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003758
3759 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 TEST_EQUAL(psa_cipher_finish(&operation,
3761 buffer, sizeof(buffer), &length),
3762 PSA_ERROR_BAD_STATE);
3763 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003764
3765 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003766 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003767 /* Not calling update means we are encrypting an empty buffer, which is OK
3768 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 ASSERT_OPERATION_IS_ACTIVE(operation);
3770 TEST_EQUAL(psa_cipher_finish(&operation,
3771 buffer, sizeof(buffer), &length),
3772 PSA_ERROR_BAD_STATE);
3773 ASSERT_OPERATION_IS_INACTIVE(operation);
3774 PSA_ASSERT(psa_cipher_abort(&operation));
3775 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003776
3777 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003778 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3779 PSA_ASSERT(psa_cipher_set_iv(&operation,
3780 iv, sizeof(iv)));
3781 PSA_ASSERT(psa_cipher_finish(&operation,
3782 buffer, sizeof(buffer), &length));
3783 TEST_EQUAL(psa_cipher_finish(&operation,
3784 buffer, sizeof(buffer), &length),
3785 PSA_ERROR_BAD_STATE);
3786 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003787
Gilles Peskine449bd832023-01-11 14:50:10 +01003788 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003789
Jaeden Ameroab439972019-02-15 14:12:05 +00003790exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003791 psa_cipher_abort(&operation);
3792 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003793}
3794/* END_CASE */
3795
3796/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003797void cipher_encrypt_fail(int alg_arg,
3798 int key_type_arg,
3799 data_t *key_data,
3800 data_t *input,
3801 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003802{
Ronald Cron5425a212020-08-04 14:58:35 +02003803 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003804 psa_status_t status;
3805 psa_key_type_t key_type = key_type_arg;
3806 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003807 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003809 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3810 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003811 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003812 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003813 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003814 size_t function_output_length;
3815 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3817
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 if (PSA_ERROR_BAD_STATE != expected_status) {
3819 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003820
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3822 psa_set_key_algorithm(&attributes, alg);
3823 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003824
Gilles Peskine449bd832023-01-11 14:50:10 +01003825 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3826 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003827 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003828
Gilles Peskine449bd832023-01-11 14:50:10 +01003829 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3830 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003831 }
3832
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003833 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3835 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003836
Gilles Peskine449bd832023-01-11 14:50:10 +01003837 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003838
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003839 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 status = psa_cipher_encrypt_setup(&operation, key, alg);
3841 if (status == PSA_SUCCESS) {
3842 if (alg != PSA_ALG_ECB_NO_PADDING) {
3843 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3844 iv, iv_size,
3845 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003846 }
3847
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 status = psa_cipher_update(&operation, input->x, input->len,
3849 output, output_buffer_size,
3850 &function_output_length);
3851 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003852 output_length += function_output_length;
3853
Gilles Peskine449bd832023-01-11 14:50:10 +01003854 status = psa_cipher_finish(&operation, output + output_length,
3855 output_buffer_size - output_length,
3856 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003857
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 TEST_EQUAL(status, expected_status);
3859 } else {
3860 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003861 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 } else {
3863 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003864 }
3865
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003866exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 psa_cipher_abort(&operation);
3868 mbedtls_free(output);
3869 psa_destroy_key(key);
3870 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003871}
3872/* END_CASE */
3873
3874/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003875void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3876 data_t *input, int iv_length,
3877 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003878{
3879 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3880 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3882 size_t output_buffer_size = 0;
3883 unsigned char *output = NULL;
3884
Gilles Peskine449bd832023-01-11 14:50:10 +01003885 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003886 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003887
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003889
Gilles Peskine449bd832023-01-11 14:50:10 +01003890 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3891 psa_set_key_algorithm(&attributes, alg);
3892 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003893
Gilles Peskine449bd832023-01-11 14:50:10 +01003894 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3895 &key));
3896 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3897 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3898 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003899
3900exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 psa_cipher_abort(&operation);
3902 mbedtls_free(output);
3903 psa_destroy_key(key);
3904 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003905}
3906/* END_CASE */
3907
3908/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003909void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3910 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003911{
3912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3913 psa_key_type_t key_type = key_type_arg;
3914 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003915 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3916 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003917 unsigned char *output = NULL;
3918 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003919 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003920 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3921
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003923
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003924 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 TEST_LE_U(ciphertext->len,
3926 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3927 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3928 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3929 TEST_LE_U(plaintext->len,
3930 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3931 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3932 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003933
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003934
3935 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003936 psa_set_key_usage_flags(&attributes,
3937 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3938 psa_set_key_algorithm(&attributes, alg);
3939 psa_set_key_type(&attributes, key_type);
3940 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3941 &key));
3942 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3943 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003944 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003945
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003946 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3948 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3949 PSA_ERROR_BAD_STATE);
3950 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3951 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3952 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003953
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003954 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003955 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3956 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3957 &length),
3958 PSA_ERROR_BAD_STATE);
3959 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3960 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3961 &length),
3962 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003963
Gilles Peskine286c3142022-04-20 17:09:38 +02003964 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003966 output_length = 0;
3967 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 PSA_ASSERT(psa_cipher_update(&operation,
3969 plaintext->x, plaintext->len,
3970 output, output_buffer_size,
3971 &length));
3972 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003973 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 PSA_ASSERT(psa_cipher_finish(&operation,
3975 mbedtls_buffer_offset(output, output_length),
3976 output_buffer_size - output_length,
3977 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003978 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003979 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003980 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003981
Gilles Peskine286c3142022-04-20 17:09:38 +02003982 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003984 output_length = 0;
3985 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 PSA_ASSERT(psa_cipher_update(&operation,
3987 ciphertext->x, ciphertext->len,
3988 output, output_buffer_size,
3989 &length));
3990 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003991 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 PSA_ASSERT(psa_cipher_finish(&operation,
3993 mbedtls_buffer_offset(output, output_length),
3994 output_buffer_size - output_length,
3995 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003996 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003997 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003998 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003999
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004000 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004001 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4003 output, output_buffer_size,
4004 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004005 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004006 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004007
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004008 /* One-shot decryption */
4009 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4011 output, output_buffer_size,
4012 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004013 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004014 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004015
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004016exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 PSA_ASSERT(psa_cipher_abort(&operation));
4018 mbedtls_free(output);
4019 psa_cipher_abort(&operation);
4020 psa_destroy_key(key);
4021 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004022}
4023/* END_CASE */
4024
4025/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004026void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004027{
4028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4029 psa_algorithm_t alg = alg_arg;
4030 psa_key_type_t key_type = key_type_arg;
4031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4032 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4033 psa_status_t status;
4034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4038 psa_set_key_algorithm(&attributes, alg);
4039 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004040
4041 /* Usage of either of these two size macros would cause divide by zero
4042 * with incorrect key types previously. Input length should be irrelevant
4043 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4045 0);
4046 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004047
4048
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4050 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004051
4052 /* Should fail due to invalid alg type (to support invalid key type).
4053 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004055
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004057
4058exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 psa_cipher_abort(&operation);
4060 psa_destroy_key(key);
4061 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004062}
4063/* END_CASE */
4064
4065/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004066void cipher_encrypt_validation(int alg_arg,
4067 int key_type_arg,
4068 data_t *key_data,
4069 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004070{
4071 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4072 psa_key_type_t key_type = key_type_arg;
4073 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004075 unsigned char *output1 = NULL;
4076 size_t output1_buffer_size = 0;
4077 size_t output1_length = 0;
4078 unsigned char *output2 = NULL;
4079 size_t output2_buffer_size = 0;
4080 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004081 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004082 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004086
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4088 psa_set_key_algorithm(&attributes, alg);
4089 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4092 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4093 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004094 TEST_CALLOC(output1, output1_buffer_size);
4095 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004096
Gilles Peskine449bd832023-01-11 14:50:10 +01004097 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4098 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004099
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004100 /* The one-shot cipher encryption uses generated iv so validating
4101 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4103 output1_buffer_size, &output1_length));
4104 TEST_LE_U(output1_length,
4105 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4106 TEST_LE_U(output1_length,
4107 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4110 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004111
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 PSA_ASSERT(psa_cipher_update(&operation,
4113 input->x, input->len,
4114 output2, output2_buffer_size,
4115 &function_output_length));
4116 TEST_LE_U(function_output_length,
4117 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4118 TEST_LE_U(function_output_length,
4119 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004120 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 PSA_ASSERT(psa_cipher_finish(&operation,
4123 output2 + output2_length,
4124 output2_buffer_size - output2_length,
4125 &function_output_length));
4126 TEST_LE_U(function_output_length,
4127 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4128 TEST_LE_U(function_output_length,
4129 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004130 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004131
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004133 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004134 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004135
Gilles Peskine50e586b2018-06-08 14:28:46 +02004136exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 psa_cipher_abort(&operation);
4138 mbedtls_free(output1);
4139 mbedtls_free(output2);
4140 psa_destroy_key(key);
4141 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004142}
4143/* END_CASE */
4144
4145/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004146void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4147 data_t *key_data, data_t *iv,
4148 data_t *input,
4149 int first_part_size_arg,
4150 int output1_length_arg, int output2_length_arg,
4151 data_t *expected_output,
4152 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004153{
Ronald Cron5425a212020-08-04 14:58:35 +02004154 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004155 psa_key_type_t key_type = key_type_arg;
4156 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004157 psa_status_t status;
4158 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004159 size_t first_part_size = first_part_size_arg;
4160 size_t output1_length = output1_length_arg;
4161 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004162 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004163 size_t output_buffer_size = 0;
4164 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004165 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004166 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4172 psa_set_key_algorithm(&attributes, alg);
4173 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4176 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 if (iv->len > 0) {
4181 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004182 }
4183
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4185 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004186 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004187
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 TEST_LE_U(first_part_size, input->len);
4189 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4190 output, output_buffer_size,
4191 &function_output_length));
4192 TEST_ASSERT(function_output_length == output1_length);
4193 TEST_LE_U(function_output_length,
4194 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4195 TEST_LE_U(function_output_length,
4196 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004197 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 if (first_part_size < input->len) {
4200 PSA_ASSERT(psa_cipher_update(&operation,
4201 input->x + first_part_size,
4202 input->len - first_part_size,
4203 (output_buffer_size == 0 ? NULL :
4204 output + total_output_length),
4205 output_buffer_size - total_output_length,
4206 &function_output_length));
4207 TEST_ASSERT(function_output_length == output2_length);
4208 TEST_LE_U(function_output_length,
4209 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4210 alg,
4211 input->len - first_part_size));
4212 TEST_LE_U(function_output_length,
4213 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004214 total_output_length += function_output_length;
4215 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004216
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 status = psa_cipher_finish(&operation,
4218 (output_buffer_size == 0 ? NULL :
4219 output + total_output_length),
4220 output_buffer_size - total_output_length,
4221 &function_output_length);
4222 TEST_LE_U(function_output_length,
4223 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4224 TEST_LE_U(function_output_length,
4225 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004226 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 if (expected_status == PSA_SUCCESS) {
4230 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004231
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004232 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004233 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004234 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004235
4236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 psa_cipher_abort(&operation);
4238 mbedtls_free(output);
4239 psa_destroy_key(key);
4240 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004241}
4242/* END_CASE */
4243
4244/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004245void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4246 data_t *key_data, data_t *iv,
4247 data_t *input,
4248 int first_part_size_arg,
4249 int output1_length_arg, int output2_length_arg,
4250 data_t *expected_output,
4251 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004252{
Ronald Cron5425a212020-08-04 14:58:35 +02004253 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004254 psa_key_type_t key_type = key_type_arg;
4255 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004256 psa_status_t status;
4257 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004258 size_t first_part_size = first_part_size_arg;
4259 size_t output1_length = output1_length_arg;
4260 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004261 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004262 size_t output_buffer_size = 0;
4263 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004264 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004265 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004266 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4271 psa_set_key_algorithm(&attributes, alg);
4272 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004273
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4275 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004276
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 if (iv->len > 0) {
4280 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004281 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004282
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4284 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004285 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004286
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 TEST_LE_U(first_part_size, input->len);
4288 PSA_ASSERT(psa_cipher_update(&operation,
4289 input->x, first_part_size,
4290 output, output_buffer_size,
4291 &function_output_length));
4292 TEST_ASSERT(function_output_length == output1_length);
4293 TEST_LE_U(function_output_length,
4294 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4295 TEST_LE_U(function_output_length,
4296 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004297 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004298
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 if (first_part_size < input->len) {
4300 PSA_ASSERT(psa_cipher_update(&operation,
4301 input->x + first_part_size,
4302 input->len - first_part_size,
4303 (output_buffer_size == 0 ? NULL :
4304 output + total_output_length),
4305 output_buffer_size - total_output_length,
4306 &function_output_length));
4307 TEST_ASSERT(function_output_length == output2_length);
4308 TEST_LE_U(function_output_length,
4309 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4310 alg,
4311 input->len - first_part_size));
4312 TEST_LE_U(function_output_length,
4313 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004314 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004315 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004316
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 status = psa_cipher_finish(&operation,
4318 (output_buffer_size == 0 ? NULL :
4319 output + total_output_length),
4320 output_buffer_size - total_output_length,
4321 &function_output_length);
4322 TEST_LE_U(function_output_length,
4323 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4324 TEST_LE_U(function_output_length,
4325 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004326 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004328
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 if (expected_status == PSA_SUCCESS) {
4330 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004331
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004332 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004333 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004334 }
4335
Gilles Peskine50e586b2018-06-08 14:28:46 +02004336exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 psa_cipher_abort(&operation);
4338 mbedtls_free(output);
4339 psa_destroy_key(key);
4340 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004341}
4342/* END_CASE */
4343
Gilles Peskine50e586b2018-06-08 14:28:46 +02004344/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004345void cipher_decrypt_fail(int alg_arg,
4346 int key_type_arg,
4347 data_t *key_data,
4348 data_t *iv,
4349 data_t *input_arg,
4350 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004351{
4352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4353 psa_status_t status;
4354 psa_key_type_t key_type = key_type_arg;
4355 psa_algorithm_t alg = alg_arg;
4356 psa_status_t expected_status = expected_status_arg;
4357 unsigned char *input = NULL;
4358 size_t input_buffer_size = 0;
4359 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004360 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004361 size_t output_buffer_size = 0;
4362 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004363 size_t function_output_length;
4364 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 if (PSA_ERROR_BAD_STATE != expected_status) {
4368 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4371 psa_set_key_algorithm(&attributes, alg);
4372 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004373
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4375 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004376 }
4377
4378 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4380 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004381 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 memcpy(input, iv->x, iv->len);
4383 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004384 }
4385
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004387 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004388
Neil Armstrong66a479f2022-02-07 15:41:19 +01004389 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4391 output_buffer_size, &output_length);
4392 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004393
Neil Armstrong66a479f2022-02-07 15:41:19 +01004394 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 status = psa_cipher_decrypt_setup(&operation, key, alg);
4396 if (status == PSA_SUCCESS) {
4397 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4398 input_arg->len) +
4399 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004400 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 if (iv->len > 0) {
4403 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (status != PSA_SUCCESS) {
4406 TEST_EQUAL(status, expected_status);
4407 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004408 }
4409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 if (status == PSA_SUCCESS) {
4411 status = psa_cipher_update(&operation,
4412 input_arg->x, input_arg->len,
4413 output_multi, output_buffer_size,
4414 &function_output_length);
4415 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004416 output_length = function_output_length;
4417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 status = psa_cipher_finish(&operation,
4419 output_multi + output_length,
4420 output_buffer_size - output_length,
4421 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 TEST_EQUAL(status, expected_status);
4424 } else {
4425 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004426 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 } else {
4428 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004429 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 } else {
4431 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004432 }
4433
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004434exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 psa_cipher_abort(&operation);
4436 mbedtls_free(input);
4437 mbedtls_free(output);
4438 mbedtls_free(output_multi);
4439 psa_destroy_key(key);
4440 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004441}
4442/* END_CASE */
4443
4444/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004445void cipher_decrypt(int alg_arg,
4446 int key_type_arg,
4447 data_t *key_data,
4448 data_t *iv,
4449 data_t *input_arg,
4450 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004451{
4452 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4453 psa_key_type_t key_type = key_type_arg;
4454 psa_algorithm_t alg = alg_arg;
4455 unsigned char *input = NULL;
4456 size_t input_buffer_size = 0;
4457 unsigned char *output = NULL;
4458 size_t output_buffer_size = 0;
4459 size_t output_length = 0;
4460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4465 psa_set_key_algorithm(&attributes, alg);
4466 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004467
4468 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4470 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004471 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 memcpy(input, iv->x, iv->len);
4473 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004474 }
4475
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004477 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004478
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4480 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4483 output_buffer_size, &output_length));
4484 TEST_LE_U(output_length,
4485 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4486 TEST_LE_U(output_length,
4487 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004488
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004489 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004490 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004491exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 mbedtls_free(input);
4493 mbedtls_free(output);
4494 psa_destroy_key(key);
4495 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004496}
4497/* END_CASE */
4498
4499/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004500void cipher_verify_output(int alg_arg,
4501 int key_type_arg,
4502 data_t *key_data,
4503 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004504{
Ronald Cron5425a212020-08-04 14:58:35 +02004505 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004506 psa_key_type_t key_type = key_type_arg;
4507 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004508 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004509 size_t output1_size = 0;
4510 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004511 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004512 size_t output2_size = 0;
4513 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004517
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4519 psa_set_key_algorithm(&attributes, alg);
4520 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4523 &key));
4524 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004525 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4528 output1, output1_size,
4529 &output1_length));
4530 TEST_LE_U(output1_length,
4531 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4532 TEST_LE_U(output1_length,
4533 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004534
4535 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004536 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4539 output2, output2_size,
4540 &output2_length));
4541 TEST_LE_U(output2_length,
4542 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4543 TEST_LE_U(output2_length,
4544 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004545
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004546 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004547
4548exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 mbedtls_free(output1);
4550 mbedtls_free(output2);
4551 psa_destroy_key(key);
4552 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004553}
4554/* END_CASE */
4555
4556/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004557void cipher_verify_output_multipart(int alg_arg,
4558 int key_type_arg,
4559 data_t *key_data,
4560 data_t *input,
4561 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004562{
Ronald Cron5425a212020-08-04 14:58:35 +02004563 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004564 psa_key_type_t key_type = key_type_arg;
4565 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004566 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004568 size_t iv_size = 16;
4569 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004570 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004571 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004572 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004573 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004574 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004575 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004576 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004577 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4578 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4584 psa_set_key_algorithm(&attributes, alg);
4585 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4588 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4591 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 if (alg != PSA_ALG_ECB_NO_PADDING) {
4594 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4595 iv, iv_size,
4596 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004597 }
4598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4600 TEST_LE_U(output1_buffer_size,
4601 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004602 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004605
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4607 output1, output1_buffer_size,
4608 &function_output_length));
4609 TEST_LE_U(function_output_length,
4610 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4611 TEST_LE_U(function_output_length,
4612 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004613 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004614
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 PSA_ASSERT(psa_cipher_update(&operation1,
4616 input->x + first_part_size,
4617 input->len - first_part_size,
David Horstmannb8dc2452024-02-06 17:03:13 +00004618 output1 + output1_length,
4619 output1_buffer_size - output1_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 &function_output_length));
4621 TEST_LE_U(function_output_length,
4622 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4623 alg,
4624 input->len - first_part_size));
4625 TEST_LE_U(function_output_length,
4626 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004627 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 PSA_ASSERT(psa_cipher_finish(&operation1,
4630 output1 + output1_length,
4631 output1_buffer_size - output1_length,
4632 &function_output_length));
4633 TEST_LE_U(function_output_length,
4634 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4635 TEST_LE_U(function_output_length,
4636 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004637 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004640
Gilles Peskine048b7f02018-06-08 14:20:49 +02004641 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 TEST_LE_U(output2_buffer_size,
4643 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4644 TEST_LE_U(output2_buffer_size,
4645 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004646 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 if (iv_length > 0) {
4649 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4650 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004651 }
Moran Pekerded84402018-06-06 16:36:50 +03004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4654 output2, output2_buffer_size,
4655 &function_output_length));
4656 TEST_LE_U(function_output_length,
4657 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4658 TEST_LE_U(function_output_length,
4659 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004660 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004661
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 PSA_ASSERT(psa_cipher_update(&operation2,
4663 output1 + first_part_size,
4664 output1_length - first_part_size,
David Horstmannb8dc2452024-02-06 17:03:13 +00004665 output2 + output2_length,
4666 output2_buffer_size - output2_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 &function_output_length));
4668 TEST_LE_U(function_output_length,
4669 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4670 alg,
4671 output1_length - first_part_size));
4672 TEST_LE_U(function_output_length,
4673 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004674 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 PSA_ASSERT(psa_cipher_finish(&operation2,
4677 output2 + output2_length,
4678 output2_buffer_size - output2_length,
4679 &function_output_length));
4680 TEST_LE_U(function_output_length,
4681 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4682 TEST_LE_U(function_output_length,
4683 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004684 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004687
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004688 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004689
4690exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 psa_cipher_abort(&operation1);
4692 psa_cipher_abort(&operation2);
4693 mbedtls_free(output1);
4694 mbedtls_free(output2);
4695 psa_destroy_key(key);
4696 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004697}
4698/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004699
Gilles Peskine20035e32018-02-03 22:44:14 +01004700/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004701void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4702 int alg_arg,
4703 data_t *nonce,
4704 data_t *additional_data,
4705 data_t *input_data,
4706 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004707{
Ronald Cron5425a212020-08-04 14:58:35 +02004708 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004709 psa_key_type_t key_type = key_type_arg;
4710 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004711 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004712 unsigned char *output_data = NULL;
4713 size_t output_size = 0;
4714 size_t output_length = 0;
4715 unsigned char *output_data2 = NULL;
4716 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004717 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004718 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4724 psa_set_key_algorithm(&attributes, alg);
4725 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4728 &key));
4729 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4730 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4733 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004734 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4735 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4737 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4738 TEST_EQUAL(output_size,
4739 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4740 TEST_LE_U(output_size,
4741 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004742 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004743 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 status = psa_aead_encrypt(key, alg,
4746 nonce->x, nonce->len,
4747 additional_data->x,
4748 additional_data->len,
4749 input_data->x, input_data->len,
4750 output_data, output_size,
4751 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004752
4753 /* If the operation is not supported, just skip and not fail in case the
4754 * encryption involves a common limitation of cryptography hardwares and
4755 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 if (status == PSA_ERROR_NOT_SUPPORTED) {
4757 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4758 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004759 }
4760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004764 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004765
Gilles Peskine003a4a92019-05-14 16:09:40 +02004766 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4767 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 TEST_EQUAL(input_data->len,
4769 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 TEST_LE_U(input_data->len,
4772 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004773
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 TEST_EQUAL(psa_aead_decrypt(key, alg,
4775 nonce->x, nonce->len,
4776 additional_data->x,
4777 additional_data->len,
4778 output_data, output_length,
4779 output_data2, output_length,
4780 &output_length2),
4781 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004782
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004783 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004784 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004785 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004786
Gilles Peskinea1cac842018-06-11 19:33:02 +02004787exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 psa_destroy_key(key);
4789 mbedtls_free(output_data);
4790 mbedtls_free(output_data2);
4791 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004792}
4793/* END_CASE */
4794
4795/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004796void aead_encrypt(int key_type_arg, data_t *key_data,
4797 int alg_arg,
4798 data_t *nonce,
4799 data_t *additional_data,
4800 data_t *input_data,
4801 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802{
Ronald Cron5425a212020-08-04 14:58:35 +02004803 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004804 psa_key_type_t key_type = key_type_arg;
4805 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004806 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004807 unsigned char *output_data = NULL;
4808 size_t output_size = 0;
4809 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004810 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004811 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4816 psa_set_key_algorithm(&attributes, alg);
4817 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004818
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4820 &key));
4821 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4822 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4825 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004826 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4827 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 TEST_EQUAL(output_size,
4829 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4830 TEST_LE_U(output_size,
4831 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004832 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 status = psa_aead_encrypt(key, alg,
4835 nonce->x, nonce->len,
4836 additional_data->x, additional_data->len,
4837 input_data->x, input_data->len,
4838 output_data, output_size,
4839 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004840
Ronald Cron28a45ed2021-02-09 20:35:42 +01004841 /* If the operation is not supported, just skip and not fail in case the
4842 * encryption involves a common limitation of cryptography hardwares and
4843 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 if (status == PSA_ERROR_NOT_SUPPORTED) {
4845 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4846 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004847 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004848
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004850 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004851 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004852
Gilles Peskinea1cac842018-06-11 19:33:02 +02004853exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 psa_destroy_key(key);
4855 mbedtls_free(output_data);
4856 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004857}
4858/* END_CASE */
4859
4860/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004861void aead_decrypt(int key_type_arg, data_t *key_data,
4862 int alg_arg,
4863 data_t *nonce,
4864 data_t *additional_data,
4865 data_t *input_data,
4866 data_t *expected_data,
4867 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004868{
Ronald Cron5425a212020-08-04 14:58:35 +02004869 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004870 psa_key_type_t key_type = key_type_arg;
4871 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004872 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004873 unsigned char *output_data = NULL;
4874 size_t output_size = 0;
4875 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004877 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004878 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4883 psa_set_key_algorithm(&attributes, alg);
4884 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4887 &key));
4888 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4889 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4892 alg);
4893 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4894 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004895 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4896 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 TEST_EQUAL(output_size,
4898 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4899 TEST_LE_U(output_size,
4900 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004901 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004902 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 status = psa_aead_decrypt(key, alg,
4905 nonce->x, nonce->len,
4906 additional_data->x,
4907 additional_data->len,
4908 input_data->x, input_data->len,
4909 output_data, output_size,
4910 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004911
Ronald Cron28a45ed2021-02-09 20:35:42 +01004912 /* If the operation is not supported, just skip and not fail in case the
4913 * decryption involves a common limitation of cryptography hardwares and
4914 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 if (status == PSA_ERROR_NOT_SUPPORTED) {
4916 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4917 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004918 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004919
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004921
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004923 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004924 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004926
Gilles Peskinea1cac842018-06-11 19:33:02 +02004927exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 psa_destroy_key(key);
4929 mbedtls_free(output_data);
4930 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004931}
4932/* END_CASE */
4933
4934/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004935void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4936 int alg_arg,
4937 data_t *nonce,
4938 data_t *additional_data,
4939 data_t *input_data,
4940 int do_set_lengths,
4941 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004942{
Paul Elliottd3f82412021-06-16 16:52:21 +01004943 size_t ad_part_len = 0;
4944 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004945 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004946
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4948 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 if (do_set_lengths) {
4951 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004952 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004954 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004956 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004957
4958 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 if (!aead_multipart_internal_func(key_type_arg, key_data,
4960 alg_arg, nonce,
4961 additional_data,
4962 ad_part_len,
4963 input_data, -1,
4964 set_lengths_method,
4965 expected_output,
4966 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004967 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004968 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 /* length(0) part, length(ad_part_len) part, length(0) part... */
4971 mbedtls_test_set_step(1000 + ad_part_len);
4972
4973 if (!aead_multipart_internal_func(key_type_arg, key_data,
4974 alg_arg, nonce,
4975 additional_data,
4976 ad_part_len,
4977 input_data, -1,
4978 set_lengths_method,
4979 expected_output,
4980 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004981 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 }
4983 }
4984
4985 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4986 /* Split data into length(data_part_len) parts. */
4987 mbedtls_test_set_step(2000 + data_part_len);
4988
4989 if (do_set_lengths) {
4990 if (data_part_len & 0x01) {
4991 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4992 } else {
4993 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4994 }
4995 }
4996
4997 if (!aead_multipart_internal_func(key_type_arg, key_data,
4998 alg_arg, nonce,
4999 additional_data, -1,
5000 input_data, data_part_len,
5001 set_lengths_method,
5002 expected_output,
5003 1, 0)) {
5004 break;
5005 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005006
5007 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005009
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 if (!aead_multipart_internal_func(key_type_arg, key_data,
5011 alg_arg, nonce,
5012 additional_data, -1,
5013 input_data, data_part_len,
5014 set_lengths_method,
5015 expected_output,
5016 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005017 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005019 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005020
Paul Elliott8fc45162021-06-23 16:06:01 +01005021 /* Goto is required to silence warnings about unused labels, as we
5022 * don't actually do any test assertions in this function. */
5023 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005024}
5025/* END_CASE */
5026
5027/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005028void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5029 int alg_arg,
5030 data_t *nonce,
5031 data_t *additional_data,
5032 data_t *input_data,
5033 int do_set_lengths,
5034 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005035{
Paul Elliottd3f82412021-06-16 16:52:21 +01005036 size_t ad_part_len = 0;
5037 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005038 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005039
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005041 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005043
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 if (do_set_lengths) {
5045 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005046 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005048 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005050 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005051
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 if (!aead_multipart_internal_func(key_type_arg, key_data,
5053 alg_arg, nonce,
5054 additional_data,
5055 ad_part_len,
5056 input_data, -1,
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(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 mbedtls_test_set_step(1000 + ad_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,
5069 ad_part_len,
5070 input_data, -1,
5071 set_lengths_method,
5072 expected_output,
5073 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005074 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005076 }
5077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005079 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005081
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 if (do_set_lengths) {
5083 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005084 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005086 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005088 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005089
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if (!aead_multipart_internal_func(key_type_arg, key_data,
5091 alg_arg, nonce,
5092 additional_data, -1,
5093 input_data, data_part_len,
5094 set_lengths_method,
5095 expected_output,
5096 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005097 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005099
5100 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005102
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 if (!aead_multipart_internal_func(key_type_arg, key_data,
5104 alg_arg, nonce,
5105 additional_data, -1,
5106 input_data, data_part_len,
5107 set_lengths_method,
5108 expected_output,
5109 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005110 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005112 }
5113
Paul Elliott8fc45162021-06-23 16:06:01 +01005114 /* Goto is required to silence warnings about unused labels, as we
5115 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005116 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005117}
5118/* END_CASE */
5119
5120/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005121void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5122 int alg_arg,
5123 int nonce_length,
5124 int expected_nonce_length_arg,
5125 data_t *additional_data,
5126 data_t *input_data,
5127 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005128{
5129
5130 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5131 psa_key_type_t key_type = key_type_arg;
5132 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005133 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005134 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5136 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005137 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005138 size_t actual_nonce_length = 0;
5139 size_t expected_nonce_length = expected_nonce_length_arg;
5140 unsigned char *output = NULL;
5141 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005142 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005143 size_t ciphertext_size = 0;
5144 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005145 size_t tag_length = 0;
5146 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5151 psa_set_key_algorithm(&attributes, alg);
5152 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005153
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5155 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005160
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005161 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005164
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005166
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005167 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005170
5171 /* If the operation is not supported, just skip and not fail in case the
5172 * encryption involves a common limitation of cryptography hardwares and
5173 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 if (status == PSA_ERROR_NOT_SUPPORTED) {
5175 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5176 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005177 }
5178
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5182 nonce_length,
5183 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 if (expected_status == PSA_SUCCESS) {
5190 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5191 alg));
5192 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005195
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005197 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5199 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005200
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5202 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005203
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5205 output, output_size,
5206 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005207
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5209 &ciphertext_length, tag_buffer,
5210 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005211 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005212
5213exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 psa_destroy_key(key);
5215 mbedtls_free(output);
5216 mbedtls_free(ciphertext);
5217 psa_aead_abort(&operation);
5218 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005219}
5220/* END_CASE */
5221
5222/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005223void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5224 int alg_arg,
5225 int nonce_length_arg,
5226 int set_lengths_method_arg,
5227 data_t *additional_data,
5228 data_t *input_data,
5229 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005230{
5231
5232 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5233 psa_key_type_t key_type = key_type_arg;
5234 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005235 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005236 uint8_t *nonce_buffer = NULL;
5237 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5238 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5239 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005240 unsigned char *output = NULL;
5241 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005242 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005243 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005244 size_t ciphertext_size = 0;
5245 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005246 size_t tag_length = 0;
5247 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005248 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005249 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005250
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005252
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5254 psa_set_key_algorithm(&attributes, alg);
5255 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005256
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5258 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005259
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005261
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005263
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005264 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005265
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005267
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005269
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005270 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005273
5274 /* If the operation is not supported, just skip and not fail in case the
5275 * encryption involves a common limitation of cryptography hardwares and
5276 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 if (status == PSA_ERROR_NOT_SUPPORTED) {
5278 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5279 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005280 }
5281
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005283
Paul Elliott4023ffd2021-09-10 16:21:22 +01005284 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 if (nonce_length_arg == -1) {
5286 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005287 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 nonce_length = 0;
5289 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005290 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005292 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005293
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 if (nonce_buffer) {
5295 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005296 nonce_buffer[index] = 'a' + index;
5297 }
Paul Elliott66696b52021-08-16 18:42:41 +01005298 }
Paul Elliott863864a2021-07-23 17:28:31 +01005299 }
5300
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5302 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5303 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005304 }
5305
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005307
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005309
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 if (expected_status == PSA_SUCCESS) {
5311 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5312 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5313 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005314 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005316 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 }
Paul Elliott863864a2021-07-23 17:28:31 +01005318
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005319 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5321 additional_data->len),
5322 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005323
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5325 output, output_size,
5326 &ciphertext_length),
5327 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5330 &ciphertext_length, tag_buffer,
5331 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5332 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005333 }
5334
5335exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 psa_destroy_key(key);
5337 mbedtls_free(output);
5338 mbedtls_free(ciphertext);
5339 mbedtls_free(nonce_buffer);
5340 psa_aead_abort(&operation);
5341 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005342}
5343/* END_CASE */
5344
5345/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005346void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005347 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005348 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005349 data_t *nonce,
5350 data_t *additional_data,
5351 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005353{
5354
5355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5356 psa_key_type_t key_type = key_type_arg;
5357 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005358 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5360 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5361 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005362 unsigned char *output = NULL;
5363 unsigned char *ciphertext = NULL;
5364 size_t output_size = output_size_arg;
5365 size_t ciphertext_size = 0;
5366 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005367 size_t tag_length = 0;
5368 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5369
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005371
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5373 psa_set_key_algorithm(&attributes, alg);
5374 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5377 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005378
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005380
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005381 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005382
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005384
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005385 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005388
5389 /* If the operation is not supported, just skip and not fail in case the
5390 * encryption involves a common limitation of cryptography hardwares and
5391 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 if (status == PSA_ERROR_NOT_SUPPORTED) {
5393 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5394 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005395 }
5396
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005398
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5400 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5405 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 status = psa_aead_update(&operation, input_data->x, input_data->len,
5408 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005409
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005413 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5415 &ciphertext_length, tag_buffer,
5416 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005417 }
5418
5419exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 psa_destroy_key(key);
5421 mbedtls_free(output);
5422 mbedtls_free(ciphertext);
5423 psa_aead_abort(&operation);
5424 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005425}
5426/* END_CASE */
5427
Paul Elliott91b021e2021-07-23 18:52:31 +01005428/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005429void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5430 int alg_arg,
5431 int finish_ciphertext_size_arg,
5432 int tag_size_arg,
5433 data_t *nonce,
5434 data_t *additional_data,
5435 data_t *input_data,
5436 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005437{
5438
5439 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5440 psa_key_type_t key_type = key_type_arg;
5441 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005442 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5444 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5445 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005446 unsigned char *ciphertext = NULL;
5447 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005448 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005449 size_t ciphertext_size = 0;
5450 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5452 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005453 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5458 psa_set_key_algorithm(&attributes, alg);
5459 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005460
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5462 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005467
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005468 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005469
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005470 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005471
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005472 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005475
5476 /* If the operation is not supported, just skip and not fail in case the
5477 * encryption involves a common limitation of cryptography hardwares and
5478 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 if (status == PSA_ERROR_NOT_SUPPORTED) {
5480 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5481 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005482 }
5483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005485
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005487
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5489 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005490
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5492 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005493
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5495 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005496
5497 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 status = psa_aead_finish(&operation, finish_ciphertext,
5499 finish_ciphertext_size,
5500 &ciphertext_length, tag_buffer,
5501 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005504
5505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 psa_destroy_key(key);
5507 mbedtls_free(ciphertext);
5508 mbedtls_free(finish_ciphertext);
5509 mbedtls_free(tag_buffer);
5510 psa_aead_abort(&operation);
5511 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005512}
5513/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005514
5515/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005516void aead_multipart_verify(int key_type_arg, data_t *key_data,
5517 int alg_arg,
5518 data_t *nonce,
5519 data_t *additional_data,
5520 data_t *input_data,
5521 data_t *tag,
5522 int tag_usage_arg,
5523 int expected_setup_status_arg,
5524 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005525{
5526 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5527 psa_key_type_t key_type = key_type_arg;
5528 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005529 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5531 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5532 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005533 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005534 unsigned char *plaintext = NULL;
5535 unsigned char *finish_plaintext = NULL;
5536 size_t plaintext_size = 0;
5537 size_t plaintext_length = 0;
5538 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005539 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005540 unsigned char *tag_buffer = NULL;
5541 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5546 psa_set_key_algorithm(&attributes, alg);
5547 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005548
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5550 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5555 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005556
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005557 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005558
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005560
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005561 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005562
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005564
5565 /* If the operation is not supported, just skip and not fail in case the
5566 * encryption involves a common limitation of cryptography hardwares and
5567 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 if (status == PSA_ERROR_NOT_SUPPORTED) {
5569 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5570 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005571 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005573
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005575 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 }
Paul Elliott9961a662021-09-17 19:19:02 +01005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 status = psa_aead_set_lengths(&operation, additional_data->len,
5583 input_data->len);
5584 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5587 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5590 input_data->len,
5591 plaintext, plaintext_size,
5592 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005595 tag_buffer = tag->x;
5596 tag_size = tag->len;
5597 }
5598
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 status = psa_aead_verify(&operation, finish_plaintext,
5600 verify_plaintext_size,
5601 &plaintext_length,
5602 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005605
5606exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 psa_destroy_key(key);
5608 mbedtls_free(plaintext);
5609 mbedtls_free(finish_plaintext);
5610 psa_aead_abort(&operation);
5611 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005612}
5613/* END_CASE */
5614
Paul Elliott9961a662021-09-17 19:19:02 +01005615/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005616void aead_multipart_setup(int key_type_arg, data_t *key_data,
5617 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005618{
5619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5620 psa_key_type_t key_type = key_type_arg;
5621 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005622 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5624 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5625 psa_status_t expected_status = expected_status_arg;
5626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 psa_set_key_usage_flags(&attributes,
5630 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5631 psa_set_key_algorithm(&attributes, alg);
5632 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5635 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005636
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005644
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005646
5647exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 psa_destroy_key(key);
5649 psa_aead_abort(&operation);
5650 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005651}
5652/* END_CASE */
5653
5654/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005655void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5656 int alg_arg,
5657 data_t *nonce,
5658 data_t *additional_data,
5659 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005660{
5661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5662 psa_key_type_t key_type = key_type_arg;
5663 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005664 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005665 unsigned char *output_data = NULL;
5666 unsigned char *final_data = NULL;
5667 size_t output_size = 0;
5668 size_t finish_output_size = 0;
5669 size_t output_length = 0;
5670 size_t key_bits = 0;
5671 size_t tag_length = 0;
5672 size_t tag_size = 0;
5673 size_t nonce_length = 0;
5674 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5675 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5676 size_t output_part_length = 0;
5677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 psa_set_key_usage_flags(&attributes,
5682 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5683 psa_set_key_algorithm(&attributes, alg);
5684 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5687 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5690 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005695
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005697
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005698 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005704 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005705
5706 /* Test all operations error without calling setup first. */
5707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5709 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5714 PSA_AEAD_NONCE_MAX_SIZE,
5715 &nonce_length),
5716 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005719
Paul Elliott481be342021-07-16 17:38:47 +01005720 /* ------------------------------------------------------- */
5721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5723 input_data->len),
5724 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005727
Paul Elliott481be342021-07-16 17:38:47 +01005728 /* ------------------------------------------------------- */
5729
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5731 additional_data->len),
5732 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005733
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005735
Paul Elliott481be342021-07-16 17:38:47 +01005736 /* ------------------------------------------------------- */
5737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5739 input_data->len, output_data,
5740 output_size, &output_length),
5741 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005744
Paul Elliott481be342021-07-16 17:38:47 +01005745 /* ------------------------------------------------------- */
5746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5748 finish_output_size,
5749 &output_part_length,
5750 tag_buffer, tag_length,
5751 &tag_size),
5752 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005755
Paul Elliott481be342021-07-16 17:38:47 +01005756 /* ------------------------------------------------------- */
5757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5759 finish_output_size,
5760 &output_part_length,
5761 tag_buffer,
5762 tag_length),
5763 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005764
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005766
5767 /* Test for double setups. */
5768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
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 Elliott481be342021-07-16 17:38:47 +01005776 /* ------------------------------------------------------- */
5777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5781 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005782
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005784
Paul Elliott374a2be2021-07-16 17:53:40 +01005785 /* ------------------------------------------------------- */
5786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5790 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005793
5794 /* ------------------------------------------------------- */
5795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5799 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005802
Paul Elliottc23a9a02021-06-21 18:32:46 +01005803 /* Test for not setting a nonce. */
5804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5808 additional_data->len),
5809 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005812
Paul Elliott7f628422021-09-01 12:08:29 +01005813 /* ------------------------------------------------------- */
5814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5818 input_data->len, output_data,
5819 output_size, &output_length),
5820 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005823
Paul Elliottbdc2c682021-09-21 18:37:10 +01005824 /* ------------------------------------------------------- */
5825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5829 finish_output_size,
5830 &output_part_length,
5831 tag_buffer, tag_length,
5832 &tag_size),
5833 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005834
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005836
5837 /* ------------------------------------------------------- */
5838
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005840
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5842 finish_output_size,
5843 &output_part_length,
5844 tag_buffer,
5845 tag_length),
5846 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005849
Paul Elliottc23a9a02021-06-21 18:32:46 +01005850 /* Test for double setting nonce. */
5851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5857 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005860
Paul Elliott374a2be2021-07-16 17:53:40 +01005861 /* Test for double generating nonce. */
5862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5866 PSA_AEAD_NONCE_MAX_SIZE,
5867 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5870 PSA_AEAD_NONCE_MAX_SIZE,
5871 &nonce_length),
5872 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005873
5874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005876
5877 /* Test for generate nonce then set and vice versa */
5878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005880
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5882 PSA_AEAD_NONCE_MAX_SIZE,
5883 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5886 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005889
Andrzej Kurekad837522021-12-15 15:28:49 +01005890 /* Test for generating nonce after calling set lengths */
5891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5895 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005896
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5898 PSA_AEAD_NONCE_MAX_SIZE,
5899 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005900
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005902
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005903 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 if (operation.alg == PSA_ALG_CCM) {
5908 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5909 input_data->len),
5910 PSA_ERROR_INVALID_ARGUMENT);
5911 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5912 PSA_AEAD_NONCE_MAX_SIZE,
5913 &nonce_length),
5914 PSA_ERROR_BAD_STATE);
5915 } else {
5916 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5917 input_data->len));
5918 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5919 PSA_AEAD_NONCE_MAX_SIZE,
5920 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005921 }
5922
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005924
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005925 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005926#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005928
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5930 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5931 input_data->len),
5932 PSA_ERROR_INVALID_ARGUMENT);
5933 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5934 PSA_AEAD_NONCE_MAX_SIZE,
5935 &nonce_length),
5936 PSA_ERROR_BAD_STATE);
5937 } else {
5938 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5939 input_data->len));
5940 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5941 PSA_AEAD_NONCE_MAX_SIZE,
5942 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005943 }
5944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005946#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005947
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005948 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005949
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5953 PSA_AEAD_NONCE_MAX_SIZE,
5954 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005955
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 if (operation.alg == PSA_ALG_CCM) {
5957 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5958 input_data->len),
5959 PSA_ERROR_INVALID_ARGUMENT);
5960 } else {
5961 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5962 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005963 }
5964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005966
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005967 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005968 /* Test for setting nonce after calling set lengths */
5969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5973 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005978
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005979 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
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 if (operation.alg == PSA_ALG_CCM) {
5984 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5985 input_data->len),
5986 PSA_ERROR_INVALID_ARGUMENT);
5987 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5988 PSA_ERROR_BAD_STATE);
5989 } else {
5990 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5991 input_data->len));
5992 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005993 }
5994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005996
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005997 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005998#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6002 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6003 input_data->len),
6004 PSA_ERROR_INVALID_ARGUMENT);
6005 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6006 PSA_ERROR_BAD_STATE);
6007 } else {
6008 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6009 input_data->len));
6010 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006011 }
6012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006014#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006015
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006016 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006017
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 if (operation.alg == PSA_ALG_CCM) {
6023 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6024 input_data->len),
6025 PSA_ERROR_INVALID_ARGUMENT);
6026 } else {
6027 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6028 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006029 }
6030
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006032
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006033 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006034#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 if (operation.alg == PSA_ALG_GCM) {
6038 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6039 SIZE_MAX),
6040 PSA_ERROR_INVALID_ARGUMENT);
6041 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6042 PSA_ERROR_BAD_STATE);
6043 } else if (operation.alg != PSA_ALG_CCM) {
6044 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6045 SIZE_MAX));
6046 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006047 }
6048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006050#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006051
Tom Cosgrove1797b052022-12-04 17:19:59 +00006052 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006053#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 if (operation.alg == PSA_ALG_GCM) {
6059 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6060 SIZE_MAX),
6061 PSA_ERROR_INVALID_ARGUMENT);
6062 } else if (operation.alg != PSA_ALG_CCM) {
6063 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6064 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006065 }
6066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006068#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006069
6070 /* ------------------------------------------------------- */
6071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6077 PSA_AEAD_NONCE_MAX_SIZE,
6078 &nonce_length),
6079 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006080
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006082
Paul Elliott7220cae2021-06-22 17:25:57 +01006083 /* Test for generating nonce in decrypt setup. */
6084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6088 PSA_AEAD_NONCE_MAX_SIZE,
6089 &nonce_length),
6090 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006093
Paul Elliottc23a9a02021-06-21 18:32:46 +01006094 /* Test for setting lengths twice. */
6095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006099
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6101 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6104 input_data->len),
6105 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006108
Andrzej Kurekad837522021-12-15 15:28:49 +01006109 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006110
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6118 additional_data->len),
6119 PSA_ERROR_BAD_STATE);
6120 } else {
6121 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6122 additional_data->len));
6123
6124 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6125 input_data->len),
6126 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006127 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006129
6130 /* ------------------------------------------------------- */
6131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 if (operation.alg == PSA_ALG_CCM) {
6137 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6138 input_data->len, output_data,
6139 output_size, &output_length),
6140 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006141
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 } else {
6143 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6144 input_data->len, output_data,
6145 output_size, &output_length));
6146
6147 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6148 input_data->len),
6149 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006150 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006152
6153 /* ------------------------------------------------------- */
6154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 if (operation.alg == PSA_ALG_CCM) {
6160 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6161 finish_output_size,
6162 &output_part_length,
6163 tag_buffer, tag_length,
6164 &tag_size));
6165 } else {
6166 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6167 finish_output_size,
6168 &output_part_length,
6169 tag_buffer, tag_length,
6170 &tag_size));
6171
6172 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6173 input_data->len),
6174 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006175 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006177
6178 /* Test for setting lengths after generating nonce + already starting data. */
6179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006181
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6183 PSA_AEAD_NONCE_MAX_SIZE,
6184 &nonce_length));
6185 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006186
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6188 additional_data->len),
6189 PSA_ERROR_BAD_STATE);
6190 } else {
6191 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6192 additional_data->len));
6193
6194 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6195 input_data->len),
6196 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006197 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006199
6200 /* ------------------------------------------------------- */
6201
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6205 PSA_AEAD_NONCE_MAX_SIZE,
6206 &nonce_length));
6207 if (operation.alg == PSA_ALG_CCM) {
6208 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6209 input_data->len, output_data,
6210 output_size, &output_length),
6211 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 } else {
6214 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6215 input_data->len, output_data,
6216 output_size, &output_length));
6217
6218 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6219 input_data->len),
6220 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006221 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006223
6224 /* ------------------------------------------------------- */
6225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6229 PSA_AEAD_NONCE_MAX_SIZE,
6230 &nonce_length));
6231 if (operation.alg == PSA_ALG_CCM) {
6232 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6233 finish_output_size,
6234 &output_part_length,
6235 tag_buffer, tag_length,
6236 &tag_size));
6237 } else {
6238 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6239 finish_output_size,
6240 &output_part_length,
6241 tag_buffer, tag_length,
6242 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6245 input_data->len),
6246 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006247 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006249
Paul Elliott243080c2021-07-21 19:01:17 +01006250 /* Test for not sending any additional data or data after setting non zero
6251 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6258 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6261 finish_output_size,
6262 &output_part_length,
6263 tag_buffer, tag_length,
6264 &tag_size),
6265 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006268
Paul Elliott243080c2021-07-21 19:01:17 +01006269 /* Test for not sending any additional data or data after setting non-zero
6270 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6277 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6280 finish_output_size,
6281 &output_part_length,
6282 tag_buffer,
6283 tag_length),
6284 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006287
Paul Elliott243080c2021-07-21 19:01:17 +01006288 /* Test for not sending any additional data after setting a non-zero length
6289 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6296 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6299 input_data->len, output_data,
6300 output_size, &output_length),
6301 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006304
Paul Elliottf94bd992021-09-19 18:15:59 +01006305 /* Test for not sending any data after setting a non-zero length for it.*/
6306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6312 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6315 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6318 finish_output_size,
6319 &output_part_length,
6320 tag_buffer, tag_length,
6321 &tag_size),
6322 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006325
Paul Elliottb0450fe2021-09-01 15:06:26 +01006326 /* Test for sending too much additional data after setting lengths. */
6327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006333
6334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6336 additional_data->len),
6337 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006340
Paul Elliotta2a09b02021-09-22 14:56:40 +01006341 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6348 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6351 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6354 1),
6355 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006358
Paul Elliottb0450fe2021-09-01 15:06:26 +01006359 /* Test for sending too much data after setting lengths. */
6360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6368 input_data->len, output_data,
6369 output_size, &output_length),
6370 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006371
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006373
Paul Elliotta2a09b02021-09-22 14:56:40 +01006374 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6381 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6384 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6387 input_data->len, output_data,
6388 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6391 1, output_data,
6392 output_size, &output_length),
6393 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006394
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006396
Paul Elliottc23a9a02021-06-21 18:32:46 +01006397 /* Test sending additional data after data. */
6398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006402
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 if (operation.alg != PSA_ALG_CCM) {
6404 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6405 input_data->len, output_data,
6406 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6409 additional_data->len),
6410 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006411 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006413
Paul Elliott534d0b42021-06-22 19:15:20 +01006414 /* Test calling finish on decryption. */
6415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6421 finish_output_size,
6422 &output_part_length,
6423 tag_buffer, tag_length,
6424 &tag_size),
6425 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006428
6429 /* Test calling verify on encryption. */
6430
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6436 finish_output_size,
6437 &output_part_length,
6438 tag_buffer,
6439 tag_length),
6440 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006441
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006443
6444
Paul Elliottc23a9a02021-06-21 18:32:46 +01006445exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 psa_destroy_key(key);
6447 psa_aead_abort(&operation);
6448 mbedtls_free(output_data);
6449 mbedtls_free(final_data);
6450 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006451}
6452/* END_CASE */
6453
6454/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006455void signature_size(int type_arg,
6456 int bits,
6457 int alg_arg,
6458 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006459{
6460 psa_key_type_t type = type_arg;
6461 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006463
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006465
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466exit:
6467 ;
6468}
6469/* END_CASE */
6470
6471/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006472void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6473 int alg_arg, data_t *input_data,
6474 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006475{
Ronald Cron5425a212020-08-04 14:58:35 +02006476 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006477 psa_key_type_t key_type = key_type_arg;
6478 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006479 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006480 unsigned char *signature = NULL;
6481 size_t signature_size;
6482 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006484
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006486
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6488 psa_set_key_algorithm(&attributes, alg);
6489 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6492 &key));
6493 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6494 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006495
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006496 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006497 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6499 key_bits, alg);
6500 TEST_ASSERT(signature_size != 0);
6501 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006502 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006503
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006504 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 PSA_ASSERT(psa_sign_hash(key, alg,
6506 input_data->x, input_data->len,
6507 signature, signature_size,
6508 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006509 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006510 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006511 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006512
6513exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006514 /*
6515 * Key attributes may have been returned by psa_get_key_attributes()
6516 * thus reset them as required.
6517 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 psa_destroy_key(key);
6521 mbedtls_free(signature);
6522 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006523}
6524/* END_CASE */
6525
Paul Elliott712d5122022-12-07 14:03:10 +00006526/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006527/**
6528 * sign_hash_interruptible() test intentions:
6529 *
6530 * Note: This test can currently only handle ECDSA.
6531 *
6532 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006533 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006534 *
6535 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6536 * expected for different max_ops values.
6537 *
6538 * 3. Test that the number of ops done prior to start and after abort is zero
6539 * and that each successful stage completes some ops (this is not mandated by
6540 * the PSA specification, but is currently the case).
6541 *
6542 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6543 * complete() calls does not alter the number of ops returned.
6544 */
Paul Elliott712d5122022-12-07 14:03:10 +00006545void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6546 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006547 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006548{
6549 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6550 psa_key_type_t key_type = key_type_arg;
6551 psa_algorithm_t alg = alg_arg;
6552 size_t key_bits;
6553 unsigned char *signature = NULL;
6554 size_t signature_size;
6555 size_t signature_length = 0xdeadbeef;
6556 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6557 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006558 uint32_t num_ops = 0;
6559 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006560 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006561 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006562 size_t min_completes = 0;
6563 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006564
Paul Elliott712d5122022-12-07 14:03:10 +00006565 psa_sign_hash_interruptible_operation_t operation =
6566 psa_sign_hash_interruptible_operation_init();
6567
6568 PSA_ASSERT(psa_crypto_init());
6569
6570 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6571 psa_set_key_algorithm(&attributes, alg);
6572 psa_set_key_type(&attributes, key_type);
6573
6574 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6575 &key));
6576 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6577 key_bits = psa_get_key_bits(&attributes);
6578
6579 /* Allocate a buffer which has the size advertised by the
6580 * library. */
6581 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6582 key_bits, alg);
6583 TEST_ASSERT(signature_size != 0);
6584 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006585 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006586
Paul Elliott0c683352022-12-16 19:16:56 +00006587 psa_interruptible_set_max_ops(max_ops);
6588
Paul Elliott6f600372023-02-06 18:41:05 +00006589 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6590 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006591
Paul Elliott712d5122022-12-07 14:03:10 +00006592 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6593 TEST_ASSERT(num_ops_prior == 0);
6594
6595 /* Start performing the signature. */
6596 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6597 input_data->x, input_data->len));
6598
6599 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6600 TEST_ASSERT(num_ops_prior == 0);
6601
6602 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006603 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006604 status = psa_sign_hash_complete(&operation, signature, signature_size,
6605 &signature_length);
6606
Paul Elliott0c683352022-12-16 19:16:56 +00006607 num_completes++;
6608
Paul Elliott712d5122022-12-07 14:03:10 +00006609 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6610 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006611 /* We are asserting here that every complete makes progress
6612 * (completes some ops), which is true of the internal
6613 * implementation and probably any implementation, however this is
6614 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006615 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006616
Paul Elliott712d5122022-12-07 14:03:10 +00006617 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006618
6619 /* Ensure calling get_num_ops() twice still returns the same
6620 * number of ops as previously reported. */
6621 num_ops = psa_sign_hash_get_num_ops(&operation);
6622
6623 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006624 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006625 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006626
6627 TEST_ASSERT(status == PSA_SUCCESS);
6628
Paul Elliott0c683352022-12-16 19:16:56 +00006629 TEST_LE_U(min_completes, num_completes);
6630 TEST_LE_U(num_completes, max_completes);
6631
Paul Elliott712d5122022-12-07 14:03:10 +00006632 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006633 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006634 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006635
6636 PSA_ASSERT(psa_sign_hash_abort(&operation));
6637
Paul Elliott59ad9452022-12-18 15:09:02 +00006638 num_ops = psa_sign_hash_get_num_ops(&operation);
6639 TEST_ASSERT(num_ops == 0);
6640
Paul Elliott712d5122022-12-07 14:03:10 +00006641exit:
6642
6643 /*
6644 * Key attributes may have been returned by psa_get_key_attributes()
6645 * thus reset them as required.
6646 */
6647 psa_reset_key_attributes(&attributes);
6648
6649 psa_destroy_key(key);
6650 mbedtls_free(signature);
6651 PSA_DONE();
6652}
6653/* END_CASE */
6654
Gilles Peskine20035e32018-02-03 22:44:14 +01006655/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006656void sign_hash_fail(int key_type_arg, data_t *key_data,
6657 int alg_arg, data_t *input_data,
6658 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006659{
Ronald Cron5425a212020-08-04 14:58:35 +02006660 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006661 psa_key_type_t key_type = key_type_arg;
6662 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006663 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006664 psa_status_t actual_status;
6665 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006666 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006667 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006669
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006670 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006671
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006673
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6675 psa_set_key_algorithm(&attributes, alg);
6676 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006677
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6679 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006680
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 actual_status = psa_sign_hash(key, alg,
6682 input_data->x, input_data->len,
6683 signature, signature_size,
6684 &signature_length);
6685 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006686 /* The value of *signature_length is unspecified on error, but
6687 * whatever it is, it should be less than signature_size, so that
6688 * if the caller tries to read *signature_length bytes without
6689 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006691
6692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 psa_reset_key_attributes(&attributes);
6694 psa_destroy_key(key);
6695 mbedtls_free(signature);
6696 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006697}
6698/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006699
Paul Elliott91007972022-12-16 12:21:24 +00006700/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006701/**
6702 * sign_hash_fail_interruptible() test intentions:
6703 *
6704 * Note: This test can currently only handle ECDSA.
6705 *
6706 * 1. Test that various failure cases for interruptible sign hash fail with the
6707 * correct error codes, and at the correct point (at start or during
6708 * complete).
6709 *
6710 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6711 * expected for different max_ops values.
6712 *
6713 * 3. Test that the number of ops done prior to start and after abort is zero
6714 * and that each successful stage completes some ops (this is not mandated by
6715 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006716 *
6717 * 4. Check that calling complete() when start() fails and complete()
6718 * after completion results in a BAD_STATE error.
6719 *
6720 * 5. Check that calling start() again after start fails results in a BAD_STATE
6721 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006722 */
Paul Elliott91007972022-12-16 12:21:24 +00006723void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6724 int alg_arg, data_t *input_data,
6725 int signature_size_arg,
6726 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006727 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006728 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006729{
6730 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6731 psa_key_type_t key_type = key_type_arg;
6732 psa_algorithm_t alg = alg_arg;
6733 size_t signature_size = signature_size_arg;
6734 psa_status_t actual_status;
6735 psa_status_t expected_start_status = expected_start_status_arg;
6736 psa_status_t expected_complete_status = expected_complete_status_arg;
6737 unsigned char *signature = NULL;
6738 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006739 uint32_t num_ops = 0;
6740 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006741 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006742 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006743 size_t min_completes = 0;
6744 size_t max_completes = 0;
6745
Paul Elliott91007972022-12-16 12:21:24 +00006746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6747 psa_sign_hash_interruptible_operation_t operation =
6748 psa_sign_hash_interruptible_operation_init();
6749
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006750 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006751
6752 PSA_ASSERT(psa_crypto_init());
6753
6754 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6755 psa_set_key_algorithm(&attributes, alg);
6756 psa_set_key_type(&attributes, key_type);
6757
6758 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6759 &key));
6760
Paul Elliott0c683352022-12-16 19:16:56 +00006761 psa_interruptible_set_max_ops(max_ops);
6762
Paul Elliott6f600372023-02-06 18:41:05 +00006763 interruptible_signverify_get_minmax_completes(max_ops,
6764 expected_complete_status,
6765 &min_completes,
6766 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006767
Paul Elliott91007972022-12-16 12:21:24 +00006768 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6769 TEST_ASSERT(num_ops_prior == 0);
6770
6771 /* Start performing the signature. */
6772 actual_status = psa_sign_hash_start(&operation, key, alg,
6773 input_data->x, input_data->len);
6774
6775 TEST_EQUAL(actual_status, expected_start_status);
6776
Paul Elliottc9774412023-02-06 15:14:07 +00006777 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006778 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006779 * start failed. */
6780 actual_status = psa_sign_hash_complete(&operation, signature,
6781 signature_size,
6782 &signature_length);
6783
6784 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6785
6786 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006787 actual_status = psa_sign_hash_start(&operation, key, alg,
6788 input_data->x, input_data->len);
6789
6790 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6791 }
6792
Paul Elliott91007972022-12-16 12:21:24 +00006793 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6794 TEST_ASSERT(num_ops_prior == 0);
6795
Paul Elliott91007972022-12-16 12:21:24 +00006796 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006797 do {
Paul Elliott91007972022-12-16 12:21:24 +00006798 actual_status = psa_sign_hash_complete(&operation, signature,
6799 signature_size,
6800 &signature_length);
6801
Paul Elliott0c683352022-12-16 19:16:56 +00006802 num_completes++;
6803
Paul Elliott334d7262023-01-20 17:29:41 +00006804 if (actual_status == PSA_SUCCESS ||
6805 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006806 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006807 /* We are asserting here that every complete makes progress
6808 * (completes some ops), which is true of the internal
6809 * implementation and probably any implementation, however this is
6810 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006811 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006812
Paul Elliott91007972022-12-16 12:21:24 +00006813 num_ops_prior = num_ops;
6814 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006815 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006816
Paul Elliottc9774412023-02-06 15:14:07 +00006817 TEST_EQUAL(actual_status, expected_complete_status);
6818
Paul Elliottefebad02023-02-15 16:56:45 +00006819 /* Check that another complete returns BAD_STATE. */
6820 actual_status = psa_sign_hash_complete(&operation, signature,
6821 signature_size,
6822 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006823
Paul Elliottefebad02023-02-15 16:56:45 +00006824 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006825
Paul Elliott91007972022-12-16 12:21:24 +00006826 PSA_ASSERT(psa_sign_hash_abort(&operation));
6827
Paul Elliott59ad9452022-12-18 15:09:02 +00006828 num_ops = psa_sign_hash_get_num_ops(&operation);
6829 TEST_ASSERT(num_ops == 0);
6830
Paul Elliott91007972022-12-16 12:21:24 +00006831 /* The value of *signature_length is unspecified on error, but
6832 * whatever it is, it should be less than signature_size, so that
6833 * if the caller tries to read *signature_length bytes without
6834 * checking the error code then they don't overflow a buffer. */
6835 TEST_LE_U(signature_length, signature_size);
6836
Paul Elliott0c683352022-12-16 19:16:56 +00006837 TEST_LE_U(min_completes, num_completes);
6838 TEST_LE_U(num_completes, max_completes);
6839
Paul Elliott91007972022-12-16 12:21:24 +00006840exit:
6841 psa_reset_key_attributes(&attributes);
6842 psa_destroy_key(key);
6843 mbedtls_free(signature);
6844 PSA_DONE();
6845}
6846/* END_CASE */
6847
mohammad16038cc1cee2018-03-28 01:21:33 +03006848/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006849void sign_verify_hash(int key_type_arg, data_t *key_data,
6850 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006851{
Ronald Cron5425a212020-08-04 14:58:35 +02006852 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006853 psa_key_type_t key_type = key_type_arg;
6854 psa_algorithm_t alg = alg_arg;
6855 size_t key_bits;
6856 unsigned char *signature = NULL;
6857 size_t signature_size;
6858 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006860
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006862
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6864 psa_set_key_algorithm(&attributes, alg);
6865 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006866
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6868 &key));
6869 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6870 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006871
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006872 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006873 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6875 key_bits, alg);
6876 TEST_ASSERT(signature_size != 0);
6877 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006878 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006879
6880 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 PSA_ASSERT(psa_sign_hash(key, alg,
6882 input_data->x, input_data->len,
6883 signature, signature_size,
6884 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006885 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 TEST_LE_U(signature_length, signature_size);
6887 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006888
6889 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 PSA_ASSERT(psa_verify_hash(key, alg,
6891 input_data->x, input_data->len,
6892 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006893
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006895 /* Flip a bit in the input and verify that the signature is now
6896 * detected as invalid. Flip a bit at the beginning, not at the end,
6897 * because ECDSA may ignore the last few bits of the input. */
6898 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 TEST_EQUAL(psa_verify_hash(key, alg,
6900 input_data->x, input_data->len,
6901 signature, signature_length),
6902 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006903 }
6904
6905exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006906 /*
6907 * Key attributes may have been returned by psa_get_key_attributes()
6908 * thus reset them as required.
6909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006911
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 psa_destroy_key(key);
6913 mbedtls_free(signature);
6914 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006915}
6916/* END_CASE */
6917
Paul Elliott712d5122022-12-07 14:03:10 +00006918/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006919/**
6920 * sign_verify_hash_interruptible() test intentions:
6921 *
6922 * Note: This test can currently only handle ECDSA.
6923 *
Paul Elliott8c092052023-03-06 17:49:14 +00006924 * 1. Test that we can sign an input hash with the given keypair and then
6925 * afterwards verify that signature. This is currently the only way to test
6926 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006927 *
6928 * 2. Test that after corrupting the hash, the verification detects an invalid
6929 * signature.
6930 *
6931 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6932 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006933 *
6934 * 4. Test that the number of ops done prior to starting signing and after abort
6935 * is zero and that each successful signing stage completes some ops (this is
6936 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006937 */
Paul Elliott712d5122022-12-07 14:03:10 +00006938void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006939 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006940 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006941{
6942 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6943 psa_key_type_t key_type = key_type_arg;
6944 psa_algorithm_t alg = alg_arg;
6945 size_t key_bits;
6946 unsigned char *signature = NULL;
6947 size_t signature_size;
6948 size_t signature_length = 0xdeadbeef;
6949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6950 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006951 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006952 uint32_t num_ops = 0;
6953 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006954 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006955 size_t min_completes = 0;
6956 size_t max_completes = 0;
6957
Paul Elliott712d5122022-12-07 14:03:10 +00006958 psa_sign_hash_interruptible_operation_t sign_operation =
6959 psa_sign_hash_interruptible_operation_init();
6960 psa_verify_hash_interruptible_operation_t verify_operation =
6961 psa_verify_hash_interruptible_operation_init();
6962
6963 PSA_ASSERT(psa_crypto_init());
6964
Paul Elliott0c683352022-12-16 19:16:56 +00006965 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6966 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006967 psa_set_key_algorithm(&attributes, alg);
6968 psa_set_key_type(&attributes, key_type);
6969
6970 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6971 &key));
6972 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6973 key_bits = psa_get_key_bits(&attributes);
6974
6975 /* Allocate a buffer which has the size advertised by the
6976 * library. */
6977 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6978 key_bits, alg);
6979 TEST_ASSERT(signature_size != 0);
6980 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006981 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006982
Paul Elliott0c683352022-12-16 19:16:56 +00006983 psa_interruptible_set_max_ops(max_ops);
6984
Paul Elliott6f600372023-02-06 18:41:05 +00006985 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6986 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006987
Paul Elliott7c173082023-02-26 18:44:45 +00006988 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6989 TEST_ASSERT(num_ops_prior == 0);
6990
Paul Elliott712d5122022-12-07 14:03:10 +00006991 /* Start performing the signature. */
6992 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6993 input_data->x, input_data->len));
6994
Paul Elliott7c173082023-02-26 18:44:45 +00006995 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6996 TEST_ASSERT(num_ops_prior == 0);
6997
Paul Elliott712d5122022-12-07 14:03:10 +00006998 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006999 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007000
Paul Elliott0c683352022-12-16 19:16:56 +00007001 status = psa_sign_hash_complete(&sign_operation, signature,
7002 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007003 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007004
7005 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007006
7007 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7008 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7009 /* We are asserting here that every complete makes progress
7010 * (completes some ops), which is true of the internal
7011 * implementation and probably any implementation, however this is
7012 * not mandated by the PSA specification. */
7013 TEST_ASSERT(num_ops > num_ops_prior);
7014
7015 num_ops_prior = num_ops;
7016 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007017 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007018
7019 TEST_ASSERT(status == PSA_SUCCESS);
7020
Paul Elliott0c683352022-12-16 19:16:56 +00007021 TEST_LE_U(min_completes, num_completes);
7022 TEST_LE_U(num_completes, max_completes);
7023
Paul Elliott712d5122022-12-07 14:03:10 +00007024 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7025
Paul Elliott7c173082023-02-26 18:44:45 +00007026 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7027 TEST_ASSERT(num_ops == 0);
7028
Paul Elliott712d5122022-12-07 14:03:10 +00007029 /* Check that the signature length looks sensible. */
7030 TEST_LE_U(signature_length, signature_size);
7031 TEST_ASSERT(signature_length > 0);
7032
Paul Elliott0c683352022-12-16 19:16:56 +00007033 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007034
7035 /* Start verification. */
7036 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7037 input_data->x, input_data->len,
7038 signature, signature_length));
7039
7040 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007041 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007042 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007043
7044 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007045 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007046
7047 TEST_ASSERT(status == PSA_SUCCESS);
7048
Paul Elliott0c683352022-12-16 19:16:56 +00007049 TEST_LE_U(min_completes, num_completes);
7050 TEST_LE_U(num_completes, max_completes);
7051
Paul Elliott712d5122022-12-07 14:03:10 +00007052 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7053
7054 verify_operation = psa_verify_hash_interruptible_operation_init();
7055
7056 if (input_data->len != 0) {
7057 /* Flip a bit in the input and verify that the signature is now
7058 * detected as invalid. Flip a bit at the beginning, not at the end,
7059 * because ECDSA may ignore the last few bits of the input. */
7060 input_data->x[0] ^= 1;
7061
Paul Elliott712d5122022-12-07 14:03:10 +00007062 /* Start verification. */
7063 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7064 input_data->x, input_data->len,
7065 signature, signature_length));
7066
7067 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007068 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007069 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007070 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007071
7072 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7073 }
7074
7075 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7076
7077exit:
7078 /*
7079 * Key attributes may have been returned by psa_get_key_attributes()
7080 * thus reset them as required.
7081 */
7082 psa_reset_key_attributes(&attributes);
7083
7084 psa_destroy_key(key);
7085 mbedtls_free(signature);
7086 PSA_DONE();
7087}
7088/* END_CASE */
7089
Gilles Peskine9911b022018-06-29 17:30:48 +02007090/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007091void verify_hash(int key_type_arg, data_t *key_data,
7092 int alg_arg, data_t *hash_data,
7093 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007094{
Ronald Cron5425a212020-08-04 14:58:35 +02007095 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007096 psa_key_type_t key_type = key_type_arg;
7097 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007098 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007099
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007101
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007103
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7105 psa_set_key_algorithm(&attributes, alg);
7106 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007107
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7109 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 PSA_ASSERT(psa_verify_hash(key, alg,
7112 hash_data->x, hash_data->len,
7113 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007114
itayzafrir5c753392018-05-08 11:18:38 +03007115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 psa_reset_key_attributes(&attributes);
7117 psa_destroy_key(key);
7118 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007119}
7120/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007121
Paul Elliott712d5122022-12-07 14:03:10 +00007122/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007123/**
7124 * verify_hash_interruptible() test intentions:
7125 *
7126 * Note: This test can currently only handle ECDSA.
7127 *
7128 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007129 * only). Given this test only does verification it can accept public keys as
7130 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007131 *
7132 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7133 * expected for different max_ops values.
7134 *
7135 * 3. Test that the number of ops done prior to start and after abort is zero
7136 * and that each successful stage completes some ops (this is not mandated by
7137 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007138 *
7139 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7140 * complete() calls does not alter the number of ops returned.
7141 *
7142 * 5. Test that after corrupting the hash, the verification detects an invalid
7143 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007144 */
Paul Elliott712d5122022-12-07 14:03:10 +00007145void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7146 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007147 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007148{
7149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7150 psa_key_type_t key_type = key_type_arg;
7151 psa_algorithm_t alg = alg_arg;
7152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7153 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007154 uint32_t num_ops = 0;
7155 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007156 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007157 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007158 size_t min_completes = 0;
7159 size_t max_completes = 0;
7160
Paul Elliott712d5122022-12-07 14:03:10 +00007161 psa_verify_hash_interruptible_operation_t operation =
7162 psa_verify_hash_interruptible_operation_init();
7163
7164 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7165
7166 PSA_ASSERT(psa_crypto_init());
7167
7168 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7169 psa_set_key_algorithm(&attributes, alg);
7170 psa_set_key_type(&attributes, key_type);
7171
7172 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7173 &key));
7174
Paul Elliott0c683352022-12-16 19:16:56 +00007175 psa_interruptible_set_max_ops(max_ops);
7176
Paul Elliott6f600372023-02-06 18:41:05 +00007177 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7178 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007179
Paul Elliott712d5122022-12-07 14:03:10 +00007180 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7181
7182 TEST_ASSERT(num_ops_prior == 0);
7183
7184 /* Start verification. */
7185 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7186 hash_data->x, hash_data->len,
7187 signature_data->x, signature_data->len)
7188 );
7189
7190 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7191
7192 TEST_ASSERT(num_ops_prior == 0);
7193
7194 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007195 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007196 status = psa_verify_hash_complete(&operation);
7197
Paul Elliott0c683352022-12-16 19:16:56 +00007198 num_completes++;
7199
Paul Elliott712d5122022-12-07 14:03:10 +00007200 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7201 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007202 /* We are asserting here that every complete makes progress
7203 * (completes some ops), which is true of the internal
7204 * implementation and probably any implementation, however this is
7205 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007206 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007207
Paul Elliott712d5122022-12-07 14:03:10 +00007208 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007209
7210 /* Ensure calling get_num_ops() twice still returns the same
7211 * number of ops as previously reported. */
7212 num_ops = psa_verify_hash_get_num_ops(&operation);
7213
7214 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007215 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007216 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007217
7218 TEST_ASSERT(status == PSA_SUCCESS);
7219
Paul Elliott0c683352022-12-16 19:16:56 +00007220 TEST_LE_U(min_completes, num_completes);
7221 TEST_LE_U(num_completes, max_completes);
7222
Paul Elliott712d5122022-12-07 14:03:10 +00007223 PSA_ASSERT(psa_verify_hash_abort(&operation));
7224
Paul Elliott59ad9452022-12-18 15:09:02 +00007225 num_ops = psa_verify_hash_get_num_ops(&operation);
7226 TEST_ASSERT(num_ops == 0);
7227
Paul Elliott8359c142023-02-24 18:40:10 +00007228 if (hash_data->len != 0) {
7229 /* Flip a bit in the hash and verify that the signature is now detected
7230 * as invalid. Flip a bit at the beginning, not at the end, because
7231 * ECDSA may ignore the last few bits of the input. */
7232 hash_data->x[0] ^= 1;
7233
7234 /* Start verification. */
7235 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7236 hash_data->x, hash_data->len,
7237 signature_data->x, signature_data->len));
7238
7239 /* Continue performing the signature until complete. */
7240 do {
7241 status = psa_verify_hash_complete(&operation);
7242 } while (status == PSA_OPERATION_INCOMPLETE);
7243
7244 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7245 }
7246
Paul Elliott712d5122022-12-07 14:03:10 +00007247exit:
7248 psa_reset_key_attributes(&attributes);
7249 psa_destroy_key(key);
7250 PSA_DONE();
7251}
7252/* END_CASE */
7253
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007254/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007255void verify_hash_fail(int key_type_arg, data_t *key_data,
7256 int alg_arg, data_t *hash_data,
7257 data_t *signature_data,
7258 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007259{
Ronald Cron5425a212020-08-04 14:58:35 +02007260 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007261 psa_key_type_t key_type = key_type_arg;
7262 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007263 psa_status_t actual_status;
7264 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007265 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007266
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007268
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7270 psa_set_key_algorithm(&attributes, alg);
7271 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007272
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7274 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007275
Gilles Peskine449bd832023-01-11 14:50:10 +01007276 actual_status = psa_verify_hash(key, alg,
7277 hash_data->x, hash_data->len,
7278 signature_data->x, signature_data->len);
7279 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007280
7281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 psa_reset_key_attributes(&attributes);
7283 psa_destroy_key(key);
7284 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007285}
7286/* END_CASE */
7287
Paul Elliott91007972022-12-16 12:21:24 +00007288/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007289/**
7290 * verify_hash_fail_interruptible() test intentions:
7291 *
7292 * Note: This test can currently only handle ECDSA.
7293 *
7294 * 1. Test that various failure cases for interruptible verify hash fail with
7295 * the correct error codes, and at the correct point (at start or during
7296 * complete).
7297 *
7298 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7299 * expected for different max_ops values.
7300 *
7301 * 3. Test that the number of ops done prior to start and after abort is zero
7302 * and that each successful stage completes some ops (this is not mandated by
7303 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007304 *
7305 * 4. Check that calling complete() when start() fails and complete()
7306 * after completion results in a BAD_STATE error.
7307 *
7308 * 5. Check that calling start() again after start fails results in a BAD_STATE
7309 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007310 */
Paul Elliott91007972022-12-16 12:21:24 +00007311void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7312 int alg_arg, data_t *hash_data,
7313 data_t *signature_data,
7314 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007315 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007316 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007317{
7318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7319 psa_key_type_t key_type = key_type_arg;
7320 psa_algorithm_t alg = alg_arg;
7321 psa_status_t actual_status;
7322 psa_status_t expected_start_status = expected_start_status_arg;
7323 psa_status_t expected_complete_status = expected_complete_status_arg;
7324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007325 uint32_t num_ops = 0;
7326 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007327 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007328 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007329 size_t min_completes = 0;
7330 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007331 psa_verify_hash_interruptible_operation_t operation =
7332 psa_verify_hash_interruptible_operation_init();
7333
7334 PSA_ASSERT(psa_crypto_init());
7335
7336 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7337 psa_set_key_algorithm(&attributes, alg);
7338 psa_set_key_type(&attributes, key_type);
7339
7340 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7341 &key));
7342
Paul Elliott0c683352022-12-16 19:16:56 +00007343 psa_interruptible_set_max_ops(max_ops);
7344
Paul Elliott6f600372023-02-06 18:41:05 +00007345 interruptible_signverify_get_minmax_completes(max_ops,
7346 expected_complete_status,
7347 &min_completes,
7348 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007349
Paul Elliott91007972022-12-16 12:21:24 +00007350 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7351 TEST_ASSERT(num_ops_prior == 0);
7352
7353 /* Start verification. */
7354 actual_status = psa_verify_hash_start(&operation, key, alg,
7355 hash_data->x, hash_data->len,
7356 signature_data->x,
7357 signature_data->len);
7358
7359 TEST_EQUAL(actual_status, expected_start_status);
7360
Paul Elliottc9774412023-02-06 15:14:07 +00007361 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007362 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007363 * start failed. */
7364 actual_status = psa_verify_hash_complete(&operation);
7365
7366 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7367
7368 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007369 actual_status = psa_verify_hash_start(&operation, key, alg,
7370 hash_data->x, hash_data->len,
7371 signature_data->x,
7372 signature_data->len);
7373
7374 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7375 }
7376
Paul Elliott91007972022-12-16 12:21:24 +00007377 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7378 TEST_ASSERT(num_ops_prior == 0);
7379
Paul Elliott91007972022-12-16 12:21:24 +00007380 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007381 do {
Paul Elliott91007972022-12-16 12:21:24 +00007382 actual_status = psa_verify_hash_complete(&operation);
7383
Paul Elliott0c683352022-12-16 19:16:56 +00007384 num_completes++;
7385
Paul Elliott334d7262023-01-20 17:29:41 +00007386 if (actual_status == PSA_SUCCESS ||
7387 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007388 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007389 /* We are asserting here that every complete makes progress
7390 * (completes some ops), which is true of the internal
7391 * implementation and probably any implementation, however this is
7392 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007393 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007394
Paul Elliott91007972022-12-16 12:21:24 +00007395 num_ops_prior = num_ops;
7396 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007397 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007398
Paul Elliottc9774412023-02-06 15:14:07 +00007399 TEST_EQUAL(actual_status, expected_complete_status);
7400
Paul Elliottefebad02023-02-15 16:56:45 +00007401 /* Check that another complete returns BAD_STATE. */
7402 actual_status = psa_verify_hash_complete(&operation);
7403 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007404
Paul Elliott0c683352022-12-16 19:16:56 +00007405 TEST_LE_U(min_completes, num_completes);
7406 TEST_LE_U(num_completes, max_completes);
7407
Paul Elliott91007972022-12-16 12:21:24 +00007408 PSA_ASSERT(psa_verify_hash_abort(&operation));
7409
Paul Elliott59ad9452022-12-18 15:09:02 +00007410 num_ops = psa_verify_hash_get_num_ops(&operation);
7411 TEST_ASSERT(num_ops == 0);
7412
Paul Elliott91007972022-12-16 12:21:24 +00007413exit:
7414 psa_reset_key_attributes(&attributes);
7415 psa_destroy_key(key);
7416 PSA_DONE();
7417}
7418/* END_CASE */
7419
Paul Elliott20a36062022-12-18 13:21:25 +00007420/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007421/**
7422 * interruptible_signverify_hash_state_test() test intentions:
7423 *
7424 * Note: This test can currently only handle ECDSA.
7425 *
7426 * 1. Test that calling the various interruptible sign and verify hash functions
7427 * in incorrect orders returns BAD_STATE errors.
7428 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007429void interruptible_signverify_hash_state_test(int key_type_arg,
7430 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007431{
7432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7433 psa_key_type_t key_type = key_type_arg;
7434 psa_algorithm_t alg = alg_arg;
7435 size_t key_bits;
7436 unsigned char *signature = NULL;
7437 size_t signature_size;
7438 size_t signature_length = 0xdeadbeef;
7439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7440 psa_sign_hash_interruptible_operation_t sign_operation =
7441 psa_sign_hash_interruptible_operation_init();
7442 psa_verify_hash_interruptible_operation_t verify_operation =
7443 psa_verify_hash_interruptible_operation_init();
7444
7445 PSA_ASSERT(psa_crypto_init());
7446
7447 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7448 PSA_KEY_USAGE_VERIFY_HASH);
7449 psa_set_key_algorithm(&attributes, alg);
7450 psa_set_key_type(&attributes, key_type);
7451
7452 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7453 &key));
7454 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7455 key_bits = psa_get_key_bits(&attributes);
7456
7457 /* Allocate a buffer which has the size advertised by the
7458 * library. */
7459 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7460 key_bits, alg);
7461 TEST_ASSERT(signature_size != 0);
7462 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007463 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007464
7465 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7466
7467 /* --- Attempt completes prior to starts --- */
7468 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7469 signature_size,
7470 &signature_length),
7471 PSA_ERROR_BAD_STATE);
7472
7473 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7474
7475 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7476 PSA_ERROR_BAD_STATE);
7477
7478 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7479
7480 /* --- Aborts in all other places. --- */
7481 psa_sign_hash_abort(&sign_operation);
7482
7483 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7484 input_data->x, input_data->len));
7485
7486 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7487
7488 psa_interruptible_set_max_ops(1);
7489
7490 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7491 input_data->x, input_data->len));
7492
7493 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7494 signature_size,
7495 &signature_length),
7496 PSA_OPERATION_INCOMPLETE);
7497
7498 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7499
7500 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7501
7502 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7503 input_data->x, input_data->len));
7504
7505 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7506 signature_size,
7507 &signature_length));
7508
7509 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7510
7511 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7512
7513 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7514 input_data->x, input_data->len,
7515 signature, signature_length));
7516
7517 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7518
7519 psa_interruptible_set_max_ops(1);
7520
7521 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7522 input_data->x, input_data->len,
7523 signature, signature_length));
7524
7525 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7526 PSA_OPERATION_INCOMPLETE);
7527
7528 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7529
7530 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7531
7532 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7533 input_data->x, input_data->len,
7534 signature, signature_length));
7535
7536 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7537
7538 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7539
7540 /* --- Attempt double starts. --- */
7541
7542 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7543 input_data->x, input_data->len));
7544
7545 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7546 input_data->x, input_data->len),
7547 PSA_ERROR_BAD_STATE);
7548
7549 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7550
7551 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7552 input_data->x, input_data->len,
7553 signature, signature_length));
7554
7555 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7556 input_data->x, input_data->len,
7557 signature, signature_length),
7558 PSA_ERROR_BAD_STATE);
7559
7560 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7561
Paul Elliott76d671a2023-02-07 17:45:18 +00007562exit:
7563 /*
7564 * Key attributes may have been returned by psa_get_key_attributes()
7565 * thus reset them as required.
7566 */
7567 psa_reset_key_attributes(&attributes);
7568
7569 psa_destroy_key(key);
7570 mbedtls_free(signature);
7571 PSA_DONE();
7572}
7573/* END_CASE */
7574
7575/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007576/**
Paul Elliottc2033502023-02-26 17:09:14 +00007577 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007578 *
7579 * Note: This test can currently only handle ECDSA.
7580 *
7581 * 1. Test various edge cases in the interruptible sign and verify hash
7582 * interfaces.
7583 */
Paul Elliottc2033502023-02-26 17:09:14 +00007584void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007585 data_t *key_data, int alg_arg, data_t *input_data)
7586{
7587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7588 psa_key_type_t key_type = key_type_arg;
7589 psa_algorithm_t alg = alg_arg;
7590 size_t key_bits;
7591 unsigned char *signature = NULL;
7592 size_t signature_size;
7593 size_t signature_length = 0xdeadbeef;
7594 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7595 uint8_t *input_buffer = NULL;
7596 psa_sign_hash_interruptible_operation_t sign_operation =
7597 psa_sign_hash_interruptible_operation_init();
7598 psa_verify_hash_interruptible_operation_t verify_operation =
7599 psa_verify_hash_interruptible_operation_init();
7600
7601 PSA_ASSERT(psa_crypto_init());
7602
7603 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7604 PSA_KEY_USAGE_VERIFY_HASH);
7605 psa_set_key_algorithm(&attributes, alg);
7606 psa_set_key_type(&attributes, key_type);
7607
7608 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7609 &key));
7610 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7611 key_bits = psa_get_key_bits(&attributes);
7612
7613 /* Allocate a buffer which has the size advertised by the
7614 * library. */
7615 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7616 key_bits, alg);
7617 TEST_ASSERT(signature_size != 0);
7618 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007619 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007620
Paul Elliott20a36062022-12-18 13:21:25 +00007621 /* --- Change function inputs mid run, to cause an error (sign only,
7622 * verify passes all inputs to start. --- */
7623
7624 psa_interruptible_set_max_ops(1);
7625
7626 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7627 input_data->x, input_data->len));
7628
7629 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7630 signature_size,
7631 &signature_length),
7632 PSA_OPERATION_INCOMPLETE);
7633
7634 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7635 0,
7636 &signature_length),
7637 PSA_ERROR_BUFFER_TOO_SMALL);
7638
Paul Elliottc9774412023-02-06 15:14:07 +00007639 /* And test that this invalidates the operation. */
7640 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7641 0,
7642 &signature_length),
7643 PSA_ERROR_BAD_STATE);
7644
Paul Elliott20a36062022-12-18 13:21:25 +00007645 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7646
Paul Elliottf9c91a72023-02-05 18:06:38 +00007647 /* Trash the hash buffer in between start and complete, to ensure
7648 * no reliance on external buffers. */
7649 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7650
Paul Elliott6c68df42023-10-23 15:33:37 +01007651 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007652
7653 memcpy(input_buffer, input_data->x, input_data->len);
7654
7655 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7656 input_buffer, input_data->len));
7657
7658 memset(input_buffer, '!', input_data->len);
7659 mbedtls_free(input_buffer);
7660 input_buffer = NULL;
7661
7662 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7663 signature_size,
7664 &signature_length));
7665
7666 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7667
Paul Elliott6c68df42023-10-23 15:33:37 +01007668 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007669
7670 memcpy(input_buffer, input_data->x, input_data->len);
7671
7672 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7673 input_buffer, input_data->len,
7674 signature, signature_length));
7675
7676 memset(input_buffer, '!', input_data->len);
7677 mbedtls_free(input_buffer);
7678 input_buffer = NULL;
7679
7680 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7681
7682 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7683
Paul Elliott20a36062022-12-18 13:21:25 +00007684exit:
7685 /*
7686 * Key attributes may have been returned by psa_get_key_attributes()
7687 * thus reset them as required.
7688 */
7689 psa_reset_key_attributes(&attributes);
7690
7691 psa_destroy_key(key);
7692 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007693 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007694 PSA_DONE();
7695}
7696/* END_CASE */
7697
Paul Elliotta4cb9092023-02-07 18:01:55 +00007698/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007699/**
Paul Elliott57702242023-02-26 20:36:10 +00007700 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007701 *
7702 * Note: This test can currently only handle ECDSA.
7703 *
7704 * 1. Test that setting max ops is reflected in both interruptible sign and
7705 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007706 * 2. Test that changing the value of max_ops to unlimited during an operation
7707 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007708 *
7709 * 3. Test that calling get_num_ops() between complete calls gives the same
7710 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007711 */
Paul Elliott57702242023-02-26 20:36:10 +00007712void interruptible_signverify_hash_ops_tests(int key_type_arg,
7713 data_t *key_data, int alg_arg,
7714 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007715{
7716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7717 psa_key_type_t key_type = key_type_arg;
7718 psa_algorithm_t alg = alg_arg;
7719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007720 size_t key_bits;
7721 unsigned char *signature = NULL;
7722 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007723 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007724 uint32_t num_ops = 0;
7725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7726
Paul Elliotta4cb9092023-02-07 18:01:55 +00007727 psa_sign_hash_interruptible_operation_t sign_operation =
7728 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007729 psa_verify_hash_interruptible_operation_t verify_operation =
7730 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007731
7732 PSA_ASSERT(psa_crypto_init());
7733
7734 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7735 PSA_KEY_USAGE_VERIFY_HASH);
7736 psa_set_key_algorithm(&attributes, alg);
7737 psa_set_key_type(&attributes, key_type);
7738
Paul Elliottf1743e22023-02-15 18:44:16 +00007739 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7740 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7741 key_bits = psa_get_key_bits(&attributes);
7742
7743 /* Allocate a buffer which has the size advertised by the
7744 * library. */
7745 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7746
7747 TEST_ASSERT(signature_size != 0);
7748 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007749 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007750
7751 /* Check that default max ops gets set if we don't set it. */
7752 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7753 input_data->x, input_data->len));
7754
7755 TEST_EQUAL(psa_interruptible_get_max_ops(),
7756 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7757
7758 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7759
Paul Elliottf1743e22023-02-15 18:44:16 +00007760 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7761 input_data->x, input_data->len,
7762 signature, signature_size));
7763
7764 TEST_EQUAL(psa_interruptible_get_max_ops(),
7765 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7766
7767 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7768
Paul Elliotta4cb9092023-02-07 18:01:55 +00007769 /* Check that max ops gets set properly. */
7770
7771 psa_interruptible_set_max_ops(0xbeef);
7772
Paul Elliottf1743e22023-02-15 18:44:16 +00007773 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007774
Paul Elliott9e8819f2023-02-26 19:01:35 +00007775 /* --- Ensure changing the max ops mid operation works (operation should
7776 * complete successfully after setting max ops to unlimited --- */
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 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7783 signature_size,
7784 &signature_length),
7785 PSA_OPERATION_INCOMPLETE);
7786
7787 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7788
7789 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7790 signature_size,
7791 &signature_length));
7792
7793 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7794
7795 psa_interruptible_set_max_ops(1);
7796
7797 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7798 input_data->x, input_data->len,
7799 signature, signature_length));
7800
7801 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7802 PSA_OPERATION_INCOMPLETE);
7803
7804 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7805
7806 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7807
7808 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7809
Paul Elliottc1e04002023-02-26 20:27:23 +00007810 /* --- Test that not calling get_num_ops inbetween complete calls does not
7811 * result in lost ops. ---*/
7812
7813 psa_interruptible_set_max_ops(1);
7814
7815 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7816 input_data->x, input_data->len));
7817
7818 /* Continue performing the signature until complete. */
7819 do {
7820 status = psa_sign_hash_complete(&sign_operation, signature,
7821 signature_size,
7822 &signature_length);
7823
7824 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7825
7826 } while (status == PSA_OPERATION_INCOMPLETE);
7827
7828 PSA_ASSERT(status);
7829
7830 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7831
7832 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7833 input_data->x, input_data->len));
7834
7835 /* Continue performing the signature until complete. */
7836 do {
7837 status = psa_sign_hash_complete(&sign_operation, signature,
7838 signature_size,
7839 &signature_length);
7840 } while (status == PSA_OPERATION_INCOMPLETE);
7841
7842 PSA_ASSERT(status);
7843
7844 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7845
7846 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7847
7848 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7849 input_data->x, input_data->len,
7850 signature, signature_length));
7851
7852 /* Continue performing the verification until complete. */
7853 do {
7854 status = psa_verify_hash_complete(&verify_operation);
7855
7856 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7857
7858 } while (status == PSA_OPERATION_INCOMPLETE);
7859
7860 PSA_ASSERT(status);
7861
7862 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7863
7864 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7865 input_data->x, input_data->len,
7866 signature, signature_length));
7867
7868 /* Continue performing the verification until complete. */
7869 do {
7870 status = psa_verify_hash_complete(&verify_operation);
7871
7872 } while (status == PSA_OPERATION_INCOMPLETE);
7873
7874 PSA_ASSERT(status);
7875
7876 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7877
7878 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7879
Paul Elliotta4cb9092023-02-07 18:01:55 +00007880exit:
7881 /*
7882 * Key attributes may have been returned by psa_get_key_attributes()
7883 * thus reset them as required.
7884 */
7885 psa_reset_key_attributes(&attributes);
7886
7887 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007888 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007889 PSA_DONE();
7890}
7891/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007892
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007893/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007894void sign_message_deterministic(int key_type_arg,
7895 data_t *key_data,
7896 int alg_arg,
7897 data_t *input_data,
7898 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007899{
7900 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7901 psa_key_type_t key_type = key_type_arg;
7902 psa_algorithm_t alg = alg_arg;
7903 size_t key_bits;
7904 unsigned char *signature = NULL;
7905 size_t signature_size;
7906 size_t signature_length = 0xdeadbeef;
7907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7908
Gilles Peskine449bd832023-01-11 14:50:10 +01007909 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007910
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7912 psa_set_key_algorithm(&attributes, alg);
7913 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007914
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7916 &key));
7917 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7918 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007919
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7921 TEST_ASSERT(signature_size != 0);
7922 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007923 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007924
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 PSA_ASSERT(psa_sign_message(key, alg,
7926 input_data->x, input_data->len,
7927 signature, signature_size,
7928 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007929
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007930 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007931 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007932
7933exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007935
Gilles Peskine449bd832023-01-11 14:50:10 +01007936 psa_destroy_key(key);
7937 mbedtls_free(signature);
7938 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007939
7940}
7941/* END_CASE */
7942
7943/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007944void sign_message_fail(int key_type_arg,
7945 data_t *key_data,
7946 int alg_arg,
7947 data_t *input_data,
7948 int signature_size_arg,
7949 int expected_status_arg)
7950{
7951 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7952 psa_key_type_t key_type = key_type_arg;
7953 psa_algorithm_t alg = alg_arg;
7954 size_t signature_size = signature_size_arg;
7955 psa_status_t actual_status;
7956 psa_status_t expected_status = expected_status_arg;
7957 unsigned char *signature = NULL;
7958 size_t signature_length = 0xdeadbeef;
7959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7960
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007961 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007962
7963 PSA_ASSERT(psa_crypto_init());
7964
7965 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7966 psa_set_key_algorithm(&attributes, alg);
7967 psa_set_key_type(&attributes, key_type);
7968
7969 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7970 &key));
7971
7972 actual_status = psa_sign_message(key, alg,
7973 input_data->x, input_data->len,
7974 signature, signature_size,
7975 &signature_length);
7976 TEST_EQUAL(actual_status, expected_status);
7977 /* The value of *signature_length is unspecified on error, but
7978 * whatever it is, it should be less than signature_size, so that
7979 * if the caller tries to read *signature_length bytes without
7980 * checking the error code then they don't overflow a buffer. */
7981 TEST_LE_U(signature_length, signature_size);
7982
7983exit:
7984 psa_reset_key_attributes(&attributes);
7985 psa_destroy_key(key);
7986 mbedtls_free(signature);
7987 PSA_DONE();
7988}
7989/* END_CASE */
7990
7991/* BEGIN_CASE */
7992void sign_verify_message(int key_type_arg,
7993 data_t *key_data,
7994 int alg_arg,
7995 data_t *input_data)
7996{
7997 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7998 psa_key_type_t key_type = key_type_arg;
7999 psa_algorithm_t alg = alg_arg;
8000 size_t key_bits;
8001 unsigned char *signature = NULL;
8002 size_t signature_size;
8003 size_t signature_length = 0xdeadbeef;
8004 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8005
8006 PSA_ASSERT(psa_crypto_init());
8007
8008 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8009 PSA_KEY_USAGE_VERIFY_MESSAGE);
8010 psa_set_key_algorithm(&attributes, alg);
8011 psa_set_key_type(&attributes, key_type);
8012
8013 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8014 &key));
8015 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8016 key_bits = psa_get_key_bits(&attributes);
8017
8018 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8019 TEST_ASSERT(signature_size != 0);
8020 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008021 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008022
8023 PSA_ASSERT(psa_sign_message(key, alg,
8024 input_data->x, input_data->len,
8025 signature, signature_size,
8026 &signature_length));
8027 TEST_LE_U(signature_length, signature_size);
8028 TEST_ASSERT(signature_length > 0);
8029
8030 PSA_ASSERT(psa_verify_message(key, alg,
8031 input_data->x, input_data->len,
8032 signature, signature_length));
8033
8034 if (input_data->len != 0) {
8035 /* Flip a bit in the input and verify that the signature is now
8036 * detected as invalid. Flip a bit at the beginning, not at the end,
8037 * because ECDSA may ignore the last few bits of the input. */
8038 input_data->x[0] ^= 1;
8039 TEST_EQUAL(psa_verify_message(key, alg,
8040 input_data->x, input_data->len,
8041 signature, signature_length),
8042 PSA_ERROR_INVALID_SIGNATURE);
8043 }
8044
8045exit:
8046 psa_reset_key_attributes(&attributes);
8047
8048 psa_destroy_key(key);
8049 mbedtls_free(signature);
8050 PSA_DONE();
8051}
8052/* END_CASE */
8053
8054/* BEGIN_CASE */
8055void verify_message(int key_type_arg,
8056 data_t *key_data,
8057 int alg_arg,
8058 data_t *input_data,
8059 data_t *signature_data)
8060{
8061 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8062 psa_key_type_t key_type = key_type_arg;
8063 psa_algorithm_t alg = alg_arg;
8064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8065
8066 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8067
8068 PSA_ASSERT(psa_crypto_init());
8069
8070 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8071 psa_set_key_algorithm(&attributes, alg);
8072 psa_set_key_type(&attributes, key_type);
8073
8074 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8075 &key));
8076
8077 PSA_ASSERT(psa_verify_message(key, alg,
8078 input_data->x, input_data->len,
8079 signature_data->x, signature_data->len));
8080
8081exit:
8082 psa_reset_key_attributes(&attributes);
8083 psa_destroy_key(key);
8084 PSA_DONE();
8085}
8086/* END_CASE */
8087
8088/* BEGIN_CASE */
8089void verify_message_fail(int key_type_arg,
8090 data_t *key_data,
8091 int alg_arg,
8092 data_t *hash_data,
8093 data_t *signature_data,
8094 int expected_status_arg)
8095{
8096 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8097 psa_key_type_t key_type = key_type_arg;
8098 psa_algorithm_t alg = alg_arg;
8099 psa_status_t actual_status;
8100 psa_status_t expected_status = expected_status_arg;
8101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8102
8103 PSA_ASSERT(psa_crypto_init());
8104
8105 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8106 psa_set_key_algorithm(&attributes, alg);
8107 psa_set_key_type(&attributes, key_type);
8108
8109 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8110 &key));
8111
8112 actual_status = psa_verify_message(key, alg,
8113 hash_data->x, hash_data->len,
8114 signature_data->x,
8115 signature_data->len);
8116 TEST_EQUAL(actual_status, expected_status);
8117
8118exit:
8119 psa_reset_key_attributes(&attributes);
8120 psa_destroy_key(key);
8121 PSA_DONE();
8122}
8123/* END_CASE */
8124
8125/* BEGIN_CASE */
8126void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008127 data_t *key_data,
8128 int alg_arg,
8129 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008130 data_t *label,
8131 int expected_output_length_arg,
8132 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008133{
Ronald Cron5425a212020-08-04 14:58:35 +02008134 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008135 psa_key_type_t key_type = key_type_arg;
8136 psa_algorithm_t alg = alg_arg;
8137 size_t expected_output_length = expected_output_length_arg;
8138 size_t key_bits;
8139 unsigned char *output = NULL;
8140 size_t output_size;
8141 size_t output_length = ~0;
8142 psa_status_t actual_status;
8143 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008145
Gilles Peskine449bd832023-01-11 14:50:10 +01008146 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008147
Gilles Peskine656896e2018-06-29 19:12:28 +02008148 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8150 psa_set_key_algorithm(&attributes, alg);
8151 psa_set_key_type(&attributes, key_type);
8152 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8153 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008154
8155 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008156 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8157 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008158
Gilles Peskine449bd832023-01-11 14:50:10 +01008159 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8160 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008161 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008162
8163 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008164 actual_status = psa_asymmetric_encrypt(key, alg,
8165 input_data->x, input_data->len,
8166 label->x, label->len,
8167 output, output_size,
8168 &output_length);
8169 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008170 if (actual_status == PSA_SUCCESS) {
8171 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008172 } else {
8173 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008174 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008175
Gilles Peskine68428122018-06-30 18:42:41 +02008176 /* If the label is empty, the test framework puts a non-null pointer
8177 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008179 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008180 if (output_size != 0) {
8181 memset(output, 0, output_size);
8182 }
8183 actual_status = psa_asymmetric_encrypt(key, alg,
8184 input_data->x, input_data->len,
8185 NULL, label->len,
8186 output, output_size,
8187 &output_length);
8188 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008189 if (actual_status == PSA_SUCCESS) {
8190 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008191 } else {
8192 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008193 }
Gilles Peskine68428122018-06-30 18:42:41 +02008194 }
8195
Gilles Peskine656896e2018-06-29 19:12:28 +02008196exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008197 /*
8198 * Key attributes may have been returned by psa_get_key_attributes()
8199 * thus reset them as required.
8200 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008201 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008202
Gilles Peskine449bd832023-01-11 14:50:10 +01008203 psa_destroy_key(key);
8204 mbedtls_free(output);
8205 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008206}
8207/* END_CASE */
8208
8209/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008210void asymmetric_encrypt_decrypt(int key_type_arg,
8211 data_t *key_data,
8212 int alg_arg,
8213 data_t *input_data,
8214 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008215{
Ronald Cron5425a212020-08-04 14:58:35 +02008216 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008217 psa_key_type_t key_type = key_type_arg;
8218 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008219 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008220 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008221 size_t output_size;
8222 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008223 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008224 size_t output2_size;
8225 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008227
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008229
Gilles Peskine449bd832023-01-11 14:50:10 +01008230 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8231 psa_set_key_algorithm(&attributes, alg);
8232 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008233
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8235 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008236
8237 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008238 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8239 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008240
Gilles Peskine449bd832023-01-11 14:50:10 +01008241 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8242 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008243 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008244
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008245 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 TEST_LE_U(output2_size,
8247 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8248 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008249 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008250
Gilles Peskineeebd7382018-06-08 18:11:54 +02008251 /* We test encryption by checking that encrypt-then-decrypt gives back
8252 * the original plaintext because of the non-optional random
8253 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008254 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8255 input_data->x, input_data->len,
8256 label->x, label->len,
8257 output, output_size,
8258 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008259 /* We don't know what ciphertext length to expect, but check that
8260 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008262
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8264 output, output_length,
8265 label->x, label->len,
8266 output2, output2_size,
8267 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008268 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008269 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008270
8271exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008272 /*
8273 * Key attributes may have been returned by psa_get_key_attributes()
8274 * thus reset them as required.
8275 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008277
Gilles Peskine449bd832023-01-11 14:50:10 +01008278 psa_destroy_key(key);
8279 mbedtls_free(output);
8280 mbedtls_free(output2);
8281 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008282}
8283/* END_CASE */
8284
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008285/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008286void asymmetric_decrypt(int key_type_arg,
8287 data_t *key_data,
8288 int alg_arg,
8289 data_t *input_data,
8290 data_t *label,
8291 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008292{
Ronald Cron5425a212020-08-04 14:58:35 +02008293 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008294 psa_key_type_t key_type = key_type_arg;
8295 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008296 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008297 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008298 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008299 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008301
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008303
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8305 psa_set_key_algorithm(&attributes, alg);
8306 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008307
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8309 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008310
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8312 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008313
8314 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008315 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8316 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008317 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008318
Gilles Peskine449bd832023-01-11 14:50:10 +01008319 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8320 input_data->x, input_data->len,
8321 label->x, label->len,
8322 output,
8323 output_size,
8324 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008325 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008326 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008327
Gilles Peskine68428122018-06-30 18:42:41 +02008328 /* If the label is empty, the test framework puts a non-null pointer
8329 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008330 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008331 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008332 if (output_size != 0) {
8333 memset(output, 0, output_size);
8334 }
8335 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8336 input_data->x, input_data->len,
8337 NULL, label->len,
8338 output,
8339 output_size,
8340 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008341 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008342 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008343 }
8344
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008345exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008346 psa_reset_key_attributes(&attributes);
8347 psa_destroy_key(key);
8348 mbedtls_free(output);
8349 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008350}
8351/* END_CASE */
8352
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008353/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008354void asymmetric_decrypt_fail(int key_type_arg,
8355 data_t *key_data,
8356 int alg_arg,
8357 data_t *input_data,
8358 data_t *label,
8359 int output_size_arg,
8360 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008361{
Ronald Cron5425a212020-08-04 14:58:35 +02008362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008363 psa_key_type_t key_type = key_type_arg;
8364 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008365 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008366 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008367 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008368 psa_status_t actual_status;
8369 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008371
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008372 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008373
Gilles Peskine449bd832023-01-11 14:50:10 +01008374 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008375
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8377 psa_set_key_algorithm(&attributes, alg);
8378 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008379
Gilles Peskine449bd832023-01-11 14:50:10 +01008380 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8381 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008382
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 actual_status = psa_asymmetric_decrypt(key, alg,
8384 input_data->x, input_data->len,
8385 label->x, label->len,
8386 output, output_size,
8387 &output_length);
8388 TEST_EQUAL(actual_status, expected_status);
8389 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008390
Gilles Peskine68428122018-06-30 18:42:41 +02008391 /* If the label is empty, the test framework puts a non-null pointer
8392 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008394 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008395 if (output_size != 0) {
8396 memset(output, 0, output_size);
8397 }
8398 actual_status = psa_asymmetric_decrypt(key, alg,
8399 input_data->x, input_data->len,
8400 NULL, label->len,
8401 output, output_size,
8402 &output_length);
8403 TEST_EQUAL(actual_status, expected_status);
8404 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008405 }
8406
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008407exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 psa_reset_key_attributes(&attributes);
8409 psa_destroy_key(key);
8410 mbedtls_free(output);
8411 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008412}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008413/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008414
8415/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008416void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008417{
8418 /* Test each valid way of initializing the object, except for `= {0}`, as
8419 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8420 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008421 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008422 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008424 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8425 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008426
Gilles Peskine449bd832023-01-11 14:50:10 +01008427 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008428
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008429 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8431 PSA_ERROR_BAD_STATE);
8432 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8433 PSA_ERROR_BAD_STATE);
8434 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8435 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008437 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 PSA_ASSERT(psa_key_derivation_abort(&func));
8439 PSA_ASSERT(psa_key_derivation_abort(&init));
8440 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008441}
8442/* END_CASE */
8443
Janos Follath16de4a42019-06-13 16:32:24 +01008444/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008445void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008446{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008447 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008448 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008449 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008450
Gilles Peskine449bd832023-01-11 14:50:10 +01008451 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008452
Gilles Peskine449bd832023-01-11 14:50:10 +01008453 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8454 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008455
8456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008457 psa_key_derivation_abort(&operation);
8458 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008459}
8460/* END_CASE */
8461
Janos Follathaf3c2a02019-06-12 12:34:34 +01008462/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008463void derive_set_capacity(int alg_arg, int capacity_arg,
8464 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008465{
8466 psa_algorithm_t alg = alg_arg;
8467 size_t capacity = capacity_arg;
8468 psa_status_t expected_status = expected_status_arg;
8469 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8470
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008472
Gilles Peskine449bd832023-01-11 14:50:10 +01008473 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008474
Gilles Peskine449bd832023-01-11 14:50:10 +01008475 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8476 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008477
8478exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008479 psa_key_derivation_abort(&operation);
8480 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008481}
8482/* END_CASE */
8483
8484/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308485void parse_binary_string_test(data_t *input, int output)
8486{
8487 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308488 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308489 TEST_EQUAL(value, output);
8490}
8491/* END_CASE */
8492
8493/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008494void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308495 int step_arg1, int key_type_arg1, data_t *input1,
8496 int expected_status_arg1,
8497 int step_arg2, int key_type_arg2, data_t *input2,
8498 int expected_status_arg2,
8499 int step_arg3, int key_type_arg3, data_t *input3,
8500 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008501 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008502{
8503 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308505 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008506 psa_status_t expected_statuses[] = { expected_status_arg1,
8507 expected_status_arg2,
8508 expected_status_arg3 };
8509 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008510 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8511 MBEDTLS_SVC_KEY_ID_INIT,
8512 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008513 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8515 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008516 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008517 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008518 psa_status_t expected_output_status = expected_output_status_arg;
8519 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008520
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008522
Gilles Peskine449bd832023-01-11 14:50:10 +01008523 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8524 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008525
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008527
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8529 mbedtls_test_set_step(i);
8530 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008531 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308532 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8533 key_types[i] != INPUT_INTEGER) {
8534 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 PSA_ASSERT(psa_import_key(&attributes,
8536 inputs[i]->x, inputs[i]->len,
8537 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308538 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008539 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008540 // When taking a private key as secret input, use key agreement
8541 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8543 &operation, keys[i]),
8544 expected_statuses[i]);
8545 } else {
8546 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8547 keys[i]),
8548 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008549 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308551 if (key_types[i] == INPUT_INTEGER) {
8552 TEST_EQUAL(psa_key_derivation_input_integer(
8553 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308554 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308555 expected_statuses[i]);
8556 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308557 TEST_EQUAL(psa_key_derivation_input_bytes(
8558 &operation, steps[i],
8559 inputs[i]->x, inputs[i]->len),
8560 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308561 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008562 }
8563 }
8564
Gilles Peskine449bd832023-01-11 14:50:10 +01008565 if (output_key_type != PSA_KEY_TYPE_NONE) {
8566 psa_reset_key_attributes(&attributes);
8567 psa_set_key_type(&attributes, output_key_type);
8568 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008569 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 psa_key_derivation_output_key(&attributes, &operation,
8571 &output_key);
8572 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008573 uint8_t buffer[1];
8574 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 psa_key_derivation_output_bytes(&operation,
8576 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008577 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008578 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008579
Janos Follathaf3c2a02019-06-12 12:34:34 +01008580exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 psa_key_derivation_abort(&operation);
8582 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8583 psa_destroy_key(keys[i]);
8584 }
8585 psa_destroy_key(output_key);
8586 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008587}
8588/* END_CASE */
8589
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308590/* BEGIN_CASE*/
8591void derive_input_invalid_cost(int alg_arg, int64_t cost)
8592{
8593 psa_algorithm_t alg = alg_arg;
8594 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8595
8596 PSA_ASSERT(psa_crypto_init());
8597 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8598
8599 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8600 PSA_KEY_DERIVATION_INPUT_COST,
8601 cost),
8602 PSA_ERROR_NOT_SUPPORTED);
8603
8604exit:
8605 psa_key_derivation_abort(&operation);
8606 PSA_DONE();
8607}
8608/* END_CASE*/
8609
Janos Follathd958bb72019-07-03 15:02:16 +01008610/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008611void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008612{
Janos Follathd958bb72019-07-03 15:02:16 +01008613 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008615 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008616 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008617 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008619 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008621 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008622 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008623 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8624 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008627
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008629
Gilles Peskine449bd832023-01-11 14:50:10 +01008630 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8631 psa_set_key_algorithm(&attributes, alg);
8632 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008633
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 PSA_ASSERT(psa_import_key(&attributes,
8635 key_data, sizeof(key_data),
8636 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008637
8638 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008639 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8640 input1, input1_length,
8641 input2, input2_length,
8642 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008645
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008646 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8648 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008649
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008651
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8653 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008654
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008655exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008656 psa_key_derivation_abort(&operation);
8657 psa_destroy_key(key);
8658 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008659}
8660/* END_CASE */
8661
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008662/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008663void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008664{
8665 uint8_t output_buffer[16];
8666 size_t buffer_size = 16;
8667 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008668 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008669
Gilles Peskine449bd832023-01-11 14:50:10 +01008670 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8671 output_buffer, buffer_size)
8672 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008673
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8675 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008676
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008678
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8680 output_buffer, buffer_size)
8681 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008682
Gilles Peskine449bd832023-01-11 14:50:10 +01008683 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8684 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008685
8686exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008687 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008688}
8689/* END_CASE */
8690
8691/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008692void derive_output(int alg_arg,
8693 int step1_arg, data_t *input1, int expected_status_arg1,
8694 int step2_arg, data_t *input2, int expected_status_arg2,
8695 int step3_arg, data_t *input3, int expected_status_arg3,
8696 int step4_arg, data_t *input4, int expected_status_arg4,
8697 data_t *key_agreement_peer_key,
8698 int requested_capacity_arg,
8699 data_t *expected_output1,
8700 data_t *expected_output2,
8701 int other_key_input_type,
8702 int key_input_type,
8703 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008704{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008705 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8707 data_t *inputs[] = { input1, input2, input3, input4 };
8708 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8709 MBEDTLS_SVC_KEY_ID_INIT,
8710 MBEDTLS_SVC_KEY_ID_INIT,
8711 MBEDTLS_SVC_KEY_ID_INIT };
8712 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8713 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008714 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008715 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008716 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008718 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008720 size_t output_buffer_size = 0;
8721 uint8_t *output_buffer = NULL;
8722 size_t expected_capacity;
8723 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008724 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8725 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8726 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8727 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008728 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008729 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008730 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008731
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8733 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008734 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 }
8736 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008737 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008738 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008739 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008740 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008742
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008743 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8745 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8746 requested_capacity));
8747 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8748 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008749 case 0:
8750 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308751 case PSA_KEY_DERIVATION_INPUT_COST:
8752 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308753 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308754 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308755 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308756 if (statuses[i] != PSA_SUCCESS) {
8757 goto exit;
8758 }
8759 break;
8760 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008761 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008762 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008763 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 TEST_EQUAL(psa_key_derivation_input_bytes(
8765 &operation, steps[i],
8766 inputs[i]->x, inputs[i]->len),
8767 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008768
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008770 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008772 break;
8773 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008774 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8775 psa_set_key_algorithm(&attributes1, alg);
8776 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008777
Gilles Peskine449bd832023-01-11 14:50:10 +01008778 PSA_ASSERT(psa_import_key(&attributes1,
8779 inputs[i]->x, inputs[i]->len,
8780 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008781
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8783 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8784 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8785 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008786 }
8787
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308788 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008789 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308790 keys[i]),
8791 statuses[i]);
8792
8793 if (statuses[i] != PSA_SUCCESS) {
8794 goto exit;
8795 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008796 break;
8797 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008798 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008799 break;
8800 }
8801 break;
8802 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008803 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008804 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8806 steps[i],
8807 inputs[i]->x,
8808 inputs[i]->len),
8809 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008810 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008811 case 1: // input key, type DERIVE
8812 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8814 psa_set_key_algorithm(&attributes2, alg);
8815 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008816
8817 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 if (other_key_input_type == 11) {
8819 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8820 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008821
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 PSA_ASSERT(psa_import_key(&attributes2,
8823 inputs[i]->x, inputs[i]->len,
8824 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008825
Gilles Peskine449bd832023-01-11 14:50:10 +01008826 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8827 steps[i],
8828 keys[i]),
8829 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008830 break;
8831 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8833 psa_set_key_algorithm(&attributes3, alg);
8834 psa_set_key_type(&attributes3,
8835 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008836
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 PSA_ASSERT(psa_import_key(&attributes3,
8838 inputs[i]->x, inputs[i]->len,
8839 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008840
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 TEST_EQUAL(psa_key_derivation_key_agreement(
8842 &operation,
8843 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8844 keys[i], key_agreement_peer_key->x,
8845 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008846 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008847 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008848 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008849 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008850 }
8851
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008853 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008855 break;
8856 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 TEST_EQUAL(psa_key_derivation_input_bytes(
8858 &operation, steps[i],
8859 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008860
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008862 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008864 break;
8865 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008866 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008867
Gilles Peskine449bd832023-01-11 14:50:10 +01008868 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8869 &current_capacity));
8870 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008871 expected_capacity = requested_capacity;
8872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008874 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8875
8876 /* For output key derivation secret must be provided using
8877 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008879 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008880 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008881
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8883 psa_set_key_algorithm(&attributes4, alg);
8884 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8885 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008886
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8888 &derived_key), expected_status);
8889 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008890 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008891 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008892 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008893 status = psa_key_derivation_output_bytes(&operation,
8894 output_buffer, output_sizes[i]);
8895 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008896 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008897 TEST_ASSERT(status == PSA_SUCCESS ||
8898 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008899 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 } else if (expected_capacity == 0 ||
8901 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008902 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008904 expected_capacity = 0;
8905 continue;
8906 }
8907 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 PSA_ASSERT(status);
8909 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008910 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008911 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008912 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008913 /* Check the operation status. */
8914 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8916 &current_capacity));
8917 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008918 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008919 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008920 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008921
8922exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 mbedtls_free(output_buffer);
8924 psa_key_derivation_abort(&operation);
8925 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8926 psa_destroy_key(keys[i]);
8927 }
8928 psa_destroy_key(derived_key);
8929 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008930}
8931/* END_CASE */
8932
8933/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008934void derive_full(int alg_arg,
8935 data_t *key_data,
8936 data_t *input1,
8937 data_t *input2,
8938 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008939{
Ronald Cron5425a212020-08-04 14:58:35 +02008940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008941 psa_algorithm_t alg = alg_arg;
8942 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008943 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008944 unsigned char output_buffer[16];
8945 size_t expected_capacity = requested_capacity;
8946 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008948
Gilles Peskine449bd832023-01-11 14:50:10 +01008949 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008950
Gilles Peskine449bd832023-01-11 14:50:10 +01008951 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8952 psa_set_key_algorithm(&attributes, alg);
8953 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008954
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8956 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008957
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8959 input1->x, input1->len,
8960 input2->x, input2->len,
8961 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008962 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008964
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8966 &current_capacity));
8967 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008968
8969 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008970 while (current_capacity > 0) {
8971 size_t read_size = sizeof(output_buffer);
8972 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008973 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 }
8975 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8976 output_buffer,
8977 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008978 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8980 &current_capacity));
8981 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008982 }
8983
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008984 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8986 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008987
Gilles Peskine449bd832023-01-11 14:50:10 +01008988 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008989
8990exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 psa_key_derivation_abort(&operation);
8992 psa_destroy_key(key);
8993 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008994}
8995/* END_CASE */
8996
Stephan Koch78109f52023-04-12 14:19:36 +02008997/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008998void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8999 int derivation_step,
9000 int capacity, int expected_capacity_status_arg,
9001 data_t *expected_output,
9002 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009003{
9004 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9005 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009006 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009007 uint8_t *output_buffer = NULL;
9008 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009009 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9010 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9011 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009012
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009013 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009015
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9017 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9018 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009019
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9021 step, input->x, input->len),
9022 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009023
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009025 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009027
Gilles Peskine449bd832023-01-11 14:50:10 +01009028 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9029 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009030
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 TEST_EQUAL(status, expected_output_status);
9032 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009033 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009034 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009036
9037exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 mbedtls_free(output_buffer);
9039 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009040 PSA_DONE();
9041}
9042/* END_CASE */
9043
Janos Follathe60c9052019-07-03 13:51:30 +01009044/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009045void derive_key_exercise(int alg_arg,
9046 data_t *key_data,
9047 data_t *input1,
9048 data_t *input2,
9049 int derived_type_arg,
9050 int derived_bits_arg,
9051 int derived_usage_arg,
9052 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009053{
Ronald Cron5425a212020-08-04 14:58:35 +02009054 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9055 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009056 psa_algorithm_t alg = alg_arg;
9057 psa_key_type_t derived_type = derived_type_arg;
9058 size_t derived_bits = derived_bits_arg;
9059 psa_key_usage_t derived_usage = derived_usage_arg;
9060 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009061 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009062 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009063 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009064 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009065
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009067
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9069 psa_set_key_algorithm(&attributes, alg);
9070 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9071 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9072 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009073
9074 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009075 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9076 input1->x, input1->len,
9077 input2->x, input2->len,
9078 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009080 }
Janos Follathe60c9052019-07-03 13:51:30 +01009081
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 psa_set_key_usage_flags(&attributes, derived_usage);
9083 psa_set_key_algorithm(&attributes, derived_alg);
9084 psa_set_key_type(&attributes, derived_type);
9085 psa_set_key_bits(&attributes, derived_bits);
9086 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9087 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009088
9089 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9091 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9092 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009093
9094 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009096 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009097 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009098
9099exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009100 /*
9101 * Key attributes may have been returned by psa_get_key_attributes()
9102 * thus reset them as required.
9103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009105
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 psa_key_derivation_abort(&operation);
9107 psa_destroy_key(base_key);
9108 psa_destroy_key(derived_key);
9109 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009110}
9111/* END_CASE */
9112
Janos Follath42fd8882019-07-03 14:17:09 +01009113/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009114void derive_key_export(int alg_arg,
9115 data_t *key_data,
9116 data_t *input1,
9117 data_t *input2,
9118 int bytes1_arg,
9119 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009120{
Ronald Cron5425a212020-08-04 14:58:35 +02009121 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9122 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009123 psa_algorithm_t alg = alg_arg;
9124 size_t bytes1 = bytes1_arg;
9125 size_t bytes2 = bytes2_arg;
9126 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009127 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009128 uint8_t *output_buffer = NULL;
9129 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009130 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9131 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009132 size_t length;
9133
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009134 TEST_CALLOC(output_buffer, capacity);
9135 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009137
Gilles Peskine449bd832023-01-11 14:50:10 +01009138 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9139 psa_set_key_algorithm(&base_attributes, alg);
9140 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9141 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9142 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009143
9144 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009145 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9146 input1->x, input1->len,
9147 input2->x, input2->len,
9148 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009149 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 }
Janos Follath42fd8882019-07-03 14:17:09 +01009151
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9153 output_buffer,
9154 capacity));
9155 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009156
9157 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9159 input1->x, input1->len,
9160 input2->x, input2->len,
9161 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009163 }
Janos Follath42fd8882019-07-03 14:17:09 +01009164
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9166 psa_set_key_algorithm(&derived_attributes, 0);
9167 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9168 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9169 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9170 &derived_key));
9171 PSA_ASSERT(psa_export_key(derived_key,
9172 export_buffer, bytes1,
9173 &length));
9174 TEST_EQUAL(length, bytes1);
9175 PSA_ASSERT(psa_destroy_key(derived_key));
9176 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9177 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9178 &derived_key));
9179 PSA_ASSERT(psa_export_key(derived_key,
9180 export_buffer + bytes1, bytes2,
9181 &length));
9182 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009183
9184 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009185 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009186 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009187
9188exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009189 mbedtls_free(output_buffer);
9190 mbedtls_free(export_buffer);
9191 psa_key_derivation_abort(&operation);
9192 psa_destroy_key(base_key);
9193 psa_destroy_key(derived_key);
9194 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009195}
9196/* END_CASE */
9197
9198/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009199void derive_key_type(int alg_arg,
9200 data_t *key_data,
9201 data_t *input1,
9202 data_t *input2,
9203 int key_type_arg, int bits_arg,
9204 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009205{
9206 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9207 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9208 const psa_algorithm_t alg = alg_arg;
9209 const psa_key_type_t key_type = key_type_arg;
9210 const size_t bits = bits_arg;
9211 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9212 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009214 uint8_t *export_buffer = NULL;
9215 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9216 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9217 size_t export_length;
9218
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009219 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009221
Gilles Peskine449bd832023-01-11 14:50:10 +01009222 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9223 psa_set_key_algorithm(&base_attributes, alg);
9224 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9225 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9226 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009227
Gilles Peskine449bd832023-01-11 14:50:10 +01009228 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009229 &operation, base_key, alg,
9230 input1->x, input1->len,
9231 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009232 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009233 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009234 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009235
Gilles Peskine449bd832023-01-11 14:50:10 +01009236 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9237 psa_set_key_algorithm(&derived_attributes, 0);
9238 psa_set_key_type(&derived_attributes, key_type);
9239 psa_set_key_bits(&derived_attributes, bits);
9240 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9241 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009242
Gilles Peskine449bd832023-01-11 14:50:10 +01009243 PSA_ASSERT(psa_export_key(derived_key,
9244 export_buffer, export_buffer_size,
9245 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009246 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009247 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009248
9249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009250 mbedtls_free(export_buffer);
9251 psa_key_derivation_abort(&operation);
9252 psa_destroy_key(base_key);
9253 psa_destroy_key(derived_key);
9254 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009255}
9256/* END_CASE */
9257
9258/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009259void derive_key(int alg_arg,
9260 data_t *key_data, data_t *input1, data_t *input2,
9261 int type_arg, int bits_arg,
9262 int expected_status_arg,
9263 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009264{
Ronald Cron5425a212020-08-04 14:58:35 +02009265 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9266 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009267 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009268 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009269 size_t bits = bits_arg;
9270 psa_status_t expected_status = expected_status_arg;
9271 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9272 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9273 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9274
Gilles Peskine449bd832023-01-11 14:50:10 +01009275 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009276
Gilles Peskine449bd832023-01-11 14:50:10 +01009277 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9278 psa_set_key_algorithm(&base_attributes, alg);
9279 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9280 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9281 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009282
Gilles Peskine449bd832023-01-11 14:50:10 +01009283 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9284 input1->x, input1->len,
9285 input2->x, input2->len,
9286 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009287 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009289
Gilles Peskine449bd832023-01-11 14:50:10 +01009290 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9291 psa_set_key_algorithm(&derived_attributes, 0);
9292 psa_set_key_type(&derived_attributes, type);
9293 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009294
9295 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009296 psa_key_derivation_output_key(&derived_attributes,
9297 &operation,
9298 &derived_key);
9299 if (is_large_output > 0) {
9300 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9301 }
9302 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009303
9304exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009305 psa_key_derivation_abort(&operation);
9306 psa_destroy_key(base_key);
9307 psa_destroy_key(derived_key);
9308 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009309}
9310/* END_CASE */
9311
9312/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009313void key_agreement_setup(int alg_arg,
9314 int our_key_type_arg, int our_key_alg_arg,
9315 data_t *our_key_data, data_t *peer_key_data,
9316 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009317{
Ronald Cron5425a212020-08-04 14:58:35 +02009318 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009319 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009320 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009321 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009322 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009324 psa_status_t expected_status = expected_status_arg;
9325 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009326
Gilles Peskine449bd832023-01-11 14:50:10 +01009327 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009328
Gilles Peskine449bd832023-01-11 14:50:10 +01009329 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9330 psa_set_key_algorithm(&attributes, our_key_alg);
9331 psa_set_key_type(&attributes, our_key_type);
9332 PSA_ASSERT(psa_import_key(&attributes,
9333 our_key_data->x, our_key_data->len,
9334 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009335
Gilles Peskine77f40d82019-04-11 21:27:06 +02009336 /* The tests currently include inputs that should fail at either step.
9337 * Test cases that fail at the setup step should be changed to call
9338 * key_derivation_setup instead, and this function should be renamed
9339 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009340 status = psa_key_derivation_setup(&operation, alg);
9341 if (status == PSA_SUCCESS) {
9342 TEST_EQUAL(psa_key_derivation_key_agreement(
9343 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9344 our_key,
9345 peer_key_data->x, peer_key_data->len),
9346 expected_status);
9347 } else {
9348 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009349 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009350
9351exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009352 psa_key_derivation_abort(&operation);
9353 psa_destroy_key(our_key);
9354 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009355}
9356/* END_CASE */
9357
9358/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009359void raw_key_agreement(int alg_arg,
9360 int our_key_type_arg, data_t *our_key_data,
9361 data_t *peer_key_data,
9362 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009363{
Ronald Cron5425a212020-08-04 14:58:35 +02009364 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009365 psa_algorithm_t alg = alg_arg;
9366 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009367 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009368 unsigned char *output = NULL;
9369 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009370 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009371
Gilles Peskine449bd832023-01-11 14:50:10 +01009372 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009373
Gilles Peskine449bd832023-01-11 14:50:10 +01009374 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9375 psa_set_key_algorithm(&attributes, alg);
9376 psa_set_key_type(&attributes, our_key_type);
9377 PSA_ASSERT(psa_import_key(&attributes,
9378 our_key_data->x, our_key_data->len,
9379 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009380
Gilles Peskine449bd832023-01-11 14:50:10 +01009381 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9382 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009383
Gilles Peskine992bee82022-04-13 23:25:52 +02009384 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 TEST_LE_U(expected_output->len,
9386 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9387 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9388 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009389
9390 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009391 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009392 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9393 peer_key_data->x, peer_key_data->len,
9394 output, expected_output->len,
9395 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009396 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009397 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009398 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009399 output = NULL;
9400 output_length = ~0;
9401
9402 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009403 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9405 peer_key_data->x, peer_key_data->len,
9406 output, expected_output->len + 1,
9407 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009408 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009409 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009410 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009411 output = NULL;
9412 output_length = ~0;
9413
9414 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009415 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009416 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9417 peer_key_data->x, peer_key_data->len,
9418 output, expected_output->len - 1,
9419 &output_length),
9420 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009421 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 TEST_LE_U(output_length, expected_output->len - 1);
9423 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009424 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009425
9426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009427 mbedtls_free(output);
9428 psa_destroy_key(our_key);
9429 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009430}
9431/* END_CASE */
9432
9433/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009434void key_agreement_capacity(int alg_arg,
9435 int our_key_type_arg, data_t *our_key_data,
9436 data_t *peer_key_data,
9437 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009438{
Ronald Cron5425a212020-08-04 14:58:35 +02009439 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009440 psa_algorithm_t alg = alg_arg;
9441 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009442 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009444 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009445 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009446
Gilles Peskine449bd832023-01-11 14:50:10 +01009447 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009448
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9450 psa_set_key_algorithm(&attributes, alg);
9451 psa_set_key_type(&attributes, our_key_type);
9452 PSA_ASSERT(psa_import_key(&attributes,
9453 our_key_data->x, our_key_data->len,
9454 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009455
Gilles Peskine449bd832023-01-11 14:50:10 +01009456 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9457 PSA_ASSERT(psa_key_derivation_key_agreement(
9458 &operation,
9459 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9460 peer_key_data->x, peer_key_data->len));
9461 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009462 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9464 PSA_KEY_DERIVATION_INPUT_INFO,
9465 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009466 }
Gilles Peskine59685592018-09-18 12:11:34 +02009467
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009468 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009469 PSA_ASSERT(psa_key_derivation_get_capacity(
9470 &operation, &actual_capacity));
9471 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009472
Gilles Peskinebf491972018-10-25 22:36:12 +02009473 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 while (actual_capacity > sizeof(output)) {
9475 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9476 output, sizeof(output)));
9477 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009478 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9480 output, actual_capacity));
9481 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9482 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009483
Gilles Peskine59685592018-09-18 12:11:34 +02009484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 psa_key_derivation_abort(&operation);
9486 psa_destroy_key(our_key);
9487 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009488}
9489/* END_CASE */
9490
Valerio Settiad819672023-12-29 12:14:41 +01009491/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9492void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009493{
9494 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009495 psa_ecc_family_t ecc_family = psa_family_arg;
9496 size_t bits = bits_arg;
9497 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009498
Valerio Settiad819672023-12-29 12:14:41 +01009499 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9500 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009501 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009502}
9503/* END_CASE */
9504
Valerio Settiac739522024-01-04 10:22:01 +01009505/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9506void ecc_conversion_functions_fail()
9507{
9508 size_t bits;
9509
Valerio Settidb6e0292024-01-05 10:15:45 +01009510 /* Invalid legacy curve identifiers. */
9511 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9512 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009513 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9514 TEST_EQUAL(0, bits);
9515
9516 /* Invalid PSA EC family. */
9517 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9518 /* Invalid bit-size for a valid EC family. */
9519 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9520
9521 /* Twisted-Edward curves are not supported yet. */
9522 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9523 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9524 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9525 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9526}
9527/* END_CASE */
9528
9529
Valerio Settibf999cb2023-12-28 17:48:13 +01009530/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009531void key_agreement_output(int alg_arg,
9532 int our_key_type_arg, data_t *our_key_data,
9533 data_t *peer_key_data,
9534 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009535{
Ronald Cron5425a212020-08-04 14:58:35 +02009536 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009537 psa_algorithm_t alg = alg_arg;
9538 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009539 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009541 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009542
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009543 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009544 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009545
Gilles Peskine449bd832023-01-11 14:50:10 +01009546 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009547
Gilles Peskine449bd832023-01-11 14:50:10 +01009548 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9549 psa_set_key_algorithm(&attributes, alg);
9550 psa_set_key_type(&attributes, our_key_type);
9551 PSA_ASSERT(psa_import_key(&attributes,
9552 our_key_data->x, our_key_data->len,
9553 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009554
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9556 PSA_ASSERT(psa_key_derivation_key_agreement(
9557 &operation,
9558 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9559 peer_key_data->x, peer_key_data->len));
9560 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009561 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009562 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9563 PSA_KEY_DERIVATION_INPUT_INFO,
9564 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009565 }
Gilles Peskine59685592018-09-18 12:11:34 +02009566
Gilles Peskine449bd832023-01-11 14:50:10 +01009567 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9568 actual_output,
9569 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009570 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009571 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009572 if (expected_output2->len != 0) {
9573 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9574 actual_output,
9575 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009576 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009577 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009578 }
Gilles Peskine59685592018-09-18 12:11:34 +02009579
9580exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009581 psa_key_derivation_abort(&operation);
9582 psa_destroy_key(our_key);
9583 PSA_DONE();
9584 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009585}
9586/* END_CASE */
9587
9588/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009589void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009590{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009591 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009592 unsigned char *output = NULL;
9593 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009594 size_t i;
9595 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009596
Gilles Peskine449bd832023-01-11 14:50:10 +01009597 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009598
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009599 TEST_CALLOC(output, bytes);
9600 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009601
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009603
Gilles Peskinea50d7392018-06-21 10:22:13 +02009604 /* Run several times, to ensure that every output byte will be
9605 * nonzero at least once with overwhelming probability
9606 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 for (run = 0; run < 10; run++) {
9608 if (bytes != 0) {
9609 memset(output, 0, bytes);
9610 }
9611 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009612
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 for (i = 0; i < bytes; i++) {
9614 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009615 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009616 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009617 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009618 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009619
9620 /* Check that every byte was changed to nonzero at least once. This
9621 * validates that psa_generate_random is overwriting every byte of
9622 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009623 for (i = 0; i < bytes; i++) {
9624 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009625 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009626
9627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009628 PSA_DONE();
9629 mbedtls_free(output);
9630 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009631}
9632/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009633
9634/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009635void generate_key(int type_arg,
9636 int bits_arg,
9637 int usage_arg,
9638 int alg_arg,
9639 int expected_status_arg,
9640 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009641{
Ronald Cron5425a212020-08-04 14:58:35 +02009642 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009643 psa_key_type_t type = type_arg;
9644 psa_key_usage_t usage = usage_arg;
9645 size_t bits = bits_arg;
9646 psa_algorithm_t alg = alg_arg;
9647 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009648 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009649 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009650
Gilles Peskine449bd832023-01-11 14:50:10 +01009651 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009652
Gilles Peskine449bd832023-01-11 14:50:10 +01009653 psa_set_key_usage_flags(&attributes, usage);
9654 psa_set_key_algorithm(&attributes, alg);
9655 psa_set_key_type(&attributes, type);
9656 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009657
9658 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009659 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009660
Gilles Peskine449bd832023-01-11 14:50:10 +01009661 if (is_large_key > 0) {
9662 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9663 }
9664 TEST_EQUAL(status, expected_status);
9665 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009667 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009668
9669 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009670 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9671 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9672 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009673
Gilles Peskine818ca122018-06-20 18:16:48 +02009674 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009675 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009676 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009677 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009678
9679exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009680 /*
9681 * Key attributes may have been returned by psa_get_key_attributes()
9682 * thus reset them as required.
9683 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009684 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009685
Gilles Peskine449bd832023-01-11 14:50:10 +01009686 psa_destroy_key(key);
9687 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009688}
9689/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009690
Valerio Setti19fec542023-07-25 12:31:50 +02009691/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine449bd832023-01-11 14:50:10 +01009692void generate_key_rsa(int bits_arg,
9693 data_t *e_arg,
9694 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009695{
Ronald Cron5425a212020-08-04 14:58:35 +02009696 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009697 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009698 size_t bits = bits_arg;
9699 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9700 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9701 psa_status_t expected_status = expected_status_arg;
9702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9703 uint8_t *exported = NULL;
9704 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009705 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009706 size_t exported_length = SIZE_MAX;
9707 uint8_t *e_read_buffer = NULL;
9708 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009709 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009710 size_t e_read_length = SIZE_MAX;
9711
Gilles Peskine449bd832023-01-11 14:50:10 +01009712 if (e_arg->len == 0 ||
9713 (e_arg->len == 3 &&
9714 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009715 is_default_public_exponent = 1;
9716 e_read_size = 0;
9717 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009718 TEST_CALLOC(e_read_buffer, e_read_size);
9719 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009720
Gilles Peskine449bd832023-01-11 14:50:10 +01009721 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009722
Gilles Peskine449bd832023-01-11 14:50:10 +01009723 psa_set_key_usage_flags(&attributes, usage);
9724 psa_set_key_algorithm(&attributes, alg);
9725 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9726 e_arg->x, e_arg->len));
9727 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009728
9729 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009730 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9731 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009732 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009733 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009734
9735 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009736 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9737 TEST_EQUAL(psa_get_key_type(&attributes), type);
9738 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009739 psa_status_t status = psa_get_key_domain_parameters(&attributes,
9740 e_read_buffer, e_read_size,
9741 &e_read_length);
9742
9743
9744#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
9745 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
9746 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +01009747 if (is_default_public_exponent) {
9748 TEST_EQUAL(e_read_length, 0);
9749 } else {
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009750 TEST_EQUAL(status, PSA_SUCCESS);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009751 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009752 }
Pengyu Lv9e976f32023-12-06 16:58:05 +08009753#else
Pengyu Lv9e976f32023-12-06 16:58:05 +08009754 (void) is_default_public_exponent;
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009755 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
Pengyu Lv9e976f32023-12-06 16:58:05 +08009756#endif
Gilles Peskinee56e8782019-04-26 17:34:02 +02009757
9758 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009759 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009760 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009761 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009762
9763 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009764 PSA_ASSERT(psa_export_public_key(key,
9765 exported, exported_size,
9766 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009767 {
9768 uint8_t *p = exported;
9769 uint8_t *end = exported + exported_length;
9770 size_t len;
9771 /* RSAPublicKey ::= SEQUENCE {
9772 * modulus INTEGER, -- n
9773 * publicExponent INTEGER } -- e
9774 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009775 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9776 MBEDTLS_ASN1_SEQUENCE |
9777 MBEDTLS_ASN1_CONSTRUCTED));
9778 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9779 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9780 MBEDTLS_ASN1_INTEGER));
9781 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009782 ++p;
9783 --len;
9784 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009785 if (e_arg->len == 0) {
9786 TEST_EQUAL(len, 3);
9787 TEST_EQUAL(p[0], 1);
9788 TEST_EQUAL(p[1], 0);
9789 TEST_EQUAL(p[2], 1);
9790 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009791 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009792 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009793 }
9794
9795exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009796 /*
9797 * Key attributes may have been returned by psa_get_key_attributes() or
9798 * set by psa_set_key_domain_parameters() thus reset them as required.
9799 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009800 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009801
Gilles Peskine449bd832023-01-11 14:50:10 +01009802 psa_destroy_key(key);
9803 PSA_DONE();
9804 mbedtls_free(e_read_buffer);
9805 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009806}
9807/* END_CASE */
9808
Darryl Greend49a4992018-06-18 17:27:26 +01009809/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009810void persistent_key_load_key_from_storage(data_t *data,
9811 int type_arg, int bits_arg,
9812 int usage_flags_arg, int alg_arg,
9813 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009814{
Gilles Peskine449bd832023-01-11 14:50:10 +01009815 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009817 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9818 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009819 psa_key_type_t type = type_arg;
9820 size_t bits = bits_arg;
9821 psa_key_usage_t usage_flags = usage_flags_arg;
9822 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009823 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009824 unsigned char *first_export = NULL;
9825 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009826 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -07009827 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +01009828 size_t second_exported_length;
9829
Gilles Peskine449bd832023-01-11 14:50:10 +01009830 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009831 TEST_CALLOC(first_export, export_size);
9832 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009833 }
Darryl Greend49a4992018-06-18 17:27:26 +01009834
Gilles Peskine449bd832023-01-11 14:50:10 +01009835 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009836
Gilles Peskine449bd832023-01-11 14:50:10 +01009837 psa_set_key_id(&attributes, key_id);
9838 psa_set_key_usage_flags(&attributes, usage_flags);
9839 psa_set_key_algorithm(&attributes, alg);
9840 psa_set_key_type(&attributes, type);
9841 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009842
Gilles Peskine449bd832023-01-11 14:50:10 +01009843 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009844 case IMPORT_KEY:
9845 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009846 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9847 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009848 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009849
Darryl Green0c6575a2018-11-07 16:05:30 +00009850 case GENERATE_KEY:
9851 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009852 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009853 break;
9854
9855 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009856#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009857 {
9858 /* Create base key */
9859 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9860 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9861 psa_set_key_usage_flags(&base_attributes,
9862 PSA_KEY_USAGE_DERIVE);
9863 psa_set_key_algorithm(&base_attributes, derive_alg);
9864 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9865 PSA_ASSERT(psa_import_key(&base_attributes,
9866 data->x, data->len,
9867 &base_key));
9868 /* Derive a key. */
9869 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9870 PSA_ASSERT(psa_key_derivation_input_key(
9871 &operation,
9872 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9873 PSA_ASSERT(psa_key_derivation_input_bytes(
9874 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9875 NULL, 0));
9876 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9877 &operation,
9878 &key));
9879 PSA_ASSERT(psa_key_derivation_abort(&operation));
9880 PSA_ASSERT(psa_destroy_key(base_key));
9881 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9882 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009883#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009884 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009885#endif
9886 break;
9887
9888 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009889 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009890 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009891 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009892 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009893
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009894 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009895 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9896 PSA_ASSERT(psa_export_key(key,
9897 first_export, export_size,
9898 &first_exported_length));
9899 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009900 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009901 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01009902 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009903 }
Darryl Greend49a4992018-06-18 17:27:26 +01009904
9905 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009906 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009907 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009908 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009909
Darryl Greend49a4992018-06-18 17:27:26 +01009910 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009911 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9912 TEST_ASSERT(mbedtls_svc_key_id_equal(
9913 psa_get_key_id(&attributes), key_id));
9914 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9915 PSA_KEY_LIFETIME_PERSISTENT);
9916 TEST_EQUAL(psa_get_key_type(&attributes), type);
9917 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9918 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9919 mbedtls_test_update_key_usage_flags(usage_flags));
9920 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009921
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009922 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009923 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9924 PSA_ASSERT(psa_export_key(key,
9925 second_export, export_size,
9926 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009927 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009928 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009929 }
9930
9931 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009932 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009933 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009934 }
Darryl Greend49a4992018-06-18 17:27:26 +01009935
9936exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009937 /*
9938 * Key attributes may have been returned by psa_get_key_attributes()
9939 * thus reset them as required.
9940 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009941 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009942
Gilles Peskine449bd832023-01-11 14:50:10 +01009943 mbedtls_free(first_export);
9944 mbedtls_free(second_export);
9945 psa_key_derivation_abort(&operation);
9946 psa_destroy_key(base_key);
9947 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009948 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009949}
9950/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009951
Neil Armstronga557cb82022-06-10 08:58:32 +02009952/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009953void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9954 int primitive_arg, int hash_arg, int role_arg,
9955 int test_input, data_t *pw_data,
9956 int inj_err_type_arg,
9957 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009958{
9959 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9960 psa_pake_operation_t operation = psa_pake_operation_init();
9961 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009962 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009963 psa_key_type_t key_type_pw = key_type_pw_arg;
9964 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009965 psa_algorithm_t hash_alg = hash_arg;
9966 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009967 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9968 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009969 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9970 psa_status_t expected_error = expected_error_arg;
9971 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009972 unsigned char *output_buffer = NULL;
9973 size_t output_len = 0;
9974
Gilles Peskine449bd832023-01-11 14:50:10 +01009975 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009976
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009977 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009978 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009979 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009980
Gilles Peskine449bd832023-01-11 14:50:10 +01009981 if (pw_data->len > 0) {
9982 psa_set_key_usage_flags(&attributes, key_usage_pw);
9983 psa_set_key_algorithm(&attributes, alg);
9984 psa_set_key_type(&attributes, key_type_pw);
9985 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9986 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009987 }
9988
Gilles Peskine449bd832023-01-11 14:50:10 +01009989 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9990 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9991 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009992
Gilles Peskine449bd832023-01-11 14:50:10 +01009993 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009994
Gilles Peskine449bd832023-01-11 14:50:10 +01009995 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9996 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9997 expected_error);
9998 PSA_ASSERT(psa_pake_abort(&operation));
9999 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10000 expected_error);
10001 PSA_ASSERT(psa_pake_abort(&operation));
10002 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10003 expected_error);
10004 PSA_ASSERT(psa_pake_abort(&operation));
10005 TEST_EQUAL(psa_pake_set_role(&operation, role),
10006 expected_error);
10007 PSA_ASSERT(psa_pake_abort(&operation));
10008 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10009 NULL, 0, NULL),
10010 expected_error);
10011 PSA_ASSERT(psa_pake_abort(&operation));
10012 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10013 expected_error);
10014 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010015 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010016 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010017
Gilles Peskine449bd832023-01-11 14:50:10 +010010018 status = psa_pake_setup(&operation, &cipher_suite);
10019 if (status != PSA_SUCCESS) {
10020 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010021 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010022 }
10023
Gilles Peskine449bd832023-01-11 14:50:10 +010010024 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10025 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10026 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010027 goto exit;
10028 }
10029
Gilles Peskine449bd832023-01-11 14:50:10 +010010030 status = psa_pake_set_role(&operation, role);
10031 if (status != PSA_SUCCESS) {
10032 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010033 goto exit;
10034 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010035
Gilles Peskine449bd832023-01-11 14:50:10 +010010036 if (pw_data->len > 0) {
10037 status = psa_pake_set_password_key(&operation, key);
10038 if (status != PSA_SUCCESS) {
10039 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010040 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010041 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010042 }
10043
Gilles Peskine449bd832023-01-11 14:50:10 +010010044 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10045 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10046 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010047 goto exit;
10048 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010049
Gilles Peskine449bd832023-01-11 14:50:10 +010010050 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10051 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10052 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010053 goto exit;
10054 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010055
Gilles Peskine449bd832023-01-11 14:50:10 +010010056 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010057 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010058 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10059 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010060 goto exit;
10061 }
10062
Gilles Peskine449bd832023-01-11 14:50:10 +010010063 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010064 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010065 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10066 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010067 goto exit;
10068 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010069
Gilles Peskine449bd832023-01-11 14:50:10 +010010070 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10071 PSA_PAKE_STEP_KEY_SHARE);
10072 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10073 PSA_PAKE_STEP_ZK_PUBLIC);
10074 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10075 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010076
Gilles Peskine449bd832023-01-11 14:50:10 +010010077 if (test_input) {
10078 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10079 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10080 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010081 goto exit;
10082 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010083
Gilles Peskine449bd832023-01-11 14:50:10 +010010084 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10085 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10086 output_buffer, size_zk_proof),
10087 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010088 goto exit;
10089 }
10090
Gilles Peskine449bd832023-01-11 14:50:10 +010010091 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10092 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10093 output_buffer, size_zk_proof),
10094 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010095 goto exit;
10096 }
10097
Gilles Peskine449bd832023-01-11 14:50:10 +010010098 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10099 output_buffer, size_key_share);
10100 if (status != PSA_SUCCESS) {
10101 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010102 goto exit;
10103 }
10104
Gilles Peskine449bd832023-01-11 14:50:10 +010010105 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10106 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10107 output_buffer, size_zk_public + 1),
10108 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010109 goto exit;
10110 }
10111
Gilles Peskine449bd832023-01-11 14:50:10 +010010112 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010113 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010114 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10115 output_buffer, size_zk_public + 1);
10116 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10117 output_buffer, size_zk_public),
10118 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010119 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010120 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010121 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010122 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10123 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10124 NULL, 0, NULL),
10125 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010126 goto exit;
10127 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010128
Gilles Peskine449bd832023-01-11 14:50:10 +010010129 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10130 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10131 output_buffer, buf_size, &output_len),
10132 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010133 goto exit;
10134 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010135
Gilles Peskine449bd832023-01-11 14:50:10 +010010136 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10137 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10138 output_buffer, buf_size, &output_len),
10139 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010140 goto exit;
10141 }
10142
Gilles Peskine449bd832023-01-11 14:50:10 +010010143 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10144 output_buffer, buf_size, &output_len);
10145 if (status != PSA_SUCCESS) {
10146 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010147 goto exit;
10148 }
10149
Gilles Peskine449bd832023-01-11 14:50:10 +010010150 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010151
Gilles Peskine449bd832023-01-11 14:50:10 +010010152 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10153 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10154 output_buffer, size_zk_public - 1, &output_len),
10155 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010156 goto exit;
10157 }
10158
Gilles Peskine449bd832023-01-11 14:50:10 +010010159 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010160 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010161 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10162 output_buffer, size_zk_public - 1, &output_len);
10163 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10164 output_buffer, buf_size, &output_len),
10165 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010166 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010167 }
10168 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010169
10170exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010171 PSA_ASSERT(psa_destroy_key(key));
10172 PSA_ASSERT(psa_pake_abort(&operation));
10173 mbedtls_free(output_buffer);
10174 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010175}
10176/* END_CASE */
10177
Neil Armstronga557cb82022-06-10 08:58:32 +020010178/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010179void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10180 int client_input_first, int inject_error,
10181 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010182{
10183 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10184 psa_pake_operation_t server = psa_pake_operation_init();
10185 psa_pake_operation_t client = psa_pake_operation_init();
10186 psa_algorithm_t alg = alg_arg;
10187 psa_algorithm_t hash_alg = hash_arg;
10188 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10190
Gilles Peskine449bd832023-01-11 14:50:10 +010010191 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010192
Gilles Peskine449bd832023-01-11 14:50:10 +010010193 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10194 psa_set_key_algorithm(&attributes, alg);
10195 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10196 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10197 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010198
Gilles Peskine449bd832023-01-11 14:50:10 +010010199 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10200 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10201 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010202
10203
Gilles Peskine449bd832023-01-11 14:50:10 +010010204 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10205 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010206
Gilles Peskine449bd832023-01-11 14:50:10 +010010207 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10208 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010209
Gilles Peskine449bd832023-01-11 14:50:10 +010010210 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10211 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010212
Gilles Peskine449bd832023-01-11 14:50:10 +010010213 ecjpake_do_round(alg, primitive_arg, &server, &client,
10214 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010215
Gilles Peskine449bd832023-01-11 14:50:10 +010010216 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010218 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010219
Gilles Peskine449bd832023-01-11 14:50:10 +010010220 ecjpake_do_round(alg, primitive_arg, &server, &client,
10221 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010222
10223exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010224 psa_destroy_key(key);
10225 psa_pake_abort(&server);
10226 psa_pake_abort(&client);
10227 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010228}
10229/* END_CASE */
10230
10231/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010232void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10233 int derive_alg_arg, data_t *pw_data,
10234 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010235{
10236 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10237 psa_pake_operation_t server = psa_pake_operation_init();
10238 psa_pake_operation_t client = psa_pake_operation_init();
10239 psa_algorithm_t alg = alg_arg;
10240 psa_algorithm_t hash_alg = hash_arg;
10241 psa_algorithm_t derive_alg = derive_alg_arg;
10242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10244 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010245 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010246 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010247 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010248 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010249
Gilles Peskine449bd832023-01-11 14:50:10 +010010250 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010251
Gilles Peskine449bd832023-01-11 14:50:10 +010010252 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10253 psa_set_key_algorithm(&attributes, alg);
10254 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10255 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10256 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010257
Gilles Peskine449bd832023-01-11 14:50:10 +010010258 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10259 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10260 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010261
Neil Armstrong1e855602022-06-15 11:32:11 +020010262 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010263 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10264 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010265
Gilles Peskine449bd832023-01-11 14:50:10 +010010266 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10267 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10268 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10269 PSA_KEY_DERIVATION_INPUT_SEED,
10270 (const uint8_t *) "", 0));
10271 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10272 PSA_KEY_DERIVATION_INPUT_SEED,
10273 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010274 }
10275
Gilles Peskine449bd832023-01-11 14:50:10 +010010276 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10277 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010278
Gilles Peskine449bd832023-01-11 14:50:10 +010010279 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10280 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010281
Gilles Peskine449bd832023-01-11 14:50:10 +010010282 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10283 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010284
Gilles Peskine449bd832023-01-11 14:50:10 +010010285 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10286 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10287 PSA_ERROR_BAD_STATE);
10288 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10289 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010290 goto exit;
10291 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010292
Neil Armstrongf983caf2022-06-15 15:27:48 +020010293 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010294 ecjpake_do_round(alg, primitive_arg, &server, &client,
10295 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010296
Gilles Peskine449bd832023-01-11 14:50:10 +010010297 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10298 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10299 PSA_ERROR_BAD_STATE);
10300 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10301 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010302 goto exit;
10303 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010304
Neil Armstrongf983caf2022-06-15 15:27:48 +020010305 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010306 ecjpake_do_round(alg, primitive_arg, &server, &client,
10307 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010308
Gilles Peskine449bd832023-01-11 14:50:10 +010010309 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10310 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010311
10312exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010313 psa_key_derivation_abort(&server_derive);
10314 psa_key_derivation_abort(&client_derive);
10315 psa_destroy_key(key);
10316 psa_pake_abort(&server);
10317 psa_pake_abort(&client);
10318 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010319}
10320/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010321
10322/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010323void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010324{
10325 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10326 const size_t bits = 256;
10327 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010328 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010329 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010330 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010331
10332 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10333 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010334 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10335 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10336 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10337 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010338 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010339 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10340 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010341
10342 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010343 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10344 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10345 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10346 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10347 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10348 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010349
10350 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010351 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10352 PSA_PAKE_OUTPUT_MAX_SIZE);
10353 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10354 PSA_PAKE_OUTPUT_MAX_SIZE);
10355 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10356 PSA_PAKE_OUTPUT_MAX_SIZE);
10357 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10358 PSA_PAKE_INPUT_MAX_SIZE);
10359 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10360 PSA_PAKE_INPUT_MAX_SIZE);
10361 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10362 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010363}
10364/* END_CASE */