blob: b605c68333deceacf81dfaa8cb274de7477eaf00 [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,
4618 output1, output1_buffer_size,
4619 &function_output_length));
4620 TEST_LE_U(function_output_length,
4621 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4622 alg,
4623 input->len - first_part_size));
4624 TEST_LE_U(function_output_length,
4625 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004626 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004627
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 PSA_ASSERT(psa_cipher_finish(&operation1,
4629 output1 + output1_length,
4630 output1_buffer_size - output1_length,
4631 &function_output_length));
4632 TEST_LE_U(function_output_length,
4633 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4634 TEST_LE_U(function_output_length,
4635 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004636 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004639
Gilles Peskine048b7f02018-06-08 14:20:49 +02004640 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 TEST_LE_U(output2_buffer_size,
4642 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4643 TEST_LE_U(output2_buffer_size,
4644 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004645 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 if (iv_length > 0) {
4648 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4649 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004650 }
Moran Pekerded84402018-06-06 16:36:50 +03004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4653 output2, output2_buffer_size,
4654 &function_output_length));
4655 TEST_LE_U(function_output_length,
4656 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4657 TEST_LE_U(function_output_length,
4658 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004659 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 PSA_ASSERT(psa_cipher_update(&operation2,
4662 output1 + first_part_size,
4663 output1_length - first_part_size,
4664 output2, output2_buffer_size,
4665 &function_output_length));
4666 TEST_LE_U(function_output_length,
4667 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4668 alg,
4669 output1_length - first_part_size));
4670 TEST_LE_U(function_output_length,
4671 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004672 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 PSA_ASSERT(psa_cipher_finish(&operation2,
4675 output2 + output2_length,
4676 output2_buffer_size - output2_length,
4677 &function_output_length));
4678 TEST_LE_U(function_output_length,
4679 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4680 TEST_LE_U(function_output_length,
4681 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004682 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004685
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004686 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004687
4688exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 psa_cipher_abort(&operation1);
4690 psa_cipher_abort(&operation2);
4691 mbedtls_free(output1);
4692 mbedtls_free(output2);
4693 psa_destroy_key(key);
4694 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004695}
4696/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004697
Gilles Peskine20035e32018-02-03 22:44:14 +01004698/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004699void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4700 int alg_arg,
4701 data_t *nonce,
4702 data_t *additional_data,
4703 data_t *input_data,
4704 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004705{
Ronald Cron5425a212020-08-04 14:58:35 +02004706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004707 psa_key_type_t key_type = key_type_arg;
4708 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004709 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004710 unsigned char *output_data = NULL;
4711 size_t output_size = 0;
4712 size_t output_length = 0;
4713 unsigned char *output_data2 = NULL;
4714 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004715 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004716 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004718
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4722 psa_set_key_algorithm(&attributes, alg);
4723 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004724
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4726 &key));
4727 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4728 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4731 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004732 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4733 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4735 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4736 TEST_EQUAL(output_size,
4737 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4738 TEST_LE_U(output_size,
4739 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004740 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004741 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004742
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 status = psa_aead_encrypt(key, alg,
4744 nonce->x, nonce->len,
4745 additional_data->x,
4746 additional_data->len,
4747 input_data->x, input_data->len,
4748 output_data, output_size,
4749 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004750
4751 /* If the operation is not supported, just skip and not fail in case the
4752 * encryption involves a common limitation of cryptography hardwares and
4753 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 if (status == PSA_ERROR_NOT_SUPPORTED) {
4755 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4756 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004757 }
4758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004762 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004763
Gilles Peskine003a4a92019-05-14 16:09:40 +02004764 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4765 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 TEST_EQUAL(input_data->len,
4767 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004768
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 TEST_LE_U(input_data->len,
4770 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004771
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 TEST_EQUAL(psa_aead_decrypt(key, alg,
4773 nonce->x, nonce->len,
4774 additional_data->x,
4775 additional_data->len,
4776 output_data, output_length,
4777 output_data2, output_length,
4778 &output_length2),
4779 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004780
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004781 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004782 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004783 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004784
Gilles Peskinea1cac842018-06-11 19:33:02 +02004785exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 psa_destroy_key(key);
4787 mbedtls_free(output_data);
4788 mbedtls_free(output_data2);
4789 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004790}
4791/* END_CASE */
4792
4793/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004794void aead_encrypt(int key_type_arg, data_t *key_data,
4795 int alg_arg,
4796 data_t *nonce,
4797 data_t *additional_data,
4798 data_t *input_data,
4799 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004800{
Ronald Cron5425a212020-08-04 14:58:35 +02004801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802 psa_key_type_t key_type = key_type_arg;
4803 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004804 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004805 unsigned char *output_data = NULL;
4806 size_t output_size = 0;
4807 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004808 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004809 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004810
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4814 psa_set_key_algorithm(&attributes, alg);
4815 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4818 &key));
4819 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4820 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4823 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004824 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4825 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 TEST_EQUAL(output_size,
4827 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4828 TEST_LE_U(output_size,
4829 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004830 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004831
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 status = psa_aead_encrypt(key, alg,
4833 nonce->x, nonce->len,
4834 additional_data->x, additional_data->len,
4835 input_data->x, input_data->len,
4836 output_data, output_size,
4837 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004838
Ronald Cron28a45ed2021-02-09 20:35:42 +01004839 /* If the operation is not supported, just skip and not fail in case the
4840 * encryption involves a common limitation of cryptography hardwares and
4841 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (status == PSA_ERROR_NOT_SUPPORTED) {
4843 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4844 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004845 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004848 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004849 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004850
Gilles Peskinea1cac842018-06-11 19:33:02 +02004851exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 psa_destroy_key(key);
4853 mbedtls_free(output_data);
4854 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004855}
4856/* END_CASE */
4857
4858/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004859void aead_decrypt(int key_type_arg, data_t *key_data,
4860 int alg_arg,
4861 data_t *nonce,
4862 data_t *additional_data,
4863 data_t *input_data,
4864 data_t *expected_data,
4865 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004866{
Ronald Cron5425a212020-08-04 14:58:35 +02004867 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004868 psa_key_type_t key_type = key_type_arg;
4869 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004870 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004871 unsigned char *output_data = NULL;
4872 size_t output_size = 0;
4873 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004874 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004875 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004876 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4881 psa_set_key_algorithm(&attributes, alg);
4882 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4885 &key));
4886 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4887 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4890 alg);
4891 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4892 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004893 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4894 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 TEST_EQUAL(output_size,
4896 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4897 TEST_LE_U(output_size,
4898 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004899 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004900 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 status = psa_aead_decrypt(key, alg,
4903 nonce->x, nonce->len,
4904 additional_data->x,
4905 additional_data->len,
4906 input_data->x, input_data->len,
4907 output_data, output_size,
4908 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004909
Ronald Cron28a45ed2021-02-09 20:35:42 +01004910 /* If the operation is not supported, just skip and not fail in case the
4911 * decryption involves a common limitation of cryptography hardwares and
4912 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 if (status == PSA_ERROR_NOT_SUPPORTED) {
4914 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4915 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004916 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004919
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004921 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004922 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004924
Gilles Peskinea1cac842018-06-11 19:33:02 +02004925exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 psa_destroy_key(key);
4927 mbedtls_free(output_data);
4928 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004929}
4930/* END_CASE */
4931
4932/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004933void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4934 int alg_arg,
4935 data_t *nonce,
4936 data_t *additional_data,
4937 data_t *input_data,
4938 int do_set_lengths,
4939 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004940{
Paul Elliottd3f82412021-06-16 16:52:21 +01004941 size_t ad_part_len = 0;
4942 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004943 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004944
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4946 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (do_set_lengths) {
4949 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004950 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004952 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004954 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004955
4956 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 if (!aead_multipart_internal_func(key_type_arg, key_data,
4958 alg_arg, nonce,
4959 additional_data,
4960 ad_part_len,
4961 input_data, -1,
4962 set_lengths_method,
4963 expected_output,
4964 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004965 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004966 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 /* length(0) part, length(ad_part_len) part, length(0) part... */
4969 mbedtls_test_set_step(1000 + ad_part_len);
4970
4971 if (!aead_multipart_internal_func(key_type_arg, key_data,
4972 alg_arg, nonce,
4973 additional_data,
4974 ad_part_len,
4975 input_data, -1,
4976 set_lengths_method,
4977 expected_output,
4978 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004979 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 }
4981 }
4982
4983 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4984 /* Split data into length(data_part_len) parts. */
4985 mbedtls_test_set_step(2000 + data_part_len);
4986
4987 if (do_set_lengths) {
4988 if (data_part_len & 0x01) {
4989 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4990 } else {
4991 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4992 }
4993 }
4994
4995 if (!aead_multipart_internal_func(key_type_arg, key_data,
4996 alg_arg, nonce,
4997 additional_data, -1,
4998 input_data, data_part_len,
4999 set_lengths_method,
5000 expected_output,
5001 1, 0)) {
5002 break;
5003 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005004
5005 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005007
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 if (!aead_multipart_internal_func(key_type_arg, key_data,
5009 alg_arg, nonce,
5010 additional_data, -1,
5011 input_data, data_part_len,
5012 set_lengths_method,
5013 expected_output,
5014 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005015 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005017 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005018
Paul Elliott8fc45162021-06-23 16:06:01 +01005019 /* Goto is required to silence warnings about unused labels, as we
5020 * don't actually do any test assertions in this function. */
5021 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005022}
5023/* END_CASE */
5024
5025/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005026void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5027 int alg_arg,
5028 data_t *nonce,
5029 data_t *additional_data,
5030 data_t *input_data,
5031 int do_set_lengths,
5032 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005033{
Paul Elliottd3f82412021-06-16 16:52:21 +01005034 size_t ad_part_len = 0;
5035 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005036 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005037
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005039 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 if (do_set_lengths) {
5043 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005044 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005046 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005048 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005049
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 if (!aead_multipart_internal_func(key_type_arg, key_data,
5051 alg_arg, nonce,
5052 additional_data,
5053 ad_part_len,
5054 input_data, -1,
5055 set_lengths_method,
5056 expected_output,
5057 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005058 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005060
5061 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 if (!aead_multipart_internal_func(key_type_arg, key_data,
5065 alg_arg, nonce,
5066 additional_data,
5067 ad_part_len,
5068 input_data, -1,
5069 set_lengths_method,
5070 expected_output,
5071 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005072 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005074 }
5075
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005077 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 if (do_set_lengths) {
5081 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005082 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005084 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005086 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (!aead_multipart_internal_func(key_type_arg, key_data,
5089 alg_arg, nonce,
5090 additional_data, -1,
5091 input_data, data_part_len,
5092 set_lengths_method,
5093 expected_output,
5094 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005095 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005097
5098 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005100
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 if (!aead_multipart_internal_func(key_type_arg, key_data,
5102 alg_arg, nonce,
5103 additional_data, -1,
5104 input_data, data_part_len,
5105 set_lengths_method,
5106 expected_output,
5107 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005108 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005110 }
5111
Paul Elliott8fc45162021-06-23 16:06:01 +01005112 /* Goto is required to silence warnings about unused labels, as we
5113 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005114 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005115}
5116/* END_CASE */
5117
5118/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005119void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5120 int alg_arg,
5121 int nonce_length,
5122 int expected_nonce_length_arg,
5123 data_t *additional_data,
5124 data_t *input_data,
5125 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005126{
5127
5128 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5129 psa_key_type_t key_type = key_type_arg;
5130 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005131 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005132 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5134 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005135 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005136 size_t actual_nonce_length = 0;
5137 size_t expected_nonce_length = expected_nonce_length_arg;
5138 unsigned char *output = NULL;
5139 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005140 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005141 size_t ciphertext_size = 0;
5142 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005143 size_t tag_length = 0;
5144 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5149 psa_set_key_algorithm(&attributes, alg);
5150 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5153 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005158
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005159 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005164
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005165 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005168
5169 /* If the operation is not supported, just skip and not fail in case the
5170 * encryption involves a common limitation of cryptography hardwares and
5171 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 if (status == PSA_ERROR_NOT_SUPPORTED) {
5173 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5174 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005175 }
5176
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005178
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5180 nonce_length,
5181 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 if (expected_status == PSA_SUCCESS) {
5188 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5189 alg));
5190 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005195 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5197 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5200 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005201
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5203 output, output_size,
5204 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005205
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5207 &ciphertext_length, tag_buffer,
5208 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005209 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005210
5211exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 psa_destroy_key(key);
5213 mbedtls_free(output);
5214 mbedtls_free(ciphertext);
5215 psa_aead_abort(&operation);
5216 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005217}
5218/* END_CASE */
5219
5220/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005221void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5222 int alg_arg,
5223 int nonce_length_arg,
5224 int set_lengths_method_arg,
5225 data_t *additional_data,
5226 data_t *input_data,
5227 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005228{
5229
5230 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5231 psa_key_type_t key_type = key_type_arg;
5232 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005233 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005234 uint8_t *nonce_buffer = NULL;
5235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5236 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5237 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005238 unsigned char *output = NULL;
5239 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005240 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005241 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005242 size_t ciphertext_size = 0;
5243 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005244 size_t tag_length = 0;
5245 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005246 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005247 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005250
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5252 psa_set_key_algorithm(&attributes, alg);
5253 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5256 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005257
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005259
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005261
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005262 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005265
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005267
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005268 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005271
5272 /* If the operation is not supported, just skip and not fail in case the
5273 * encryption involves a common limitation of cryptography hardwares and
5274 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 if (status == PSA_ERROR_NOT_SUPPORTED) {
5276 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5277 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005278 }
5279
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005281
Paul Elliott4023ffd2021-09-10 16:21:22 +01005282 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 if (nonce_length_arg == -1) {
5284 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005285 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 nonce_length = 0;
5287 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005288 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005290 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005291
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (nonce_buffer) {
5293 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005294 nonce_buffer[index] = 'a' + index;
5295 }
Paul Elliott66696b52021-08-16 18:42:41 +01005296 }
Paul Elliott863864a2021-07-23 17:28:31 +01005297 }
5298
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5300 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5301 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005302 }
5303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005305
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005307
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 if (expected_status == PSA_SUCCESS) {
5309 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5310 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5311 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005312 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005314 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 }
Paul Elliott863864a2021-07-23 17:28:31 +01005316
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005317 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5319 additional_data->len),
5320 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005321
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5323 output, output_size,
5324 &ciphertext_length),
5325 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005326
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5328 &ciphertext_length, tag_buffer,
5329 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5330 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005331 }
5332
5333exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 psa_destroy_key(key);
5335 mbedtls_free(output);
5336 mbedtls_free(ciphertext);
5337 mbedtls_free(nonce_buffer);
5338 psa_aead_abort(&operation);
5339 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005340}
5341/* END_CASE */
5342
5343/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005344void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005345 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005346 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005347 data_t *nonce,
5348 data_t *additional_data,
5349 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005351{
5352
5353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5354 psa_key_type_t key_type = key_type_arg;
5355 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005356 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5358 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5359 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005360 unsigned char *output = NULL;
5361 unsigned char *ciphertext = NULL;
5362 size_t output_size = output_size_arg;
5363 size_t ciphertext_size = 0;
5364 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005365 size_t tag_length = 0;
5366 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5367
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005369
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5371 psa_set_key_algorithm(&attributes, alg);
5372 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5375 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005378
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005379 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005382
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005383 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005386
5387 /* If the operation is not supported, just skip and not fail in case the
5388 * encryption involves a common limitation of cryptography hardwares and
5389 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 if (status == PSA_ERROR_NOT_SUPPORTED) {
5391 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5392 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005393 }
5394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005396
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5398 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5403 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 status = psa_aead_update(&operation, input_data->x, input_data->len,
5406 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005409
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005411 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5413 &ciphertext_length, tag_buffer,
5414 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005415 }
5416
5417exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 psa_destroy_key(key);
5419 mbedtls_free(output);
5420 mbedtls_free(ciphertext);
5421 psa_aead_abort(&operation);
5422 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005423}
5424/* END_CASE */
5425
Paul Elliott91b021e2021-07-23 18:52:31 +01005426/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005427void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5428 int alg_arg,
5429 int finish_ciphertext_size_arg,
5430 int tag_size_arg,
5431 data_t *nonce,
5432 data_t *additional_data,
5433 data_t *input_data,
5434 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005435{
5436
5437 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5438 psa_key_type_t key_type = key_type_arg;
5439 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005440 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5442 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5443 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005444 unsigned char *ciphertext = NULL;
5445 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005446 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005447 size_t ciphertext_size = 0;
5448 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5450 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005451 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005452
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5456 psa_set_key_algorithm(&attributes, alg);
5457 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005458
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5460 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005465
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005466 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005467
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005468 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005469
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005470 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005473
5474 /* If the operation is not supported, just skip and not fail in case the
5475 * encryption involves a common limitation of cryptography hardwares and
5476 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 if (status == PSA_ERROR_NOT_SUPPORTED) {
5478 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5479 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005480 }
5481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005485
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5487 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005488
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5490 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005491
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5493 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005494
5495 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 status = psa_aead_finish(&operation, finish_ciphertext,
5497 finish_ciphertext_size,
5498 &ciphertext_length, tag_buffer,
5499 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005500
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005502
5503exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 psa_destroy_key(key);
5505 mbedtls_free(ciphertext);
5506 mbedtls_free(finish_ciphertext);
5507 mbedtls_free(tag_buffer);
5508 psa_aead_abort(&operation);
5509 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005510}
5511/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005512
5513/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005514void aead_multipart_verify(int key_type_arg, data_t *key_data,
5515 int alg_arg,
5516 data_t *nonce,
5517 data_t *additional_data,
5518 data_t *input_data,
5519 data_t *tag,
5520 int tag_usage_arg,
5521 int expected_setup_status_arg,
5522 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005523{
5524 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5525 psa_key_type_t key_type = key_type_arg;
5526 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005527 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5529 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5530 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005531 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005532 unsigned char *plaintext = NULL;
5533 unsigned char *finish_plaintext = NULL;
5534 size_t plaintext_size = 0;
5535 size_t plaintext_length = 0;
5536 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005537 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005538 unsigned char *tag_buffer = NULL;
5539 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5544 psa_set_key_algorithm(&attributes, alg);
5545 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5548 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5553 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005554
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005555 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005558
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005559 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005562
5563 /* If the operation is not supported, just skip and not fail in case the
5564 * encryption involves a common limitation of cryptography hardwares and
5565 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 if (status == PSA_ERROR_NOT_SUPPORTED) {
5567 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5568 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005569 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005573 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 }
Paul Elliott9961a662021-09-17 19:19:02 +01005575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 status = psa_aead_set_lengths(&operation, additional_data->len,
5581 input_data->len);
5582 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5585 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005586
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5588 input_data->len,
5589 plaintext, plaintext_size,
5590 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005591
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005593 tag_buffer = tag->x;
5594 tag_size = tag->len;
5595 }
5596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 status = psa_aead_verify(&operation, finish_plaintext,
5598 verify_plaintext_size,
5599 &plaintext_length,
5600 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005603
5604exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 psa_destroy_key(key);
5606 mbedtls_free(plaintext);
5607 mbedtls_free(finish_plaintext);
5608 psa_aead_abort(&operation);
5609 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005610}
5611/* END_CASE */
5612
Paul Elliott9961a662021-09-17 19:19:02 +01005613/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005614void aead_multipart_setup(int key_type_arg, data_t *key_data,
5615 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005616{
5617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5618 psa_key_type_t key_type = key_type_arg;
5619 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005620 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5622 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5623 psa_status_t expected_status = expected_status_arg;
5624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 psa_set_key_usage_flags(&attributes,
5628 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5629 psa_set_key_algorithm(&attributes, alg);
5630 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5633 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005634
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005636
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005644
5645exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 psa_destroy_key(key);
5647 psa_aead_abort(&operation);
5648 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005649}
5650/* END_CASE */
5651
5652/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005653void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5654 int alg_arg,
5655 data_t *nonce,
5656 data_t *additional_data,
5657 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005658{
5659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5660 psa_key_type_t key_type = key_type_arg;
5661 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005662 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005663 unsigned char *output_data = NULL;
5664 unsigned char *final_data = NULL;
5665 size_t output_size = 0;
5666 size_t finish_output_size = 0;
5667 size_t output_length = 0;
5668 size_t key_bits = 0;
5669 size_t tag_length = 0;
5670 size_t tag_size = 0;
5671 size_t nonce_length = 0;
5672 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5673 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5674 size_t output_part_length = 0;
5675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5676
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 psa_set_key_usage_flags(&attributes,
5680 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5681 psa_set_key_algorithm(&attributes, alg);
5682 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5685 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5688 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005695
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005696 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005701
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005702 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703
5704 /* Test all operations error without calling setup first. */
5705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5707 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005708
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5712 PSA_AEAD_NONCE_MAX_SIZE,
5713 &nonce_length),
5714 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005715
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717
Paul Elliott481be342021-07-16 17:38:47 +01005718 /* ------------------------------------------------------- */
5719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5721 input_data->len),
5722 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005725
Paul Elliott481be342021-07-16 17:38:47 +01005726 /* ------------------------------------------------------- */
5727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5729 additional_data->len),
5730 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005733
Paul Elliott481be342021-07-16 17:38:47 +01005734 /* ------------------------------------------------------- */
5735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5737 input_data->len, output_data,
5738 output_size, &output_length),
5739 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005742
Paul Elliott481be342021-07-16 17:38:47 +01005743 /* ------------------------------------------------------- */
5744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5746 finish_output_size,
5747 &output_part_length,
5748 tag_buffer, tag_length,
5749 &tag_size),
5750 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005753
Paul Elliott481be342021-07-16 17:38:47 +01005754 /* ------------------------------------------------------- */
5755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5757 finish_output_size,
5758 &output_part_length,
5759 tag_buffer,
5760 tag_length),
5761 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005762
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005764
5765 /* Test for double setups. */
5766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5770 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005773
Paul Elliott481be342021-07-16 17:38:47 +01005774 /* ------------------------------------------------------- */
5775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5779 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005780
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005782
Paul Elliott374a2be2021-07-16 17:53:40 +01005783 /* ------------------------------------------------------- */
5784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5788 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005791
5792 /* ------------------------------------------------------- */
5793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5797 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005800
Paul Elliottc23a9a02021-06-21 18:32:46 +01005801 /* Test for not setting a nonce. */
5802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5806 additional_data->len),
5807 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005810
Paul Elliott7f628422021-09-01 12:08:29 +01005811 /* ------------------------------------------------------- */
5812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5816 input_data->len, output_data,
5817 output_size, &output_length),
5818 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005821
Paul Elliottbdc2c682021-09-21 18:37:10 +01005822 /* ------------------------------------------------------- */
5823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5827 finish_output_size,
5828 &output_part_length,
5829 tag_buffer, tag_length,
5830 &tag_size),
5831 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005832
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005834
5835 /* ------------------------------------------------------- */
5836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005838
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5840 finish_output_size,
5841 &output_part_length,
5842 tag_buffer,
5843 tag_length),
5844 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005847
Paul Elliottc23a9a02021-06-21 18:32:46 +01005848 /* Test for double setting nonce. */
5849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5855 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005858
Paul Elliott374a2be2021-07-16 17:53:40 +01005859 /* Test for double generating nonce. */
5860
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5864 PSA_AEAD_NONCE_MAX_SIZE,
5865 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5868 PSA_AEAD_NONCE_MAX_SIZE,
5869 &nonce_length),
5870 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005871
5872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005874
5875 /* Test for generate nonce then set and vice versa */
5876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5880 PSA_AEAD_NONCE_MAX_SIZE,
5881 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5884 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005887
Andrzej Kurekad837522021-12-15 15:28:49 +01005888 /* Test for generating nonce after calling set lengths */
5889
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5893 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5896 PSA_AEAD_NONCE_MAX_SIZE,
5897 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005900
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005901 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005902
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 if (operation.alg == PSA_ALG_CCM) {
5906 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5907 input_data->len),
5908 PSA_ERROR_INVALID_ARGUMENT);
5909 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5910 PSA_AEAD_NONCE_MAX_SIZE,
5911 &nonce_length),
5912 PSA_ERROR_BAD_STATE);
5913 } else {
5914 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5915 input_data->len));
5916 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5917 PSA_AEAD_NONCE_MAX_SIZE,
5918 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005919 }
5920
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005922
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005923 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005924#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005926
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5928 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5929 input_data->len),
5930 PSA_ERROR_INVALID_ARGUMENT);
5931 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5932 PSA_AEAD_NONCE_MAX_SIZE,
5933 &nonce_length),
5934 PSA_ERROR_BAD_STATE);
5935 } else {
5936 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5937 input_data->len));
5938 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5939 PSA_AEAD_NONCE_MAX_SIZE,
5940 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005941 }
5942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005944#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005945
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005946 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005949
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5951 PSA_AEAD_NONCE_MAX_SIZE,
5952 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if (operation.alg == PSA_ALG_CCM) {
5955 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5956 input_data->len),
5957 PSA_ERROR_INVALID_ARGUMENT);
5958 } else {
5959 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5960 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005961 }
5962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005964
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005965 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005966 /* Test for setting nonce after calling set lengths */
5967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5971 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005976
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005977 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if (operation.alg == PSA_ALG_CCM) {
5982 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5983 input_data->len),
5984 PSA_ERROR_INVALID_ARGUMENT);
5985 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5986 PSA_ERROR_BAD_STATE);
5987 } else {
5988 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5989 input_data->len));
5990 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005991 }
5992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005994
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005995 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005996#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005998
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6000 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6001 input_data->len),
6002 PSA_ERROR_INVALID_ARGUMENT);
6003 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6004 PSA_ERROR_BAD_STATE);
6005 } else {
6006 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6007 input_data->len));
6008 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006009 }
6010
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006012#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006013
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006014 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006015
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006017
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 if (operation.alg == PSA_ALG_CCM) {
6021 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6022 input_data->len),
6023 PSA_ERROR_INVALID_ARGUMENT);
6024 } else {
6025 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6026 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006027 }
6028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006030
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006031 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006032#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006034
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 if (operation.alg == PSA_ALG_GCM) {
6036 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6037 SIZE_MAX),
6038 PSA_ERROR_INVALID_ARGUMENT);
6039 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6040 PSA_ERROR_BAD_STATE);
6041 } else if (operation.alg != PSA_ALG_CCM) {
6042 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6043 SIZE_MAX));
6044 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006045 }
6046
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006048#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006049
Tom Cosgrove1797b052022-12-04 17:19:59 +00006050 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006051#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 if (operation.alg == PSA_ALG_GCM) {
6057 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6058 SIZE_MAX),
6059 PSA_ERROR_INVALID_ARGUMENT);
6060 } else if (operation.alg != PSA_ALG_CCM) {
6061 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6062 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006063 }
6064
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006066#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006067
6068 /* ------------------------------------------------------- */
6069
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6075 PSA_AEAD_NONCE_MAX_SIZE,
6076 &nonce_length),
6077 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006078
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006080
Paul Elliott7220cae2021-06-22 17:25:57 +01006081 /* Test for generating nonce in decrypt setup. */
6082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6086 PSA_AEAD_NONCE_MAX_SIZE,
6087 &nonce_length),
6088 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006091
Paul Elliottc23a9a02021-06-21 18:32:46 +01006092 /* Test for setting lengths twice. */
6093
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6099 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6102 input_data->len),
6103 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006106
Andrzej Kurekad837522021-12-15 15:28:49 +01006107 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006110
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6116 additional_data->len),
6117 PSA_ERROR_BAD_STATE);
6118 } else {
6119 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6120 additional_data->len));
6121
6122 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6123 input_data->len),
6124 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006125 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006127
6128 /* ------------------------------------------------------- */
6129
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 if (operation.alg == PSA_ALG_CCM) {
6135 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6136 input_data->len, output_data,
6137 output_size, &output_length),
6138 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 } else {
6141 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6142 input_data->len, output_data,
6143 output_size, &output_length));
6144
6145 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6146 input_data->len),
6147 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006148 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006150
6151 /* ------------------------------------------------------- */
6152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (operation.alg == PSA_ALG_CCM) {
6158 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6159 finish_output_size,
6160 &output_part_length,
6161 tag_buffer, tag_length,
6162 &tag_size));
6163 } else {
6164 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6165 finish_output_size,
6166 &output_part_length,
6167 tag_buffer, tag_length,
6168 &tag_size));
6169
6170 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6171 input_data->len),
6172 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006173 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006175
6176 /* Test for setting lengths after generating nonce + already starting data. */
6177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6181 PSA_AEAD_NONCE_MAX_SIZE,
6182 &nonce_length));
6183 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6186 additional_data->len),
6187 PSA_ERROR_BAD_STATE);
6188 } else {
6189 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6190 additional_data->len));
6191
6192 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6193 input_data->len),
6194 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006195 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006197
6198 /* ------------------------------------------------------- */
6199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006201
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6203 PSA_AEAD_NONCE_MAX_SIZE,
6204 &nonce_length));
6205 if (operation.alg == PSA_ALG_CCM) {
6206 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6207 input_data->len, output_data,
6208 output_size, &output_length),
6209 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 } else {
6212 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6213 input_data->len, output_data,
6214 output_size, &output_length));
6215
6216 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6217 input_data->len),
6218 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006219 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006221
6222 /* ------------------------------------------------------- */
6223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6227 PSA_AEAD_NONCE_MAX_SIZE,
6228 &nonce_length));
6229 if (operation.alg == PSA_ALG_CCM) {
6230 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6231 finish_output_size,
6232 &output_part_length,
6233 tag_buffer, tag_length,
6234 &tag_size));
6235 } else {
6236 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6237 finish_output_size,
6238 &output_part_length,
6239 tag_buffer, tag_length,
6240 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6243 input_data->len),
6244 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006245 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006247
Paul Elliott243080c2021-07-21 19:01:17 +01006248 /* Test for not sending any additional data or data after setting non zero
6249 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6256 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6259 finish_output_size,
6260 &output_part_length,
6261 tag_buffer, tag_length,
6262 &tag_size),
6263 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006264
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006266
Paul Elliott243080c2021-07-21 19:01:17 +01006267 /* Test for not sending any additional data or data after setting non-zero
6268 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6275 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6278 finish_output_size,
6279 &output_part_length,
6280 tag_buffer,
6281 tag_length),
6282 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006285
Paul Elliott243080c2021-07-21 19:01:17 +01006286 /* Test for not sending any additional data after setting a non-zero length
6287 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6294 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6297 input_data->len, output_data,
6298 output_size, &output_length),
6299 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006302
Paul Elliottf94bd992021-09-19 18:15:59 +01006303 /* Test for not sending any data after setting a non-zero length for it.*/
6304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6310 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6313 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006314
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6316 finish_output_size,
6317 &output_part_length,
6318 tag_buffer, tag_length,
6319 &tag_size),
6320 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006323
Paul Elliottb0450fe2021-09-01 15:06:26 +01006324 /* Test for sending too much additional data after setting lengths. */
6325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006331
6332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6334 additional_data->len),
6335 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006338
Paul Elliotta2a09b02021-09-22 14:56:40 +01006339 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6346 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6349 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6352 1),
6353 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006354
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006356
Paul Elliottb0450fe2021-09-01 15:06:26 +01006357 /* Test for sending too much data after setting lengths. */
6358
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6366 input_data->len, output_data,
6367 output_size, &output_length),
6368 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006371
Paul Elliotta2a09b02021-09-22 14:56:40 +01006372 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006373
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6379 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6382 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6385 input_data->len, output_data,
6386 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6389 1, output_data,
6390 output_size, &output_length),
6391 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006394
Paul Elliottc23a9a02021-06-21 18:32:46 +01006395 /* Test sending additional data after data. */
6396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 if (operation.alg != PSA_ALG_CCM) {
6402 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6403 input_data->len, output_data,
6404 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6407 additional_data->len),
6408 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006409 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006411
Paul Elliott534d0b42021-06-22 19:15:20 +01006412 /* Test calling finish on decryption. */
6413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6419 finish_output_size,
6420 &output_part_length,
6421 tag_buffer, tag_length,
6422 &tag_size),
6423 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006426
6427 /* Test calling verify on encryption. */
6428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006430
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6434 finish_output_size,
6435 &output_part_length,
6436 tag_buffer,
6437 tag_length),
6438 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006441
6442
Paul Elliottc23a9a02021-06-21 18:32:46 +01006443exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 psa_destroy_key(key);
6445 psa_aead_abort(&operation);
6446 mbedtls_free(output_data);
6447 mbedtls_free(final_data);
6448 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006449}
6450/* END_CASE */
6451
6452/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006453void signature_size(int type_arg,
6454 int bits,
6455 int alg_arg,
6456 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006457{
6458 psa_key_type_t type = type_arg;
6459 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006463
Gilles Peskinee59236f2018-01-27 23:32:46 +01006464exit:
6465 ;
6466}
6467/* END_CASE */
6468
6469/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006470void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6471 int alg_arg, data_t *input_data,
6472 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006473{
Ronald Cron5425a212020-08-04 14:58:35 +02006474 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006475 psa_key_type_t key_type = key_type_arg;
6476 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006477 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006478 unsigned char *signature = NULL;
6479 size_t signature_size;
6480 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006482
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006484
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6486 psa_set_key_algorithm(&attributes, alg);
6487 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6490 &key));
6491 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6492 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006493
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006494 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006495 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6497 key_bits, alg);
6498 TEST_ASSERT(signature_size != 0);
6499 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006500 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006501
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006502 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 PSA_ASSERT(psa_sign_hash(key, alg,
6504 input_data->x, input_data->len,
6505 signature, signature_size,
6506 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006507 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006508 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006509 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006510
6511exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006512 /*
6513 * Key attributes may have been returned by psa_get_key_attributes()
6514 * thus reset them as required.
6515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006517
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 psa_destroy_key(key);
6519 mbedtls_free(signature);
6520 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006521}
6522/* END_CASE */
6523
Paul Elliott712d5122022-12-07 14:03:10 +00006524/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006525/**
6526 * sign_hash_interruptible() test intentions:
6527 *
6528 * Note: This test can currently only handle ECDSA.
6529 *
6530 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006531 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006532 *
6533 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6534 * expected for different max_ops values.
6535 *
6536 * 3. Test that the number of ops done prior to start and after abort is zero
6537 * and that each successful stage completes some ops (this is not mandated by
6538 * the PSA specification, but is currently the case).
6539 *
6540 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6541 * complete() calls does not alter the number of ops returned.
6542 */
Paul Elliott712d5122022-12-07 14:03:10 +00006543void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6544 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006545 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006546{
6547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6548 psa_key_type_t key_type = key_type_arg;
6549 psa_algorithm_t alg = alg_arg;
6550 size_t key_bits;
6551 unsigned char *signature = NULL;
6552 size_t signature_size;
6553 size_t signature_length = 0xdeadbeef;
6554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6555 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006556 uint32_t num_ops = 0;
6557 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006558 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006559 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006560 size_t min_completes = 0;
6561 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006562
Paul Elliott712d5122022-12-07 14:03:10 +00006563 psa_sign_hash_interruptible_operation_t operation =
6564 psa_sign_hash_interruptible_operation_init();
6565
6566 PSA_ASSERT(psa_crypto_init());
6567
6568 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6569 psa_set_key_algorithm(&attributes, alg);
6570 psa_set_key_type(&attributes, key_type);
6571
6572 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6573 &key));
6574 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6575 key_bits = psa_get_key_bits(&attributes);
6576
6577 /* Allocate a buffer which has the size advertised by the
6578 * library. */
6579 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6580 key_bits, alg);
6581 TEST_ASSERT(signature_size != 0);
6582 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006583 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006584
Paul Elliott0c683352022-12-16 19:16:56 +00006585 psa_interruptible_set_max_ops(max_ops);
6586
Paul Elliott6f600372023-02-06 18:41:05 +00006587 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6588 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006589
Paul Elliott712d5122022-12-07 14:03:10 +00006590 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6591 TEST_ASSERT(num_ops_prior == 0);
6592
6593 /* Start performing the signature. */
6594 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6595 input_data->x, input_data->len));
6596
6597 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6598 TEST_ASSERT(num_ops_prior == 0);
6599
6600 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006601 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006602 status = psa_sign_hash_complete(&operation, signature, signature_size,
6603 &signature_length);
6604
Paul Elliott0c683352022-12-16 19:16:56 +00006605 num_completes++;
6606
Paul Elliott712d5122022-12-07 14:03:10 +00006607 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6608 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006609 /* We are asserting here that every complete makes progress
6610 * (completes some ops), which is true of the internal
6611 * implementation and probably any implementation, however this is
6612 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006613 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006614
Paul Elliott712d5122022-12-07 14:03:10 +00006615 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006616
6617 /* Ensure calling get_num_ops() twice still returns the same
6618 * number of ops as previously reported. */
6619 num_ops = psa_sign_hash_get_num_ops(&operation);
6620
6621 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006622 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006623 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006624
6625 TEST_ASSERT(status == PSA_SUCCESS);
6626
Paul Elliott0c683352022-12-16 19:16:56 +00006627 TEST_LE_U(min_completes, num_completes);
6628 TEST_LE_U(num_completes, max_completes);
6629
Paul Elliott712d5122022-12-07 14:03:10 +00006630 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006631 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006632 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006633
6634 PSA_ASSERT(psa_sign_hash_abort(&operation));
6635
Paul Elliott59ad9452022-12-18 15:09:02 +00006636 num_ops = psa_sign_hash_get_num_ops(&operation);
6637 TEST_ASSERT(num_ops == 0);
6638
Paul Elliott712d5122022-12-07 14:03:10 +00006639exit:
6640
6641 /*
6642 * Key attributes may have been returned by psa_get_key_attributes()
6643 * thus reset them as required.
6644 */
6645 psa_reset_key_attributes(&attributes);
6646
6647 psa_destroy_key(key);
6648 mbedtls_free(signature);
6649 PSA_DONE();
6650}
6651/* END_CASE */
6652
Gilles Peskine20035e32018-02-03 22:44:14 +01006653/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006654void sign_hash_fail(int key_type_arg, data_t *key_data,
6655 int alg_arg, data_t *input_data,
6656 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006657{
Ronald Cron5425a212020-08-04 14:58:35 +02006658 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006659 psa_key_type_t key_type = key_type_arg;
6660 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006661 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006662 psa_status_t actual_status;
6663 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006664 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006665 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006667
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006668 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006669
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006671
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6673 psa_set_key_algorithm(&attributes, alg);
6674 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006675
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6677 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006678
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 actual_status = psa_sign_hash(key, alg,
6680 input_data->x, input_data->len,
6681 signature, signature_size,
6682 &signature_length);
6683 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006684 /* The value of *signature_length is unspecified on error, but
6685 * whatever it is, it should be less than signature_size, so that
6686 * if the caller tries to read *signature_length bytes without
6687 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006689
6690exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 psa_reset_key_attributes(&attributes);
6692 psa_destroy_key(key);
6693 mbedtls_free(signature);
6694 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006695}
6696/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006697
Paul Elliott91007972022-12-16 12:21:24 +00006698/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006699/**
6700 * sign_hash_fail_interruptible() test intentions:
6701 *
6702 * Note: This test can currently only handle ECDSA.
6703 *
6704 * 1. Test that various failure cases for interruptible sign hash fail with the
6705 * correct error codes, and at the correct point (at start or during
6706 * complete).
6707 *
6708 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6709 * expected for different max_ops values.
6710 *
6711 * 3. Test that the number of ops done prior to start and after abort is zero
6712 * and that each successful stage completes some ops (this is not mandated by
6713 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006714 *
6715 * 4. Check that calling complete() when start() fails and complete()
6716 * after completion results in a BAD_STATE error.
6717 *
6718 * 5. Check that calling start() again after start fails results in a BAD_STATE
6719 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006720 */
Paul Elliott91007972022-12-16 12:21:24 +00006721void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6722 int alg_arg, data_t *input_data,
6723 int signature_size_arg,
6724 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006725 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006726 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006727{
6728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6729 psa_key_type_t key_type = key_type_arg;
6730 psa_algorithm_t alg = alg_arg;
6731 size_t signature_size = signature_size_arg;
6732 psa_status_t actual_status;
6733 psa_status_t expected_start_status = expected_start_status_arg;
6734 psa_status_t expected_complete_status = expected_complete_status_arg;
6735 unsigned char *signature = NULL;
6736 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006737 uint32_t num_ops = 0;
6738 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006739 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006740 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006741 size_t min_completes = 0;
6742 size_t max_completes = 0;
6743
Paul Elliott91007972022-12-16 12:21:24 +00006744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6745 psa_sign_hash_interruptible_operation_t operation =
6746 psa_sign_hash_interruptible_operation_init();
6747
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006748 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006749
6750 PSA_ASSERT(psa_crypto_init());
6751
6752 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6753 psa_set_key_algorithm(&attributes, alg);
6754 psa_set_key_type(&attributes, key_type);
6755
6756 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6757 &key));
6758
Paul Elliott0c683352022-12-16 19:16:56 +00006759 psa_interruptible_set_max_ops(max_ops);
6760
Paul Elliott6f600372023-02-06 18:41:05 +00006761 interruptible_signverify_get_minmax_completes(max_ops,
6762 expected_complete_status,
6763 &min_completes,
6764 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006765
Paul Elliott91007972022-12-16 12:21:24 +00006766 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6767 TEST_ASSERT(num_ops_prior == 0);
6768
6769 /* Start performing the signature. */
6770 actual_status = psa_sign_hash_start(&operation, key, alg,
6771 input_data->x, input_data->len);
6772
6773 TEST_EQUAL(actual_status, expected_start_status);
6774
Paul Elliottc9774412023-02-06 15:14:07 +00006775 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006776 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006777 * start failed. */
6778 actual_status = psa_sign_hash_complete(&operation, signature,
6779 signature_size,
6780 &signature_length);
6781
6782 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6783
6784 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006785 actual_status = psa_sign_hash_start(&operation, key, alg,
6786 input_data->x, input_data->len);
6787
6788 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6789 }
6790
Paul Elliott91007972022-12-16 12:21:24 +00006791 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6792 TEST_ASSERT(num_ops_prior == 0);
6793
Paul Elliott91007972022-12-16 12:21:24 +00006794 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006795 do {
Paul Elliott91007972022-12-16 12:21:24 +00006796 actual_status = psa_sign_hash_complete(&operation, signature,
6797 signature_size,
6798 &signature_length);
6799
Paul Elliott0c683352022-12-16 19:16:56 +00006800 num_completes++;
6801
Paul Elliott334d7262023-01-20 17:29:41 +00006802 if (actual_status == PSA_SUCCESS ||
6803 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006804 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006805 /* We are asserting here that every complete makes progress
6806 * (completes some ops), which is true of the internal
6807 * implementation and probably any implementation, however this is
6808 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006809 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006810
Paul Elliott91007972022-12-16 12:21:24 +00006811 num_ops_prior = num_ops;
6812 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006813 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006814
Paul Elliottc9774412023-02-06 15:14:07 +00006815 TEST_EQUAL(actual_status, expected_complete_status);
6816
Paul Elliottefebad02023-02-15 16:56:45 +00006817 /* Check that another complete returns BAD_STATE. */
6818 actual_status = psa_sign_hash_complete(&operation, signature,
6819 signature_size,
6820 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006821
Paul Elliottefebad02023-02-15 16:56:45 +00006822 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006823
Paul Elliott91007972022-12-16 12:21:24 +00006824 PSA_ASSERT(psa_sign_hash_abort(&operation));
6825
Paul Elliott59ad9452022-12-18 15:09:02 +00006826 num_ops = psa_sign_hash_get_num_ops(&operation);
6827 TEST_ASSERT(num_ops == 0);
6828
Paul Elliott91007972022-12-16 12:21:24 +00006829 /* The value of *signature_length is unspecified on error, but
6830 * whatever it is, it should be less than signature_size, so that
6831 * if the caller tries to read *signature_length bytes without
6832 * checking the error code then they don't overflow a buffer. */
6833 TEST_LE_U(signature_length, signature_size);
6834
Paul Elliott0c683352022-12-16 19:16:56 +00006835 TEST_LE_U(min_completes, num_completes);
6836 TEST_LE_U(num_completes, max_completes);
6837
Paul Elliott91007972022-12-16 12:21:24 +00006838exit:
6839 psa_reset_key_attributes(&attributes);
6840 psa_destroy_key(key);
6841 mbedtls_free(signature);
6842 PSA_DONE();
6843}
6844/* END_CASE */
6845
mohammad16038cc1cee2018-03-28 01:21:33 +03006846/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006847void sign_verify_hash(int key_type_arg, data_t *key_data,
6848 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006849{
Ronald Cron5425a212020-08-04 14:58:35 +02006850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006851 psa_key_type_t key_type = key_type_arg;
6852 psa_algorithm_t alg = alg_arg;
6853 size_t key_bits;
6854 unsigned char *signature = NULL;
6855 size_t signature_size;
6856 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006857 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006858
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006860
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6862 psa_set_key_algorithm(&attributes, alg);
6863 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006864
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6866 &key));
6867 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6868 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006869
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006870 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006871 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6873 key_bits, alg);
6874 TEST_ASSERT(signature_size != 0);
6875 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006876 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006877
6878 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 PSA_ASSERT(psa_sign_hash(key, alg,
6880 input_data->x, input_data->len,
6881 signature, signature_size,
6882 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006883 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 TEST_LE_U(signature_length, signature_size);
6885 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006886
6887 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 PSA_ASSERT(psa_verify_hash(key, alg,
6889 input_data->x, input_data->len,
6890 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006891
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006893 /* Flip a bit in the input and verify that the signature is now
6894 * detected as invalid. Flip a bit at the beginning, not at the end,
6895 * because ECDSA may ignore the last few bits of the input. */
6896 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 TEST_EQUAL(psa_verify_hash(key, alg,
6898 input_data->x, input_data->len,
6899 signature, signature_length),
6900 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006901 }
6902
6903exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006904 /*
6905 * Key attributes may have been returned by psa_get_key_attributes()
6906 * thus reset them as required.
6907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006909
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 psa_destroy_key(key);
6911 mbedtls_free(signature);
6912 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006913}
6914/* END_CASE */
6915
Paul Elliott712d5122022-12-07 14:03:10 +00006916/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006917/**
6918 * sign_verify_hash_interruptible() test intentions:
6919 *
6920 * Note: This test can currently only handle ECDSA.
6921 *
Paul Elliott8c092052023-03-06 17:49:14 +00006922 * 1. Test that we can sign an input hash with the given keypair and then
6923 * afterwards verify that signature. This is currently the only way to test
6924 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006925 *
6926 * 2. Test that after corrupting the hash, the verification detects an invalid
6927 * signature.
6928 *
6929 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6930 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006931 *
6932 * 4. Test that the number of ops done prior to starting signing and after abort
6933 * is zero and that each successful signing stage completes some ops (this is
6934 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006935 */
Paul Elliott712d5122022-12-07 14:03:10 +00006936void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006937 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006938 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006939{
6940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6941 psa_key_type_t key_type = key_type_arg;
6942 psa_algorithm_t alg = alg_arg;
6943 size_t key_bits;
6944 unsigned char *signature = NULL;
6945 size_t signature_size;
6946 size_t signature_length = 0xdeadbeef;
6947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6948 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006949 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006950 uint32_t num_ops = 0;
6951 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006952 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006953 size_t min_completes = 0;
6954 size_t max_completes = 0;
6955
Paul Elliott712d5122022-12-07 14:03:10 +00006956 psa_sign_hash_interruptible_operation_t sign_operation =
6957 psa_sign_hash_interruptible_operation_init();
6958 psa_verify_hash_interruptible_operation_t verify_operation =
6959 psa_verify_hash_interruptible_operation_init();
6960
6961 PSA_ASSERT(psa_crypto_init());
6962
Paul Elliott0c683352022-12-16 19:16:56 +00006963 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6964 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006965 psa_set_key_algorithm(&attributes, alg);
6966 psa_set_key_type(&attributes, key_type);
6967
6968 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6969 &key));
6970 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6971 key_bits = psa_get_key_bits(&attributes);
6972
6973 /* Allocate a buffer which has the size advertised by the
6974 * library. */
6975 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6976 key_bits, alg);
6977 TEST_ASSERT(signature_size != 0);
6978 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006979 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006980
Paul Elliott0c683352022-12-16 19:16:56 +00006981 psa_interruptible_set_max_ops(max_ops);
6982
Paul Elliott6f600372023-02-06 18:41:05 +00006983 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6984 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006985
Paul Elliott7c173082023-02-26 18:44:45 +00006986 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6987 TEST_ASSERT(num_ops_prior == 0);
6988
Paul Elliott712d5122022-12-07 14:03:10 +00006989 /* Start performing the signature. */
6990 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6991 input_data->x, input_data->len));
6992
Paul Elliott7c173082023-02-26 18:44:45 +00006993 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6994 TEST_ASSERT(num_ops_prior == 0);
6995
Paul Elliott712d5122022-12-07 14:03:10 +00006996 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006997 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006998
Paul Elliott0c683352022-12-16 19:16:56 +00006999 status = psa_sign_hash_complete(&sign_operation, signature,
7000 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007001 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007002
7003 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007004
7005 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7006 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7007 /* We are asserting here that every complete makes progress
7008 * (completes some ops), which is true of the internal
7009 * implementation and probably any implementation, however this is
7010 * not mandated by the PSA specification. */
7011 TEST_ASSERT(num_ops > num_ops_prior);
7012
7013 num_ops_prior = num_ops;
7014 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007015 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007016
7017 TEST_ASSERT(status == PSA_SUCCESS);
7018
Paul Elliott0c683352022-12-16 19:16:56 +00007019 TEST_LE_U(min_completes, num_completes);
7020 TEST_LE_U(num_completes, max_completes);
7021
Paul Elliott712d5122022-12-07 14:03:10 +00007022 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7023
Paul Elliott7c173082023-02-26 18:44:45 +00007024 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7025 TEST_ASSERT(num_ops == 0);
7026
Paul Elliott712d5122022-12-07 14:03:10 +00007027 /* Check that the signature length looks sensible. */
7028 TEST_LE_U(signature_length, signature_size);
7029 TEST_ASSERT(signature_length > 0);
7030
Paul Elliott0c683352022-12-16 19:16:56 +00007031 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007032
7033 /* Start verification. */
7034 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7035 input_data->x, input_data->len,
7036 signature, signature_length));
7037
7038 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007039 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007040 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007041
7042 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007043 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007044
7045 TEST_ASSERT(status == PSA_SUCCESS);
7046
Paul Elliott0c683352022-12-16 19:16:56 +00007047 TEST_LE_U(min_completes, num_completes);
7048 TEST_LE_U(num_completes, max_completes);
7049
Paul Elliott712d5122022-12-07 14:03:10 +00007050 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7051
7052 verify_operation = psa_verify_hash_interruptible_operation_init();
7053
7054 if (input_data->len != 0) {
7055 /* Flip a bit in the input and verify that the signature is now
7056 * detected as invalid. Flip a bit at the beginning, not at the end,
7057 * because ECDSA may ignore the last few bits of the input. */
7058 input_data->x[0] ^= 1;
7059
Paul Elliott712d5122022-12-07 14:03:10 +00007060 /* Start verification. */
7061 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7062 input_data->x, input_data->len,
7063 signature, signature_length));
7064
7065 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007066 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007067 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007068 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007069
7070 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7071 }
7072
7073 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7074
7075exit:
7076 /*
7077 * Key attributes may have been returned by psa_get_key_attributes()
7078 * thus reset them as required.
7079 */
7080 psa_reset_key_attributes(&attributes);
7081
7082 psa_destroy_key(key);
7083 mbedtls_free(signature);
7084 PSA_DONE();
7085}
7086/* END_CASE */
7087
Gilles Peskine9911b022018-06-29 17:30:48 +02007088/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007089void verify_hash(int key_type_arg, data_t *key_data,
7090 int alg_arg, data_t *hash_data,
7091 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007092{
Ronald Cron5425a212020-08-04 14:58:35 +02007093 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007094 psa_key_type_t key_type = key_type_arg;
7095 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007097
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007099
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007101
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7103 psa_set_key_algorithm(&attributes, alg);
7104 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007105
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7107 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007108
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 PSA_ASSERT(psa_verify_hash(key, alg,
7110 hash_data->x, hash_data->len,
7111 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007112
itayzafrir5c753392018-05-08 11:18:38 +03007113exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 psa_reset_key_attributes(&attributes);
7115 psa_destroy_key(key);
7116 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007117}
7118/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007119
Paul Elliott712d5122022-12-07 14:03:10 +00007120/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007121/**
7122 * verify_hash_interruptible() test intentions:
7123 *
7124 * Note: This test can currently only handle ECDSA.
7125 *
7126 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007127 * only). Given this test only does verification it can accept public keys as
7128 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007129 *
7130 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7131 * expected for different max_ops values.
7132 *
7133 * 3. Test that the number of ops done prior to start and after abort is zero
7134 * and that each successful stage completes some ops (this is not mandated by
7135 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007136 *
7137 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7138 * complete() calls does not alter the number of ops returned.
7139 *
7140 * 5. Test that after corrupting the hash, the verification detects an invalid
7141 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007142 */
Paul Elliott712d5122022-12-07 14:03:10 +00007143void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7144 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007145 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007146{
7147 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7148 psa_key_type_t key_type = key_type_arg;
7149 psa_algorithm_t alg = alg_arg;
7150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7151 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007152 uint32_t num_ops = 0;
7153 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007154 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007155 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007156 size_t min_completes = 0;
7157 size_t max_completes = 0;
7158
Paul Elliott712d5122022-12-07 14:03:10 +00007159 psa_verify_hash_interruptible_operation_t operation =
7160 psa_verify_hash_interruptible_operation_init();
7161
7162 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7163
7164 PSA_ASSERT(psa_crypto_init());
7165
7166 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7167 psa_set_key_algorithm(&attributes, alg);
7168 psa_set_key_type(&attributes, key_type);
7169
7170 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7171 &key));
7172
Paul Elliott0c683352022-12-16 19:16:56 +00007173 psa_interruptible_set_max_ops(max_ops);
7174
Paul Elliott6f600372023-02-06 18:41:05 +00007175 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7176 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007177
Paul Elliott712d5122022-12-07 14:03:10 +00007178 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7179
7180 TEST_ASSERT(num_ops_prior == 0);
7181
7182 /* Start verification. */
7183 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7184 hash_data->x, hash_data->len,
7185 signature_data->x, signature_data->len)
7186 );
7187
7188 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7189
7190 TEST_ASSERT(num_ops_prior == 0);
7191
7192 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007193 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007194 status = psa_verify_hash_complete(&operation);
7195
Paul Elliott0c683352022-12-16 19:16:56 +00007196 num_completes++;
7197
Paul Elliott712d5122022-12-07 14:03:10 +00007198 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7199 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007200 /* We are asserting here that every complete makes progress
7201 * (completes some ops), which is true of the internal
7202 * implementation and probably any implementation, however this is
7203 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007204 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007205
Paul Elliott712d5122022-12-07 14:03:10 +00007206 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007207
7208 /* Ensure calling get_num_ops() twice still returns the same
7209 * number of ops as previously reported. */
7210 num_ops = psa_verify_hash_get_num_ops(&operation);
7211
7212 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007213 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007214 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007215
7216 TEST_ASSERT(status == PSA_SUCCESS);
7217
Paul Elliott0c683352022-12-16 19:16:56 +00007218 TEST_LE_U(min_completes, num_completes);
7219 TEST_LE_U(num_completes, max_completes);
7220
Paul Elliott712d5122022-12-07 14:03:10 +00007221 PSA_ASSERT(psa_verify_hash_abort(&operation));
7222
Paul Elliott59ad9452022-12-18 15:09:02 +00007223 num_ops = psa_verify_hash_get_num_ops(&operation);
7224 TEST_ASSERT(num_ops == 0);
7225
Paul Elliott8359c142023-02-24 18:40:10 +00007226 if (hash_data->len != 0) {
7227 /* Flip a bit in the hash and verify that the signature is now detected
7228 * as invalid. Flip a bit at the beginning, not at the end, because
7229 * ECDSA may ignore the last few bits of the input. */
7230 hash_data->x[0] ^= 1;
7231
7232 /* Start verification. */
7233 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7234 hash_data->x, hash_data->len,
7235 signature_data->x, signature_data->len));
7236
7237 /* Continue performing the signature until complete. */
7238 do {
7239 status = psa_verify_hash_complete(&operation);
7240 } while (status == PSA_OPERATION_INCOMPLETE);
7241
7242 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7243 }
7244
Paul Elliott712d5122022-12-07 14:03:10 +00007245exit:
7246 psa_reset_key_attributes(&attributes);
7247 psa_destroy_key(key);
7248 PSA_DONE();
7249}
7250/* END_CASE */
7251
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007252/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007253void verify_hash_fail(int key_type_arg, data_t *key_data,
7254 int alg_arg, data_t *hash_data,
7255 data_t *signature_data,
7256 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007257{
Ronald Cron5425a212020-08-04 14:58:35 +02007258 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007259 psa_key_type_t key_type = key_type_arg;
7260 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007261 psa_status_t actual_status;
7262 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007264
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007266
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7268 psa_set_key_algorithm(&attributes, alg);
7269 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007270
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7272 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007273
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 actual_status = psa_verify_hash(key, alg,
7275 hash_data->x, hash_data->len,
7276 signature_data->x, signature_data->len);
7277 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007278
7279exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 psa_reset_key_attributes(&attributes);
7281 psa_destroy_key(key);
7282 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007283}
7284/* END_CASE */
7285
Paul Elliott91007972022-12-16 12:21:24 +00007286/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007287/**
7288 * verify_hash_fail_interruptible() test intentions:
7289 *
7290 * Note: This test can currently only handle ECDSA.
7291 *
7292 * 1. Test that various failure cases for interruptible verify hash fail with
7293 * the correct error codes, and at the correct point (at start or during
7294 * complete).
7295 *
7296 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7297 * expected for different max_ops values.
7298 *
7299 * 3. Test that the number of ops done prior to start and after abort is zero
7300 * and that each successful stage completes some ops (this is not mandated by
7301 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007302 *
7303 * 4. Check that calling complete() when start() fails and complete()
7304 * after completion results in a BAD_STATE error.
7305 *
7306 * 5. Check that calling start() again after start fails results in a BAD_STATE
7307 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007308 */
Paul Elliott91007972022-12-16 12:21:24 +00007309void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7310 int alg_arg, data_t *hash_data,
7311 data_t *signature_data,
7312 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007313 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007314 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007315{
7316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7317 psa_key_type_t key_type = key_type_arg;
7318 psa_algorithm_t alg = alg_arg;
7319 psa_status_t actual_status;
7320 psa_status_t expected_start_status = expected_start_status_arg;
7321 psa_status_t expected_complete_status = expected_complete_status_arg;
7322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007323 uint32_t num_ops = 0;
7324 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007325 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007326 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007327 size_t min_completes = 0;
7328 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007329 psa_verify_hash_interruptible_operation_t operation =
7330 psa_verify_hash_interruptible_operation_init();
7331
7332 PSA_ASSERT(psa_crypto_init());
7333
7334 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7335 psa_set_key_algorithm(&attributes, alg);
7336 psa_set_key_type(&attributes, key_type);
7337
7338 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7339 &key));
7340
Paul Elliott0c683352022-12-16 19:16:56 +00007341 psa_interruptible_set_max_ops(max_ops);
7342
Paul Elliott6f600372023-02-06 18:41:05 +00007343 interruptible_signverify_get_minmax_completes(max_ops,
7344 expected_complete_status,
7345 &min_completes,
7346 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007347
Paul Elliott91007972022-12-16 12:21:24 +00007348 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7349 TEST_ASSERT(num_ops_prior == 0);
7350
7351 /* Start verification. */
7352 actual_status = psa_verify_hash_start(&operation, key, alg,
7353 hash_data->x, hash_data->len,
7354 signature_data->x,
7355 signature_data->len);
7356
7357 TEST_EQUAL(actual_status, expected_start_status);
7358
Paul Elliottc9774412023-02-06 15:14:07 +00007359 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007360 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007361 * start failed. */
7362 actual_status = psa_verify_hash_complete(&operation);
7363
7364 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7365
7366 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007367 actual_status = psa_verify_hash_start(&operation, key, alg,
7368 hash_data->x, hash_data->len,
7369 signature_data->x,
7370 signature_data->len);
7371
7372 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7373 }
7374
Paul Elliott91007972022-12-16 12:21:24 +00007375 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7376 TEST_ASSERT(num_ops_prior == 0);
7377
Paul Elliott91007972022-12-16 12:21:24 +00007378 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007379 do {
Paul Elliott91007972022-12-16 12:21:24 +00007380 actual_status = psa_verify_hash_complete(&operation);
7381
Paul Elliott0c683352022-12-16 19:16:56 +00007382 num_completes++;
7383
Paul Elliott334d7262023-01-20 17:29:41 +00007384 if (actual_status == PSA_SUCCESS ||
7385 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007386 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007387 /* We are asserting here that every complete makes progress
7388 * (completes some ops), which is true of the internal
7389 * implementation and probably any implementation, however this is
7390 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007391 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007392
Paul Elliott91007972022-12-16 12:21:24 +00007393 num_ops_prior = num_ops;
7394 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007395 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007396
Paul Elliottc9774412023-02-06 15:14:07 +00007397 TEST_EQUAL(actual_status, expected_complete_status);
7398
Paul Elliottefebad02023-02-15 16:56:45 +00007399 /* Check that another complete returns BAD_STATE. */
7400 actual_status = psa_verify_hash_complete(&operation);
7401 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007402
Paul Elliott0c683352022-12-16 19:16:56 +00007403 TEST_LE_U(min_completes, num_completes);
7404 TEST_LE_U(num_completes, max_completes);
7405
Paul Elliott91007972022-12-16 12:21:24 +00007406 PSA_ASSERT(psa_verify_hash_abort(&operation));
7407
Paul Elliott59ad9452022-12-18 15:09:02 +00007408 num_ops = psa_verify_hash_get_num_ops(&operation);
7409 TEST_ASSERT(num_ops == 0);
7410
Paul Elliott91007972022-12-16 12:21:24 +00007411exit:
7412 psa_reset_key_attributes(&attributes);
7413 psa_destroy_key(key);
7414 PSA_DONE();
7415}
7416/* END_CASE */
7417
Paul Elliott20a36062022-12-18 13:21:25 +00007418/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007419/**
7420 * interruptible_signverify_hash_state_test() test intentions:
7421 *
7422 * Note: This test can currently only handle ECDSA.
7423 *
7424 * 1. Test that calling the various interruptible sign and verify hash functions
7425 * in incorrect orders returns BAD_STATE errors.
7426 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007427void interruptible_signverify_hash_state_test(int key_type_arg,
7428 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007429{
7430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7431 psa_key_type_t key_type = key_type_arg;
7432 psa_algorithm_t alg = alg_arg;
7433 size_t key_bits;
7434 unsigned char *signature = NULL;
7435 size_t signature_size;
7436 size_t signature_length = 0xdeadbeef;
7437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7438 psa_sign_hash_interruptible_operation_t sign_operation =
7439 psa_sign_hash_interruptible_operation_init();
7440 psa_verify_hash_interruptible_operation_t verify_operation =
7441 psa_verify_hash_interruptible_operation_init();
7442
7443 PSA_ASSERT(psa_crypto_init());
7444
7445 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7446 PSA_KEY_USAGE_VERIFY_HASH);
7447 psa_set_key_algorithm(&attributes, alg);
7448 psa_set_key_type(&attributes, key_type);
7449
7450 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7451 &key));
7452 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7453 key_bits = psa_get_key_bits(&attributes);
7454
7455 /* Allocate a buffer which has the size advertised by the
7456 * library. */
7457 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7458 key_bits, alg);
7459 TEST_ASSERT(signature_size != 0);
7460 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007461 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007462
7463 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7464
7465 /* --- Attempt completes prior to starts --- */
7466 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7467 signature_size,
7468 &signature_length),
7469 PSA_ERROR_BAD_STATE);
7470
7471 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7472
7473 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7474 PSA_ERROR_BAD_STATE);
7475
7476 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7477
7478 /* --- Aborts in all other places. --- */
7479 psa_sign_hash_abort(&sign_operation);
7480
7481 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7482 input_data->x, input_data->len));
7483
7484 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7485
7486 psa_interruptible_set_max_ops(1);
7487
7488 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7489 input_data->x, input_data->len));
7490
7491 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7492 signature_size,
7493 &signature_length),
7494 PSA_OPERATION_INCOMPLETE);
7495
7496 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7497
7498 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7499
7500 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7501 input_data->x, input_data->len));
7502
7503 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7504 signature_size,
7505 &signature_length));
7506
7507 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7508
7509 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7510
7511 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7512 input_data->x, input_data->len,
7513 signature, signature_length));
7514
7515 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7516
7517 psa_interruptible_set_max_ops(1);
7518
7519 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7520 input_data->x, input_data->len,
7521 signature, signature_length));
7522
7523 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7524 PSA_OPERATION_INCOMPLETE);
7525
7526 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7527
7528 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7529
7530 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7531 input_data->x, input_data->len,
7532 signature, signature_length));
7533
7534 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7535
7536 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7537
7538 /* --- Attempt double starts. --- */
7539
7540 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7541 input_data->x, input_data->len));
7542
7543 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7544 input_data->x, input_data->len),
7545 PSA_ERROR_BAD_STATE);
7546
7547 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7548
7549 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7550 input_data->x, input_data->len,
7551 signature, signature_length));
7552
7553 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7554 input_data->x, input_data->len,
7555 signature, signature_length),
7556 PSA_ERROR_BAD_STATE);
7557
7558 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7559
Paul Elliott76d671a2023-02-07 17:45:18 +00007560exit:
7561 /*
7562 * Key attributes may have been returned by psa_get_key_attributes()
7563 * thus reset them as required.
7564 */
7565 psa_reset_key_attributes(&attributes);
7566
7567 psa_destroy_key(key);
7568 mbedtls_free(signature);
7569 PSA_DONE();
7570}
7571/* END_CASE */
7572
7573/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007574/**
Paul Elliottc2033502023-02-26 17:09:14 +00007575 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007576 *
7577 * Note: This test can currently only handle ECDSA.
7578 *
7579 * 1. Test various edge cases in the interruptible sign and verify hash
7580 * interfaces.
7581 */
Paul Elliottc2033502023-02-26 17:09:14 +00007582void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007583 data_t *key_data, int alg_arg, data_t *input_data)
7584{
7585 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7586 psa_key_type_t key_type = key_type_arg;
7587 psa_algorithm_t alg = alg_arg;
7588 size_t key_bits;
7589 unsigned char *signature = NULL;
7590 size_t signature_size;
7591 size_t signature_length = 0xdeadbeef;
7592 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7593 uint8_t *input_buffer = NULL;
7594 psa_sign_hash_interruptible_operation_t sign_operation =
7595 psa_sign_hash_interruptible_operation_init();
7596 psa_verify_hash_interruptible_operation_t verify_operation =
7597 psa_verify_hash_interruptible_operation_init();
7598
7599 PSA_ASSERT(psa_crypto_init());
7600
7601 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7602 PSA_KEY_USAGE_VERIFY_HASH);
7603 psa_set_key_algorithm(&attributes, alg);
7604 psa_set_key_type(&attributes, key_type);
7605
7606 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7607 &key));
7608 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7609 key_bits = psa_get_key_bits(&attributes);
7610
7611 /* Allocate a buffer which has the size advertised by the
7612 * library. */
7613 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7614 key_bits, alg);
7615 TEST_ASSERT(signature_size != 0);
7616 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007617 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007618
Paul Elliott20a36062022-12-18 13:21:25 +00007619 /* --- Change function inputs mid run, to cause an error (sign only,
7620 * verify passes all inputs to start. --- */
7621
7622 psa_interruptible_set_max_ops(1);
7623
7624 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7625 input_data->x, input_data->len));
7626
7627 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7628 signature_size,
7629 &signature_length),
7630 PSA_OPERATION_INCOMPLETE);
7631
7632 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7633 0,
7634 &signature_length),
7635 PSA_ERROR_BUFFER_TOO_SMALL);
7636
Paul Elliottc9774412023-02-06 15:14:07 +00007637 /* And test that this invalidates the operation. */
7638 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7639 0,
7640 &signature_length),
7641 PSA_ERROR_BAD_STATE);
7642
Paul Elliott20a36062022-12-18 13:21:25 +00007643 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7644
Paul Elliottf9c91a72023-02-05 18:06:38 +00007645 /* Trash the hash buffer in between start and complete, to ensure
7646 * no reliance on external buffers. */
7647 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7648
Paul Elliott6c68df42023-10-23 15:33:37 +01007649 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007650
7651 memcpy(input_buffer, input_data->x, input_data->len);
7652
7653 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7654 input_buffer, input_data->len));
7655
7656 memset(input_buffer, '!', input_data->len);
7657 mbedtls_free(input_buffer);
7658 input_buffer = NULL;
7659
7660 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7661 signature_size,
7662 &signature_length));
7663
7664 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7665
Paul Elliott6c68df42023-10-23 15:33:37 +01007666 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007667
7668 memcpy(input_buffer, input_data->x, input_data->len);
7669
7670 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7671 input_buffer, input_data->len,
7672 signature, signature_length));
7673
7674 memset(input_buffer, '!', input_data->len);
7675 mbedtls_free(input_buffer);
7676 input_buffer = NULL;
7677
7678 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7679
7680 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7681
Paul Elliott20a36062022-12-18 13:21:25 +00007682exit:
7683 /*
7684 * Key attributes may have been returned by psa_get_key_attributes()
7685 * thus reset them as required.
7686 */
7687 psa_reset_key_attributes(&attributes);
7688
7689 psa_destroy_key(key);
7690 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007691 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007692 PSA_DONE();
7693}
7694/* END_CASE */
7695
Paul Elliotta4cb9092023-02-07 18:01:55 +00007696/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007697/**
Paul Elliott57702242023-02-26 20:36:10 +00007698 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007699 *
7700 * Note: This test can currently only handle ECDSA.
7701 *
7702 * 1. Test that setting max ops is reflected in both interruptible sign and
7703 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007704 * 2. Test that changing the value of max_ops to unlimited during an operation
7705 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007706 *
7707 * 3. Test that calling get_num_ops() between complete calls gives the same
7708 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007709 */
Paul Elliott57702242023-02-26 20:36:10 +00007710void interruptible_signverify_hash_ops_tests(int key_type_arg,
7711 data_t *key_data, int alg_arg,
7712 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007713{
7714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7715 psa_key_type_t key_type = key_type_arg;
7716 psa_algorithm_t alg = alg_arg;
7717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007718 size_t key_bits;
7719 unsigned char *signature = NULL;
7720 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007721 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007722 uint32_t num_ops = 0;
7723 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7724
Paul Elliotta4cb9092023-02-07 18:01:55 +00007725 psa_sign_hash_interruptible_operation_t sign_operation =
7726 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007727 psa_verify_hash_interruptible_operation_t verify_operation =
7728 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007729
7730 PSA_ASSERT(psa_crypto_init());
7731
7732 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7733 PSA_KEY_USAGE_VERIFY_HASH);
7734 psa_set_key_algorithm(&attributes, alg);
7735 psa_set_key_type(&attributes, key_type);
7736
Paul Elliottf1743e22023-02-15 18:44:16 +00007737 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7738 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7739 key_bits = psa_get_key_bits(&attributes);
7740
7741 /* Allocate a buffer which has the size advertised by the
7742 * library. */
7743 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7744
7745 TEST_ASSERT(signature_size != 0);
7746 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007747 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007748
7749 /* Check that default max ops gets set if we don't set it. */
7750 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7751 input_data->x, input_data->len));
7752
7753 TEST_EQUAL(psa_interruptible_get_max_ops(),
7754 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7755
7756 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7757
Paul Elliottf1743e22023-02-15 18:44:16 +00007758 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7759 input_data->x, input_data->len,
7760 signature, signature_size));
7761
7762 TEST_EQUAL(psa_interruptible_get_max_ops(),
7763 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7764
7765 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7766
Paul Elliotta4cb9092023-02-07 18:01:55 +00007767 /* Check that max ops gets set properly. */
7768
7769 psa_interruptible_set_max_ops(0xbeef);
7770
Paul Elliottf1743e22023-02-15 18:44:16 +00007771 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007772
Paul Elliott9e8819f2023-02-26 19:01:35 +00007773 /* --- Ensure changing the max ops mid operation works (operation should
7774 * complete successfully after setting max ops to unlimited --- */
7775 psa_interruptible_set_max_ops(1);
7776
7777 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7778 input_data->x, input_data->len));
7779
7780 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7781 signature_size,
7782 &signature_length),
7783 PSA_OPERATION_INCOMPLETE);
7784
7785 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7786
7787 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7788 signature_size,
7789 &signature_length));
7790
7791 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7792
7793 psa_interruptible_set_max_ops(1);
7794
7795 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7796 input_data->x, input_data->len,
7797 signature, signature_length));
7798
7799 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7800 PSA_OPERATION_INCOMPLETE);
7801
7802 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7803
7804 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7805
7806 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7807
Paul Elliottc1e04002023-02-26 20:27:23 +00007808 /* --- Test that not calling get_num_ops inbetween complete calls does not
7809 * result in lost ops. ---*/
7810
7811 psa_interruptible_set_max_ops(1);
7812
7813 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7814 input_data->x, input_data->len));
7815
7816 /* Continue performing the signature until complete. */
7817 do {
7818 status = psa_sign_hash_complete(&sign_operation, signature,
7819 signature_size,
7820 &signature_length);
7821
7822 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7823
7824 } while (status == PSA_OPERATION_INCOMPLETE);
7825
7826 PSA_ASSERT(status);
7827
7828 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7829
7830 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7831 input_data->x, input_data->len));
7832
7833 /* Continue performing the signature until complete. */
7834 do {
7835 status = psa_sign_hash_complete(&sign_operation, signature,
7836 signature_size,
7837 &signature_length);
7838 } while (status == PSA_OPERATION_INCOMPLETE);
7839
7840 PSA_ASSERT(status);
7841
7842 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7843
7844 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7845
7846 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7847 input_data->x, input_data->len,
7848 signature, signature_length));
7849
7850 /* Continue performing the verification until complete. */
7851 do {
7852 status = psa_verify_hash_complete(&verify_operation);
7853
7854 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7855
7856 } while (status == PSA_OPERATION_INCOMPLETE);
7857
7858 PSA_ASSERT(status);
7859
7860 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7861
7862 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7863 input_data->x, input_data->len,
7864 signature, signature_length));
7865
7866 /* Continue performing the verification until complete. */
7867 do {
7868 status = psa_verify_hash_complete(&verify_operation);
7869
7870 } while (status == PSA_OPERATION_INCOMPLETE);
7871
7872 PSA_ASSERT(status);
7873
7874 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7875
7876 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7877
Paul Elliotta4cb9092023-02-07 18:01:55 +00007878exit:
7879 /*
7880 * Key attributes may have been returned by psa_get_key_attributes()
7881 * thus reset them as required.
7882 */
7883 psa_reset_key_attributes(&attributes);
7884
7885 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007886 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007887 PSA_DONE();
7888}
7889/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007890
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007891/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007892void sign_message_deterministic(int key_type_arg,
7893 data_t *key_data,
7894 int alg_arg,
7895 data_t *input_data,
7896 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007897{
7898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7899 psa_key_type_t key_type = key_type_arg;
7900 psa_algorithm_t alg = alg_arg;
7901 size_t key_bits;
7902 unsigned char *signature = NULL;
7903 size_t signature_size;
7904 size_t signature_length = 0xdeadbeef;
7905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7906
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007908
Gilles Peskine449bd832023-01-11 14:50:10 +01007909 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7910 psa_set_key_algorithm(&attributes, alg);
7911 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007912
Gilles Peskine449bd832023-01-11 14:50:10 +01007913 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7914 &key));
7915 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7916 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007917
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7919 TEST_ASSERT(signature_size != 0);
7920 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007921 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007922
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 PSA_ASSERT(psa_sign_message(key, alg,
7924 input_data->x, input_data->len,
7925 signature, signature_size,
7926 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007927
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007928 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007929 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007930
7931exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007932 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007933
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 psa_destroy_key(key);
7935 mbedtls_free(signature);
7936 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007937
7938}
7939/* END_CASE */
7940
7941/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007942void sign_message_fail(int key_type_arg,
7943 data_t *key_data,
7944 int alg_arg,
7945 data_t *input_data,
7946 int signature_size_arg,
7947 int expected_status_arg)
7948{
7949 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7950 psa_key_type_t key_type = key_type_arg;
7951 psa_algorithm_t alg = alg_arg;
7952 size_t signature_size = signature_size_arg;
7953 psa_status_t actual_status;
7954 psa_status_t expected_status = expected_status_arg;
7955 unsigned char *signature = NULL;
7956 size_t signature_length = 0xdeadbeef;
7957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7958
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007959 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007960
7961 PSA_ASSERT(psa_crypto_init());
7962
7963 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7964 psa_set_key_algorithm(&attributes, alg);
7965 psa_set_key_type(&attributes, key_type);
7966
7967 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7968 &key));
7969
7970 actual_status = psa_sign_message(key, alg,
7971 input_data->x, input_data->len,
7972 signature, signature_size,
7973 &signature_length);
7974 TEST_EQUAL(actual_status, expected_status);
7975 /* The value of *signature_length is unspecified on error, but
7976 * whatever it is, it should be less than signature_size, so that
7977 * if the caller tries to read *signature_length bytes without
7978 * checking the error code then they don't overflow a buffer. */
7979 TEST_LE_U(signature_length, signature_size);
7980
7981exit:
7982 psa_reset_key_attributes(&attributes);
7983 psa_destroy_key(key);
7984 mbedtls_free(signature);
7985 PSA_DONE();
7986}
7987/* END_CASE */
7988
7989/* BEGIN_CASE */
7990void sign_verify_message(int key_type_arg,
7991 data_t *key_data,
7992 int alg_arg,
7993 data_t *input_data)
7994{
7995 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7996 psa_key_type_t key_type = key_type_arg;
7997 psa_algorithm_t alg = alg_arg;
7998 size_t key_bits;
7999 unsigned char *signature = NULL;
8000 size_t signature_size;
8001 size_t signature_length = 0xdeadbeef;
8002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8003
8004 PSA_ASSERT(psa_crypto_init());
8005
8006 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8007 PSA_KEY_USAGE_VERIFY_MESSAGE);
8008 psa_set_key_algorithm(&attributes, alg);
8009 psa_set_key_type(&attributes, key_type);
8010
8011 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8012 &key));
8013 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8014 key_bits = psa_get_key_bits(&attributes);
8015
8016 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8017 TEST_ASSERT(signature_size != 0);
8018 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008019 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008020
8021 PSA_ASSERT(psa_sign_message(key, alg,
8022 input_data->x, input_data->len,
8023 signature, signature_size,
8024 &signature_length));
8025 TEST_LE_U(signature_length, signature_size);
8026 TEST_ASSERT(signature_length > 0);
8027
8028 PSA_ASSERT(psa_verify_message(key, alg,
8029 input_data->x, input_data->len,
8030 signature, signature_length));
8031
8032 if (input_data->len != 0) {
8033 /* Flip a bit in the input and verify that the signature is now
8034 * detected as invalid. Flip a bit at the beginning, not at the end,
8035 * because ECDSA may ignore the last few bits of the input. */
8036 input_data->x[0] ^= 1;
8037 TEST_EQUAL(psa_verify_message(key, alg,
8038 input_data->x, input_data->len,
8039 signature, signature_length),
8040 PSA_ERROR_INVALID_SIGNATURE);
8041 }
8042
8043exit:
8044 psa_reset_key_attributes(&attributes);
8045
8046 psa_destroy_key(key);
8047 mbedtls_free(signature);
8048 PSA_DONE();
8049}
8050/* END_CASE */
8051
8052/* BEGIN_CASE */
8053void verify_message(int key_type_arg,
8054 data_t *key_data,
8055 int alg_arg,
8056 data_t *input_data,
8057 data_t *signature_data)
8058{
8059 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8060 psa_key_type_t key_type = key_type_arg;
8061 psa_algorithm_t alg = alg_arg;
8062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8063
8064 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8065
8066 PSA_ASSERT(psa_crypto_init());
8067
8068 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8069 psa_set_key_algorithm(&attributes, alg);
8070 psa_set_key_type(&attributes, key_type);
8071
8072 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8073 &key));
8074
8075 PSA_ASSERT(psa_verify_message(key, alg,
8076 input_data->x, input_data->len,
8077 signature_data->x, signature_data->len));
8078
8079exit:
8080 psa_reset_key_attributes(&attributes);
8081 psa_destroy_key(key);
8082 PSA_DONE();
8083}
8084/* END_CASE */
8085
8086/* BEGIN_CASE */
8087void verify_message_fail(int key_type_arg,
8088 data_t *key_data,
8089 int alg_arg,
8090 data_t *hash_data,
8091 data_t *signature_data,
8092 int expected_status_arg)
8093{
8094 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8095 psa_key_type_t key_type = key_type_arg;
8096 psa_algorithm_t alg = alg_arg;
8097 psa_status_t actual_status;
8098 psa_status_t expected_status = expected_status_arg;
8099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8100
8101 PSA_ASSERT(psa_crypto_init());
8102
8103 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8104 psa_set_key_algorithm(&attributes, alg);
8105 psa_set_key_type(&attributes, key_type);
8106
8107 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8108 &key));
8109
8110 actual_status = psa_verify_message(key, alg,
8111 hash_data->x, hash_data->len,
8112 signature_data->x,
8113 signature_data->len);
8114 TEST_EQUAL(actual_status, expected_status);
8115
8116exit:
8117 psa_reset_key_attributes(&attributes);
8118 psa_destroy_key(key);
8119 PSA_DONE();
8120}
8121/* END_CASE */
8122
8123/* BEGIN_CASE */
8124void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008125 data_t *key_data,
8126 int alg_arg,
8127 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008128 data_t *label,
8129 int expected_output_length_arg,
8130 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008131{
Ronald Cron5425a212020-08-04 14:58:35 +02008132 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008133 psa_key_type_t key_type = key_type_arg;
8134 psa_algorithm_t alg = alg_arg;
8135 size_t expected_output_length = expected_output_length_arg;
8136 size_t key_bits;
8137 unsigned char *output = NULL;
8138 size_t output_size;
8139 size_t output_length = ~0;
8140 psa_status_t actual_status;
8141 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008143
Gilles Peskine449bd832023-01-11 14:50:10 +01008144 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008145
Gilles Peskine656896e2018-06-29 19:12:28 +02008146 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008147 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8148 psa_set_key_algorithm(&attributes, alg);
8149 psa_set_key_type(&attributes, key_type);
8150 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8151 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008152
8153 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008154 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8155 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008156
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8158 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008159 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008160
8161 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008162 actual_status = psa_asymmetric_encrypt(key, alg,
8163 input_data->x, input_data->len,
8164 label->x, label->len,
8165 output, output_size,
8166 &output_length);
8167 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008168 if (actual_status == PSA_SUCCESS) {
8169 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008170 } else {
8171 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008172 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008173
Gilles Peskine68428122018-06-30 18:42:41 +02008174 /* If the label is empty, the test framework puts a non-null pointer
8175 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008176 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008177 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 if (output_size != 0) {
8179 memset(output, 0, output_size);
8180 }
8181 actual_status = psa_asymmetric_encrypt(key, alg,
8182 input_data->x, input_data->len,
8183 NULL, label->len,
8184 output, output_size,
8185 &output_length);
8186 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008187 if (actual_status == PSA_SUCCESS) {
8188 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008189 } else {
8190 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008191 }
Gilles Peskine68428122018-06-30 18:42:41 +02008192 }
8193
Gilles Peskine656896e2018-06-29 19:12:28 +02008194exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008195 /*
8196 * Key attributes may have been returned by psa_get_key_attributes()
8197 * thus reset them as required.
8198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008199 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008200
Gilles Peskine449bd832023-01-11 14:50:10 +01008201 psa_destroy_key(key);
8202 mbedtls_free(output);
8203 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008204}
8205/* END_CASE */
8206
8207/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008208void asymmetric_encrypt_decrypt(int key_type_arg,
8209 data_t *key_data,
8210 int alg_arg,
8211 data_t *input_data,
8212 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008213{
Ronald Cron5425a212020-08-04 14:58:35 +02008214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008215 psa_key_type_t key_type = key_type_arg;
8216 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008217 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008218 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008219 size_t output_size;
8220 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008221 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008222 size_t output2_size;
8223 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008224 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008225
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008227
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8229 psa_set_key_algorithm(&attributes, alg);
8230 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008231
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8233 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008234
8235 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008236 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8237 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008238
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8240 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008241 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008242
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008243 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 TEST_LE_U(output2_size,
8245 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8246 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008247 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008248
Gilles Peskineeebd7382018-06-08 18:11:54 +02008249 /* We test encryption by checking that encrypt-then-decrypt gives back
8250 * the original plaintext because of the non-optional random
8251 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008252 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8253 input_data->x, input_data->len,
8254 label->x, label->len,
8255 output, output_size,
8256 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008257 /* We don't know what ciphertext length to expect, but check that
8258 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008259 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008260
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8262 output, output_length,
8263 label->x, label->len,
8264 output2, output2_size,
8265 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008266 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008267 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008268
8269exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008270 /*
8271 * Key attributes may have been returned by psa_get_key_attributes()
8272 * thus reset them as required.
8273 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008274 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008275
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 psa_destroy_key(key);
8277 mbedtls_free(output);
8278 mbedtls_free(output2);
8279 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008280}
8281/* END_CASE */
8282
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008283/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008284void asymmetric_decrypt(int key_type_arg,
8285 data_t *key_data,
8286 int alg_arg,
8287 data_t *input_data,
8288 data_t *label,
8289 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008290{
Ronald Cron5425a212020-08-04 14:58:35 +02008291 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008292 psa_key_type_t key_type = key_type_arg;
8293 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008294 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008295 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008296 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008297 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008298 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008299
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008301
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8303 psa_set_key_algorithm(&attributes, alg);
8304 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008305
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8307 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008308
Gilles Peskine449bd832023-01-11 14:50:10 +01008309 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8310 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008311
8312 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008313 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8314 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008315 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008316
Gilles Peskine449bd832023-01-11 14:50:10 +01008317 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8318 input_data->x, input_data->len,
8319 label->x, label->len,
8320 output,
8321 output_size,
8322 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008323 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008324 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008325
Gilles Peskine68428122018-06-30 18:42:41 +02008326 /* If the label is empty, the test framework puts a non-null pointer
8327 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008329 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008330 if (output_size != 0) {
8331 memset(output, 0, output_size);
8332 }
8333 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8334 input_data->x, input_data->len,
8335 NULL, label->len,
8336 output,
8337 output_size,
8338 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008339 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008340 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008341 }
8342
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008343exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 psa_reset_key_attributes(&attributes);
8345 psa_destroy_key(key);
8346 mbedtls_free(output);
8347 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008348}
8349/* END_CASE */
8350
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008351/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008352void asymmetric_decrypt_fail(int key_type_arg,
8353 data_t *key_data,
8354 int alg_arg,
8355 data_t *input_data,
8356 data_t *label,
8357 int output_size_arg,
8358 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008359{
Ronald Cron5425a212020-08-04 14:58:35 +02008360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008361 psa_key_type_t key_type = key_type_arg;
8362 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008363 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008364 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008365 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008366 psa_status_t actual_status;
8367 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008369
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008370 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008371
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008373
Gilles Peskine449bd832023-01-11 14:50:10 +01008374 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8375 psa_set_key_algorithm(&attributes, alg);
8376 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008377
Gilles Peskine449bd832023-01-11 14:50:10 +01008378 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8379 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008380
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 actual_status = psa_asymmetric_decrypt(key, alg,
8382 input_data->x, input_data->len,
8383 label->x, label->len,
8384 output, output_size,
8385 &output_length);
8386 TEST_EQUAL(actual_status, expected_status);
8387 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008388
Gilles Peskine68428122018-06-30 18:42:41 +02008389 /* If the label is empty, the test framework puts a non-null pointer
8390 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008392 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 if (output_size != 0) {
8394 memset(output, 0, output_size);
8395 }
8396 actual_status = psa_asymmetric_decrypt(key, alg,
8397 input_data->x, input_data->len,
8398 NULL, label->len,
8399 output, output_size,
8400 &output_length);
8401 TEST_EQUAL(actual_status, expected_status);
8402 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008403 }
8404
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008405exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008406 psa_reset_key_attributes(&attributes);
8407 psa_destroy_key(key);
8408 mbedtls_free(output);
8409 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008410}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008411/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008412
8413/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008414void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008415{
8416 /* Test each valid way of initializing the object, except for `= {0}`, as
8417 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8418 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008419 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008420 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008422 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8423 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008424
Gilles Peskine449bd832023-01-11 14:50:10 +01008425 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008426
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008427 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008428 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8429 PSA_ERROR_BAD_STATE);
8430 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8431 PSA_ERROR_BAD_STATE);
8432 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8433 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008434
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008435 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 PSA_ASSERT(psa_key_derivation_abort(&func));
8437 PSA_ASSERT(psa_key_derivation_abort(&init));
8438 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008439}
8440/* END_CASE */
8441
Janos Follath16de4a42019-06-13 16:32:24 +01008442/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008443void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008444{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008445 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008446 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008447 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008448
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008450
Gilles Peskine449bd832023-01-11 14:50:10 +01008451 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8452 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008453
8454exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 psa_key_derivation_abort(&operation);
8456 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008457}
8458/* END_CASE */
8459
Janos Follathaf3c2a02019-06-12 12:34:34 +01008460/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008461void derive_set_capacity(int alg_arg, int capacity_arg,
8462 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008463{
8464 psa_algorithm_t alg = alg_arg;
8465 size_t capacity = capacity_arg;
8466 psa_status_t expected_status = expected_status_arg;
8467 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8468
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008470
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008472
Gilles Peskine449bd832023-01-11 14:50:10 +01008473 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8474 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008475
8476exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008477 psa_key_derivation_abort(&operation);
8478 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008479}
8480/* END_CASE */
8481
8482/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308483void parse_binary_string_test(data_t *input, int output)
8484{
8485 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308486 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308487 TEST_EQUAL(value, output);
8488}
8489/* END_CASE */
8490
8491/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008492void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308493 int step_arg1, int key_type_arg1, data_t *input1,
8494 int expected_status_arg1,
8495 int step_arg2, int key_type_arg2, data_t *input2,
8496 int expected_status_arg2,
8497 int step_arg3, int key_type_arg3, data_t *input3,
8498 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008500{
8501 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308503 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 psa_status_t expected_statuses[] = { expected_status_arg1,
8505 expected_status_arg2,
8506 expected_status_arg3 };
8507 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008508 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8509 MBEDTLS_SVC_KEY_ID_INIT,
8510 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008511 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8513 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008514 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008515 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008516 psa_status_t expected_output_status = expected_output_status_arg;
8517 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008518
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008520
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8522 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008523
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008525
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8527 mbedtls_test_set_step(i);
8528 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008529 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308530 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8531 key_types[i] != INPUT_INTEGER) {
8532 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008533 PSA_ASSERT(psa_import_key(&attributes,
8534 inputs[i]->x, inputs[i]->len,
8535 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308536 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008538 // When taking a private key as secret input, use key agreement
8539 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8541 &operation, keys[i]),
8542 expected_statuses[i]);
8543 } else {
8544 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8545 keys[i]),
8546 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008547 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308549 if (key_types[i] == INPUT_INTEGER) {
8550 TEST_EQUAL(psa_key_derivation_input_integer(
8551 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308552 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308553 expected_statuses[i]);
8554 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308555 TEST_EQUAL(psa_key_derivation_input_bytes(
8556 &operation, steps[i],
8557 inputs[i]->x, inputs[i]->len),
8558 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308559 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008560 }
8561 }
8562
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 if (output_key_type != PSA_KEY_TYPE_NONE) {
8564 psa_reset_key_attributes(&attributes);
8565 psa_set_key_type(&attributes, output_key_type);
8566 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008567 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 psa_key_derivation_output_key(&attributes, &operation,
8569 &output_key);
8570 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008571 uint8_t buffer[1];
8572 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 psa_key_derivation_output_bytes(&operation,
8574 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008575 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008576 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008577
Janos Follathaf3c2a02019-06-12 12:34:34 +01008578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 psa_key_derivation_abort(&operation);
8580 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8581 psa_destroy_key(keys[i]);
8582 }
8583 psa_destroy_key(output_key);
8584 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008585}
8586/* END_CASE */
8587
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308588/* BEGIN_CASE*/
8589void derive_input_invalid_cost(int alg_arg, int64_t cost)
8590{
8591 psa_algorithm_t alg = alg_arg;
8592 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8593
8594 PSA_ASSERT(psa_crypto_init());
8595 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8596
8597 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8598 PSA_KEY_DERIVATION_INPUT_COST,
8599 cost),
8600 PSA_ERROR_NOT_SUPPORTED);
8601
8602exit:
8603 psa_key_derivation_abort(&operation);
8604 PSA_DONE();
8605}
8606/* END_CASE*/
8607
Janos Follathd958bb72019-07-03 15:02:16 +01008608/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008609void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008610{
Janos Follathd958bb72019-07-03 15:02:16 +01008611 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008613 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008614 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008615 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008617 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008619 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008621 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8622 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008625
Gilles Peskine449bd832023-01-11 14:50:10 +01008626 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008627
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8629 psa_set_key_algorithm(&attributes, alg);
8630 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008631
Gilles Peskine449bd832023-01-11 14:50:10 +01008632 PSA_ASSERT(psa_import_key(&attributes,
8633 key_data, sizeof(key_data),
8634 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008635
8636 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8638 input1, input1_length,
8639 input2, input2_length,
8640 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008641 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008643
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008644 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8646 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008647
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008649
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8651 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008652
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008653exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008654 psa_key_derivation_abort(&operation);
8655 psa_destroy_key(key);
8656 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008657}
8658/* END_CASE */
8659
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008660/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008661void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008662{
8663 uint8_t output_buffer[16];
8664 size_t buffer_size = 16;
8665 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008666 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008667
Gilles Peskine449bd832023-01-11 14:50:10 +01008668 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8669 output_buffer, buffer_size)
8670 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008671
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8673 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008674
Gilles Peskine449bd832023-01-11 14:50:10 +01008675 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008676
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8678 output_buffer, buffer_size)
8679 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008680
Gilles Peskine449bd832023-01-11 14:50:10 +01008681 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8682 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008683
8684exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008685 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008686}
8687/* END_CASE */
8688
8689/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008690void derive_output(int alg_arg,
8691 int step1_arg, data_t *input1, int expected_status_arg1,
8692 int step2_arg, data_t *input2, int expected_status_arg2,
8693 int step3_arg, data_t *input3, int expected_status_arg3,
8694 int step4_arg, data_t *input4, int expected_status_arg4,
8695 data_t *key_agreement_peer_key,
8696 int requested_capacity_arg,
8697 data_t *expected_output1,
8698 data_t *expected_output2,
8699 int other_key_input_type,
8700 int key_input_type,
8701 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008702{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008703 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008704 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8705 data_t *inputs[] = { input1, input2, input3, input4 };
8706 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8707 MBEDTLS_SVC_KEY_ID_INIT,
8708 MBEDTLS_SVC_KEY_ID_INIT,
8709 MBEDTLS_SVC_KEY_ID_INIT };
8710 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8711 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008712 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008713 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008714 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008715 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008716 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008718 size_t output_buffer_size = 0;
8719 uint8_t *output_buffer = NULL;
8720 size_t expected_capacity;
8721 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008722 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8723 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8724 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8725 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008726 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008727 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008728 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008729
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8731 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008732 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008733 }
8734 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008735 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008737 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008738 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008739 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008740
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008741 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008742 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8743 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8744 requested_capacity));
8745 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8746 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008747 case 0:
8748 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308749 case PSA_KEY_DERIVATION_INPUT_COST:
8750 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308751 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308752 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308753 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308754 if (statuses[i] != PSA_SUCCESS) {
8755 goto exit;
8756 }
8757 break;
8758 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008759 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008761 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008762 TEST_EQUAL(psa_key_derivation_input_bytes(
8763 &operation, steps[i],
8764 inputs[i]->x, inputs[i]->len),
8765 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008766
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008768 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008770 break;
8771 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8773 psa_set_key_algorithm(&attributes1, alg);
8774 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008775
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 PSA_ASSERT(psa_import_key(&attributes1,
8777 inputs[i]->x, inputs[i]->len,
8778 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008779
Gilles Peskine449bd832023-01-11 14:50:10 +01008780 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8781 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8782 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8783 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008784 }
8785
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308786 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308788 keys[i]),
8789 statuses[i]);
8790
8791 if (statuses[i] != PSA_SUCCESS) {
8792 goto exit;
8793 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008794 break;
8795 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008796 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008797 break;
8798 }
8799 break;
8800 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008802 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008803 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8804 steps[i],
8805 inputs[i]->x,
8806 inputs[i]->len),
8807 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008808 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008809 case 1: // input key, type DERIVE
8810 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8812 psa_set_key_algorithm(&attributes2, alg);
8813 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008814
8815 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 if (other_key_input_type == 11) {
8817 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8818 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008819
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 PSA_ASSERT(psa_import_key(&attributes2,
8821 inputs[i]->x, inputs[i]->len,
8822 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008823
Gilles Peskine449bd832023-01-11 14:50:10 +01008824 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8825 steps[i],
8826 keys[i]),
8827 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008828 break;
8829 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8831 psa_set_key_algorithm(&attributes3, alg);
8832 psa_set_key_type(&attributes3,
8833 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008834
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 PSA_ASSERT(psa_import_key(&attributes3,
8836 inputs[i]->x, inputs[i]->len,
8837 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008838
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 TEST_EQUAL(psa_key_derivation_key_agreement(
8840 &operation,
8841 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8842 keys[i], key_agreement_peer_key->x,
8843 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008844 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008845 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008846 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008847 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008848 }
8849
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008851 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008853 break;
8854 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 TEST_EQUAL(psa_key_derivation_input_bytes(
8856 &operation, steps[i],
8857 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008858
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008860 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008862 break;
8863 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008864 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008865
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8867 &current_capacity));
8868 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008869 expected_capacity = requested_capacity;
8870
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008872 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8873
8874 /* For output key derivation secret must be provided using
8875 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008877 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008879
Gilles Peskine449bd832023-01-11 14:50:10 +01008880 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8881 psa_set_key_algorithm(&attributes4, alg);
8882 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8883 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008884
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8886 &derived_key), expected_status);
8887 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008888 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008889 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008890 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008891 status = psa_key_derivation_output_bytes(&operation,
8892 output_buffer, output_sizes[i]);
8893 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008894 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008895 TEST_ASSERT(status == PSA_SUCCESS ||
8896 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008897 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 } else if (expected_capacity == 0 ||
8899 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008900 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008902 expected_capacity = 0;
8903 continue;
8904 }
8905 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008906 PSA_ASSERT(status);
8907 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008908 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008909 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008911 /* Check the operation status. */
8912 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8914 &current_capacity));
8915 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008916 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008917 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008918 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008919
8920exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008921 mbedtls_free(output_buffer);
8922 psa_key_derivation_abort(&operation);
8923 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8924 psa_destroy_key(keys[i]);
8925 }
8926 psa_destroy_key(derived_key);
8927 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008928}
8929/* END_CASE */
8930
8931/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008932void derive_full(int alg_arg,
8933 data_t *key_data,
8934 data_t *input1,
8935 data_t *input2,
8936 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008937{
Ronald Cron5425a212020-08-04 14:58:35 +02008938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008939 psa_algorithm_t alg = alg_arg;
8940 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008941 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008942 unsigned char output_buffer[16];
8943 size_t expected_capacity = requested_capacity;
8944 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008946
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008948
Gilles Peskine449bd832023-01-11 14:50:10 +01008949 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8950 psa_set_key_algorithm(&attributes, alg);
8951 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008952
Gilles Peskine449bd832023-01-11 14:50:10 +01008953 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8954 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008955
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8957 input1->x, input1->len,
8958 input2->x, input2->len,
8959 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008960 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008962
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8964 &current_capacity));
8965 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008966
8967 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 while (current_capacity > 0) {
8969 size_t read_size = sizeof(output_buffer);
8970 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008971 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008972 }
8973 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8974 output_buffer,
8975 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008976 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8978 &current_capacity));
8979 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008980 }
8981
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008982 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008983 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8984 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008985
Gilles Peskine449bd832023-01-11 14:50:10 +01008986 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008987
8988exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008989 psa_key_derivation_abort(&operation);
8990 psa_destroy_key(key);
8991 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008992}
8993/* END_CASE */
8994
Stephan Koch78109f52023-04-12 14:19:36 +02008995/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008996void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8997 int derivation_step,
8998 int capacity, int expected_capacity_status_arg,
8999 data_t *expected_output,
9000 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009001{
9002 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9003 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009004 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009005 uint8_t *output_buffer = NULL;
9006 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009007 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9008 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9009 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009010
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009011 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009012 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009013
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9015 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9016 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009017
Gilles Peskine449bd832023-01-11 14:50:10 +01009018 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9019 step, input->x, input->len),
9020 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009021
Gilles Peskine449bd832023-01-11 14:50:10 +01009022 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009023 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009025
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9027 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009028
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 TEST_EQUAL(status, expected_output_status);
9030 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009031 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009032 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009033 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009034
9035exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009036 mbedtls_free(output_buffer);
9037 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009038 PSA_DONE();
9039}
9040/* END_CASE */
9041
Janos Follathe60c9052019-07-03 13:51:30 +01009042/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009043void derive_key_exercise(int alg_arg,
9044 data_t *key_data,
9045 data_t *input1,
9046 data_t *input2,
9047 int derived_type_arg,
9048 int derived_bits_arg,
9049 int derived_usage_arg,
9050 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009051{
Ronald Cron5425a212020-08-04 14:58:35 +02009052 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9053 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009054 psa_algorithm_t alg = alg_arg;
9055 psa_key_type_t derived_type = derived_type_arg;
9056 size_t derived_bits = derived_bits_arg;
9057 psa_key_usage_t derived_usage = derived_usage_arg;
9058 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009059 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009060 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009061 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009062 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009063
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009065
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9067 psa_set_key_algorithm(&attributes, alg);
9068 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9069 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9070 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009071
9072 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009073 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9074 input1->x, input1->len,
9075 input2->x, input2->len,
9076 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009078 }
Janos Follathe60c9052019-07-03 13:51:30 +01009079
Gilles Peskine449bd832023-01-11 14:50:10 +01009080 psa_set_key_usage_flags(&attributes, derived_usage);
9081 psa_set_key_algorithm(&attributes, derived_alg);
9082 psa_set_key_type(&attributes, derived_type);
9083 psa_set_key_bits(&attributes, derived_bits);
9084 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9085 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009086
9087 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9089 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9090 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009091
9092 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009093 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009094 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009096
9097exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009098 /*
9099 * Key attributes may have been returned by psa_get_key_attributes()
9100 * thus reset them as required.
9101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009102 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009103
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 psa_key_derivation_abort(&operation);
9105 psa_destroy_key(base_key);
9106 psa_destroy_key(derived_key);
9107 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009108}
9109/* END_CASE */
9110
Janos Follath42fd8882019-07-03 14:17:09 +01009111/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009112void derive_key_export(int alg_arg,
9113 data_t *key_data,
9114 data_t *input1,
9115 data_t *input2,
9116 int bytes1_arg,
9117 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009118{
Ronald Cron5425a212020-08-04 14:58:35 +02009119 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9120 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009121 psa_algorithm_t alg = alg_arg;
9122 size_t bytes1 = bytes1_arg;
9123 size_t bytes2 = bytes2_arg;
9124 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009125 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009126 uint8_t *output_buffer = NULL;
9127 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009128 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9129 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009130 size_t length;
9131
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009132 TEST_CALLOC(output_buffer, capacity);
9133 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9137 psa_set_key_algorithm(&base_attributes, alg);
9138 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9139 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9140 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009141
9142 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9144 input1->x, input1->len,
9145 input2->x, input2->len,
9146 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 }
Janos Follath42fd8882019-07-03 14:17:09 +01009149
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9151 output_buffer,
9152 capacity));
9153 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009154
9155 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009156 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9157 input1->x, input1->len,
9158 input2->x, input2->len,
9159 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009161 }
Janos Follath42fd8882019-07-03 14:17:09 +01009162
Gilles Peskine449bd832023-01-11 14:50:10 +01009163 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9164 psa_set_key_algorithm(&derived_attributes, 0);
9165 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9166 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9167 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9168 &derived_key));
9169 PSA_ASSERT(psa_export_key(derived_key,
9170 export_buffer, bytes1,
9171 &length));
9172 TEST_EQUAL(length, bytes1);
9173 PSA_ASSERT(psa_destroy_key(derived_key));
9174 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9175 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9176 &derived_key));
9177 PSA_ASSERT(psa_export_key(derived_key,
9178 export_buffer + bytes1, bytes2,
9179 &length));
9180 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009181
9182 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009183 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009184 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009185
9186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009187 mbedtls_free(output_buffer);
9188 mbedtls_free(export_buffer);
9189 psa_key_derivation_abort(&operation);
9190 psa_destroy_key(base_key);
9191 psa_destroy_key(derived_key);
9192 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009193}
9194/* END_CASE */
9195
9196/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009197void derive_key_type(int alg_arg,
9198 data_t *key_data,
9199 data_t *input1,
9200 data_t *input2,
9201 int key_type_arg, int bits_arg,
9202 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009203{
9204 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9205 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9206 const psa_algorithm_t alg = alg_arg;
9207 const psa_key_type_t key_type = key_type_arg;
9208 const size_t bits = bits_arg;
9209 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9210 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009211 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009212 uint8_t *export_buffer = NULL;
9213 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9214 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9215 size_t export_length;
9216
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009217 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009218 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009219
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9221 psa_set_key_algorithm(&base_attributes, alg);
9222 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9223 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9224 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009225
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009227 &operation, base_key, alg,
9228 input1->x, input1->len,
9229 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009231 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009232 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009233
Gilles Peskine449bd832023-01-11 14:50:10 +01009234 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9235 psa_set_key_algorithm(&derived_attributes, 0);
9236 psa_set_key_type(&derived_attributes, key_type);
9237 psa_set_key_bits(&derived_attributes, bits);
9238 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9239 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009240
Gilles Peskine449bd832023-01-11 14:50:10 +01009241 PSA_ASSERT(psa_export_key(derived_key,
9242 export_buffer, export_buffer_size,
9243 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009244 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009245 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009246
9247exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 mbedtls_free(export_buffer);
9249 psa_key_derivation_abort(&operation);
9250 psa_destroy_key(base_key);
9251 psa_destroy_key(derived_key);
9252 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009253}
9254/* END_CASE */
9255
9256/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009257void derive_key(int alg_arg,
9258 data_t *key_data, data_t *input1, data_t *input2,
9259 int type_arg, int bits_arg,
9260 int expected_status_arg,
9261 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009262{
Ronald Cron5425a212020-08-04 14:58:35 +02009263 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9264 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009265 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009266 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009267 size_t bits = bits_arg;
9268 psa_status_t expected_status = expected_status_arg;
9269 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9270 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9271 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9272
Gilles Peskine449bd832023-01-11 14:50:10 +01009273 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009274
Gilles Peskine449bd832023-01-11 14:50:10 +01009275 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9276 psa_set_key_algorithm(&base_attributes, alg);
9277 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9278 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9279 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009280
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9282 input1->x, input1->len,
9283 input2->x, input2->len,
9284 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009285 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009286 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009287
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9289 psa_set_key_algorithm(&derived_attributes, 0);
9290 psa_set_key_type(&derived_attributes, type);
9291 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009292
9293 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009294 psa_key_derivation_output_key(&derived_attributes,
9295 &operation,
9296 &derived_key);
9297 if (is_large_output > 0) {
9298 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9299 }
9300 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009301
9302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009303 psa_key_derivation_abort(&operation);
9304 psa_destroy_key(base_key);
9305 psa_destroy_key(derived_key);
9306 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009307}
9308/* END_CASE */
9309
9310/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009311void key_agreement_setup(int alg_arg,
9312 int our_key_type_arg, int our_key_alg_arg,
9313 data_t *our_key_data, data_t *peer_key_data,
9314 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009315{
Ronald Cron5425a212020-08-04 14:58:35 +02009316 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009317 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009318 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009319 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009320 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009321 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009322 psa_status_t expected_status = expected_status_arg;
9323 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009324
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009326
Gilles Peskine449bd832023-01-11 14:50:10 +01009327 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9328 psa_set_key_algorithm(&attributes, our_key_alg);
9329 psa_set_key_type(&attributes, our_key_type);
9330 PSA_ASSERT(psa_import_key(&attributes,
9331 our_key_data->x, our_key_data->len,
9332 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009333
Gilles Peskine77f40d82019-04-11 21:27:06 +02009334 /* The tests currently include inputs that should fail at either step.
9335 * Test cases that fail at the setup step should be changed to call
9336 * key_derivation_setup instead, and this function should be renamed
9337 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009338 status = psa_key_derivation_setup(&operation, alg);
9339 if (status == PSA_SUCCESS) {
9340 TEST_EQUAL(psa_key_derivation_key_agreement(
9341 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9342 our_key,
9343 peer_key_data->x, peer_key_data->len),
9344 expected_status);
9345 } else {
9346 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009347 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009348
9349exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009350 psa_key_derivation_abort(&operation);
9351 psa_destroy_key(our_key);
9352 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009353}
9354/* END_CASE */
9355
9356/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009357void raw_key_agreement(int alg_arg,
9358 int our_key_type_arg, data_t *our_key_data,
9359 data_t *peer_key_data,
9360 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009361{
Ronald Cron5425a212020-08-04 14:58:35 +02009362 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009363 psa_algorithm_t alg = alg_arg;
9364 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009366 unsigned char *output = NULL;
9367 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009368 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009369
Gilles Peskine449bd832023-01-11 14:50:10 +01009370 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009371
Gilles Peskine449bd832023-01-11 14:50:10 +01009372 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9373 psa_set_key_algorithm(&attributes, alg);
9374 psa_set_key_type(&attributes, our_key_type);
9375 PSA_ASSERT(psa_import_key(&attributes,
9376 our_key_data->x, our_key_data->len,
9377 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009378
Gilles Peskine449bd832023-01-11 14:50:10 +01009379 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9380 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009381
Gilles Peskine992bee82022-04-13 23:25:52 +02009382 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009383 TEST_LE_U(expected_output->len,
9384 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9385 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9386 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009387
9388 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009389 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009390 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9391 peer_key_data->x, peer_key_data->len,
9392 output, expected_output->len,
9393 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009394 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009395 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009396 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009397 output = NULL;
9398 output_length = ~0;
9399
9400 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009401 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009402 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9403 peer_key_data->x, peer_key_data->len,
9404 output, expected_output->len + 1,
9405 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009406 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009407 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009409 output = NULL;
9410 output_length = ~0;
9411
9412 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009413 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009414 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9415 peer_key_data->x, peer_key_data->len,
9416 output, expected_output->len - 1,
9417 &output_length),
9418 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009419 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 TEST_LE_U(output_length, expected_output->len - 1);
9421 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009422 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009423
9424exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009425 mbedtls_free(output);
9426 psa_destroy_key(our_key);
9427 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009428}
9429/* END_CASE */
9430
9431/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009432void key_agreement_capacity(int alg_arg,
9433 int our_key_type_arg, data_t *our_key_data,
9434 data_t *peer_key_data,
9435 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009436{
Ronald Cron5425a212020-08-04 14:58:35 +02009437 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009438 psa_algorithm_t alg = alg_arg;
9439 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009440 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009442 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009443 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009444
Gilles Peskine449bd832023-01-11 14:50:10 +01009445 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009446
Gilles Peskine449bd832023-01-11 14:50:10 +01009447 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9448 psa_set_key_algorithm(&attributes, alg);
9449 psa_set_key_type(&attributes, our_key_type);
9450 PSA_ASSERT(psa_import_key(&attributes,
9451 our_key_data->x, our_key_data->len,
9452 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009453
Gilles Peskine449bd832023-01-11 14:50:10 +01009454 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9455 PSA_ASSERT(psa_key_derivation_key_agreement(
9456 &operation,
9457 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9458 peer_key_data->x, peer_key_data->len));
9459 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009460 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009461 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9462 PSA_KEY_DERIVATION_INPUT_INFO,
9463 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009464 }
Gilles Peskine59685592018-09-18 12:11:34 +02009465
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009466 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009467 PSA_ASSERT(psa_key_derivation_get_capacity(
9468 &operation, &actual_capacity));
9469 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009470
Gilles Peskinebf491972018-10-25 22:36:12 +02009471 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 while (actual_capacity > sizeof(output)) {
9473 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9474 output, sizeof(output)));
9475 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009476 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9478 output, actual_capacity));
9479 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9480 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009481
Gilles Peskine59685592018-09-18 12:11:34 +02009482exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009483 psa_key_derivation_abort(&operation);
9484 psa_destroy_key(our_key);
9485 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009486}
9487/* END_CASE */
9488
Valerio Settiad819672023-12-29 12:14:41 +01009489/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9490void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009491{
9492 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009493 psa_ecc_family_t ecc_family = psa_family_arg;
9494 size_t bits = bits_arg;
9495 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009496
Valerio Settiad819672023-12-29 12:14:41 +01009497 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9498 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009499 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009500}
9501/* END_CASE */
9502
Valerio Settiac739522024-01-04 10:22:01 +01009503/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9504void ecc_conversion_functions_fail()
9505{
9506 size_t bits;
9507
Valerio Settidb6e0292024-01-05 10:15:45 +01009508 /* Invalid legacy curve identifiers. */
9509 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9510 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009511 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9512 TEST_EQUAL(0, bits);
9513
9514 /* Invalid PSA EC family. */
9515 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9516 /* Invalid bit-size for a valid EC family. */
9517 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9518
9519 /* Twisted-Edward curves are not supported yet. */
9520 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9521 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9522 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9523 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9524}
9525/* END_CASE */
9526
9527
Valerio Settibf999cb2023-12-28 17:48:13 +01009528/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009529void key_agreement_output(int alg_arg,
9530 int our_key_type_arg, data_t *our_key_data,
9531 data_t *peer_key_data,
9532 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009533{
Ronald Cron5425a212020-08-04 14:58:35 +02009534 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009535 psa_algorithm_t alg = alg_arg;
9536 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009537 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009539 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009540
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009541 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009542 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009543
Gilles Peskine449bd832023-01-11 14:50:10 +01009544 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009545
Gilles Peskine449bd832023-01-11 14:50:10 +01009546 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9547 psa_set_key_algorithm(&attributes, alg);
9548 psa_set_key_type(&attributes, our_key_type);
9549 PSA_ASSERT(psa_import_key(&attributes,
9550 our_key_data->x, our_key_data->len,
9551 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009552
Gilles Peskine449bd832023-01-11 14:50:10 +01009553 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9554 PSA_ASSERT(psa_key_derivation_key_agreement(
9555 &operation,
9556 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9557 peer_key_data->x, peer_key_data->len));
9558 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009559 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009560 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9561 PSA_KEY_DERIVATION_INPUT_INFO,
9562 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009563 }
Gilles Peskine59685592018-09-18 12:11:34 +02009564
Gilles Peskine449bd832023-01-11 14:50:10 +01009565 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9566 actual_output,
9567 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009568 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009569 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009570 if (expected_output2->len != 0) {
9571 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9572 actual_output,
9573 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009574 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009575 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009576 }
Gilles Peskine59685592018-09-18 12:11:34 +02009577
9578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 psa_key_derivation_abort(&operation);
9580 psa_destroy_key(our_key);
9581 PSA_DONE();
9582 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009583}
9584/* END_CASE */
9585
9586/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009587void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009588{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009589 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009590 unsigned char *output = NULL;
9591 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009592 size_t i;
9593 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009594
Gilles Peskine449bd832023-01-11 14:50:10 +01009595 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009596
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009597 TEST_CALLOC(output, bytes);
9598 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009599
Gilles Peskine449bd832023-01-11 14:50:10 +01009600 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009601
Gilles Peskinea50d7392018-06-21 10:22:13 +02009602 /* Run several times, to ensure that every output byte will be
9603 * nonzero at least once with overwhelming probability
9604 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009605 for (run = 0; run < 10; run++) {
9606 if (bytes != 0) {
9607 memset(output, 0, bytes);
9608 }
9609 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009610
Gilles Peskine449bd832023-01-11 14:50:10 +01009611 for (i = 0; i < bytes; i++) {
9612 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009613 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009614 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009615 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009616 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009617
9618 /* Check that every byte was changed to nonzero at least once. This
9619 * validates that psa_generate_random is overwriting every byte of
9620 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009621 for (i = 0; i < bytes; i++) {
9622 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009623 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009624
9625exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009626 PSA_DONE();
9627 mbedtls_free(output);
9628 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009629}
9630/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009631
9632/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009633void generate_key(int type_arg,
9634 int bits_arg,
9635 int usage_arg,
9636 int alg_arg,
9637 int expected_status_arg,
9638 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009639{
Ronald Cron5425a212020-08-04 14:58:35 +02009640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009641 psa_key_type_t type = type_arg;
9642 psa_key_usage_t usage = usage_arg;
9643 size_t bits = bits_arg;
9644 psa_algorithm_t alg = alg_arg;
9645 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009647 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009648
Gilles Peskine449bd832023-01-11 14:50:10 +01009649 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009650
Gilles Peskine449bd832023-01-11 14:50:10 +01009651 psa_set_key_usage_flags(&attributes, usage);
9652 psa_set_key_algorithm(&attributes, alg);
9653 psa_set_key_type(&attributes, type);
9654 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009655
9656 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009657 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009658
Gilles Peskine449bd832023-01-11 14:50:10 +01009659 if (is_large_key > 0) {
9660 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9661 }
9662 TEST_EQUAL(status, expected_status);
9663 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009664 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009665 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009666
9667 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009668 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9669 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9670 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009671
Gilles Peskine818ca122018-06-20 18:16:48 +02009672 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009673 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009674 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009675 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009676
9677exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009678 /*
9679 * Key attributes may have been returned by psa_get_key_attributes()
9680 * thus reset them as required.
9681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009682 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009683
Gilles Peskine449bd832023-01-11 14:50:10 +01009684 psa_destroy_key(key);
9685 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009686}
9687/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009688
Valerio Setti19fec542023-07-25 12:31:50 +02009689/* 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 +01009690void generate_key_rsa(int bits_arg,
9691 data_t *e_arg,
9692 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009693{
Ronald Cron5425a212020-08-04 14:58:35 +02009694 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009695 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009696 size_t bits = bits_arg;
9697 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9698 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9699 psa_status_t expected_status = expected_status_arg;
9700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9701 uint8_t *exported = NULL;
9702 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009703 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009704 size_t exported_length = SIZE_MAX;
9705 uint8_t *e_read_buffer = NULL;
9706 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009707 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009708 size_t e_read_length = SIZE_MAX;
9709
Gilles Peskine449bd832023-01-11 14:50:10 +01009710 if (e_arg->len == 0 ||
9711 (e_arg->len == 3 &&
9712 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009713 is_default_public_exponent = 1;
9714 e_read_size = 0;
9715 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009716 TEST_CALLOC(e_read_buffer, e_read_size);
9717 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009718
Gilles Peskine449bd832023-01-11 14:50:10 +01009719 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009720
Gilles Peskine449bd832023-01-11 14:50:10 +01009721 psa_set_key_usage_flags(&attributes, usage);
9722 psa_set_key_algorithm(&attributes, alg);
9723 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9724 e_arg->x, e_arg->len));
9725 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009726
9727 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009728 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9729 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009730 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009731 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009732
9733 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009734 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9735 TEST_EQUAL(psa_get_key_type(&attributes), type);
9736 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009737 psa_status_t status = psa_get_key_domain_parameters(&attributes,
9738 e_read_buffer, e_read_size,
9739 &e_read_length);
9740
9741
9742#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
9743 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
9744 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +01009745 if (is_default_public_exponent) {
9746 TEST_EQUAL(e_read_length, 0);
9747 } else {
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009748 TEST_EQUAL(status, PSA_SUCCESS);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009749 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009750 }
Pengyu Lv9e976f32023-12-06 16:58:05 +08009751#else
Pengyu Lv9e976f32023-12-06 16:58:05 +08009752 (void) is_default_public_exponent;
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009753 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
Pengyu Lv9e976f32023-12-06 16:58:05 +08009754#endif
Gilles Peskinee56e8782019-04-26 17:34:02 +02009755
9756 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009757 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009758 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009759 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009760
9761 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009762 PSA_ASSERT(psa_export_public_key(key,
9763 exported, exported_size,
9764 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009765 {
9766 uint8_t *p = exported;
9767 uint8_t *end = exported + exported_length;
9768 size_t len;
9769 /* RSAPublicKey ::= SEQUENCE {
9770 * modulus INTEGER, -- n
9771 * publicExponent INTEGER } -- e
9772 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009773 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9774 MBEDTLS_ASN1_SEQUENCE |
9775 MBEDTLS_ASN1_CONSTRUCTED));
9776 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9777 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9778 MBEDTLS_ASN1_INTEGER));
9779 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009780 ++p;
9781 --len;
9782 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009783 if (e_arg->len == 0) {
9784 TEST_EQUAL(len, 3);
9785 TEST_EQUAL(p[0], 1);
9786 TEST_EQUAL(p[1], 0);
9787 TEST_EQUAL(p[2], 1);
9788 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009789 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009790 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009791 }
9792
9793exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009794 /*
9795 * Key attributes may have been returned by psa_get_key_attributes() or
9796 * set by psa_set_key_domain_parameters() thus reset them as required.
9797 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009798 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009799
Gilles Peskine449bd832023-01-11 14:50:10 +01009800 psa_destroy_key(key);
9801 PSA_DONE();
9802 mbedtls_free(e_read_buffer);
9803 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009804}
9805/* END_CASE */
9806
Darryl Greend49a4992018-06-18 17:27:26 +01009807/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009808void persistent_key_load_key_from_storage(data_t *data,
9809 int type_arg, int bits_arg,
9810 int usage_flags_arg, int alg_arg,
9811 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009812{
Gilles Peskine449bd832023-01-11 14:50:10 +01009813 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009815 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9816 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009817 psa_key_type_t type = type_arg;
9818 size_t bits = bits_arg;
9819 psa_key_usage_t usage_flags = usage_flags_arg;
9820 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009821 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009822 unsigned char *first_export = NULL;
9823 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009824 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -07009825 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +01009826 size_t second_exported_length;
9827
Gilles Peskine449bd832023-01-11 14:50:10 +01009828 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009829 TEST_CALLOC(first_export, export_size);
9830 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009831 }
Darryl Greend49a4992018-06-18 17:27:26 +01009832
Gilles Peskine449bd832023-01-11 14:50:10 +01009833 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009834
Gilles Peskine449bd832023-01-11 14:50:10 +01009835 psa_set_key_id(&attributes, key_id);
9836 psa_set_key_usage_flags(&attributes, usage_flags);
9837 psa_set_key_algorithm(&attributes, alg);
9838 psa_set_key_type(&attributes, type);
9839 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009840
Gilles Peskine449bd832023-01-11 14:50:10 +01009841 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009842 case IMPORT_KEY:
9843 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009844 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9845 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009846 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009847
Darryl Green0c6575a2018-11-07 16:05:30 +00009848 case GENERATE_KEY:
9849 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009850 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009851 break;
9852
9853 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009854#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009855 {
9856 /* Create base key */
9857 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9858 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9859 psa_set_key_usage_flags(&base_attributes,
9860 PSA_KEY_USAGE_DERIVE);
9861 psa_set_key_algorithm(&base_attributes, derive_alg);
9862 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9863 PSA_ASSERT(psa_import_key(&base_attributes,
9864 data->x, data->len,
9865 &base_key));
9866 /* Derive a key. */
9867 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9868 PSA_ASSERT(psa_key_derivation_input_key(
9869 &operation,
9870 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9871 PSA_ASSERT(psa_key_derivation_input_bytes(
9872 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9873 NULL, 0));
9874 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9875 &operation,
9876 &key));
9877 PSA_ASSERT(psa_key_derivation_abort(&operation));
9878 PSA_ASSERT(psa_destroy_key(base_key));
9879 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9880 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009881#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009882 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009883#endif
9884 break;
9885
9886 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009887 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009888 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009889 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009890 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009891
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009892 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009893 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9894 PSA_ASSERT(psa_export_key(key,
9895 first_export, export_size,
9896 &first_exported_length));
9897 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009898 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009899 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01009900 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009901 }
Darryl Greend49a4992018-06-18 17:27:26 +01009902
9903 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009904 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009905 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009906 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009907
Darryl Greend49a4992018-06-18 17:27:26 +01009908 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009909 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9910 TEST_ASSERT(mbedtls_svc_key_id_equal(
9911 psa_get_key_id(&attributes), key_id));
9912 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9913 PSA_KEY_LIFETIME_PERSISTENT);
9914 TEST_EQUAL(psa_get_key_type(&attributes), type);
9915 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9916 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9917 mbedtls_test_update_key_usage_flags(usage_flags));
9918 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009919
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009920 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009921 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9922 PSA_ASSERT(psa_export_key(key,
9923 second_export, export_size,
9924 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009925 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009926 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009927 }
9928
9929 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009930 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009931 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009932 }
Darryl Greend49a4992018-06-18 17:27:26 +01009933
9934exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009935 /*
9936 * Key attributes may have been returned by psa_get_key_attributes()
9937 * thus reset them as required.
9938 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009939 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009940
Gilles Peskine449bd832023-01-11 14:50:10 +01009941 mbedtls_free(first_export);
9942 mbedtls_free(second_export);
9943 psa_key_derivation_abort(&operation);
9944 psa_destroy_key(base_key);
9945 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009946 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009947}
9948/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009949
Neil Armstronga557cb82022-06-10 08:58:32 +02009950/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009951void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9952 int primitive_arg, int hash_arg, int role_arg,
9953 int test_input, data_t *pw_data,
9954 int inj_err_type_arg,
9955 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009956{
9957 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9958 psa_pake_operation_t operation = psa_pake_operation_init();
9959 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009960 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009961 psa_key_type_t key_type_pw = key_type_pw_arg;
9962 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009963 psa_algorithm_t hash_alg = hash_arg;
9964 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009967 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9968 psa_status_t expected_error = expected_error_arg;
9969 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009970 unsigned char *output_buffer = NULL;
9971 size_t output_len = 0;
9972
Gilles Peskine449bd832023-01-11 14:50:10 +01009973 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009974
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009975 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009976 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009977 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009978
Gilles Peskine449bd832023-01-11 14:50:10 +01009979 if (pw_data->len > 0) {
9980 psa_set_key_usage_flags(&attributes, key_usage_pw);
9981 psa_set_key_algorithm(&attributes, alg);
9982 psa_set_key_type(&attributes, key_type_pw);
9983 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9984 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009985 }
9986
Gilles Peskine449bd832023-01-11 14:50:10 +01009987 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9988 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9989 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009990
Gilles Peskine449bd832023-01-11 14:50:10 +01009991 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009992
Gilles Peskine449bd832023-01-11 14:50:10 +01009993 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9994 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9995 expected_error);
9996 PSA_ASSERT(psa_pake_abort(&operation));
9997 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9998 expected_error);
9999 PSA_ASSERT(psa_pake_abort(&operation));
10000 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10001 expected_error);
10002 PSA_ASSERT(psa_pake_abort(&operation));
10003 TEST_EQUAL(psa_pake_set_role(&operation, role),
10004 expected_error);
10005 PSA_ASSERT(psa_pake_abort(&operation));
10006 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10007 NULL, 0, NULL),
10008 expected_error);
10009 PSA_ASSERT(psa_pake_abort(&operation));
10010 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10011 expected_error);
10012 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010013 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010014 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010015
Gilles Peskine449bd832023-01-11 14:50:10 +010010016 status = psa_pake_setup(&operation, &cipher_suite);
10017 if (status != PSA_SUCCESS) {
10018 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010019 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010020 }
10021
Gilles Peskine449bd832023-01-11 14:50:10 +010010022 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10023 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10024 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010025 goto exit;
10026 }
10027
Gilles Peskine449bd832023-01-11 14:50:10 +010010028 status = psa_pake_set_role(&operation, role);
10029 if (status != PSA_SUCCESS) {
10030 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010031 goto exit;
10032 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010033
Gilles Peskine449bd832023-01-11 14:50:10 +010010034 if (pw_data->len > 0) {
10035 status = psa_pake_set_password_key(&operation, key);
10036 if (status != PSA_SUCCESS) {
10037 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010038 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010039 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010040 }
10041
Gilles Peskine449bd832023-01-11 14:50:10 +010010042 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10043 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10044 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010045 goto exit;
10046 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010047
Gilles Peskine449bd832023-01-11 14:50:10 +010010048 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10049 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10050 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010051 goto exit;
10052 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010053
Gilles Peskine449bd832023-01-11 14:50:10 +010010054 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010055 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010056 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10057 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010058 goto exit;
10059 }
10060
Gilles Peskine449bd832023-01-11 14:50:10 +010010061 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010062 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010063 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10064 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010065 goto exit;
10066 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010067
Gilles Peskine449bd832023-01-11 14:50:10 +010010068 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10069 PSA_PAKE_STEP_KEY_SHARE);
10070 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10071 PSA_PAKE_STEP_ZK_PUBLIC);
10072 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10073 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010074
Gilles Peskine449bd832023-01-11 14:50:10 +010010075 if (test_input) {
10076 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10077 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10078 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010079 goto exit;
10080 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010081
Gilles Peskine449bd832023-01-11 14:50:10 +010010082 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10083 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10084 output_buffer, size_zk_proof),
10085 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010086 goto exit;
10087 }
10088
Gilles Peskine449bd832023-01-11 14:50:10 +010010089 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10090 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10091 output_buffer, size_zk_proof),
10092 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010093 goto exit;
10094 }
10095
Gilles Peskine449bd832023-01-11 14:50:10 +010010096 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10097 output_buffer, size_key_share);
10098 if (status != PSA_SUCCESS) {
10099 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010100 goto exit;
10101 }
10102
Gilles Peskine449bd832023-01-11 14:50:10 +010010103 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10104 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10105 output_buffer, size_zk_public + 1),
10106 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010107 goto exit;
10108 }
10109
Gilles Peskine449bd832023-01-11 14:50:10 +010010110 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010111 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010112 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10113 output_buffer, size_zk_public + 1);
10114 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10115 output_buffer, size_zk_public),
10116 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010117 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010118 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010119 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010120 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10121 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10122 NULL, 0, NULL),
10123 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010124 goto exit;
10125 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010126
Gilles Peskine449bd832023-01-11 14:50:10 +010010127 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10128 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10129 output_buffer, buf_size, &output_len),
10130 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010131 goto exit;
10132 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010133
Gilles Peskine449bd832023-01-11 14:50:10 +010010134 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10135 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10136 output_buffer, buf_size, &output_len),
10137 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010138 goto exit;
10139 }
10140
Gilles Peskine449bd832023-01-11 14:50:10 +010010141 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10142 output_buffer, buf_size, &output_len);
10143 if (status != PSA_SUCCESS) {
10144 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010145 goto exit;
10146 }
10147
Gilles Peskine449bd832023-01-11 14:50:10 +010010148 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010149
Gilles Peskine449bd832023-01-11 14:50:10 +010010150 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10151 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10152 output_buffer, size_zk_public - 1, &output_len),
10153 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010154 goto exit;
10155 }
10156
Gilles Peskine449bd832023-01-11 14:50:10 +010010157 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010158 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010159 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10160 output_buffer, size_zk_public - 1, &output_len);
10161 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10162 output_buffer, buf_size, &output_len),
10163 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010164 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010165 }
10166 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010167
10168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010169 PSA_ASSERT(psa_destroy_key(key));
10170 PSA_ASSERT(psa_pake_abort(&operation));
10171 mbedtls_free(output_buffer);
10172 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010173}
10174/* END_CASE */
10175
Neil Armstronga557cb82022-06-10 08:58:32 +020010176/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010177void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10178 int client_input_first, int inject_error,
10179 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010180{
10181 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10182 psa_pake_operation_t server = psa_pake_operation_init();
10183 psa_pake_operation_t client = psa_pake_operation_init();
10184 psa_algorithm_t alg = alg_arg;
10185 psa_algorithm_t hash_alg = hash_arg;
10186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10188
Gilles Peskine449bd832023-01-11 14:50:10 +010010189 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010190
Gilles Peskine449bd832023-01-11 14:50:10 +010010191 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10192 psa_set_key_algorithm(&attributes, alg);
10193 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10194 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10195 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010196
Gilles Peskine449bd832023-01-11 14:50:10 +010010197 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10198 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10199 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010200
10201
Gilles Peskine449bd832023-01-11 14:50:10 +010010202 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10203 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010204
Gilles Peskine449bd832023-01-11 14:50:10 +010010205 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10206 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010207
Gilles Peskine449bd832023-01-11 14:50:10 +010010208 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10209 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010210
Gilles Peskine449bd832023-01-11 14:50:10 +010010211 ecjpake_do_round(alg, primitive_arg, &server, &client,
10212 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010213
Gilles Peskine449bd832023-01-11 14:50:10 +010010214 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010215 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010216 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010217
Gilles Peskine449bd832023-01-11 14:50:10 +010010218 ecjpake_do_round(alg, primitive_arg, &server, &client,
10219 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010220
10221exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010222 psa_destroy_key(key);
10223 psa_pake_abort(&server);
10224 psa_pake_abort(&client);
10225 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010226}
10227/* END_CASE */
10228
10229/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010230void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10231 int derive_alg_arg, data_t *pw_data,
10232 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010233{
10234 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10235 psa_pake_operation_t server = psa_pake_operation_init();
10236 psa_pake_operation_t client = psa_pake_operation_init();
10237 psa_algorithm_t alg = alg_arg;
10238 psa_algorithm_t hash_alg = hash_arg;
10239 psa_algorithm_t derive_alg = derive_alg_arg;
10240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10242 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010243 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010244 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010245 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010246 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010247
Gilles Peskine449bd832023-01-11 14:50:10 +010010248 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010249
Gilles Peskine449bd832023-01-11 14:50:10 +010010250 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10251 psa_set_key_algorithm(&attributes, alg);
10252 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10253 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10254 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010255
Gilles Peskine449bd832023-01-11 14:50:10 +010010256 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10257 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10258 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010259
Neil Armstrong1e855602022-06-15 11:32:11 +020010260 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010261 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10262 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010263
Gilles Peskine449bd832023-01-11 14:50:10 +010010264 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10265 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10266 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10267 PSA_KEY_DERIVATION_INPUT_SEED,
10268 (const uint8_t *) "", 0));
10269 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10270 PSA_KEY_DERIVATION_INPUT_SEED,
10271 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010272 }
10273
Gilles Peskine449bd832023-01-11 14:50:10 +010010274 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10275 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010276
Gilles Peskine449bd832023-01-11 14:50:10 +010010277 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10278 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010279
Gilles Peskine449bd832023-01-11 14:50:10 +010010280 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10281 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010282
Gilles Peskine449bd832023-01-11 14:50:10 +010010283 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10284 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10285 PSA_ERROR_BAD_STATE);
10286 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10287 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010288 goto exit;
10289 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010290
Neil Armstrongf983caf2022-06-15 15:27:48 +020010291 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010292 ecjpake_do_round(alg, primitive_arg, &server, &client,
10293 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010294
Gilles Peskine449bd832023-01-11 14:50:10 +010010295 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10296 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10297 PSA_ERROR_BAD_STATE);
10298 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10299 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010300 goto exit;
10301 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010302
Neil Armstrongf983caf2022-06-15 15:27:48 +020010303 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010304 ecjpake_do_round(alg, primitive_arg, &server, &client,
10305 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010306
Gilles Peskine449bd832023-01-11 14:50:10 +010010307 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10308 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010309
10310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010311 psa_key_derivation_abort(&server_derive);
10312 psa_key_derivation_abort(&client_derive);
10313 psa_destroy_key(key);
10314 psa_pake_abort(&server);
10315 psa_pake_abort(&client);
10316 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010317}
10318/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010319
10320/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010321void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010322{
10323 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10324 const size_t bits = 256;
10325 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010326 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010327 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010328 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010329
10330 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10331 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010332 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10333 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10334 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10335 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010336 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010337 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10338 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010339
10340 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010341 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10342 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10343 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10344 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10345 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10346 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010347
10348 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010349 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10350 PSA_PAKE_OUTPUT_MAX_SIZE);
10351 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10352 PSA_PAKE_OUTPUT_MAX_SIZE);
10353 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10354 PSA_PAKE_OUTPUT_MAX_SIZE);
10355 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10356 PSA_PAKE_INPUT_MAX_SIZE);
10357 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10358 PSA_PAKE_INPUT_MAX_SIZE);
10359 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10360 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010361}
10362/* END_CASE */