blob: cb4315b7736f0fcc629ec2f28583f9af82e2b6ed [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
Gilles Peskine42649d92022-11-23 14:15:57 +01007#include "common.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02009/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
10 * uses mbedtls_ctr_drbg internally. */
11#include "mbedtls/ctr_drbg.h"
12
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020013#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020014#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +010016#include "psa_crypto_core.h"
17
Gilles Peskine8e94efe2021-02-13 00:25:53 +010018#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010019#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010020#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053021#if defined(PSA_CRYPTO_DRIVER_TEST)
22#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053023#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
24#else
25#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053026#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010027
Gilles Peskine4023c012021-05-27 13:21:20 +020028/* If this comes up, it's a bug in the test code or in the test data. */
29#define UNUSED 0xdeadbeef
30
Dave Rodgman647791d2021-06-23 12:49:59 +010031/* Assert that an operation is (not) active.
32 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010033#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
34#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010035
Przemek Stekiel7c795482022-11-15 22:26:12 +010036#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010037int ecjpake_operation_setup(psa_pake_operation_t *operation,
38 psa_pake_cipher_suite_t *cipher_suite,
39 psa_pake_role_t role,
40 mbedtls_svc_key_id_t key,
41 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010042{
Gilles Peskine449bd832023-01-11 14:50:10 +010043 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010044
Gilles Peskine449bd832023-01-11 14:50:10 +010045 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010046
Gilles Peskine449bd832023-01-11 14:50:10 +010047 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 if (key_available) {
50 PSA_ASSERT(psa_pake_set_password_key(operation, key));
51 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010052 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010053exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010054 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010055}
56#endif
57
Jaeden Amerof24c7f82018-06-27 17:20:43 +010058/** An invalid export length that will never be set by psa_export_key(). */
59static const size_t INVALID_EXPORT_LENGTH = ~0U;
60
Gilles Peskinea7aa4422018-08-14 15:17:54 +020061/** Test if a buffer contains a constant byte value.
62 *
63 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020064 *
65 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020066 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020067 * \param size Size of the buffer in bytes.
68 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020069 * \return 1 if the buffer is all-bits-zero.
70 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020071 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020073{
74 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010075 for (i = 0; i < size; i++) {
76 if (((unsigned char *) buffer)[i] != c) {
77 return 0;
78 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020079 }
Gilles Peskine449bd832023-01-11 14:50:10 +010080 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020081}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010082#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020083/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010084static int asn1_write_10x(unsigned char **p,
85 unsigned char *start,
86 size_t bits,
87 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020088{
89 int ret;
90 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (bits == 0) {
92 return MBEDTLS_ERR_ASN1_INVALID_DATA;
93 }
94 if (bits <= 8 && x >= 1 << (bits - 1)) {
95 return MBEDTLS_ERR_ASN1_INVALID_DATA;
96 }
97 if (*p < start || *p - start < (ptrdiff_t) len) {
98 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
99 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200100 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 (*p)[len-1] = x;
102 if (bits % 8 == 0) {
103 (*p)[1] |= 1;
104 } else {
105 (*p)[0] |= 1 << (bits % 8);
106 }
107 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
108 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
109 MBEDTLS_ASN1_INTEGER));
110 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200111}
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113static int construct_fake_rsa_key(unsigned char *buffer,
114 size_t buffer_size,
115 unsigned char **p,
116 size_t bits,
117 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200118{
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200120 int ret;
121 int len = 0;
122 /* Construct something that looks like a DER encoding of
123 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
124 * RSAPrivateKey ::= SEQUENCE {
125 * version Version,
126 * modulus INTEGER, -- n
127 * publicExponent INTEGER, -- e
128 * privateExponent INTEGER, -- d
129 * prime1 INTEGER, -- p
130 * prime2 INTEGER, -- q
131 * exponent1 INTEGER, -- d mod (p-1)
132 * exponent2 INTEGER, -- d mod (q-1)
133 * coefficient INTEGER, -- (inverse of q) mod p
134 * otherPrimeInfos OtherPrimeInfos OPTIONAL
135 * }
136 * Or, for a public key, the same structure with only
137 * version, modulus and publicExponent.
138 */
139 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (keypair) {
141 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
142 asn1_write_10x(p, buffer, half_bits, 1));
143 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
144 asn1_write_10x(p, buffer, half_bits, 1));
145 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
146 asn1_write_10x(p, buffer, half_bits, 1));
147 MBEDTLS_ASN1_CHK_ADD(len, /* q */
148 asn1_write_10x(p, buffer, half_bits, 1));
149 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
150 asn1_write_10x(p, buffer, half_bits, 3));
151 MBEDTLS_ASN1_CHK_ADD(len, /* d */
152 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200153 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
155 asn1_write_10x(p, buffer, 17, 1));
156 MBEDTLS_ASN1_CHK_ADD(len, /* n */
157 asn1_write_10x(p, buffer, bits, 1));
158 if (keypair) {
159 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
160 mbedtls_asn1_write_int(p, buffer, 0));
161 }
162 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200163 {
164 const unsigned char tag =
165 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200167 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200169}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100170#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172int exercise_mac_setup(psa_key_type_t key_type,
173 const unsigned char *key_bytes,
174 size_t key_length,
175 psa_algorithm_t alg,
176 psa_mac_operation_t *operation,
177 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178{
Ronald Cron5425a212020-08-04 14:58:35 +0200179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
183 psa_set_key_algorithm(&attributes, alg);
184 psa_set_key_type(&attributes, key_type);
185 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100188 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100190 /* If setup failed, reproduce the failure, so that the caller can
191 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (*status != PSA_SUCCESS) {
193 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100194 }
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 psa_destroy_key(key);
197 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100198
199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 psa_destroy_key(key);
201 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100202}
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204int exercise_cipher_setup(psa_key_type_t key_type,
205 const unsigned char *key_bytes,
206 size_t key_length,
207 psa_algorithm_t alg,
208 psa_cipher_operation_t *operation,
209 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100210{
Ronald Cron5425a212020-08-04 14:58:35 +0200211 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
215 psa_set_key_algorithm(&attributes, alg);
216 psa_set_key_type(&attributes, key_type);
217 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100220 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* If setup failed, reproduce the failure, so that the caller can
223 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (*status != PSA_SUCCESS) {
225 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
226 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 }
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 psa_destroy_key(key);
230 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100231
232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 psa_destroy_key(key);
234 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100235}
236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200238{
239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200241 uint8_t buffer[1];
242 size_t length;
243 int ok = 0;
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 psa_set_key_id(&attributes, key_id);
246 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
247 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
248 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
249 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
250 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200251 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200253 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
255 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
256 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
257 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
258 TEST_EQUAL(psa_get_key_type(&attributes), 0);
259 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
262 PSA_ERROR_INVALID_HANDLE);
263 TEST_EQUAL(psa_export_public_key(key,
264 buffer, sizeof(buffer), &length),
265 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200266
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200267 ok = 1;
268
269exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100270 /*
271 * Key attributes may have been returned by psa_get_key_attributes()
272 * thus reset them as required.
273 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200277}
278
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200279/* Assert that a key isn't reported as having a slot number. */
280#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100281#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200282 do \
283 { \
284 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 TEST_EQUAL(psa_get_key_slot_number( \
286 attributes, \
287 &ASSERT_NO_SLOT_NUMBER_slot_number), \
288 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200289 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200291#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292#define ASSERT_NO_SLOT_NUMBER(attributes) \
293 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200294#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
295
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530296#define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */
297
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100298/* An overapproximation of the amount of storage needed for a key of the
299 * given type and with the given content. The API doesn't make it easy
300 * to find a good value for the size. The current implementation doesn't
301 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302#define KEY_BITS_FROM_DATA(type, data) \
303 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100304
Darryl Green0c6575a2018-11-07 16:05:30 +0000305typedef enum {
306 IMPORT_KEY = 0,
307 GENERATE_KEY = 1,
308 DERIVE_KEY = 2
309} generate_method;
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100312 DO_NOT_SET_LENGTHS = 0,
313 SET_LENGTHS_BEFORE_NONCE = 1,
314 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100315} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100318 USE_NULL_TAG = 0,
319 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100320} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100321
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530322
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323/*!
324 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 * \param key_type_arg Type of key passed in
326 * \param key_data The encryption / decryption key data
327 * \param alg_arg The type of algorithm used
328 * \param nonce Nonce data
329 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100330 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100331 * feed additional data in to be encrypted /
332 * decrypted. If -1, no chunking.
333 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100334 * \param data_part_len_arg If not -1, the length of chunks to feed
335 * the data in to be encrypted / decrypted. If
336 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100337 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100338 * expected here, this controls whether or not
339 * to set lengths, and in what order with
340 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100343 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100344 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100346 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
348 int alg_arg,
349 data_t *nonce,
350 data_t *additional_data,
351 int ad_part_len_arg,
352 data_t *input_data,
353 int data_part_len_arg,
354 set_lengths_method_t set_lengths_method,
355 data_t *expected_output,
356 int is_encrypt,
357 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100358{
359 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
360 psa_key_type_t key_type = key_type_arg;
361 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100362 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100363 unsigned char *output_data = NULL;
364 unsigned char *part_data = NULL;
365 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100366 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100367 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100368 size_t output_size = 0;
369 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t output_length = 0;
371 size_t key_bits = 0;
372 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100373 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100374 size_t part_length = 0;
375 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100377 size_t ad_part_len = 0;
378 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100380 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
381 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
382
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100383 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100384 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (is_encrypt) {
389 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
390 } else {
391 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100393
394 psa_set_key_algorithm(&attributes, alg);
395 psa_set_key_type(&attributes, key_type);
396
397 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
398 &key));
399
400 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
401 key_bits = psa_get_key_bits(&attributes);
402
403 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
404
405 if (is_encrypt) {
406 /* Tag gets written at end of buffer. */
407 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
408 (input_data->len +
409 tag_length));
410 data_true_size = input_data->len;
411 } else {
412 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
413 (input_data->len -
414 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100415
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100416 /* Do not want to attempt to decrypt tag. */
417 data_true_size = input_data->len - tag_length;
418 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100419
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100420 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (is_encrypt) {
423 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
424 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
425 } else {
426 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
427 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100428 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100429
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100430 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (is_encrypt) {
433 status = psa_aead_encrypt_setup(&operation, key, alg);
434 } else {
435 status = psa_aead_decrypt_setup(&operation, key, alg);
436 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100437
438 /* If the operation is not supported, just skip and not fail in case the
439 * encryption involves a common limitation of cryptography hardwares and
440 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (status == PSA_ERROR_NOT_SUPPORTED) {
442 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
443 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100444 }
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
449 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
450 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
451 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
452 data_true_size));
453 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
454 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
455 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
458 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100459 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100463 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100466 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 part_offset += part_length, part_count++) {
468 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100469 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100471 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100473 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100474 }
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 PSA_ASSERT(psa_aead_update_ad(&operation,
477 additional_data->x + part_offset,
478 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100479
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100482 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
484 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 }
486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 data_part_len = (size_t) data_part_len_arg;
490 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
491 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100492
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100493 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100496 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 part_offset += part_length, part_count++) {
498 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100499 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 } else if ((data_true_size - part_offset) < data_part_len) {
501 part_length = (data_true_size - part_offset);
502 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100503 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100504 }
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 PSA_ASSERT(psa_aead_update(&operation,
507 (input_data->x + part_offset),
508 part_length, part_data,
509 part_data_size,
510 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (output_data && output_part_length) {
513 memcpy((output_data + output_length), part_data,
514 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 }
516
Paul Elliottd3f82412021-06-16 16:52:21 +0100517 output_length += output_part_length;
518 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100520 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
522 data_true_size, output_data,
523 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100524 }
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (is_encrypt) {
527 PSA_ASSERT(psa_aead_finish(&operation, final_data,
528 final_output_size,
529 &output_part_length,
530 tag_buffer, tag_length,
531 &tag_size));
532 } else {
533 PSA_ASSERT(psa_aead_verify(&operation, final_data,
534 final_output_size,
535 &output_part_length,
536 (input_data->x + data_true_size),
537 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (output_data && output_part_length) {
541 memcpy((output_data + output_length), final_data,
542 output_part_length);
543 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100544
545 output_length += output_part_length;
546
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100547
548 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
549 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (is_encrypt) {
551 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (output_data && tag_length) {
554 memcpy((output_data + output_length), tag_buffer,
555 tag_length);
556 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100557
558 output_length += tag_length;
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 TEST_EQUAL(output_length,
561 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
562 input_data->len));
563 TEST_LE_U(output_length,
564 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
565 } else {
566 TEST_EQUAL(output_length,
567 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
568 input_data->len));
569 TEST_LE_U(output_length,
570 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100571 }
572
Paul Elliottd3f82412021-06-16 16:52:21 +0100573
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100574 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100575 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100576
Paul Elliottd3f82412021-06-16 16:52:21 +0100577
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100578 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100579
580exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 psa_destroy_key(key);
582 psa_aead_abort(&operation);
583 mbedtls_free(output_data);
584 mbedtls_free(part_data);
585 mbedtls_free(final_data);
586 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100589}
590
Neil Armstrong4766f992022-02-28 16:23:59 +0100591/*!
592 * \brief Internal Function for MAC multipart tests.
593 * \param key_type_arg Type of key passed in
594 * \param key_data The encryption / decryption key data
595 * \param alg_arg The type of algorithm used
596 * \param input_data Data to encrypt / decrypt
597 * \param data_part_len_arg If not -1, the length of chunks to feed
598 * the data in to be encrypted / decrypted. If
599 * -1, no chunking
600 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000601 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100602 * \param do_zero_parts If non-zero, interleave zero length chunks
603 * with normal length chunks.
604 * \return int Zero on failure, non-zero on success.
605 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
607 int alg_arg,
608 data_t *input_data,
609 int data_part_len_arg,
610 data_t *expected_output,
611 int is_verify,
612 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100613{
614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
615 psa_key_type_t key_type = key_type_arg;
616 psa_algorithm_t alg = alg_arg;
617 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
618 unsigned char mac[PSA_MAC_MAX_SIZE];
619 size_t part_offset = 0;
620 size_t part_length = 0;
621 size_t data_part_len = 0;
622 size_t mac_len = 0;
623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
624 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
625
626 int test_ok = 0;
627 size_t part_count = 0;
628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (is_verify) {
632 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
633 } else {
634 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
635 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 psa_set_key_algorithm(&attributes, alg);
638 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
641 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (is_verify) {
644 status = psa_mac_verify_setup(&operation, key, alg);
645 } else {
646 status = psa_mac_sign_setup(&operation, key, alg);
647 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100652 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100656 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 part_offset += part_length, part_count++) {
658 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 } else if ((input_data->len - part_offset) < data_part_len) {
661 part_length = (input_data->len - part_offset);
662 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100663 part_length = data_part_len;
664 }
665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 PSA_ASSERT(psa_mac_update(&operation,
667 (input_data->x + part_offset),
668 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100669 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100671 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
673 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100674 }
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (is_verify) {
677 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
678 expected_output->len));
679 } else {
680 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
681 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100682
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100683 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100684 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100685 }
686
687 test_ok = 1;
688
689exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 psa_destroy_key(key);
691 psa_mac_abort(&operation);
692 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100695}
696
Neil Armstrong75673ab2022-06-15 17:39:01 +0200697#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100698static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
699 psa_pake_operation_t *server,
700 psa_pake_operation_t *client,
701 int client_input_first,
702 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200703{
704 unsigned char *buffer0 = NULL, *buffer1 = NULL;
705 size_t buffer_length = (
706 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
707 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
708 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200709 /* The output should be exactly this size according to the spec */
710 const size_t expected_size_key_share =
711 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
712 /* The output should be exactly this size according to the spec */
713 const size_t expected_size_zk_public =
714 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
715 /* The output can be smaller: the spec allows stripping leading zeroes */
716 const size_t max_expected_size_zk_proof =
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200718 size_t buffer0_off = 0;
719 size_t buffer1_off = 0;
720 size_t s_g1_len, s_g2_len, s_a_len;
721 size_t s_g1_off, s_g2_off, s_a_off;
722 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
723 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
724 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
725 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
726 size_t c_g1_len, c_g2_len, c_a_len;
727 size_t c_g1_off, c_g2_off, c_a_off;
728 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
729 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
730 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
731 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
732 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200733 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200734
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100735 TEST_CALLOC(buffer0, buffer_length);
736 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200739 case 1:
740 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
742 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000743 buffer_length - buffer0_off, &s_g1_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200745 s_g1_off = buffer0_off;
746 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
748 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000749 buffer_length - buffer0_off, &s_x1_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200751 s_x1_pk_off = buffer0_off;
752 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
754 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000755 buffer_length - buffer0_off, &s_x1_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200757 s_x1_pr_off = buffer0_off;
758 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
760 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000761 buffer_length - buffer0_off, &s_g2_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200763 s_g2_off = buffer0_off;
764 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
766 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000767 buffer_length - buffer0_off, &s_x2_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200769 s_x2_pk_off = buffer0_off;
770 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
772 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000773 buffer_length - buffer0_off, &s_x2_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200775 s_x2_pr_off = buffer0_off;
776 buffer0_off += s_x2_pr_len;
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500779 buffer0[s_x1_pr_off + 8] ^= 1;
780 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200781 expected_status = PSA_ERROR_DATA_INVALID;
782 }
783
Neil Armstrong51009d72022-09-05 17:59:54 +0200784 /*
785 * When injecting errors in inputs, the implementation is
786 * free to detect it right away of with a delay.
787 * This permits delaying the error until the end of the input
788 * sequence, if no error appears then, this will be treated
789 * as an error.
790 */
791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200793 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
795 buffer0 + s_g1_off, s_g1_len);
796 if (inject_error == 1 && status != PSA_SUCCESS) {
797 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200798 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 } else {
800 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200801 }
802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
804 buffer0 + s_x1_pk_off,
805 s_x1_pk_len);
806 if (inject_error == 1 && status != PSA_SUCCESS) {
807 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200808 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 } else {
810 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200811 }
812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
814 buffer0 + s_x1_pr_off,
815 s_x1_pr_len);
816 if (inject_error == 1 && status != PSA_SUCCESS) {
817 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200818 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 } else {
820 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200821 }
822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
824 buffer0 + s_g2_off,
825 s_g2_len);
826 if (inject_error == 1 && status != PSA_SUCCESS) {
827 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200828 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 } else {
830 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200831 }
832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
834 buffer0 + s_x2_pk_off,
835 s_x2_pk_len);
836 if (inject_error == 1 && status != PSA_SUCCESS) {
837 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200838 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 } else {
840 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200841 }
842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
844 buffer0 + s_x2_pr_off,
845 s_x2_pr_len);
846 if (inject_error == 1 && status != PSA_SUCCESS) {
847 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200848 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 } else {
850 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200851 }
852
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200853 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (inject_error == 1) {
855 TEST_ASSERT(
856 !"One of the last psa_pake_input() calls should have returned the expected error.");
857 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200858 }
859
860 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
862 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000863 buffer_length - buffer1_off, &c_g1_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200865 c_g1_off = buffer1_off;
866 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
868 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000869 buffer_length - buffer1_off, &c_x1_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200871 c_x1_pk_off = buffer1_off;
872 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
874 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000875 buffer_length - buffer1_off, &c_x1_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200877 c_x1_pr_off = buffer1_off;
878 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
880 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000881 buffer_length - buffer1_off, &c_g2_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200883 c_g2_off = buffer1_off;
884 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
886 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000887 buffer_length - buffer1_off, &c_x2_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200889 c_x2_pk_off = buffer1_off;
890 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
892 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000893 buffer_length - buffer1_off, &c_x2_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200895 c_x2_pr_off = buffer1_off;
896 buffer1_off += c_x2_pr_len;
897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200899 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
901 buffer0 + s_g1_off, s_g1_len);
902 if (inject_error == 1 && status != PSA_SUCCESS) {
903 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200904 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 } else {
906 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200907 }
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
910 buffer0 + s_x1_pk_off,
911 s_x1_pk_len);
912 if (inject_error == 1 && status != PSA_SUCCESS) {
913 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200914 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 } else {
916 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200917 }
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
920 buffer0 + s_x1_pr_off,
921 s_x1_pr_len);
922 if (inject_error == 1 && status != PSA_SUCCESS) {
923 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200924 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 } else {
926 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200927 }
928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
930 buffer0 + s_g2_off,
931 s_g2_len);
932 if (inject_error == 1 && status != PSA_SUCCESS) {
933 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200934 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 } else {
936 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200937 }
938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
940 buffer0 + s_x2_pk_off,
941 s_x2_pk_len);
942 if (inject_error == 1 && status != PSA_SUCCESS) {
943 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200944 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 } else {
946 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200947 }
948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
950 buffer0 + s_x2_pr_off,
951 s_x2_pr_len);
952 if (inject_error == 1 && status != PSA_SUCCESS) {
953 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200954 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 } else {
956 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200957 }
958
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200959 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (inject_error == 1) {
961 TEST_ASSERT(
962 !"One of the last psa_pake_input() calls should have returned the expected error.");
963 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200964 }
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500967 buffer1[c_x1_pr_off + 12] ^= 1;
968 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200969 expected_status = PSA_ERROR_DATA_INVALID;
970 }
971
972 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
974 buffer1 + c_g1_off, c_g1_len);
975 if (inject_error == 2 && status != PSA_SUCCESS) {
976 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200977 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 } else {
979 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200980 }
981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
983 buffer1 + c_x1_pk_off, c_x1_pk_len);
984 if (inject_error == 2 && status != PSA_SUCCESS) {
985 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200986 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 } else {
988 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200989 }
990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
992 buffer1 + c_x1_pr_off, c_x1_pr_len);
993 if (inject_error == 2 && status != PSA_SUCCESS) {
994 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200995 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 } else {
997 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200998 }
999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1001 buffer1 + c_g2_off, c_g2_len);
1002 if (inject_error == 2 && status != PSA_SUCCESS) {
1003 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001004 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 } else {
1006 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001007 }
1008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1010 buffer1 + c_x2_pk_off, c_x2_pk_len);
1011 if (inject_error == 2 && status != PSA_SUCCESS) {
1012 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001013 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 } else {
1015 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001016 }
1017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1019 buffer1 + c_x2_pr_off, c_x2_pr_len);
1020 if (inject_error == 2 && status != PSA_SUCCESS) {
1021 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001022 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 } else {
1024 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001025 }
1026
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001027 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (inject_error == 2) {
1029 TEST_ASSERT(
1030 !"One of the last psa_pake_input() calls should have returned the expected error.");
1031 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001032
1033 break;
1034
1035 case 2:
1036 /* Server second round Output */
1037 buffer0_off = 0;
1038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1040 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001041 buffer_length - buffer0_off, &s_a_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001043 s_a_off = buffer0_off;
1044 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1046 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001047 buffer_length - buffer0_off, &s_x2s_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001049 s_x2s_pk_off = buffer0_off;
1050 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1052 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001053 buffer_length - buffer0_off, &s_x2s_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001055 s_x2s_pr_off = buffer0_off;
1056 buffer0_off += s_x2s_pr_len;
1057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001059 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001060 expected_status = PSA_ERROR_DATA_INVALID;
1061 }
1062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001064 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1066 buffer0 + s_a_off, s_a_len);
1067 if (inject_error == 3 && status != PSA_SUCCESS) {
1068 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001069 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 } else {
1071 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001072 }
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1075 buffer0 + s_x2s_pk_off,
1076 s_x2s_pk_len);
1077 if (inject_error == 3 && status != PSA_SUCCESS) {
1078 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001079 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 } else {
1081 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001082 }
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1085 buffer0 + s_x2s_pr_off,
1086 s_x2s_pr_len);
1087 if (inject_error == 3 && status != PSA_SUCCESS) {
1088 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001089 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 } else {
1091 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001092 }
1093
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001094 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (inject_error == 3) {
1096 TEST_ASSERT(
1097 !"One of the last psa_pake_input() calls should have returned the expected error.");
1098 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001099 }
1100
1101 /* Client second round Output */
1102 buffer1_off = 0;
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1105 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001106 buffer_length - buffer1_off, &c_a_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001108 c_a_off = buffer1_off;
1109 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1111 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001112 buffer_length - buffer1_off, &c_x2s_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001114 c_x2s_pk_off = buffer1_off;
1115 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1117 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001118 buffer_length - buffer1_off, &c_x2s_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001120 c_x2s_pr_off = buffer1_off;
1121 buffer1_off += c_x2s_pr_len;
1122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001124 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1126 buffer0 + s_a_off, s_a_len);
1127 if (inject_error == 3 && status != PSA_SUCCESS) {
1128 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001129 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 } else {
1131 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001132 }
1133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1135 buffer0 + s_x2s_pk_off,
1136 s_x2s_pk_len);
1137 if (inject_error == 3 && status != PSA_SUCCESS) {
1138 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001139 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 } else {
1141 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001142 }
1143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1145 buffer0 + s_x2s_pr_off,
1146 s_x2s_pr_len);
1147 if (inject_error == 3 && status != PSA_SUCCESS) {
1148 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001149 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 } else {
1151 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001152 }
1153
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001154 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if (inject_error == 3) {
1156 TEST_ASSERT(
1157 !"One of the last psa_pake_input() calls should have returned the expected error.");
1158 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001159 }
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001162 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001163 expected_status = PSA_ERROR_DATA_INVALID;
1164 }
1165
1166 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1168 buffer1 + c_a_off, c_a_len);
1169 if (inject_error == 4 && status != PSA_SUCCESS) {
1170 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001171 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 } else {
1173 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001174 }
1175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1177 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1178 if (inject_error == 4 && status != PSA_SUCCESS) {
1179 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001180 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 } else {
1182 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001183 }
1184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1186 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1187 if (inject_error == 4 && status != PSA_SUCCESS) {
1188 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001189 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 } else {
1191 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001192 }
1193
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001194 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (inject_error == 4) {
1196 TEST_ASSERT(
1197 !"One of the last psa_pake_input() calls should have returned the expected error.");
1198 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001199
1200 break;
1201
1202 }
1203
Neil Armstrongf983caf2022-06-15 15:27:48 +02001204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 mbedtls_free(buffer0);
1206 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001207}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001208#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001211 INJECT_ERR_NONE = 0,
1212 INJECT_ERR_UNINITIALIZED_ACCESS,
1213 INJECT_ERR_DUPLICATE_SETUP,
1214 INJECT_ERR_INVALID_USER,
1215 INJECT_ERR_INVALID_PEER,
1216 INJECT_ERR_SET_USER,
1217 INJECT_ERR_SET_PEER,
1218 INJECT_EMPTY_IO_BUFFER,
1219 INJECT_UNKNOWN_STEP,
1220 INJECT_INVALID_FIRST_STEP,
1221 INJECT_WRONG_BUFFER_SIZE,
1222 INJECT_VALID_OPERATION_AFTER_FAILURE,
1223 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1224 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1225} ecjpake_injected_failure_t;
1226
Paul Elliott01885fa2023-02-09 12:07:30 +00001227#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001228
Paul Elliott6f600372023-02-06 18:41:05 +00001229static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1230 psa_status_t expected_status,
1231 size_t *min_completes,
1232 size_t *max_completes)
1233{
1234
1235 /* This is slightly contrived, but we only really know that with a minimum
1236 value of max_ops that a successful operation should take more than one op
1237 to complete, and likewise that with a max_ops of
1238 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1239 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001240
Paul Elliott6f600372023-02-06 18:41:05 +00001241 if (expected_status == PSA_SUCCESS) {
1242 *min_completes = 2;
1243 } else {
1244 *min_completes = 1;
1245 }
1246
1247 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1248 } else {
1249 *min_completes = 1;
1250 *max_completes = 1;
1251 }
1252}
Paul Elliott01885fa2023-02-09 12:07:30 +00001253#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001254
Gilles Peskinee59236f2018-01-27 23:32:46 +01001255/* END_HEADER */
1256
1257/* BEGIN_DEPENDENCIES
1258 * depends_on:MBEDTLS_PSA_CRYPTO_C
1259 * END_DEPENDENCIES
1260 */
1261
1262/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001263void psa_can_do_hash()
1264{
1265 /* We can't test that this is specific to drivers until partial init has
1266 * been implemented, but we can at least test before/after full init. */
1267 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1268 PSA_INIT();
1269 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1270 PSA_DONE();
1271}
1272/* END_CASE */
1273
1274/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001276{
1277 size_t max_truncated_mac_size =
1278 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1279
1280 /* Check that the length for a truncated MAC always fits in the algorithm
1281 * encoding. The shifted mask is the maximum truncated value. The
1282 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001284}
1285/* END_CASE */
1286
1287/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288void import_with_policy(int type_arg,
1289 int usage_arg, int alg_arg,
1290 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001291{
1292 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1293 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001294 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001295 psa_key_type_t type = type_arg;
1296 psa_key_usage_t usage = usage_arg;
1297 psa_algorithm_t alg = alg_arg;
1298 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001300 psa_status_t status;
1301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 psa_set_key_type(&attributes, type);
1305 psa_set_key_usage_flags(&attributes, usage);
1306 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 status = psa_import_key(&attributes,
1309 key_material, sizeof(key_material),
1310 &key);
1311 TEST_EQUAL(status, expected_status);
1312 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001313 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1317 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1318 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1319 mbedtls_test_update_key_usage_flags(usage));
1320 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1321 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 PSA_ASSERT(psa_destroy_key(key));
1324 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001325
1326exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001327 /*
1328 * Key attributes may have been returned by psa_get_key_attributes()
1329 * thus reset them as required.
1330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 psa_destroy_key(key);
1334 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001335}
1336/* END_CASE */
1337
1338/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001339void import_with_data(data_t *data, int type_arg,
1340 int attr_bits_arg,
1341 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001342{
1343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1344 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001346 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001347 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001348 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001349 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 psa_set_key_type(&attributes, type);
1354 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001357 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1358 *
1359 * This can happen with a type supported only by a driver:
1360 * - the driver sees the invalid data (for example wrong size) and thinks
1361 * "well perhaps this is a key size I don't support" so it returns
1362 * NOT_SUPPORTED which is correct at this point;
1363 * - we fallback to built-ins, which don't support this type, so return
1364 * NOT_SUPPORTED which again is correct at this point.
1365 */
1366 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1367 status == PSA_ERROR_NOT_SUPPORTED) {
1368 ; // OK
1369 } else {
1370 TEST_EQUAL(status, expected_status);
1371 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001373 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1377 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1378 if (attr_bits != 0) {
1379 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1380 }
1381 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 PSA_ASSERT(psa_destroy_key(key));
1384 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001385
1386exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001387 /*
1388 * Key attributes may have been returned by psa_get_key_attributes()
1389 * thus reset them as required.
1390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 psa_destroy_key(key);
1394 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395}
1396/* END_CASE */
1397
1398/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001399/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001400void import_large_key(int type_arg, int byte_size_arg,
1401 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001402{
1403 psa_key_type_t type = type_arg;
1404 size_t byte_size = byte_size_arg;
1405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1406 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001408 psa_status_t status;
1409 uint8_t *buffer = NULL;
1410 size_t buffer_size = byte_size + 1;
1411 size_t n;
1412
Steven Cooreman69967ce2021-01-18 18:01:08 +01001413 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001414 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001415 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001419
1420 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1422 psa_set_key_type(&attributes, type);
1423 status = psa_import_key(&attributes, buffer, byte_size, &key);
1424 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1425 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (status == PSA_SUCCESS) {
1428 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1429 TEST_EQUAL(psa_get_key_type(&attributes), type);
1430 TEST_EQUAL(psa_get_key_bits(&attributes),
1431 PSA_BYTES_TO_BITS(byte_size));
1432 ASSERT_NO_SLOT_NUMBER(&attributes);
1433 memset(buffer, 0, byte_size + 1);
1434 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1435 for (n = 0; n < byte_size; n++) {
1436 TEST_EQUAL(buffer[n], 'K');
1437 }
1438 for (n = byte_size; n < buffer_size; n++) {
1439 TEST_EQUAL(buffer[n], 0);
1440 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001441 }
1442
1443exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001444 /*
1445 * Key attributes may have been returned by psa_get_key_attributes()
1446 * thus reset them as required.
1447 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 psa_destroy_key(key);
1451 PSA_DONE();
1452 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001453}
1454/* END_CASE */
1455
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001456/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001457/* Import an RSA key with a valid structure (but not valid numbers
1458 * inside, beyond having sensible size and parity). This is expected to
1459 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001461{
Ronald Cron5425a212020-08-04 14:58:35 +02001462 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001463 size_t bits = bits_arg;
1464 psa_status_t expected_status = expected_status_arg;
1465 psa_status_t status;
1466 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001467 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001468 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001470 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001471 unsigned char *p;
1472 int ret;
1473 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001474 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001477 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1480 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001481 length = ret;
1482
1483 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 psa_set_key_type(&attributes, type);
1485 status = psa_import_key(&attributes, p, length, &key);
1486 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (status == PSA_SUCCESS) {
1489 PSA_ASSERT(psa_destroy_key(key));
1490 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001491
1492exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_free(buffer);
1494 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001495}
1496/* END_CASE */
1497
1498/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499void import_export(data_t *data,
1500 int type_arg,
1501 int usage_arg, int alg_arg,
1502 int lifetime_arg,
1503 int expected_bits,
1504 int export_size_delta,
1505 int expected_export_status_arg,
1506 /*whether reexport must give the original input exactly*/
1507 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001508{
Ronald Cron5425a212020-08-04 14:58:35 +02001509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001510 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001511 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001512 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301514 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001515 unsigned char *exported = NULL;
1516 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001517 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001518 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001519 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001521 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522
Moran Pekercb088e72018-07-17 17:36:59 +03001523 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001524 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001526 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 }
1528 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 psa_set_key_lifetime(&attributes, lifetime);
1531 psa_set_key_usage_flags(&attributes, usage_arg);
1532 psa_set_key_algorithm(&attributes, alg);
1533 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001534
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001535 if (PSA_KEY_TYPE_IS_DH(type) &&
1536 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001537 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1538 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001539 }
1540
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001541 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001542 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001543 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544
1545 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1547 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1548 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1549 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001550
1551 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 status = psa_export_key(key, exported, export_size, &exported_length);
1553 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001554
1555 /* The exported length must be set by psa_export_key() to a value between 0
1556 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1558 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1559 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1562 export_size - exported_length));
1563 if (status != PSA_SUCCESS) {
1564 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001565 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001566 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567
Gilles Peskineea38a922021-02-13 00:05:16 +01001568 /* Run sanity checks on the exported key. For non-canonical inputs,
1569 * this validates the canonical representations. For canonical inputs,
1570 * this doesn't directly validate the implementation, but it still helps
1571 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if (!psa_key_lifetime_is_external(lifetime)) {
1573 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301574 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 }
Archana4d7ae1d2021-07-07 02:50:22 +05301576 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001579 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001581 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1583 &key2));
1584 PSA_ASSERT(psa_export_key(key2,
1585 reexported,
1586 export_size,
1587 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001588 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001589 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 TEST_LE_U(exported_length,
1593 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1594 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001595 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1596 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1597 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1598 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1599 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001600
1601destroy:
1602 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 PSA_ASSERT(psa_destroy_key(key));
1604 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001605
1606exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001607 /*
1608 * Key attributes may have been returned by psa_get_key_attributes()
1609 * thus reset them as required.
1610 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 psa_reset_key_attributes(&got_attributes);
1612 psa_destroy_key(key);
1613 mbedtls_free(exported);
1614 mbedtls_free(reexported);
1615 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001616}
1617/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001618
Moran Pekerf709f4a2018-06-06 17:26:04 +03001619/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001620void import_export_public_key(data_t *data,
1621 int type_arg, // key pair or public key
1622 int alg_arg,
1623 int lifetime_arg,
1624 int export_size_delta,
1625 int expected_export_status_arg,
1626 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001627{
Ronald Cron5425a212020-08-04 14:58:35 +02001628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001629 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001630 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001631 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301633 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001634 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001635 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001636 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001637 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 psa_set_key_lifetime(&attributes, lifetime);
1642 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1643 psa_set_key_algorithm(&attributes, alg);
1644 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001645
1646 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001648
Gilles Peskine49c25912018-10-29 15:15:31 +01001649 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001650 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 status = psa_export_public_key(key,
1652 exported, export_size,
1653 &exported_length);
1654 TEST_EQUAL(status, expected_export_status);
1655 if (status == PSA_SUCCESS) {
1656 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001657 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1659 bits = psa_get_key_bits(&attributes);
1660 TEST_LE_U(expected_public_key->len,
1661 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1662 TEST_LE_U(expected_public_key->len,
1663 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1664 TEST_LE_U(expected_public_key->len,
1665 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001666 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001667 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001668 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001669exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001670 /*
1671 * Key attributes may have been returned by psa_get_key_attributes()
1672 * thus reset them as required.
1673 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 mbedtls_free(exported);
1677 psa_destroy_key(key);
1678 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001679}
1680/* END_CASE */
1681
Gilles Peskine20035e32018-02-03 22:44:14 +01001682/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683void import_and_exercise_key(data_t *data,
1684 int type_arg,
1685 int bits_arg,
1686 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001687{
Ronald Cron5425a212020-08-04 14:58:35 +02001688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001689 psa_key_type_t type = type_arg;
1690 size_t bits = bits_arg;
1691 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001694 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 psa_set_key_usage_flags(&attributes, usage);
1699 psa_set_key_algorithm(&attributes, alg);
1700 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001701
1702 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001704
1705 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1707 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1708 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001709
1710 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001712 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 PSA_ASSERT(psa_destroy_key(key));
1716 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001717
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001718exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001719 /*
1720 * Key attributes may have been returned by psa_get_key_attributes()
1721 * thus reset them as required.
1722 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 psa_reset_key_attributes(&attributes);
1726 psa_destroy_key(key);
1727 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001728}
1729/* END_CASE */
1730
1731/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001732void effective_key_attributes(int type_arg, int expected_type_arg,
1733 int bits_arg, int expected_bits_arg,
1734 int usage_arg, int expected_usage_arg,
1735 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001736{
Ronald Cron5425a212020-08-04 14:58:35 +02001737 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001738 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001739 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001740 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001741 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001742 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001743 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001744 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001745 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 psa_set_key_usage_flags(&attributes, usage);
1751 psa_set_key_algorithm(&attributes, alg);
1752 psa_set_key_type(&attributes, key_type);
1753 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 PSA_ASSERT(psa_generate_key(&attributes, &key));
1756 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1759 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1760 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1761 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1762 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001763
1764exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001765 /*
1766 * Key attributes may have been returned by psa_get_key_attributes()
1767 * thus reset them as required.
1768 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 psa_destroy_key(key);
1772 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001773}
1774/* END_CASE */
1775
1776/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001777void check_key_policy(int type_arg, int bits_arg,
1778 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001779{
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1781 usage_arg,
1782 mbedtls_test_update_key_usage_flags(usage_arg),
1783 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001784 goto exit;
1785}
1786/* END_CASE */
1787
1788/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001790{
1791 /* Test each valid way of initializing the object, except for `= {0}`, as
1792 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1793 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001794 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001796 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1797 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1802 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1803 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 TEST_EQUAL(psa_get_key_type(&func), 0);
1806 TEST_EQUAL(psa_get_key_type(&init), 0);
1807 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 TEST_EQUAL(psa_get_key_bits(&func), 0);
1810 TEST_EQUAL(psa_get_key_bits(&init), 0);
1811 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1814 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1815 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1818 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1819 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001820}
1821/* END_CASE */
1822
1823/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001824void mac_key_policy(int policy_usage_arg,
1825 int policy_alg_arg,
1826 int key_type_arg,
1827 data_t *key_data,
1828 int exercise_alg_arg,
1829 int expected_status_sign_arg,
1830 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001831{
Ronald Cron5425a212020-08-04 14:58:35 +02001832 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001833 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001834 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001835 psa_key_type_t key_type = key_type_arg;
1836 psa_algorithm_t policy_alg = policy_alg_arg;
1837 psa_algorithm_t exercise_alg = exercise_alg_arg;
1838 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001839 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001840 psa_status_t expected_status_sign = expected_status_sign_arg;
1841 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001842 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 psa_set_key_usage_flags(&attributes, policy_usage);
1847 psa_set_key_algorithm(&attributes, policy_alg);
1848 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1851 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1854 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1857 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001858
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001859 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001861 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1863 input, 128,
1864 mac, PSA_MAC_MAX_SIZE, &mac_len),
1865 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001866
Neil Armstrong3af9b972022-02-07 12:20:21 +01001867 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 PSA_ASSERT(psa_mac_abort(&operation));
1869 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1870 if (status == PSA_SUCCESS) {
1871 status = psa_mac_update(&operation, input, 128);
1872 if (status == PSA_SUCCESS) {
1873 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1874 &mac_len),
1875 expected_status_sign);
1876 } else {
1877 TEST_EQUAL(status, expected_status_sign);
1878 }
1879 } else {
1880 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001881 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001883
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001884 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 status = psa_mac_verify(key, exercise_alg, input, 128,
1886 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1889 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1890 } else {
1891 TEST_EQUAL(status, expected_status_verify);
1892 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893
Neil Armstrong3af9b972022-02-07 12:20:21 +01001894 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1896 if (status == PSA_SUCCESS) {
1897 status = psa_mac_update(&operation, input, 128);
1898 if (status == PSA_SUCCESS) {
1899 status = psa_mac_verify_finish(&operation, mac, mac_len);
1900 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1901 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1902 } else {
1903 TEST_EQUAL(status, expected_status_verify);
1904 }
1905 } else {
1906 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001907 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 } else {
1909 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001910 }
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 memset(mac, 0, sizeof(mac));
1915 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1916 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001917
1918exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 psa_mac_abort(&operation);
1920 psa_destroy_key(key);
1921 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922}
1923/* END_CASE */
1924
1925/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001926void cipher_key_policy(int policy_usage_arg,
1927 int policy_alg,
1928 int key_type,
1929 data_t *key_data,
1930 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931{
Ronald Cron5425a212020-08-04 14:58:35 +02001932 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001934 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001935 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001936 size_t output_buffer_size = 0;
1937 size_t input_buffer_size = 0;
1938 size_t output_length = 0;
1939 uint8_t *output = NULL;
1940 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941 psa_status_t status;
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1944 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1945 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001946
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001947 TEST_CALLOC(input, input_buffer_size);
1948 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 psa_set_key_usage_flags(&attributes, policy_usage);
1953 psa_set_key_algorithm(&attributes, policy_alg);
1954 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1957 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001959 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 TEST_EQUAL(policy_usage,
1961 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001962
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001963 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1965 output, output_buffer_size,
1966 &output_length);
1967 if (policy_alg == exercise_alg &&
1968 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1969 PSA_ASSERT(status);
1970 } else {
1971 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1972 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001973
1974 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1976 if (policy_alg == exercise_alg &&
1977 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1978 PSA_ASSERT(status);
1979 } else {
1980 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1981 }
1982 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001984 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1986 input, input_buffer_size,
1987 &output_length);
1988 if (policy_alg == exercise_alg &&
1989 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1990 PSA_ASSERT(status);
1991 } else {
1992 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1993 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001994
1995 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1997 if (policy_alg == exercise_alg &&
1998 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1999 PSA_ASSERT(status);
2000 } else {
2001 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2002 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003
2004exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 psa_cipher_abort(&operation);
2006 mbedtls_free(input);
2007 mbedtls_free(output);
2008 psa_destroy_key(key);
2009 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002010}
2011/* END_CASE */
2012
2013/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002014void aead_key_policy(int policy_usage_arg,
2015 int policy_alg,
2016 int key_type,
2017 data_t *key_data,
2018 int nonce_length_arg,
2019 int tag_length_arg,
2020 int exercise_alg,
2021 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022{
Ronald Cron5425a212020-08-04 14:58:35 +02002023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002025 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002026 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002028 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 size_t nonce_length = nonce_length_arg;
2031 unsigned char tag[16];
2032 size_t tag_length = tag_length_arg;
2033 size_t output_length;
2034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 TEST_LE_U(nonce_length, sizeof(nonce));
2036 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 psa_set_key_usage_flags(&attributes, policy_usage);
2041 psa_set_key_algorithm(&attributes, policy_alg);
2042 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2045 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002047 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 TEST_EQUAL(policy_usage,
2049 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002050
Neil Armstrong752d8112022-02-07 14:51:11 +01002051 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 status = psa_aead_encrypt(key, exercise_alg,
2053 nonce, nonce_length,
2054 NULL, 0,
2055 NULL, 0,
2056 tag, tag_length,
2057 &output_length);
2058 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2059 TEST_EQUAL(status, expected_status);
2060 } else {
2061 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2062 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002063
Neil Armstrong752d8112022-02-07 14:51:11 +01002064 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2066 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2067 TEST_EQUAL(status, expected_status);
2068 } else {
2069 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2070 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002071
2072 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 memset(tag, 0, sizeof(tag));
2074 status = psa_aead_decrypt(key, exercise_alg,
2075 nonce, nonce_length,
2076 NULL, 0,
2077 tag, tag_length,
2078 NULL, 0,
2079 &output_length);
2080 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2081 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2082 } else if (expected_status == PSA_SUCCESS) {
2083 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2084 } else {
2085 TEST_EQUAL(status, expected_status);
2086 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087
Neil Armstrong752d8112022-02-07 14:51:11 +01002088 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 PSA_ASSERT(psa_aead_abort(&operation));
2090 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2091 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2092 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2093 } else {
2094 TEST_EQUAL(status, expected_status);
2095 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002096
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 PSA_ASSERT(psa_aead_abort(&operation));
2099 psa_destroy_key(key);
2100 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002101}
2102/* END_CASE */
2103
2104/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105void asymmetric_encryption_key_policy(int policy_usage_arg,
2106 int policy_alg,
2107 int key_type,
2108 data_t *key_data,
2109 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002110{
Ronald Cron5425a212020-08-04 14:58:35 +02002111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002113 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002114 psa_status_t status;
2115 size_t key_bits;
2116 size_t buffer_length;
2117 unsigned char *buffer = NULL;
2118 size_t output_length;
2119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 psa_set_key_usage_flags(&attributes, policy_usage);
2123 psa_set_key_algorithm(&attributes, policy_alg);
2124 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2127 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002129 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 TEST_EQUAL(policy_usage,
2131 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2134 key_bits = psa_get_key_bits(&attributes);
2135 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2136 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002137 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 status = psa_asymmetric_encrypt(key, exercise_alg,
2140 NULL, 0,
2141 NULL, 0,
2142 buffer, buffer_length,
2143 &output_length);
2144 if (policy_alg == exercise_alg &&
2145 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2146 PSA_ASSERT(status);
2147 } else {
2148 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2149 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (buffer_length != 0) {
2152 memset(buffer, 0, buffer_length);
2153 }
2154 status = psa_asymmetric_decrypt(key, exercise_alg,
2155 buffer, buffer_length,
2156 NULL, 0,
2157 buffer, buffer_length,
2158 &output_length);
2159 if (policy_alg == exercise_alg &&
2160 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2161 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2162 } else {
2163 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2164 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002165
2166exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002167 /*
2168 * Key attributes may have been returned by psa_get_key_attributes()
2169 * thus reset them as required.
2170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 psa_destroy_key(key);
2174 PSA_DONE();
2175 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002176}
2177/* END_CASE */
2178
2179/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180void asymmetric_signature_key_policy(int policy_usage_arg,
2181 int policy_alg,
2182 int key_type,
2183 data_t *key_data,
2184 int exercise_alg,
2185 int payload_length_arg,
2186 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002187{
Ronald Cron5425a212020-08-04 14:58:35 +02002188 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002190 psa_key_usage_t policy_usage = policy_usage_arg;
2191 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002192 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002194 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2195 * compatible with the policy and `payload_length_arg` is supposed to be
2196 * a valid input length to sign. If `payload_length_arg <= 0`,
2197 * `exercise_alg` is supposed to be forbidden by the policy. */
2198 int compatible_alg = payload_length_arg > 0;
2199 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002201 size_t signature_length;
2202
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002203 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002204 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 TEST_EQUAL(expected_usage,
2206 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 psa_set_key_usage_flags(&attributes, policy_usage);
2211 psa_set_key_algorithm(&attributes, policy_alg);
2212 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2215 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 status = psa_sign_hash(key, exercise_alg,
2220 payload, payload_length,
2221 signature, sizeof(signature),
2222 &signature_length);
2223 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2224 PSA_ASSERT(status);
2225 } else {
2226 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2227 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 memset(signature, 0, sizeof(signature));
2230 status = psa_verify_hash(key, exercise_alg,
2231 payload, payload_length,
2232 signature, sizeof(signature));
2233 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2234 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2235 } else {
2236 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2237 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2240 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2241 status = psa_sign_message(key, exercise_alg,
2242 payload, payload_length,
2243 signature, sizeof(signature),
2244 &signature_length);
2245 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2246 PSA_ASSERT(status);
2247 } else {
2248 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2249 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 memset(signature, 0, sizeof(signature));
2252 status = psa_verify_message(key, exercise_alg,
2253 payload, payload_length,
2254 signature, sizeof(signature));
2255 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2256 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2257 } else {
2258 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2259 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002260 }
2261
Gilles Peskined5b33222018-06-18 22:20:03 +02002262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 psa_destroy_key(key);
2264 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002265}
2266/* END_CASE */
2267
Janos Follathba3fab92019-06-11 14:50:16 +01002268/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269void derive_key_policy(int policy_usage,
2270 int policy_alg,
2271 int key_type,
2272 data_t *key_data,
2273 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002274{
Ronald Cron5425a212020-08-04 14:58:35 +02002275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002277 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002278 psa_status_t status;
2279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 psa_set_key_usage_flags(&attributes, policy_usage);
2283 psa_set_key_algorithm(&attributes, policy_alg);
2284 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2287 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2292 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2293 PSA_ASSERT(psa_key_derivation_input_bytes(
2294 &operation,
2295 PSA_KEY_DERIVATION_INPUT_SEED,
2296 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002297 }
Janos Follathba3fab92019-06-11 14:50:16 +01002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 status = psa_key_derivation_input_key(&operation,
2300 PSA_KEY_DERIVATION_INPUT_SECRET,
2301 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (policy_alg == exercise_alg &&
2304 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2305 PSA_ASSERT(status);
2306 } else {
2307 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2308 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002309
2310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 psa_key_derivation_abort(&operation);
2312 psa_destroy_key(key);
2313 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002314}
2315/* END_CASE */
2316
2317/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002318void agreement_key_policy(int policy_usage,
2319 int policy_alg,
2320 int key_type_arg,
2321 data_t *key_data,
2322 int exercise_alg,
2323 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002324{
Ronald Cron5425a212020-08-04 14:58:35 +02002325 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002327 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002328 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002329 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002330 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 psa_set_key_usage_flags(&attributes, policy_usage);
2335 psa_set_key_algorithm(&attributes, policy_alg);
2336 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2339 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2342 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002345
2346exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 psa_key_derivation_abort(&operation);
2348 psa_destroy_key(key);
2349 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002350}
2351/* END_CASE */
2352
2353/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354void key_policy_alg2(int key_type_arg, data_t *key_data,
2355 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002356{
Ronald Cron5425a212020-08-04 14:58:35 +02002357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002358 psa_key_type_t key_type = key_type_arg;
2359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2360 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2361 psa_key_usage_t usage = usage_arg;
2362 psa_algorithm_t alg = alg_arg;
2363 psa_algorithm_t alg2 = alg2_arg;
2364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 psa_set_key_usage_flags(&attributes, usage);
2368 psa_set_key_algorithm(&attributes, alg);
2369 psa_set_key_enrollment_algorithm(&attributes, alg2);
2370 psa_set_key_type(&attributes, key_type);
2371 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2372 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002373
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002374 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 usage = mbedtls_test_update_key_usage_flags(usage);
2376 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2377 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2378 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2379 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002382 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 }
2384 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002385 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002387
2388exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002389 /*
2390 * Key attributes may have been returned by psa_get_key_attributes()
2391 * thus reset them as required.
2392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 psa_destroy_key(key);
2396 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002401void raw_agreement_key_policy(int policy_usage,
2402 int policy_alg,
2403 int key_type_arg,
2404 data_t *key_data,
2405 int exercise_alg,
2406 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002407{
Ronald Cron5425a212020-08-04 14:58:35 +02002408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002410 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002411 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002412 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002413 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 psa_set_key_usage_flags(&attributes, policy_usage);
2418 psa_set_key_algorithm(&attributes, policy_alg);
2419 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2422 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002427
2428exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 psa_key_derivation_abort(&operation);
2430 psa_destroy_key(key);
2431 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002432}
2433/* END_CASE */
2434
2435/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436void copy_success(int source_usage_arg,
2437 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002438 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 int type_arg, data_t *material,
2440 int copy_attributes,
2441 int target_usage_arg,
2442 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002443 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 int expected_usage_arg,
2445 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002446{
Gilles Peskineca25db92019-04-19 11:43:08 +02002447 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2448 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002449 psa_key_usage_t expected_usage = expected_usage_arg;
2450 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002451 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302452 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2453 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002454 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2455 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002456 uint8_t *export_buffer = NULL;
2457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459
Gilles Peskineca25db92019-04-19 11:43:08 +02002460 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2462 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2463 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2464 psa_set_key_type(&source_attributes, type_arg);
2465 psa_set_key_lifetime(&source_attributes, source_lifetime);
2466 PSA_ASSERT(psa_import_key(&source_attributes,
2467 material->x, material->len,
2468 &source_key));
2469 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002470
Gilles Peskineca25db92019-04-19 11:43:08 +02002471 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002473 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002474 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (target_usage_arg != -1) {
2478 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2479 }
2480 if (target_alg_arg != -1) {
2481 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2482 }
2483 if (target_alg2_arg != -1) {
2484 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2485 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002486
Archana8a180362021-07-05 02:18:48 +05302487
Gilles Peskine57ab7212019-01-28 13:03:09 +01002488 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 PSA_ASSERT(psa_copy_key(source_key,
2490 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002491
2492 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002494
2495 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2497 TEST_EQUAL(psa_get_key_type(&source_attributes),
2498 psa_get_key_type(&target_attributes));
2499 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2500 psa_get_key_bits(&target_attributes));
2501 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2502 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2503 TEST_EQUAL(expected_alg2,
2504 psa_get_key_enrollment_algorithm(&target_attributes));
2505 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002506 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002507 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2509 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002510 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002511 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002512 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (!psa_key_lifetime_is_external(target_lifetime)) {
2515 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302516 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 }
2518 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302519 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 }
Archana8a180362021-07-05 02:18:48 +05302521 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002524
2525exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002526 /*
2527 * Source and target key attributes may have been returned by
2528 * psa_get_key_attributes() thus reset them as required.
2529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 psa_reset_key_attributes(&source_attributes);
2531 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 PSA_DONE();
2534 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002535}
2536/* END_CASE */
2537
2538/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539void copy_fail(int source_usage_arg,
2540 int source_alg_arg, int source_alg2_arg,
2541 int source_lifetime_arg,
2542 int type_arg, data_t *material,
2543 int target_type_arg, int target_bits_arg,
2544 int target_usage_arg,
2545 int target_alg_arg, int target_alg2_arg,
2546 int target_id_arg, int target_lifetime_arg,
2547 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002548{
2549 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2550 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002551 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2552 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002556
2557 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2559 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2560 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2561 psa_set_key_type(&source_attributes, type_arg);
2562 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2563 PSA_ASSERT(psa_import_key(&source_attributes,
2564 material->x, material->len,
2565 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002566
2567 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 psa_set_key_id(&target_attributes, key_id);
2569 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2570 psa_set_key_type(&target_attributes, target_type_arg);
2571 psa_set_key_bits(&target_attributes, target_bits_arg);
2572 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2573 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2574 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002575
2576 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 TEST_EQUAL(psa_copy_key(source_key,
2578 &target_attributes, &target_key),
2579 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002582
Gilles Peskine4a644642019-05-03 17:14:08 +02002583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 psa_reset_key_attributes(&source_attributes);
2585 psa_reset_key_attributes(&target_attributes);
2586 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002587}
2588/* END_CASE */
2589
2590/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002591void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002592{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002593 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002594 /* Test each valid way of initializing the object, except for `= {0}`, as
2595 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2596 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002597 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002599 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2600 psa_hash_operation_t zero;
2601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002603
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002604 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2606 PSA_ERROR_BAD_STATE);
2607 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2608 PSA_ERROR_BAD_STATE);
2609 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2610 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002611
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002612 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 PSA_ASSERT(psa_hash_abort(&func));
2614 PSA_ASSERT(psa_hash_abort(&init));
2615 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002616}
2617/* END_CASE */
2618
2619/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002620void hash_setup(int alg_arg,
2621 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002622{
2623 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002624 uint8_t *output = NULL;
2625 size_t output_size = 0;
2626 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002627 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002628 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002629 psa_status_t status;
2630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002632
Neil Armstrongedb20862022-02-07 15:47:44 +01002633 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002635 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 status = psa_hash_compute(alg, NULL, 0,
2638 output, output_size, &output_length);
2639 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002640
2641 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 status = psa_hash_setup(&operation, alg);
2643 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002644
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002645 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002647
2648 /* If setup failed, reproduce the failure, so as to
2649 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (status != PSA_SUCCESS) {
2651 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2652 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002653
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002654 /* Now the operation object should be reusable. */
2655#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2657 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002658#endif
2659
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002660exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 mbedtls_free(output);
2662 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002663}
2664/* END_CASE */
2665
2666/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002667void hash_compute_fail(int alg_arg, data_t *input,
2668 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002669{
2670 psa_algorithm_t alg = alg_arg;
2671 uint8_t *output = NULL;
2672 size_t output_size = output_size_arg;
2673 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002674 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002675 psa_status_t expected_status = expected_status_arg;
2676 psa_status_t status;
2677
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002678 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002681
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002682 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 status = psa_hash_compute(alg, input->x, input->len,
2684 output, output_size, &output_length);
2685 TEST_EQUAL(status, expected_status);
2686 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002687
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002688 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 status = psa_hash_setup(&operation, alg);
2690 if (status == PSA_SUCCESS) {
2691 status = psa_hash_update(&operation, input->x, input->len);
2692 if (status == PSA_SUCCESS) {
2693 status = psa_hash_finish(&operation, output, output_size,
2694 &output_length);
2695 if (status == PSA_SUCCESS) {
2696 TEST_LE_U(output_length, output_size);
2697 } else {
2698 TEST_EQUAL(status, expected_status);
2699 }
2700 } else {
2701 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002702 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 } else {
2704 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002705 }
2706
Gilles Peskine0a749c82019-11-28 19:33:58 +01002707exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 PSA_ASSERT(psa_hash_abort(&operation));
2709 mbedtls_free(output);
2710 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002711}
2712/* END_CASE */
2713
2714/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002715void hash_compare_fail(int alg_arg, data_t *input,
2716 data_t *reference_hash,
2717 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002718{
2719 psa_algorithm_t alg = alg_arg;
2720 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002721 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002722 psa_status_t status;
2723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002725
Neil Armstrong55a1be12022-02-07 11:23:20 +01002726 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 status = psa_hash_compare(alg, input->x, input->len,
2728 reference_hash->x, reference_hash->len);
2729 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002730
Neil Armstrong55a1be12022-02-07 11:23:20 +01002731 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 status = psa_hash_setup(&operation, alg);
2733 if (status == PSA_SUCCESS) {
2734 status = psa_hash_update(&operation, input->x, input->len);
2735 if (status == PSA_SUCCESS) {
2736 status = psa_hash_verify(&operation, reference_hash->x,
2737 reference_hash->len);
2738 TEST_EQUAL(status, expected_status);
2739 } else {
2740 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002741 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 } else {
2743 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002744 }
2745
Gilles Peskine88e08462020-01-28 20:43:00 +01002746exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 PSA_ASSERT(psa_hash_abort(&operation));
2748 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002749}
2750/* END_CASE */
2751
2752/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002753void hash_compute_compare(int alg_arg, data_t *input,
2754 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002755{
2756 psa_algorithm_t alg = alg_arg;
2757 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2758 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002759 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002760 size_t i;
2761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002763
Neil Armstrongca30a002022-02-07 11:40:23 +01002764 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2766 output, PSA_HASH_LENGTH(alg),
2767 &output_length));
2768 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002769 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002770 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002771
Neil Armstrongca30a002022-02-07 11:40:23 +01002772 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 PSA_ASSERT(psa_hash_setup(&operation, alg));
2774 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2775 PSA_ASSERT(psa_hash_finish(&operation, output,
2776 PSA_HASH_LENGTH(alg),
2777 &output_length));
2778 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002779 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002780 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002781
2782 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2784 output, sizeof(output),
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);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002789
Neil Armstrongca30a002022-02-07 11:40:23 +01002790 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 PSA_ASSERT(psa_hash_setup(&operation, alg));
2792 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2793 PSA_ASSERT(psa_hash_finish(&operation, output,
2794 sizeof(output), &output_length));
2795 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002796 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002797 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002798
2799 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2801 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002802
Neil Armstrongca30a002022-02-07 11:40:23 +01002803 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 PSA_ASSERT(psa_hash_setup(&operation, alg));
2805 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2806 PSA_ASSERT(psa_hash_verify(&operation, output,
2807 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002808
2809 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2811 output, output_length + 1),
2812 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002813
Neil Armstrongca30a002022-02-07 11:40:23 +01002814 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 PSA_ASSERT(psa_hash_setup(&operation, alg));
2816 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2817 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2818 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002819
2820 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2822 output, output_length - 1),
2823 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002824
Neil Armstrongca30a002022-02-07 11:40:23 +01002825 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 PSA_ASSERT(psa_hash_setup(&operation, alg));
2827 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2828 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2829 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002830
Gilles Peskine0a749c82019-11-28 19:33:58 +01002831 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 for (i = 0; i < output_length; i++) {
2833 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002834 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002835
2836 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2838 output, output_length),
2839 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002840
2841 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 PSA_ASSERT(psa_hash_setup(&operation, alg));
2843 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2844 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2845 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002846
Gilles Peskine0a749c82019-11-28 19:33:58 +01002847 output[i] ^= 1;
2848 }
2849
2850exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 PSA_ASSERT(psa_hash_abort(&operation));
2852 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002853}
2854/* END_CASE */
2855
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002856/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002857void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002858{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002859 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002860 unsigned char input[] = "";
2861 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002862 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002863 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2864 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2866 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002867 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002868 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002869 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002872
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002873 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 PSA_ASSERT(psa_hash_setup(&operation, alg));
2875 ASSERT_OPERATION_IS_ACTIVE(operation);
2876 TEST_EQUAL(psa_hash_setup(&operation, alg),
2877 PSA_ERROR_BAD_STATE);
2878 ASSERT_OPERATION_IS_INACTIVE(operation);
2879 PSA_ASSERT(psa_hash_abort(&operation));
2880 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002881
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002882 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2884 PSA_ERROR_BAD_STATE);
2885 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002886
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002887 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002889 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 ASSERT_OPERATION_IS_ACTIVE(operation);
2891 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2892 PSA_ERROR_BAD_STATE);
2893 ASSERT_OPERATION_IS_INACTIVE(operation);
2894 PSA_ASSERT(psa_hash_abort(&operation));
2895 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002896
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002897 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 PSA_ASSERT(psa_hash_setup(&operation, alg));
2899 PSA_ASSERT(psa_hash_finish(&operation,
2900 hash, sizeof(hash), &hash_len));
2901 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2902 PSA_ERROR_BAD_STATE);
2903 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002904
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002905 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 TEST_EQUAL(psa_hash_verify(&operation,
2907 valid_hash, sizeof(valid_hash)),
2908 PSA_ERROR_BAD_STATE);
2909 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002910
2911 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 PSA_ASSERT(psa_hash_setup(&operation, alg));
2913 PSA_ASSERT(psa_hash_finish(&operation,
2914 hash, sizeof(hash), &hash_len));
2915 TEST_EQUAL(psa_hash_verify(&operation,
2916 valid_hash, sizeof(valid_hash)),
2917 PSA_ERROR_BAD_STATE);
2918 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002919
2920 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 PSA_ASSERT(psa_hash_setup(&operation, alg));
2922 ASSERT_OPERATION_IS_ACTIVE(operation);
2923 PSA_ASSERT(psa_hash_verify(&operation,
2924 valid_hash, sizeof(valid_hash)));
2925 ASSERT_OPERATION_IS_INACTIVE(operation);
2926 TEST_EQUAL(psa_hash_verify(&operation,
2927 valid_hash, sizeof(valid_hash)),
2928 PSA_ERROR_BAD_STATE);
2929 ASSERT_OPERATION_IS_INACTIVE(operation);
2930 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002931
2932 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 TEST_EQUAL(psa_hash_finish(&operation,
2934 hash, sizeof(hash), &hash_len),
2935 PSA_ERROR_BAD_STATE);
2936 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002937
2938 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 PSA_ASSERT(psa_hash_setup(&operation, alg));
2940 PSA_ASSERT(psa_hash_finish(&operation,
2941 hash, sizeof(hash), &hash_len));
2942 TEST_EQUAL(psa_hash_finish(&operation,
2943 hash, sizeof(hash), &hash_len),
2944 PSA_ERROR_BAD_STATE);
2945 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002946
2947 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 PSA_ASSERT(psa_hash_setup(&operation, alg));
2949 PSA_ASSERT(psa_hash_verify(&operation,
2950 valid_hash, sizeof(valid_hash)));
2951 TEST_EQUAL(psa_hash_finish(&operation,
2952 hash, sizeof(hash), &hash_len),
2953 PSA_ERROR_BAD_STATE);
2954 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002955
2956exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002958}
2959/* END_CASE */
2960
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002961/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002962void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002963{
2964 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002965 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2966 * appended to it */
2967 unsigned char hash[] = {
2968 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2969 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2971 };
2972 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002973 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002976
itayzafrir27e69452018-11-01 14:26:34 +02002977 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 PSA_ASSERT(psa_hash_setup(&operation, alg));
2979 ASSERT_OPERATION_IS_ACTIVE(operation);
2980 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2981 PSA_ERROR_INVALID_SIGNATURE);
2982 ASSERT_OPERATION_IS_INACTIVE(operation);
2983 PSA_ASSERT(psa_hash_abort(&operation));
2984 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002985
itayzafrir27e69452018-11-01 14:26:34 +02002986 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 PSA_ASSERT(psa_hash_setup(&operation, alg));
2988 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2989 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002990
itayzafrir27e69452018-11-01 14:26:34 +02002991 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 PSA_ASSERT(psa_hash_setup(&operation, alg));
2993 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2994 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002995
itayzafrirec93d302018-10-18 18:01:10 +03002996exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002998}
2999/* END_CASE */
3000
Ronald Cronee414c72021-03-18 18:50:08 +01003001/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003002void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003003{
3004 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003005 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003007 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003008 size_t hash_len;
3009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003011
itayzafrir58028322018-10-25 10:22:01 +03003012 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 PSA_ASSERT(psa_hash_setup(&operation, alg));
3014 TEST_EQUAL(psa_hash_finish(&operation,
3015 hash, expected_size - 1, &hash_len),
3016 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003017
3018exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003020}
3021/* END_CASE */
3022
Ronald Cronee414c72021-03-18 18:50:08 +01003023/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003024void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003025{
3026 psa_algorithm_t alg = PSA_ALG_SHA_256;
3027 unsigned char hash[PSA_HASH_MAX_SIZE];
3028 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3029 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3030 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3031 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3032 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3033 size_t hash_len;
3034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 PSA_ASSERT(psa_crypto_init());
3036 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3039 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3040 PSA_ASSERT(psa_hash_finish(&op_finished,
3041 hash, sizeof(hash), &hash_len));
3042 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3043 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3046 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3049 PSA_ASSERT(psa_hash_finish(&op_init,
3050 hash, sizeof(hash), &hash_len));
3051 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3052 PSA_ASSERT(psa_hash_finish(&op_finished,
3053 hash, sizeof(hash), &hash_len));
3054 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3055 PSA_ASSERT(psa_hash_finish(&op_aborted,
3056 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003057
3058exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 psa_hash_abort(&op_source);
3060 psa_hash_abort(&op_init);
3061 psa_hash_abort(&op_setup);
3062 psa_hash_abort(&op_finished);
3063 psa_hash_abort(&op_aborted);
3064 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003065}
3066/* END_CASE */
3067
Ronald Cronee414c72021-03-18 18:50:08 +01003068/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003069void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003070{
3071 psa_algorithm_t alg = PSA_ALG_SHA_256;
3072 unsigned char hash[PSA_HASH_MAX_SIZE];
3073 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3074 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3075 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3076 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3077 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3078 size_t hash_len;
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3083 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3084 PSA_ASSERT(psa_hash_finish(&op_finished,
3085 hash, sizeof(hash), &hash_len));
3086 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3087 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3090 PSA_ASSERT(psa_hash_finish(&op_target,
3091 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3094 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3095 PSA_ERROR_BAD_STATE);
3096 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3097 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003098
3099exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 psa_hash_abort(&op_target);
3101 psa_hash_abort(&op_init);
3102 psa_hash_abort(&op_setup);
3103 psa_hash_abort(&op_finished);
3104 psa_hash_abort(&op_aborted);
3105 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003106}
3107/* END_CASE */
3108
itayzafrir58028322018-10-25 10:22:01 +03003109/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003110void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003111{
Jaeden Amero252ef282019-02-15 14:05:35 +00003112 const uint8_t input[1] = { 0 };
3113
Jaeden Amero769ce272019-01-04 11:48:03 +00003114 /* Test each valid way of initializing the object, except for `= {0}`, as
3115 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3116 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003117 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003119 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3120 psa_mac_operation_t zero;
3121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003123
Jaeden Amero252ef282019-02-15 14:05:35 +00003124 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 TEST_EQUAL(psa_mac_update(&func,
3126 input, sizeof(input)),
3127 PSA_ERROR_BAD_STATE);
3128 TEST_EQUAL(psa_mac_update(&init,
3129 input, sizeof(input)),
3130 PSA_ERROR_BAD_STATE);
3131 TEST_EQUAL(psa_mac_update(&zero,
3132 input, sizeof(input)),
3133 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003134
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003135 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 PSA_ASSERT(psa_mac_abort(&func));
3137 PSA_ASSERT(psa_mac_abort(&init));
3138 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003139}
3140/* END_CASE */
3141
3142/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003143void mac_setup(int key_type_arg,
3144 data_t *key,
3145 int alg_arg,
3146 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003147{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003148 psa_key_type_t key_type = key_type_arg;
3149 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003150 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003151 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003152 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3153#if defined(KNOWN_SUPPORTED_MAC_ALG)
3154 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3155#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3160 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 }
3163 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003165 /* The operation object should be reusable. */
3166#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3168 smoke_test_key_data,
3169 sizeof(smoke_test_key_data),
3170 KNOWN_SUPPORTED_MAC_ALG,
3171 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003172 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 }
3174 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003175#endif
3176
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003179}
3180/* END_CASE */
3181
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003182/* 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 +01003183void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003184{
Ronald Cron5425a212020-08-04 14:58:35 +02003185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003186 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3187 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003188 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003189 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3190 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3192 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003194 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3195 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3196 size_t sign_mac_length = 0;
3197 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3198 const uint8_t verify_mac[] = {
3199 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3200 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3202 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 PSA_ASSERT(psa_crypto_init());
3205 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3206 psa_set_key_algorithm(&attributes, alg);
3207 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003208
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3210 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003211
Jaeden Amero252ef282019-02-15 14:05:35 +00003212 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3214 PSA_ERROR_BAD_STATE);
3215 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003216
3217 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3219 &sign_mac_length),
3220 PSA_ERROR_BAD_STATE);
3221 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003222
3223 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 TEST_EQUAL(psa_mac_verify_finish(&operation,
3225 verify_mac, sizeof(verify_mac)),
3226 PSA_ERROR_BAD_STATE);
3227 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003228
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003229 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3231 ASSERT_OPERATION_IS_ACTIVE(operation);
3232 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3233 PSA_ERROR_BAD_STATE);
3234 ASSERT_OPERATION_IS_INACTIVE(operation);
3235 PSA_ASSERT(psa_mac_abort(&operation));
3236 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003237
Jaeden Amero252ef282019-02-15 14:05:35 +00003238 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3240 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3241 PSA_ASSERT(psa_mac_sign_finish(&operation,
3242 sign_mac, sizeof(sign_mac),
3243 &sign_mac_length));
3244 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3245 PSA_ERROR_BAD_STATE);
3246 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003247
3248 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3250 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3251 PSA_ASSERT(psa_mac_verify_finish(&operation,
3252 verify_mac, sizeof(verify_mac)));
3253 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3254 PSA_ERROR_BAD_STATE);
3255 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003256
3257 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3259 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3260 PSA_ASSERT(psa_mac_sign_finish(&operation,
3261 sign_mac, sizeof(sign_mac),
3262 &sign_mac_length));
3263 TEST_EQUAL(psa_mac_sign_finish(&operation,
3264 sign_mac, sizeof(sign_mac),
3265 &sign_mac_length),
3266 PSA_ERROR_BAD_STATE);
3267 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003268
3269 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3271 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3272 PSA_ASSERT(psa_mac_verify_finish(&operation,
3273 verify_mac, sizeof(verify_mac)));
3274 TEST_EQUAL(psa_mac_verify_finish(&operation,
3275 verify_mac, sizeof(verify_mac)),
3276 PSA_ERROR_BAD_STATE);
3277 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003278
3279 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3281 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3282 ASSERT_OPERATION_IS_ACTIVE(operation);
3283 TEST_EQUAL(psa_mac_verify_finish(&operation,
3284 verify_mac, sizeof(verify_mac)),
3285 PSA_ERROR_BAD_STATE);
3286 ASSERT_OPERATION_IS_INACTIVE(operation);
3287 PSA_ASSERT(psa_mac_abort(&operation));
3288 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003289
3290 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3292 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3293 ASSERT_OPERATION_IS_ACTIVE(operation);
3294 TEST_EQUAL(psa_mac_sign_finish(&operation,
3295 sign_mac, sizeof(sign_mac),
3296 &sign_mac_length),
3297 PSA_ERROR_BAD_STATE);
3298 ASSERT_OPERATION_IS_INACTIVE(operation);
3299 PSA_ASSERT(psa_mac_abort(&operation));
3300 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003301
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003303
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003304exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003306}
3307/* END_CASE */
3308
3309/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003310void mac_sign_verify_multi(int key_type_arg,
3311 data_t *key_data,
3312 int alg_arg,
3313 data_t *input,
3314 int is_verify,
3315 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003316{
3317 size_t data_part_len = 0;
3318
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003320 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003322
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 if (mac_multipart_internal_func(key_type_arg, key_data,
3324 alg_arg,
3325 input, data_part_len,
3326 expected_mac,
3327 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003328 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003330
3331 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (mac_multipart_internal_func(key_type_arg, key_data,
3335 alg_arg,
3336 input, data_part_len,
3337 expected_mac,
3338 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003339 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003341 }
3342
3343 /* Goto is required to silence warnings about unused labels, as we
3344 * don't actually do any test assertions in this function. */
3345 goto exit;
3346}
3347/* END_CASE */
3348
3349/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003350void mac_sign(int key_type_arg,
3351 data_t *key_data,
3352 int alg_arg,
3353 data_t *input,
3354 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003355{
Ronald Cron5425a212020-08-04 14:58:35 +02003356 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003357 psa_key_type_t key_type = key_type_arg;
3358 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003359 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003361 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003362 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003364 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003365 const size_t output_sizes_to_test[] = {
3366 0,
3367 1,
3368 expected_mac->len - 1,
3369 expected_mac->len,
3370 expected_mac->len + 1,
3371 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003374 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003378
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3380 psa_set_key_algorithm(&attributes, alg);
3381 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003382
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3384 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003387 const size_t output_size = output_sizes_to_test[i];
3388 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 (output_size >= expected_mac->len ? PSA_SUCCESS :
3390 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003393 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003394
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003395 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 TEST_EQUAL(psa_mac_compute(key, alg,
3397 input->x, input->len,
3398 actual_mac, output_size, &mac_length),
3399 expected_status);
3400 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003401 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003402 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003403 }
3404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 if (output_size > 0) {
3406 memset(actual_mac, 0, output_size);
3407 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003408
3409 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3411 PSA_ASSERT(psa_mac_update(&operation,
3412 input->x, input->len));
3413 TEST_EQUAL(psa_mac_sign_finish(&operation,
3414 actual_mac, output_size,
3415 &mac_length),
3416 expected_status);
3417 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003418
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003420 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003421 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003422 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003424 actual_mac = NULL;
3425 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003426
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003427exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 psa_mac_abort(&operation);
3429 psa_destroy_key(key);
3430 PSA_DONE();
3431 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003432}
3433/* END_CASE */
3434
3435/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003436void mac_verify(int key_type_arg,
3437 data_t *key_data,
3438 int alg_arg,
3439 data_t *input,
3440 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003441{
Ronald Cron5425a212020-08-04 14:58:35 +02003442 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003443 psa_key_type_t key_type = key_type_arg;
3444 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003445 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003447 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003448
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003450
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003452
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3454 psa_set_key_algorithm(&attributes, alg);
3455 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003456
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3458 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003459
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003460 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3462 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003463
3464 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3466 PSA_ASSERT(psa_mac_update(&operation,
3467 input->x, input->len));
3468 PSA_ASSERT(psa_mac_verify_finish(&operation,
3469 expected_mac->x,
3470 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003471
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003472 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 TEST_EQUAL(psa_mac_verify(key, alg,
3474 input->x, input->len,
3475 expected_mac->x,
3476 expected_mac->len - 1),
3477 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003478
3479 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3481 PSA_ASSERT(psa_mac_update(&operation,
3482 input->x, input->len));
3483 TEST_EQUAL(psa_mac_verify_finish(&operation,
3484 expected_mac->x,
3485 expected_mac->len - 1),
3486 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003487
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003488 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003489 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3491 TEST_EQUAL(psa_mac_verify(key, alg,
3492 input->x, input->len,
3493 perturbed_mac, expected_mac->len + 1),
3494 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003495
3496 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3498 PSA_ASSERT(psa_mac_update(&operation,
3499 input->x, input->len));
3500 TEST_EQUAL(psa_mac_verify_finish(&operation,
3501 perturbed_mac,
3502 expected_mac->len + 1),
3503 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003504
3505 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 for (size_t i = 0; i < expected_mac->len; i++) {
3507 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003508 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003509
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 TEST_EQUAL(psa_mac_verify(key, alg,
3511 input->x, input->len,
3512 perturbed_mac, expected_mac->len),
3513 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003514
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3516 PSA_ASSERT(psa_mac_update(&operation,
3517 input->x, input->len));
3518 TEST_EQUAL(psa_mac_verify_finish(&operation,
3519 perturbed_mac,
3520 expected_mac->len),
3521 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003522 perturbed_mac[i] ^= 1;
3523 }
3524
Gilles Peskine8c9def32018-02-08 10:02:12 +01003525exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 psa_mac_abort(&operation);
3527 psa_destroy_key(key);
3528 PSA_DONE();
3529 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003530}
3531/* END_CASE */
3532
3533/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003534void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003535{
Jaeden Ameroab439972019-02-15 14:12:05 +00003536 const uint8_t input[1] = { 0 };
3537 unsigned char output[1] = { 0 };
3538 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003539 /* Test each valid way of initializing the object, except for `= {0}`, as
3540 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3541 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003542 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003544 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3545 psa_cipher_operation_t zero;
3546
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003548
Jaeden Ameroab439972019-02-15 14:12:05 +00003549 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 TEST_EQUAL(psa_cipher_update(&func,
3551 input, sizeof(input),
3552 output, sizeof(output),
3553 &output_length),
3554 PSA_ERROR_BAD_STATE);
3555 TEST_EQUAL(psa_cipher_update(&init,
3556 input, sizeof(input),
3557 output, sizeof(output),
3558 &output_length),
3559 PSA_ERROR_BAD_STATE);
3560 TEST_EQUAL(psa_cipher_update(&zero,
3561 input, sizeof(input),
3562 output, sizeof(output),
3563 &output_length),
3564 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003565
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003566 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 PSA_ASSERT(psa_cipher_abort(&func));
3568 PSA_ASSERT(psa_cipher_abort(&init));
3569 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003570}
3571/* END_CASE */
3572
3573/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003574void cipher_setup(int key_type_arg,
3575 data_t *key,
3576 int alg_arg,
3577 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003578{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003579 psa_key_type_t key_type = key_type_arg;
3580 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003581 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003582 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003583 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003584#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003585 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3586#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003587
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003589
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3591 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003592 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 }
3594 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003595
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003596 /* The operation object should be reusable. */
3597#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3599 smoke_test_key_data,
3600 sizeof(smoke_test_key_data),
3601 KNOWN_SUPPORTED_CIPHER_ALG,
3602 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 }
3605 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003606#endif
3607
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003608exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003609 psa_cipher_abort(&operation);
3610 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003611}
3612/* END_CASE */
3613
Ronald Cronee414c72021-03-18 18:50:08 +01003614/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003615void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003616{
Ronald Cron5425a212020-08-04 14:58:35 +02003617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003618 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3619 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003620 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003621 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003622 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003623 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003624 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003625 0xaa, 0xaa, 0xaa, 0xaa
3626 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003627 const uint8_t text[] = {
3628 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003629 0xbb, 0xbb, 0xbb, 0xbb
3630 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003631 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003632 size_t length = 0;
3633
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 PSA_ASSERT(psa_crypto_init());
3635 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3636 psa_set_key_algorithm(&attributes, alg);
3637 psa_set_key_type(&attributes, key_type);
3638 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3639 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003640
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003641 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003642 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3643 ASSERT_OPERATION_IS_ACTIVE(operation);
3644 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3645 PSA_ERROR_BAD_STATE);
3646 ASSERT_OPERATION_IS_INACTIVE(operation);
3647 PSA_ASSERT(psa_cipher_abort(&operation));
3648 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003649
3650 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003651 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3652 ASSERT_OPERATION_IS_ACTIVE(operation);
3653 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3654 PSA_ERROR_BAD_STATE);
3655 ASSERT_OPERATION_IS_INACTIVE(operation);
3656 PSA_ASSERT(psa_cipher_abort(&operation));
3657 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003658
Jaeden Ameroab439972019-02-15 14:12:05 +00003659 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003660 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3661 buffer, sizeof(buffer),
3662 &length),
3663 PSA_ERROR_BAD_STATE);
3664 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003665
3666 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003667 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3668 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3669 buffer, sizeof(buffer),
3670 &length));
3671 ASSERT_OPERATION_IS_ACTIVE(operation);
3672 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3673 buffer, sizeof(buffer),
3674 &length),
3675 PSA_ERROR_BAD_STATE);
3676 ASSERT_OPERATION_IS_INACTIVE(operation);
3677 PSA_ASSERT(psa_cipher_abort(&operation));
3678 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003679
3680 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003681 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3682 PSA_ASSERT(psa_cipher_set_iv(&operation,
3683 iv, sizeof(iv)));
3684 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3685 buffer, sizeof(buffer),
3686 &length),
3687 PSA_ERROR_BAD_STATE);
3688 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003689
3690 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003691 TEST_EQUAL(psa_cipher_set_iv(&operation,
3692 iv, sizeof(iv)),
3693 PSA_ERROR_BAD_STATE);
3694 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003695
3696 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003697 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3698 PSA_ASSERT(psa_cipher_set_iv(&operation,
3699 iv, sizeof(iv)));
3700 ASSERT_OPERATION_IS_ACTIVE(operation);
3701 TEST_EQUAL(psa_cipher_set_iv(&operation,
3702 iv, sizeof(iv)),
3703 PSA_ERROR_BAD_STATE);
3704 ASSERT_OPERATION_IS_INACTIVE(operation);
3705 PSA_ASSERT(psa_cipher_abort(&operation));
3706 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003707
3708 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003709 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3710 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3711 buffer, sizeof(buffer),
3712 &length));
3713 TEST_EQUAL(psa_cipher_set_iv(&operation,
3714 iv, sizeof(iv)),
3715 PSA_ERROR_BAD_STATE);
3716 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003717
3718 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 TEST_EQUAL(psa_cipher_update(&operation,
3720 text, sizeof(text),
3721 buffer, sizeof(buffer),
3722 &length),
3723 PSA_ERROR_BAD_STATE);
3724 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003725
3726 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003727 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3728 ASSERT_OPERATION_IS_ACTIVE(operation);
3729 TEST_EQUAL(psa_cipher_update(&operation,
3730 text, sizeof(text),
3731 buffer, sizeof(buffer),
3732 &length),
3733 PSA_ERROR_BAD_STATE);
3734 ASSERT_OPERATION_IS_INACTIVE(operation);
3735 PSA_ASSERT(psa_cipher_abort(&operation));
3736 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003737
3738 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3740 PSA_ASSERT(psa_cipher_set_iv(&operation,
3741 iv, sizeof(iv)));
3742 PSA_ASSERT(psa_cipher_finish(&operation,
3743 buffer, sizeof(buffer), &length));
3744 TEST_EQUAL(psa_cipher_update(&operation,
3745 text, sizeof(text),
3746 buffer, sizeof(buffer),
3747 &length),
3748 PSA_ERROR_BAD_STATE);
3749 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003750
3751 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 TEST_EQUAL(psa_cipher_finish(&operation,
3753 buffer, sizeof(buffer), &length),
3754 PSA_ERROR_BAD_STATE);
3755 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003756
3757 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003758 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003759 /* Not calling update means we are encrypting an empty buffer, which is OK
3760 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 ASSERT_OPERATION_IS_ACTIVE(operation);
3762 TEST_EQUAL(psa_cipher_finish(&operation,
3763 buffer, sizeof(buffer), &length),
3764 PSA_ERROR_BAD_STATE);
3765 ASSERT_OPERATION_IS_INACTIVE(operation);
3766 PSA_ASSERT(psa_cipher_abort(&operation));
3767 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003768
3769 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003770 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3771 PSA_ASSERT(psa_cipher_set_iv(&operation,
3772 iv, sizeof(iv)));
3773 PSA_ASSERT(psa_cipher_finish(&operation,
3774 buffer, sizeof(buffer), &length));
3775 TEST_EQUAL(psa_cipher_finish(&operation,
3776 buffer, sizeof(buffer), &length),
3777 PSA_ERROR_BAD_STATE);
3778 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003779
Gilles Peskine449bd832023-01-11 14:50:10 +01003780 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003781
Jaeden Ameroab439972019-02-15 14:12:05 +00003782exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003783 psa_cipher_abort(&operation);
3784 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003785}
3786/* END_CASE */
3787
3788/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003789void cipher_encrypt_fail(int alg_arg,
3790 int key_type_arg,
3791 data_t *key_data,
3792 data_t *input,
3793 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003794{
Ronald Cron5425a212020-08-04 14:58:35 +02003795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003796 psa_status_t status;
3797 psa_key_type_t key_type = key_type_arg;
3798 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003799 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003800 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003801 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3802 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003803 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003804 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003805 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003806 size_t function_output_length;
3807 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003808 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3809
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 if (PSA_ERROR_BAD_STATE != expected_status) {
3811 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3814 psa_set_key_algorithm(&attributes, alg);
3815 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003816
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3818 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003819 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003820
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3822 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003823 }
3824
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003825 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3827 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003828
Gilles Peskine449bd832023-01-11 14:50:10 +01003829 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003830
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003831 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 status = psa_cipher_encrypt_setup(&operation, key, alg);
3833 if (status == PSA_SUCCESS) {
3834 if (alg != PSA_ALG_ECB_NO_PADDING) {
3835 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3836 iv, iv_size,
3837 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003838 }
3839
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 status = psa_cipher_update(&operation, input->x, input->len,
3841 output, output_buffer_size,
3842 &function_output_length);
3843 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003844 output_length += function_output_length;
3845
Gilles Peskine449bd832023-01-11 14:50:10 +01003846 status = psa_cipher_finish(&operation, output + output_length,
3847 output_buffer_size - output_length,
3848 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003849
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 TEST_EQUAL(status, expected_status);
3851 } else {
3852 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003853 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003854 } else {
3855 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003856 }
3857
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003858exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003859 psa_cipher_abort(&operation);
3860 mbedtls_free(output);
3861 psa_destroy_key(key);
3862 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003863}
3864/* END_CASE */
3865
3866/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003867void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3868 data_t *input, int iv_length,
3869 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003870{
3871 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3872 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3874 size_t output_buffer_size = 0;
3875 unsigned char *output = NULL;
3876
Gilles Peskine449bd832023-01-11 14:50:10 +01003877 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003878 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003879
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003881
Gilles Peskine449bd832023-01-11 14:50:10 +01003882 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3883 psa_set_key_algorithm(&attributes, alg);
3884 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003885
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3887 &key));
3888 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3889 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3890 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003891
3892exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 psa_cipher_abort(&operation);
3894 mbedtls_free(output);
3895 psa_destroy_key(key);
3896 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003897}
3898/* END_CASE */
3899
3900/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003901void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3902 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003903{
3904 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3905 psa_key_type_t key_type = key_type_arg;
3906 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003907 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3908 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003909 unsigned char *output = NULL;
3910 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003911 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3913
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003915
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003916 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 TEST_LE_U(ciphertext->len,
3918 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3919 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3920 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3921 TEST_LE_U(plaintext->len,
3922 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3923 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3924 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003925
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003926
3927 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 psa_set_key_usage_flags(&attributes,
3929 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3930 psa_set_key_algorithm(&attributes, alg);
3931 psa_set_key_type(&attributes, key_type);
3932 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3933 &key));
3934 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3935 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003936 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003937
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003938 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3940 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3941 PSA_ERROR_BAD_STATE);
3942 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3943 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3944 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003945
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003946 /* generate_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_generate_iv(&operation, iv, sizeof(iv),
3949 &length),
3950 PSA_ERROR_BAD_STATE);
3951 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3952 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3953 &length),
3954 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003955
Gilles Peskine286c3142022-04-20 17:09:38 +02003956 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003958 output_length = 0;
3959 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 PSA_ASSERT(psa_cipher_update(&operation,
3961 plaintext->x, plaintext->len,
3962 output, output_buffer_size,
3963 &length));
3964 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003965 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 PSA_ASSERT(psa_cipher_finish(&operation,
3967 mbedtls_buffer_offset(output, output_length),
3968 output_buffer_size - output_length,
3969 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003970 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003971 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003972 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003973
Gilles Peskine286c3142022-04-20 17:09:38 +02003974 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003976 output_length = 0;
3977 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 PSA_ASSERT(psa_cipher_update(&operation,
3979 ciphertext->x, ciphertext->len,
3980 output, output_buffer_size,
3981 &length));
3982 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003983 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 PSA_ASSERT(psa_cipher_finish(&operation,
3985 mbedtls_buffer_offset(output, output_length),
3986 output_buffer_size - output_length,
3987 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003988 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003989 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003990 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003991
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003992 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003993 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3995 output, output_buffer_size,
3996 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003997 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003998 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003999
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004000 /* One-shot decryption */
4001 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4003 output, output_buffer_size,
4004 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004005 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004006 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004007
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004008exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 PSA_ASSERT(psa_cipher_abort(&operation));
4010 mbedtls_free(output);
4011 psa_cipher_abort(&operation);
4012 psa_destroy_key(key);
4013 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004014}
4015/* END_CASE */
4016
4017/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004018void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004019{
4020 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4021 psa_algorithm_t alg = alg_arg;
4022 psa_key_type_t key_type = key_type_arg;
4023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4024 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4025 psa_status_t status;
4026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004028
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4030 psa_set_key_algorithm(&attributes, alg);
4031 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004032
4033 /* Usage of either of these two size macros would cause divide by zero
4034 * with incorrect key types previously. Input length should be irrelevant
4035 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4037 0);
4038 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004039
4040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4042 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004043
4044 /* Should fail due to invalid alg type (to support invalid key type).
4045 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004049
4050exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 psa_cipher_abort(&operation);
4052 psa_destroy_key(key);
4053 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004054}
4055/* END_CASE */
4056
4057/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004058void cipher_encrypt_validation(int alg_arg,
4059 int key_type_arg,
4060 data_t *key_data,
4061 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004062{
4063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4064 psa_key_type_t key_type = key_type_arg;
4065 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004067 unsigned char *output1 = NULL;
4068 size_t output1_buffer_size = 0;
4069 size_t output1_length = 0;
4070 unsigned char *output2 = NULL;
4071 size_t output2_buffer_size = 0;
4072 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004073 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004074 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004078
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4080 psa_set_key_algorithm(&attributes, alg);
4081 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4084 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4085 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004086 TEST_CALLOC(output1, output1_buffer_size);
4087 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004088
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4090 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004091
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004092 /* The one-shot cipher encryption uses generated iv so validating
4093 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4095 output1_buffer_size, &output1_length));
4096 TEST_LE_U(output1_length,
4097 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4098 TEST_LE_U(output1_length,
4099 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4102 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 PSA_ASSERT(psa_cipher_update(&operation,
4105 input->x, input->len,
4106 output2, output2_buffer_size,
4107 &function_output_length));
4108 TEST_LE_U(function_output_length,
4109 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4110 TEST_LE_U(function_output_length,
4111 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004112 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 PSA_ASSERT(psa_cipher_finish(&operation,
4115 output2 + output2_length,
4116 output2_buffer_size - output2_length,
4117 &function_output_length));
4118 TEST_LE_U(function_output_length,
4119 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4120 TEST_LE_U(function_output_length,
4121 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004122 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004123
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004125 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004126 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004127
Gilles Peskine50e586b2018-06-08 14:28:46 +02004128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 psa_cipher_abort(&operation);
4130 mbedtls_free(output1);
4131 mbedtls_free(output2);
4132 psa_destroy_key(key);
4133 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004134}
4135/* END_CASE */
4136
4137/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004138void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4139 data_t *key_data, data_t *iv,
4140 data_t *input,
4141 int first_part_size_arg,
4142 int output1_length_arg, int output2_length_arg,
4143 data_t *expected_output,
4144 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004145{
Ronald Cron5425a212020-08-04 14:58:35 +02004146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004147 psa_key_type_t key_type = key_type_arg;
4148 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004149 psa_status_t status;
4150 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004151 size_t first_part_size = first_part_size_arg;
4152 size_t output1_length = output1_length_arg;
4153 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004154 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004155 size_t output_buffer_size = 0;
4156 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004157 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004158 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4164 psa_set_key_algorithm(&attributes, alg);
4165 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4168 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 if (iv->len > 0) {
4173 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004174 }
4175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4177 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004178 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 TEST_LE_U(first_part_size, input->len);
4181 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4182 output, output_buffer_size,
4183 &function_output_length));
4184 TEST_ASSERT(function_output_length == output1_length);
4185 TEST_LE_U(function_output_length,
4186 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4187 TEST_LE_U(function_output_length,
4188 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004189 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 if (first_part_size < input->len) {
4192 PSA_ASSERT(psa_cipher_update(&operation,
4193 input->x + first_part_size,
4194 input->len - first_part_size,
4195 (output_buffer_size == 0 ? NULL :
4196 output + total_output_length),
4197 output_buffer_size - total_output_length,
4198 &function_output_length));
4199 TEST_ASSERT(function_output_length == output2_length);
4200 TEST_LE_U(function_output_length,
4201 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4202 alg,
4203 input->len - first_part_size));
4204 TEST_LE_U(function_output_length,
4205 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004206 total_output_length += function_output_length;
4207 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 status = psa_cipher_finish(&operation,
4210 (output_buffer_size == 0 ? NULL :
4211 output + total_output_length),
4212 output_buffer_size - total_output_length,
4213 &function_output_length);
4214 TEST_LE_U(function_output_length,
4215 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4216 TEST_LE_U(function_output_length,
4217 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004218 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 if (expected_status == PSA_SUCCESS) {
4222 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004223
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004224 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004225 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004226 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004227
4228exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 psa_cipher_abort(&operation);
4230 mbedtls_free(output);
4231 psa_destroy_key(key);
4232 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233}
4234/* END_CASE */
4235
4236/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004237void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4238 data_t *key_data, data_t *iv,
4239 data_t *input,
4240 int first_part_size_arg,
4241 int output1_length_arg, int output2_length_arg,
4242 data_t *expected_output,
4243 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004244{
Ronald Cron5425a212020-08-04 14:58:35 +02004245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004246 psa_key_type_t key_type = key_type_arg;
4247 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004248 psa_status_t status;
4249 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004250 size_t first_part_size = first_part_size_arg;
4251 size_t output1_length = output1_length_arg;
4252 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004253 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004254 size_t output_buffer_size = 0;
4255 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004256 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004257 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4263 psa_set_key_algorithm(&attributes, alg);
4264 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4267 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004270
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 if (iv->len > 0) {
4272 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004273 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4276 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004277 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 TEST_LE_U(first_part_size, input->len);
4280 PSA_ASSERT(psa_cipher_update(&operation,
4281 input->x, first_part_size,
4282 output, output_buffer_size,
4283 &function_output_length));
4284 TEST_ASSERT(function_output_length == output1_length);
4285 TEST_LE_U(function_output_length,
4286 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4287 TEST_LE_U(function_output_length,
4288 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004289 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 if (first_part_size < input->len) {
4292 PSA_ASSERT(psa_cipher_update(&operation,
4293 input->x + first_part_size,
4294 input->len - first_part_size,
4295 (output_buffer_size == 0 ? NULL :
4296 output + total_output_length),
4297 output_buffer_size - total_output_length,
4298 &function_output_length));
4299 TEST_ASSERT(function_output_length == output2_length);
4300 TEST_LE_U(function_output_length,
4301 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4302 alg,
4303 input->len - first_part_size));
4304 TEST_LE_U(function_output_length,
4305 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004306 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004307 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 status = psa_cipher_finish(&operation,
4310 (output_buffer_size == 0 ? NULL :
4311 output + total_output_length),
4312 output_buffer_size - total_output_length,
4313 &function_output_length);
4314 TEST_LE_U(function_output_length,
4315 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4316 TEST_LE_U(function_output_length,
4317 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004318 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (expected_status == PSA_SUCCESS) {
4322 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004323
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004324 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004325 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004326 }
4327
Gilles Peskine50e586b2018-06-08 14:28:46 +02004328exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 psa_cipher_abort(&operation);
4330 mbedtls_free(output);
4331 psa_destroy_key(key);
4332 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004333}
4334/* END_CASE */
4335
Gilles Peskine50e586b2018-06-08 14:28:46 +02004336/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004337void cipher_decrypt_fail(int alg_arg,
4338 int key_type_arg,
4339 data_t *key_data,
4340 data_t *iv,
4341 data_t *input_arg,
4342 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004343{
4344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4345 psa_status_t status;
4346 psa_key_type_t key_type = key_type_arg;
4347 psa_algorithm_t alg = alg_arg;
4348 psa_status_t expected_status = expected_status_arg;
4349 unsigned char *input = NULL;
4350 size_t input_buffer_size = 0;
4351 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004352 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004353 size_t output_buffer_size = 0;
4354 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004355 size_t function_output_length;
4356 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (PSA_ERROR_BAD_STATE != expected_status) {
4360 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4363 psa_set_key_algorithm(&attributes, alg);
4364 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4367 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004368 }
4369
4370 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4372 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004373 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 memcpy(input, iv->x, iv->len);
4375 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004376 }
4377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004379 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004380
Neil Armstrong66a479f2022-02-07 15:41:19 +01004381 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4383 output_buffer_size, &output_length);
4384 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004385
Neil Armstrong66a479f2022-02-07 15:41:19 +01004386 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 status = psa_cipher_decrypt_setup(&operation, key, alg);
4388 if (status == PSA_SUCCESS) {
4389 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4390 input_arg->len) +
4391 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004392 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004393
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 if (iv->len > 0) {
4395 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 if (status != PSA_SUCCESS) {
4398 TEST_EQUAL(status, expected_status);
4399 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004400 }
4401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 if (status == PSA_SUCCESS) {
4403 status = psa_cipher_update(&operation,
4404 input_arg->x, input_arg->len,
4405 output_multi, output_buffer_size,
4406 &function_output_length);
4407 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004408 output_length = function_output_length;
4409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 status = psa_cipher_finish(&operation,
4411 output_multi + output_length,
4412 output_buffer_size - output_length,
4413 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 TEST_EQUAL(status, expected_status);
4416 } else {
4417 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004418 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 } else {
4420 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004421 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 } else {
4423 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004424 }
4425
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 psa_cipher_abort(&operation);
4428 mbedtls_free(input);
4429 mbedtls_free(output);
4430 mbedtls_free(output_multi);
4431 psa_destroy_key(key);
4432 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004433}
4434/* END_CASE */
4435
4436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004437void cipher_decrypt(int alg_arg,
4438 int key_type_arg,
4439 data_t *key_data,
4440 data_t *iv,
4441 data_t *input_arg,
4442 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004443{
4444 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4445 psa_key_type_t key_type = key_type_arg;
4446 psa_algorithm_t alg = alg_arg;
4447 unsigned char *input = NULL;
4448 size_t input_buffer_size = 0;
4449 unsigned char *output = NULL;
4450 size_t output_buffer_size = 0;
4451 size_t output_length = 0;
4452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4457 psa_set_key_algorithm(&attributes, alg);
4458 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004459
4460 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4462 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004463 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 memcpy(input, iv->x, iv->len);
4465 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004466 }
4467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004469 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4472 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004473
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4475 output_buffer_size, &output_length));
4476 TEST_LE_U(output_length,
4477 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4478 TEST_LE_U(output_length,
4479 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004480
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004481 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004482 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004483exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 mbedtls_free(input);
4485 mbedtls_free(output);
4486 psa_destroy_key(key);
4487 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004488}
4489/* END_CASE */
4490
4491/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004492void cipher_verify_output(int alg_arg,
4493 int key_type_arg,
4494 data_t *key_data,
4495 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004496{
Ronald Cron5425a212020-08-04 14:58:35 +02004497 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004498 psa_key_type_t key_type = key_type_arg;
4499 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004500 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004501 size_t output1_size = 0;
4502 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004503 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004504 size_t output2_size = 0;
4505 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004507
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4511 psa_set_key_algorithm(&attributes, alg);
4512 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4515 &key));
4516 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004517 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4520 output1, output1_size,
4521 &output1_length));
4522 TEST_LE_U(output1_length,
4523 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4524 TEST_LE_U(output1_length,
4525 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004526
4527 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004528 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004529
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4531 output2, output2_size,
4532 &output2_length));
4533 TEST_LE_U(output2_length,
4534 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4535 TEST_LE_U(output2_length,
4536 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004537
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004538 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004539
4540exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 mbedtls_free(output1);
4542 mbedtls_free(output2);
4543 psa_destroy_key(key);
4544 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004545}
4546/* END_CASE */
4547
4548/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004549void cipher_verify_output_multipart(int alg_arg,
4550 int key_type_arg,
4551 data_t *key_data,
4552 data_t *input,
4553 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004554{
Ronald Cron5425a212020-08-04 14:58:35 +02004555 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004556 psa_key_type_t key_type = key_type_arg;
4557 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004558 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004560 size_t iv_size = 16;
4561 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004562 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004563 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004564 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004565 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004566 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004567 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004568 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004569 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4570 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4576 psa_set_key_algorithm(&attributes, alg);
4577 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4580 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4583 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if (alg != PSA_ALG_ECB_NO_PADDING) {
4586 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4587 iv, iv_size,
4588 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004589 }
4590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4592 TEST_LE_U(output1_buffer_size,
4593 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004594 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4599 output1, output1_buffer_size,
4600 &function_output_length));
4601 TEST_LE_U(function_output_length,
4602 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4603 TEST_LE_U(function_output_length,
4604 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004605 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 PSA_ASSERT(psa_cipher_update(&operation1,
4608 input->x + first_part_size,
4609 input->len - first_part_size,
4610 output1, output1_buffer_size,
4611 &function_output_length));
4612 TEST_LE_U(function_output_length,
4613 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4614 alg,
4615 input->len - first_part_size));
4616 TEST_LE_U(function_output_length,
4617 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004618 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 PSA_ASSERT(psa_cipher_finish(&operation1,
4621 output1 + output1_length,
4622 output1_buffer_size - output1_length,
4623 &function_output_length));
4624 TEST_LE_U(function_output_length,
4625 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4626 TEST_LE_U(function_output_length,
4627 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004628 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004631
Gilles Peskine048b7f02018-06-08 14:20:49 +02004632 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 TEST_LE_U(output2_buffer_size,
4634 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4635 TEST_LE_U(output2_buffer_size,
4636 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004637 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 if (iv_length > 0) {
4640 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4641 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004642 }
Moran Pekerded84402018-06-06 16:36:50 +03004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4645 output2, output2_buffer_size,
4646 &function_output_length));
4647 TEST_LE_U(function_output_length,
4648 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4649 TEST_LE_U(function_output_length,
4650 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004651 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 PSA_ASSERT(psa_cipher_update(&operation2,
4654 output1 + first_part_size,
4655 output1_length - first_part_size,
4656 output2, output2_buffer_size,
4657 &function_output_length));
4658 TEST_LE_U(function_output_length,
4659 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4660 alg,
4661 output1_length - first_part_size));
4662 TEST_LE_U(function_output_length,
4663 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004664 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 PSA_ASSERT(psa_cipher_finish(&operation2,
4667 output2 + output2_length,
4668 output2_buffer_size - output2_length,
4669 &function_output_length));
4670 TEST_LE_U(function_output_length,
4671 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4672 TEST_LE_U(function_output_length,
4673 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004674 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004677
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004678 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004679
4680exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 psa_cipher_abort(&operation1);
4682 psa_cipher_abort(&operation2);
4683 mbedtls_free(output1);
4684 mbedtls_free(output2);
4685 psa_destroy_key(key);
4686 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004687}
4688/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004689
Gilles Peskine20035e32018-02-03 22:44:14 +01004690/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004691void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4692 int alg_arg,
4693 data_t *nonce,
4694 data_t *additional_data,
4695 data_t *input_data,
4696 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004697{
Ronald Cron5425a212020-08-04 14:58:35 +02004698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004699 psa_key_type_t key_type = key_type_arg;
4700 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004701 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004702 unsigned char *output_data = NULL;
4703 size_t output_size = 0;
4704 size_t output_length = 0;
4705 unsigned char *output_data2 = NULL;
4706 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004707 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004708 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004712
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4714 psa_set_key_algorithm(&attributes, alg);
4715 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4718 &key));
4719 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4720 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4723 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004724 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4725 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4727 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4728 TEST_EQUAL(output_size,
4729 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4730 TEST_LE_U(output_size,
4731 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004732 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004733 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 status = psa_aead_encrypt(key, alg,
4736 nonce->x, nonce->len,
4737 additional_data->x,
4738 additional_data->len,
4739 input_data->x, input_data->len,
4740 output_data, output_size,
4741 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004742
4743 /* If the operation is not supported, just skip and not fail in case the
4744 * encryption involves a common limitation of cryptography hardwares and
4745 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 if (status == PSA_ERROR_NOT_SUPPORTED) {
4747 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4748 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004749 }
4750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004754 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004755
Gilles Peskine003a4a92019-05-14 16:09:40 +02004756 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4757 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 TEST_EQUAL(input_data->len,
4759 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 TEST_LE_U(input_data->len,
4762 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 TEST_EQUAL(psa_aead_decrypt(key, alg,
4765 nonce->x, nonce->len,
4766 additional_data->x,
4767 additional_data->len,
4768 output_data, output_length,
4769 output_data2, output_length,
4770 &output_length2),
4771 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004772
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004773 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004774 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004775 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004776
Gilles Peskinea1cac842018-06-11 19:33:02 +02004777exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 psa_destroy_key(key);
4779 mbedtls_free(output_data);
4780 mbedtls_free(output_data2);
4781 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004782}
4783/* END_CASE */
4784
4785/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004786void aead_encrypt(int key_type_arg, data_t *key_data,
4787 int alg_arg,
4788 data_t *nonce,
4789 data_t *additional_data,
4790 data_t *input_data,
4791 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004792{
Ronald Cron5425a212020-08-04 14:58:35 +02004793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794 psa_key_type_t key_type = key_type_arg;
4795 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004796 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004797 unsigned char *output_data = NULL;
4798 size_t output_size = 0;
4799 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004801 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4806 psa_set_key_algorithm(&attributes, alg);
4807 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4810 &key));
4811 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4812 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4815 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004816 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4817 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 TEST_EQUAL(output_size,
4819 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4820 TEST_LE_U(output_size,
4821 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004822 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 status = psa_aead_encrypt(key, alg,
4825 nonce->x, nonce->len,
4826 additional_data->x, additional_data->len,
4827 input_data->x, input_data->len,
4828 output_data, output_size,
4829 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004830
Ronald Cron28a45ed2021-02-09 20:35:42 +01004831 /* If the operation is not supported, just skip and not fail in case the
4832 * encryption involves a common limitation of cryptography hardwares and
4833 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (status == PSA_ERROR_NOT_SUPPORTED) {
4835 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4836 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004837 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004840 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004841 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004842
Gilles Peskinea1cac842018-06-11 19:33:02 +02004843exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 psa_destroy_key(key);
4845 mbedtls_free(output_data);
4846 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004847}
4848/* END_CASE */
4849
4850/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004851void aead_decrypt(int key_type_arg, data_t *key_data,
4852 int alg_arg,
4853 data_t *nonce,
4854 data_t *additional_data,
4855 data_t *input_data,
4856 data_t *expected_data,
4857 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004858{
Ronald Cron5425a212020-08-04 14:58:35 +02004859 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004860 psa_key_type_t key_type = key_type_arg;
4861 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004862 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004863 unsigned char *output_data = NULL;
4864 size_t output_size = 0;
4865 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004866 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004867 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004868 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4873 psa_set_key_algorithm(&attributes, alg);
4874 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4877 &key));
4878 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4879 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4882 alg);
4883 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4884 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004885 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4886 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 TEST_EQUAL(output_size,
4888 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4889 TEST_LE_U(output_size,
4890 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004891 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004892 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 status = psa_aead_decrypt(key, alg,
4895 nonce->x, nonce->len,
4896 additional_data->x,
4897 additional_data->len,
4898 input_data->x, input_data->len,
4899 output_data, output_size,
4900 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004901
Ronald Cron28a45ed2021-02-09 20:35:42 +01004902 /* If the operation is not supported, just skip and not fail in case the
4903 * decryption involves a common limitation of cryptography hardwares and
4904 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (status == PSA_ERROR_NOT_SUPPORTED) {
4906 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4907 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004908 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004913 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004914 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004916
Gilles Peskinea1cac842018-06-11 19:33:02 +02004917exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 psa_destroy_key(key);
4919 mbedtls_free(output_data);
4920 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004921}
4922/* END_CASE */
4923
4924/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004925void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4926 int alg_arg,
4927 data_t *nonce,
4928 data_t *additional_data,
4929 data_t *input_data,
4930 int do_set_lengths,
4931 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004932{
Paul Elliottd3f82412021-06-16 16:52:21 +01004933 size_t ad_part_len = 0;
4934 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004935 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004936
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4938 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 if (do_set_lengths) {
4941 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004942 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004944 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004946 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004947
4948 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 if (!aead_multipart_internal_func(key_type_arg, key_data,
4950 alg_arg, nonce,
4951 additional_data,
4952 ad_part_len,
4953 input_data, -1,
4954 set_lengths_method,
4955 expected_output,
4956 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004957 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004958 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 /* length(0) part, length(ad_part_len) part, length(0) part... */
4961 mbedtls_test_set_step(1000 + ad_part_len);
4962
4963 if (!aead_multipart_internal_func(key_type_arg, key_data,
4964 alg_arg, nonce,
4965 additional_data,
4966 ad_part_len,
4967 input_data, -1,
4968 set_lengths_method,
4969 expected_output,
4970 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004971 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 }
4973 }
4974
4975 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4976 /* Split data into length(data_part_len) parts. */
4977 mbedtls_test_set_step(2000 + data_part_len);
4978
4979 if (do_set_lengths) {
4980 if (data_part_len & 0x01) {
4981 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4982 } else {
4983 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4984 }
4985 }
4986
4987 if (!aead_multipart_internal_func(key_type_arg, key_data,
4988 alg_arg, nonce,
4989 additional_data, -1,
4990 input_data, data_part_len,
4991 set_lengths_method,
4992 expected_output,
4993 1, 0)) {
4994 break;
4995 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004996
4997 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 if (!aead_multipart_internal_func(key_type_arg, key_data,
5001 alg_arg, nonce,
5002 additional_data, -1,
5003 input_data, data_part_len,
5004 set_lengths_method,
5005 expected_output,
5006 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005007 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005009 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005010
Paul Elliott8fc45162021-06-23 16:06:01 +01005011 /* Goto is required to silence warnings about unused labels, as we
5012 * don't actually do any test assertions in this function. */
5013 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005014}
5015/* END_CASE */
5016
5017/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005018void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5019 int alg_arg,
5020 data_t *nonce,
5021 data_t *additional_data,
5022 data_t *input_data,
5023 int do_set_lengths,
5024 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005025{
Paul Elliottd3f82412021-06-16 16:52:21 +01005026 size_t ad_part_len = 0;
5027 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005028 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005033
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 if (do_set_lengths) {
5035 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005036 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005038 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005040 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 if (!aead_multipart_internal_func(key_type_arg, key_data,
5043 alg_arg, nonce,
5044 additional_data,
5045 ad_part_len,
5046 input_data, -1,
5047 set_lengths_method,
5048 expected_output,
5049 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005050 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005052
5053 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 if (!aead_multipart_internal_func(key_type_arg, key_data,
5057 alg_arg, nonce,
5058 additional_data,
5059 ad_part_len,
5060 input_data, -1,
5061 set_lengths_method,
5062 expected_output,
5063 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005064 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005066 }
5067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005069 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 if (do_set_lengths) {
5073 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005074 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005076 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005078 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 if (!aead_multipart_internal_func(key_type_arg, key_data,
5081 alg_arg, nonce,
5082 additional_data, -1,
5083 input_data, data_part_len,
5084 set_lengths_method,
5085 expected_output,
5086 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005087 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005089
5090 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005092
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (!aead_multipart_internal_func(key_type_arg, key_data,
5094 alg_arg, nonce,
5095 additional_data, -1,
5096 input_data, data_part_len,
5097 set_lengths_method,
5098 expected_output,
5099 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005100 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005102 }
5103
Paul Elliott8fc45162021-06-23 16:06:01 +01005104 /* Goto is required to silence warnings about unused labels, as we
5105 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005106 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005107}
5108/* END_CASE */
5109
5110/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005111void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5112 int alg_arg,
5113 int nonce_length,
5114 int expected_nonce_length_arg,
5115 data_t *additional_data,
5116 data_t *input_data,
5117 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005118{
5119
5120 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5121 psa_key_type_t key_type = key_type_arg;
5122 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005123 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005124 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5125 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5126 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005127 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005128 size_t actual_nonce_length = 0;
5129 size_t expected_nonce_length = expected_nonce_length_arg;
5130 unsigned char *output = NULL;
5131 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005132 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005133 size_t ciphertext_size = 0;
5134 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005135 size_t tag_length = 0;
5136 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5141 psa_set_key_algorithm(&attributes, alg);
5142 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5145 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005150
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005151 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005156
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005157 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005160
5161 /* If the operation is not supported, just skip and not fail in case the
5162 * encryption involves a common limitation of cryptography hardwares and
5163 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (status == PSA_ERROR_NOT_SUPPORTED) {
5165 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5166 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005167 }
5168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5172 nonce_length,
5173 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005174
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005176
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005178
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 if (expected_status == PSA_SUCCESS) {
5180 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5181 alg));
5182 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005185
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005187 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5189 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005190
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5192 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5195 output, output_size,
5196 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005197
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5199 &ciphertext_length, tag_buffer,
5200 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005201 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005202
5203exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 psa_destroy_key(key);
5205 mbedtls_free(output);
5206 mbedtls_free(ciphertext);
5207 psa_aead_abort(&operation);
5208 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005209}
5210/* END_CASE */
5211
5212/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005213void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5214 int alg_arg,
5215 int nonce_length_arg,
5216 int set_lengths_method_arg,
5217 data_t *additional_data,
5218 data_t *input_data,
5219 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005220{
5221
5222 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5223 psa_key_type_t key_type = key_type_arg;
5224 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005225 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005226 uint8_t *nonce_buffer = NULL;
5227 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5228 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5229 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005230 unsigned char *output = NULL;
5231 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005232 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005233 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005234 size_t ciphertext_size = 0;
5235 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005236 size_t tag_length = 0;
5237 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005238 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005239 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005242
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5244 psa_set_key_algorithm(&attributes, alg);
5245 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005246
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5248 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005253
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005254 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005257
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005259
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005260 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005261
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005263
5264 /* If the operation is not supported, just skip and not fail in case the
5265 * encryption involves a common limitation of cryptography hardwares and
5266 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (status == PSA_ERROR_NOT_SUPPORTED) {
5268 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5269 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005270 }
5271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005273
Paul Elliott4023ffd2021-09-10 16:21:22 +01005274 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 if (nonce_length_arg == -1) {
5276 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005277 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 nonce_length = 0;
5279 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005280 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005282 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 if (nonce_buffer) {
5285 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005286 nonce_buffer[index] = 'a' + index;
5287 }
Paul Elliott66696b52021-08-16 18:42:41 +01005288 }
Paul Elliott863864a2021-07-23 17:28:31 +01005289 }
5290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5292 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5293 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005294 }
5295
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005297
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 if (expected_status == PSA_SUCCESS) {
5301 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5302 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5303 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005304 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005306 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 }
Paul Elliott863864a2021-07-23 17:28:31 +01005308
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005309 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5311 additional_data->len),
5312 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005313
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5315 output, output_size,
5316 &ciphertext_length),
5317 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5320 &ciphertext_length, tag_buffer,
5321 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5322 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005323 }
5324
5325exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 psa_destroy_key(key);
5327 mbedtls_free(output);
5328 mbedtls_free(ciphertext);
5329 mbedtls_free(nonce_buffer);
5330 psa_aead_abort(&operation);
5331 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005332}
5333/* END_CASE */
5334
5335/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005336void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005337 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005338 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005339 data_t *nonce,
5340 data_t *additional_data,
5341 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005343{
5344
5345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5346 psa_key_type_t key_type = key_type_arg;
5347 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005348 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5350 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5351 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005352 unsigned char *output = NULL;
5353 unsigned char *ciphertext = NULL;
5354 size_t output_size = output_size_arg;
5355 size_t ciphertext_size = 0;
5356 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005357 size_t tag_length = 0;
5358 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005361
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5363 psa_set_key_algorithm(&attributes, alg);
5364 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005365
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5367 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005370
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005371 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005374
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005375 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005378
5379 /* If the operation is not supported, just skip and not fail in case the
5380 * encryption involves a common limitation of cryptography hardwares and
5381 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 if (status == PSA_ERROR_NOT_SUPPORTED) {
5383 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5384 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005385 }
5386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5390 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5395 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005396
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 status = psa_aead_update(&operation, input_data->x, input_data->len,
5398 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005403 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5405 &ciphertext_length, tag_buffer,
5406 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005407 }
5408
5409exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 psa_destroy_key(key);
5411 mbedtls_free(output);
5412 mbedtls_free(ciphertext);
5413 psa_aead_abort(&operation);
5414 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005415}
5416/* END_CASE */
5417
Paul Elliott91b021e2021-07-23 18:52:31 +01005418/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005419void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5420 int alg_arg,
5421 int finish_ciphertext_size_arg,
5422 int tag_size_arg,
5423 data_t *nonce,
5424 data_t *additional_data,
5425 data_t *input_data,
5426 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005427{
5428
5429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5430 psa_key_type_t key_type = key_type_arg;
5431 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005432 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5434 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5435 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005436 unsigned char *ciphertext = NULL;
5437 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005438 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005439 size_t ciphertext_size = 0;
5440 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5442 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005443 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5448 psa_set_key_algorithm(&attributes, alg);
5449 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5452 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005457
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005458 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005459
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005460 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005461
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005462 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005465
5466 /* If the operation is not supported, just skip and not fail in case the
5467 * encryption involves a common limitation of cryptography hardwares and
5468 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 if (status == PSA_ERROR_NOT_SUPPORTED) {
5470 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5471 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005472 }
5473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5479 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005480
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5482 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5485 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005486
5487 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 status = psa_aead_finish(&operation, finish_ciphertext,
5489 finish_ciphertext_size,
5490 &ciphertext_length, tag_buffer,
5491 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005492
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005494
5495exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 psa_destroy_key(key);
5497 mbedtls_free(ciphertext);
5498 mbedtls_free(finish_ciphertext);
5499 mbedtls_free(tag_buffer);
5500 psa_aead_abort(&operation);
5501 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005502}
5503/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005504
5505/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005506void aead_multipart_verify(int key_type_arg, data_t *key_data,
5507 int alg_arg,
5508 data_t *nonce,
5509 data_t *additional_data,
5510 data_t *input_data,
5511 data_t *tag,
5512 int tag_usage_arg,
5513 int expected_setup_status_arg,
5514 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005515{
5516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5517 psa_key_type_t key_type = key_type_arg;
5518 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005519 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5521 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5522 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005523 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005524 unsigned char *plaintext = NULL;
5525 unsigned char *finish_plaintext = NULL;
5526 size_t plaintext_size = 0;
5527 size_t plaintext_length = 0;
5528 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005529 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005530 unsigned char *tag_buffer = NULL;
5531 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5536 psa_set_key_algorithm(&attributes, alg);
5537 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5540 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5545 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005546
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005547 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005548
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005550
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005551 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005554
5555 /* If the operation is not supported, just skip and not fail in case the
5556 * encryption involves a common limitation of cryptography hardwares and
5557 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 if (status == PSA_ERROR_NOT_SUPPORTED) {
5559 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5560 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005561 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005565 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 }
Paul Elliott9961a662021-09-17 19:19:02 +01005567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 status = psa_aead_set_lengths(&operation, additional_data->len,
5573 input_data->len);
5574 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5577 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5580 input_data->len,
5581 plaintext, plaintext_size,
5582 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005585 tag_buffer = tag->x;
5586 tag_size = tag->len;
5587 }
5588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 status = psa_aead_verify(&operation, finish_plaintext,
5590 verify_plaintext_size,
5591 &plaintext_length,
5592 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005595
5596exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 psa_destroy_key(key);
5598 mbedtls_free(plaintext);
5599 mbedtls_free(finish_plaintext);
5600 psa_aead_abort(&operation);
5601 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005602}
5603/* END_CASE */
5604
Paul Elliott9961a662021-09-17 19:19:02 +01005605/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005606void aead_multipart_setup(int key_type_arg, data_t *key_data,
5607 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005608{
5609 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5610 psa_key_type_t key_type = key_type_arg;
5611 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005612 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5614 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5615 psa_status_t expected_status = expected_status_arg;
5616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005618
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 psa_set_key_usage_flags(&attributes,
5620 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5621 psa_set_key_algorithm(&attributes, alg);
5622 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005623
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5625 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005630
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005632
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005634
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005636
5637exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 psa_destroy_key(key);
5639 psa_aead_abort(&operation);
5640 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005641}
5642/* END_CASE */
5643
5644/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005645void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5646 int alg_arg,
5647 data_t *nonce,
5648 data_t *additional_data,
5649 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005650{
5651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5652 psa_key_type_t key_type = key_type_arg;
5653 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005654 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005655 unsigned char *output_data = NULL;
5656 unsigned char *final_data = NULL;
5657 size_t output_size = 0;
5658 size_t finish_output_size = 0;
5659 size_t output_length = 0;
5660 size_t key_bits = 0;
5661 size_t tag_length = 0;
5662 size_t tag_size = 0;
5663 size_t nonce_length = 0;
5664 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5665 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5666 size_t output_part_length = 0;
5667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5668
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 psa_set_key_usage_flags(&attributes,
5672 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5673 psa_set_key_algorithm(&attributes, alg);
5674 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5677 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5680 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005688 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005693
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005694 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005695
5696 /* Test all operations error without calling setup first. */
5697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5699 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005702
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5704 PSA_AEAD_NONCE_MAX_SIZE,
5705 &nonce_length),
5706 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005709
Paul Elliott481be342021-07-16 17:38:47 +01005710 /* ------------------------------------------------------- */
5711
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5713 input_data->len),
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_update_ad(&operation, additional_data->x,
5721 additional_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(&operation, input_data->x,
5729 input_data->len, output_data,
5730 output_size, &output_length),
5731 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005734
Paul Elliott481be342021-07-16 17:38:47 +01005735 /* ------------------------------------------------------- */
5736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5738 finish_output_size,
5739 &output_part_length,
5740 tag_buffer, tag_length,
5741 &tag_size),
5742 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005745
Paul Elliott481be342021-07-16 17:38:47 +01005746 /* ------------------------------------------------------- */
5747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5749 finish_output_size,
5750 &output_part_length,
5751 tag_buffer,
5752 tag_length),
5753 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756
5757 /* Test for double setups. */
5758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5762 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005765
Paul Elliott481be342021-07-16 17:38:47 +01005766 /* ------------------------------------------------------- */
5767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5771 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774
Paul Elliott374a2be2021-07-16 17:53:40 +01005775 /* ------------------------------------------------------- */
5776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5780 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005783
5784 /* ------------------------------------------------------- */
5785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5789 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005792
Paul Elliottc23a9a02021-06-21 18:32:46 +01005793 /* Test for not setting a nonce. */
5794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005796
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5798 additional_data->len),
5799 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005802
Paul Elliott7f628422021-09-01 12:08:29 +01005803 /* ------------------------------------------------------- */
5804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5808 input_data->len, output_data,
5809 output_size, &output_length),
5810 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005813
Paul Elliottbdc2c682021-09-21 18:37:10 +01005814 /* ------------------------------------------------------- */
5815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5819 finish_output_size,
5820 &output_part_length,
5821 tag_buffer, tag_length,
5822 &tag_size),
5823 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005826
5827 /* ------------------------------------------------------- */
5828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5832 finish_output_size,
5833 &output_part_length,
5834 tag_buffer,
5835 tag_length),
5836 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005839
Paul Elliottc23a9a02021-06-21 18:32:46 +01005840 /* Test for double setting nonce. */
5841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5847 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005850
Paul Elliott374a2be2021-07-16 17:53:40 +01005851 /* Test for double generating nonce. */
5852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5856 PSA_AEAD_NONCE_MAX_SIZE,
5857 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5860 PSA_AEAD_NONCE_MAX_SIZE,
5861 &nonce_length),
5862 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005863
5864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005866
5867 /* Test for generate nonce then set and vice versa */
5868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5872 PSA_AEAD_NONCE_MAX_SIZE,
5873 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5876 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005879
Andrzej Kurekad837522021-12-15 15:28:49 +01005880 /* Test for generating nonce after calling set lengths */
5881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5885 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5888 PSA_AEAD_NONCE_MAX_SIZE,
5889 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005892
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005893 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005896
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 if (operation.alg == PSA_ALG_CCM) {
5898 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5899 input_data->len),
5900 PSA_ERROR_INVALID_ARGUMENT);
5901 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5902 PSA_AEAD_NONCE_MAX_SIZE,
5903 &nonce_length),
5904 PSA_ERROR_BAD_STATE);
5905 } else {
5906 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5907 input_data->len));
5908 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5909 PSA_AEAD_NONCE_MAX_SIZE,
5910 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005911 }
5912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005914
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005915 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005916#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005918
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5920 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5921 input_data->len),
5922 PSA_ERROR_INVALID_ARGUMENT);
5923 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5924 PSA_AEAD_NONCE_MAX_SIZE,
5925 &nonce_length),
5926 PSA_ERROR_BAD_STATE);
5927 } else {
5928 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5929 input_data->len));
5930 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5931 PSA_AEAD_NONCE_MAX_SIZE,
5932 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005933 }
5934
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005936#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005937
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005938 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005941
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5943 PSA_AEAD_NONCE_MAX_SIZE,
5944 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005945
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 if (operation.alg == PSA_ALG_CCM) {
5947 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5948 input_data->len),
5949 PSA_ERROR_INVALID_ARGUMENT);
5950 } else {
5951 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5952 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005953 }
5954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005956
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005957 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005958 /* Test for setting nonce after calling set lengths */
5959
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5963 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005968
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005969 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 if (operation.alg == PSA_ALG_CCM) {
5974 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5975 input_data->len),
5976 PSA_ERROR_INVALID_ARGUMENT);
5977 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5978 PSA_ERROR_BAD_STATE);
5979 } else {
5980 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5981 input_data->len));
5982 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005983 }
5984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005986
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005987 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005988#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5992 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5993 input_data->len),
5994 PSA_ERROR_INVALID_ARGUMENT);
5995 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5996 PSA_ERROR_BAD_STATE);
5997 } else {
5998 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5999 input_data->len));
6000 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006001 }
6002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006004#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006005
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006006 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006009
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (operation.alg == PSA_ALG_CCM) {
6013 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6014 input_data->len),
6015 PSA_ERROR_INVALID_ARGUMENT);
6016 } else {
6017 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6018 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006019 }
6020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006022
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006023 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006024#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 if (operation.alg == PSA_ALG_GCM) {
6028 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6029 SIZE_MAX),
6030 PSA_ERROR_INVALID_ARGUMENT);
6031 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6032 PSA_ERROR_BAD_STATE);
6033 } else if (operation.alg != PSA_ALG_CCM) {
6034 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6035 SIZE_MAX));
6036 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006037 }
6038
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006040#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006041
Tom Cosgrove1797b052022-12-04 17:19:59 +00006042 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006043#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 if (operation.alg == PSA_ALG_GCM) {
6049 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6050 SIZE_MAX),
6051 PSA_ERROR_INVALID_ARGUMENT);
6052 } else if (operation.alg != PSA_ALG_CCM) {
6053 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6054 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006055 }
6056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006058#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006059
6060 /* ------------------------------------------------------- */
6061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6067 PSA_AEAD_NONCE_MAX_SIZE,
6068 &nonce_length),
6069 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006070
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006072
Paul Elliott7220cae2021-06-22 17:25:57 +01006073 /* Test for generating nonce in decrypt setup. */
6074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6078 PSA_AEAD_NONCE_MAX_SIZE,
6079 &nonce_length),
6080 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006083
Paul Elliottc23a9a02021-06-21 18:32:46 +01006084 /* Test for setting lengths twice. */
6085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6091 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6094 input_data->len),
6095 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006098
Andrzej Kurekad837522021-12-15 15:28:49 +01006099 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6108 additional_data->len),
6109 PSA_ERROR_BAD_STATE);
6110 } else {
6111 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6112 additional_data->len));
6113
6114 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6115 input_data->len),
6116 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006117 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006119
6120 /* ------------------------------------------------------- */
6121
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006123
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 if (operation.alg == PSA_ALG_CCM) {
6127 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6128 input_data->len, output_data,
6129 output_size, &output_length),
6130 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 } else {
6133 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6134 input_data->len, output_data,
6135 output_size, &output_length));
6136
6137 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6138 input_data->len),
6139 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006140 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006142
6143 /* ------------------------------------------------------- */
6144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if (operation.alg == PSA_ALG_CCM) {
6150 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6151 finish_output_size,
6152 &output_part_length,
6153 tag_buffer, tag_length,
6154 &tag_size));
6155 } else {
6156 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6157 finish_output_size,
6158 &output_part_length,
6159 tag_buffer, tag_length,
6160 &tag_size));
6161
6162 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6163 input_data->len),
6164 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006165 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006167
6168 /* Test for setting lengths after generating nonce + already starting data. */
6169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6173 PSA_AEAD_NONCE_MAX_SIZE,
6174 &nonce_length));
6175 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6178 additional_data->len),
6179 PSA_ERROR_BAD_STATE);
6180 } else {
6181 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6182 additional_data->len));
6183
6184 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6185 input_data->len),
6186 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006187 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006189
6190 /* ------------------------------------------------------- */
6191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006193
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6195 PSA_AEAD_NONCE_MAX_SIZE,
6196 &nonce_length));
6197 if (operation.alg == PSA_ALG_CCM) {
6198 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6199 input_data->len, output_data,
6200 output_size, &output_length),
6201 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 } else {
6204 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6205 input_data->len, output_data,
6206 output_size, &output_length));
6207
6208 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6209 input_data->len),
6210 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006211 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006213
6214 /* ------------------------------------------------------- */
6215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6219 PSA_AEAD_NONCE_MAX_SIZE,
6220 &nonce_length));
6221 if (operation.alg == PSA_ALG_CCM) {
6222 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6223 finish_output_size,
6224 &output_part_length,
6225 tag_buffer, tag_length,
6226 &tag_size));
6227 } else {
6228 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6229 finish_output_size,
6230 &output_part_length,
6231 tag_buffer, tag_length,
6232 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6235 input_data->len),
6236 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006237 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006239
Paul Elliott243080c2021-07-21 19:01:17 +01006240 /* Test for not sending any additional data or data after setting non zero
6241 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006242
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006244
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6248 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6251 finish_output_size,
6252 &output_part_length,
6253 tag_buffer, tag_length,
6254 &tag_size),
6255 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006258
Paul Elliott243080c2021-07-21 19:01:17 +01006259 /* Test for not sending any additional data or data after setting non-zero
6260 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6267 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006268
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6270 finish_output_size,
6271 &output_part_length,
6272 tag_buffer,
6273 tag_length),
6274 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006277
Paul Elliott243080c2021-07-21 19:01:17 +01006278 /* Test for not sending any additional data after setting a non-zero length
6279 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6286 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6289 input_data->len, output_data,
6290 output_size, &output_length),
6291 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006294
Paul Elliottf94bd992021-09-19 18:15:59 +01006295 /* Test for not sending any data after setting a non-zero length for it.*/
6296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6302 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6305 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6308 finish_output_size,
6309 &output_part_length,
6310 tag_buffer, tag_length,
6311 &tag_size),
6312 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006315
Paul Elliottb0450fe2021-09-01 15:06:26 +01006316 /* Test for sending too much additional data after setting lengths. */
6317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006323
6324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6326 additional_data->len),
6327 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006330
Paul Elliotta2a09b02021-09-22 14:56:40 +01006331 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6338 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6341 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6344 1),
6345 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006348
Paul Elliottb0450fe2021-09-01 15:06:26 +01006349 /* Test for sending too much data after setting lengths. */
6350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006354
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6358 input_data->len, output_data,
6359 output_size, &output_length),
6360 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006363
Paul Elliotta2a09b02021-09-22 14:56:40 +01006364 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6371 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6374 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6377 input_data->len, output_data,
6378 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6381 1, output_data,
6382 output_size, &output_length),
6383 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006386
Paul Elliottc23a9a02021-06-21 18:32:46 +01006387 /* Test sending additional data after data. */
6388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 if (operation.alg != PSA_ALG_CCM) {
6394 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6395 input_data->len, output_data,
6396 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6399 additional_data->len),
6400 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006401 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006403
Paul Elliott534d0b42021-06-22 19:15:20 +01006404 /* Test calling finish on decryption. */
6405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6411 finish_output_size,
6412 &output_part_length,
6413 tag_buffer, tag_length,
6414 &tag_size),
6415 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006418
6419 /* Test calling verify on encryption. */
6420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6426 finish_output_size,
6427 &output_part_length,
6428 tag_buffer,
6429 tag_length),
6430 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006433
6434
Paul Elliottc23a9a02021-06-21 18:32:46 +01006435exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 psa_destroy_key(key);
6437 psa_aead_abort(&operation);
6438 mbedtls_free(output_data);
6439 mbedtls_free(final_data);
6440 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006441}
6442/* END_CASE */
6443
6444/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006445void signature_size(int type_arg,
6446 int bits,
6447 int alg_arg,
6448 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006449{
6450 psa_key_type_t type = type_arg;
6451 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006453
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006455
Gilles Peskinee59236f2018-01-27 23:32:46 +01006456exit:
6457 ;
6458}
6459/* END_CASE */
6460
6461/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006462void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6463 int alg_arg, data_t *input_data,
6464 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006465{
Ronald Cron5425a212020-08-04 14:58:35 +02006466 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006467 psa_key_type_t key_type = key_type_arg;
6468 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006469 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006470 unsigned char *signature = NULL;
6471 size_t signature_size;
6472 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006474
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006476
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6478 psa_set_key_algorithm(&attributes, alg);
6479 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006480
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6482 &key));
6483 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6484 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006485
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006486 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006487 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6489 key_bits, alg);
6490 TEST_ASSERT(signature_size != 0);
6491 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006492 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006493
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006494 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 PSA_ASSERT(psa_sign_hash(key, alg,
6496 input_data->x, input_data->len,
6497 signature, signature_size,
6498 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006499 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006500 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006501 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006502
6503exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006504 /*
6505 * Key attributes may have been returned by psa_get_key_attributes()
6506 * thus reset them as required.
6507 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006509
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 psa_destroy_key(key);
6511 mbedtls_free(signature);
6512 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006513}
6514/* END_CASE */
6515
Paul Elliott712d5122022-12-07 14:03:10 +00006516/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006517/**
6518 * sign_hash_interruptible() test intentions:
6519 *
6520 * Note: This test can currently only handle ECDSA.
6521 *
6522 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006523 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006524 *
6525 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6526 * expected for different max_ops values.
6527 *
6528 * 3. Test that the number of ops done prior to start and after abort is zero
6529 * and that each successful stage completes some ops (this is not mandated by
6530 * the PSA specification, but is currently the case).
6531 *
6532 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6533 * complete() calls does not alter the number of ops returned.
6534 */
Paul Elliott712d5122022-12-07 14:03:10 +00006535void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6536 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006537 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006538{
6539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6540 psa_key_type_t key_type = key_type_arg;
6541 psa_algorithm_t alg = alg_arg;
6542 size_t key_bits;
6543 unsigned char *signature = NULL;
6544 size_t signature_size;
6545 size_t signature_length = 0xdeadbeef;
6546 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6547 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006548 uint32_t num_ops = 0;
6549 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006550 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006551 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006552 size_t min_completes = 0;
6553 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006554
Paul Elliott712d5122022-12-07 14:03:10 +00006555 psa_sign_hash_interruptible_operation_t operation =
6556 psa_sign_hash_interruptible_operation_init();
6557
6558 PSA_ASSERT(psa_crypto_init());
6559
6560 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6561 psa_set_key_algorithm(&attributes, alg);
6562 psa_set_key_type(&attributes, key_type);
6563
6564 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6565 &key));
6566 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6567 key_bits = psa_get_key_bits(&attributes);
6568
6569 /* Allocate a buffer which has the size advertised by the
6570 * library. */
6571 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6572 key_bits, alg);
6573 TEST_ASSERT(signature_size != 0);
6574 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006575 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006576
Paul Elliott0c683352022-12-16 19:16:56 +00006577 psa_interruptible_set_max_ops(max_ops);
6578
Paul Elliott6f600372023-02-06 18:41:05 +00006579 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6580 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006581
Paul Elliott712d5122022-12-07 14:03:10 +00006582 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6583 TEST_ASSERT(num_ops_prior == 0);
6584
6585 /* Start performing the signature. */
6586 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6587 input_data->x, input_data->len));
6588
6589 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6590 TEST_ASSERT(num_ops_prior == 0);
6591
6592 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006593 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006594 status = psa_sign_hash_complete(&operation, signature, signature_size,
6595 &signature_length);
6596
Paul Elliott0c683352022-12-16 19:16:56 +00006597 num_completes++;
6598
Paul Elliott712d5122022-12-07 14:03:10 +00006599 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6600 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006601 /* We are asserting here that every complete makes progress
6602 * (completes some ops), which is true of the internal
6603 * implementation and probably any implementation, however this is
6604 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006605 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006606
Paul Elliott712d5122022-12-07 14:03:10 +00006607 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006608
6609 /* Ensure calling get_num_ops() twice still returns the same
6610 * number of ops as previously reported. */
6611 num_ops = psa_sign_hash_get_num_ops(&operation);
6612
6613 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006614 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006615 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006616
6617 TEST_ASSERT(status == PSA_SUCCESS);
6618
Paul Elliott0c683352022-12-16 19:16:56 +00006619 TEST_LE_U(min_completes, num_completes);
6620 TEST_LE_U(num_completes, max_completes);
6621
Paul Elliott712d5122022-12-07 14:03:10 +00006622 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006623 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006624 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006625
6626 PSA_ASSERT(psa_sign_hash_abort(&operation));
6627
Paul Elliott59ad9452022-12-18 15:09:02 +00006628 num_ops = psa_sign_hash_get_num_ops(&operation);
6629 TEST_ASSERT(num_ops == 0);
6630
Paul Elliott712d5122022-12-07 14:03:10 +00006631exit:
6632
6633 /*
6634 * Key attributes may have been returned by psa_get_key_attributes()
6635 * thus reset them as required.
6636 */
6637 psa_reset_key_attributes(&attributes);
6638
6639 psa_destroy_key(key);
6640 mbedtls_free(signature);
6641 PSA_DONE();
6642}
6643/* END_CASE */
6644
Gilles Peskine20035e32018-02-03 22:44:14 +01006645/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006646void sign_hash_fail(int key_type_arg, data_t *key_data,
6647 int alg_arg, data_t *input_data,
6648 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006649{
Ronald Cron5425a212020-08-04 14:58:35 +02006650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006651 psa_key_type_t key_type = key_type_arg;
6652 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006653 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006654 psa_status_t actual_status;
6655 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006656 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006657 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006659
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006660 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006661
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006663
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6665 psa_set_key_algorithm(&attributes, alg);
6666 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6669 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 actual_status = psa_sign_hash(key, alg,
6672 input_data->x, input_data->len,
6673 signature, signature_size,
6674 &signature_length);
6675 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006676 /* The value of *signature_length is unspecified on error, but
6677 * whatever it is, it should be less than signature_size, so that
6678 * if the caller tries to read *signature_length bytes without
6679 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006681
6682exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 psa_reset_key_attributes(&attributes);
6684 psa_destroy_key(key);
6685 mbedtls_free(signature);
6686 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006687}
6688/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006689
Paul Elliott91007972022-12-16 12:21:24 +00006690/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006691/**
6692 * sign_hash_fail_interruptible() test intentions:
6693 *
6694 * Note: This test can currently only handle ECDSA.
6695 *
6696 * 1. Test that various failure cases for interruptible sign hash fail with the
6697 * correct error codes, and at the correct point (at start or during
6698 * complete).
6699 *
6700 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6701 * expected for different max_ops values.
6702 *
6703 * 3. Test that the number of ops done prior to start and after abort is zero
6704 * and that each successful stage completes some ops (this is not mandated by
6705 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006706 *
6707 * 4. Check that calling complete() when start() fails and complete()
6708 * after completion results in a BAD_STATE error.
6709 *
6710 * 5. Check that calling start() again after start fails results in a BAD_STATE
6711 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006712 */
Paul Elliott91007972022-12-16 12:21:24 +00006713void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6714 int alg_arg, data_t *input_data,
6715 int signature_size_arg,
6716 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006717 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006718 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006719{
6720 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6721 psa_key_type_t key_type = key_type_arg;
6722 psa_algorithm_t alg = alg_arg;
6723 size_t signature_size = signature_size_arg;
6724 psa_status_t actual_status;
6725 psa_status_t expected_start_status = expected_start_status_arg;
6726 psa_status_t expected_complete_status = expected_complete_status_arg;
6727 unsigned char *signature = NULL;
6728 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006729 uint32_t num_ops = 0;
6730 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006731 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006732 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006733 size_t min_completes = 0;
6734 size_t max_completes = 0;
6735
Paul Elliott91007972022-12-16 12:21:24 +00006736 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6737 psa_sign_hash_interruptible_operation_t operation =
6738 psa_sign_hash_interruptible_operation_init();
6739
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006740 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006741
6742 PSA_ASSERT(psa_crypto_init());
6743
6744 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6745 psa_set_key_algorithm(&attributes, alg);
6746 psa_set_key_type(&attributes, key_type);
6747
6748 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6749 &key));
6750
Paul Elliott0c683352022-12-16 19:16:56 +00006751 psa_interruptible_set_max_ops(max_ops);
6752
Paul Elliott6f600372023-02-06 18:41:05 +00006753 interruptible_signverify_get_minmax_completes(max_ops,
6754 expected_complete_status,
6755 &min_completes,
6756 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006757
Paul Elliott91007972022-12-16 12:21:24 +00006758 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6759 TEST_ASSERT(num_ops_prior == 0);
6760
6761 /* Start performing the signature. */
6762 actual_status = psa_sign_hash_start(&operation, key, alg,
6763 input_data->x, input_data->len);
6764
6765 TEST_EQUAL(actual_status, expected_start_status);
6766
Paul Elliottc9774412023-02-06 15:14:07 +00006767 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006768 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006769 * start failed. */
6770 actual_status = psa_sign_hash_complete(&operation, signature,
6771 signature_size,
6772 &signature_length);
6773
6774 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6775
6776 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006777 actual_status = psa_sign_hash_start(&operation, key, alg,
6778 input_data->x, input_data->len);
6779
6780 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6781 }
6782
Paul Elliott91007972022-12-16 12:21:24 +00006783 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6784 TEST_ASSERT(num_ops_prior == 0);
6785
Paul Elliott91007972022-12-16 12:21:24 +00006786 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006787 do {
Paul Elliott91007972022-12-16 12:21:24 +00006788 actual_status = psa_sign_hash_complete(&operation, signature,
6789 signature_size,
6790 &signature_length);
6791
Paul Elliott0c683352022-12-16 19:16:56 +00006792 num_completes++;
6793
Paul Elliott334d7262023-01-20 17:29:41 +00006794 if (actual_status == PSA_SUCCESS ||
6795 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006796 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006797 /* We are asserting here that every complete makes progress
6798 * (completes some ops), which is true of the internal
6799 * implementation and probably any implementation, however this is
6800 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006801 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006802
Paul Elliott91007972022-12-16 12:21:24 +00006803 num_ops_prior = num_ops;
6804 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006805 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006806
Paul Elliottc9774412023-02-06 15:14:07 +00006807 TEST_EQUAL(actual_status, expected_complete_status);
6808
Paul Elliottefebad02023-02-15 16:56:45 +00006809 /* Check that another complete returns BAD_STATE. */
6810 actual_status = psa_sign_hash_complete(&operation, signature,
6811 signature_size,
6812 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006813
Paul Elliottefebad02023-02-15 16:56:45 +00006814 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006815
Paul Elliott91007972022-12-16 12:21:24 +00006816 PSA_ASSERT(psa_sign_hash_abort(&operation));
6817
Paul Elliott59ad9452022-12-18 15:09:02 +00006818 num_ops = psa_sign_hash_get_num_ops(&operation);
6819 TEST_ASSERT(num_ops == 0);
6820
Paul Elliott91007972022-12-16 12:21:24 +00006821 /* The value of *signature_length is unspecified on error, but
6822 * whatever it is, it should be less than signature_size, so that
6823 * if the caller tries to read *signature_length bytes without
6824 * checking the error code then they don't overflow a buffer. */
6825 TEST_LE_U(signature_length, signature_size);
6826
Paul Elliott0c683352022-12-16 19:16:56 +00006827 TEST_LE_U(min_completes, num_completes);
6828 TEST_LE_U(num_completes, max_completes);
6829
Paul Elliott91007972022-12-16 12:21:24 +00006830exit:
6831 psa_reset_key_attributes(&attributes);
6832 psa_destroy_key(key);
6833 mbedtls_free(signature);
6834 PSA_DONE();
6835}
6836/* END_CASE */
6837
mohammad16038cc1cee2018-03-28 01:21:33 +03006838/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006839void sign_verify_hash(int key_type_arg, data_t *key_data,
6840 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006841{
Ronald Cron5425a212020-08-04 14:58:35 +02006842 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006843 psa_key_type_t key_type = key_type_arg;
6844 psa_algorithm_t alg = alg_arg;
6845 size_t key_bits;
6846 unsigned char *signature = NULL;
6847 size_t signature_size;
6848 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006849 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006852
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6854 psa_set_key_algorithm(&attributes, alg);
6855 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006856
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6858 &key));
6859 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6860 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006861
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006862 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006863 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6865 key_bits, alg);
6866 TEST_ASSERT(signature_size != 0);
6867 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006868 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006869
6870 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 PSA_ASSERT(psa_sign_hash(key, alg,
6872 input_data->x, input_data->len,
6873 signature, signature_size,
6874 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006875 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 TEST_LE_U(signature_length, signature_size);
6877 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006878
6879 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 PSA_ASSERT(psa_verify_hash(key, alg,
6881 input_data->x, input_data->len,
6882 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006883
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006885 /* Flip a bit in the input and verify that the signature is now
6886 * detected as invalid. Flip a bit at the beginning, not at the end,
6887 * because ECDSA may ignore the last few bits of the input. */
6888 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 TEST_EQUAL(psa_verify_hash(key, alg,
6890 input_data->x, input_data->len,
6891 signature, signature_length),
6892 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006893 }
6894
6895exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006896 /*
6897 * Key attributes may have been returned by psa_get_key_attributes()
6898 * thus reset them as required.
6899 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 psa_destroy_key(key);
6903 mbedtls_free(signature);
6904 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006905}
6906/* END_CASE */
6907
Paul Elliott712d5122022-12-07 14:03:10 +00006908/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006909/**
6910 * sign_verify_hash_interruptible() test intentions:
6911 *
6912 * Note: This test can currently only handle ECDSA.
6913 *
Paul Elliott8c092052023-03-06 17:49:14 +00006914 * 1. Test that we can sign an input hash with the given keypair and then
6915 * afterwards verify that signature. This is currently the only way to test
6916 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006917 *
6918 * 2. Test that after corrupting the hash, the verification detects an invalid
6919 * signature.
6920 *
6921 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6922 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006923 *
6924 * 4. Test that the number of ops done prior to starting signing and after abort
6925 * is zero and that each successful signing stage completes some ops (this is
6926 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006927 */
Paul Elliott712d5122022-12-07 14:03:10 +00006928void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006929 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006930 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006931{
6932 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6933 psa_key_type_t key_type = key_type_arg;
6934 psa_algorithm_t alg = alg_arg;
6935 size_t key_bits;
6936 unsigned char *signature = NULL;
6937 size_t signature_size;
6938 size_t signature_length = 0xdeadbeef;
6939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6940 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006941 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006942 uint32_t num_ops = 0;
6943 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006944 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006945 size_t min_completes = 0;
6946 size_t max_completes = 0;
6947
Paul Elliott712d5122022-12-07 14:03:10 +00006948 psa_sign_hash_interruptible_operation_t sign_operation =
6949 psa_sign_hash_interruptible_operation_init();
6950 psa_verify_hash_interruptible_operation_t verify_operation =
6951 psa_verify_hash_interruptible_operation_init();
6952
6953 PSA_ASSERT(psa_crypto_init());
6954
Paul Elliott0c683352022-12-16 19:16:56 +00006955 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6956 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006957 psa_set_key_algorithm(&attributes, alg);
6958 psa_set_key_type(&attributes, key_type);
6959
6960 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6961 &key));
6962 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6963 key_bits = psa_get_key_bits(&attributes);
6964
6965 /* Allocate a buffer which has the size advertised by the
6966 * library. */
6967 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6968 key_bits, alg);
6969 TEST_ASSERT(signature_size != 0);
6970 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006971 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006972
Paul Elliott0c683352022-12-16 19:16:56 +00006973 psa_interruptible_set_max_ops(max_ops);
6974
Paul Elliott6f600372023-02-06 18:41:05 +00006975 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6976 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006977
Paul Elliott7c173082023-02-26 18:44:45 +00006978 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6979 TEST_ASSERT(num_ops_prior == 0);
6980
Paul Elliott712d5122022-12-07 14:03:10 +00006981 /* Start performing the signature. */
6982 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6983 input_data->x, input_data->len));
6984
Paul Elliott7c173082023-02-26 18:44:45 +00006985 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6986 TEST_ASSERT(num_ops_prior == 0);
6987
Paul Elliott712d5122022-12-07 14:03:10 +00006988 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006989 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006990
Paul Elliott0c683352022-12-16 19:16:56 +00006991 status = psa_sign_hash_complete(&sign_operation, signature,
6992 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006993 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006994
6995 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006996
6997 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6998 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6999 /* We are asserting here that every complete makes progress
7000 * (completes some ops), which is true of the internal
7001 * implementation and probably any implementation, however this is
7002 * not mandated by the PSA specification. */
7003 TEST_ASSERT(num_ops > num_ops_prior);
7004
7005 num_ops_prior = num_ops;
7006 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007007 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007008
7009 TEST_ASSERT(status == PSA_SUCCESS);
7010
Paul Elliott0c683352022-12-16 19:16:56 +00007011 TEST_LE_U(min_completes, num_completes);
7012 TEST_LE_U(num_completes, max_completes);
7013
Paul Elliott712d5122022-12-07 14:03:10 +00007014 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7015
Paul Elliott7c173082023-02-26 18:44:45 +00007016 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7017 TEST_ASSERT(num_ops == 0);
7018
Paul Elliott712d5122022-12-07 14:03:10 +00007019 /* Check that the signature length looks sensible. */
7020 TEST_LE_U(signature_length, signature_size);
7021 TEST_ASSERT(signature_length > 0);
7022
Paul Elliott0c683352022-12-16 19:16:56 +00007023 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007024
7025 /* Start verification. */
7026 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7027 input_data->x, input_data->len,
7028 signature, signature_length));
7029
7030 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007031 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007032 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007033
7034 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007035 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007036
7037 TEST_ASSERT(status == PSA_SUCCESS);
7038
Paul Elliott0c683352022-12-16 19:16:56 +00007039 TEST_LE_U(min_completes, num_completes);
7040 TEST_LE_U(num_completes, max_completes);
7041
Paul Elliott712d5122022-12-07 14:03:10 +00007042 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7043
7044 verify_operation = psa_verify_hash_interruptible_operation_init();
7045
7046 if (input_data->len != 0) {
7047 /* Flip a bit in the input and verify that the signature is now
7048 * detected as invalid. Flip a bit at the beginning, not at the end,
7049 * because ECDSA may ignore the last few bits of the input. */
7050 input_data->x[0] ^= 1;
7051
Paul Elliott712d5122022-12-07 14:03:10 +00007052 /* Start verification. */
7053 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7054 input_data->x, input_data->len,
7055 signature, signature_length));
7056
7057 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007058 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007059 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007060 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007061
7062 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7063 }
7064
7065 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7066
7067exit:
7068 /*
7069 * Key attributes may have been returned by psa_get_key_attributes()
7070 * thus reset them as required.
7071 */
7072 psa_reset_key_attributes(&attributes);
7073
7074 psa_destroy_key(key);
7075 mbedtls_free(signature);
7076 PSA_DONE();
7077}
7078/* END_CASE */
7079
Gilles Peskine9911b022018-06-29 17:30:48 +02007080/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007081void verify_hash(int key_type_arg, data_t *key_data,
7082 int alg_arg, data_t *hash_data,
7083 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007084{
Ronald Cron5425a212020-08-04 14:58:35 +02007085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007086 psa_key_type_t key_type = key_type_arg;
7087 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007091
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007093
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7095 psa_set_key_algorithm(&attributes, alg);
7096 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007097
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7099 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007100
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 PSA_ASSERT(psa_verify_hash(key, alg,
7102 hash_data->x, hash_data->len,
7103 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007104
itayzafrir5c753392018-05-08 11:18:38 +03007105exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 psa_reset_key_attributes(&attributes);
7107 psa_destroy_key(key);
7108 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007109}
7110/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007111
Paul Elliott712d5122022-12-07 14:03:10 +00007112/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007113/**
7114 * verify_hash_interruptible() test intentions:
7115 *
7116 * Note: This test can currently only handle ECDSA.
7117 *
7118 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007119 * only). Given this test only does verification it can accept public keys as
7120 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007121 *
7122 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7123 * expected for different max_ops values.
7124 *
7125 * 3. Test that the number of ops done prior to start and after abort is zero
7126 * and that each successful stage completes some ops (this is not mandated by
7127 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007128 *
7129 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7130 * complete() calls does not alter the number of ops returned.
7131 *
7132 * 5. Test that after corrupting the hash, the verification detects an invalid
7133 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007134 */
Paul Elliott712d5122022-12-07 14:03:10 +00007135void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7136 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007137 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007138{
7139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7140 psa_key_type_t key_type = key_type_arg;
7141 psa_algorithm_t alg = alg_arg;
7142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7143 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007144 uint32_t num_ops = 0;
7145 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007146 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007147 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007148 size_t min_completes = 0;
7149 size_t max_completes = 0;
7150
Paul Elliott712d5122022-12-07 14:03:10 +00007151 psa_verify_hash_interruptible_operation_t operation =
7152 psa_verify_hash_interruptible_operation_init();
7153
7154 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7155
7156 PSA_ASSERT(psa_crypto_init());
7157
7158 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7159 psa_set_key_algorithm(&attributes, alg);
7160 psa_set_key_type(&attributes, key_type);
7161
7162 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7163 &key));
7164
Paul Elliott0c683352022-12-16 19:16:56 +00007165 psa_interruptible_set_max_ops(max_ops);
7166
Paul Elliott6f600372023-02-06 18:41:05 +00007167 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7168 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007169
Paul Elliott712d5122022-12-07 14:03:10 +00007170 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7171
7172 TEST_ASSERT(num_ops_prior == 0);
7173
7174 /* Start verification. */
7175 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7176 hash_data->x, hash_data->len,
7177 signature_data->x, signature_data->len)
7178 );
7179
7180 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7181
7182 TEST_ASSERT(num_ops_prior == 0);
7183
7184 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007185 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007186 status = psa_verify_hash_complete(&operation);
7187
Paul Elliott0c683352022-12-16 19:16:56 +00007188 num_completes++;
7189
Paul Elliott712d5122022-12-07 14:03:10 +00007190 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7191 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007192 /* We are asserting here that every complete makes progress
7193 * (completes some ops), which is true of the internal
7194 * implementation and probably any implementation, however this is
7195 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007196 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007197
Paul Elliott712d5122022-12-07 14:03:10 +00007198 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007199
7200 /* Ensure calling get_num_ops() twice still returns the same
7201 * number of ops as previously reported. */
7202 num_ops = psa_verify_hash_get_num_ops(&operation);
7203
7204 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007205 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007206 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007207
7208 TEST_ASSERT(status == PSA_SUCCESS);
7209
Paul Elliott0c683352022-12-16 19:16:56 +00007210 TEST_LE_U(min_completes, num_completes);
7211 TEST_LE_U(num_completes, max_completes);
7212
Paul Elliott712d5122022-12-07 14:03:10 +00007213 PSA_ASSERT(psa_verify_hash_abort(&operation));
7214
Paul Elliott59ad9452022-12-18 15:09:02 +00007215 num_ops = psa_verify_hash_get_num_ops(&operation);
7216 TEST_ASSERT(num_ops == 0);
7217
Paul Elliott8359c142023-02-24 18:40:10 +00007218 if (hash_data->len != 0) {
7219 /* Flip a bit in the hash and verify that the signature is now detected
7220 * as invalid. Flip a bit at the beginning, not at the end, because
7221 * ECDSA may ignore the last few bits of the input. */
7222 hash_data->x[0] ^= 1;
7223
7224 /* Start verification. */
7225 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7226 hash_data->x, hash_data->len,
7227 signature_data->x, signature_data->len));
7228
7229 /* Continue performing the signature until complete. */
7230 do {
7231 status = psa_verify_hash_complete(&operation);
7232 } while (status == PSA_OPERATION_INCOMPLETE);
7233
7234 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7235 }
7236
Paul Elliott712d5122022-12-07 14:03:10 +00007237exit:
7238 psa_reset_key_attributes(&attributes);
7239 psa_destroy_key(key);
7240 PSA_DONE();
7241}
7242/* END_CASE */
7243
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007244/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007245void verify_hash_fail(int key_type_arg, data_t *key_data,
7246 int alg_arg, data_t *hash_data,
7247 data_t *signature_data,
7248 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007249{
Ronald Cron5425a212020-08-04 14:58:35 +02007250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007251 psa_key_type_t key_type = key_type_arg;
7252 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007253 psa_status_t actual_status;
7254 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007256
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007258
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7260 psa_set_key_algorithm(&attributes, alg);
7261 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007262
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7264 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007265
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 actual_status = psa_verify_hash(key, alg,
7267 hash_data->x, hash_data->len,
7268 signature_data->x, signature_data->len);
7269 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007270
7271exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 psa_reset_key_attributes(&attributes);
7273 psa_destroy_key(key);
7274 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007275}
7276/* END_CASE */
7277
Paul Elliott91007972022-12-16 12:21:24 +00007278/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007279/**
7280 * verify_hash_fail_interruptible() test intentions:
7281 *
7282 * Note: This test can currently only handle ECDSA.
7283 *
7284 * 1. Test that various failure cases for interruptible verify hash fail with
7285 * the correct error codes, and at the correct point (at start or during
7286 * complete).
7287 *
7288 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7289 * expected for different max_ops values.
7290 *
7291 * 3. Test that the number of ops done prior to start and after abort is zero
7292 * and that each successful stage completes some ops (this is not mandated by
7293 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007294 *
7295 * 4. Check that calling complete() when start() fails and complete()
7296 * after completion results in a BAD_STATE error.
7297 *
7298 * 5. Check that calling start() again after start fails results in a BAD_STATE
7299 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007300 */
Paul Elliott91007972022-12-16 12:21:24 +00007301void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7302 int alg_arg, data_t *hash_data,
7303 data_t *signature_data,
7304 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007305 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007306 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007307{
7308 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7309 psa_key_type_t key_type = key_type_arg;
7310 psa_algorithm_t alg = alg_arg;
7311 psa_status_t actual_status;
7312 psa_status_t expected_start_status = expected_start_status_arg;
7313 psa_status_t expected_complete_status = expected_complete_status_arg;
7314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007315 uint32_t num_ops = 0;
7316 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007317 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007318 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007319 size_t min_completes = 0;
7320 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007321 psa_verify_hash_interruptible_operation_t operation =
7322 psa_verify_hash_interruptible_operation_init();
7323
7324 PSA_ASSERT(psa_crypto_init());
7325
7326 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7327 psa_set_key_algorithm(&attributes, alg);
7328 psa_set_key_type(&attributes, key_type);
7329
7330 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7331 &key));
7332
Paul Elliott0c683352022-12-16 19:16:56 +00007333 psa_interruptible_set_max_ops(max_ops);
7334
Paul Elliott6f600372023-02-06 18:41:05 +00007335 interruptible_signverify_get_minmax_completes(max_ops,
7336 expected_complete_status,
7337 &min_completes,
7338 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007339
Paul Elliott91007972022-12-16 12:21:24 +00007340 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7341 TEST_ASSERT(num_ops_prior == 0);
7342
7343 /* Start verification. */
7344 actual_status = psa_verify_hash_start(&operation, key, alg,
7345 hash_data->x, hash_data->len,
7346 signature_data->x,
7347 signature_data->len);
7348
7349 TEST_EQUAL(actual_status, expected_start_status);
7350
Paul Elliottc9774412023-02-06 15:14:07 +00007351 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007352 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007353 * start failed. */
7354 actual_status = psa_verify_hash_complete(&operation);
7355
7356 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7357
7358 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007359 actual_status = psa_verify_hash_start(&operation, key, alg,
7360 hash_data->x, hash_data->len,
7361 signature_data->x,
7362 signature_data->len);
7363
7364 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7365 }
7366
Paul Elliott91007972022-12-16 12:21:24 +00007367 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7368 TEST_ASSERT(num_ops_prior == 0);
7369
Paul Elliott91007972022-12-16 12:21:24 +00007370 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007371 do {
Paul Elliott91007972022-12-16 12:21:24 +00007372 actual_status = psa_verify_hash_complete(&operation);
7373
Paul Elliott0c683352022-12-16 19:16:56 +00007374 num_completes++;
7375
Paul Elliott334d7262023-01-20 17:29:41 +00007376 if (actual_status == PSA_SUCCESS ||
7377 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007378 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007379 /* We are asserting here that every complete makes progress
7380 * (completes some ops), which is true of the internal
7381 * implementation and probably any implementation, however this is
7382 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007383 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007384
Paul Elliott91007972022-12-16 12:21:24 +00007385 num_ops_prior = num_ops;
7386 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007387 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007388
Paul Elliottc9774412023-02-06 15:14:07 +00007389 TEST_EQUAL(actual_status, expected_complete_status);
7390
Paul Elliottefebad02023-02-15 16:56:45 +00007391 /* Check that another complete returns BAD_STATE. */
7392 actual_status = psa_verify_hash_complete(&operation);
7393 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007394
Paul Elliott0c683352022-12-16 19:16:56 +00007395 TEST_LE_U(min_completes, num_completes);
7396 TEST_LE_U(num_completes, max_completes);
7397
Paul Elliott91007972022-12-16 12:21:24 +00007398 PSA_ASSERT(psa_verify_hash_abort(&operation));
7399
Paul Elliott59ad9452022-12-18 15:09:02 +00007400 num_ops = psa_verify_hash_get_num_ops(&operation);
7401 TEST_ASSERT(num_ops == 0);
7402
Paul Elliott91007972022-12-16 12:21:24 +00007403exit:
7404 psa_reset_key_attributes(&attributes);
7405 psa_destroy_key(key);
7406 PSA_DONE();
7407}
7408/* END_CASE */
7409
Paul Elliott20a36062022-12-18 13:21:25 +00007410/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007411/**
7412 * interruptible_signverify_hash_state_test() test intentions:
7413 *
7414 * Note: This test can currently only handle ECDSA.
7415 *
7416 * 1. Test that calling the various interruptible sign and verify hash functions
7417 * in incorrect orders returns BAD_STATE errors.
7418 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007419void interruptible_signverify_hash_state_test(int key_type_arg,
7420 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007421{
7422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7423 psa_key_type_t key_type = key_type_arg;
7424 psa_algorithm_t alg = alg_arg;
7425 size_t key_bits;
7426 unsigned char *signature = NULL;
7427 size_t signature_size;
7428 size_t signature_length = 0xdeadbeef;
7429 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7430 psa_sign_hash_interruptible_operation_t sign_operation =
7431 psa_sign_hash_interruptible_operation_init();
7432 psa_verify_hash_interruptible_operation_t verify_operation =
7433 psa_verify_hash_interruptible_operation_init();
7434
7435 PSA_ASSERT(psa_crypto_init());
7436
7437 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7438 PSA_KEY_USAGE_VERIFY_HASH);
7439 psa_set_key_algorithm(&attributes, alg);
7440 psa_set_key_type(&attributes, key_type);
7441
7442 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7443 &key));
7444 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7445 key_bits = psa_get_key_bits(&attributes);
7446
7447 /* Allocate a buffer which has the size advertised by the
7448 * library. */
7449 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7450 key_bits, alg);
7451 TEST_ASSERT(signature_size != 0);
7452 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007453 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007454
7455 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7456
7457 /* --- Attempt completes prior to starts --- */
7458 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7459 signature_size,
7460 &signature_length),
7461 PSA_ERROR_BAD_STATE);
7462
7463 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7464
7465 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7466 PSA_ERROR_BAD_STATE);
7467
7468 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7469
7470 /* --- Aborts in all other places. --- */
7471 psa_sign_hash_abort(&sign_operation);
7472
7473 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7474 input_data->x, input_data->len));
7475
7476 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7477
7478 psa_interruptible_set_max_ops(1);
7479
7480 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7481 input_data->x, input_data->len));
7482
7483 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7484 signature_size,
7485 &signature_length),
7486 PSA_OPERATION_INCOMPLETE);
7487
7488 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7489
7490 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7491
7492 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7493 input_data->x, input_data->len));
7494
7495 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7496 signature_size,
7497 &signature_length));
7498
7499 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7500
7501 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7502
7503 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7504 input_data->x, input_data->len,
7505 signature, signature_length));
7506
7507 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7508
7509 psa_interruptible_set_max_ops(1);
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 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7516 PSA_OPERATION_INCOMPLETE);
7517
7518 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7519
7520 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7521
7522 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7523 input_data->x, input_data->len,
7524 signature, signature_length));
7525
7526 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7527
7528 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7529
7530 /* --- Attempt double starts. --- */
7531
7532 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7533 input_data->x, input_data->len));
7534
7535 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7536 input_data->x, input_data->len),
7537 PSA_ERROR_BAD_STATE);
7538
7539 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7540
7541 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7542 input_data->x, input_data->len,
7543 signature, signature_length));
7544
7545 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7546 input_data->x, input_data->len,
7547 signature, signature_length),
7548 PSA_ERROR_BAD_STATE);
7549
7550 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7551
Paul Elliott76d671a2023-02-07 17:45:18 +00007552exit:
7553 /*
7554 * Key attributes may have been returned by psa_get_key_attributes()
7555 * thus reset them as required.
7556 */
7557 psa_reset_key_attributes(&attributes);
7558
7559 psa_destroy_key(key);
7560 mbedtls_free(signature);
7561 PSA_DONE();
7562}
7563/* END_CASE */
7564
7565/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007566/**
Paul Elliottc2033502023-02-26 17:09:14 +00007567 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007568 *
7569 * Note: This test can currently only handle ECDSA.
7570 *
7571 * 1. Test various edge cases in the interruptible sign and verify hash
7572 * interfaces.
7573 */
Paul Elliottc2033502023-02-26 17:09:14 +00007574void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007575 data_t *key_data, int alg_arg, data_t *input_data)
7576{
7577 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7578 psa_key_type_t key_type = key_type_arg;
7579 psa_algorithm_t alg = alg_arg;
7580 size_t key_bits;
7581 unsigned char *signature = NULL;
7582 size_t signature_size;
7583 size_t signature_length = 0xdeadbeef;
7584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7585 uint8_t *input_buffer = NULL;
7586 psa_sign_hash_interruptible_operation_t sign_operation =
7587 psa_sign_hash_interruptible_operation_init();
7588 psa_verify_hash_interruptible_operation_t verify_operation =
7589 psa_verify_hash_interruptible_operation_init();
7590
7591 PSA_ASSERT(psa_crypto_init());
7592
7593 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7594 PSA_KEY_USAGE_VERIFY_HASH);
7595 psa_set_key_algorithm(&attributes, alg);
7596 psa_set_key_type(&attributes, key_type);
7597
7598 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7599 &key));
7600 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7601 key_bits = psa_get_key_bits(&attributes);
7602
7603 /* Allocate a buffer which has the size advertised by the
7604 * library. */
7605 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7606 key_bits, alg);
7607 TEST_ASSERT(signature_size != 0);
7608 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007609 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007610
Paul Elliott20a36062022-12-18 13:21:25 +00007611 /* --- Change function inputs mid run, to cause an error (sign only,
7612 * verify passes all inputs to start. --- */
7613
7614 psa_interruptible_set_max_ops(1);
7615
7616 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7617 input_data->x, input_data->len));
7618
7619 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7620 signature_size,
7621 &signature_length),
7622 PSA_OPERATION_INCOMPLETE);
7623
7624 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7625 0,
7626 &signature_length),
7627 PSA_ERROR_BUFFER_TOO_SMALL);
7628
Paul Elliottc9774412023-02-06 15:14:07 +00007629 /* And test that this invalidates the operation. */
7630 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7631 0,
7632 &signature_length),
7633 PSA_ERROR_BAD_STATE);
7634
Paul Elliott20a36062022-12-18 13:21:25 +00007635 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7636
Paul Elliottf9c91a72023-02-05 18:06:38 +00007637 /* Trash the hash buffer in between start and complete, to ensure
7638 * no reliance on external buffers. */
7639 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7640
Paul Elliott6c68df42023-10-23 15:33:37 +01007641 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007642
7643 memcpy(input_buffer, input_data->x, input_data->len);
7644
7645 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7646 input_buffer, input_data->len));
7647
7648 memset(input_buffer, '!', input_data->len);
7649 mbedtls_free(input_buffer);
7650 input_buffer = NULL;
7651
7652 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7653 signature_size,
7654 &signature_length));
7655
7656 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7657
Paul Elliott6c68df42023-10-23 15:33:37 +01007658 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007659
7660 memcpy(input_buffer, input_data->x, input_data->len);
7661
7662 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7663 input_buffer, input_data->len,
7664 signature, signature_length));
7665
7666 memset(input_buffer, '!', input_data->len);
7667 mbedtls_free(input_buffer);
7668 input_buffer = NULL;
7669
7670 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7671
7672 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7673
Paul Elliott20a36062022-12-18 13:21:25 +00007674exit:
7675 /*
7676 * Key attributes may have been returned by psa_get_key_attributes()
7677 * thus reset them as required.
7678 */
7679 psa_reset_key_attributes(&attributes);
7680
7681 psa_destroy_key(key);
7682 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007683 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007684 PSA_DONE();
7685}
7686/* END_CASE */
7687
Paul Elliotta4cb9092023-02-07 18:01:55 +00007688/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007689/**
Paul Elliott57702242023-02-26 20:36:10 +00007690 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007691 *
7692 * Note: This test can currently only handle ECDSA.
7693 *
7694 * 1. Test that setting max ops is reflected in both interruptible sign and
7695 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007696 * 2. Test that changing the value of max_ops to unlimited during an operation
7697 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007698 *
7699 * 3. Test that calling get_num_ops() between complete calls gives the same
7700 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007701 */
Paul Elliott57702242023-02-26 20:36:10 +00007702void interruptible_signverify_hash_ops_tests(int key_type_arg,
7703 data_t *key_data, int alg_arg,
7704 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007705{
7706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7707 psa_key_type_t key_type = key_type_arg;
7708 psa_algorithm_t alg = alg_arg;
7709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007710 size_t key_bits;
7711 unsigned char *signature = NULL;
7712 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007713 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007714 uint32_t num_ops = 0;
7715 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7716
Paul Elliotta4cb9092023-02-07 18:01:55 +00007717 psa_sign_hash_interruptible_operation_t sign_operation =
7718 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007719 psa_verify_hash_interruptible_operation_t verify_operation =
7720 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007721
7722 PSA_ASSERT(psa_crypto_init());
7723
7724 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7725 PSA_KEY_USAGE_VERIFY_HASH);
7726 psa_set_key_algorithm(&attributes, alg);
7727 psa_set_key_type(&attributes, key_type);
7728
Paul Elliottf1743e22023-02-15 18:44:16 +00007729 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7730 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7731 key_bits = psa_get_key_bits(&attributes);
7732
7733 /* Allocate a buffer which has the size advertised by the
7734 * library. */
7735 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7736
7737 TEST_ASSERT(signature_size != 0);
7738 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007739 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007740
7741 /* Check that default max ops gets set if we don't set it. */
7742 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7743 input_data->x, input_data->len));
7744
7745 TEST_EQUAL(psa_interruptible_get_max_ops(),
7746 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7747
7748 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7749
Paul Elliottf1743e22023-02-15 18:44:16 +00007750 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7751 input_data->x, input_data->len,
7752 signature, signature_size));
7753
7754 TEST_EQUAL(psa_interruptible_get_max_ops(),
7755 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7756
7757 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7758
Paul Elliotta4cb9092023-02-07 18:01:55 +00007759 /* Check that max ops gets set properly. */
7760
7761 psa_interruptible_set_max_ops(0xbeef);
7762
Paul Elliottf1743e22023-02-15 18:44:16 +00007763 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007764
Paul Elliott9e8819f2023-02-26 19:01:35 +00007765 /* --- Ensure changing the max ops mid operation works (operation should
7766 * complete successfully after setting max ops to unlimited --- */
7767 psa_interruptible_set_max_ops(1);
7768
7769 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7770 input_data->x, input_data->len));
7771
7772 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7773 signature_size,
7774 &signature_length),
7775 PSA_OPERATION_INCOMPLETE);
7776
7777 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7778
7779 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7780 signature_size,
7781 &signature_length));
7782
7783 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7784
7785 psa_interruptible_set_max_ops(1);
7786
7787 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7788 input_data->x, input_data->len,
7789 signature, signature_length));
7790
7791 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7792 PSA_OPERATION_INCOMPLETE);
7793
7794 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7795
7796 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7797
7798 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7799
Paul Elliottc1e04002023-02-26 20:27:23 +00007800 /* --- Test that not calling get_num_ops inbetween complete calls does not
7801 * result in lost ops. ---*/
7802
7803 psa_interruptible_set_max_ops(1);
7804
7805 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7806 input_data->x, input_data->len));
7807
7808 /* Continue performing the signature until complete. */
7809 do {
7810 status = psa_sign_hash_complete(&sign_operation, signature,
7811 signature_size,
7812 &signature_length);
7813
7814 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7815
7816 } while (status == PSA_OPERATION_INCOMPLETE);
7817
7818 PSA_ASSERT(status);
7819
7820 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7821
7822 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7823 input_data->x, input_data->len));
7824
7825 /* Continue performing the signature until complete. */
7826 do {
7827 status = psa_sign_hash_complete(&sign_operation, signature,
7828 signature_size,
7829 &signature_length);
7830 } while (status == PSA_OPERATION_INCOMPLETE);
7831
7832 PSA_ASSERT(status);
7833
7834 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7835
7836 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7837
7838 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7839 input_data->x, input_data->len,
7840 signature, signature_length));
7841
7842 /* Continue performing the verification until complete. */
7843 do {
7844 status = psa_verify_hash_complete(&verify_operation);
7845
7846 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7847
7848 } while (status == PSA_OPERATION_INCOMPLETE);
7849
7850 PSA_ASSERT(status);
7851
7852 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7853
7854 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7855 input_data->x, input_data->len,
7856 signature, signature_length));
7857
7858 /* Continue performing the verification until complete. */
7859 do {
7860 status = psa_verify_hash_complete(&verify_operation);
7861
7862 } while (status == PSA_OPERATION_INCOMPLETE);
7863
7864 PSA_ASSERT(status);
7865
7866 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7867
7868 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7869
Paul Elliotta4cb9092023-02-07 18:01:55 +00007870exit:
7871 /*
7872 * Key attributes may have been returned by psa_get_key_attributes()
7873 * thus reset them as required.
7874 */
7875 psa_reset_key_attributes(&attributes);
7876
7877 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007878 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007879 PSA_DONE();
7880}
7881/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007882
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007883/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007884void sign_message_deterministic(int key_type_arg,
7885 data_t *key_data,
7886 int alg_arg,
7887 data_t *input_data,
7888 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007889{
7890 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7891 psa_key_type_t key_type = key_type_arg;
7892 psa_algorithm_t alg = alg_arg;
7893 size_t key_bits;
7894 unsigned char *signature = NULL;
7895 size_t signature_size;
7896 size_t signature_length = 0xdeadbeef;
7897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7898
Gilles Peskine449bd832023-01-11 14:50:10 +01007899 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007900
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7902 psa_set_key_algorithm(&attributes, alg);
7903 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007904
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7906 &key));
7907 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7908 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007909
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7911 TEST_ASSERT(signature_size != 0);
7912 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007913 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007914
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 PSA_ASSERT(psa_sign_message(key, alg,
7916 input_data->x, input_data->len,
7917 signature, signature_size,
7918 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007919
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007920 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007921 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007922
7923exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007925
Gilles Peskine449bd832023-01-11 14:50:10 +01007926 psa_destroy_key(key);
7927 mbedtls_free(signature);
7928 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007929
7930}
7931/* END_CASE */
7932
7933/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007934void sign_message_fail(int key_type_arg,
7935 data_t *key_data,
7936 int alg_arg,
7937 data_t *input_data,
7938 int signature_size_arg,
7939 int expected_status_arg)
7940{
7941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7942 psa_key_type_t key_type = key_type_arg;
7943 psa_algorithm_t alg = alg_arg;
7944 size_t signature_size = signature_size_arg;
7945 psa_status_t actual_status;
7946 psa_status_t expected_status = expected_status_arg;
7947 unsigned char *signature = NULL;
7948 size_t signature_length = 0xdeadbeef;
7949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7950
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007951 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007952
7953 PSA_ASSERT(psa_crypto_init());
7954
7955 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7956 psa_set_key_algorithm(&attributes, alg);
7957 psa_set_key_type(&attributes, key_type);
7958
7959 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7960 &key));
7961
7962 actual_status = psa_sign_message(key, alg,
7963 input_data->x, input_data->len,
7964 signature, signature_size,
7965 &signature_length);
7966 TEST_EQUAL(actual_status, expected_status);
7967 /* The value of *signature_length is unspecified on error, but
7968 * whatever it is, it should be less than signature_size, so that
7969 * if the caller tries to read *signature_length bytes without
7970 * checking the error code then they don't overflow a buffer. */
7971 TEST_LE_U(signature_length, signature_size);
7972
7973exit:
7974 psa_reset_key_attributes(&attributes);
7975 psa_destroy_key(key);
7976 mbedtls_free(signature);
7977 PSA_DONE();
7978}
7979/* END_CASE */
7980
7981/* BEGIN_CASE */
7982void sign_verify_message(int key_type_arg,
7983 data_t *key_data,
7984 int alg_arg,
7985 data_t *input_data)
7986{
7987 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7988 psa_key_type_t key_type = key_type_arg;
7989 psa_algorithm_t alg = alg_arg;
7990 size_t key_bits;
7991 unsigned char *signature = NULL;
7992 size_t signature_size;
7993 size_t signature_length = 0xdeadbeef;
7994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7995
7996 PSA_ASSERT(psa_crypto_init());
7997
7998 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7999 PSA_KEY_USAGE_VERIFY_MESSAGE);
8000 psa_set_key_algorithm(&attributes, alg);
8001 psa_set_key_type(&attributes, key_type);
8002
8003 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8004 &key));
8005 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8006 key_bits = psa_get_key_bits(&attributes);
8007
8008 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8009 TEST_ASSERT(signature_size != 0);
8010 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008011 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008012
8013 PSA_ASSERT(psa_sign_message(key, alg,
8014 input_data->x, input_data->len,
8015 signature, signature_size,
8016 &signature_length));
8017 TEST_LE_U(signature_length, signature_size);
8018 TEST_ASSERT(signature_length > 0);
8019
8020 PSA_ASSERT(psa_verify_message(key, alg,
8021 input_data->x, input_data->len,
8022 signature, signature_length));
8023
8024 if (input_data->len != 0) {
8025 /* Flip a bit in the input and verify that the signature is now
8026 * detected as invalid. Flip a bit at the beginning, not at the end,
8027 * because ECDSA may ignore the last few bits of the input. */
8028 input_data->x[0] ^= 1;
8029 TEST_EQUAL(psa_verify_message(key, alg,
8030 input_data->x, input_data->len,
8031 signature, signature_length),
8032 PSA_ERROR_INVALID_SIGNATURE);
8033 }
8034
8035exit:
8036 psa_reset_key_attributes(&attributes);
8037
8038 psa_destroy_key(key);
8039 mbedtls_free(signature);
8040 PSA_DONE();
8041}
8042/* END_CASE */
8043
8044/* BEGIN_CASE */
8045void verify_message(int key_type_arg,
8046 data_t *key_data,
8047 int alg_arg,
8048 data_t *input_data,
8049 data_t *signature_data)
8050{
8051 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8052 psa_key_type_t key_type = key_type_arg;
8053 psa_algorithm_t alg = alg_arg;
8054 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8055
8056 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8057
8058 PSA_ASSERT(psa_crypto_init());
8059
8060 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8061 psa_set_key_algorithm(&attributes, alg);
8062 psa_set_key_type(&attributes, key_type);
8063
8064 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8065 &key));
8066
8067 PSA_ASSERT(psa_verify_message(key, alg,
8068 input_data->x, input_data->len,
8069 signature_data->x, signature_data->len));
8070
8071exit:
8072 psa_reset_key_attributes(&attributes);
8073 psa_destroy_key(key);
8074 PSA_DONE();
8075}
8076/* END_CASE */
8077
8078/* BEGIN_CASE */
8079void verify_message_fail(int key_type_arg,
8080 data_t *key_data,
8081 int alg_arg,
8082 data_t *hash_data,
8083 data_t *signature_data,
8084 int expected_status_arg)
8085{
8086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8087 psa_key_type_t key_type = key_type_arg;
8088 psa_algorithm_t alg = alg_arg;
8089 psa_status_t actual_status;
8090 psa_status_t expected_status = expected_status_arg;
8091 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8092
8093 PSA_ASSERT(psa_crypto_init());
8094
8095 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8096 psa_set_key_algorithm(&attributes, alg);
8097 psa_set_key_type(&attributes, key_type);
8098
8099 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8100 &key));
8101
8102 actual_status = psa_verify_message(key, alg,
8103 hash_data->x, hash_data->len,
8104 signature_data->x,
8105 signature_data->len);
8106 TEST_EQUAL(actual_status, expected_status);
8107
8108exit:
8109 psa_reset_key_attributes(&attributes);
8110 psa_destroy_key(key);
8111 PSA_DONE();
8112}
8113/* END_CASE */
8114
8115/* BEGIN_CASE */
8116void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008117 data_t *key_data,
8118 int alg_arg,
8119 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008120 data_t *label,
8121 int expected_output_length_arg,
8122 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008123{
Ronald Cron5425a212020-08-04 14:58:35 +02008124 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008125 psa_key_type_t key_type = key_type_arg;
8126 psa_algorithm_t alg = alg_arg;
8127 size_t expected_output_length = expected_output_length_arg;
8128 size_t key_bits;
8129 unsigned char *output = NULL;
8130 size_t output_size;
8131 size_t output_length = ~0;
8132 psa_status_t actual_status;
8133 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008135
Gilles Peskine449bd832023-01-11 14:50:10 +01008136 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008137
Gilles Peskine656896e2018-06-29 19:12:28 +02008138 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008139 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8140 psa_set_key_algorithm(&attributes, alg);
8141 psa_set_key_type(&attributes, key_type);
8142 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8143 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008144
8145 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008146 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8147 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008148
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8150 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008151 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008152
8153 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008154 actual_status = psa_asymmetric_encrypt(key, alg,
8155 input_data->x, input_data->len,
8156 label->x, label->len,
8157 output, output_size,
8158 &output_length);
8159 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008160 if (actual_status == PSA_SUCCESS) {
8161 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008162 } else {
8163 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008164 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008165
Gilles Peskine68428122018-06-30 18:42:41 +02008166 /* If the label is empty, the test framework puts a non-null pointer
8167 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008168 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008169 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008170 if (output_size != 0) {
8171 memset(output, 0, output_size);
8172 }
8173 actual_status = psa_asymmetric_encrypt(key, alg,
8174 input_data->x, input_data->len,
8175 NULL, label->len,
8176 output, output_size,
8177 &output_length);
8178 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008179 if (actual_status == PSA_SUCCESS) {
8180 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008181 } else {
8182 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008183 }
Gilles Peskine68428122018-06-30 18:42:41 +02008184 }
8185
Gilles Peskine656896e2018-06-29 19:12:28 +02008186exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008187 /*
8188 * Key attributes may have been returned by psa_get_key_attributes()
8189 * thus reset them as required.
8190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008191 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008192
Gilles Peskine449bd832023-01-11 14:50:10 +01008193 psa_destroy_key(key);
8194 mbedtls_free(output);
8195 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008196}
8197/* END_CASE */
8198
8199/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008200void asymmetric_encrypt_decrypt(int key_type_arg,
8201 data_t *key_data,
8202 int alg_arg,
8203 data_t *input_data,
8204 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008205{
Ronald Cron5425a212020-08-04 14:58:35 +02008206 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008207 psa_key_type_t key_type = key_type_arg;
8208 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008209 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008210 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008211 size_t output_size;
8212 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008213 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008214 size_t output2_size;
8215 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008217
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008219
Gilles Peskine449bd832023-01-11 14:50:10 +01008220 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8221 psa_set_key_algorithm(&attributes, alg);
8222 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008223
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8225 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008226
8227 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8229 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008230
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8232 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008233 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008234
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008235 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008236 TEST_LE_U(output2_size,
8237 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8238 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008239 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008240
Gilles Peskineeebd7382018-06-08 18:11:54 +02008241 /* We test encryption by checking that encrypt-then-decrypt gives back
8242 * the original plaintext because of the non-optional random
8243 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8245 input_data->x, input_data->len,
8246 label->x, label->len,
8247 output, output_size,
8248 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008249 /* We don't know what ciphertext length to expect, but check that
8250 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008251 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008252
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8254 output, output_length,
8255 label->x, label->len,
8256 output2, output2_size,
8257 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008258 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008259 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008260
8261exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008262 /*
8263 * Key attributes may have been returned by psa_get_key_attributes()
8264 * thus reset them as required.
8265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008266 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008267
Gilles Peskine449bd832023-01-11 14:50:10 +01008268 psa_destroy_key(key);
8269 mbedtls_free(output);
8270 mbedtls_free(output2);
8271 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008272}
8273/* END_CASE */
8274
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008275/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008276void asymmetric_decrypt(int key_type_arg,
8277 data_t *key_data,
8278 int alg_arg,
8279 data_t *input_data,
8280 data_t *label,
8281 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008282{
Ronald Cron5425a212020-08-04 14:58:35 +02008283 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008284 psa_key_type_t key_type = key_type_arg;
8285 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008286 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008287 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008288 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008289 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008291
Gilles Peskine449bd832023-01-11 14:50:10 +01008292 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008293
Gilles Peskine449bd832023-01-11 14:50:10 +01008294 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8295 psa_set_key_algorithm(&attributes, alg);
8296 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008297
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8299 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008300
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8302 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008303
8304 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008305 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8306 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008307 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008308
Gilles Peskine449bd832023-01-11 14:50:10 +01008309 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8310 input_data->x, input_data->len,
8311 label->x, label->len,
8312 output,
8313 output_size,
8314 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008315 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008316 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008317
Gilles Peskine68428122018-06-30 18:42:41 +02008318 /* If the label is empty, the test framework puts a non-null pointer
8319 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008320 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008321 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 if (output_size != 0) {
8323 memset(output, 0, output_size);
8324 }
8325 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8326 input_data->x, input_data->len,
8327 NULL, label->len,
8328 output,
8329 output_size,
8330 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008331 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008332 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008333 }
8334
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008335exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008336 psa_reset_key_attributes(&attributes);
8337 psa_destroy_key(key);
8338 mbedtls_free(output);
8339 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008340}
8341/* END_CASE */
8342
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008343/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008344void asymmetric_decrypt_fail(int key_type_arg,
8345 data_t *key_data,
8346 int alg_arg,
8347 data_t *input_data,
8348 data_t *label,
8349 int output_size_arg,
8350 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008351{
Ronald Cron5425a212020-08-04 14:58:35 +02008352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008353 psa_key_type_t key_type = key_type_arg;
8354 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008355 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008356 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008357 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008358 psa_status_t actual_status;
8359 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008361
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008362 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008363
Gilles Peskine449bd832023-01-11 14:50:10 +01008364 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008365
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8367 psa_set_key_algorithm(&attributes, alg);
8368 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008369
Gilles Peskine449bd832023-01-11 14:50:10 +01008370 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8371 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008372
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 actual_status = psa_asymmetric_decrypt(key, alg,
8374 input_data->x, input_data->len,
8375 label->x, label->len,
8376 output, output_size,
8377 &output_length);
8378 TEST_EQUAL(actual_status, expected_status);
8379 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008380
Gilles Peskine68428122018-06-30 18:42:41 +02008381 /* If the label is empty, the test framework puts a non-null pointer
8382 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008384 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 if (output_size != 0) {
8386 memset(output, 0, output_size);
8387 }
8388 actual_status = psa_asymmetric_decrypt(key, alg,
8389 input_data->x, input_data->len,
8390 NULL, label->len,
8391 output, output_size,
8392 &output_length);
8393 TEST_EQUAL(actual_status, expected_status);
8394 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008395 }
8396
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008397exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 psa_reset_key_attributes(&attributes);
8399 psa_destroy_key(key);
8400 mbedtls_free(output);
8401 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008402}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008403/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008404
8405/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008406void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008407{
8408 /* Test each valid way of initializing the object, except for `= {0}`, as
8409 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8410 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008411 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008412 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008413 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008414 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8415 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008416
Gilles Peskine449bd832023-01-11 14:50:10 +01008417 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008418
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008419 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008420 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8421 PSA_ERROR_BAD_STATE);
8422 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8423 PSA_ERROR_BAD_STATE);
8424 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8425 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008426
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008427 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008428 PSA_ASSERT(psa_key_derivation_abort(&func));
8429 PSA_ASSERT(psa_key_derivation_abort(&init));
8430 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008431}
8432/* END_CASE */
8433
Janos Follath16de4a42019-06-13 16:32:24 +01008434/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008435void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008436{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008437 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008438 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008439 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008440
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008442
Gilles Peskine449bd832023-01-11 14:50:10 +01008443 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8444 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008445
8446exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008447 psa_key_derivation_abort(&operation);
8448 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008449}
8450/* END_CASE */
8451
Janos Follathaf3c2a02019-06-12 12:34:34 +01008452/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008453void derive_set_capacity(int alg_arg, int capacity_arg,
8454 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008455{
8456 psa_algorithm_t alg = alg_arg;
8457 size_t capacity = capacity_arg;
8458 psa_status_t expected_status = expected_status_arg;
8459 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8460
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008462
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008464
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8466 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008467
8468exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 psa_key_derivation_abort(&operation);
8470 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008471}
8472/* END_CASE */
8473
8474/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308475void parse_binary_string_test(data_t *input, int output)
8476{
8477 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308478 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308479 TEST_EQUAL(value, output);
8480}
8481/* END_CASE */
8482
8483/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008484void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308485 int step_arg1, int key_type_arg1, data_t *input1,
8486 int expected_status_arg1,
8487 int step_arg2, int key_type_arg2, data_t *input2,
8488 int expected_status_arg2,
8489 int step_arg3, int key_type_arg3, data_t *input3,
8490 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008492{
8493 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308495 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008496 psa_status_t expected_statuses[] = { expected_status_arg1,
8497 expected_status_arg2,
8498 expected_status_arg3 };
8499 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008500 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8501 MBEDTLS_SVC_KEY_ID_INIT,
8502 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008503 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8505 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008506 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008507 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008508 psa_status_t expected_output_status = expected_output_status_arg;
8509 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008510
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008512
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8514 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008515
Gilles Peskine449bd832023-01-11 14:50:10 +01008516 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008517
Gilles Peskine449bd832023-01-11 14:50:10 +01008518 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8519 mbedtls_test_set_step(i);
8520 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008521 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308522 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8523 key_types[i] != INPUT_INTEGER) {
8524 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 PSA_ASSERT(psa_import_key(&attributes,
8526 inputs[i]->x, inputs[i]->len,
8527 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308528 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008530 // When taking a private key as secret input, use key agreement
8531 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8533 &operation, keys[i]),
8534 expected_statuses[i]);
8535 } else {
8536 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8537 keys[i]),
8538 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008539 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308541 if (key_types[i] == INPUT_INTEGER) {
8542 TEST_EQUAL(psa_key_derivation_input_integer(
8543 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308544 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308545 expected_statuses[i]);
8546 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308547 TEST_EQUAL(psa_key_derivation_input_bytes(
8548 &operation, steps[i],
8549 inputs[i]->x, inputs[i]->len),
8550 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308551 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008552 }
8553 }
8554
Gilles Peskine449bd832023-01-11 14:50:10 +01008555 if (output_key_type != PSA_KEY_TYPE_NONE) {
8556 psa_reset_key_attributes(&attributes);
8557 psa_set_key_type(&attributes, output_key_type);
8558 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008559 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008560 psa_key_derivation_output_key(&attributes, &operation,
8561 &output_key);
8562 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008563 uint8_t buffer[1];
8564 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008565 psa_key_derivation_output_bytes(&operation,
8566 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008567 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008569
Janos Follathaf3c2a02019-06-12 12:34:34 +01008570exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008571 psa_key_derivation_abort(&operation);
8572 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8573 psa_destroy_key(keys[i]);
8574 }
8575 psa_destroy_key(output_key);
8576 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008577}
8578/* END_CASE */
8579
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308580/* BEGIN_CASE*/
8581void derive_input_invalid_cost(int alg_arg, int64_t cost)
8582{
8583 psa_algorithm_t alg = alg_arg;
8584 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8585
8586 PSA_ASSERT(psa_crypto_init());
8587 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8588
8589 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8590 PSA_KEY_DERIVATION_INPUT_COST,
8591 cost),
8592 PSA_ERROR_NOT_SUPPORTED);
8593
8594exit:
8595 psa_key_derivation_abort(&operation);
8596 PSA_DONE();
8597}
8598/* END_CASE*/
8599
Janos Follathd958bb72019-07-03 15:02:16 +01008600/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008601void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008602{
Janos Follathd958bb72019-07-03 15:02:16 +01008603 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008604 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008605 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008606 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008607 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008609 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008611 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008613 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8614 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008617
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008619
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8621 psa_set_key_algorithm(&attributes, alg);
8622 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008623
Gilles Peskine449bd832023-01-11 14:50:10 +01008624 PSA_ASSERT(psa_import_key(&attributes,
8625 key_data, sizeof(key_data),
8626 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008627
8628 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8630 input1, input1_length,
8631 input2, input2_length,
8632 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008635
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008636 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8638 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008639
Gilles Peskine449bd832023-01-11 14:50:10 +01008640 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008641
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8643 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008644
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008645exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 psa_key_derivation_abort(&operation);
8647 psa_destroy_key(key);
8648 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008649}
8650/* END_CASE */
8651
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008652/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008653void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008654{
8655 uint8_t output_buffer[16];
8656 size_t buffer_size = 16;
8657 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008658 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008659
Gilles Peskine449bd832023-01-11 14:50:10 +01008660 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8661 output_buffer, buffer_size)
8662 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008663
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8665 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008666
Gilles Peskine449bd832023-01-11 14:50:10 +01008667 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008668
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8670 output_buffer, buffer_size)
8671 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008672
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8674 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008675
8676exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008678}
8679/* END_CASE */
8680
8681/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008682void derive_output(int alg_arg,
8683 int step1_arg, data_t *input1, int expected_status_arg1,
8684 int step2_arg, data_t *input2, int expected_status_arg2,
8685 int step3_arg, data_t *input3, int expected_status_arg3,
8686 int step4_arg, data_t *input4, int expected_status_arg4,
8687 data_t *key_agreement_peer_key,
8688 int requested_capacity_arg,
8689 data_t *expected_output1,
8690 data_t *expected_output2,
8691 int other_key_input_type,
8692 int key_input_type,
8693 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008694{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008695 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008696 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8697 data_t *inputs[] = { input1, input2, input3, input4 };
8698 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8699 MBEDTLS_SVC_KEY_ID_INIT,
8700 MBEDTLS_SVC_KEY_ID_INIT,
8701 MBEDTLS_SVC_KEY_ID_INIT };
8702 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8703 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008704 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008705 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008706 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008708 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008710 size_t output_buffer_size = 0;
8711 uint8_t *output_buffer = NULL;
8712 size_t expected_capacity;
8713 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008714 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8715 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8716 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8717 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008718 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008719 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008720 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008721
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8723 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008724 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 }
8726 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008727 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008729 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008730 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008731 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008732
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008733 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8735 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8736 requested_capacity));
8737 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8738 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008739 case 0:
8740 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308741 case PSA_KEY_DERIVATION_INPUT_COST:
8742 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308743 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308744 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308745 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308746 if (statuses[i] != PSA_SUCCESS) {
8747 goto exit;
8748 }
8749 break;
8750 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008751 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008753 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008754 TEST_EQUAL(psa_key_derivation_input_bytes(
8755 &operation, steps[i],
8756 inputs[i]->x, inputs[i]->len),
8757 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008758
Gilles Peskine449bd832023-01-11 14:50:10 +01008759 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008760 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008762 break;
8763 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8765 psa_set_key_algorithm(&attributes1, alg);
8766 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008767
Gilles Peskine449bd832023-01-11 14:50:10 +01008768 PSA_ASSERT(psa_import_key(&attributes1,
8769 inputs[i]->x, inputs[i]->len,
8770 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008771
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8773 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8774 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8775 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008776 }
8777
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308778 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308780 keys[i]),
8781 statuses[i]);
8782
8783 if (statuses[i] != PSA_SUCCESS) {
8784 goto exit;
8785 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008786 break;
8787 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008788 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008789 break;
8790 }
8791 break;
8792 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008794 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8796 steps[i],
8797 inputs[i]->x,
8798 inputs[i]->len),
8799 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008800 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008801 case 1: // input key, type DERIVE
8802 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008803 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8804 psa_set_key_algorithm(&attributes2, alg);
8805 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008806
8807 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 if (other_key_input_type == 11) {
8809 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8810 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008811
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 PSA_ASSERT(psa_import_key(&attributes2,
8813 inputs[i]->x, inputs[i]->len,
8814 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008815
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8817 steps[i],
8818 keys[i]),
8819 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008820 break;
8821 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8823 psa_set_key_algorithm(&attributes3, alg);
8824 psa_set_key_type(&attributes3,
8825 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008826
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 PSA_ASSERT(psa_import_key(&attributes3,
8828 inputs[i]->x, inputs[i]->len,
8829 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008830
Gilles Peskine449bd832023-01-11 14:50:10 +01008831 TEST_EQUAL(psa_key_derivation_key_agreement(
8832 &operation,
8833 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8834 keys[i], key_agreement_peer_key->x,
8835 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008836 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008837 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008838 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008839 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008840 }
8841
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008843 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008844 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008845 break;
8846 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008847 TEST_EQUAL(psa_key_derivation_input_bytes(
8848 &operation, steps[i],
8849 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008850
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008852 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008854 break;
8855 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008856 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008857
Gilles Peskine449bd832023-01-11 14:50:10 +01008858 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8859 &current_capacity));
8860 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008861 expected_capacity = requested_capacity;
8862
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008864 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8865
8866 /* For output key derivation secret must be provided using
8867 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008868 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008869 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008871
Gilles Peskine449bd832023-01-11 14:50:10 +01008872 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8873 psa_set_key_algorithm(&attributes4, alg);
8874 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8875 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008876
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8878 &derived_key), expected_status);
8879 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008880 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008882 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 status = psa_key_derivation_output_bytes(&operation,
8884 output_buffer, output_sizes[i]);
8885 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008886 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 TEST_ASSERT(status == PSA_SUCCESS ||
8888 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008889 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 } else if (expected_capacity == 0 ||
8891 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008892 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008893 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008894 expected_capacity = 0;
8895 continue;
8896 }
8897 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 PSA_ASSERT(status);
8899 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008900 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008901 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008902 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008903 /* Check the operation status. */
8904 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8906 &current_capacity));
8907 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008908 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008909 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008911
8912exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 mbedtls_free(output_buffer);
8914 psa_key_derivation_abort(&operation);
8915 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8916 psa_destroy_key(keys[i]);
8917 }
8918 psa_destroy_key(derived_key);
8919 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008920}
8921/* END_CASE */
8922
8923/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008924void derive_full(int alg_arg,
8925 data_t *key_data,
8926 data_t *input1,
8927 data_t *input2,
8928 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008929{
Ronald Cron5425a212020-08-04 14:58:35 +02008930 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008931 psa_algorithm_t alg = alg_arg;
8932 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008933 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008934 unsigned char output_buffer[16];
8935 size_t expected_capacity = requested_capacity;
8936 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008938
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008940
Gilles Peskine449bd832023-01-11 14:50:10 +01008941 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8942 psa_set_key_algorithm(&attributes, alg);
8943 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008944
Gilles Peskine449bd832023-01-11 14:50:10 +01008945 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8946 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008947
Gilles Peskine449bd832023-01-11 14:50:10 +01008948 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8949 input1->x, input1->len,
8950 input2->x, input2->len,
8951 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008952 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008953 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008954
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8956 &current_capacity));
8957 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008958
8959 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008960 while (current_capacity > 0) {
8961 size_t read_size = sizeof(output_buffer);
8962 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008963 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008964 }
8965 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8966 output_buffer,
8967 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008968 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008969 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8970 &current_capacity));
8971 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008972 }
8973
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008974 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008975 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8976 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008977
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008979
8980exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 psa_key_derivation_abort(&operation);
8982 psa_destroy_key(key);
8983 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008984}
8985/* END_CASE */
8986
Stephan Koch78109f52023-04-12 14:19:36 +02008987/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008988void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8989 int derivation_step,
8990 int capacity, int expected_capacity_status_arg,
8991 data_t *expected_output,
8992 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008993{
8994 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8995 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008996 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008997 uint8_t *output_buffer = NULL;
8998 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008999 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9000 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9001 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009002
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009003 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009004 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009005
Gilles Peskine449bd832023-01-11 14:50:10 +01009006 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9007 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9008 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009009
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9011 step, input->x, input->len),
9012 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009013
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009015 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009017
Gilles Peskine449bd832023-01-11 14:50:10 +01009018 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9019 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009020
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 TEST_EQUAL(status, expected_output_status);
9022 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009023 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009024 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009026
9027exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009028 mbedtls_free(output_buffer);
9029 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009030 PSA_DONE();
9031}
9032/* END_CASE */
9033
Janos Follathe60c9052019-07-03 13:51:30 +01009034/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009035void derive_key_exercise(int alg_arg,
9036 data_t *key_data,
9037 data_t *input1,
9038 data_t *input2,
9039 int derived_type_arg,
9040 int derived_bits_arg,
9041 int derived_usage_arg,
9042 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009043{
Ronald Cron5425a212020-08-04 14:58:35 +02009044 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9045 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009046 psa_algorithm_t alg = alg_arg;
9047 psa_key_type_t derived_type = derived_type_arg;
9048 size_t derived_bits = derived_bits_arg;
9049 psa_key_usage_t derived_usage = derived_usage_arg;
9050 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009051 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009052 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009054 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009055
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009057
Gilles Peskine449bd832023-01-11 14:50:10 +01009058 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9059 psa_set_key_algorithm(&attributes, alg);
9060 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9061 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9062 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009063
9064 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9066 input1->x, input1->len,
9067 input2->x, input2->len,
9068 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009069 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009070 }
Janos Follathe60c9052019-07-03 13:51:30 +01009071
Gilles Peskine449bd832023-01-11 14:50:10 +01009072 psa_set_key_usage_flags(&attributes, derived_usage);
9073 psa_set_key_algorithm(&attributes, derived_alg);
9074 psa_set_key_type(&attributes, derived_type);
9075 psa_set_key_bits(&attributes, derived_bits);
9076 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9077 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009078
9079 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009080 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9081 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9082 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009083
9084 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009086 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009088
9089exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009090 /*
9091 * Key attributes may have been returned by psa_get_key_attributes()
9092 * thus reset them as required.
9093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009095
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 psa_key_derivation_abort(&operation);
9097 psa_destroy_key(base_key);
9098 psa_destroy_key(derived_key);
9099 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009100}
9101/* END_CASE */
9102
Janos Follath42fd8882019-07-03 14:17:09 +01009103/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009104void derive_key_export(int alg_arg,
9105 data_t *key_data,
9106 data_t *input1,
9107 data_t *input2,
9108 int bytes1_arg,
9109 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009110{
Ronald Cron5425a212020-08-04 14:58:35 +02009111 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9112 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009113 psa_algorithm_t alg = alg_arg;
9114 size_t bytes1 = bytes1_arg;
9115 size_t bytes2 = bytes2_arg;
9116 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009117 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009118 uint8_t *output_buffer = NULL;
9119 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009120 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9121 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009122 size_t length;
9123
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009124 TEST_CALLOC(output_buffer, capacity);
9125 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009127
Gilles Peskine449bd832023-01-11 14:50:10 +01009128 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9129 psa_set_key_algorithm(&base_attributes, alg);
9130 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9131 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9132 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009133
9134 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009135 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9136 input1->x, input1->len,
9137 input2->x, input2->len,
9138 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009139 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 }
Janos Follath42fd8882019-07-03 14:17:09 +01009141
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9143 output_buffer,
9144 capacity));
9145 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009146
9147 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9149 input1->x, input1->len,
9150 input2->x, input2->len,
9151 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 }
Janos Follath42fd8882019-07-03 14:17:09 +01009154
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9156 psa_set_key_algorithm(&derived_attributes, 0);
9157 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9158 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9159 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9160 &derived_key));
9161 PSA_ASSERT(psa_export_key(derived_key,
9162 export_buffer, bytes1,
9163 &length));
9164 TEST_EQUAL(length, bytes1);
9165 PSA_ASSERT(psa_destroy_key(derived_key));
9166 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
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, bytes2,
9171 &length));
9172 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009173
9174 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009175 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009176 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009177
9178exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009179 mbedtls_free(output_buffer);
9180 mbedtls_free(export_buffer);
9181 psa_key_derivation_abort(&operation);
9182 psa_destroy_key(base_key);
9183 psa_destroy_key(derived_key);
9184 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009185}
9186/* END_CASE */
9187
9188/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009189void derive_key_type(int alg_arg,
9190 data_t *key_data,
9191 data_t *input1,
9192 data_t *input2,
9193 int key_type_arg, int bits_arg,
9194 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009195{
9196 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9197 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9198 const psa_algorithm_t alg = alg_arg;
9199 const psa_key_type_t key_type = key_type_arg;
9200 const size_t bits = bits_arg;
9201 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9202 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009203 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009204 uint8_t *export_buffer = NULL;
9205 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9206 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9207 size_t export_length;
9208
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009209 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009210 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009211
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9213 psa_set_key_algorithm(&base_attributes, alg);
9214 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9215 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9216 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009217
Gilles Peskine449bd832023-01-11 14:50:10 +01009218 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009219 &operation, base_key, alg,
9220 input1->x, input1->len,
9221 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009222 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009224 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009225
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9227 psa_set_key_algorithm(&derived_attributes, 0);
9228 psa_set_key_type(&derived_attributes, key_type);
9229 psa_set_key_bits(&derived_attributes, bits);
9230 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9231 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009232
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 PSA_ASSERT(psa_export_key(derived_key,
9234 export_buffer, export_buffer_size,
9235 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009236 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009237 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009238
9239exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 mbedtls_free(export_buffer);
9241 psa_key_derivation_abort(&operation);
9242 psa_destroy_key(base_key);
9243 psa_destroy_key(derived_key);
9244 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009245}
9246/* END_CASE */
9247
9248/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009249void derive_key(int alg_arg,
9250 data_t *key_data, data_t *input1, data_t *input2,
9251 int type_arg, int bits_arg,
9252 int expected_status_arg,
9253 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009254{
Ronald Cron5425a212020-08-04 14:58:35 +02009255 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9256 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009257 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009258 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009259 size_t bits = bits_arg;
9260 psa_status_t expected_status = expected_status_arg;
9261 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9262 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9263 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9264
Gilles Peskine449bd832023-01-11 14:50:10 +01009265 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009266
Gilles Peskine449bd832023-01-11 14:50:10 +01009267 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9268 psa_set_key_algorithm(&base_attributes, alg);
9269 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9270 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9271 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009272
Gilles Peskine449bd832023-01-11 14:50:10 +01009273 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9274 input1->x, input1->len,
9275 input2->x, input2->len,
9276 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009277 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009279
Gilles Peskine449bd832023-01-11 14:50:10 +01009280 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9281 psa_set_key_algorithm(&derived_attributes, 0);
9282 psa_set_key_type(&derived_attributes, type);
9283 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009284
9285 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009286 psa_key_derivation_output_key(&derived_attributes,
9287 &operation,
9288 &derived_key);
9289 if (is_large_output > 0) {
9290 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9291 }
9292 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009293
9294exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009295 psa_key_derivation_abort(&operation);
9296 psa_destroy_key(base_key);
9297 psa_destroy_key(derived_key);
9298 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009299}
9300/* END_CASE */
9301
9302/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009303void key_agreement_setup(int alg_arg,
9304 int our_key_type_arg, int our_key_alg_arg,
9305 data_t *our_key_data, data_t *peer_key_data,
9306 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009307{
Ronald Cron5425a212020-08-04 14:58:35 +02009308 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009309 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009310 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009311 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009312 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009314 psa_status_t expected_status = expected_status_arg;
9315 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009316
Gilles Peskine449bd832023-01-11 14:50:10 +01009317 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009318
Gilles Peskine449bd832023-01-11 14:50:10 +01009319 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9320 psa_set_key_algorithm(&attributes, our_key_alg);
9321 psa_set_key_type(&attributes, our_key_type);
9322 PSA_ASSERT(psa_import_key(&attributes,
9323 our_key_data->x, our_key_data->len,
9324 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009325
Gilles Peskine77f40d82019-04-11 21:27:06 +02009326 /* The tests currently include inputs that should fail at either step.
9327 * Test cases that fail at the setup step should be changed to call
9328 * key_derivation_setup instead, and this function should be renamed
9329 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 status = psa_key_derivation_setup(&operation, alg);
9331 if (status == PSA_SUCCESS) {
9332 TEST_EQUAL(psa_key_derivation_key_agreement(
9333 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9334 our_key,
9335 peer_key_data->x, peer_key_data->len),
9336 expected_status);
9337 } else {
9338 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009339 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009340
9341exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009342 psa_key_derivation_abort(&operation);
9343 psa_destroy_key(our_key);
9344 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009345}
9346/* END_CASE */
9347
9348/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009349void raw_key_agreement(int alg_arg,
9350 int our_key_type_arg, data_t *our_key_data,
9351 data_t *peer_key_data,
9352 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009353{
Ronald Cron5425a212020-08-04 14:58:35 +02009354 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009355 psa_algorithm_t alg = alg_arg;
9356 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009358 unsigned char *output = NULL;
9359 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009360 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009361
Gilles Peskine449bd832023-01-11 14:50:10 +01009362 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009363
Gilles Peskine449bd832023-01-11 14:50:10 +01009364 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9365 psa_set_key_algorithm(&attributes, alg);
9366 psa_set_key_type(&attributes, our_key_type);
9367 PSA_ASSERT(psa_import_key(&attributes,
9368 our_key_data->x, our_key_data->len,
9369 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009370
Gilles Peskine449bd832023-01-11 14:50:10 +01009371 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9372 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009373
Gilles Peskine992bee82022-04-13 23:25:52 +02009374 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 TEST_LE_U(expected_output->len,
9376 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9377 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9378 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009379
9380 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009381 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009382 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9383 peer_key_data->x, peer_key_data->len,
9384 output, expected_output->len,
9385 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009386 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009387 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009388 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009389 output = NULL;
9390 output_length = ~0;
9391
9392 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009393 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009394 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9395 peer_key_data->x, peer_key_data->len,
9396 output, expected_output->len + 1,
9397 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009398 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009399 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009400 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009401 output = NULL;
9402 output_length = ~0;
9403
9404 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009405 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009406 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9407 peer_key_data->x, peer_key_data->len,
9408 output, expected_output->len - 1,
9409 &output_length),
9410 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009411 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009412 TEST_LE_U(output_length, expected_output->len - 1);
9413 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009414 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009415
9416exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009417 mbedtls_free(output);
9418 psa_destroy_key(our_key);
9419 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009420}
9421/* END_CASE */
9422
9423/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009424void key_agreement_capacity(int alg_arg,
9425 int our_key_type_arg, data_t *our_key_data,
9426 data_t *peer_key_data,
9427 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009428{
Ronald Cron5425a212020-08-04 14:58:35 +02009429 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009430 psa_algorithm_t alg = alg_arg;
9431 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009432 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009434 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009435 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009436
Gilles Peskine449bd832023-01-11 14:50:10 +01009437 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009438
Gilles Peskine449bd832023-01-11 14:50:10 +01009439 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9440 psa_set_key_algorithm(&attributes, alg);
9441 psa_set_key_type(&attributes, our_key_type);
9442 PSA_ASSERT(psa_import_key(&attributes,
9443 our_key_data->x, our_key_data->len,
9444 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009445
Gilles Peskine449bd832023-01-11 14:50:10 +01009446 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9447 PSA_ASSERT(psa_key_derivation_key_agreement(
9448 &operation,
9449 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9450 peer_key_data->x, peer_key_data->len));
9451 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009452 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009453 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9454 PSA_KEY_DERIVATION_INPUT_INFO,
9455 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009456 }
Gilles Peskine59685592018-09-18 12:11:34 +02009457
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009458 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009459 PSA_ASSERT(psa_key_derivation_get_capacity(
9460 &operation, &actual_capacity));
9461 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009462
Gilles Peskinebf491972018-10-25 22:36:12 +02009463 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009464 while (actual_capacity > sizeof(output)) {
9465 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9466 output, sizeof(output)));
9467 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009468 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009469 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9470 output, actual_capacity));
9471 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9472 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009473
Gilles Peskine59685592018-09-18 12:11:34 +02009474exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009475 psa_key_derivation_abort(&operation);
9476 psa_destroy_key(our_key);
9477 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009478}
9479/* END_CASE */
9480
9481/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009482void key_agreement_output(int alg_arg,
9483 int our_key_type_arg, data_t *our_key_data,
9484 data_t *peer_key_data,
9485 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009486{
Ronald Cron5425a212020-08-04 14:58:35 +02009487 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009488 psa_algorithm_t alg = alg_arg;
9489 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009490 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009491 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009492 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009493
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009494 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009495 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009496
Gilles Peskine449bd832023-01-11 14:50:10 +01009497 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009498
Gilles Peskine449bd832023-01-11 14:50:10 +01009499 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9500 psa_set_key_algorithm(&attributes, alg);
9501 psa_set_key_type(&attributes, our_key_type);
9502 PSA_ASSERT(psa_import_key(&attributes,
9503 our_key_data->x, our_key_data->len,
9504 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009505
Gilles Peskine449bd832023-01-11 14:50:10 +01009506 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9507 PSA_ASSERT(psa_key_derivation_key_agreement(
9508 &operation,
9509 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9510 peer_key_data->x, peer_key_data->len));
9511 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009512 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009513 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9514 PSA_KEY_DERIVATION_INPUT_INFO,
9515 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009516 }
Gilles Peskine59685592018-09-18 12:11:34 +02009517
Gilles Peskine449bd832023-01-11 14:50:10 +01009518 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9519 actual_output,
9520 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009521 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009522 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009523 if (expected_output2->len != 0) {
9524 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9525 actual_output,
9526 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009527 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009528 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009529 }
Gilles Peskine59685592018-09-18 12:11:34 +02009530
9531exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009532 psa_key_derivation_abort(&operation);
9533 psa_destroy_key(our_key);
9534 PSA_DONE();
9535 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009536}
9537/* END_CASE */
9538
9539/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009540void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009541{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009542 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009543 unsigned char *output = NULL;
9544 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009545 size_t i;
9546 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009547
Gilles Peskine449bd832023-01-11 14:50:10 +01009548 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009549
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009550 TEST_CALLOC(output, bytes);
9551 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009552
Gilles Peskine449bd832023-01-11 14:50:10 +01009553 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009554
Gilles Peskinea50d7392018-06-21 10:22:13 +02009555 /* Run several times, to ensure that every output byte will be
9556 * nonzero at least once with overwhelming probability
9557 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009558 for (run = 0; run < 10; run++) {
9559 if (bytes != 0) {
9560 memset(output, 0, bytes);
9561 }
9562 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009563
Gilles Peskine449bd832023-01-11 14:50:10 +01009564 for (i = 0; i < bytes; i++) {
9565 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009566 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009567 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009568 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009569 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009570
9571 /* Check that every byte was changed to nonzero at least once. This
9572 * validates that psa_generate_random is overwriting every byte of
9573 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009574 for (i = 0; i < bytes; i++) {
9575 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009576 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009577
9578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 PSA_DONE();
9580 mbedtls_free(output);
9581 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009582}
9583/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009584
9585/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009586void generate_key(int type_arg,
9587 int bits_arg,
9588 int usage_arg,
9589 int alg_arg,
9590 int expected_status_arg,
9591 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009592{
Ronald Cron5425a212020-08-04 14:58:35 +02009593 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009594 psa_key_type_t type = type_arg;
9595 psa_key_usage_t usage = usage_arg;
9596 size_t bits = bits_arg;
9597 psa_algorithm_t alg = alg_arg;
9598 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009600 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009601
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009603
Gilles Peskine449bd832023-01-11 14:50:10 +01009604 psa_set_key_usage_flags(&attributes, usage);
9605 psa_set_key_algorithm(&attributes, alg);
9606 psa_set_key_type(&attributes, type);
9607 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009608
9609 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009610 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009611
Gilles Peskine449bd832023-01-11 14:50:10 +01009612 if (is_large_key > 0) {
9613 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9614 }
9615 TEST_EQUAL(status, expected_status);
9616 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009617 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009618 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009619
9620 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009621 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9622 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9623 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009624
Gilles Peskine818ca122018-06-20 18:16:48 +02009625 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009626 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009627 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009628 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009629
9630exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009631 /*
9632 * Key attributes may have been returned by psa_get_key_attributes()
9633 * thus reset them as required.
9634 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009635 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009636
Gilles Peskine449bd832023-01-11 14:50:10 +01009637 psa_destroy_key(key);
9638 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009639}
9640/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009641
Valerio Setti19fec542023-07-25 12:31:50 +02009642/* 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 +01009643void generate_key_rsa(int bits_arg,
9644 data_t *e_arg,
9645 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009646{
Ronald Cron5425a212020-08-04 14:58:35 +02009647 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009648 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009649 size_t bits = bits_arg;
9650 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9651 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9652 psa_status_t expected_status = expected_status_arg;
9653 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9654 uint8_t *exported = NULL;
9655 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009656 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009657 size_t exported_length = SIZE_MAX;
9658 uint8_t *e_read_buffer = NULL;
9659 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009660 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009661 size_t e_read_length = SIZE_MAX;
9662
Gilles Peskine449bd832023-01-11 14:50:10 +01009663 if (e_arg->len == 0 ||
9664 (e_arg->len == 3 &&
9665 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009666 is_default_public_exponent = 1;
9667 e_read_size = 0;
9668 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009669 TEST_CALLOC(e_read_buffer, e_read_size);
9670 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009671
Gilles Peskine449bd832023-01-11 14:50:10 +01009672 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009673
Gilles Peskine449bd832023-01-11 14:50:10 +01009674 psa_set_key_usage_flags(&attributes, usage);
9675 psa_set_key_algorithm(&attributes, alg);
9676 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9677 e_arg->x, e_arg->len));
9678 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009679
9680 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009681 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9682 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009683 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009684 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009685
9686 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009687 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9688 TEST_EQUAL(psa_get_key_type(&attributes), type);
9689 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9690 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9691 e_read_buffer, e_read_size,
9692 &e_read_length));
9693 if (is_default_public_exponent) {
9694 TEST_EQUAL(e_read_length, 0);
9695 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009696 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009697 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009698
9699 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009700 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009701 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009702 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009703
9704 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009705 PSA_ASSERT(psa_export_public_key(key,
9706 exported, exported_size,
9707 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009708 {
9709 uint8_t *p = exported;
9710 uint8_t *end = exported + exported_length;
9711 size_t len;
9712 /* RSAPublicKey ::= SEQUENCE {
9713 * modulus INTEGER, -- n
9714 * publicExponent INTEGER } -- e
9715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009716 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9717 MBEDTLS_ASN1_SEQUENCE |
9718 MBEDTLS_ASN1_CONSTRUCTED));
9719 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9720 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9721 MBEDTLS_ASN1_INTEGER));
9722 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009723 ++p;
9724 --len;
9725 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009726 if (e_arg->len == 0) {
9727 TEST_EQUAL(len, 3);
9728 TEST_EQUAL(p[0], 1);
9729 TEST_EQUAL(p[1], 0);
9730 TEST_EQUAL(p[2], 1);
9731 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009732 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009733 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009734 }
9735
9736exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009737 /*
9738 * Key attributes may have been returned by psa_get_key_attributes() or
9739 * set by psa_set_key_domain_parameters() thus reset them as required.
9740 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009741 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009742
Gilles Peskine449bd832023-01-11 14:50:10 +01009743 psa_destroy_key(key);
9744 PSA_DONE();
9745 mbedtls_free(e_read_buffer);
9746 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009747}
9748/* END_CASE */
9749
Darryl Greend49a4992018-06-18 17:27:26 +01009750/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009751void persistent_key_load_key_from_storage(data_t *data,
9752 int type_arg, int bits_arg,
9753 int usage_flags_arg, int alg_arg,
9754 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009755{
Gilles Peskine449bd832023-01-11 14:50:10 +01009756 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9759 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009760 psa_key_type_t type = type_arg;
9761 size_t bits = bits_arg;
9762 psa_key_usage_t usage_flags = usage_flags_arg;
9763 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009764 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009765 unsigned char *first_export = NULL;
9766 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009767 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -07009768 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +01009769 size_t second_exported_length;
9770
Gilles Peskine449bd832023-01-11 14:50:10 +01009771 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009772 TEST_CALLOC(first_export, export_size);
9773 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009774 }
Darryl Greend49a4992018-06-18 17:27:26 +01009775
Gilles Peskine449bd832023-01-11 14:50:10 +01009776 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009777
Gilles Peskine449bd832023-01-11 14:50:10 +01009778 psa_set_key_id(&attributes, key_id);
9779 psa_set_key_usage_flags(&attributes, usage_flags);
9780 psa_set_key_algorithm(&attributes, alg);
9781 psa_set_key_type(&attributes, type);
9782 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009783
Gilles Peskine449bd832023-01-11 14:50:10 +01009784 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009785 case IMPORT_KEY:
9786 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009787 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9788 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009789 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009790
Darryl Green0c6575a2018-11-07 16:05:30 +00009791 case GENERATE_KEY:
9792 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009793 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009794 break;
9795
9796 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009797#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009798 {
9799 /* Create base key */
9800 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9801 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9802 psa_set_key_usage_flags(&base_attributes,
9803 PSA_KEY_USAGE_DERIVE);
9804 psa_set_key_algorithm(&base_attributes, derive_alg);
9805 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9806 PSA_ASSERT(psa_import_key(&base_attributes,
9807 data->x, data->len,
9808 &base_key));
9809 /* Derive a key. */
9810 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9811 PSA_ASSERT(psa_key_derivation_input_key(
9812 &operation,
9813 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9814 PSA_ASSERT(psa_key_derivation_input_bytes(
9815 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9816 NULL, 0));
9817 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9818 &operation,
9819 &key));
9820 PSA_ASSERT(psa_key_derivation_abort(&operation));
9821 PSA_ASSERT(psa_destroy_key(base_key));
9822 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9823 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009824#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009825 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009826#endif
9827 break;
9828
9829 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009830 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009831 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009832 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009833 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009834
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009835 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009836 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9837 PSA_ASSERT(psa_export_key(key,
9838 first_export, export_size,
9839 &first_exported_length));
9840 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009841 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009842 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01009843 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009844 }
Darryl Greend49a4992018-06-18 17:27:26 +01009845
9846 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009847 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009848 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009849 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009850
Darryl Greend49a4992018-06-18 17:27:26 +01009851 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009852 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9853 TEST_ASSERT(mbedtls_svc_key_id_equal(
9854 psa_get_key_id(&attributes), key_id));
9855 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9856 PSA_KEY_LIFETIME_PERSISTENT);
9857 TEST_EQUAL(psa_get_key_type(&attributes), type);
9858 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9859 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9860 mbedtls_test_update_key_usage_flags(usage_flags));
9861 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009862
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009863 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009864 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9865 PSA_ASSERT(psa_export_key(key,
9866 second_export, export_size,
9867 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009868 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009869 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009870 }
9871
9872 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009873 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009874 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009875 }
Darryl Greend49a4992018-06-18 17:27:26 +01009876
9877exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009878 /*
9879 * Key attributes may have been returned by psa_get_key_attributes()
9880 * thus reset them as required.
9881 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009882 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009883
Gilles Peskine449bd832023-01-11 14:50:10 +01009884 mbedtls_free(first_export);
9885 mbedtls_free(second_export);
9886 psa_key_derivation_abort(&operation);
9887 psa_destroy_key(base_key);
9888 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009889 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009890}
9891/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009892
Neil Armstronga557cb82022-06-10 08:58:32 +02009893/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009894void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9895 int primitive_arg, int hash_arg, int role_arg,
9896 int test_input, data_t *pw_data,
9897 int inj_err_type_arg,
9898 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009899{
9900 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9901 psa_pake_operation_t operation = psa_pake_operation_init();
9902 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009903 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009904 psa_key_type_t key_type_pw = key_type_pw_arg;
9905 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009906 psa_algorithm_t hash_alg = hash_arg;
9907 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009910 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9911 psa_status_t expected_error = expected_error_arg;
9912 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009913 unsigned char *output_buffer = NULL;
9914 size_t output_len = 0;
9915
Gilles Peskine449bd832023-01-11 14:50:10 +01009916 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009917
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009918 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009919 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009920 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009921
Gilles Peskine449bd832023-01-11 14:50:10 +01009922 if (pw_data->len > 0) {
9923 psa_set_key_usage_flags(&attributes, key_usage_pw);
9924 psa_set_key_algorithm(&attributes, alg);
9925 psa_set_key_type(&attributes, key_type_pw);
9926 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9927 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009928 }
9929
Gilles Peskine449bd832023-01-11 14:50:10 +01009930 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9931 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9932 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009933
Gilles Peskine449bd832023-01-11 14:50:10 +01009934 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009935
Gilles Peskine449bd832023-01-11 14:50:10 +01009936 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9937 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9938 expected_error);
9939 PSA_ASSERT(psa_pake_abort(&operation));
9940 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9941 expected_error);
9942 PSA_ASSERT(psa_pake_abort(&operation));
9943 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9944 expected_error);
9945 PSA_ASSERT(psa_pake_abort(&operation));
9946 TEST_EQUAL(psa_pake_set_role(&operation, role),
9947 expected_error);
9948 PSA_ASSERT(psa_pake_abort(&operation));
9949 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9950 NULL, 0, NULL),
9951 expected_error);
9952 PSA_ASSERT(psa_pake_abort(&operation));
9953 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9954 expected_error);
9955 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009956 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009957 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009958
Gilles Peskine449bd832023-01-11 14:50:10 +01009959 status = psa_pake_setup(&operation, &cipher_suite);
9960 if (status != PSA_SUCCESS) {
9961 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009962 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009963 }
9964
Gilles Peskine449bd832023-01-11 14:50:10 +01009965 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9966 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9967 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009968 goto exit;
9969 }
9970
Gilles Peskine449bd832023-01-11 14:50:10 +01009971 status = psa_pake_set_role(&operation, role);
9972 if (status != PSA_SUCCESS) {
9973 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009974 goto exit;
9975 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009976
Gilles Peskine449bd832023-01-11 14:50:10 +01009977 if (pw_data->len > 0) {
9978 status = psa_pake_set_password_key(&operation, key);
9979 if (status != PSA_SUCCESS) {
9980 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009981 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009982 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009983 }
9984
Gilles Peskine449bd832023-01-11 14:50:10 +01009985 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9986 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9987 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009988 goto exit;
9989 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009990
Gilles Peskine449bd832023-01-11 14:50:10 +01009991 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9992 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9993 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009994 goto exit;
9995 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009996
Gilles Peskine449bd832023-01-11 14:50:10 +01009997 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009998 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009999 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10000 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010001 goto exit;
10002 }
10003
Gilles Peskine449bd832023-01-11 14:50:10 +010010004 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010005 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010006 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10007 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010008 goto exit;
10009 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010010
Gilles Peskine449bd832023-01-11 14:50:10 +010010011 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10012 PSA_PAKE_STEP_KEY_SHARE);
10013 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10014 PSA_PAKE_STEP_ZK_PUBLIC);
10015 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10016 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010017
Gilles Peskine449bd832023-01-11 14:50:10 +010010018 if (test_input) {
10019 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10020 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10021 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010022 goto exit;
10023 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010024
Gilles Peskine449bd832023-01-11 14:50:10 +010010025 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10026 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10027 output_buffer, size_zk_proof),
10028 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010029 goto exit;
10030 }
10031
Gilles Peskine449bd832023-01-11 14:50:10 +010010032 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10033 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10034 output_buffer, size_zk_proof),
10035 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010036 goto exit;
10037 }
10038
Gilles Peskine449bd832023-01-11 14:50:10 +010010039 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10040 output_buffer, size_key_share);
10041 if (status != PSA_SUCCESS) {
10042 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010043 goto exit;
10044 }
10045
Gilles Peskine449bd832023-01-11 14:50:10 +010010046 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10047 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10048 output_buffer, size_zk_public + 1),
10049 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010050 goto exit;
10051 }
10052
Gilles Peskine449bd832023-01-11 14:50:10 +010010053 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010054 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010055 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10056 output_buffer, size_zk_public + 1);
10057 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10058 output_buffer, size_zk_public),
10059 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010060 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010061 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010062 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010063 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10064 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10065 NULL, 0, NULL),
10066 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010067 goto exit;
10068 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010069
Gilles Peskine449bd832023-01-11 14:50:10 +010010070 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10071 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10072 output_buffer, buf_size, &output_len),
10073 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010074 goto exit;
10075 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010076
Gilles Peskine449bd832023-01-11 14:50:10 +010010077 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10078 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10079 output_buffer, buf_size, &output_len),
10080 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010081 goto exit;
10082 }
10083
Gilles Peskine449bd832023-01-11 14:50:10 +010010084 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10085 output_buffer, buf_size, &output_len);
10086 if (status != PSA_SUCCESS) {
10087 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010088 goto exit;
10089 }
10090
Gilles Peskine449bd832023-01-11 14:50:10 +010010091 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010092
Gilles Peskine449bd832023-01-11 14:50:10 +010010093 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10094 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10095 output_buffer, size_zk_public - 1, &output_len),
10096 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010097 goto exit;
10098 }
10099
Gilles Peskine449bd832023-01-11 14:50:10 +010010100 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010101 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010102 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10103 output_buffer, size_zk_public - 1, &output_len);
10104 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10105 output_buffer, buf_size, &output_len),
10106 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010107 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010108 }
10109 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010110
10111exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010112 PSA_ASSERT(psa_destroy_key(key));
10113 PSA_ASSERT(psa_pake_abort(&operation));
10114 mbedtls_free(output_buffer);
10115 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010116}
10117/* END_CASE */
10118
Neil Armstronga557cb82022-06-10 08:58:32 +020010119/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010120void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10121 int client_input_first, int inject_error,
10122 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010123{
10124 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10125 psa_pake_operation_t server = psa_pake_operation_init();
10126 psa_pake_operation_t client = psa_pake_operation_init();
10127 psa_algorithm_t alg = alg_arg;
10128 psa_algorithm_t hash_alg = hash_arg;
10129 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10130 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10131
Gilles Peskine449bd832023-01-11 14:50:10 +010010132 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010133
Gilles Peskine449bd832023-01-11 14:50:10 +010010134 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10135 psa_set_key_algorithm(&attributes, alg);
10136 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10137 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10138 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010139
Gilles Peskine449bd832023-01-11 14:50:10 +010010140 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10141 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10142 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010143
10144
Gilles Peskine449bd832023-01-11 14:50:10 +010010145 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10146 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010147
Gilles Peskine449bd832023-01-11 14:50:10 +010010148 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10149 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010150
Gilles Peskine449bd832023-01-11 14:50:10 +010010151 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10152 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010153
Gilles Peskine449bd832023-01-11 14:50:10 +010010154 ecjpake_do_round(alg, primitive_arg, &server, &client,
10155 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010156
Gilles Peskine449bd832023-01-11 14:50:10 +010010157 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010158 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010159 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010160
Gilles Peskine449bd832023-01-11 14:50:10 +010010161 ecjpake_do_round(alg, primitive_arg, &server, &client,
10162 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010163
10164exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010165 psa_destroy_key(key);
10166 psa_pake_abort(&server);
10167 psa_pake_abort(&client);
10168 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010169}
10170/* END_CASE */
10171
10172/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010173void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10174 int derive_alg_arg, data_t *pw_data,
10175 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010176{
10177 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10178 psa_pake_operation_t server = psa_pake_operation_init();
10179 psa_pake_operation_t client = psa_pake_operation_init();
10180 psa_algorithm_t alg = alg_arg;
10181 psa_algorithm_t hash_alg = hash_arg;
10182 psa_algorithm_t derive_alg = derive_alg_arg;
10183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10185 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010186 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010187 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010188 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010189 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010190
Gilles Peskine449bd832023-01-11 14:50:10 +010010191 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010192
Gilles Peskine449bd832023-01-11 14:50:10 +010010193 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10194 psa_set_key_algorithm(&attributes, alg);
10195 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10196 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10197 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010198
Gilles Peskine449bd832023-01-11 14:50:10 +010010199 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10200 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10201 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010202
Neil Armstrong1e855602022-06-15 11:32:11 +020010203 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010204 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10205 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010206
Gilles Peskine449bd832023-01-11 14:50:10 +010010207 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10208 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10209 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10210 PSA_KEY_DERIVATION_INPUT_SEED,
10211 (const uint8_t *) "", 0));
10212 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10213 PSA_KEY_DERIVATION_INPUT_SEED,
10214 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010215 }
10216
Gilles Peskine449bd832023-01-11 14:50:10 +010010217 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10218 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010219
Gilles Peskine449bd832023-01-11 14:50:10 +010010220 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10221 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010222
Gilles Peskine449bd832023-01-11 14:50:10 +010010223 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10224 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010225
Gilles Peskine449bd832023-01-11 14:50:10 +010010226 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10227 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10228 PSA_ERROR_BAD_STATE);
10229 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10230 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010231 goto exit;
10232 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010233
Neil Armstrongf983caf2022-06-15 15:27:48 +020010234 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010235 ecjpake_do_round(alg, primitive_arg, &server, &client,
10236 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010237
Gilles Peskine449bd832023-01-11 14:50:10 +010010238 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10239 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10240 PSA_ERROR_BAD_STATE);
10241 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10242 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010243 goto exit;
10244 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010245
Neil Armstrongf983caf2022-06-15 15:27:48 +020010246 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010247 ecjpake_do_round(alg, primitive_arg, &server, &client,
10248 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010249
Gilles Peskine449bd832023-01-11 14:50:10 +010010250 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10251 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010252
10253exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010254 psa_key_derivation_abort(&server_derive);
10255 psa_key_derivation_abort(&client_derive);
10256 psa_destroy_key(key);
10257 psa_pake_abort(&server);
10258 psa_pake_abort(&client);
10259 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010260}
10261/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010262
10263/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010264void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010265{
10266 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10267 const size_t bits = 256;
10268 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010269 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010270 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010271 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010272
10273 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10274 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010275 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10276 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10277 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10278 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010279 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010280 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10281 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010282
10283 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010284 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10285 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10286 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10287 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10288 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10289 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010290
10291 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010292 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10293 PSA_PAKE_OUTPUT_MAX_SIZE);
10294 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10295 PSA_PAKE_OUTPUT_MAX_SIZE);
10296 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10297 PSA_PAKE_OUTPUT_MAX_SIZE);
10298 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10299 PSA_PAKE_INPUT_MAX_SIZE);
10300 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10301 PSA_PAKE_INPUT_MAX_SIZE);
10302 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10303 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010304}
10305/* END_CASE */