blob: ce5a240b2c2ccef2fe45462324d87994b62b7277 [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
Gilles Peskine8e94efe2021-02-13 00:25:53 +010016#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010017#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010018#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053019#if defined(PSA_CRYPTO_DRIVER_TEST)
20#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053021#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
22#else
23#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053024#endif
Przemek Stekiel8258ea72022-10-19 12:17:19 +020025#include "mbedtls/legacy_or_psa.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010026
Gilles Peskine4023c012021-05-27 13:21:20 +020027/* If this comes up, it's a bug in the test code or in the test data. */
28#define UNUSED 0xdeadbeef
29
Dave Rodgman647791d2021-06-23 12:49:59 +010030/* Assert that an operation is (not) active.
31 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010032#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
33#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010034
Przemek Stekiel7c795482022-11-15 22:26:12 +010035#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010036int ecjpake_operation_setup(psa_pake_operation_t *operation,
37 psa_pake_cipher_suite_t *cipher_suite,
38 psa_pake_role_t role,
39 mbedtls_svc_key_id_t key,
40 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010041{
Gilles Peskine449bd832023-01-11 14:50:10 +010042 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 if (key_available) {
49 PSA_ASSERT(psa_pake_set_password_key(operation, key));
50 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010051 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010052exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010053 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010054}
55#endif
56
Jaeden Amerof24c7f82018-06-27 17:20:43 +010057/** An invalid export length that will never be set by psa_export_key(). */
58static const size_t INVALID_EXPORT_LENGTH = ~0U;
59
Gilles Peskinea7aa4422018-08-14 15:17:54 +020060/** Test if a buffer contains a constant byte value.
61 *
62 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020063 *
64 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020065 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020066 * \param size Size of the buffer in bytes.
67 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020068 * \return 1 if the buffer is all-bits-zero.
69 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020070 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020072{
73 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010074 for (i = 0; i < size; i++) {
75 if (((unsigned char *) buffer)[i] != c) {
76 return 0;
77 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020078 }
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020080}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010081#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020082/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010083static int asn1_write_10x(unsigned char **p,
84 unsigned char *start,
85 size_t bits,
86 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020087{
88 int ret;
89 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 if (bits == 0) {
91 return MBEDTLS_ERR_ASN1_INVALID_DATA;
92 }
93 if (bits <= 8 && x >= 1 << (bits - 1)) {
94 return MBEDTLS_ERR_ASN1_INVALID_DATA;
95 }
96 if (*p < start || *p - start < (ptrdiff_t) len) {
97 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
98 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +020099 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 (*p)[len-1] = x;
101 if (bits % 8 == 0) {
102 (*p)[1] |= 1;
103 } else {
104 (*p)[0] |= 1 << (bits % 8);
105 }
106 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
107 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
108 MBEDTLS_ASN1_INTEGER));
109 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200110}
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112static int construct_fake_rsa_key(unsigned char *buffer,
113 size_t buffer_size,
114 unsigned char **p,
115 size_t bits,
116 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200117{
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200119 int ret;
120 int len = 0;
121 /* Construct something that looks like a DER encoding of
122 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
123 * RSAPrivateKey ::= SEQUENCE {
124 * version Version,
125 * modulus INTEGER, -- n
126 * publicExponent INTEGER, -- e
127 * privateExponent INTEGER, -- d
128 * prime1 INTEGER, -- p
129 * prime2 INTEGER, -- q
130 * exponent1 INTEGER, -- d mod (p-1)
131 * exponent2 INTEGER, -- d mod (q-1)
132 * coefficient INTEGER, -- (inverse of q) mod p
133 * otherPrimeInfos OtherPrimeInfos OPTIONAL
134 * }
135 * Or, for a public key, the same structure with only
136 * version, modulus and publicExponent.
137 */
138 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (keypair) {
140 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
141 asn1_write_10x(p, buffer, half_bits, 1));
142 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
143 asn1_write_10x(p, buffer, half_bits, 1));
144 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* q */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
149 asn1_write_10x(p, buffer, half_bits, 3));
150 MBEDTLS_ASN1_CHK_ADD(len, /* d */
151 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200152 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
154 asn1_write_10x(p, buffer, 17, 1));
155 MBEDTLS_ASN1_CHK_ADD(len, /* n */
156 asn1_write_10x(p, buffer, bits, 1));
157 if (keypair) {
158 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
159 mbedtls_asn1_write_int(p, buffer, 0));
160 }
161 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200162 {
163 const unsigned char tag =
164 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200166 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200168}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100169#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171int exercise_mac_setup(psa_key_type_t key_type,
172 const unsigned char *key_bytes,
173 size_t key_length,
174 psa_algorithm_t alg,
175 psa_mac_operation_t *operation,
176 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100177{
Ronald Cron5425a212020-08-04 14:58:35 +0200178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
182 psa_set_key_algorithm(&attributes, alg);
183 psa_set_key_type(&attributes, key_type);
184 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100189 /* If setup failed, reproduce the failure, so that the caller can
190 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (*status != PSA_SUCCESS) {
192 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 psa_destroy_key(key);
196 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197
198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 psa_destroy_key(key);
200 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201}
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203int exercise_cipher_setup(psa_key_type_t key_type,
204 const unsigned char *key_bytes,
205 size_t key_length,
206 psa_algorithm_t alg,
207 psa_cipher_operation_t *operation,
208 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100209{
Ronald Cron5425a212020-08-04 14:58:35 +0200210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
214 psa_set_key_algorithm(&attributes, alg);
215 psa_set_key_type(&attributes, key_type);
216 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100219 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100221 /* If setup failed, reproduce the failure, so that the caller can
222 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (*status != PSA_SUCCESS) {
224 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
225 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100226 }
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 psa_destroy_key(key);
229 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230
231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 psa_destroy_key(key);
233 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100234}
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237{
238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 uint8_t buffer[1];
241 size_t length;
242 int ok = 0;
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 psa_set_key_id(&attributes, key_id);
245 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
246 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
247 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
248 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
249 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200250 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
254 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
255 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
256 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
257 TEST_EQUAL(psa_get_key_type(&attributes), 0);
258 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
261 PSA_ERROR_INVALID_HANDLE);
262 TEST_EQUAL(psa_export_public_key(key,
263 buffer, sizeof(buffer), &length),
264 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200265
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200266 ok = 1;
267
268exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100269 /*
270 * Key attributes may have been returned by psa_get_key_attributes()
271 * thus reset them as required.
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200276}
277
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200278/* Assert that a key isn't reported as having a slot number. */
279#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200281 do \
282 { \
283 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 TEST_EQUAL(psa_get_key_slot_number( \
285 attributes, \
286 &ASSERT_NO_SLOT_NUMBER_slot_number), \
287 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200288 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200290#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291#define ASSERT_NO_SLOT_NUMBER(attributes) \
292 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200293#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
294
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100295/* An overapproximation of the amount of storage needed for a key of the
296 * given type and with the given content. The API doesn't make it easy
297 * to find a good value for the size. The current implementation doesn't
298 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299#define KEY_BITS_FROM_DATA(type, data) \
300 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100301
Darryl Green0c6575a2018-11-07 16:05:30 +0000302typedef enum {
303 IMPORT_KEY = 0,
304 GENERATE_KEY = 1,
305 DERIVE_KEY = 2
306} generate_method;
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100309 DO_NOT_SET_LENGTHS = 0,
310 SET_LENGTHS_BEFORE_NONCE = 1,
311 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100312} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100315 USE_NULL_TAG = 0,
316 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100317} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100318
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100319/*!
320 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100321 * \param key_type_arg Type of key passed in
322 * \param key_data The encryption / decryption key data
323 * \param alg_arg The type of algorithm used
324 * \param nonce Nonce data
325 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100326 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 * feed additional data in to be encrypted /
328 * decrypted. If -1, no chunking.
329 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100330 * \param data_part_len_arg If not -1, the length of chunks to feed
331 * the data in to be encrypted / decrypted. If
332 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100333 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100334 * expected here, this controls whether or not
335 * to set lengths, and in what order with
336 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100337 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100339 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100340 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
344 int alg_arg,
345 data_t *nonce,
346 data_t *additional_data,
347 int ad_part_len_arg,
348 data_t *input_data,
349 int data_part_len_arg,
350 set_lengths_method_t set_lengths_method,
351 data_t *expected_output,
352 int is_encrypt,
353 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100354{
355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
356 psa_key_type_t key_type = key_type_arg;
357 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100358 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100359 unsigned char *output_data = NULL;
360 unsigned char *part_data = NULL;
361 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100362 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100363 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100364 size_t output_size = 0;
365 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 size_t output_length = 0;
367 size_t key_bits = 0;
368 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100369 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t part_length = 0;
371 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100372 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100373 size_t ad_part_len = 0;
374 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
377 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
378
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100380 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (is_encrypt) {
385 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
386 } else {
387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100388 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100389
390 psa_set_key_algorithm(&attributes, alg);
391 psa_set_key_type(&attributes, key_type);
392
393 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
394 &key));
395
396 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
397 key_bits = psa_get_key_bits(&attributes);
398
399 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
400
401 if (is_encrypt) {
402 /* Tag gets written at end of buffer. */
403 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
404 (input_data->len +
405 tag_length));
406 data_true_size = input_data->len;
407 } else {
408 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
409 (input_data->len -
410 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100411
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100412 /* Do not want to attempt to decrypt tag. */
413 data_true_size = input_data->len - tag_length;
414 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 ASSERT_ALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (is_encrypt) {
419 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
420 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
421 } else {
422 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
423 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100424 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 ASSERT_ALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (is_encrypt) {
429 status = psa_aead_encrypt_setup(&operation, key, alg);
430 } else {
431 status = psa_aead_decrypt_setup(&operation, key, alg);
432 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
434 /* If the operation is not supported, just skip and not fail in case the
435 * encryption involves a common limitation of cryptography hardwares and
436 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (status == PSA_ERROR_NOT_SUPPORTED) {
438 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
439 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
445 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
446 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
447 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
448 data_true_size));
449 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
450 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
451 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
454 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100455 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100459 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100462 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 part_offset += part_length, part_count++) {
464 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100465 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100467 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100469 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 }
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 PSA_ASSERT(psa_aead_update_ad(&operation,
473 additional_data->x + part_offset,
474 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100475
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
480 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100484 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 data_part_len = (size_t) data_part_len_arg;
486 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
487 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 ASSERT_ALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100492 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 part_offset += part_length, part_count++) {
494 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100495 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 } else if ((data_true_size - part_offset) < data_part_len) {
497 part_length = (data_true_size - part_offset);
498 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 PSA_ASSERT(psa_aead_update(&operation,
503 (input_data->x + part_offset),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (output_data && output_part_length) {
509 memcpy((output_data + output_length), part_data,
510 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 }
512
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 output_length += output_part_length;
514 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100516 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
518 data_true_size, output_data,
519 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (is_encrypt) {
523 PSA_ASSERT(psa_aead_finish(&operation, final_data,
524 final_output_size,
525 &output_part_length,
526 tag_buffer, tag_length,
527 &tag_size));
528 } else {
529 PSA_ASSERT(psa_aead_verify(&operation, final_data,
530 final_output_size,
531 &output_part_length,
532 (input_data->x + data_true_size),
533 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100534 }
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (output_data && output_part_length) {
537 memcpy((output_data + output_length), final_data,
538 output_part_length);
539 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100540
541 output_length += output_part_length;
542
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100543
544 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
545 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (is_encrypt) {
547 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (output_data && tag_length) {
550 memcpy((output_data + output_length), tag_buffer,
551 tag_length);
552 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100553
554 output_length += tag_length;
555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 TEST_EQUAL(output_length,
557 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
558 input_data->len));
559 TEST_LE_U(output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
561 } else {
562 TEST_EQUAL(output_length,
563 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
564 input_data->len));
565 TEST_LE_U(output_length,
566 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100567 }
568
Paul Elliottd3f82412021-06-16 16:52:21 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 ASSERT_COMPARE(expected_output->x, expected_output->len,
571 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100572
Paul Elliottd3f82412021-06-16 16:52:21 +0100573
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100574 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100575
576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 psa_destroy_key(key);
578 psa_aead_abort(&operation);
579 mbedtls_free(output_data);
580 mbedtls_free(part_data);
581 mbedtls_free(final_data);
582 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100585}
586
Neil Armstrong4766f992022-02-28 16:23:59 +0100587/*!
588 * \brief Internal Function for MAC multipart tests.
589 * \param key_type_arg Type of key passed in
590 * \param key_data The encryption / decryption key data
591 * \param alg_arg The type of algorithm used
592 * \param input_data Data to encrypt / decrypt
593 * \param data_part_len_arg If not -1, the length of chunks to feed
594 * the data in to be encrypted / decrypted. If
595 * -1, no chunking
596 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000597 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100598 * \param do_zero_parts If non-zero, interleave zero length chunks
599 * with normal length chunks.
600 * \return int Zero on failure, non-zero on success.
601 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
603 int alg_arg,
604 data_t *input_data,
605 int data_part_len_arg,
606 data_t *expected_output,
607 int is_verify,
608 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100609{
610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
611 psa_key_type_t key_type = key_type_arg;
612 psa_algorithm_t alg = alg_arg;
613 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
614 unsigned char mac[PSA_MAC_MAX_SIZE];
615 size_t part_offset = 0;
616 size_t part_length = 0;
617 size_t data_part_len = 0;
618 size_t mac_len = 0;
619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
620 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
621
622 int test_ok = 0;
623 size_t part_count = 0;
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (is_verify) {
628 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
629 } else {
630 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
631 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 psa_set_key_algorithm(&attributes, alg);
634 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
637 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (is_verify) {
640 status = psa_mac_verify_setup(&operation, key, alg);
641 } else {
642 status = psa_mac_sign_setup(&operation, key, alg);
643 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100648 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100652 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 part_offset += part_length, part_count++) {
654 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100655 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 } else if ((input_data->len - part_offset) < data_part_len) {
657 part_length = (input_data->len - part_offset);
658 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 part_length = data_part_len;
660 }
661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 PSA_ASSERT(psa_mac_update(&operation,
663 (input_data->x + part_offset),
664 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100665 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100667 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
669 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (is_verify) {
673 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
674 expected_output->len));
675 } else {
676 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
677 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 ASSERT_COMPARE(expected_output->x, expected_output->len,
680 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100681 }
682
683 test_ok = 1;
684
685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 psa_destroy_key(key);
687 psa_mac_abort(&operation);
688 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100691}
692
Neil Armstrong75673ab2022-06-15 17:39:01 +0200693#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100694static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
695 psa_pake_operation_t *server,
696 psa_pake_operation_t *client,
697 int client_input_first,
698 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200699{
700 unsigned char *buffer0 = NULL, *buffer1 = NULL;
701 size_t buffer_length = (
702 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
703 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
704 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200705 /* The output should be exactly this size according to the spec */
706 const size_t expected_size_key_share =
707 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
708 /* The output should be exactly this size according to the spec */
709 const size_t expected_size_zk_public =
710 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
711 /* The output can be smaller: the spec allows stripping leading zeroes */
712 const size_t max_expected_size_zk_proof =
713 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200714 size_t buffer0_off = 0;
715 size_t buffer1_off = 0;
716 size_t s_g1_len, s_g2_len, s_a_len;
717 size_t s_g1_off, s_g2_off, s_a_off;
718 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
719 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
720 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
721 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
722 size_t c_g1_len, c_g2_len, c_a_len;
723 size_t c_g1_off, c_g2_off, c_a_off;
724 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
725 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
726 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
727 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
728 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200729 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 ASSERT_ALLOC(buffer0, buffer_length);
732 ASSERT_ALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200735 case 1:
736 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
738 buffer0 + buffer0_off,
739 512 - buffer0_off, &s_g1_len));
740 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200741 s_g1_off = buffer0_off;
742 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
744 buffer0 + buffer0_off,
745 512 - buffer0_off, &s_x1_pk_len));
746 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200747 s_x1_pk_off = buffer0_off;
748 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
750 buffer0 + buffer0_off,
751 512 - buffer0_off, &s_x1_pr_len));
752 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200753 s_x1_pr_off = buffer0_off;
754 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
756 buffer0 + buffer0_off,
757 512 - buffer0_off, &s_g2_len));
758 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200759 s_g2_off = buffer0_off;
760 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
762 buffer0 + buffer0_off,
763 512 - buffer0_off, &s_x2_pk_len));
764 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200765 s_x2_pk_off = buffer0_off;
766 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
768 buffer0 + buffer0_off,
769 512 - buffer0_off, &s_x2_pr_len));
770 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200771 s_x2_pr_off = buffer0_off;
772 buffer0_off += s_x2_pr_len;
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500775 buffer0[s_x1_pr_off + 8] ^= 1;
776 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200777 expected_status = PSA_ERROR_DATA_INVALID;
778 }
779
Neil Armstrong51009d72022-09-05 17:59:54 +0200780 /*
781 * When injecting errors in inputs, the implementation is
782 * free to detect it right away of with a delay.
783 * This permits delaying the error until the end of the input
784 * sequence, if no error appears then, this will be treated
785 * as an error.
786 */
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200789 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
791 buffer0 + s_g1_off, s_g1_len);
792 if (inject_error == 1 && status != PSA_SUCCESS) {
793 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200794 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 } else {
796 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200797 }
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
800 buffer0 + s_x1_pk_off,
801 s_x1_pk_len);
802 if (inject_error == 1 && status != PSA_SUCCESS) {
803 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200804 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 } else {
806 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200807 }
808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
810 buffer0 + s_x1_pr_off,
811 s_x1_pr_len);
812 if (inject_error == 1 && status != PSA_SUCCESS) {
813 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200814 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 } else {
816 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200817 }
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
820 buffer0 + s_g2_off,
821 s_g2_len);
822 if (inject_error == 1 && status != PSA_SUCCESS) {
823 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200824 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 } else {
826 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200827 }
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
830 buffer0 + s_x2_pk_off,
831 s_x2_pk_len);
832 if (inject_error == 1 && status != PSA_SUCCESS) {
833 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200834 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 } else {
836 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200837 }
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
840 buffer0 + s_x2_pr_off,
841 s_x2_pr_len);
842 if (inject_error == 1 && status != PSA_SUCCESS) {
843 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200844 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 } else {
846 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200847 }
848
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200849 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (inject_error == 1) {
851 TEST_ASSERT(
852 !"One of the last psa_pake_input() calls should have returned the expected error.");
853 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200854 }
855
856 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
858 buffer1 + buffer1_off,
859 512 - buffer1_off, &c_g1_len));
860 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200861 c_g1_off = buffer1_off;
862 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
864 buffer1 + buffer1_off,
865 512 - buffer1_off, &c_x1_pk_len));
866 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200867 c_x1_pk_off = buffer1_off;
868 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
870 buffer1 + buffer1_off,
871 512 - buffer1_off, &c_x1_pr_len));
872 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200873 c_x1_pr_off = buffer1_off;
874 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
876 buffer1 + buffer1_off,
877 512 - buffer1_off, &c_g2_len));
878 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200879 c_g2_off = buffer1_off;
880 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
882 buffer1 + buffer1_off,
883 512 - buffer1_off, &c_x2_pk_len));
884 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200885 c_x2_pk_off = buffer1_off;
886 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
888 buffer1 + buffer1_off,
889 512 - buffer1_off, &c_x2_pr_len));
890 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200891 c_x2_pr_off = buffer1_off;
892 buffer1_off += c_x2_pr_len;
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200895 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
897 buffer0 + s_g1_off, s_g1_len);
898 if (inject_error == 1 && status != PSA_SUCCESS) {
899 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200900 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 } else {
902 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200903 }
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
906 buffer0 + s_x1_pk_off,
907 s_x1_pk_len);
908 if (inject_error == 1 && status != PSA_SUCCESS) {
909 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200910 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 } else {
912 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200913 }
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
916 buffer0 + s_x1_pr_off,
917 s_x1_pr_len);
918 if (inject_error == 1 && status != PSA_SUCCESS) {
919 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200920 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 } else {
922 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200923 }
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
926 buffer0 + s_g2_off,
927 s_g2_len);
928 if (inject_error == 1 && status != PSA_SUCCESS) {
929 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200930 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 } else {
932 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200933 }
934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
936 buffer0 + s_x2_pk_off,
937 s_x2_pk_len);
938 if (inject_error == 1 && status != PSA_SUCCESS) {
939 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200940 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 } else {
942 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200943 }
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
946 buffer0 + s_x2_pr_off,
947 s_x2_pr_len);
948 if (inject_error == 1 && status != PSA_SUCCESS) {
949 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200950 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 } else {
952 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200953 }
954
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200955 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (inject_error == 1) {
957 TEST_ASSERT(
958 !"One of the last psa_pake_input() calls should have returned the expected error.");
959 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500963 buffer1[c_x1_pr_off + 12] ^= 1;
964 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200965 expected_status = PSA_ERROR_DATA_INVALID;
966 }
967
968 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
970 buffer1 + c_g1_off, c_g1_len);
971 if (inject_error == 2 && status != PSA_SUCCESS) {
972 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200973 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 } else {
975 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200976 }
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
979 buffer1 + c_x1_pk_off, c_x1_pk_len);
980 if (inject_error == 2 && status != PSA_SUCCESS) {
981 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200982 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 } else {
984 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200985 }
986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
988 buffer1 + c_x1_pr_off, c_x1_pr_len);
989 if (inject_error == 2 && status != PSA_SUCCESS) {
990 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200991 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 } else {
993 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200994 }
995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
997 buffer1 + c_g2_off, c_g2_len);
998 if (inject_error == 2 && status != PSA_SUCCESS) {
999 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001000 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 } else {
1002 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001003 }
1004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1006 buffer1 + c_x2_pk_off, c_x2_pk_len);
1007 if (inject_error == 2 && status != PSA_SUCCESS) {
1008 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001009 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 } else {
1011 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001012 }
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1015 buffer1 + c_x2_pr_off, c_x2_pr_len);
1016 if (inject_error == 2 && status != PSA_SUCCESS) {
1017 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001018 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 } else {
1020 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001021 }
1022
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001023 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if (inject_error == 2) {
1025 TEST_ASSERT(
1026 !"One of the last psa_pake_input() calls should have returned the expected error.");
1027 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001028
1029 break;
1030
1031 case 2:
1032 /* Server second round Output */
1033 buffer0_off = 0;
1034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1036 buffer0 + buffer0_off,
1037 512 - buffer0_off, &s_a_len));
1038 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001039 s_a_off = buffer0_off;
1040 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1042 buffer0 + buffer0_off,
1043 512 - buffer0_off, &s_x2s_pk_len));
1044 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001045 s_x2s_pk_off = buffer0_off;
1046 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1048 buffer0 + buffer0_off,
1049 512 - buffer0_off, &s_x2s_pr_len));
1050 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001051 s_x2s_pr_off = buffer0_off;
1052 buffer0_off += s_x2s_pr_len;
1053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001055 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001056 expected_status = PSA_ERROR_DATA_INVALID;
1057 }
1058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001060 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1062 buffer0 + s_a_off, s_a_len);
1063 if (inject_error == 3 && status != PSA_SUCCESS) {
1064 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001065 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 } else {
1067 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001068 }
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1071 buffer0 + s_x2s_pk_off,
1072 s_x2s_pk_len);
1073 if (inject_error == 3 && status != PSA_SUCCESS) {
1074 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001075 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 } else {
1077 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001078 }
1079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1081 buffer0 + s_x2s_pr_off,
1082 s_x2s_pr_len);
1083 if (inject_error == 3 && status != PSA_SUCCESS) {
1084 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001085 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 } else {
1087 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001088 }
1089
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001090 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (inject_error == 3) {
1092 TEST_ASSERT(
1093 !"One of the last psa_pake_input() calls should have returned the expected error.");
1094 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001095 }
1096
1097 /* Client second round Output */
1098 buffer1_off = 0;
1099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1101 buffer1 + buffer1_off,
1102 512 - buffer1_off, &c_a_len));
1103 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001104 c_a_off = buffer1_off;
1105 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1107 buffer1 + buffer1_off,
1108 512 - buffer1_off, &c_x2s_pk_len));
1109 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001110 c_x2s_pk_off = buffer1_off;
1111 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1113 buffer1 + buffer1_off,
1114 512 - buffer1_off, &c_x2s_pr_len));
1115 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001116 c_x2s_pr_off = buffer1_off;
1117 buffer1_off += c_x2s_pr_len;
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001120 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1122 buffer0 + s_a_off, s_a_len);
1123 if (inject_error == 3 && status != PSA_SUCCESS) {
1124 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001125 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 } else {
1127 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001128 }
1129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1131 buffer0 + s_x2s_pk_off,
1132 s_x2s_pk_len);
1133 if (inject_error == 3 && status != PSA_SUCCESS) {
1134 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001135 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 } else {
1137 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001138 }
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1141 buffer0 + s_x2s_pr_off,
1142 s_x2s_pr_len);
1143 if (inject_error == 3 && status != PSA_SUCCESS) {
1144 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001145 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 } else {
1147 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001148 }
1149
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001150 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (inject_error == 3) {
1152 TEST_ASSERT(
1153 !"One of the last psa_pake_input() calls should have returned the expected error.");
1154 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001155 }
1156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001158 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001159 expected_status = PSA_ERROR_DATA_INVALID;
1160 }
1161
1162 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1164 buffer1 + c_a_off, c_a_len);
1165 if (inject_error == 4 && status != PSA_SUCCESS) {
1166 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001167 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 } else {
1169 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001170 }
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1173 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1174 if (inject_error == 4 && status != PSA_SUCCESS) {
1175 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001176 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 } else {
1178 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001179 }
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1182 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1183 if (inject_error == 4 && status != PSA_SUCCESS) {
1184 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001185 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 } else {
1187 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001188 }
1189
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001190 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (inject_error == 4) {
1192 TEST_ASSERT(
1193 !"One of the last psa_pake_input() calls should have returned the expected error.");
1194 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001195
1196 break;
1197
1198 }
1199
Neil Armstrongf983caf2022-06-15 15:27:48 +02001200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 mbedtls_free(buffer0);
1202 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001203}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001204#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001207 INJECT_ERR_NONE = 0,
1208 INJECT_ERR_UNINITIALIZED_ACCESS,
1209 INJECT_ERR_DUPLICATE_SETUP,
1210 INJECT_ERR_INVALID_USER,
1211 INJECT_ERR_INVALID_PEER,
1212 INJECT_ERR_SET_USER,
1213 INJECT_ERR_SET_PEER,
1214 INJECT_EMPTY_IO_BUFFER,
1215 INJECT_UNKNOWN_STEP,
1216 INJECT_INVALID_FIRST_STEP,
1217 INJECT_WRONG_BUFFER_SIZE,
1218 INJECT_VALID_OPERATION_AFTER_FAILURE,
1219 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1220 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1221} ecjpake_injected_failure_t;
1222
Gilles Peskinee59236f2018-01-27 23:32:46 +01001223/* END_HEADER */
1224
1225/* BEGIN_DEPENDENCIES
1226 * depends_on:MBEDTLS_PSA_CRYPTO_C
1227 * END_DEPENDENCIES
1228 */
1229
1230/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001231void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001232{
1233 size_t max_truncated_mac_size =
1234 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1235
1236 /* Check that the length for a truncated MAC always fits in the algorithm
1237 * encoding. The shifted mask is the maximum truncated value. The
1238 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001244void import_with_policy(int type_arg,
1245 int usage_arg, int alg_arg,
1246 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001247{
1248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1249 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001251 psa_key_type_t type = type_arg;
1252 psa_key_usage_t usage = usage_arg;
1253 psa_algorithm_t alg = alg_arg;
1254 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001256 psa_status_t status;
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 psa_set_key_type(&attributes, type);
1261 psa_set_key_usage_flags(&attributes, usage);
1262 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 status = psa_import_key(&attributes,
1265 key_material, sizeof(key_material),
1266 &key);
1267 TEST_EQUAL(status, expected_status);
1268 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001269 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1273 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1274 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1275 mbedtls_test_update_key_usage_flags(usage));
1276 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1277 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 PSA_ASSERT(psa_destroy_key(key));
1280 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001281
1282exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001283 /*
1284 * Key attributes may have been returned by psa_get_key_attributes()
1285 * thus reset them as required.
1286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 psa_destroy_key(key);
1290 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001291}
1292/* END_CASE */
1293
1294/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295void import_with_data(data_t *data, int type_arg,
1296 int attr_bits_arg,
1297 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001298{
1299 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1300 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001301 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001302 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001303 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001304 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001305 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 psa_set_key_type(&attributes, type);
1310 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 status = psa_import_key(&attributes, data->x, data->len, &key);
1313 TEST_EQUAL(status, expected_status);
1314 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001315 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1319 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1320 if (attr_bits != 0) {
1321 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1322 }
1323 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 PSA_ASSERT(psa_destroy_key(key));
1326 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327
1328exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001329 /*
1330 * Key attributes may have been returned by psa_get_key_attributes()
1331 * thus reset them as required.
1332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 psa_destroy_key(key);
1336 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001337}
1338/* END_CASE */
1339
1340/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001341/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342void import_large_key(int type_arg, int byte_size_arg,
1343 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001344{
1345 psa_key_type_t type = type_arg;
1346 size_t byte_size = byte_size_arg;
1347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1348 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001350 psa_status_t status;
1351 uint8_t *buffer = NULL;
1352 size_t buffer_size = byte_size + 1;
1353 size_t n;
1354
Steven Cooreman69967ce2021-01-18 18:01:08 +01001355 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001356 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1358 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001361
1362 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1364 psa_set_key_type(&attributes, type);
1365 status = psa_import_key(&attributes, buffer, byte_size, &key);
1366 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1367 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (status == PSA_SUCCESS) {
1370 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1371 TEST_EQUAL(psa_get_key_type(&attributes), type);
1372 TEST_EQUAL(psa_get_key_bits(&attributes),
1373 PSA_BYTES_TO_BITS(byte_size));
1374 ASSERT_NO_SLOT_NUMBER(&attributes);
1375 memset(buffer, 0, byte_size + 1);
1376 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1377 for (n = 0; n < byte_size; n++) {
1378 TEST_EQUAL(buffer[n], 'K');
1379 }
1380 for (n = byte_size; n < buffer_size; n++) {
1381 TEST_EQUAL(buffer[n], 0);
1382 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001383 }
1384
1385exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001386 /*
1387 * Key attributes may have been returned by psa_get_key_attributes()
1388 * thus reset them as required.
1389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 psa_destroy_key(key);
1393 PSA_DONE();
1394 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001395}
1396/* END_CASE */
1397
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001398/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001399/* Import an RSA key with a valid structure (but not valid numbers
1400 * inside, beyond having sensible size and parity). This is expected to
1401 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001403{
Ronald Cron5425a212020-08-04 14:58:35 +02001404 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001405 size_t bits = bits_arg;
1406 psa_status_t expected_status = expected_status_arg;
1407 psa_status_t status;
1408 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001409 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001410 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001412 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001413 unsigned char *p;
1414 int ret;
1415 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 PSA_ASSERT(psa_crypto_init());
1419 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1422 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001423 length = ret;
1424
1425 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 psa_set_key_type(&attributes, type);
1427 status = psa_import_key(&attributes, p, length, &key);
1428 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (status == PSA_SUCCESS) {
1431 PSA_ASSERT(psa_destroy_key(key));
1432 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001433
1434exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 mbedtls_free(buffer);
1436 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001437}
1438/* END_CASE */
1439
1440/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001441void import_export(data_t *data,
1442 int type_arg,
1443 int usage_arg, int alg_arg,
1444 int lifetime_arg,
1445 int expected_bits,
1446 int export_size_delta,
1447 int expected_export_status_arg,
1448 /*whether reexport must give the original input exactly*/
1449 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001450{
Ronald Cron5425a212020-08-04 14:58:35 +02001451 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001452 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001453 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001454 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001455 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301456 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001457 unsigned char *exported = NULL;
1458 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001459 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001460 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001461 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001463 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464
Moran Pekercb088e72018-07-17 17:36:59 +03001465 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 ASSERT_ALLOC(exported, export_size);
1467 if (!canonical_input) {
1468 ASSERT_ALLOC(reexported, export_size);
1469 }
1470 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 psa_set_key_lifetime(&attributes, lifetime);
1473 psa_set_key_usage_flags(&attributes, usage_arg);
1474 psa_set_key_algorithm(&attributes, alg);
1475 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001476
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001477 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001479
1480 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1482 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1483 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1484 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001485
1486 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 status = psa_export_key(key, exported, export_size, &exported_length);
1488 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001489
1490 /* The exported length must be set by psa_export_key() to a value between 0
1491 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1493 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1494 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1497 export_size - exported_length));
1498 if (status != PSA_SUCCESS) {
1499 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001501 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502
Gilles Peskineea38a922021-02-13 00:05:16 +01001503 /* Run sanity checks on the exported key. For non-canonical inputs,
1504 * this validates the canonical representations. For canonical inputs,
1505 * this doesn't directly validate the implementation, but it still helps
1506 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (!psa_key_lifetime_is_external(lifetime)) {
1508 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301509 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 }
Archana4d7ae1d2021-07-07 02:50:22 +05301511 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (canonical_input) {
1514 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1515 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001516 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1518 &key2));
1519 PSA_ASSERT(psa_export_key(key2,
1520 reexported,
1521 export_size,
1522 &reexported_length));
1523 ASSERT_COMPARE(exported, exported_length,
1524 reexported, reexported_length);
1525 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 TEST_LE_U(exported_length,
1528 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1529 psa_get_key_bits(&got_attributes)));
1530 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531
1532destroy:
1533 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 PSA_ASSERT(psa_destroy_key(key));
1535 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001536
1537exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001538 /*
1539 * Key attributes may have been returned by psa_get_key_attributes()
1540 * thus reset them as required.
1541 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 psa_reset_key_attributes(&got_attributes);
1543 psa_destroy_key(key);
1544 mbedtls_free(exported);
1545 mbedtls_free(reexported);
1546 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547}
1548/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001549
Moran Pekerf709f4a2018-06-06 17:26:04 +03001550/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551void import_export_public_key(data_t *data,
1552 int type_arg, // key pair or public key
1553 int alg_arg,
1554 int lifetime_arg,
1555 int export_size_delta,
1556 int expected_export_status_arg,
1557 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001558{
Ronald Cron5425a212020-08-04 14:58:35 +02001559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001560 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001561 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001562 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001563 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301564 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001565 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001566 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001567 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001568 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 psa_set_key_lifetime(&attributes, lifetime);
1573 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1574 psa_set_key_algorithm(&attributes, alg);
1575 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001576
1577 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001579
Gilles Peskine49c25912018-10-29 15:15:31 +01001580 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 ASSERT_ALLOC(exported, export_size);
1582 status = psa_export_public_key(key,
1583 exported, export_size,
1584 &exported_length);
1585 TEST_EQUAL(status, expected_export_status);
1586 if (status == PSA_SUCCESS) {
1587 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001588 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1590 bits = psa_get_key_bits(&attributes);
1591 TEST_LE_U(expected_public_key->len,
1592 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1593 TEST_LE_U(expected_public_key->len,
1594 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1595 TEST_LE_U(expected_public_key->len,
1596 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1597 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1598 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001599 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001600exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001601 /*
1602 * Key attributes may have been returned by psa_get_key_attributes()
1603 * thus reset them as required.
1604 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 mbedtls_free(exported);
1608 psa_destroy_key(key);
1609 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001610}
1611/* END_CASE */
1612
Gilles Peskine20035e32018-02-03 22:44:14 +01001613/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614void import_and_exercise_key(data_t *data,
1615 int type_arg,
1616 int bits_arg,
1617 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001618{
Ronald Cron5425a212020-08-04 14:58:35 +02001619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001620 psa_key_type_t type = type_arg;
1621 size_t bits = bits_arg;
1622 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001625 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 psa_set_key_usage_flags(&attributes, usage);
1630 psa_set_key_algorithm(&attributes, alg);
1631 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001632
1633 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001635
1636 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1638 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1639 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001640
1641 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 PSA_ASSERT(psa_destroy_key(key));
1647 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001648
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001649exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001650 /*
1651 * Key attributes may have been returned by psa_get_key_attributes()
1652 * thus reset them as required.
1653 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 psa_reset_key_attributes(&attributes);
1657 psa_destroy_key(key);
1658 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001659}
1660/* END_CASE */
1661
1662/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663void effective_key_attributes(int type_arg, int expected_type_arg,
1664 int bits_arg, int expected_bits_arg,
1665 int usage_arg, int expected_usage_arg,
1666 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001667{
Ronald Cron5425a212020-08-04 14:58:35 +02001668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001669 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001670 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001671 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001672 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001673 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001674 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001675 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001676 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 psa_set_key_usage_flags(&attributes, usage);
1682 psa_set_key_algorithm(&attributes, alg);
1683 psa_set_key_type(&attributes, key_type);
1684 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 PSA_ASSERT(psa_generate_key(&attributes, &key));
1687 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1690 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1691 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1692 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1693 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001694
1695exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001696 /*
1697 * Key attributes may have been returned by psa_get_key_attributes()
1698 * thus reset them as required.
1699 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 psa_destroy_key(key);
1703 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001704}
1705/* END_CASE */
1706
1707/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708void check_key_policy(int type_arg, int bits_arg,
1709 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001710{
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1712 usage_arg,
1713 mbedtls_test_update_key_usage_flags(usage_arg),
1714 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001715 goto exit;
1716}
1717/* END_CASE */
1718
1719/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001720void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001721{
1722 /* Test each valid way of initializing the object, except for `= {0}`, as
1723 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1724 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001725 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001727 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1728 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1733 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1734 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 TEST_EQUAL(psa_get_key_type(&func), 0);
1737 TEST_EQUAL(psa_get_key_type(&init), 0);
1738 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 TEST_EQUAL(psa_get_key_bits(&func), 0);
1741 TEST_EQUAL(psa_get_key_bits(&init), 0);
1742 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1745 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1746 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1749 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1750 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001751}
1752/* END_CASE */
1753
1754/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001755void mac_key_policy(int policy_usage_arg,
1756 int policy_alg_arg,
1757 int key_type_arg,
1758 data_t *key_data,
1759 int exercise_alg_arg,
1760 int expected_status_sign_arg,
1761 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001762{
Ronald Cron5425a212020-08-04 14:58:35 +02001763 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001765 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001766 psa_key_type_t key_type = key_type_arg;
1767 psa_algorithm_t policy_alg = policy_alg_arg;
1768 psa_algorithm_t exercise_alg = exercise_alg_arg;
1769 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001770 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001771 psa_status_t expected_status_sign = expected_status_sign_arg;
1772 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001773 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 psa_set_key_usage_flags(&attributes, policy_usage);
1778 psa_set_key_algorithm(&attributes, policy_alg);
1779 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1782 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1785 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1788 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001789
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001790 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001792 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1794 input, 128,
1795 mac, PSA_MAC_MAX_SIZE, &mac_len),
1796 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001797
Neil Armstrong3af9b972022-02-07 12:20:21 +01001798 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 PSA_ASSERT(psa_mac_abort(&operation));
1800 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1801 if (status == PSA_SUCCESS) {
1802 status = psa_mac_update(&operation, input, 128);
1803 if (status == PSA_SUCCESS) {
1804 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1805 &mac_len),
1806 expected_status_sign);
1807 } else {
1808 TEST_EQUAL(status, expected_status_sign);
1809 }
1810 } else {
1811 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001812 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001814
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001815 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 status = psa_mac_verify(key, exercise_alg, input, 128,
1817 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1820 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1821 } else {
1822 TEST_EQUAL(status, expected_status_verify);
1823 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001824
Neil Armstrong3af9b972022-02-07 12:20:21 +01001825 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1827 if (status == PSA_SUCCESS) {
1828 status = psa_mac_update(&operation, input, 128);
1829 if (status == PSA_SUCCESS) {
1830 status = psa_mac_verify_finish(&operation, mac, mac_len);
1831 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1832 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1833 } else {
1834 TEST_EQUAL(status, expected_status_verify);
1835 }
1836 } else {
1837 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001838 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 } else {
1840 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001841 }
1842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 memset(mac, 0, sizeof(mac));
1846 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1847 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001848
1849exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 psa_mac_abort(&operation);
1851 psa_destroy_key(key);
1852 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001853}
1854/* END_CASE */
1855
1856/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857void cipher_key_policy(int policy_usage_arg,
1858 int policy_alg,
1859 int key_type,
1860 data_t *key_data,
1861 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001862{
Ronald Cron5425a212020-08-04 14:58:35 +02001863 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001865 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001866 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001867 size_t output_buffer_size = 0;
1868 size_t input_buffer_size = 0;
1869 size_t output_length = 0;
1870 uint8_t *output = NULL;
1871 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001872 psa_status_t status;
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1875 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1876 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 ASSERT_ALLOC(input, input_buffer_size);
1879 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 psa_set_key_usage_flags(&attributes, policy_usage);
1884 psa_set_key_algorithm(&attributes, policy_alg);
1885 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1888 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001890 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 TEST_EQUAL(policy_usage,
1892 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001893
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001894 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1896 output, output_buffer_size,
1897 &output_length);
1898 if (policy_alg == exercise_alg &&
1899 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1900 PSA_ASSERT(status);
1901 } else {
1902 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1903 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001904
1905 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1907 if (policy_alg == exercise_alg &&
1908 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1909 PSA_ASSERT(status);
1910 } else {
1911 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1912 }
1913 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001915 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1917 input, input_buffer_size,
1918 &output_length);
1919 if (policy_alg == exercise_alg &&
1920 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1921 PSA_ASSERT(status);
1922 } else {
1923 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1924 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001925
1926 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1928 if (policy_alg == exercise_alg &&
1929 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1930 PSA_ASSERT(status);
1931 } else {
1932 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1933 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934
1935exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 psa_cipher_abort(&operation);
1937 mbedtls_free(input);
1938 mbedtls_free(output);
1939 psa_destroy_key(key);
1940 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941}
1942/* END_CASE */
1943
1944/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001945void aead_key_policy(int policy_usage_arg,
1946 int policy_alg,
1947 int key_type,
1948 data_t *key_data,
1949 int nonce_length_arg,
1950 int tag_length_arg,
1951 int exercise_alg,
1952 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953{
Ronald Cron5425a212020-08-04 14:58:35 +02001954 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001956 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001957 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001959 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961 size_t nonce_length = nonce_length_arg;
1962 unsigned char tag[16];
1963 size_t tag_length = tag_length_arg;
1964 size_t output_length;
1965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 TEST_LE_U(nonce_length, sizeof(nonce));
1967 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 psa_set_key_usage_flags(&attributes, policy_usage);
1972 psa_set_key_algorithm(&attributes, policy_alg);
1973 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1976 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001978 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 TEST_EQUAL(policy_usage,
1980 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001981
Neil Armstrong752d8112022-02-07 14:51:11 +01001982 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_aead_encrypt(key, exercise_alg,
1984 nonce, nonce_length,
1985 NULL, 0,
1986 NULL, 0,
1987 tag, tag_length,
1988 &output_length);
1989 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1990 TEST_EQUAL(status, expected_status);
1991 } else {
1992 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1993 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994
Neil Armstrong752d8112022-02-07 14:51:11 +01001995 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
1997 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1998 TEST_EQUAL(status, expected_status);
1999 } else {
2000 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2001 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002002
2003 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 memset(tag, 0, sizeof(tag));
2005 status = psa_aead_decrypt(key, exercise_alg,
2006 nonce, nonce_length,
2007 NULL, 0,
2008 tag, tag_length,
2009 NULL, 0,
2010 &output_length);
2011 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2012 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2013 } else if (expected_status == PSA_SUCCESS) {
2014 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2015 } else {
2016 TEST_EQUAL(status, expected_status);
2017 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002018
Neil Armstrong752d8112022-02-07 14:51:11 +01002019 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 PSA_ASSERT(psa_aead_abort(&operation));
2021 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2022 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2023 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2024 } else {
2025 TEST_EQUAL(status, expected_status);
2026 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002027
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 PSA_ASSERT(psa_aead_abort(&operation));
2030 psa_destroy_key(key);
2031 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032}
2033/* END_CASE */
2034
2035/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036void asymmetric_encryption_key_policy(int policy_usage_arg,
2037 int policy_alg,
2038 int key_type,
2039 data_t *key_data,
2040 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041{
Ronald Cron5425a212020-08-04 14:58:35 +02002042 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002044 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002045 psa_status_t status;
2046 size_t key_bits;
2047 size_t buffer_length;
2048 unsigned char *buffer = NULL;
2049 size_t output_length;
2050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 psa_set_key_usage_flags(&attributes, policy_usage);
2054 psa_set_key_algorithm(&attributes, policy_alg);
2055 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2058 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002059
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002060 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 TEST_EQUAL(policy_usage,
2062 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2065 key_bits = psa_get_key_bits(&attributes);
2066 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2067 exercise_alg);
2068 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 status = psa_asymmetric_encrypt(key, exercise_alg,
2071 NULL, 0,
2072 NULL, 0,
2073 buffer, buffer_length,
2074 &output_length);
2075 if (policy_alg == exercise_alg &&
2076 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2077 PSA_ASSERT(status);
2078 } else {
2079 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2080 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if (buffer_length != 0) {
2083 memset(buffer, 0, buffer_length);
2084 }
2085 status = psa_asymmetric_decrypt(key, exercise_alg,
2086 buffer, buffer_length,
2087 NULL, 0,
2088 buffer, buffer_length,
2089 &output_length);
2090 if (policy_alg == exercise_alg &&
2091 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2092 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2093 } else {
2094 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2095 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096
2097exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002098 /*
2099 * Key attributes may have been returned by psa_get_key_attributes()
2100 * thus reset them as required.
2101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 psa_destroy_key(key);
2105 PSA_DONE();
2106 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002107}
2108/* END_CASE */
2109
2110/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002111void asymmetric_signature_key_policy(int policy_usage_arg,
2112 int policy_alg,
2113 int key_type,
2114 data_t *key_data,
2115 int exercise_alg,
2116 int payload_length_arg,
2117 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118{
Ronald Cron5425a212020-08-04 14:58:35 +02002119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002121 psa_key_usage_t policy_usage = policy_usage_arg;
2122 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002125 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2126 * compatible with the policy and `payload_length_arg` is supposed to be
2127 * a valid input length to sign. If `payload_length_arg <= 0`,
2128 * `exercise_alg` is supposed to be forbidden by the policy. */
2129 int compatible_alg = payload_length_arg > 0;
2130 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132 size_t signature_length;
2133
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002134 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002135 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 TEST_EQUAL(expected_usage,
2137 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 psa_set_key_usage_flags(&attributes, policy_usage);
2142 psa_set_key_algorithm(&attributes, policy_alg);
2143 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2146 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 status = psa_sign_hash(key, exercise_alg,
2151 payload, payload_length,
2152 signature, sizeof(signature),
2153 &signature_length);
2154 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2155 PSA_ASSERT(status);
2156 } else {
2157 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2158 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 memset(signature, 0, sizeof(signature));
2161 status = psa_verify_hash(key, exercise_alg,
2162 payload, payload_length,
2163 signature, sizeof(signature));
2164 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2165 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2166 } else {
2167 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2168 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2171 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2172 status = psa_sign_message(key, exercise_alg,
2173 payload, payload_length,
2174 signature, sizeof(signature),
2175 &signature_length);
2176 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2177 PSA_ASSERT(status);
2178 } else {
2179 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2180 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 memset(signature, 0, sizeof(signature));
2183 status = psa_verify_message(key, exercise_alg,
2184 payload, payload_length,
2185 signature, sizeof(signature));
2186 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2187 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2188 } else {
2189 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2190 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002191 }
2192
Gilles Peskined5b33222018-06-18 22:20:03 +02002193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 psa_destroy_key(key);
2195 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002196}
2197/* END_CASE */
2198
Janos Follathba3fab92019-06-11 14:50:16 +01002199/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200void derive_key_policy(int policy_usage,
2201 int policy_alg,
2202 int key_type,
2203 data_t *key_data,
2204 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002205{
Ronald Cron5425a212020-08-04 14:58:35 +02002206 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002208 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002209 psa_status_t status;
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 psa_set_key_usage_flags(&attributes, policy_usage);
2214 psa_set_key_algorithm(&attributes, policy_alg);
2215 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2218 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2223 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2224 PSA_ASSERT(psa_key_derivation_input_bytes(
2225 &operation,
2226 PSA_KEY_DERIVATION_INPUT_SEED,
2227 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002228 }
Janos Follathba3fab92019-06-11 14:50:16 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 status = psa_key_derivation_input_key(&operation,
2231 PSA_KEY_DERIVATION_INPUT_SECRET,
2232 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (policy_alg == exercise_alg &&
2235 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2236 PSA_ASSERT(status);
2237 } else {
2238 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2239 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002240
2241exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 psa_key_derivation_abort(&operation);
2243 psa_destroy_key(key);
2244 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002245}
2246/* END_CASE */
2247
2248/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002249void agreement_key_policy(int policy_usage,
2250 int policy_alg,
2251 int key_type_arg,
2252 data_t *key_data,
2253 int exercise_alg,
2254 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002255{
Ronald Cron5425a212020-08-04 14:58:35 +02002256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002258 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002259 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002260 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002261 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 psa_set_key_usage_flags(&attributes, policy_usage);
2266 psa_set_key_algorithm(&attributes, policy_alg);
2267 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2270 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2273 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002276
2277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 psa_key_derivation_abort(&operation);
2279 psa_destroy_key(key);
2280 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002281}
2282/* END_CASE */
2283
2284/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002285void key_policy_alg2(int key_type_arg, data_t *key_data,
2286 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002287{
Ronald Cron5425a212020-08-04 14:58:35 +02002288 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002289 psa_key_type_t key_type = key_type_arg;
2290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2291 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2292 psa_key_usage_t usage = usage_arg;
2293 psa_algorithm_t alg = alg_arg;
2294 psa_algorithm_t alg2 = alg2_arg;
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 psa_set_key_usage_flags(&attributes, usage);
2299 psa_set_key_algorithm(&attributes, alg);
2300 psa_set_key_enrollment_algorithm(&attributes, alg2);
2301 psa_set_key_type(&attributes, key_type);
2302 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2303 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002304
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002305 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 usage = mbedtls_test_update_key_usage_flags(usage);
2307 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2308 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2309 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2310 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002313 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 }
2315 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002316 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002318
2319exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002320 /*
2321 * Key attributes may have been returned by psa_get_key_attributes()
2322 * thus reset them as required.
2323 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 psa_destroy_key(key);
2327 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002328}
2329/* END_CASE */
2330
2331/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002332void raw_agreement_key_policy(int policy_usage,
2333 int policy_alg,
2334 int key_type_arg,
2335 data_t *key_data,
2336 int exercise_alg,
2337 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002338{
Ronald Cron5425a212020-08-04 14:58:35 +02002339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002341 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002342 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002343 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002344 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 psa_set_key_usage_flags(&attributes, policy_usage);
2349 psa_set_key_algorithm(&attributes, policy_alg);
2350 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2353 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002358
2359exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 psa_key_derivation_abort(&operation);
2361 psa_destroy_key(key);
2362 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002363}
2364/* END_CASE */
2365
2366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002367void copy_success(int source_usage_arg,
2368 int source_alg_arg, int source_alg2_arg,
2369 unsigned int source_lifetime_arg,
2370 int type_arg, data_t *material,
2371 int copy_attributes,
2372 int target_usage_arg,
2373 int target_alg_arg, int target_alg2_arg,
2374 unsigned int target_lifetime_arg,
2375 int expected_usage_arg,
2376 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002377{
Gilles Peskineca25db92019-04-19 11:43:08 +02002378 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2379 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002380 psa_key_usage_t expected_usage = expected_usage_arg;
2381 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002382 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302383 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2384 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002385 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2386 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002387 uint8_t *export_buffer = NULL;
2388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002390
Gilles Peskineca25db92019-04-19 11:43:08 +02002391 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2393 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2394 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2395 psa_set_key_type(&source_attributes, type_arg);
2396 psa_set_key_lifetime(&source_attributes, source_lifetime);
2397 PSA_ASSERT(psa_import_key(&source_attributes,
2398 material->x, material->len,
2399 &source_key));
2400 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002401
Gilles Peskineca25db92019-04-19 11:43:08 +02002402 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002404 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002405 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 if (target_usage_arg != -1) {
2409 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2410 }
2411 if (target_alg_arg != -1) {
2412 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2413 }
2414 if (target_alg2_arg != -1) {
2415 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2416 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002417
Archana8a180362021-07-05 02:18:48 +05302418
Gilles Peskine57ab7212019-01-28 13:03:09 +01002419 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 PSA_ASSERT(psa_copy_key(source_key,
2421 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002422
2423 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002425
2426 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2428 TEST_EQUAL(psa_get_key_type(&source_attributes),
2429 psa_get_key_type(&target_attributes));
2430 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2431 psa_get_key_bits(&target_attributes));
2432 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2433 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2434 TEST_EQUAL(expected_alg2,
2435 psa_get_key_enrollment_algorithm(&target_attributes));
2436 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002437 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 ASSERT_ALLOC(export_buffer, material->len);
2439 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2440 material->len, &length));
2441 ASSERT_COMPARE(material->x, material->len,
2442 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (!psa_key_lifetime_is_external(target_lifetime)) {
2446 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302447 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 }
2449 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302450 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 }
Archana8a180362021-07-05 02:18:48 +05302452 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002455
2456exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002457 /*
2458 * Source and target key attributes may have been returned by
2459 * psa_get_key_attributes() thus reset them as required.
2460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 psa_reset_key_attributes(&source_attributes);
2462 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 PSA_DONE();
2465 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002466}
2467/* END_CASE */
2468
2469/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470void copy_fail(int source_usage_arg,
2471 int source_alg_arg, int source_alg2_arg,
2472 int source_lifetime_arg,
2473 int type_arg, data_t *material,
2474 int target_type_arg, int target_bits_arg,
2475 int target_usage_arg,
2476 int target_alg_arg, int target_alg2_arg,
2477 int target_id_arg, int target_lifetime_arg,
2478 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002479{
2480 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2481 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002482 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2483 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002487
2488 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2490 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2491 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2492 psa_set_key_type(&source_attributes, type_arg);
2493 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2494 PSA_ASSERT(psa_import_key(&source_attributes,
2495 material->x, material->len,
2496 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002497
2498 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 psa_set_key_id(&target_attributes, key_id);
2500 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2501 psa_set_key_type(&target_attributes, target_type_arg);
2502 psa_set_key_bits(&target_attributes, target_bits_arg);
2503 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2504 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2505 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002506
2507 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 TEST_EQUAL(psa_copy_key(source_key,
2509 &target_attributes, &target_key),
2510 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002513
Gilles Peskine4a644642019-05-03 17:14:08 +02002514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 psa_reset_key_attributes(&source_attributes);
2516 psa_reset_key_attributes(&target_attributes);
2517 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002518}
2519/* END_CASE */
2520
2521/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002522void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002523{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002524 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002525 /* Test each valid way of initializing the object, except for `= {0}`, as
2526 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2527 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002528 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002530 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2531 psa_hash_operation_t zero;
2532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002534
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002535 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2537 PSA_ERROR_BAD_STATE);
2538 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2539 PSA_ERROR_BAD_STATE);
2540 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2541 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002542
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002543 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 PSA_ASSERT(psa_hash_abort(&func));
2545 PSA_ASSERT(psa_hash_abort(&init));
2546 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002547}
2548/* END_CASE */
2549
2550/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002551void hash_setup(int alg_arg,
2552 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002553{
2554 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002555 uint8_t *output = NULL;
2556 size_t output_size = 0;
2557 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002558 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002559 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002560 psa_status_t status;
2561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563
Neil Armstrongedb20862022-02-07 15:47:44 +01002564 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 output_size = PSA_HASH_LENGTH(alg);
2566 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 status = psa_hash_compute(alg, NULL, 0,
2569 output, output_size, &output_length);
2570 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002571
2572 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 status = psa_hash_setup(&operation, alg);
2574 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002575
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002576 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002578
2579 /* If setup failed, reproduce the failure, so as to
2580 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (status != PSA_SUCCESS) {
2582 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2583 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002584
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002585 /* Now the operation object should be reusable. */
2586#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2588 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002589#endif
2590
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002591exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 mbedtls_free(output);
2593 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002594}
2595/* END_CASE */
2596
2597/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598void hash_compute_fail(int alg_arg, data_t *input,
2599 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002600{
2601 psa_algorithm_t alg = alg_arg;
2602 uint8_t *output = NULL;
2603 size_t output_size = output_size_arg;
2604 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002605 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002606 psa_status_t expected_status = expected_status_arg;
2607 psa_status_t status;
2608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002612
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002613 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 status = psa_hash_compute(alg, input->x, input->len,
2615 output, output_size, &output_length);
2616 TEST_EQUAL(status, expected_status);
2617 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002618
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002619 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 status = psa_hash_setup(&operation, alg);
2621 if (status == PSA_SUCCESS) {
2622 status = psa_hash_update(&operation, input->x, input->len);
2623 if (status == PSA_SUCCESS) {
2624 status = psa_hash_finish(&operation, output, output_size,
2625 &output_length);
2626 if (status == PSA_SUCCESS) {
2627 TEST_LE_U(output_length, output_size);
2628 } else {
2629 TEST_EQUAL(status, expected_status);
2630 }
2631 } else {
2632 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002633 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 } else {
2635 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002636 }
2637
Gilles Peskine0a749c82019-11-28 19:33:58 +01002638exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 PSA_ASSERT(psa_hash_abort(&operation));
2640 mbedtls_free(output);
2641 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002642}
2643/* END_CASE */
2644
2645/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646void hash_compare_fail(int alg_arg, data_t *input,
2647 data_t *reference_hash,
2648 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002649{
2650 psa_algorithm_t alg = alg_arg;
2651 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002652 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002653 psa_status_t status;
2654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002656
Neil Armstrong55a1be12022-02-07 11:23:20 +01002657 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 status = psa_hash_compare(alg, input->x, input->len,
2659 reference_hash->x, reference_hash->len);
2660 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002661
Neil Armstrong55a1be12022-02-07 11:23:20 +01002662 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 status = psa_hash_setup(&operation, alg);
2664 if (status == PSA_SUCCESS) {
2665 status = psa_hash_update(&operation, input->x, input->len);
2666 if (status == PSA_SUCCESS) {
2667 status = psa_hash_verify(&operation, reference_hash->x,
2668 reference_hash->len);
2669 TEST_EQUAL(status, expected_status);
2670 } else {
2671 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002672 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 } else {
2674 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002675 }
2676
Gilles Peskine88e08462020-01-28 20:43:00 +01002677exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 PSA_ASSERT(psa_hash_abort(&operation));
2679 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002680}
2681/* END_CASE */
2682
2683/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002684void hash_compute_compare(int alg_arg, data_t *input,
2685 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002686{
2687 psa_algorithm_t alg = alg_arg;
2688 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2689 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002690 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002691 size_t i;
2692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002694
Neil Armstrongca30a002022-02-07 11:40:23 +01002695 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2697 output, PSA_HASH_LENGTH(alg),
2698 &output_length));
2699 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2700 ASSERT_COMPARE(output, output_length,
2701 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002702
Neil Armstrongca30a002022-02-07 11:40:23 +01002703 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 PSA_ASSERT(psa_hash_setup(&operation, alg));
2705 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2706 PSA_ASSERT(psa_hash_finish(&operation, output,
2707 PSA_HASH_LENGTH(alg),
2708 &output_length));
2709 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2710 ASSERT_COMPARE(output, output_length,
2711 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002712
2713 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2715 output, sizeof(output),
2716 &output_length));
2717 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2718 ASSERT_COMPARE(output, output_length,
2719 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002720
Neil Armstrongca30a002022-02-07 11:40:23 +01002721 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 PSA_ASSERT(psa_hash_setup(&operation, alg));
2723 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2724 PSA_ASSERT(psa_hash_finish(&operation, output,
2725 sizeof(output), &output_length));
2726 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2727 ASSERT_COMPARE(output, output_length,
2728 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002729
2730 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2732 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002733
Neil Armstrongca30a002022-02-07 11:40:23 +01002734 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 PSA_ASSERT(psa_hash_setup(&operation, alg));
2736 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2737 PSA_ASSERT(psa_hash_verify(&operation, output,
2738 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002739
2740 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2742 output, output_length + 1),
2743 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002744
Neil Armstrongca30a002022-02-07 11:40:23 +01002745 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 PSA_ASSERT(psa_hash_setup(&operation, alg));
2747 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2748 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2749 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002750
2751 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2753 output, output_length - 1),
2754 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002755
Neil Armstrongca30a002022-02-07 11:40:23 +01002756 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 PSA_ASSERT(psa_hash_setup(&operation, alg));
2758 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2759 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2760 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002761
Gilles Peskine0a749c82019-11-28 19:33:58 +01002762 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 for (i = 0; i < output_length; i++) {
2764 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002765 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002766
2767 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2769 output, output_length),
2770 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002771
2772 /* 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 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2776 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002777
Gilles Peskine0a749c82019-11-28 19:33:58 +01002778 output[i] ^= 1;
2779 }
2780
2781exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 PSA_ASSERT(psa_hash_abort(&operation));
2783 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002784}
2785/* END_CASE */
2786
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002787/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002788void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002789{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002790 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002791 unsigned char input[] = "";
2792 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002793 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002794 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2795 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2797 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002798 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002799 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002800 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002803
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002804 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 PSA_ASSERT(psa_hash_setup(&operation, alg));
2806 ASSERT_OPERATION_IS_ACTIVE(operation);
2807 TEST_EQUAL(psa_hash_setup(&operation, alg),
2808 PSA_ERROR_BAD_STATE);
2809 ASSERT_OPERATION_IS_INACTIVE(operation);
2810 PSA_ASSERT(psa_hash_abort(&operation));
2811 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002812
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002813 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2815 PSA_ERROR_BAD_STATE);
2816 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002817
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002818 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002820 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 ASSERT_OPERATION_IS_ACTIVE(operation);
2822 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2823 PSA_ERROR_BAD_STATE);
2824 ASSERT_OPERATION_IS_INACTIVE(operation);
2825 PSA_ASSERT(psa_hash_abort(&operation));
2826 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002827
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002828 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 PSA_ASSERT(psa_hash_setup(&operation, alg));
2830 PSA_ASSERT(psa_hash_finish(&operation,
2831 hash, sizeof(hash), &hash_len));
2832 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2833 PSA_ERROR_BAD_STATE);
2834 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002835
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002836 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 TEST_EQUAL(psa_hash_verify(&operation,
2838 valid_hash, sizeof(valid_hash)),
2839 PSA_ERROR_BAD_STATE);
2840 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002841
2842 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 PSA_ASSERT(psa_hash_setup(&operation, alg));
2844 PSA_ASSERT(psa_hash_finish(&operation,
2845 hash, sizeof(hash), &hash_len));
2846 TEST_EQUAL(psa_hash_verify(&operation,
2847 valid_hash, sizeof(valid_hash)),
2848 PSA_ERROR_BAD_STATE);
2849 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002850
2851 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 PSA_ASSERT(psa_hash_setup(&operation, alg));
2853 ASSERT_OPERATION_IS_ACTIVE(operation);
2854 PSA_ASSERT(psa_hash_verify(&operation,
2855 valid_hash, sizeof(valid_hash)));
2856 ASSERT_OPERATION_IS_INACTIVE(operation);
2857 TEST_EQUAL(psa_hash_verify(&operation,
2858 valid_hash, sizeof(valid_hash)),
2859 PSA_ERROR_BAD_STATE);
2860 ASSERT_OPERATION_IS_INACTIVE(operation);
2861 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002862
2863 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 TEST_EQUAL(psa_hash_finish(&operation,
2865 hash, sizeof(hash), &hash_len),
2866 PSA_ERROR_BAD_STATE);
2867 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002868
2869 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 PSA_ASSERT(psa_hash_setup(&operation, alg));
2871 PSA_ASSERT(psa_hash_finish(&operation,
2872 hash, sizeof(hash), &hash_len));
2873 TEST_EQUAL(psa_hash_finish(&operation,
2874 hash, sizeof(hash), &hash_len),
2875 PSA_ERROR_BAD_STATE);
2876 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002877
2878 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 PSA_ASSERT(psa_hash_setup(&operation, alg));
2880 PSA_ASSERT(psa_hash_verify(&operation,
2881 valid_hash, sizeof(valid_hash)));
2882 TEST_EQUAL(psa_hash_finish(&operation,
2883 hash, sizeof(hash), &hash_len),
2884 PSA_ERROR_BAD_STATE);
2885 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002886
2887exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002889}
2890/* END_CASE */
2891
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002892/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002894{
2895 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002896 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2897 * appended to it */
2898 unsigned char hash[] = {
2899 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2900 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2902 };
2903 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002904 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002907
itayzafrir27e69452018-11-01 14:26:34 +02002908 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 PSA_ASSERT(psa_hash_setup(&operation, alg));
2910 ASSERT_OPERATION_IS_ACTIVE(operation);
2911 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2912 PSA_ERROR_INVALID_SIGNATURE);
2913 ASSERT_OPERATION_IS_INACTIVE(operation);
2914 PSA_ASSERT(psa_hash_abort(&operation));
2915 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002916
itayzafrir27e69452018-11-01 14:26:34 +02002917 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 PSA_ASSERT(psa_hash_setup(&operation, alg));
2919 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2920 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002921
itayzafrir27e69452018-11-01 14:26:34 +02002922 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 PSA_ASSERT(psa_hash_setup(&operation, alg));
2924 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2925 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002926
itayzafrirec93d302018-10-18 18:01:10 +03002927exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002929}
2930/* END_CASE */
2931
Ronald Cronee414c72021-03-18 18:50:08 +01002932/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002934{
2935 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002936 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002938 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002939 size_t hash_len;
2940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03002942
itayzafrir58028322018-10-25 10:22:01 +03002943 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 PSA_ASSERT(psa_hash_setup(&operation, alg));
2945 TEST_EQUAL(psa_hash_finish(&operation,
2946 hash, expected_size - 1, &hash_len),
2947 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03002948
2949exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03002951}
2952/* END_CASE */
2953
Ronald Cronee414c72021-03-18 18:50:08 +01002954/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002955void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002956{
2957 psa_algorithm_t alg = PSA_ALG_SHA_256;
2958 unsigned char hash[PSA_HASH_MAX_SIZE];
2959 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2960 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2961 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2962 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2963 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2964 size_t hash_len;
2965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 PSA_ASSERT(psa_crypto_init());
2967 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
2970 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
2971 PSA_ASSERT(psa_hash_finish(&op_finished,
2972 hash, sizeof(hash), &hash_len));
2973 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
2974 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
2977 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
2980 PSA_ASSERT(psa_hash_finish(&op_init,
2981 hash, sizeof(hash), &hash_len));
2982 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
2983 PSA_ASSERT(psa_hash_finish(&op_finished,
2984 hash, sizeof(hash), &hash_len));
2985 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
2986 PSA_ASSERT(psa_hash_finish(&op_aborted,
2987 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002988
2989exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 psa_hash_abort(&op_source);
2991 psa_hash_abort(&op_init);
2992 psa_hash_abort(&op_setup);
2993 psa_hash_abort(&op_finished);
2994 psa_hash_abort(&op_aborted);
2995 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002996}
2997/* END_CASE */
2998
Ronald Cronee414c72021-03-18 18:50:08 +01002999/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003001{
3002 psa_algorithm_t alg = PSA_ALG_SHA_256;
3003 unsigned char hash[PSA_HASH_MAX_SIZE];
3004 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3005 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3006 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3007 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3008 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3009 size_t hash_len;
3010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3014 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3015 PSA_ASSERT(psa_hash_finish(&op_finished,
3016 hash, sizeof(hash), &hash_len));
3017 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3018 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3021 PSA_ASSERT(psa_hash_finish(&op_target,
3022 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3025 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3026 PSA_ERROR_BAD_STATE);
3027 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3028 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003029
3030exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 psa_hash_abort(&op_target);
3032 psa_hash_abort(&op_init);
3033 psa_hash_abort(&op_setup);
3034 psa_hash_abort(&op_finished);
3035 psa_hash_abort(&op_aborted);
3036 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037}
3038/* END_CASE */
3039
itayzafrir58028322018-10-25 10:22:01 +03003040/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003041void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003042{
Jaeden Amero252ef282019-02-15 14:05:35 +00003043 const uint8_t input[1] = { 0 };
3044
Jaeden Amero769ce272019-01-04 11:48:03 +00003045 /* Test each valid way of initializing the object, except for `= {0}`, as
3046 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3047 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003048 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003050 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3051 psa_mac_operation_t zero;
3052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003054
Jaeden Amero252ef282019-02-15 14:05:35 +00003055 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 TEST_EQUAL(psa_mac_update(&func,
3057 input, sizeof(input)),
3058 PSA_ERROR_BAD_STATE);
3059 TEST_EQUAL(psa_mac_update(&init,
3060 input, sizeof(input)),
3061 PSA_ERROR_BAD_STATE);
3062 TEST_EQUAL(psa_mac_update(&zero,
3063 input, sizeof(input)),
3064 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003065
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003066 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 PSA_ASSERT(psa_mac_abort(&func));
3068 PSA_ASSERT(psa_mac_abort(&init));
3069 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003070}
3071/* END_CASE */
3072
3073/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003074void mac_setup(int key_type_arg,
3075 data_t *key,
3076 int alg_arg,
3077 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003078{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003079 psa_key_type_t key_type = key_type_arg;
3080 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003081 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003082 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003083 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3084#if defined(KNOWN_SUPPORTED_MAC_ALG)
3085 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3086#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3091 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 }
3094 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003095
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003096 /* The operation object should be reusable. */
3097#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3099 smoke_test_key_data,
3100 sizeof(smoke_test_key_data),
3101 KNOWN_SUPPORTED_MAC_ALG,
3102 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 }
3105 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003106#endif
3107
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003108exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003110}
3111/* END_CASE */
3112
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003113/* 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 +01003114void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003115{
Ronald Cron5425a212020-08-04 14:58:35 +02003116 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003117 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3118 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003119 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003120 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3121 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3123 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3126 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3127 size_t sign_mac_length = 0;
3128 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3129 const uint8_t verify_mac[] = {
3130 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3131 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3133 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 PSA_ASSERT(psa_crypto_init());
3136 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3137 psa_set_key_algorithm(&attributes, alg);
3138 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3141 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003142
Jaeden Amero252ef282019-02-15 14:05:35 +00003143 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3145 PSA_ERROR_BAD_STATE);
3146 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003147
3148 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3150 &sign_mac_length),
3151 PSA_ERROR_BAD_STATE);
3152 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003153
3154 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 TEST_EQUAL(psa_mac_verify_finish(&operation,
3156 verify_mac, sizeof(verify_mac)),
3157 PSA_ERROR_BAD_STATE);
3158 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003159
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003160 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3162 ASSERT_OPERATION_IS_ACTIVE(operation);
3163 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3164 PSA_ERROR_BAD_STATE);
3165 ASSERT_OPERATION_IS_INACTIVE(operation);
3166 PSA_ASSERT(psa_mac_abort(&operation));
3167 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003168
Jaeden Amero252ef282019-02-15 14:05:35 +00003169 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3171 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3172 PSA_ASSERT(psa_mac_sign_finish(&operation,
3173 sign_mac, sizeof(sign_mac),
3174 &sign_mac_length));
3175 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3176 PSA_ERROR_BAD_STATE);
3177 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003178
3179 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3181 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3182 PSA_ASSERT(psa_mac_verify_finish(&operation,
3183 verify_mac, sizeof(verify_mac)));
3184 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3185 PSA_ERROR_BAD_STATE);
3186 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003187
3188 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3190 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3191 PSA_ASSERT(psa_mac_sign_finish(&operation,
3192 sign_mac, sizeof(sign_mac),
3193 &sign_mac_length));
3194 TEST_EQUAL(psa_mac_sign_finish(&operation,
3195 sign_mac, sizeof(sign_mac),
3196 &sign_mac_length),
3197 PSA_ERROR_BAD_STATE);
3198 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003199
3200 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3202 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3203 PSA_ASSERT(psa_mac_verify_finish(&operation,
3204 verify_mac, sizeof(verify_mac)));
3205 TEST_EQUAL(psa_mac_verify_finish(&operation,
3206 verify_mac, sizeof(verify_mac)),
3207 PSA_ERROR_BAD_STATE);
3208 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003209
3210 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3212 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3213 ASSERT_OPERATION_IS_ACTIVE(operation);
3214 TEST_EQUAL(psa_mac_verify_finish(&operation,
3215 verify_mac, sizeof(verify_mac)),
3216 PSA_ERROR_BAD_STATE);
3217 ASSERT_OPERATION_IS_INACTIVE(operation);
3218 PSA_ASSERT(psa_mac_abort(&operation));
3219 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003220
3221 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3223 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3224 ASSERT_OPERATION_IS_ACTIVE(operation);
3225 TEST_EQUAL(psa_mac_sign_finish(&operation,
3226 sign_mac, sizeof(sign_mac),
3227 &sign_mac_length),
3228 PSA_ERROR_BAD_STATE);
3229 ASSERT_OPERATION_IS_INACTIVE(operation);
3230 PSA_ASSERT(psa_mac_abort(&operation));
3231 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003234
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003237}
3238/* END_CASE */
3239
3240/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003241void mac_sign_verify_multi(int key_type_arg,
3242 data_t *key_data,
3243 int alg_arg,
3244 data_t *input,
3245 int is_verify,
3246 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003247{
3248 size_t data_part_len = 0;
3249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003251 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003253
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 if (mac_multipart_internal_func(key_type_arg, key_data,
3255 alg_arg,
3256 input, data_part_len,
3257 expected_mac,
3258 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003259 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003261
3262 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 if (mac_multipart_internal_func(key_type_arg, key_data,
3266 alg_arg,
3267 input, data_part_len,
3268 expected_mac,
3269 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003270 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003272 }
3273
3274 /* Goto is required to silence warnings about unused labels, as we
3275 * don't actually do any test assertions in this function. */
3276 goto exit;
3277}
3278/* END_CASE */
3279
3280/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281void mac_sign(int key_type_arg,
3282 data_t *key_data,
3283 int alg_arg,
3284 data_t *input,
3285 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003286{
Ronald Cron5425a212020-08-04 14:58:35 +02003287 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003288 psa_key_type_t key_type = key_type_arg;
3289 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003290 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003291 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003292 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003293 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003295 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003296 const size_t output_sizes_to_test[] = {
3297 0,
3298 1,
3299 expected_mac->len - 1,
3300 expected_mac->len,
3301 expected_mac->len + 1,
3302 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003303
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003305 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003307
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3311 psa_set_key_algorithm(&attributes, alg);
3312 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003313
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3315 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003316
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003318 const size_t output_size = output_sizes_to_test[i];
3319 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 (output_size >= expected_mac->len ? PSA_SUCCESS :
3321 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003322
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 mbedtls_test_set_step(output_size);
3324 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003325
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003326 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 TEST_EQUAL(psa_mac_compute(key, alg,
3328 input->x, input->len,
3329 actual_mac, output_size, &mac_length),
3330 expected_status);
3331 if (expected_status == PSA_SUCCESS) {
3332 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3333 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003334 }
3335
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 if (output_size > 0) {
3337 memset(actual_mac, 0, output_size);
3338 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003339
3340 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3342 PSA_ASSERT(psa_mac_update(&operation,
3343 input->x, input->len));
3344 TEST_EQUAL(psa_mac_sign_finish(&operation,
3345 actual_mac, output_size,
3346 &mac_length),
3347 expected_status);
3348 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 if (expected_status == PSA_SUCCESS) {
3351 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3352 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003353 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003355 actual_mac = NULL;
3356 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003357
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003358exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 psa_mac_abort(&operation);
3360 psa_destroy_key(key);
3361 PSA_DONE();
3362 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003363}
3364/* END_CASE */
3365
3366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003367void mac_verify(int key_type_arg,
3368 data_t *key_data,
3369 int alg_arg,
3370 data_t *input,
3371 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003372{
Ronald Cron5425a212020-08-04 14:58:35 +02003373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003374 psa_key_type_t key_type = key_type_arg;
3375 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003376 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003378 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3385 psa_set_key_algorithm(&attributes, alg);
3386 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3389 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003390
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003391 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3393 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003394
3395 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3397 PSA_ASSERT(psa_mac_update(&operation,
3398 input->x, input->len));
3399 PSA_ASSERT(psa_mac_verify_finish(&operation,
3400 expected_mac->x,
3401 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003402
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003403 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 TEST_EQUAL(psa_mac_verify(key, alg,
3405 input->x, input->len,
3406 expected_mac->x,
3407 expected_mac->len - 1),
3408 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003409
3410 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3412 PSA_ASSERT(psa_mac_update(&operation,
3413 input->x, input->len));
3414 TEST_EQUAL(psa_mac_verify_finish(&operation,
3415 expected_mac->x,
3416 expected_mac->len - 1),
3417 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003418
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003419 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3421 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3422 TEST_EQUAL(psa_mac_verify(key, alg,
3423 input->x, input->len,
3424 perturbed_mac, expected_mac->len + 1),
3425 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003426
3427 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3429 PSA_ASSERT(psa_mac_update(&operation,
3430 input->x, input->len));
3431 TEST_EQUAL(psa_mac_verify_finish(&operation,
3432 perturbed_mac,
3433 expected_mac->len + 1),
3434 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003435
3436 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 for (size_t i = 0; i < expected_mac->len; i++) {
3438 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003439 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003440
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 TEST_EQUAL(psa_mac_verify(key, alg,
3442 input->x, input->len,
3443 perturbed_mac, expected_mac->len),
3444 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003445
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3447 PSA_ASSERT(psa_mac_update(&operation,
3448 input->x, input->len));
3449 TEST_EQUAL(psa_mac_verify_finish(&operation,
3450 perturbed_mac,
3451 expected_mac->len),
3452 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003453 perturbed_mac[i] ^= 1;
3454 }
3455
Gilles Peskine8c9def32018-02-08 10:02:12 +01003456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 psa_mac_abort(&operation);
3458 psa_destroy_key(key);
3459 PSA_DONE();
3460 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003461}
3462/* END_CASE */
3463
3464/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003465void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003466{
Jaeden Ameroab439972019-02-15 14:12:05 +00003467 const uint8_t input[1] = { 0 };
3468 unsigned char output[1] = { 0 };
3469 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003470 /* Test each valid way of initializing the object, except for `= {0}`, as
3471 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3472 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003473 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003475 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3476 psa_cipher_operation_t zero;
3477
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003479
Jaeden Ameroab439972019-02-15 14:12:05 +00003480 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 TEST_EQUAL(psa_cipher_update(&func,
3482 input, sizeof(input),
3483 output, sizeof(output),
3484 &output_length),
3485 PSA_ERROR_BAD_STATE);
3486 TEST_EQUAL(psa_cipher_update(&init,
3487 input, sizeof(input),
3488 output, sizeof(output),
3489 &output_length),
3490 PSA_ERROR_BAD_STATE);
3491 TEST_EQUAL(psa_cipher_update(&zero,
3492 input, sizeof(input),
3493 output, sizeof(output),
3494 &output_length),
3495 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003496
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003497 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 PSA_ASSERT(psa_cipher_abort(&func));
3499 PSA_ASSERT(psa_cipher_abort(&init));
3500 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003501}
3502/* END_CASE */
3503
3504/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003505void cipher_setup(int key_type_arg,
3506 data_t *key,
3507 int alg_arg,
3508 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003509{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003510 psa_key_type_t key_type = key_type_arg;
3511 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003512 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003513 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003514 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003515#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003516 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3517#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003518
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003520
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3522 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003523 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 }
3525 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003526
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003527 /* The operation object should be reusable. */
3528#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3530 smoke_test_key_data,
3531 sizeof(smoke_test_key_data),
3532 KNOWN_SUPPORTED_CIPHER_ALG,
3533 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003534 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 }
3536 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003537#endif
3538
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003539exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 psa_cipher_abort(&operation);
3541 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003542}
3543/* END_CASE */
3544
Ronald Cronee414c72021-03-18 18:50:08 +01003545/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003546void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003547{
Ronald Cron5425a212020-08-04 14:58:35 +02003548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003549 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3550 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003552 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003553 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003554 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003555 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 0xaa, 0xaa, 0xaa, 0xaa
3557 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003558 const uint8_t text[] = {
3559 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 0xbb, 0xbb, 0xbb, 0xbb
3561 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003562 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003563 size_t length = 0;
3564
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 PSA_ASSERT(psa_crypto_init());
3566 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3567 psa_set_key_algorithm(&attributes, alg);
3568 psa_set_key_type(&attributes, key_type);
3569 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3570 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003571
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003572 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3574 ASSERT_OPERATION_IS_ACTIVE(operation);
3575 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3576 PSA_ERROR_BAD_STATE);
3577 ASSERT_OPERATION_IS_INACTIVE(operation);
3578 PSA_ASSERT(psa_cipher_abort(&operation));
3579 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003580
3581 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3583 ASSERT_OPERATION_IS_ACTIVE(operation);
3584 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3585 PSA_ERROR_BAD_STATE);
3586 ASSERT_OPERATION_IS_INACTIVE(operation);
3587 PSA_ASSERT(psa_cipher_abort(&operation));
3588 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003589
Jaeden Ameroab439972019-02-15 14:12:05 +00003590 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3592 buffer, sizeof(buffer),
3593 &length),
3594 PSA_ERROR_BAD_STATE);
3595 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003596
3597 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3599 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3600 buffer, sizeof(buffer),
3601 &length));
3602 ASSERT_OPERATION_IS_ACTIVE(operation);
3603 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3604 buffer, sizeof(buffer),
3605 &length),
3606 PSA_ERROR_BAD_STATE);
3607 ASSERT_OPERATION_IS_INACTIVE(operation);
3608 PSA_ASSERT(psa_cipher_abort(&operation));
3609 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003610
3611 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3613 PSA_ASSERT(psa_cipher_set_iv(&operation,
3614 iv, sizeof(iv)));
3615 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3616 buffer, sizeof(buffer),
3617 &length),
3618 PSA_ERROR_BAD_STATE);
3619 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003620
3621 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 TEST_EQUAL(psa_cipher_set_iv(&operation,
3623 iv, sizeof(iv)),
3624 PSA_ERROR_BAD_STATE);
3625 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003626
3627 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003628 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3629 PSA_ASSERT(psa_cipher_set_iv(&operation,
3630 iv, sizeof(iv)));
3631 ASSERT_OPERATION_IS_ACTIVE(operation);
3632 TEST_EQUAL(psa_cipher_set_iv(&operation,
3633 iv, sizeof(iv)),
3634 PSA_ERROR_BAD_STATE);
3635 ASSERT_OPERATION_IS_INACTIVE(operation);
3636 PSA_ASSERT(psa_cipher_abort(&operation));
3637 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003638
3639 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3641 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3642 buffer, sizeof(buffer),
3643 &length));
3644 TEST_EQUAL(psa_cipher_set_iv(&operation,
3645 iv, sizeof(iv)),
3646 PSA_ERROR_BAD_STATE);
3647 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003648
3649 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 TEST_EQUAL(psa_cipher_update(&operation,
3651 text, sizeof(text),
3652 buffer, sizeof(buffer),
3653 &length),
3654 PSA_ERROR_BAD_STATE);
3655 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003656
3657 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003658 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3659 ASSERT_OPERATION_IS_ACTIVE(operation);
3660 TEST_EQUAL(psa_cipher_update(&operation,
3661 text, sizeof(text),
3662 buffer, sizeof(buffer),
3663 &length),
3664 PSA_ERROR_BAD_STATE);
3665 ASSERT_OPERATION_IS_INACTIVE(operation);
3666 PSA_ASSERT(psa_cipher_abort(&operation));
3667 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003668
3669 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003670 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3671 PSA_ASSERT(psa_cipher_set_iv(&operation,
3672 iv, sizeof(iv)));
3673 PSA_ASSERT(psa_cipher_finish(&operation,
3674 buffer, sizeof(buffer), &length));
3675 TEST_EQUAL(psa_cipher_update(&operation,
3676 text, sizeof(text),
3677 buffer, sizeof(buffer),
3678 &length),
3679 PSA_ERROR_BAD_STATE);
3680 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003681
3682 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003683 TEST_EQUAL(psa_cipher_finish(&operation,
3684 buffer, sizeof(buffer), &length),
3685 PSA_ERROR_BAD_STATE);
3686 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003687
3688 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003689 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003690 /* Not calling update means we are encrypting an empty buffer, which is OK
3691 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003692 ASSERT_OPERATION_IS_ACTIVE(operation);
3693 TEST_EQUAL(psa_cipher_finish(&operation,
3694 buffer, sizeof(buffer), &length),
3695 PSA_ERROR_BAD_STATE);
3696 ASSERT_OPERATION_IS_INACTIVE(operation);
3697 PSA_ASSERT(psa_cipher_abort(&operation));
3698 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003699
3700 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003701 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3702 PSA_ASSERT(psa_cipher_set_iv(&operation,
3703 iv, sizeof(iv)));
3704 PSA_ASSERT(psa_cipher_finish(&operation,
3705 buffer, sizeof(buffer), &length));
3706 TEST_EQUAL(psa_cipher_finish(&operation,
3707 buffer, sizeof(buffer), &length),
3708 PSA_ERROR_BAD_STATE);
3709 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003710
Gilles Peskine449bd832023-01-11 14:50:10 +01003711 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003712
Jaeden Ameroab439972019-02-15 14:12:05 +00003713exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003714 psa_cipher_abort(&operation);
3715 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003716}
3717/* END_CASE */
3718
3719/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003720void cipher_encrypt_fail(int alg_arg,
3721 int key_type_arg,
3722 data_t *key_data,
3723 data_t *input,
3724 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003725{
Ronald Cron5425a212020-08-04 14:58:35 +02003726 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003727 psa_status_t status;
3728 psa_key_type_t key_type = key_type_arg;
3729 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003730 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003731 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003732 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3733 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003734 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003735 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003736 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003737 size_t function_output_length;
3738 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3740
Gilles Peskine449bd832023-01-11 14:50:10 +01003741 if (PSA_ERROR_BAD_STATE != expected_status) {
3742 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003743
Gilles Peskine449bd832023-01-11 14:50:10 +01003744 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3745 psa_set_key_algorithm(&attributes, alg);
3746 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003747
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3749 input->len);
3750 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003751
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3753 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003754 }
3755
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003756 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003757 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3758 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003759
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003761
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003762 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 status = psa_cipher_encrypt_setup(&operation, key, alg);
3764 if (status == PSA_SUCCESS) {
3765 if (alg != PSA_ALG_ECB_NO_PADDING) {
3766 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3767 iv, iv_size,
3768 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003769 }
3770
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 status = psa_cipher_update(&operation, input->x, input->len,
3772 output, output_buffer_size,
3773 &function_output_length);
3774 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003775 output_length += function_output_length;
3776
Gilles Peskine449bd832023-01-11 14:50:10 +01003777 status = psa_cipher_finish(&operation, output + output_length,
3778 output_buffer_size - output_length,
3779 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003780
Gilles Peskine449bd832023-01-11 14:50:10 +01003781 TEST_EQUAL(status, expected_status);
3782 } else {
3783 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003784 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 } else {
3786 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003787 }
3788
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003789exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 psa_cipher_abort(&operation);
3791 mbedtls_free(output);
3792 psa_destroy_key(key);
3793 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003794}
3795/* END_CASE */
3796
3797/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003798void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3799 data_t *input, int iv_length,
3800 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003801{
3802 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3803 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3805 size_t output_buffer_size = 0;
3806 unsigned char *output = NULL;
3807
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3809 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003810
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003812
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);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003816
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3818 &key));
3819 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3820 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3821 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003822
3823exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003824 psa_cipher_abort(&operation);
3825 mbedtls_free(output);
3826 psa_destroy_key(key);
3827 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003828}
3829/* END_CASE */
3830
3831/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003832void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3833 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003834{
3835 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3836 psa_key_type_t key_type = key_type_arg;
3837 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003838 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3839 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003840 unsigned char *output = NULL;
3841 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003842 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3844
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003846
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003847 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 TEST_LE_U(ciphertext->len,
3849 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3850 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3851 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3852 TEST_LE_U(plaintext->len,
3853 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3854 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3855 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003856
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003857
3858 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003859 psa_set_key_usage_flags(&attributes,
3860 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3861 psa_set_key_algorithm(&attributes, alg);
3862 psa_set_key_type(&attributes, key_type);
3863 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3864 &key));
3865 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3866 plaintext->len);
3867 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003868
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003869 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3871 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3872 PSA_ERROR_BAD_STATE);
3873 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3874 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3875 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003876
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003877 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3879 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3880 &length),
3881 PSA_ERROR_BAD_STATE);
3882 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3883 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3884 &length),
3885 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003886
Gilles Peskine286c3142022-04-20 17:09:38 +02003887 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003889 output_length = 0;
3890 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 PSA_ASSERT(psa_cipher_update(&operation,
3892 plaintext->x, plaintext->len,
3893 output, output_buffer_size,
3894 &length));
3895 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003896 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 PSA_ASSERT(psa_cipher_finish(&operation,
3898 mbedtls_buffer_offset(output, output_length),
3899 output_buffer_size - output_length,
3900 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003901 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3903 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003904
Gilles Peskine286c3142022-04-20 17:09:38 +02003905 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003907 output_length = 0;
3908 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 PSA_ASSERT(psa_cipher_update(&operation,
3910 ciphertext->x, ciphertext->len,
3911 output, output_buffer_size,
3912 &length));
3913 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003914 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 PSA_ASSERT(psa_cipher_finish(&operation,
3916 mbedtls_buffer_offset(output, output_length),
3917 output_buffer_size - output_length,
3918 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003919 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 ASSERT_COMPARE(plaintext->x, plaintext->len,
3921 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003922
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003923 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003924 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3926 output, output_buffer_size,
3927 &output_length));
3928 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3929 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003930
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003931 /* One-shot decryption */
3932 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3934 output, output_buffer_size,
3935 &output_length));
3936 ASSERT_COMPARE(plaintext->x, plaintext->len,
3937 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003938
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003939exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 PSA_ASSERT(psa_cipher_abort(&operation));
3941 mbedtls_free(output);
3942 psa_cipher_abort(&operation);
3943 psa_destroy_key(key);
3944 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003945}
3946/* END_CASE */
3947
3948/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003949void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01003950{
3951 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3952 psa_algorithm_t alg = alg_arg;
3953 psa_key_type_t key_type = key_type_arg;
3954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3955 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3956 psa_status_t status;
3957
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01003959
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3961 psa_set_key_algorithm(&attributes, alg);
3962 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01003963
3964 /* Usage of either of these two size macros would cause divide by zero
3965 * with incorrect key types previously. Input length should be irrelevant
3966 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
3968 0);
3969 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01003970
3971
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3973 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01003974
3975 /* Should fail due to invalid alg type (to support invalid key type).
3976 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01003978
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01003980
3981exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 psa_cipher_abort(&operation);
3983 psa_destroy_key(key);
3984 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01003985}
3986/* END_CASE */
3987
3988/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003989void cipher_encrypt_validation(int alg_arg,
3990 int key_type_arg,
3991 data_t *key_data,
3992 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003993{
3994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3995 psa_key_type_t key_type = key_type_arg;
3996 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003998 unsigned char *output1 = NULL;
3999 size_t output1_buffer_size = 0;
4000 size_t output1_length = 0;
4001 unsigned char *output2 = NULL;
4002 size_t output2_buffer_size = 0;
4003 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004004 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004005 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004007
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004009
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4011 psa_set_key_algorithm(&attributes, alg);
4012 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4015 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4016 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4017 ASSERT_ALLOC(output1, output1_buffer_size);
4018 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004019
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4021 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004022
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004023 /* The one-shot cipher encryption uses generated iv so validating
4024 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4026 output1_buffer_size, &output1_length));
4027 TEST_LE_U(output1_length,
4028 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4029 TEST_LE_U(output1_length,
4030 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4033 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 PSA_ASSERT(psa_cipher_update(&operation,
4036 input->x, input->len,
4037 output2, output2_buffer_size,
4038 &function_output_length));
4039 TEST_LE_U(function_output_length,
4040 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4041 TEST_LE_U(function_output_length,
4042 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004043 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004044
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 PSA_ASSERT(psa_cipher_finish(&operation,
4046 output2 + output2_length,
4047 output2_buffer_size - output2_length,
4048 &function_output_length));
4049 TEST_LE_U(function_output_length,
4050 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4051 TEST_LE_U(function_output_length,
4052 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004053 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004054
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 PSA_ASSERT(psa_cipher_abort(&operation));
4056 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4057 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004058
Gilles Peskine50e586b2018-06-08 14:28:46 +02004059exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 psa_cipher_abort(&operation);
4061 mbedtls_free(output1);
4062 mbedtls_free(output2);
4063 psa_destroy_key(key);
4064 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004065}
4066/* END_CASE */
4067
4068/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004069void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4070 data_t *key_data, data_t *iv,
4071 data_t *input,
4072 int first_part_size_arg,
4073 int output1_length_arg, int output2_length_arg,
4074 data_t *expected_output,
4075 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004076{
Ronald Cron5425a212020-08-04 14:58:35 +02004077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004078 psa_key_type_t key_type = key_type_arg;
4079 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004080 psa_status_t status;
4081 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004082 size_t first_part_size = first_part_size_arg;
4083 size_t output1_length = output1_length_arg;
4084 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004085 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004086 size_t output_buffer_size = 0;
4087 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004088 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004089 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4095 psa_set_key_algorithm(&attributes, alg);
4096 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4099 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004102
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 if (iv->len > 0) {
4104 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004105 }
4106
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4108 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4109 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 TEST_LE_U(first_part_size, input->len);
4112 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4113 output, output_buffer_size,
4114 &function_output_length));
4115 TEST_ASSERT(function_output_length == output1_length);
4116 TEST_LE_U(function_output_length,
4117 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4118 TEST_LE_U(function_output_length,
4119 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004120 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (first_part_size < input->len) {
4123 PSA_ASSERT(psa_cipher_update(&operation,
4124 input->x + first_part_size,
4125 input->len - first_part_size,
4126 (output_buffer_size == 0 ? NULL :
4127 output + total_output_length),
4128 output_buffer_size - total_output_length,
4129 &function_output_length));
4130 TEST_ASSERT(function_output_length == output2_length);
4131 TEST_LE_U(function_output_length,
4132 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4133 alg,
4134 input->len - first_part_size));
4135 TEST_LE_U(function_output_length,
4136 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004137 total_output_length += function_output_length;
4138 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 status = psa_cipher_finish(&operation,
4141 (output_buffer_size == 0 ? NULL :
4142 output + total_output_length),
4143 output_buffer_size - total_output_length,
4144 &function_output_length);
4145 TEST_LE_U(function_output_length,
4146 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4147 TEST_LE_U(function_output_length,
4148 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004149 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 if (expected_status == PSA_SUCCESS) {
4153 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 ASSERT_COMPARE(expected_output->x, expected_output->len,
4156 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004157 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004158
4159exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 psa_cipher_abort(&operation);
4161 mbedtls_free(output);
4162 psa_destroy_key(key);
4163 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004164}
4165/* END_CASE */
4166
4167/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004168void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4169 data_t *key_data, data_t *iv,
4170 data_t *input,
4171 int first_part_size_arg,
4172 int output1_length_arg, int output2_length_arg,
4173 data_t *expected_output,
4174 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004175{
Ronald Cron5425a212020-08-04 14:58:35 +02004176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004177 psa_key_type_t key_type = key_type_arg;
4178 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004179 psa_status_t status;
4180 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004181 size_t first_part_size = first_part_size_arg;
4182 size_t output1_length = output1_length_arg;
4183 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004184 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004185 size_t output_buffer_size = 0;
4186 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004187 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004188 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4194 psa_set_key_algorithm(&attributes, alg);
4195 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4198 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 if (iv->len > 0) {
4203 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004204 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004205
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4207 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4208 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 TEST_LE_U(first_part_size, input->len);
4211 PSA_ASSERT(psa_cipher_update(&operation,
4212 input->x, first_part_size,
4213 output, output_buffer_size,
4214 &function_output_length));
4215 TEST_ASSERT(function_output_length == output1_length);
4216 TEST_LE_U(function_output_length,
4217 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4218 TEST_LE_U(function_output_length,
4219 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004220 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 if (first_part_size < input->len) {
4223 PSA_ASSERT(psa_cipher_update(&operation,
4224 input->x + first_part_size,
4225 input->len - first_part_size,
4226 (output_buffer_size == 0 ? NULL :
4227 output + total_output_length),
4228 output_buffer_size - total_output_length,
4229 &function_output_length));
4230 TEST_ASSERT(function_output_length == output2_length);
4231 TEST_LE_U(function_output_length,
4232 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4233 alg,
4234 input->len - first_part_size));
4235 TEST_LE_U(function_output_length,
4236 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004237 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004238 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 status = psa_cipher_finish(&operation,
4241 (output_buffer_size == 0 ? NULL :
4242 output + total_output_length),
4243 output_buffer_size - total_output_length,
4244 &function_output_length);
4245 TEST_LE_U(function_output_length,
4246 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4247 TEST_LE_U(function_output_length,
4248 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004249 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 if (expected_status == PSA_SUCCESS) {
4253 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 ASSERT_COMPARE(expected_output->x, expected_output->len,
4256 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004257 }
4258
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 psa_cipher_abort(&operation);
4261 mbedtls_free(output);
4262 psa_destroy_key(key);
4263 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004264}
4265/* END_CASE */
4266
Gilles Peskine50e586b2018-06-08 14:28:46 +02004267/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004268void cipher_decrypt_fail(int alg_arg,
4269 int key_type_arg,
4270 data_t *key_data,
4271 data_t *iv,
4272 data_t *input_arg,
4273 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004274{
4275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4276 psa_status_t status;
4277 psa_key_type_t key_type = key_type_arg;
4278 psa_algorithm_t alg = alg_arg;
4279 psa_status_t expected_status = expected_status_arg;
4280 unsigned char *input = NULL;
4281 size_t input_buffer_size = 0;
4282 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004283 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004284 size_t output_buffer_size = 0;
4285 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004286 size_t function_output_length;
4287 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (PSA_ERROR_BAD_STATE != expected_status) {
4291 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004292
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4294 psa_set_key_algorithm(&attributes, alg);
4295 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4298 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004299 }
4300
4301 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4303 if (input_buffer_size > 0) {
4304 ASSERT_ALLOC(input, input_buffer_size);
4305 memcpy(input, iv->x, iv->len);
4306 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004307 }
4308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4310 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004311
Neil Armstrong66a479f2022-02-07 15:41:19 +01004312 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4314 output_buffer_size, &output_length);
4315 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004316
Neil Armstrong66a479f2022-02-07 15:41:19 +01004317 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 status = psa_cipher_decrypt_setup(&operation, key, alg);
4319 if (status == PSA_SUCCESS) {
4320 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4321 input_arg->len) +
4322 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4323 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 if (iv->len > 0) {
4326 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 if (status != PSA_SUCCESS) {
4329 TEST_EQUAL(status, expected_status);
4330 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004331 }
4332
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 if (status == PSA_SUCCESS) {
4334 status = psa_cipher_update(&operation,
4335 input_arg->x, input_arg->len,
4336 output_multi, output_buffer_size,
4337 &function_output_length);
4338 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004339 output_length = function_output_length;
4340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 status = psa_cipher_finish(&operation,
4342 output_multi + output_length,
4343 output_buffer_size - output_length,
4344 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004345
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 TEST_EQUAL(status, expected_status);
4347 } else {
4348 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004349 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 } else {
4351 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004352 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 } else {
4354 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004355 }
4356
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004357exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 psa_cipher_abort(&operation);
4359 mbedtls_free(input);
4360 mbedtls_free(output);
4361 mbedtls_free(output_multi);
4362 psa_destroy_key(key);
4363 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004364}
4365/* END_CASE */
4366
4367/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004368void cipher_decrypt(int alg_arg,
4369 int key_type_arg,
4370 data_t *key_data,
4371 data_t *iv,
4372 data_t *input_arg,
4373 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004374{
4375 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4376 psa_key_type_t key_type = key_type_arg;
4377 psa_algorithm_t alg = alg_arg;
4378 unsigned char *input = NULL;
4379 size_t input_buffer_size = 0;
4380 unsigned char *output = NULL;
4381 size_t output_buffer_size = 0;
4382 size_t output_length = 0;
4383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4388 psa_set_key_algorithm(&attributes, alg);
4389 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004390
4391 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4393 if (input_buffer_size > 0) {
4394 ASSERT_ALLOC(input, input_buffer_size);
4395 memcpy(input, iv->x, iv->len);
4396 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004397 }
4398
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4400 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4403 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4406 output_buffer_size, &output_length));
4407 TEST_LE_U(output_length,
4408 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4409 TEST_LE_U(output_length,
4410 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 ASSERT_COMPARE(expected_output->x, expected_output->len,
4413 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004414exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 mbedtls_free(input);
4416 mbedtls_free(output);
4417 psa_destroy_key(key);
4418 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004419}
4420/* END_CASE */
4421
4422/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004423void cipher_verify_output(int alg_arg,
4424 int key_type_arg,
4425 data_t *key_data,
4426 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004427{
Ronald Cron5425a212020-08-04 14:58:35 +02004428 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004429 psa_key_type_t key_type = key_type_arg;
4430 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004431 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004432 size_t output1_size = 0;
4433 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004434 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004435 size_t output2_size = 0;
4436 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4442 psa_set_key_algorithm(&attributes, alg);
4443 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004444
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4446 &key));
4447 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4448 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4451 output1, output1_size,
4452 &output1_length));
4453 TEST_LE_U(output1_length,
4454 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4455 TEST_LE_U(output1_length,
4456 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004457
4458 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4462 output2, output2_size,
4463 &output2_length));
4464 TEST_LE_U(output2_length,
4465 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4466 TEST_LE_U(output2_length,
4467 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004470
4471exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 mbedtls_free(output1);
4473 mbedtls_free(output2);
4474 psa_destroy_key(key);
4475 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004476}
4477/* END_CASE */
4478
4479/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004480void cipher_verify_output_multipart(int alg_arg,
4481 int key_type_arg,
4482 data_t *key_data,
4483 data_t *input,
4484 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004485{
Ronald Cron5425a212020-08-04 14:58:35 +02004486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004487 psa_key_type_t key_type = key_type_arg;
4488 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004489 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004491 size_t iv_size = 16;
4492 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004493 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004494 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004495 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004496 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004497 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004498 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004499 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004500 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4501 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4507 psa_set_key_algorithm(&attributes, alg);
4508 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4511 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4514 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (alg != PSA_ALG_ECB_NO_PADDING) {
4517 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4518 iv, iv_size,
4519 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004520 }
4521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4523 TEST_LE_U(output1_buffer_size,
4524 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4525 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4530 output1, output1_buffer_size,
4531 &function_output_length));
4532 TEST_LE_U(function_output_length,
4533 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4534 TEST_LE_U(function_output_length,
4535 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004536 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 PSA_ASSERT(psa_cipher_update(&operation1,
4539 input->x + first_part_size,
4540 input->len - first_part_size,
4541 output1, output1_buffer_size,
4542 &function_output_length));
4543 TEST_LE_U(function_output_length,
4544 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4545 alg,
4546 input->len - first_part_size));
4547 TEST_LE_U(function_output_length,
4548 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004549 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004550
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 PSA_ASSERT(psa_cipher_finish(&operation1,
4552 output1 + output1_length,
4553 output1_buffer_size - output1_length,
4554 &function_output_length));
4555 TEST_LE_U(function_output_length,
4556 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4557 TEST_LE_U(function_output_length,
4558 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004559 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004560
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004562
Gilles Peskine048b7f02018-06-08 14:20:49 +02004563 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 TEST_LE_U(output2_buffer_size,
4565 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4566 TEST_LE_U(output2_buffer_size,
4567 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4568 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 if (iv_length > 0) {
4571 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4572 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004573 }
Moran Pekerded84402018-06-06 16:36:50 +03004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4576 output2, output2_buffer_size,
4577 &function_output_length));
4578 TEST_LE_U(function_output_length,
4579 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4580 TEST_LE_U(function_output_length,
4581 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004582 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004583
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 PSA_ASSERT(psa_cipher_update(&operation2,
4585 output1 + first_part_size,
4586 output1_length - first_part_size,
4587 output2, output2_buffer_size,
4588 &function_output_length));
4589 TEST_LE_U(function_output_length,
4590 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4591 alg,
4592 output1_length - first_part_size));
4593 TEST_LE_U(function_output_length,
4594 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004595 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 PSA_ASSERT(psa_cipher_finish(&operation2,
4598 output2 + output2_length,
4599 output2_buffer_size - output2_length,
4600 &function_output_length));
4601 TEST_LE_U(function_output_length,
4602 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4603 TEST_LE_U(function_output_length,
4604 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004605 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004610
4611exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 psa_cipher_abort(&operation1);
4613 psa_cipher_abort(&operation2);
4614 mbedtls_free(output1);
4615 mbedtls_free(output2);
4616 psa_destroy_key(key);
4617 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004618}
4619/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004620
Gilles Peskine20035e32018-02-03 22:44:14 +01004621/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004622void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4623 int alg_arg,
4624 data_t *nonce,
4625 data_t *additional_data,
4626 data_t *input_data,
4627 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004628{
Ronald Cron5425a212020-08-04 14:58:35 +02004629 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004630 psa_key_type_t key_type = key_type_arg;
4631 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004632 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004633 unsigned char *output_data = NULL;
4634 size_t output_size = 0;
4635 size_t output_length = 0;
4636 unsigned char *output_data2 = NULL;
4637 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004638 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004639 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4645 psa_set_key_algorithm(&attributes, alg);
4646 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4649 &key));
4650 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4651 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4654 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004655 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4656 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4658 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4659 TEST_EQUAL(output_size,
4660 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4661 TEST_LE_U(output_size,
4662 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004663 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 status = psa_aead_encrypt(key, alg,
4667 nonce->x, nonce->len,
4668 additional_data->x,
4669 additional_data->len,
4670 input_data->x, input_data->len,
4671 output_data, output_size,
4672 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004673
4674 /* If the operation is not supported, just skip and not fail in case the
4675 * encryption involves a common limitation of cryptography hardwares and
4676 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 if (status == PSA_ERROR_NOT_SUPPORTED) {
4678 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4679 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004680 }
4681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 if (PSA_SUCCESS == expected_result) {
4685 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004686
Gilles Peskine003a4a92019-05-14 16:09:40 +02004687 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4688 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 TEST_EQUAL(input_data->len,
4690 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 TEST_LE_U(input_data->len,
4693 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 TEST_EQUAL(psa_aead_decrypt(key, alg,
4696 nonce->x, nonce->len,
4697 additional_data->x,
4698 additional_data->len,
4699 output_data, output_length,
4700 output_data2, output_length,
4701 &output_length2),
4702 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 ASSERT_COMPARE(input_data->x, input_data->len,
4705 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004706 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004707
Gilles Peskinea1cac842018-06-11 19:33:02 +02004708exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 psa_destroy_key(key);
4710 mbedtls_free(output_data);
4711 mbedtls_free(output_data2);
4712 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004713}
4714/* END_CASE */
4715
4716/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004717void aead_encrypt(int key_type_arg, data_t *key_data,
4718 int alg_arg,
4719 data_t *nonce,
4720 data_t *additional_data,
4721 data_t *input_data,
4722 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004723{
Ronald Cron5425a212020-08-04 14:58:35 +02004724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004725 psa_key_type_t key_type = key_type_arg;
4726 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004727 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004728 unsigned char *output_data = NULL;
4729 size_t output_size = 0;
4730 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004732 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4737 psa_set_key_algorithm(&attributes, alg);
4738 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4741 &key));
4742 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4743 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4746 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004747 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4748 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 TEST_EQUAL(output_size,
4750 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4751 TEST_LE_U(output_size,
4752 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4753 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 status = psa_aead_encrypt(key, alg,
4756 nonce->x, nonce->len,
4757 additional_data->x, additional_data->len,
4758 input_data->x, input_data->len,
4759 output_data, output_size,
4760 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004761
Ronald Cron28a45ed2021-02-09 20:35:42 +01004762 /* If the operation is not supported, just skip and not fail in case the
4763 * encryption involves a common limitation of cryptography hardwares and
4764 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (status == PSA_ERROR_NOT_SUPPORTED) {
4766 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4767 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004768 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 PSA_ASSERT(status);
4771 ASSERT_COMPARE(expected_result->x, expected_result->len,
4772 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004773
Gilles Peskinea1cac842018-06-11 19:33:02 +02004774exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 psa_destroy_key(key);
4776 mbedtls_free(output_data);
4777 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004778}
4779/* END_CASE */
4780
4781/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004782void aead_decrypt(int key_type_arg, data_t *key_data,
4783 int alg_arg,
4784 data_t *nonce,
4785 data_t *additional_data,
4786 data_t *input_data,
4787 data_t *expected_data,
4788 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004789{
Ronald Cron5425a212020-08-04 14:58:35 +02004790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004791 psa_key_type_t key_type = key_type_arg;
4792 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004793 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794 unsigned char *output_data = NULL;
4795 size_t output_size = 0;
4796 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004798 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004799 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4804 psa_set_key_algorithm(&attributes, alg);
4805 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4808 &key));
4809 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4810 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4813 alg);
4814 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4815 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004816 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4817 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 TEST_EQUAL(output_size,
4819 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4820 TEST_LE_U(output_size,
4821 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004822 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 status = psa_aead_decrypt(key, alg,
4826 nonce->x, nonce->len,
4827 additional_data->x,
4828 additional_data->len,
4829 input_data->x, input_data->len,
4830 output_data, output_size,
4831 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004832
Ronald Cron28a45ed2021-02-09 20:35:42 +01004833 /* If the operation is not supported, just skip and not fail in case the
4834 * decryption involves a common limitation of cryptography hardwares and
4835 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (status == PSA_ERROR_NOT_SUPPORTED) {
4837 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4838 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004839 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (expected_result == PSA_SUCCESS) {
4844 ASSERT_COMPARE(expected_data->x, expected_data->len,
4845 output_data, output_length);
4846 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004847
Gilles Peskinea1cac842018-06-11 19:33:02 +02004848exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 psa_destroy_key(key);
4850 mbedtls_free(output_data);
4851 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004852}
4853/* END_CASE */
4854
4855/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004856void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4857 int alg_arg,
4858 data_t *nonce,
4859 data_t *additional_data,
4860 data_t *input_data,
4861 int do_set_lengths,
4862 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004863{
Paul Elliottd3f82412021-06-16 16:52:21 +01004864 size_t ad_part_len = 0;
4865 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004866 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4869 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (do_set_lengths) {
4872 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004873 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004875 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004877 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004878
4879 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (!aead_multipart_internal_func(key_type_arg, key_data,
4881 alg_arg, nonce,
4882 additional_data,
4883 ad_part_len,
4884 input_data, -1,
4885 set_lengths_method,
4886 expected_output,
4887 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004888 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004889 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 /* length(0) part, length(ad_part_len) part, length(0) part... */
4892 mbedtls_test_set_step(1000 + ad_part_len);
4893
4894 if (!aead_multipart_internal_func(key_type_arg, key_data,
4895 alg_arg, nonce,
4896 additional_data,
4897 ad_part_len,
4898 input_data, -1,
4899 set_lengths_method,
4900 expected_output,
4901 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004902 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 }
4904 }
4905
4906 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4907 /* Split data into length(data_part_len) parts. */
4908 mbedtls_test_set_step(2000 + data_part_len);
4909
4910 if (do_set_lengths) {
4911 if (data_part_len & 0x01) {
4912 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4913 } else {
4914 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4915 }
4916 }
4917
4918 if (!aead_multipart_internal_func(key_type_arg, key_data,
4919 alg_arg, nonce,
4920 additional_data, -1,
4921 input_data, data_part_len,
4922 set_lengths_method,
4923 expected_output,
4924 1, 0)) {
4925 break;
4926 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004927
4928 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004930
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 if (!aead_multipart_internal_func(key_type_arg, key_data,
4932 alg_arg, nonce,
4933 additional_data, -1,
4934 input_data, data_part_len,
4935 set_lengths_method,
4936 expected_output,
4937 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004938 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004940 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004941
Paul Elliott8fc45162021-06-23 16:06:01 +01004942 /* Goto is required to silence warnings about unused labels, as we
4943 * don't actually do any test assertions in this function. */
4944 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004945}
4946/* END_CASE */
4947
4948/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004949void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
4950 int alg_arg,
4951 data_t *nonce,
4952 data_t *additional_data,
4953 data_t *input_data,
4954 int do_set_lengths,
4955 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004956{
Paul Elliottd3f82412021-06-16 16:52:21 +01004957 size_t ad_part_len = 0;
4958 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004959 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004962 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 if (do_set_lengths) {
4966 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004967 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004969 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004971 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 if (!aead_multipart_internal_func(key_type_arg, key_data,
4974 alg_arg, nonce,
4975 additional_data,
4976 ad_part_len,
4977 input_data, -1,
4978 set_lengths_method,
4979 expected_output,
4980 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004981 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004983
4984 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 if (!aead_multipart_internal_func(key_type_arg, key_data,
4988 alg_arg, nonce,
4989 additional_data,
4990 ad_part_len,
4991 input_data, -1,
4992 set_lengths_method,
4993 expected_output,
4994 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004995 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004997 }
4998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005000 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 if (do_set_lengths) {
5004 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005005 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005007 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005009 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005010
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 if (!aead_multipart_internal_func(key_type_arg, key_data,
5012 alg_arg, nonce,
5013 additional_data, -1,
5014 input_data, data_part_len,
5015 set_lengths_method,
5016 expected_output,
5017 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005018 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005020
5021 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 if (!aead_multipart_internal_func(key_type_arg, key_data,
5025 alg_arg, nonce,
5026 additional_data, -1,
5027 input_data, data_part_len,
5028 set_lengths_method,
5029 expected_output,
5030 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005033 }
5034
Paul Elliott8fc45162021-06-23 16:06:01 +01005035 /* Goto is required to silence warnings about unused labels, as we
5036 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005037 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005038}
5039/* END_CASE */
5040
5041/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005042void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5043 int alg_arg,
5044 int nonce_length,
5045 int expected_nonce_length_arg,
5046 data_t *additional_data,
5047 data_t *input_data,
5048 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005049{
5050
5051 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5052 psa_key_type_t key_type = key_type_arg;
5053 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005054 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005055 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5057 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005058 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005059 size_t actual_nonce_length = 0;
5060 size_t expected_nonce_length = expected_nonce_length_arg;
5061 unsigned char *output = NULL;
5062 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005063 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005064 size_t ciphertext_size = 0;
5065 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005066 size_t tag_length = 0;
5067 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5072 psa_set_key_algorithm(&attributes, alg);
5073 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5076 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005081
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005085
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005089
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005091
5092 /* If the operation is not supported, just skip and not fail in case the
5093 * encryption involves a common limitation of cryptography hardwares and
5094 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 if (status == PSA_ERROR_NOT_SUPPORTED) {
5096 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5097 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005098 }
5099
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5103 nonce_length,
5104 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 if (expected_status == PSA_SUCCESS) {
5111 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5112 alg));
5113 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005118 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5120 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5123 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5126 output, output_size,
5127 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5130 &ciphertext_length, tag_buffer,
5131 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005132 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005133
5134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 psa_destroy_key(key);
5136 mbedtls_free(output);
5137 mbedtls_free(ciphertext);
5138 psa_aead_abort(&operation);
5139 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005140}
5141/* END_CASE */
5142
5143/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005144void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5145 int alg_arg,
5146 int nonce_length_arg,
5147 int set_lengths_method_arg,
5148 data_t *additional_data,
5149 data_t *input_data,
5150 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005151{
5152
5153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5154 psa_key_type_t key_type = key_type_arg;
5155 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005156 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005157 uint8_t *nonce_buffer = NULL;
5158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5159 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5160 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005161 unsigned char *output = NULL;
5162 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005163 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005164 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005165 size_t ciphertext_size = 0;
5166 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005167 size_t tag_length = 0;
5168 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005169 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005170 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005173
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5175 psa_set_key_algorithm(&attributes, alg);
5176 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5179 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005190
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005194
5195 /* If the operation is not supported, just skip and not fail in case the
5196 * encryption involves a common limitation of cryptography hardwares and
5197 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 if (status == PSA_ERROR_NOT_SUPPORTED) {
5199 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5200 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005201 }
5202
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005204
Paul Elliott4023ffd2021-09-10 16:21:22 +01005205 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (nonce_length_arg == -1) {
5207 /* Arbitrary size buffer, to test zero length valid buffer. */
5208 ASSERT_ALLOC(nonce_buffer, 4);
5209 nonce_length = 0;
5210 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005211 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 nonce_length = (size_t) nonce_length_arg;
5213 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005214
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 if (nonce_buffer) {
5216 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005217 nonce_buffer[index] = 'a' + index;
5218 }
Paul Elliott66696b52021-08-16 18:42:41 +01005219 }
Paul Elliott863864a2021-07-23 17:28:31 +01005220 }
5221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5223 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5224 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005225 }
5226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 if (expected_status == PSA_SUCCESS) {
5232 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5233 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5234 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005235 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005237 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 }
Paul Elliott863864a2021-07-23 17:28:31 +01005239
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005240 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5242 additional_data->len),
5243 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005244
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5246 output, output_size,
5247 &ciphertext_length),
5248 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5251 &ciphertext_length, tag_buffer,
5252 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5253 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005254 }
5255
5256exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 psa_destroy_key(key);
5258 mbedtls_free(output);
5259 mbedtls_free(ciphertext);
5260 mbedtls_free(nonce_buffer);
5261 psa_aead_abort(&operation);
5262 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005263}
5264/* END_CASE */
5265
5266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005267void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005268 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005269 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005270 data_t *nonce,
5271 data_t *additional_data,
5272 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005274{
5275
5276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5277 psa_key_type_t key_type = key_type_arg;
5278 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005279 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5281 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5282 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005283 unsigned char *output = NULL;
5284 unsigned char *ciphertext = NULL;
5285 size_t output_size = output_size_arg;
5286 size_t ciphertext_size = 0;
5287 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005288 size_t tag_length = 0;
5289 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005292
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5294 psa_set_key_algorithm(&attributes, alg);
5295 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5298 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005301
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005305
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005307
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005309
5310 /* If the operation is not supported, just skip and not fail in case the
5311 * encryption involves a common limitation of cryptography hardwares and
5312 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 if (status == PSA_ERROR_NOT_SUPPORTED) {
5314 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5315 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005316 }
5317
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5321 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5326 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 status = psa_aead_update(&operation, input_data->x, input_data->len,
5329 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005332
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005334 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5336 &ciphertext_length, tag_buffer,
5337 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005338 }
5339
5340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 psa_destroy_key(key);
5342 mbedtls_free(output);
5343 mbedtls_free(ciphertext);
5344 psa_aead_abort(&operation);
5345 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005346}
5347/* END_CASE */
5348
Paul Elliott91b021e2021-07-23 18:52:31 +01005349/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005350void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5351 int alg_arg,
5352 int finish_ciphertext_size_arg,
5353 int tag_size_arg,
5354 data_t *nonce,
5355 data_t *additional_data,
5356 data_t *input_data,
5357 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005358{
5359
5360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5361 psa_key_type_t key_type = key_type_arg;
5362 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005363 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5365 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5366 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005367 unsigned char *ciphertext = NULL;
5368 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005369 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005370 size_t ciphertext_size = 0;
5371 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5373 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005374 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5379 psa_set_key_algorithm(&attributes, alg);
5380 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005381
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5383 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005396
5397 /* If the operation is not supported, just skip and not fail in case the
5398 * encryption involves a common limitation of cryptography hardwares and
5399 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 if (status == PSA_ERROR_NOT_SUPPORTED) {
5401 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5402 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005403 }
5404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005408
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5410 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5413 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5416 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005417
5418 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 status = psa_aead_finish(&operation, finish_ciphertext,
5420 finish_ciphertext_size,
5421 &ciphertext_length, tag_buffer,
5422 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005425
5426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 psa_destroy_key(key);
5428 mbedtls_free(ciphertext);
5429 mbedtls_free(finish_ciphertext);
5430 mbedtls_free(tag_buffer);
5431 psa_aead_abort(&operation);
5432 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005433}
5434/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005435
5436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005437void aead_multipart_verify(int key_type_arg, data_t *key_data,
5438 int alg_arg,
5439 data_t *nonce,
5440 data_t *additional_data,
5441 data_t *input_data,
5442 data_t *tag,
5443 int tag_usage_arg,
5444 int expected_setup_status_arg,
5445 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005446{
5447 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5448 psa_key_type_t key_type = key_type_arg;
5449 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005450 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5452 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5453 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005454 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005455 unsigned char *plaintext = NULL;
5456 unsigned char *finish_plaintext = NULL;
5457 size_t plaintext_size = 0;
5458 size_t plaintext_length = 0;
5459 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005460 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005461 unsigned char *tag_buffer = NULL;
5462 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5467 psa_set_key_algorithm(&attributes, alg);
5468 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5471 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5476 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005479
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005485
5486 /* If the operation is not supported, just skip and not fail in case the
5487 * encryption involves a common limitation of cryptography hardwares and
5488 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 if (status == PSA_ERROR_NOT_SUPPORTED) {
5490 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5491 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005492 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005494
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 }
Paul Elliott9961a662021-09-17 19:19:02 +01005498
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005500
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 status = psa_aead_set_lengths(&operation, additional_data->len,
5504 input_data->len);
5505 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5508 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5511 input_data->len,
5512 plaintext, plaintext_size,
5513 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005516 tag_buffer = tag->x;
5517 tag_size = tag->len;
5518 }
5519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 status = psa_aead_verify(&operation, finish_plaintext,
5521 verify_plaintext_size,
5522 &plaintext_length,
5523 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005526
5527exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 psa_destroy_key(key);
5529 mbedtls_free(plaintext);
5530 mbedtls_free(finish_plaintext);
5531 psa_aead_abort(&operation);
5532 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005533}
5534/* END_CASE */
5535
Paul Elliott9961a662021-09-17 19:19:02 +01005536/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005537void aead_multipart_setup(int key_type_arg, data_t *key_data,
5538 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005539{
5540 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5541 psa_key_type_t key_type = key_type_arg;
5542 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005543 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5545 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5546 psa_status_t expected_status = expected_status_arg;
5547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 psa_set_key_usage_flags(&attributes,
5551 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5552 psa_set_key_algorithm(&attributes, alg);
5553 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005554
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5556 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005567
5568exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 psa_destroy_key(key);
5570 psa_aead_abort(&operation);
5571 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005572}
5573/* END_CASE */
5574
5575/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005576void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5577 int alg_arg,
5578 data_t *nonce,
5579 data_t *additional_data,
5580 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005581{
5582 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5583 psa_key_type_t key_type = key_type_arg;
5584 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005585 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005586 unsigned char *output_data = NULL;
5587 unsigned char *final_data = NULL;
5588 size_t output_size = 0;
5589 size_t finish_output_size = 0;
5590 size_t output_length = 0;
5591 size_t key_bits = 0;
5592 size_t tag_length = 0;
5593 size_t tag_size = 0;
5594 size_t nonce_length = 0;
5595 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5596 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5597 size_t output_part_length = 0;
5598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5599
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 psa_set_key_usage_flags(&attributes,
5603 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5604 psa_set_key_algorithm(&attributes, alg);
5605 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5608 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005609
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5611 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005618
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005622
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005626
5627 /* Test all operations error without calling setup first. */
5628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5630 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5635 PSA_AEAD_NONCE_MAX_SIZE,
5636 &nonce_length),
5637 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005640
Paul Elliott481be342021-07-16 17:38:47 +01005641 /* ------------------------------------------------------- */
5642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5644 input_data->len),
5645 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005648
Paul Elliott481be342021-07-16 17:38:47 +01005649 /* ------------------------------------------------------- */
5650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5652 additional_data->len),
5653 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005656
Paul Elliott481be342021-07-16 17:38:47 +01005657 /* ------------------------------------------------------- */
5658
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5660 input_data->len, output_data,
5661 output_size, &output_length),
5662 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005665
Paul Elliott481be342021-07-16 17:38:47 +01005666 /* ------------------------------------------------------- */
5667
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5669 finish_output_size,
5670 &output_part_length,
5671 tag_buffer, tag_length,
5672 &tag_size),
5673 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005674
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005676
Paul Elliott481be342021-07-16 17:38:47 +01005677 /* ------------------------------------------------------- */
5678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5680 finish_output_size,
5681 &output_part_length,
5682 tag_buffer,
5683 tag_length),
5684 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
5688 /* Test for double setups. */
5689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5693 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
Paul Elliott481be342021-07-16 17:38:47 +01005697 /* ------------------------------------------------------- */
5698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5702 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005705
Paul Elliott374a2be2021-07-16 17:53:40 +01005706 /* ------------------------------------------------------- */
5707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5711 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005714
5715 /* ------------------------------------------------------- */
5716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5720 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005723
Paul Elliottc23a9a02021-06-21 18:32:46 +01005724 /* Test for not setting a nonce. */
5725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5729 additional_data->len),
5730 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005733
Paul Elliott7f628422021-09-01 12:08:29 +01005734 /* ------------------------------------------------------- */
5735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5739 input_data->len, output_data,
5740 output_size, &output_length),
5741 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005744
Paul Elliottbdc2c682021-09-21 18:37:10 +01005745 /* ------------------------------------------------------- */
5746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5750 finish_output_size,
5751 &output_part_length,
5752 tag_buffer, tag_length,
5753 &tag_size),
5754 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005757
5758 /* ------------------------------------------------------- */
5759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5763 finish_output_size,
5764 &output_part_length,
5765 tag_buffer,
5766 tag_length),
5767 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005770
Paul Elliottc23a9a02021-06-21 18:32:46 +01005771 /* Test for double setting nonce. */
5772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5778 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005781
Paul Elliott374a2be2021-07-16 17:53:40 +01005782 /* Test for double generating nonce. */
5783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5787 PSA_AEAD_NONCE_MAX_SIZE,
5788 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5791 PSA_AEAD_NONCE_MAX_SIZE,
5792 &nonce_length),
5793 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005794
5795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005797
5798 /* Test for generate nonce then set and vice versa */
5799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5803 PSA_AEAD_NONCE_MAX_SIZE,
5804 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5807 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005810
Andrzej Kurekad837522021-12-15 15:28:49 +01005811 /* Test for generating nonce after calling set lengths */
5812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5816 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5819 PSA_AEAD_NONCE_MAX_SIZE,
5820 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005823
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005824 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 if (operation.alg == PSA_ALG_CCM) {
5829 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5830 input_data->len),
5831 PSA_ERROR_INVALID_ARGUMENT);
5832 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5833 PSA_AEAD_NONCE_MAX_SIZE,
5834 &nonce_length),
5835 PSA_ERROR_BAD_STATE);
5836 } else {
5837 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5838 input_data->len));
5839 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5840 PSA_AEAD_NONCE_MAX_SIZE,
5841 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005842 }
5843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005845
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005846 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005847#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5851 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5852 input_data->len),
5853 PSA_ERROR_INVALID_ARGUMENT);
5854 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5855 PSA_AEAD_NONCE_MAX_SIZE,
5856 &nonce_length),
5857 PSA_ERROR_BAD_STATE);
5858 } else {
5859 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5860 input_data->len));
5861 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5862 PSA_AEAD_NONCE_MAX_SIZE,
5863 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005864 }
5865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005867#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005868
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005869 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5874 PSA_AEAD_NONCE_MAX_SIZE,
5875 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 if (operation.alg == PSA_ALG_CCM) {
5878 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5879 input_data->len),
5880 PSA_ERROR_INVALID_ARGUMENT);
5881 } else {
5882 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5883 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005884 }
5885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005887
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005888 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005889 /* Test for setting nonce after calling set lengths */
5890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5894 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005899
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005900 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005901
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005903
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 if (operation.alg == PSA_ALG_CCM) {
5905 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5906 input_data->len),
5907 PSA_ERROR_INVALID_ARGUMENT);
5908 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5909 PSA_ERROR_BAD_STATE);
5910 } else {
5911 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5912 input_data->len));
5913 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005914 }
5915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005917
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005918 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005919#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5923 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5924 input_data->len),
5925 PSA_ERROR_INVALID_ARGUMENT);
5926 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5927 PSA_ERROR_BAD_STATE);
5928 } else {
5929 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5930 input_data->len));
5931 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005932 }
5933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005935#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005936
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005937 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005938
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 if (operation.alg == PSA_ALG_CCM) {
5944 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5945 input_data->len),
5946 PSA_ERROR_INVALID_ARGUMENT);
5947 } else {
5948 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5949 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005950 }
5951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005953
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005954 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00005955#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 if (operation.alg == PSA_ALG_GCM) {
5959 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5960 SIZE_MAX),
5961 PSA_ERROR_INVALID_ARGUMENT);
5962 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5963 PSA_ERROR_BAD_STATE);
5964 } else if (operation.alg != PSA_ALG_CCM) {
5965 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5966 SIZE_MAX));
5967 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005968 }
5969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00005971#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005972
Tom Cosgrove1797b052022-12-04 17:19:59 +00005973 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00005974#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 if (operation.alg == PSA_ALG_GCM) {
5980 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5981 SIZE_MAX),
5982 PSA_ERROR_INVALID_ARGUMENT);
5983 } else if (operation.alg != PSA_ALG_CCM) {
5984 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5985 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005986 }
5987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00005989#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005990
5991 /* ------------------------------------------------------- */
5992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5998 PSA_AEAD_NONCE_MAX_SIZE,
5999 &nonce_length),
6000 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006003
Paul Elliott7220cae2021-06-22 17:25:57 +01006004 /* Test for generating nonce in decrypt setup. */
6005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6009 PSA_AEAD_NONCE_MAX_SIZE,
6010 &nonce_length),
6011 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006014
Paul Elliottc23a9a02021-06-21 18:32:46 +01006015 /* Test for setting lengths twice. */
6016
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6022 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6025 input_data->len),
6026 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006029
Andrzej Kurekad837522021-12-15 15:28:49 +01006030 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006031
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6039 additional_data->len),
6040 PSA_ERROR_BAD_STATE);
6041 } else {
6042 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6043 additional_data->len));
6044
6045 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6046 input_data->len),
6047 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006048 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006050
6051 /* ------------------------------------------------------- */
6052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 if (operation.alg == PSA_ALG_CCM) {
6058 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6059 input_data->len, output_data,
6060 output_size, &output_length),
6061 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 } else {
6064 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6065 input_data->len, output_data,
6066 output_size, &output_length));
6067
6068 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6069 input_data->len),
6070 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006071 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006073
6074 /* ------------------------------------------------------- */
6075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (operation.alg == PSA_ALG_CCM) {
6081 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6082 finish_output_size,
6083 &output_part_length,
6084 tag_buffer, tag_length,
6085 &tag_size));
6086 } else {
6087 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6088 finish_output_size,
6089 &output_part_length,
6090 tag_buffer, tag_length,
6091 &tag_size));
6092
6093 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6094 input_data->len),
6095 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006096 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006098
6099 /* Test for setting lengths after generating nonce + already starting data. */
6100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6104 PSA_AEAD_NONCE_MAX_SIZE,
6105 &nonce_length));
6106 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6109 additional_data->len),
6110 PSA_ERROR_BAD_STATE);
6111 } else {
6112 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6113 additional_data->len));
6114
6115 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6116 input_data->len),
6117 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006118 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006120
6121 /* ------------------------------------------------------- */
6122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006124
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6126 PSA_AEAD_NONCE_MAX_SIZE,
6127 &nonce_length));
6128 if (operation.alg == PSA_ALG_CCM) {
6129 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6130 input_data->len, output_data,
6131 output_size, &output_length),
6132 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 } else {
6135 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6136 input_data->len, output_data,
6137 output_size, &output_length));
6138
6139 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6140 input_data->len),
6141 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006142 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006144
6145 /* ------------------------------------------------------- */
6146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6150 PSA_AEAD_NONCE_MAX_SIZE,
6151 &nonce_length));
6152 if (operation.alg == PSA_ALG_CCM) {
6153 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6154 finish_output_size,
6155 &output_part_length,
6156 tag_buffer, tag_length,
6157 &tag_size));
6158 } else {
6159 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6160 finish_output_size,
6161 &output_part_length,
6162 tag_buffer, tag_length,
6163 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6166 input_data->len),
6167 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006168 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006170
Paul Elliott243080c2021-07-21 19:01:17 +01006171 /* Test for not sending any additional data or data after setting non zero
6172 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6179 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6182 finish_output_size,
6183 &output_part_length,
6184 tag_buffer, tag_length,
6185 &tag_size),
6186 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006189
Paul Elliott243080c2021-07-21 19:01:17 +01006190 /* Test for not sending any additional data or data after setting non-zero
6191 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6198 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6201 finish_output_size,
6202 &output_part_length,
6203 tag_buffer,
6204 tag_length),
6205 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006208
Paul Elliott243080c2021-07-21 19:01:17 +01006209 /* Test for not sending any additional data after setting a non-zero length
6210 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006213
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6217 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6220 input_data->len, output_data,
6221 output_size, &output_length),
6222 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006225
Paul Elliottf94bd992021-09-19 18:15:59 +01006226 /* Test for not sending any data after setting a non-zero length for it.*/
6227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6233 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6236 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6239 finish_output_size,
6240 &output_part_length,
6241 tag_buffer, tag_length,
6242 &tag_size),
6243 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006244
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006246
Paul Elliottb0450fe2021-09-01 15:06:26 +01006247 /* Test for sending too much additional data after setting lengths. */
6248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006254
6255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6257 additional_data->len),
6258 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006261
Paul Elliotta2a09b02021-09-22 14:56:40 +01006262 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006267
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6269 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006270
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6272 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6275 1),
6276 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006279
Paul Elliottb0450fe2021-09-01 15:06:26 +01006280 /* Test for sending too much data after setting lengths. */
6281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +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 Elliottb0450fe2021-09-01 15:06:26 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006294
Paul Elliotta2a09b02021-09-22 14:56:40 +01006295 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6302 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6305 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6308 input_data->len, output_data,
6309 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6312 1, output_data,
6313 output_size, &output_length),
6314 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006317
Paul Elliottc23a9a02021-06-21 18:32:46 +01006318 /* Test sending additional data after data. */
6319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 if (operation.alg != PSA_ALG_CCM) {
6325 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6326 input_data->len, output_data,
6327 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6330 additional_data->len),
6331 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006332 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006334
Paul Elliott534d0b42021-06-22 19:15:20 +01006335 /* Test calling finish on decryption. */
6336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6342 finish_output_size,
6343 &output_part_length,
6344 tag_buffer, tag_length,
6345 &tag_size),
6346 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006349
6350 /* Test calling verify on encryption. */
6351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6357 finish_output_size,
6358 &output_part_length,
6359 tag_buffer,
6360 tag_length),
6361 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006364
6365
Paul Elliottc23a9a02021-06-21 18:32:46 +01006366exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 psa_destroy_key(key);
6368 psa_aead_abort(&operation);
6369 mbedtls_free(output_data);
6370 mbedtls_free(final_data);
6371 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006372}
6373/* END_CASE */
6374
6375/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006376void signature_size(int type_arg,
6377 int bits,
6378 int alg_arg,
6379 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006380{
6381 psa_key_type_t type = type_arg;
6382 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006386
Gilles Peskinee59236f2018-01-27 23:32:46 +01006387exit:
6388 ;
6389}
6390/* END_CASE */
6391
6392/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006393void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6394 int alg_arg, data_t *input_data,
6395 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006396{
Ronald Cron5425a212020-08-04 14:58:35 +02006397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006398 psa_key_type_t key_type = key_type_arg;
6399 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006400 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006401 unsigned char *signature = NULL;
6402 size_t signature_size;
6403 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006404 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6409 psa_set_key_algorithm(&attributes, alg);
6410 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6413 &key));
6414 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6415 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006416
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006417 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006418 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6420 key_bits, alg);
6421 TEST_ASSERT(signature_size != 0);
6422 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6423 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006424
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006425 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 PSA_ASSERT(psa_sign_hash(key, alg,
6427 input_data->x, input_data->len,
6428 signature, signature_size,
6429 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006430 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 ASSERT_COMPARE(output_data->x, output_data->len,
6432 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006433
6434exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006435 /*
6436 * Key attributes may have been returned by psa_get_key_attributes()
6437 * thus reset them as required.
6438 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 psa_destroy_key(key);
6442 mbedtls_free(signature);
6443 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006444}
6445/* END_CASE */
6446
Paul Elliott712d5122022-12-07 14:03:10 +00006447/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6448void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6449 int alg_arg, data_t *input_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006450 data_t *output_data, int max_ops,
6451 int min_completes, int max_completes)
Paul Elliott712d5122022-12-07 14:03:10 +00006452{
6453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6454 psa_key_type_t key_type = key_type_arg;
6455 psa_algorithm_t alg = alg_arg;
6456 size_t key_bits;
6457 unsigned char *signature = NULL;
6458 size_t signature_size;
6459 size_t signature_length = 0xdeadbeef;
6460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6461 psa_status_t status = PSA_OPERATION_INCOMPLETE;
6462 size_t num_ops = 0;
6463 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006464 size_t num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006465 psa_sign_hash_interruptible_operation_t operation =
6466 psa_sign_hash_interruptible_operation_init();
6467
6468 PSA_ASSERT(psa_crypto_init());
6469
6470 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6471 psa_set_key_algorithm(&attributes, alg);
6472 psa_set_key_type(&attributes, key_type);
6473
6474 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6475 &key));
6476 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6477 key_bits = psa_get_key_bits(&attributes);
6478
6479 /* Allocate a buffer which has the size advertised by the
6480 * library. */
6481 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6482 key_bits, alg);
6483 TEST_ASSERT(signature_size != 0);
6484 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6485 ASSERT_ALLOC(signature, signature_size);
6486
Paul Elliott0c683352022-12-16 19:16:56 +00006487 psa_interruptible_set_max_ops(max_ops);
6488
Paul Elliott712d5122022-12-07 14:03:10 +00006489 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6490 TEST_ASSERT(num_ops_prior == 0);
6491
6492 /* Start performing the signature. */
6493 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6494 input_data->x, input_data->len));
6495
6496 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6497 TEST_ASSERT(num_ops_prior == 0);
6498
6499 /* Continue performing the signature until complete. */
6500 while (status == PSA_OPERATION_INCOMPLETE) {
6501 status = psa_sign_hash_complete(&operation, signature, signature_size,
6502 &signature_length);
6503
Paul Elliott0c683352022-12-16 19:16:56 +00006504 num_completes++;
6505
Paul Elliott712d5122022-12-07 14:03:10 +00006506 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6507 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott712d5122022-12-07 14:03:10 +00006508 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006509
Paul Elliott712d5122022-12-07 14:03:10 +00006510 num_ops_prior = num_ops;
6511 }
6512 }
6513
6514 TEST_ASSERT(status == PSA_SUCCESS);
6515
Paul Elliott0c683352022-12-16 19:16:56 +00006516 TEST_LE_U(min_completes, num_completes);
6517 TEST_LE_U(num_completes, max_completes);
6518
Paul Elliott712d5122022-12-07 14:03:10 +00006519 /* Verify that the signature is what is expected. */
6520 ASSERT_COMPARE(output_data->x, output_data->len,
6521 signature, signature_length);
6522
6523 PSA_ASSERT(psa_sign_hash_abort(&operation));
6524
6525exit:
6526
6527 /*
6528 * Key attributes may have been returned by psa_get_key_attributes()
6529 * thus reset them as required.
6530 */
6531 psa_reset_key_attributes(&attributes);
6532
6533 psa_destroy_key(key);
6534 mbedtls_free(signature);
6535 PSA_DONE();
6536}
6537/* END_CASE */
6538
Gilles Peskine20035e32018-02-03 22:44:14 +01006539/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006540void sign_hash_fail(int key_type_arg, data_t *key_data,
6541 int alg_arg, data_t *input_data,
6542 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006543{
Ronald Cron5425a212020-08-04 14:58:35 +02006544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006545 psa_key_type_t key_type = key_type_arg;
6546 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006547 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006548 psa_status_t actual_status;
6549 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006550 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006551 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006552 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006553
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006555
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006557
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6559 psa_set_key_algorithm(&attributes, alg);
6560 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006561
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6563 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006564
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 actual_status = psa_sign_hash(key, alg,
6566 input_data->x, input_data->len,
6567 signature, signature_size,
6568 &signature_length);
6569 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006570 /* The value of *signature_length is unspecified on error, but
6571 * whatever it is, it should be less than signature_size, so that
6572 * if the caller tries to read *signature_length bytes without
6573 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006575
6576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 psa_reset_key_attributes(&attributes);
6578 psa_destroy_key(key);
6579 mbedtls_free(signature);
6580 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006581}
6582/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006583
Paul Elliott91007972022-12-16 12:21:24 +00006584/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6585void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6586 int alg_arg, data_t *input_data,
6587 int signature_size_arg,
6588 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006589 int expected_complete_status_arg,
6590 int max_ops, int min_completes,
6591 int max_completes)
Paul Elliott91007972022-12-16 12:21:24 +00006592{
6593 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6594 psa_key_type_t key_type = key_type_arg;
6595 psa_algorithm_t alg = alg_arg;
6596 size_t signature_size = signature_size_arg;
6597 psa_status_t actual_status;
6598 psa_status_t expected_start_status = expected_start_status_arg;
6599 psa_status_t expected_complete_status = expected_complete_status_arg;
6600 unsigned char *signature = NULL;
6601 size_t signature_length = 0xdeadbeef;
6602 size_t num_ops = 0;
6603 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006604 size_t num_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00006605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6606 psa_sign_hash_interruptible_operation_t operation =
6607 psa_sign_hash_interruptible_operation_init();
6608
6609 ASSERT_ALLOC(signature, signature_size);
6610
6611 PSA_ASSERT(psa_crypto_init());
6612
6613 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6614 psa_set_key_algorithm(&attributes, alg);
6615 psa_set_key_type(&attributes, key_type);
6616
6617 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6618 &key));
6619
Paul Elliott0c683352022-12-16 19:16:56 +00006620 psa_interruptible_set_max_ops(max_ops);
6621
Paul Elliott91007972022-12-16 12:21:24 +00006622 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6623 TEST_ASSERT(num_ops_prior == 0);
6624
6625 /* Start performing the signature. */
6626 actual_status = psa_sign_hash_start(&operation, key, alg,
6627 input_data->x, input_data->len);
6628
6629 TEST_EQUAL(actual_status, expected_start_status);
6630
6631 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6632 TEST_ASSERT(num_ops_prior == 0);
6633
6634 actual_status = PSA_OPERATION_INCOMPLETE;
6635
6636 /* Continue performing the signature until complete. */
6637 while (actual_status == PSA_OPERATION_INCOMPLETE) {
6638 actual_status = psa_sign_hash_complete(&operation, signature,
6639 signature_size,
6640 &signature_length);
6641
Paul Elliott0c683352022-12-16 19:16:56 +00006642 num_completes++;
6643
Paul Elliott91007972022-12-16 12:21:24 +00006644 /* If the psa_sign_hash_start() failed, psa_sign_hash_complete()
6645 * should also fail with bad state. */
6646 if (expected_start_status != PSA_SUCCESS) {
6647 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6648 } else if (actual_status != PSA_OPERATION_INCOMPLETE) {
6649 TEST_EQUAL(actual_status, expected_complete_status);
6650 } else {
6651 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott91007972022-12-16 12:21:24 +00006652 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006653
Paul Elliott91007972022-12-16 12:21:24 +00006654 num_ops_prior = num_ops;
6655 }
6656 }
6657
6658 PSA_ASSERT(psa_sign_hash_abort(&operation));
6659
6660 /* The value of *signature_length is unspecified on error, but
6661 * whatever it is, it should be less than signature_size, so that
6662 * if the caller tries to read *signature_length bytes without
6663 * checking the error code then they don't overflow a buffer. */
6664 TEST_LE_U(signature_length, signature_size);
6665
Paul Elliott0c683352022-12-16 19:16:56 +00006666 TEST_LE_U(min_completes, num_completes);
6667 TEST_LE_U(num_completes, max_completes);
6668
Paul Elliott91007972022-12-16 12:21:24 +00006669exit:
6670 psa_reset_key_attributes(&attributes);
6671 psa_destroy_key(key);
6672 mbedtls_free(signature);
6673 PSA_DONE();
6674}
6675/* END_CASE */
6676
mohammad16038cc1cee2018-03-28 01:21:33 +03006677/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006678void sign_verify_hash(int key_type_arg, data_t *key_data,
6679 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006680{
Ronald Cron5425a212020-08-04 14:58:35 +02006681 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006682 psa_key_type_t key_type = key_type_arg;
6683 psa_algorithm_t alg = alg_arg;
6684 size_t key_bits;
6685 unsigned char *signature = NULL;
6686 size_t signature_size;
6687 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006689
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006691
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6693 psa_set_key_algorithm(&attributes, alg);
6694 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006695
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6697 &key));
6698 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6699 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006700
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006701 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006702 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6704 key_bits, alg);
6705 TEST_ASSERT(signature_size != 0);
6706 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6707 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006708
6709 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 PSA_ASSERT(psa_sign_hash(key, alg,
6711 input_data->x, input_data->len,
6712 signature, signature_size,
6713 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006714 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 TEST_LE_U(signature_length, signature_size);
6716 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006717
6718 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 PSA_ASSERT(psa_verify_hash(key, alg,
6720 input_data->x, input_data->len,
6721 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006722
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006724 /* Flip a bit in the input and verify that the signature is now
6725 * detected as invalid. Flip a bit at the beginning, not at the end,
6726 * because ECDSA may ignore the last few bits of the input. */
6727 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 TEST_EQUAL(psa_verify_hash(key, alg,
6729 input_data->x, input_data->len,
6730 signature, signature_length),
6731 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006732 }
6733
6734exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006735 /*
6736 * Key attributes may have been returned by psa_get_key_attributes()
6737 * thus reset them as required.
6738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006740
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 psa_destroy_key(key);
6742 mbedtls_free(signature);
6743 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006744}
6745/* END_CASE */
6746
Paul Elliott712d5122022-12-07 14:03:10 +00006747/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6748void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006749 int alg_arg, data_t *input_data,
6750 int max_ops, int min_completes,
6751 int max_completes)
Paul Elliott712d5122022-12-07 14:03:10 +00006752{
6753 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6754 psa_key_type_t key_type = key_type_arg;
6755 psa_algorithm_t alg = alg_arg;
6756 size_t key_bits;
6757 unsigned char *signature = NULL;
6758 size_t signature_size;
6759 size_t signature_length = 0xdeadbeef;
6760 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6761 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliott0c683352022-12-16 19:16:56 +00006762 size_t num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006763 psa_sign_hash_interruptible_operation_t sign_operation =
6764 psa_sign_hash_interruptible_operation_init();
6765 psa_verify_hash_interruptible_operation_t verify_operation =
6766 psa_verify_hash_interruptible_operation_init();
6767
6768 PSA_ASSERT(psa_crypto_init());
6769
Paul Elliott0c683352022-12-16 19:16:56 +00006770 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6771 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006772 psa_set_key_algorithm(&attributes, alg);
6773 psa_set_key_type(&attributes, key_type);
6774
6775 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6776 &key));
6777 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6778 key_bits = psa_get_key_bits(&attributes);
6779
6780 /* Allocate a buffer which has the size advertised by the
6781 * library. */
6782 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6783 key_bits, alg);
6784 TEST_ASSERT(signature_size != 0);
6785 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6786 ASSERT_ALLOC(signature, signature_size);
6787
Paul Elliott0c683352022-12-16 19:16:56 +00006788 psa_interruptible_set_max_ops(max_ops);
6789
Paul Elliott712d5122022-12-07 14:03:10 +00006790 /* Start performing the signature. */
6791 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6792 input_data->x, input_data->len));
6793
6794 /* Continue performing the signature until complete. */
6795 while (status == PSA_OPERATION_INCOMPLETE) {
6796
Paul Elliott0c683352022-12-16 19:16:56 +00006797 status = psa_sign_hash_complete(&sign_operation, signature,
6798 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006799 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006800
6801 num_completes++;
Paul Elliott712d5122022-12-07 14:03:10 +00006802 }
6803
6804 TEST_ASSERT(status == PSA_SUCCESS);
6805
Paul Elliott0c683352022-12-16 19:16:56 +00006806 TEST_LE_U(min_completes, num_completes);
6807 TEST_LE_U(num_completes, max_completes);
6808
Paul Elliott712d5122022-12-07 14:03:10 +00006809 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6810
6811 /* Check that the signature length looks sensible. */
6812 TEST_LE_U(signature_length, signature_size);
6813 TEST_ASSERT(signature_length > 0);
6814
Paul Elliott0c683352022-12-16 19:16:56 +00006815 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006816 status = PSA_OPERATION_INCOMPLETE;
6817
6818 /* Start verification. */
6819 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6820 input_data->x, input_data->len,
6821 signature, signature_length));
6822
6823 /* Continue performing the signature until complete. */
6824 while (status == PSA_OPERATION_INCOMPLETE) {
6825 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00006826
6827 num_completes++;
Paul Elliott712d5122022-12-07 14:03:10 +00006828 }
6829
6830 TEST_ASSERT(status == PSA_SUCCESS);
6831
Paul Elliott0c683352022-12-16 19:16:56 +00006832 TEST_LE_U(min_completes, num_completes);
6833 TEST_LE_U(num_completes, max_completes);
6834
Paul Elliott712d5122022-12-07 14:03:10 +00006835 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6836
6837 verify_operation = psa_verify_hash_interruptible_operation_init();
6838
6839 if (input_data->len != 0) {
6840 /* Flip a bit in the input and verify that the signature is now
6841 * detected as invalid. Flip a bit at the beginning, not at the end,
6842 * because ECDSA may ignore the last few bits of the input. */
6843 input_data->x[0] ^= 1;
6844
6845 status = PSA_OPERATION_INCOMPLETE;
6846
6847 /* Start verification. */
6848 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6849 input_data->x, input_data->len,
6850 signature, signature_length));
6851
6852 /* Continue performing the signature until complete. */
6853 while (status == PSA_OPERATION_INCOMPLETE) {
6854 status = psa_verify_hash_complete(&verify_operation);
6855 }
6856
6857 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
6858 }
6859
6860 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6861
6862exit:
6863 /*
6864 * Key attributes may have been returned by psa_get_key_attributes()
6865 * thus reset them as required.
6866 */
6867 psa_reset_key_attributes(&attributes);
6868
6869 psa_destroy_key(key);
6870 mbedtls_free(signature);
6871 PSA_DONE();
6872}
6873/* END_CASE */
6874
Gilles Peskine9911b022018-06-29 17:30:48 +02006875/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006876void verify_hash(int key_type_arg, data_t *key_data,
6877 int alg_arg, data_t *hash_data,
6878 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03006879{
Ronald Cron5425a212020-08-04 14:58:35 +02006880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006881 psa_key_type_t key_type = key_type_arg;
6882 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006883 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02006886
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03006888
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6890 psa_set_key_algorithm(&attributes, alg);
6891 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03006892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6894 &key));
itayzafrir5c753392018-05-08 11:18:38 +03006895
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 PSA_ASSERT(psa_verify_hash(key, alg,
6897 hash_data->x, hash_data->len,
6898 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01006899
itayzafrir5c753392018-05-08 11:18:38 +03006900exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 psa_reset_key_attributes(&attributes);
6902 psa_destroy_key(key);
6903 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03006904}
6905/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006906
Paul Elliott712d5122022-12-07 14:03:10 +00006907/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6908void verify_hash_interruptible(int key_type_arg, data_t *key_data,
6909 int alg_arg, data_t *hash_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006910 data_t *signature_data, int max_ops,
6911 int min_completes, int max_completes)
Paul Elliott712d5122022-12-07 14:03:10 +00006912{
6913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6914 psa_key_type_t key_type = key_type_arg;
6915 psa_algorithm_t alg = alg_arg;
6916 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6917 psa_status_t status = PSA_OPERATION_INCOMPLETE;
6918 size_t num_ops = 0;
6919 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006920 size_t num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006921 psa_verify_hash_interruptible_operation_t operation =
6922 psa_verify_hash_interruptible_operation_init();
6923
6924 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
6925
6926 PSA_ASSERT(psa_crypto_init());
6927
6928 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6929 psa_set_key_algorithm(&attributes, alg);
6930 psa_set_key_type(&attributes, key_type);
6931
6932 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6933 &key));
6934
Paul Elliott0c683352022-12-16 19:16:56 +00006935 psa_interruptible_set_max_ops(max_ops);
6936
Paul Elliott712d5122022-12-07 14:03:10 +00006937 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
6938
6939 TEST_ASSERT(num_ops_prior == 0);
6940
6941 /* Start verification. */
6942 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
6943 hash_data->x, hash_data->len,
6944 signature_data->x, signature_data->len)
6945 );
6946
6947 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
6948
6949 TEST_ASSERT(num_ops_prior == 0);
6950
6951 /* Continue performing the signature until complete. */
6952 while (status == PSA_OPERATION_INCOMPLETE) {
6953 status = psa_verify_hash_complete(&operation);
6954
Paul Elliott0c683352022-12-16 19:16:56 +00006955 num_completes++;
6956
Paul Elliott712d5122022-12-07 14:03:10 +00006957 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6958 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott712d5122022-12-07 14:03:10 +00006959 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006960
Paul Elliott712d5122022-12-07 14:03:10 +00006961 num_ops_prior = num_ops;
6962 }
6963 }
6964
6965 TEST_ASSERT(status == PSA_SUCCESS);
6966
Paul Elliott0c683352022-12-16 19:16:56 +00006967 TEST_LE_U(min_completes, num_completes);
6968 TEST_LE_U(num_completes, max_completes);
6969
Paul Elliott712d5122022-12-07 14:03:10 +00006970 PSA_ASSERT(psa_verify_hash_abort(&operation));
6971
6972exit:
6973 psa_reset_key_attributes(&attributes);
6974 psa_destroy_key(key);
6975 PSA_DONE();
6976}
6977/* END_CASE */
6978
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006979/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006980void verify_hash_fail(int key_type_arg, data_t *key_data,
6981 int alg_arg, data_t *hash_data,
6982 data_t *signature_data,
6983 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006984{
Ronald Cron5425a212020-08-04 14:58:35 +02006985 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006986 psa_key_type_t key_type = key_type_arg;
6987 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006988 psa_status_t actual_status;
6989 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006991
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006993
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6995 psa_set_key_algorithm(&attributes, alg);
6996 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006997
Gilles Peskine449bd832023-01-11 14:50:10 +01006998 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6999 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007000
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 actual_status = psa_verify_hash(key, alg,
7002 hash_data->x, hash_data->len,
7003 signature_data->x, signature_data->len);
7004 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007005
7006exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 psa_reset_key_attributes(&attributes);
7008 psa_destroy_key(key);
7009 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007010}
7011/* END_CASE */
7012
Paul Elliott91007972022-12-16 12:21:24 +00007013/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7014void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7015 int alg_arg, data_t *hash_data,
7016 data_t *signature_data,
7017 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007018 int expected_complete_status_arg,
7019 int max_ops, int min_completes,
7020 int max_completes)
Paul Elliott91007972022-12-16 12:21:24 +00007021{
7022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7023 psa_key_type_t key_type = key_type_arg;
7024 psa_algorithm_t alg = alg_arg;
7025 psa_status_t actual_status;
7026 psa_status_t expected_start_status = expected_start_status_arg;
7027 psa_status_t expected_complete_status = expected_complete_status_arg;
7028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7029 size_t num_ops = 0;
7030 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007031 size_t num_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007032 psa_verify_hash_interruptible_operation_t operation =
7033 psa_verify_hash_interruptible_operation_init();
7034
7035 PSA_ASSERT(psa_crypto_init());
7036
7037 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7038 psa_set_key_algorithm(&attributes, alg);
7039 psa_set_key_type(&attributes, key_type);
7040
7041 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7042 &key));
7043
Paul Elliott0c683352022-12-16 19:16:56 +00007044 psa_interruptible_set_max_ops(max_ops);
7045
Paul Elliott91007972022-12-16 12:21:24 +00007046 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7047 TEST_ASSERT(num_ops_prior == 0);
7048
7049 /* Start verification. */
7050 actual_status = psa_verify_hash_start(&operation, key, alg,
7051 hash_data->x, hash_data->len,
7052 signature_data->x,
7053 signature_data->len);
7054
7055 TEST_EQUAL(actual_status, expected_start_status);
7056
7057 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7058 TEST_ASSERT(num_ops_prior == 0);
7059
7060 actual_status = PSA_OPERATION_INCOMPLETE;
7061
7062 /* Continue performing the signature until complete. */
7063 while (actual_status == PSA_OPERATION_INCOMPLETE) {
7064 actual_status = psa_verify_hash_complete(&operation);
7065
Paul Elliott0c683352022-12-16 19:16:56 +00007066 num_completes++;
7067
Paul Elliott91007972022-12-16 12:21:24 +00007068 /* If the psa_verify_hash_start() failed,
7069 * psa_verify_hash_complete() should also fail with bad state.*/
7070 if (expected_start_status != PSA_SUCCESS) {
7071 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7072 } else if (actual_status != PSA_OPERATION_INCOMPLETE) {
7073 TEST_EQUAL(actual_status, expected_complete_status);
7074 } else {
7075 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott91007972022-12-16 12:21:24 +00007076 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007077
Paul Elliott91007972022-12-16 12:21:24 +00007078 num_ops_prior = num_ops;
7079 }
7080 }
7081
Paul Elliott0c683352022-12-16 19:16:56 +00007082 TEST_LE_U(min_completes, num_completes);
7083 TEST_LE_U(num_completes, max_completes);
7084
Paul Elliott91007972022-12-16 12:21:24 +00007085 PSA_ASSERT(psa_verify_hash_abort(&operation));
7086
7087exit:
7088 psa_reset_key_attributes(&attributes);
7089 psa_destroy_key(key);
7090 PSA_DONE();
7091}
7092/* END_CASE */
7093
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007094/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007095void sign_message_deterministic(int key_type_arg,
7096 data_t *key_data,
7097 int alg_arg,
7098 data_t *input_data,
7099 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007100{
7101 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7102 psa_key_type_t key_type = key_type_arg;
7103 psa_algorithm_t alg = alg_arg;
7104 size_t key_bits;
7105 unsigned char *signature = NULL;
7106 size_t signature_size;
7107 size_t signature_length = 0xdeadbeef;
7108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7109
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007111
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7113 psa_set_key_algorithm(&attributes, alg);
7114 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007115
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7117 &key));
7118 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7119 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007120
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7122 TEST_ASSERT(signature_size != 0);
7123 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7124 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007125
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 PSA_ASSERT(psa_sign_message(key, alg,
7127 input_data->x, input_data->len,
7128 signature, signature_size,
7129 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007130
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 ASSERT_COMPARE(output_data->x, output_data->len,
7132 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007133
7134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007136
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 psa_destroy_key(key);
7138 mbedtls_free(signature);
7139 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007140
7141}
7142/* END_CASE */
7143
7144/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007145void sign_message_fail(int key_type_arg,
7146 data_t *key_data,
7147 int alg_arg,
7148 data_t *input_data,
7149 int signature_size_arg,
7150 int expected_status_arg)
7151{
7152 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7153 psa_key_type_t key_type = key_type_arg;
7154 psa_algorithm_t alg = alg_arg;
7155 size_t signature_size = signature_size_arg;
7156 psa_status_t actual_status;
7157 psa_status_t expected_status = expected_status_arg;
7158 unsigned char *signature = NULL;
7159 size_t signature_length = 0xdeadbeef;
7160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7161
7162 ASSERT_ALLOC(signature, signature_size);
7163
7164 PSA_ASSERT(psa_crypto_init());
7165
7166 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7167 psa_set_key_algorithm(&attributes, alg);
7168 psa_set_key_type(&attributes, key_type);
7169
7170 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7171 &key));
7172
7173 actual_status = psa_sign_message(key, alg,
7174 input_data->x, input_data->len,
7175 signature, signature_size,
7176 &signature_length);
7177 TEST_EQUAL(actual_status, expected_status);
7178 /* The value of *signature_length is unspecified on error, but
7179 * whatever it is, it should be less than signature_size, so that
7180 * if the caller tries to read *signature_length bytes without
7181 * checking the error code then they don't overflow a buffer. */
7182 TEST_LE_U(signature_length, signature_size);
7183
7184exit:
7185 psa_reset_key_attributes(&attributes);
7186 psa_destroy_key(key);
7187 mbedtls_free(signature);
7188 PSA_DONE();
7189}
7190/* END_CASE */
7191
7192/* BEGIN_CASE */
7193void sign_verify_message(int key_type_arg,
7194 data_t *key_data,
7195 int alg_arg,
7196 data_t *input_data)
7197{
7198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7199 psa_key_type_t key_type = key_type_arg;
7200 psa_algorithm_t alg = alg_arg;
7201 size_t key_bits;
7202 unsigned char *signature = NULL;
7203 size_t signature_size;
7204 size_t signature_length = 0xdeadbeef;
7205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7206
7207 PSA_ASSERT(psa_crypto_init());
7208
7209 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7210 PSA_KEY_USAGE_VERIFY_MESSAGE);
7211 psa_set_key_algorithm(&attributes, alg);
7212 psa_set_key_type(&attributes, key_type);
7213
7214 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7215 &key));
7216 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7217 key_bits = psa_get_key_bits(&attributes);
7218
7219 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7220 TEST_ASSERT(signature_size != 0);
7221 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7222 ASSERT_ALLOC(signature, signature_size);
7223
7224 PSA_ASSERT(psa_sign_message(key, alg,
7225 input_data->x, input_data->len,
7226 signature, signature_size,
7227 &signature_length));
7228 TEST_LE_U(signature_length, signature_size);
7229 TEST_ASSERT(signature_length > 0);
7230
7231 PSA_ASSERT(psa_verify_message(key, alg,
7232 input_data->x, input_data->len,
7233 signature, signature_length));
7234
7235 if (input_data->len != 0) {
7236 /* Flip a bit in the input and verify that the signature is now
7237 * detected as invalid. Flip a bit at the beginning, not at the end,
7238 * because ECDSA may ignore the last few bits of the input. */
7239 input_data->x[0] ^= 1;
7240 TEST_EQUAL(psa_verify_message(key, alg,
7241 input_data->x, input_data->len,
7242 signature, signature_length),
7243 PSA_ERROR_INVALID_SIGNATURE);
7244 }
7245
7246exit:
7247 psa_reset_key_attributes(&attributes);
7248
7249 psa_destroy_key(key);
7250 mbedtls_free(signature);
7251 PSA_DONE();
7252}
7253/* END_CASE */
7254
7255/* BEGIN_CASE */
7256void verify_message(int key_type_arg,
7257 data_t *key_data,
7258 int alg_arg,
7259 data_t *input_data,
7260 data_t *signature_data)
7261{
7262 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7263 psa_key_type_t key_type = key_type_arg;
7264 psa_algorithm_t alg = alg_arg;
7265 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7266
7267 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7268
7269 PSA_ASSERT(psa_crypto_init());
7270
7271 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7272 psa_set_key_algorithm(&attributes, alg);
7273 psa_set_key_type(&attributes, key_type);
7274
7275 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7276 &key));
7277
7278 PSA_ASSERT(psa_verify_message(key, alg,
7279 input_data->x, input_data->len,
7280 signature_data->x, signature_data->len));
7281
7282exit:
7283 psa_reset_key_attributes(&attributes);
7284 psa_destroy_key(key);
7285 PSA_DONE();
7286}
7287/* END_CASE */
7288
7289/* BEGIN_CASE */
7290void verify_message_fail(int key_type_arg,
7291 data_t *key_data,
7292 int alg_arg,
7293 data_t *hash_data,
7294 data_t *signature_data,
7295 int expected_status_arg)
7296{
7297 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7298 psa_key_type_t key_type = key_type_arg;
7299 psa_algorithm_t alg = alg_arg;
7300 psa_status_t actual_status;
7301 psa_status_t expected_status = expected_status_arg;
7302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7303
7304 PSA_ASSERT(psa_crypto_init());
7305
7306 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7307 psa_set_key_algorithm(&attributes, alg);
7308 psa_set_key_type(&attributes, key_type);
7309
7310 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7311 &key));
7312
7313 actual_status = psa_verify_message(key, alg,
7314 hash_data->x, hash_data->len,
7315 signature_data->x,
7316 signature_data->len);
7317 TEST_EQUAL(actual_status, expected_status);
7318
7319exit:
7320 psa_reset_key_attributes(&attributes);
7321 psa_destroy_key(key);
7322 PSA_DONE();
7323}
7324/* END_CASE */
7325
7326/* BEGIN_CASE */
7327void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02007328 data_t *key_data,
7329 int alg_arg,
7330 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 data_t *label,
7332 int expected_output_length_arg,
7333 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02007334{
Ronald Cron5425a212020-08-04 14:58:35 +02007335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007336 psa_key_type_t key_type = key_type_arg;
7337 psa_algorithm_t alg = alg_arg;
7338 size_t expected_output_length = expected_output_length_arg;
7339 size_t key_bits;
7340 unsigned char *output = NULL;
7341 size_t output_size;
7342 size_t output_length = ~0;
7343 psa_status_t actual_status;
7344 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007346
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007348
Gilles Peskine656896e2018-06-29 19:12:28 +02007349 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
7351 psa_set_key_algorithm(&attributes, alg);
7352 psa_set_key_type(&attributes, key_type);
7353 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7354 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02007355
7356 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7358 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007359
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7361 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7362 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02007363
7364 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 actual_status = psa_asymmetric_encrypt(key, alg,
7366 input_data->x, input_data->len,
7367 label->x, label->len,
7368 output, output_size,
7369 &output_length);
7370 TEST_EQUAL(actual_status, expected_status);
7371 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine656896e2018-06-29 19:12:28 +02007372
Gilles Peskine68428122018-06-30 18:42:41 +02007373 /* If the label is empty, the test framework puts a non-null pointer
7374 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007376 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 if (output_size != 0) {
7378 memset(output, 0, output_size);
7379 }
7380 actual_status = psa_asymmetric_encrypt(key, alg,
7381 input_data->x, input_data->len,
7382 NULL, label->len,
7383 output, output_size,
7384 &output_length);
7385 TEST_EQUAL(actual_status, expected_status);
7386 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007387 }
7388
Gilles Peskine656896e2018-06-29 19:12:28 +02007389exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007390 /*
7391 * Key attributes may have been returned by psa_get_key_attributes()
7392 * thus reset them as required.
7393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007395
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 psa_destroy_key(key);
7397 mbedtls_free(output);
7398 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02007399}
7400/* END_CASE */
7401
7402/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007403void asymmetric_encrypt_decrypt(int key_type_arg,
7404 data_t *key_data,
7405 int alg_arg,
7406 data_t *input_data,
7407 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007408{
Ronald Cron5425a212020-08-04 14:58:35 +02007409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007410 psa_key_type_t key_type = key_type_arg;
7411 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007412 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007413 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007414 size_t output_size;
7415 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007416 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007417 size_t output2_size;
7418 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007420
Gilles Peskine449bd832023-01-11 14:50:10 +01007421 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007422
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
7424 psa_set_key_algorithm(&attributes, alg);
7425 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007426
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7428 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007429
7430 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7432 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007433
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7435 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7436 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007437
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007438 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 TEST_LE_U(output2_size,
7440 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
7441 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7442 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007443
Gilles Peskineeebd7382018-06-08 18:11:54 +02007444 /* We test encryption by checking that encrypt-then-decrypt gives back
7445 * the original plaintext because of the non-optional random
7446 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
7448 input_data->x, input_data->len,
7449 label->x, label->len,
7450 output, output_size,
7451 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007452 /* We don't know what ciphertext length to expect, but check that
7453 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007455
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7457 output, output_length,
7458 label->x, label->len,
7459 output2, output2_size,
7460 &output2_length));
7461 ASSERT_COMPARE(input_data->x, input_data->len,
7462 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007463
7464exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007465 /*
7466 * Key attributes may have been returned by psa_get_key_attributes()
7467 * thus reset them as required.
7468 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007470
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 psa_destroy_key(key);
7472 mbedtls_free(output);
7473 mbedtls_free(output2);
7474 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007475}
7476/* END_CASE */
7477
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007478/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007479void asymmetric_decrypt(int key_type_arg,
7480 data_t *key_data,
7481 int alg_arg,
7482 data_t *input_data,
7483 data_t *label,
7484 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007485{
Ronald Cron5425a212020-08-04 14:58:35 +02007486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007487 psa_key_type_t key_type = key_type_arg;
7488 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007489 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007490 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007491 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007492 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007494
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007496
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7498 psa_set_key_algorithm(&attributes, alg);
7499 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007500
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7502 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007503
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7505 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007506
7507 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7509 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7510 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007511
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7513 input_data->x, input_data->len,
7514 label->x, label->len,
7515 output,
7516 output_size,
7517 &output_length));
7518 ASSERT_COMPARE(expected_data->x, expected_data->len,
7519 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007520
Gilles Peskine68428122018-06-30 18:42:41 +02007521 /* If the label is empty, the test framework puts a non-null pointer
7522 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007524 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 if (output_size != 0) {
7526 memset(output, 0, output_size);
7527 }
7528 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7529 input_data->x, input_data->len,
7530 NULL, label->len,
7531 output,
7532 output_size,
7533 &output_length));
7534 ASSERT_COMPARE(expected_data->x, expected_data->len,
7535 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007536 }
7537
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007538exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 psa_reset_key_attributes(&attributes);
7540 psa_destroy_key(key);
7541 mbedtls_free(output);
7542 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007543}
7544/* END_CASE */
7545
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007546/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007547void asymmetric_decrypt_fail(int key_type_arg,
7548 data_t *key_data,
7549 int alg_arg,
7550 data_t *input_data,
7551 data_t *label,
7552 int output_size_arg,
7553 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007554{
Ronald Cron5425a212020-08-04 14:58:35 +02007555 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007556 psa_key_type_t key_type = key_type_arg;
7557 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007558 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007559 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007560 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007561 psa_status_t actual_status;
7562 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007564
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007566
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007568
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7570 psa_set_key_algorithm(&attributes, alg);
7571 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007572
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7574 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007575
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 actual_status = psa_asymmetric_decrypt(key, alg,
7577 input_data->x, input_data->len,
7578 label->x, label->len,
7579 output, output_size,
7580 &output_length);
7581 TEST_EQUAL(actual_status, expected_status);
7582 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007583
Gilles Peskine68428122018-06-30 18:42:41 +02007584 /* If the label is empty, the test framework puts a non-null pointer
7585 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007587 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 if (output_size != 0) {
7589 memset(output, 0, output_size);
7590 }
7591 actual_status = psa_asymmetric_decrypt(key, alg,
7592 input_data->x, input_data->len,
7593 NULL, label->len,
7594 output, output_size,
7595 &output_length);
7596 TEST_EQUAL(actual_status, expected_status);
7597 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02007598 }
7599
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007600exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 psa_reset_key_attributes(&attributes);
7602 psa_destroy_key(key);
7603 mbedtls_free(output);
7604 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007605}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007606/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007607
7608/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007609void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00007610{
7611 /* Test each valid way of initializing the object, except for `= {0}`, as
7612 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7613 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007614 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007615 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007617 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7618 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00007621
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007622 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
7624 PSA_ERROR_BAD_STATE);
7625 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
7626 PSA_ERROR_BAD_STATE);
7627 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
7628 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007629
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007630 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 PSA_ASSERT(psa_key_derivation_abort(&func));
7632 PSA_ASSERT(psa_key_derivation_abort(&init));
7633 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00007634}
7635/* END_CASE */
7636
Janos Follath16de4a42019-06-13 16:32:24 +01007637/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007638void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02007639{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007640 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007641 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007642 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007643
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02007645
Gilles Peskine449bd832023-01-11 14:50:10 +01007646 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
7647 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02007648
7649exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007650 psa_key_derivation_abort(&operation);
7651 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02007652}
7653/* END_CASE */
7654
Janos Follathaf3c2a02019-06-12 12:34:34 +01007655/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007656void derive_set_capacity(int alg_arg, int capacity_arg,
7657 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01007658{
7659 psa_algorithm_t alg = alg_arg;
7660 size_t capacity = capacity_arg;
7661 psa_status_t expected_status = expected_status_arg;
7662 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7663
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01007665
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01007667
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
7669 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01007670
7671exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 psa_key_derivation_abort(&operation);
7673 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01007674}
7675/* END_CASE */
7676
7677/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007678void derive_input(int alg_arg,
7679 int step_arg1, int key_type_arg1, data_t *input1,
7680 int expected_status_arg1,
7681 int step_arg2, int key_type_arg2, data_t *input2,
7682 int expected_status_arg2,
7683 int step_arg3, int key_type_arg3, data_t *input3,
7684 int expected_status_arg3,
7685 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01007686{
7687 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
7689 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
7690 psa_status_t expected_statuses[] = { expected_status_arg1,
7691 expected_status_arg2,
7692 expected_status_arg3 };
7693 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02007694 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7695 MBEDTLS_SVC_KEY_ID_INIT,
7696 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007697 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7699 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007700 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007701 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007702 psa_status_t expected_output_status = expected_output_status_arg;
7703 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007704
Gilles Peskine449bd832023-01-11 14:50:10 +01007705 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01007706
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
7708 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01007709
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01007711
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
7713 mbedtls_test_set_step(i);
7714 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02007715 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01007716 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
7717 psa_set_key_type(&attributes, key_types[i]);
7718 PSA_ASSERT(psa_import_key(&attributes,
7719 inputs[i]->x, inputs[i]->len,
7720 &keys[i]));
7721 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
7722 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007723 // When taking a private key as secret input, use key agreement
7724 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
7726 &operation, keys[i]),
7727 expected_statuses[i]);
7728 } else {
7729 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
7730 keys[i]),
7731 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007732 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 } else {
7734 TEST_EQUAL(psa_key_derivation_input_bytes(
7735 &operation, steps[i],
7736 inputs[i]->x, inputs[i]->len),
7737 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01007738 }
7739 }
7740
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 if (output_key_type != PSA_KEY_TYPE_NONE) {
7742 psa_reset_key_attributes(&attributes);
7743 psa_set_key_type(&attributes, output_key_type);
7744 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007745 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 psa_key_derivation_output_key(&attributes, &operation,
7747 &output_key);
7748 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007749 uint8_t buffer[1];
7750 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 psa_key_derivation_output_bytes(&operation,
7752 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007753 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007754 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007755
Janos Follathaf3c2a02019-06-12 12:34:34 +01007756exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007757 psa_key_derivation_abort(&operation);
7758 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
7759 psa_destroy_key(keys[i]);
7760 }
7761 psa_destroy_key(output_key);
7762 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01007763}
7764/* END_CASE */
7765
Janos Follathd958bb72019-07-03 15:02:16 +01007766/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007767void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007768{
Janos Follathd958bb72019-07-03 15:02:16 +01007769 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007770 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007771 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007772 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007773 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01007775 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01007776 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007777 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01007778 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007779 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7780 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01007781 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007783
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007785
Gilles Peskine449bd832023-01-11 14:50:10 +01007786 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
7787 psa_set_key_algorithm(&attributes, alg);
7788 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007789
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 PSA_ASSERT(psa_import_key(&attributes,
7791 key_data, sizeof(key_data),
7792 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007793
7794 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01007795 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
7796 input1, input1_length,
7797 input2, input2_length,
7798 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01007799 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007800 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007801
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007802 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01007803 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
7804 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007805
Gilles Peskine449bd832023-01-11 14:50:10 +01007806 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007807
Gilles Peskine449bd832023-01-11 14:50:10 +01007808 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
7809 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007810
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007811exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007812 psa_key_derivation_abort(&operation);
7813 psa_destroy_key(key);
7814 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007815}
7816/* END_CASE */
7817
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007818/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007819void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007820{
7821 uint8_t output_buffer[16];
7822 size_t buffer_size = 16;
7823 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007824 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007825
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
7827 output_buffer, buffer_size)
7828 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007829
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
7831 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007832
Gilles Peskine449bd832023-01-11 14:50:10 +01007833 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007834
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
7836 output_buffer, buffer_size)
7837 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007838
Gilles Peskine449bd832023-01-11 14:50:10 +01007839 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
7840 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007841
7842exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007843 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007844}
7845/* END_CASE */
7846
7847/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007848void derive_output(int alg_arg,
7849 int step1_arg, data_t *input1, int expected_status_arg1,
7850 int step2_arg, data_t *input2, int expected_status_arg2,
7851 int step3_arg, data_t *input3, int expected_status_arg3,
7852 int step4_arg, data_t *input4, int expected_status_arg4,
7853 data_t *key_agreement_peer_key,
7854 int requested_capacity_arg,
7855 data_t *expected_output1,
7856 data_t *expected_output2,
7857 int other_key_input_type,
7858 int key_input_type,
7859 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007860{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007861 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007862 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
7863 data_t *inputs[] = { input1, input2, input3, input4 };
7864 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7865 MBEDTLS_SVC_KEY_ID_INIT,
7866 MBEDTLS_SVC_KEY_ID_INIT,
7867 MBEDTLS_SVC_KEY_ID_INIT };
7868 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
7869 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007870 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007871 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007872 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01007873 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007874 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01007875 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007876 size_t output_buffer_size = 0;
7877 uint8_t *output_buffer = NULL;
7878 size_t expected_capacity;
7879 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007880 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7881 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7882 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7883 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007884 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007885 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007886 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007887
Gilles Peskine449bd832023-01-11 14:50:10 +01007888 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
7889 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007890 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01007891 }
7892 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007893 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01007894 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007895 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007896 ASSERT_ALLOC(output_buffer, output_buffer_size);
7897 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007898
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007899 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
7901 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
7902 requested_capacity));
7903 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
7904 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02007905 case 0:
7906 break;
7907 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007909 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 TEST_EQUAL(psa_key_derivation_input_bytes(
7911 &operation, steps[i],
7912 inputs[i]->x, inputs[i]->len),
7913 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007914
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007916 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007917 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007918 break;
7919 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
7921 psa_set_key_algorithm(&attributes1, alg);
7922 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007923
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 PSA_ASSERT(psa_import_key(&attributes1,
7925 inputs[i]->x, inputs[i]->len,
7926 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007927
Gilles Peskine449bd832023-01-11 14:50:10 +01007928 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
7929 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
7930 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
7931 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007932 }
7933
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 PSA_ASSERT(psa_key_derivation_input_key(&operation,
7935 steps[i],
7936 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007937 break;
7938 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007939 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007940 break;
7941 }
7942 break;
7943 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007944 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007945 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01007946 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
7947 steps[i],
7948 inputs[i]->x,
7949 inputs[i]->len),
7950 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007951 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007952 case 1: // input key, type DERIVE
7953 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01007954 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
7955 psa_set_key_algorithm(&attributes2, alg);
7956 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007957
7958 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01007959 if (other_key_input_type == 11) {
7960 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
7961 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007962
Gilles Peskine449bd832023-01-11 14:50:10 +01007963 PSA_ASSERT(psa_import_key(&attributes2,
7964 inputs[i]->x, inputs[i]->len,
7965 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007966
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 TEST_EQUAL(psa_key_derivation_input_key(&operation,
7968 steps[i],
7969 keys[i]),
7970 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007971 break;
7972 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01007973 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
7974 psa_set_key_algorithm(&attributes3, alg);
7975 psa_set_key_type(&attributes3,
7976 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007977
Gilles Peskine449bd832023-01-11 14:50:10 +01007978 PSA_ASSERT(psa_import_key(&attributes3,
7979 inputs[i]->x, inputs[i]->len,
7980 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007981
Gilles Peskine449bd832023-01-11 14:50:10 +01007982 TEST_EQUAL(psa_key_derivation_key_agreement(
7983 &operation,
7984 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7985 keys[i], key_agreement_peer_key->x,
7986 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007987 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007988 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007989 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007990 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007991 }
7992
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007994 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007996 break;
7997 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007998 TEST_EQUAL(psa_key_derivation_input_bytes(
7999 &operation, steps[i],
8000 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008001
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008003 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008005 break;
8006 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008007 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008008
Gilles Peskine449bd832023-01-11 14:50:10 +01008009 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8010 &current_capacity));
8011 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008012 expected_capacity = requested_capacity;
8013
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008015 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8016
8017 /* For output key derivation secret must be provided using
8018 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008019 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008020 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008021 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008022
Gilles Peskine449bd832023-01-11 14:50:10 +01008023 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8024 psa_set_key_algorithm(&attributes4, alg);
8025 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8026 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008027
Gilles Peskine449bd832023-01-11 14:50:10 +01008028 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8029 &derived_key), expected_status);
8030 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008031 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008032 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008033 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008034 status = psa_key_derivation_output_bytes(&operation,
8035 output_buffer, output_sizes[i]);
8036 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008037 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 TEST_ASSERT(status == PSA_SUCCESS ||
8039 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008040 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008041 } else if (expected_capacity == 0 ||
8042 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008043 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008044 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008045 expected_capacity = 0;
8046 continue;
8047 }
8048 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008049 PSA_ASSERT(status);
8050 if (output_sizes[i] != 0) {
8051 ASSERT_COMPARE(output_buffer, output_sizes[i],
8052 expected_outputs[i], output_sizes[i]);
8053 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008054 /* Check the operation status. */
8055 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008056 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8057 &current_capacity));
8058 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008059 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008060 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008061 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008062
8063exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008064 mbedtls_free(output_buffer);
8065 psa_key_derivation_abort(&operation);
8066 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8067 psa_destroy_key(keys[i]);
8068 }
8069 psa_destroy_key(derived_key);
8070 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008071}
8072/* END_CASE */
8073
8074/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008075void derive_full(int alg_arg,
8076 data_t *key_data,
8077 data_t *input1,
8078 data_t *input2,
8079 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008080{
Ronald Cron5425a212020-08-04 14:58:35 +02008081 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008082 psa_algorithm_t alg = alg_arg;
8083 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008084 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008085 unsigned char output_buffer[16];
8086 size_t expected_capacity = requested_capacity;
8087 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008089
Gilles Peskine449bd832023-01-11 14:50:10 +01008090 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008091
Gilles Peskine449bd832023-01-11 14:50:10 +01008092 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8093 psa_set_key_algorithm(&attributes, alg);
8094 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008095
Gilles Peskine449bd832023-01-11 14:50:10 +01008096 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8097 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008098
Gilles Peskine449bd832023-01-11 14:50:10 +01008099 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8100 input1->x, input1->len,
8101 input2->x, input2->len,
8102 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008104 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008105
Gilles Peskine449bd832023-01-11 14:50:10 +01008106 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8107 &current_capacity));
8108 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008109
8110 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 while (current_capacity > 0) {
8112 size_t read_size = sizeof(output_buffer);
8113 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008114 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008115 }
8116 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8117 output_buffer,
8118 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008119 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008120 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8121 &current_capacity));
8122 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008123 }
8124
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008125 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008126 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8127 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008128
Gilles Peskine449bd832023-01-11 14:50:10 +01008129 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008130
8131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008132 psa_key_derivation_abort(&operation);
8133 psa_destroy_key(key);
8134 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008135}
8136/* END_CASE */
8137
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008138/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008139void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8140 int derivation_step,
8141 int capacity, int expected_capacity_status_arg,
8142 data_t *expected_output,
8143 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008144{
8145 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8146 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008147 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008148 uint8_t *output_buffer = NULL;
8149 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008150 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8151 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8152 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008153
Gilles Peskine449bd832023-01-11 14:50:10 +01008154 ASSERT_ALLOC(output_buffer, expected_output->len);
8155 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008156
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8158 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8159 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008160
Gilles Peskine449bd832023-01-11 14:50:10 +01008161 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8162 step, input->x, input->len),
8163 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008164
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008166 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008167 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008168
Gilles Peskine449bd832023-01-11 14:50:10 +01008169 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8170 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008171
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 TEST_EQUAL(status, expected_output_status);
8173 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8174 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8175 expected_output->len);
8176 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008177
8178exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008179 mbedtls_free(output_buffer);
8180 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008181 PSA_DONE();
8182}
8183/* END_CASE */
8184
Janos Follathe60c9052019-07-03 13:51:30 +01008185/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008186void derive_key_exercise(int alg_arg,
8187 data_t *key_data,
8188 data_t *input1,
8189 data_t *input2,
8190 int derived_type_arg,
8191 int derived_bits_arg,
8192 int derived_usage_arg,
8193 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008194{
Ronald Cron5425a212020-08-04 14:58:35 +02008195 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8196 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008197 psa_algorithm_t alg = alg_arg;
8198 psa_key_type_t derived_type = derived_type_arg;
8199 size_t derived_bits = derived_bits_arg;
8200 psa_key_usage_t derived_usage = derived_usage_arg;
8201 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008202 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008203 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008204 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008205 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008206
Gilles Peskine449bd832023-01-11 14:50:10 +01008207 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008208
Gilles Peskine449bd832023-01-11 14:50:10 +01008209 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8210 psa_set_key_algorithm(&attributes, alg);
8211 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8212 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8213 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008214
8215 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008216 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8217 input1->x, input1->len,
8218 input2->x, input2->len,
8219 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008221 }
Janos Follathe60c9052019-07-03 13:51:30 +01008222
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 psa_set_key_usage_flags(&attributes, derived_usage);
8224 psa_set_key_algorithm(&attributes, derived_alg);
8225 psa_set_key_type(&attributes, derived_type);
8226 psa_set_key_bits(&attributes, derived_bits);
8227 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
8228 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008229
8230 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
8232 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
8233 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008234
8235 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008236 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02008237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008238 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02008239
8240exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008241 /*
8242 * Key attributes may have been returned by psa_get_key_attributes()
8243 * thus reset them as required.
8244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008246
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 psa_key_derivation_abort(&operation);
8248 psa_destroy_key(base_key);
8249 psa_destroy_key(derived_key);
8250 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008251}
8252/* END_CASE */
8253
Janos Follath42fd8882019-07-03 14:17:09 +01008254/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008255void derive_key_export(int alg_arg,
8256 data_t *key_data,
8257 data_t *input1,
8258 data_t *input2,
8259 int bytes1_arg,
8260 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008261{
Ronald Cron5425a212020-08-04 14:58:35 +02008262 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8263 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008264 psa_algorithm_t alg = alg_arg;
8265 size_t bytes1 = bytes1_arg;
8266 size_t bytes2 = bytes2_arg;
8267 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008268 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008269 uint8_t *output_buffer = NULL;
8270 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008271 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8272 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008273 size_t length;
8274
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 ASSERT_ALLOC(output_buffer, capacity);
8276 ASSERT_ALLOC(export_buffer, capacity);
8277 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008278
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8280 psa_set_key_algorithm(&base_attributes, alg);
8281 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8282 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8283 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008284
8285 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8287 input1->x, input1->len,
8288 input2->x, input2->len,
8289 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008291 }
Janos Follath42fd8882019-07-03 14:17:09 +01008292
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8294 output_buffer,
8295 capacity));
8296 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008297
8298 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8300 input1->x, input1->len,
8301 input2->x, input2->len,
8302 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008303 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 }
Janos Follath42fd8882019-07-03 14:17:09 +01008305
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8307 psa_set_key_algorithm(&derived_attributes, 0);
8308 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
8309 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
8310 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8311 &derived_key));
8312 PSA_ASSERT(psa_export_key(derived_key,
8313 export_buffer, bytes1,
8314 &length));
8315 TEST_EQUAL(length, bytes1);
8316 PSA_ASSERT(psa_destroy_key(derived_key));
8317 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
8318 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8319 &derived_key));
8320 PSA_ASSERT(psa_export_key(derived_key,
8321 export_buffer + bytes1, bytes2,
8322 &length));
8323 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008324
8325 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008326 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
8327 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008328
8329exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008330 mbedtls_free(output_buffer);
8331 mbedtls_free(export_buffer);
8332 psa_key_derivation_abort(&operation);
8333 psa_destroy_key(base_key);
8334 psa_destroy_key(derived_key);
8335 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008336}
8337/* END_CASE */
8338
8339/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008340void derive_key_type(int alg_arg,
8341 data_t *key_data,
8342 data_t *input1,
8343 data_t *input2,
8344 int key_type_arg, int bits_arg,
8345 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008346{
8347 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8348 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8349 const psa_algorithm_t alg = alg_arg;
8350 const psa_key_type_t key_type = key_type_arg;
8351 const size_t bits = bits_arg;
8352 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8353 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008355 uint8_t *export_buffer = NULL;
8356 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8357 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8358 size_t export_length;
8359
Gilles Peskine449bd832023-01-11 14:50:10 +01008360 ASSERT_ALLOC(export_buffer, export_buffer_size);
8361 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008362
Gilles Peskine449bd832023-01-11 14:50:10 +01008363 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8364 psa_set_key_algorithm(&base_attributes, alg);
8365 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8366 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8367 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008368
Gilles Peskine449bd832023-01-11 14:50:10 +01008369 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008370 &operation, base_key, alg,
8371 input1->x, input1->len,
8372 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008374 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008376
Gilles Peskine449bd832023-01-11 14:50:10 +01008377 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8378 psa_set_key_algorithm(&derived_attributes, 0);
8379 psa_set_key_type(&derived_attributes, key_type);
8380 psa_set_key_bits(&derived_attributes, bits);
8381 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8382 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008383
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 PSA_ASSERT(psa_export_key(derived_key,
8385 export_buffer, export_buffer_size,
8386 &export_length));
8387 ASSERT_COMPARE(export_buffer, export_length,
8388 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008389
8390exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 mbedtls_free(export_buffer);
8392 psa_key_derivation_abort(&operation);
8393 psa_destroy_key(base_key);
8394 psa_destroy_key(derived_key);
8395 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008396}
8397/* END_CASE */
8398
8399/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008400void derive_key(int alg_arg,
8401 data_t *key_data, data_t *input1, data_t *input2,
8402 int type_arg, int bits_arg,
8403 int expected_status_arg,
8404 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02008405{
Ronald Cron5425a212020-08-04 14:58:35 +02008406 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8407 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008408 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008409 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008410 size_t bits = bits_arg;
8411 psa_status_t expected_status = expected_status_arg;
8412 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8413 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8414 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8415
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02008417
Gilles Peskine449bd832023-01-11 14:50:10 +01008418 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8419 psa_set_key_algorithm(&base_attributes, alg);
8420 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8421 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8422 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02008423
Gilles Peskine449bd832023-01-11 14:50:10 +01008424 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8425 input1->x, input1->len,
8426 input2->x, input2->len,
8427 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02008428 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008429 }
Gilles Peskinec744d992019-07-30 17:26:54 +02008430
Gilles Peskine449bd832023-01-11 14:50:10 +01008431 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8432 psa_set_key_algorithm(&derived_attributes, 0);
8433 psa_set_key_type(&derived_attributes, type);
8434 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008435
8436 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008437 psa_key_derivation_output_key(&derived_attributes,
8438 &operation,
8439 &derived_key);
8440 if (is_large_output > 0) {
8441 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8442 }
8443 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02008444
8445exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 psa_key_derivation_abort(&operation);
8447 psa_destroy_key(base_key);
8448 psa_destroy_key(derived_key);
8449 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02008450}
8451/* END_CASE */
8452
8453/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008454void key_agreement_setup(int alg_arg,
8455 int our_key_type_arg, int our_key_alg_arg,
8456 data_t *our_key_data, data_t *peer_key_data,
8457 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02008458{
Ronald Cron5425a212020-08-04 14:58:35 +02008459 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008460 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008461 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008462 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008463 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008465 psa_status_t expected_status = expected_status_arg;
8466 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008467
Gilles Peskine449bd832023-01-11 14:50:10 +01008468 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02008469
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8471 psa_set_key_algorithm(&attributes, our_key_alg);
8472 psa_set_key_type(&attributes, our_key_type);
8473 PSA_ASSERT(psa_import_key(&attributes,
8474 our_key_data->x, our_key_data->len,
8475 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02008476
Gilles Peskine77f40d82019-04-11 21:27:06 +02008477 /* The tests currently include inputs that should fail at either step.
8478 * Test cases that fail at the setup step should be changed to call
8479 * key_derivation_setup instead, and this function should be renamed
8480 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 status = psa_key_derivation_setup(&operation, alg);
8482 if (status == PSA_SUCCESS) {
8483 TEST_EQUAL(psa_key_derivation_key_agreement(
8484 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8485 our_key,
8486 peer_key_data->x, peer_key_data->len),
8487 expected_status);
8488 } else {
8489 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02008490 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008491
8492exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 psa_key_derivation_abort(&operation);
8494 psa_destroy_key(our_key);
8495 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02008496}
8497/* END_CASE */
8498
8499/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008500void raw_key_agreement(int alg_arg,
8501 int our_key_type_arg, data_t *our_key_data,
8502 data_t *peer_key_data,
8503 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02008504{
Ronald Cron5425a212020-08-04 14:58:35 +02008505 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008506 psa_algorithm_t alg = alg_arg;
8507 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008508 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008509 unsigned char *output = NULL;
8510 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008511 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008512
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02008514
Gilles Peskine449bd832023-01-11 14:50:10 +01008515 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8516 psa_set_key_algorithm(&attributes, alg);
8517 psa_set_key_type(&attributes, our_key_type);
8518 PSA_ASSERT(psa_import_key(&attributes,
8519 our_key_data->x, our_key_data->len,
8520 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02008521
Gilles Peskine449bd832023-01-11 14:50:10 +01008522 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
8523 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008524
Gilles Peskine992bee82022-04-13 23:25:52 +02008525 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 TEST_LE_U(expected_output->len,
8527 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
8528 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
8529 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02008530
8531 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 ASSERT_ALLOC(output, expected_output->len);
8533 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8534 peer_key_data->x, peer_key_data->len,
8535 output, expected_output->len,
8536 &output_length));
8537 ASSERT_COMPARE(output, output_length,
8538 expected_output->x, expected_output->len);
8539 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008540 output = NULL;
8541 output_length = ~0;
8542
8543 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008544 ASSERT_ALLOC(output, expected_output->len + 1);
8545 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8546 peer_key_data->x, peer_key_data->len,
8547 output, expected_output->len + 1,
8548 &output_length));
8549 ASSERT_COMPARE(output, output_length,
8550 expected_output->x, expected_output->len);
8551 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008552 output = NULL;
8553 output_length = ~0;
8554
8555 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 ASSERT_ALLOC(output, expected_output->len - 1);
8557 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
8558 peer_key_data->x, peer_key_data->len,
8559 output, expected_output->len - 1,
8560 &output_length),
8561 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02008562 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 TEST_LE_U(output_length, expected_output->len - 1);
8564 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008565 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008566
8567exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 mbedtls_free(output);
8569 psa_destroy_key(our_key);
8570 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02008571}
8572/* END_CASE */
8573
8574/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008575void key_agreement_capacity(int alg_arg,
8576 int our_key_type_arg, data_t *our_key_data,
8577 data_t *peer_key_data,
8578 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02008579{
Ronald Cron5425a212020-08-04 14:58:35 +02008580 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008581 psa_algorithm_t alg = alg_arg;
8582 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008583 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008585 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008586 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008587
Gilles Peskine449bd832023-01-11 14:50:10 +01008588 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02008589
Gilles Peskine449bd832023-01-11 14:50:10 +01008590 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8591 psa_set_key_algorithm(&attributes, alg);
8592 psa_set_key_type(&attributes, our_key_type);
8593 PSA_ASSERT(psa_import_key(&attributes,
8594 our_key_data->x, our_key_data->len,
8595 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02008596
Gilles Peskine449bd832023-01-11 14:50:10 +01008597 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8598 PSA_ASSERT(psa_key_derivation_key_agreement(
8599 &operation,
8600 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8601 peer_key_data->x, peer_key_data->len));
8602 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008603 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
8605 PSA_KEY_DERIVATION_INPUT_INFO,
8606 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008607 }
Gilles Peskine59685592018-09-18 12:11:34 +02008608
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008609 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 PSA_ASSERT(psa_key_derivation_get_capacity(
8611 &operation, &actual_capacity));
8612 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02008613
Gilles Peskinebf491972018-10-25 22:36:12 +02008614 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 while (actual_capacity > sizeof(output)) {
8616 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8617 output, sizeof(output)));
8618 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02008619 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8621 output, actual_capacity));
8622 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
8623 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02008624
Gilles Peskine59685592018-09-18 12:11:34 +02008625exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008626 psa_key_derivation_abort(&operation);
8627 psa_destroy_key(our_key);
8628 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02008629}
8630/* END_CASE */
8631
8632/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008633void key_agreement_output(int alg_arg,
8634 int our_key_type_arg, data_t *our_key_data,
8635 data_t *peer_key_data,
8636 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02008637{
Ronald Cron5425a212020-08-04 14:58:35 +02008638 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008639 psa_algorithm_t alg = alg_arg;
8640 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008641 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008643 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008644
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
8646 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02008647
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02008649
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8651 psa_set_key_algorithm(&attributes, alg);
8652 psa_set_key_type(&attributes, our_key_type);
8653 PSA_ASSERT(psa_import_key(&attributes,
8654 our_key_data->x, our_key_data->len,
8655 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02008656
Gilles Peskine449bd832023-01-11 14:50:10 +01008657 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8658 PSA_ASSERT(psa_key_derivation_key_agreement(
8659 &operation,
8660 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8661 peer_key_data->x, peer_key_data->len));
8662 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008663 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
8665 PSA_KEY_DERIVATION_INPUT_INFO,
8666 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008667 }
Gilles Peskine59685592018-09-18 12:11:34 +02008668
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8670 actual_output,
8671 expected_output1->len));
8672 ASSERT_COMPARE(actual_output, expected_output1->len,
8673 expected_output1->x, expected_output1->len);
8674 if (expected_output2->len != 0) {
8675 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8676 actual_output,
8677 expected_output2->len));
8678 ASSERT_COMPARE(actual_output, expected_output2->len,
8679 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008680 }
Gilles Peskine59685592018-09-18 12:11:34 +02008681
8682exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008683 psa_key_derivation_abort(&operation);
8684 psa_destroy_key(our_key);
8685 PSA_DONE();
8686 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02008687}
8688/* END_CASE */
8689
8690/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008691void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02008692{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008693 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008694 unsigned char *output = NULL;
8695 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008696 size_t i;
8697 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008698
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00008700
Gilles Peskine449bd832023-01-11 14:50:10 +01008701 ASSERT_ALLOC(output, bytes);
8702 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02008703
Gilles Peskine449bd832023-01-11 14:50:10 +01008704 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02008705
Gilles Peskinea50d7392018-06-21 10:22:13 +02008706 /* Run several times, to ensure that every output byte will be
8707 * nonzero at least once with overwhelming probability
8708 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 for (run = 0; run < 10; run++) {
8710 if (bytes != 0) {
8711 memset(output, 0, bytes);
8712 }
8713 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02008714
Gilles Peskine449bd832023-01-11 14:50:10 +01008715 for (i = 0; i < bytes; i++) {
8716 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02008717 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008719 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008720 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008721
8722 /* Check that every byte was changed to nonzero at least once. This
8723 * validates that psa_generate_random is overwriting every byte of
8724 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 for (i = 0; i < bytes; i++) {
8726 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02008727 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008728
8729exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 PSA_DONE();
8731 mbedtls_free(output);
8732 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02008733}
8734/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008735
8736/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008737void generate_key(int type_arg,
8738 int bits_arg,
8739 int usage_arg,
8740 int alg_arg,
8741 int expected_status_arg,
8742 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02008743{
Ronald Cron5425a212020-08-04 14:58:35 +02008744 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008745 psa_key_type_t type = type_arg;
8746 psa_key_usage_t usage = usage_arg;
8747 size_t bits = bits_arg;
8748 psa_algorithm_t alg = alg_arg;
8749 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008750 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008751 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008752
Gilles Peskine449bd832023-01-11 14:50:10 +01008753 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02008754
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 psa_set_key_usage_flags(&attributes, usage);
8756 psa_set_key_algorithm(&attributes, alg);
8757 psa_set_key_type(&attributes, type);
8758 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02008759
8760 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008762
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 if (is_large_key > 0) {
8764 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8765 }
8766 TEST_EQUAL(status, expected_status);
8767 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008768 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02008770
8771 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
8773 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
8774 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02008775
Gilles Peskine818ca122018-06-20 18:16:48 +02008776 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02008778 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02008780
8781exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008782 /*
8783 * Key attributes may have been returned by psa_get_key_attributes()
8784 * thus reset them as required.
8785 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008786 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008787
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 psa_destroy_key(key);
8789 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02008790}
8791/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008792
Ronald Cronee414c72021-03-18 18:50:08 +01008793/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
Gilles Peskine449bd832023-01-11 14:50:10 +01008794void generate_key_rsa(int bits_arg,
8795 data_t *e_arg,
8796 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02008797{
Ronald Cron5425a212020-08-04 14:58:35 +02008798 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008799 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008800 size_t bits = bits_arg;
8801 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8802 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8803 psa_status_t expected_status = expected_status_arg;
8804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8805 uint8_t *exported = NULL;
8806 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008808 size_t exported_length = SIZE_MAX;
8809 uint8_t *e_read_buffer = NULL;
8810 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008812 size_t e_read_length = SIZE_MAX;
8813
Gilles Peskine449bd832023-01-11 14:50:10 +01008814 if (e_arg->len == 0 ||
8815 (e_arg->len == 3 &&
8816 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008817 is_default_public_exponent = 1;
8818 e_read_size = 0;
8819 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 ASSERT_ALLOC(e_read_buffer, e_read_size);
8821 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008822
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02008824
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 psa_set_key_usage_flags(&attributes, usage);
8826 psa_set_key_algorithm(&attributes, alg);
8827 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
8828 e_arg->x, e_arg->len));
8829 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008830
8831 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
8833 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008834 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008836
8837 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8839 TEST_EQUAL(psa_get_key_type(&attributes), type);
8840 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
8841 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
8842 e_read_buffer, e_read_size,
8843 &e_read_length));
8844 if (is_default_public_exponent) {
8845 TEST_EQUAL(e_read_length, 0);
8846 } else {
8847 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
8848 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008849
8850 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008852 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008854
8855 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 PSA_ASSERT(psa_export_public_key(key,
8857 exported, exported_size,
8858 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02008859 {
8860 uint8_t *p = exported;
8861 uint8_t *end = exported + exported_length;
8862 size_t len;
8863 /* RSAPublicKey ::= SEQUENCE {
8864 * modulus INTEGER, -- n
8865 * publicExponent INTEGER } -- e
8866 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
8868 MBEDTLS_ASN1_SEQUENCE |
8869 MBEDTLS_ASN1_CONSTRUCTED));
8870 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
8871 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
8872 MBEDTLS_ASN1_INTEGER));
8873 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008874 ++p;
8875 --len;
8876 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 if (e_arg->len == 0) {
8878 TEST_EQUAL(len, 3);
8879 TEST_EQUAL(p[0], 1);
8880 TEST_EQUAL(p[1], 0);
8881 TEST_EQUAL(p[2], 1);
8882 } else {
8883 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008884 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008885 }
8886
8887exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008888 /*
8889 * Key attributes may have been returned by psa_get_key_attributes() or
8890 * set by psa_set_key_domain_parameters() thus reset them as required.
8891 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008893
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 psa_destroy_key(key);
8895 PSA_DONE();
8896 mbedtls_free(e_read_buffer);
8897 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008898}
8899/* END_CASE */
8900
Darryl Greend49a4992018-06-18 17:27:26 +01008901/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01008902void persistent_key_load_key_from_storage(data_t *data,
8903 int type_arg, int bits_arg,
8904 int usage_flags_arg, int alg_arg,
8905 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01008906{
Gilles Peskine449bd832023-01-11 14:50:10 +01008907 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008908 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008909 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8910 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008911 psa_key_type_t type = type_arg;
8912 size_t bits = bits_arg;
8913 psa_key_usage_t usage_flags = usage_flags_arg;
8914 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008915 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008916 unsigned char *first_export = NULL;
8917 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008918 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01008919 size_t first_exported_length;
8920 size_t second_exported_length;
8921
Gilles Peskine449bd832023-01-11 14:50:10 +01008922 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
8923 ASSERT_ALLOC(first_export, export_size);
8924 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008925 }
Darryl Greend49a4992018-06-18 17:27:26 +01008926
Gilles Peskine449bd832023-01-11 14:50:10 +01008927 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01008928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 psa_set_key_id(&attributes, key_id);
8930 psa_set_key_usage_flags(&attributes, usage_flags);
8931 psa_set_key_algorithm(&attributes, alg);
8932 psa_set_key_type(&attributes, type);
8933 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01008934
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00008936 case IMPORT_KEY:
8937 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008938 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
8939 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00008940 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008941
Darryl Green0c6575a2018-11-07 16:05:30 +00008942 case GENERATE_KEY:
8943 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00008945 break;
8946
8947 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008948#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008949 {
8950 /* Create base key */
8951 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
8952 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8953 psa_set_key_usage_flags(&base_attributes,
8954 PSA_KEY_USAGE_DERIVE);
8955 psa_set_key_algorithm(&base_attributes, derive_alg);
8956 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8957 PSA_ASSERT(psa_import_key(&base_attributes,
8958 data->x, data->len,
8959 &base_key));
8960 /* Derive a key. */
8961 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
8962 PSA_ASSERT(psa_key_derivation_input_key(
8963 &operation,
8964 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
8965 PSA_ASSERT(psa_key_derivation_input_bytes(
8966 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
8967 NULL, 0));
8968 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
8969 &operation,
8970 &key));
8971 PSA_ASSERT(psa_key_derivation_abort(&operation));
8972 PSA_ASSERT(psa_destroy_key(base_key));
8973 base_key = MBEDTLS_SVC_KEY_ID_INIT;
8974 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008975#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008976 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008977#endif
8978 break;
8979
8980 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008982 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008983 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008984 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01008985
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008986 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
8988 PSA_ASSERT(psa_export_key(key,
8989 first_export, export_size,
8990 &first_exported_length));
8991 if (generation_method == IMPORT_KEY) {
8992 ASSERT_COMPARE(data->x, data->len,
8993 first_export, first_exported_length);
8994 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008995 }
Darryl Greend49a4992018-06-18 17:27:26 +01008996
8997 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01008998 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008999 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009001
Darryl Greend49a4992018-06-18 17:27:26 +01009002 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9004 TEST_ASSERT(mbedtls_svc_key_id_equal(
9005 psa_get_key_id(&attributes), key_id));
9006 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9007 PSA_KEY_LIFETIME_PERSISTENT);
9008 TEST_EQUAL(psa_get_key_type(&attributes), type);
9009 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9010 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9011 mbedtls_test_update_key_usage_flags(usage_flags));
9012 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009013
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009014 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9016 PSA_ASSERT(psa_export_key(key,
9017 second_export, export_size,
9018 &second_exported_length));
9019 ASSERT_COMPARE(first_export, first_exported_length,
9020 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009021 }
9022
9023 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009025 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 }
Darryl Greend49a4992018-06-18 17:27:26 +01009027
9028exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009029 /*
9030 * Key attributes may have been returned by psa_get_key_attributes()
9031 * thus reset them as required.
9032 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009033 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009034
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 mbedtls_free(first_export);
9036 mbedtls_free(second_export);
9037 psa_key_derivation_abort(&operation);
9038 psa_destroy_key(base_key);
9039 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009040 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009041}
9042/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009043
Neil Armstronga557cb82022-06-10 08:58:32 +02009044/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009045void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9046 int primitive_arg, int hash_arg, int role_arg,
9047 int test_input, data_t *pw_data,
9048 int inj_err_type_arg,
9049 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009050{
9051 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9052 psa_pake_operation_t operation = psa_pake_operation_init();
9053 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009054 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009055 psa_key_type_t key_type_pw = key_type_pw_arg;
9056 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009057 psa_algorithm_t hash_alg = hash_arg;
9058 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009059 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009061 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9062 psa_status_t expected_error = expected_error_arg;
9063 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009064 unsigned char *output_buffer = NULL;
9065 size_t output_len = 0;
9066
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009068
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009069 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009070 PSA_PAKE_STEP_KEY_SHARE);
9071 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009072
Gilles Peskine449bd832023-01-11 14:50:10 +01009073 if (pw_data->len > 0) {
9074 psa_set_key_usage_flags(&attributes, key_usage_pw);
9075 psa_set_key_algorithm(&attributes, alg);
9076 psa_set_key_type(&attributes, key_type_pw);
9077 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9078 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009079 }
9080
Gilles Peskine449bd832023-01-11 14:50:10 +01009081 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9082 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9083 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009084
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009086
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9088 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9089 expected_error);
9090 PSA_ASSERT(psa_pake_abort(&operation));
9091 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9092 expected_error);
9093 PSA_ASSERT(psa_pake_abort(&operation));
9094 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9095 expected_error);
9096 PSA_ASSERT(psa_pake_abort(&operation));
9097 TEST_EQUAL(psa_pake_set_role(&operation, role),
9098 expected_error);
9099 PSA_ASSERT(psa_pake_abort(&operation));
9100 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9101 NULL, 0, NULL),
9102 expected_error);
9103 PSA_ASSERT(psa_pake_abort(&operation));
9104 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9105 expected_error);
9106 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009107 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009108 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009109
Gilles Peskine449bd832023-01-11 14:50:10 +01009110 status = psa_pake_setup(&operation, &cipher_suite);
9111 if (status != PSA_SUCCESS) {
9112 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009113 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009114 }
9115
Gilles Peskine449bd832023-01-11 14:50:10 +01009116 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9117 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9118 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009119 goto exit;
9120 }
9121
Gilles Peskine449bd832023-01-11 14:50:10 +01009122 status = psa_pake_set_role(&operation, role);
9123 if (status != PSA_SUCCESS) {
9124 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009125 goto exit;
9126 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009127
Gilles Peskine449bd832023-01-11 14:50:10 +01009128 if (pw_data->len > 0) {
9129 status = psa_pake_set_password_key(&operation, key);
9130 if (status != PSA_SUCCESS) {
9131 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009132 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009133 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009134 }
9135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9137 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9138 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009139 goto exit;
9140 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009141
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9143 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9144 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009145 goto exit;
9146 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009147
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009149 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9151 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009152 goto exit;
9153 }
9154
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009156 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9158 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009159 goto exit;
9160 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009161
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9163 PSA_PAKE_STEP_KEY_SHARE);
9164 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9165 PSA_PAKE_STEP_ZK_PUBLIC);
9166 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9167 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009168
Gilles Peskine449bd832023-01-11 14:50:10 +01009169 if (test_input) {
9170 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9171 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9172 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009173 goto exit;
9174 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009175
Gilles Peskine449bd832023-01-11 14:50:10 +01009176 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9177 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9178 output_buffer, size_zk_proof),
9179 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009180 goto exit;
9181 }
9182
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9184 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9185 output_buffer, size_zk_proof),
9186 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009187 goto exit;
9188 }
9189
Gilles Peskine449bd832023-01-11 14:50:10 +01009190 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9191 output_buffer, size_key_share);
9192 if (status != PSA_SUCCESS) {
9193 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009194 goto exit;
9195 }
9196
Gilles Peskine449bd832023-01-11 14:50:10 +01009197 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9198 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9199 output_buffer, size_zk_public + 1),
9200 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009201 goto exit;
9202 }
9203
Gilles Peskine449bd832023-01-11 14:50:10 +01009204 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009205 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9207 output_buffer, size_zk_public + 1);
9208 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9209 output_buffer, size_zk_public),
9210 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009211 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009212 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009213 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9215 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9216 NULL, 0, NULL),
9217 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009218 goto exit;
9219 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009220
Gilles Peskine449bd832023-01-11 14:50:10 +01009221 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9222 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9223 output_buffer, buf_size, &output_len),
9224 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009225 goto exit;
9226 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009227
Gilles Peskine449bd832023-01-11 14:50:10 +01009228 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9229 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9230 output_buffer, buf_size, &output_len),
9231 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009232 goto exit;
9233 }
9234
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9236 output_buffer, buf_size, &output_len);
9237 if (status != PSA_SUCCESS) {
9238 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009239 goto exit;
9240 }
9241
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +01009243
Gilles Peskine449bd832023-01-11 14:50:10 +01009244 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9245 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9246 output_buffer, size_zk_public - 1, &output_len),
9247 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +01009248 goto exit;
9249 }
9250
Gilles Peskine449bd832023-01-11 14:50:10 +01009251 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009252 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009253 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9254 output_buffer, size_zk_public - 1, &output_len);
9255 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9256 output_buffer, buf_size, &output_len),
9257 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009258 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009259 }
9260 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009261
9262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009263 PSA_ASSERT(psa_destroy_key(key));
9264 PSA_ASSERT(psa_pake_abort(&operation));
9265 mbedtls_free(output_buffer);
9266 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009267}
9268/* END_CASE */
9269
Neil Armstronga557cb82022-06-10 08:58:32 +02009270/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009271void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
9272 int client_input_first, int inject_error,
9273 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009274{
9275 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9276 psa_pake_operation_t server = psa_pake_operation_init();
9277 psa_pake_operation_t client = psa_pake_operation_init();
9278 psa_algorithm_t alg = alg_arg;
9279 psa_algorithm_t hash_alg = hash_arg;
9280 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9282
Gilles Peskine449bd832023-01-11 14:50:10 +01009283 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009284
Gilles Peskine449bd832023-01-11 14:50:10 +01009285 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9286 psa_set_key_algorithm(&attributes, alg);
9287 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9288 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9289 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009290
Gilles Peskine449bd832023-01-11 14:50:10 +01009291 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9292 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9293 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009294
9295
Gilles Peskine449bd832023-01-11 14:50:10 +01009296 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9297 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009298
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9300 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009301
Gilles Peskine449bd832023-01-11 14:50:10 +01009302 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9303 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009304
Gilles Peskine449bd832023-01-11 14:50:10 +01009305 ecjpake_do_round(alg, primitive_arg, &server, &client,
9306 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009307
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009309 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009310 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009311
Gilles Peskine449bd832023-01-11 14:50:10 +01009312 ecjpake_do_round(alg, primitive_arg, &server, &client,
9313 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009314
9315exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 psa_destroy_key(key);
9317 psa_pake_abort(&server);
9318 psa_pake_abort(&client);
9319 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009320}
9321/* END_CASE */
9322
9323/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009324void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
9325 int derive_alg_arg, data_t *pw_data,
9326 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009327{
9328 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9329 psa_pake_operation_t server = psa_pake_operation_init();
9330 psa_pake_operation_t client = psa_pake_operation_init();
9331 psa_algorithm_t alg = alg_arg;
9332 psa_algorithm_t hash_alg = hash_arg;
9333 psa_algorithm_t derive_alg = derive_alg_arg;
9334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9336 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009337 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009338 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009339 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009340 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009341
Gilles Peskine449bd832023-01-11 14:50:10 +01009342 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009343
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9345 psa_set_key_algorithm(&attributes, alg);
9346 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9347 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9348 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009349
Gilles Peskine449bd832023-01-11 14:50:10 +01009350 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9351 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9352 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009353
Neil Armstrong1e855602022-06-15 11:32:11 +02009354 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009355 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
9356 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +02009357
Gilles Peskine449bd832023-01-11 14:50:10 +01009358 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
9359 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
9360 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
9361 PSA_KEY_DERIVATION_INPUT_SEED,
9362 (const uint8_t *) "", 0));
9363 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
9364 PSA_KEY_DERIVATION_INPUT_SEED,
9365 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +02009366 }
9367
Gilles Peskine449bd832023-01-11 14:50:10 +01009368 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9369 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009370
Gilles Peskine449bd832023-01-11 14:50:10 +01009371 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9372 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009373
Gilles Peskine449bd832023-01-11 14:50:10 +01009374 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9375 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009376
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
9378 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9379 PSA_ERROR_BAD_STATE);
9380 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9381 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009382 goto exit;
9383 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009384
Neil Armstrongf983caf2022-06-15 15:27:48 +02009385 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009386 ecjpake_do_round(alg, primitive_arg, &server, &client,
9387 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009388
Gilles Peskine449bd832023-01-11 14:50:10 +01009389 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
9390 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9391 PSA_ERROR_BAD_STATE);
9392 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9393 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009394 goto exit;
9395 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009396
Neil Armstrongf983caf2022-06-15 15:27:48 +02009397 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009398 ecjpake_do_round(alg, primitive_arg, &server, &client,
9399 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009400
Gilles Peskine449bd832023-01-11 14:50:10 +01009401 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
9402 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009403
9404exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009405 psa_key_derivation_abort(&server_derive);
9406 psa_key_derivation_abort(&client_derive);
9407 psa_destroy_key(key);
9408 psa_pake_abort(&server);
9409 psa_pake_abort(&client);
9410 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009411}
9412/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009413
9414/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009415void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009416{
9417 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9418 const size_t bits = 256;
9419 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009421 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009423
9424 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9425 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009426 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9427 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
9428 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9429 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009430 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9432 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009433
9434 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9436 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
9437 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9438 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
9439 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9440 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009441
9442 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +01009443 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9444 PSA_PAKE_OUTPUT_MAX_SIZE);
9445 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9446 PSA_PAKE_OUTPUT_MAX_SIZE);
9447 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9448 PSA_PAKE_OUTPUT_MAX_SIZE);
9449 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9450 PSA_PAKE_INPUT_MAX_SIZE);
9451 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9452 PSA_PAKE_INPUT_MAX_SIZE);
9453 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9454 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009455}
9456/* END_CASE */