blob: 61ac74e86d964f414169594cd52a7027860bbc0f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
Gilles Peskine42649d92022-11-23 14:15:57 +01007#include "common.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02009/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
10 * uses mbedtls_ctr_drbg internally. */
11#include "mbedtls/ctr_drbg.h"
12
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020013#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020014#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +010016/* For psa_can_do_hash() */
17#include "psa_crypto_core.h"
18
Gilles Peskine8e94efe2021-02-13 00:25:53 +010019#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010020#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010021#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053022#if defined(PSA_CRYPTO_DRIVER_TEST)
23#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053024#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
25#else
26#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053027#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010028
Gilles Peskine4023c012021-05-27 13:21:20 +020029/* If this comes up, it's a bug in the test code or in the test data. */
30#define UNUSED 0xdeadbeef
31
Dave Rodgman647791d2021-06-23 12:49:59 +010032/* Assert that an operation is (not) active.
33 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010034#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
35#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010036
Przemek Stekiel7c795482022-11-15 22:26:12 +010037#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010038int ecjpake_operation_setup(psa_pake_operation_t *operation,
39 psa_pake_cipher_suite_t *cipher_suite,
40 psa_pake_role_t role,
41 mbedtls_svc_key_id_t key,
42 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010043{
Gilles Peskine449bd832023-01-11 14:50:10 +010044 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 if (key_available) {
51 PSA_ASSERT(psa_pake_set_password_key(operation, key));
52 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010053 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010054exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010055 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010056}
57#endif
58
Jaeden Amerof24c7f82018-06-27 17:20:43 +010059/** An invalid export length that will never be set by psa_export_key(). */
60static const size_t INVALID_EXPORT_LENGTH = ~0U;
61
Gilles Peskinea7aa4422018-08-14 15:17:54 +020062/** Test if a buffer contains a constant byte value.
63 *
64 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020065 *
66 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020067 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020068 * \param size Size of the buffer in bytes.
69 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020070 * \return 1 if the buffer is all-bits-zero.
71 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020072 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020074{
75 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 for (i = 0; i < size; i++) {
77 if (((unsigned char *) buffer)[i] != c) {
78 return 0;
79 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020080 }
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020082}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010083#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020084/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int asn1_write_10x(unsigned char **p,
86 unsigned char *start,
87 size_t bits,
88 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020089{
90 int ret;
91 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (bits == 0) {
93 return MBEDTLS_ERR_ASN1_INVALID_DATA;
94 }
95 if (bits <= 8 && x >= 1 << (bits - 1)) {
96 return MBEDTLS_ERR_ASN1_INVALID_DATA;
97 }
98 if (*p < start || *p - start < (ptrdiff_t) len) {
99 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
100 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200101 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 (*p)[len-1] = x;
103 if (bits % 8 == 0) {
104 (*p)[1] |= 1;
105 } else {
106 (*p)[0] |= 1 << (bits % 8);
107 }
108 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
109 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
110 MBEDTLS_ASN1_INTEGER));
111 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200112}
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114static int construct_fake_rsa_key(unsigned char *buffer,
115 size_t buffer_size,
116 unsigned char **p,
117 size_t bits,
118 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200119{
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200121 int ret;
122 int len = 0;
123 /* Construct something that looks like a DER encoding of
124 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
125 * RSAPrivateKey ::= SEQUENCE {
126 * version Version,
127 * modulus INTEGER, -- n
128 * publicExponent INTEGER, -- e
129 * privateExponent INTEGER, -- d
130 * prime1 INTEGER, -- p
131 * prime2 INTEGER, -- q
132 * exponent1 INTEGER, -- d mod (p-1)
133 * exponent2 INTEGER, -- d mod (q-1)
134 * coefficient INTEGER, -- (inverse of q) mod p
135 * otherPrimeInfos OtherPrimeInfos OPTIONAL
136 * }
137 * Or, for a public key, the same structure with only
138 * version, modulus and publicExponent.
139 */
140 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (keypair) {
142 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
143 asn1_write_10x(p, buffer, half_bits, 1));
144 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* q */
149 asn1_write_10x(p, buffer, half_bits, 1));
150 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
151 asn1_write_10x(p, buffer, half_bits, 3));
152 MBEDTLS_ASN1_CHK_ADD(len, /* d */
153 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200154 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
156 asn1_write_10x(p, buffer, 17, 1));
157 MBEDTLS_ASN1_CHK_ADD(len, /* n */
158 asn1_write_10x(p, buffer, bits, 1));
159 if (keypair) {
160 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
161 mbedtls_asn1_write_int(p, buffer, 0));
162 }
163 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200164 {
165 const unsigned char tag =
166 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200168 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100171#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173int exercise_mac_setup(psa_key_type_t key_type,
174 const unsigned char *key_bytes,
175 size_t key_length,
176 psa_algorithm_t alg,
177 psa_mac_operation_t *operation,
178 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100179{
Ronald Cron5425a212020-08-04 14:58:35 +0200180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
184 psa_set_key_algorithm(&attributes, alg);
185 psa_set_key_type(&attributes, key_type);
186 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100189 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100191 /* If setup failed, reproduce the failure, so that the caller can
192 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (*status != PSA_SUCCESS) {
194 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 }
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 psa_destroy_key(key);
198 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100199
200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 psa_destroy_key(key);
202 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203}
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205int exercise_cipher_setup(psa_key_type_t key_type,
206 const unsigned char *key_bytes,
207 size_t key_length,
208 psa_algorithm_t alg,
209 psa_cipher_operation_t *operation,
210 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100211{
Ronald Cron5425a212020-08-04 14:58:35 +0200212 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
216 psa_set_key_algorithm(&attributes, alg);
217 psa_set_key_type(&attributes, key_type);
218 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100221 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100223 /* If setup failed, reproduce the failure, so that the caller can
224 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (*status != PSA_SUCCESS) {
226 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
227 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100228 }
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 psa_destroy_key(key);
231 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100232
233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 psa_destroy_key(key);
235 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100236}
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239{
240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 uint8_t buffer[1];
243 size_t length;
244 int ok = 0;
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 psa_set_key_id(&attributes, key_id);
247 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
248 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
249 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
250 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
251 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200254 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
256 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
257 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
258 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
259 TEST_EQUAL(psa_get_key_type(&attributes), 0);
260 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
263 PSA_ERROR_INVALID_HANDLE);
264 TEST_EQUAL(psa_export_public_key(key,
265 buffer, sizeof(buffer), &length),
266 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200267
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200268 ok = 1;
269
270exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100271 /*
272 * Key attributes may have been returned by psa_get_key_attributes()
273 * thus reset them as required.
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200278}
279
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200280/* Assert that a key isn't reported as having a slot number. */
281#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100282#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200283 do \
284 { \
285 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 TEST_EQUAL(psa_get_key_slot_number( \
287 attributes, \
288 &ASSERT_NO_SLOT_NUMBER_slot_number), \
289 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200290 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200292#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293#define ASSERT_NO_SLOT_NUMBER(attributes) \
294 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200295#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
296
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100297/* An overapproximation of the amount of storage needed for a key of the
298 * given type and with the given content. The API doesn't make it easy
299 * to find a good value for the size. The current implementation doesn't
300 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301#define KEY_BITS_FROM_DATA(type, data) \
302 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100303
Darryl Green0c6575a2018-11-07 16:05:30 +0000304typedef enum {
305 IMPORT_KEY = 0,
306 GENERATE_KEY = 1,
307 DERIVE_KEY = 2
308} generate_method;
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100311 DO_NOT_SET_LENGTHS = 0,
312 SET_LENGTHS_BEFORE_NONCE = 1,
313 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100314} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100317 USE_NULL_TAG = 0,
318 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100319} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100320
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100321/*!
322 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 * \param key_type_arg Type of key passed in
324 * \param key_data The encryption / decryption key data
325 * \param alg_arg The type of algorithm used
326 * \param nonce Nonce data
327 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100328 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100329 * feed additional data in to be encrypted /
330 * decrypted. If -1, no chunking.
331 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100332 * \param data_part_len_arg If not -1, the length of chunks to feed
333 * the data in to be encrypted / decrypted. If
334 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100335 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100336 * expected here, this controls whether or not
337 * to set lengths, and in what order with
338 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100339 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100341 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100342 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100343 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100344 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
346 int alg_arg,
347 data_t *nonce,
348 data_t *additional_data,
349 int ad_part_len_arg,
350 data_t *input_data,
351 int data_part_len_arg,
352 set_lengths_method_t set_lengths_method,
353 data_t *expected_output,
354 int is_encrypt,
355 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100356{
357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
358 psa_key_type_t key_type = key_type_arg;
359 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100360 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100361 unsigned char *output_data = NULL;
362 unsigned char *part_data = NULL;
363 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100364 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100366 size_t output_size = 0;
367 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100368 size_t output_length = 0;
369 size_t key_bits = 0;
370 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100371 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100372 size_t part_length = 0;
373 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100374 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100375 size_t ad_part_len = 0;
376 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100377 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
379 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100382 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (is_encrypt) {
387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
388 } else {
389 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100391
392 psa_set_key_algorithm(&attributes, alg);
393 psa_set_key_type(&attributes, key_type);
394
395 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
396 &key));
397
398 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
399 key_bits = psa_get_key_bits(&attributes);
400
401 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
402
403 if (is_encrypt) {
404 /* Tag gets written at end of buffer. */
405 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
406 (input_data->len +
407 tag_length));
408 data_true_size = input_data->len;
409 } else {
410 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
411 (input_data->len -
412 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100414 /* Do not want to attempt to decrypt tag. */
415 data_true_size = input_data->len - tag_length;
416 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 ASSERT_ALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (is_encrypt) {
421 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
422 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
423 } else {
424 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
425 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100426 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 ASSERT_ALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (is_encrypt) {
431 status = psa_aead_encrypt_setup(&operation, key, alg);
432 } else {
433 status = psa_aead_decrypt_setup(&operation, key, alg);
434 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100435
436 /* If the operation is not supported, just skip and not fail in case the
437 * encryption involves a common limitation of cryptography hardwares and
438 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (status == PSA_ERROR_NOT_SUPPORTED) {
440 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
441 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 }
443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
447 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
448 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
449 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
450 data_true_size));
451 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
452 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
453 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
456 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100460 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100461 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100464 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 part_offset += part_length, part_count++) {
466 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100467 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100469 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100471 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100472 }
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 PSA_ASSERT(psa_aead_update_ad(&operation,
475 additional_data->x + part_offset,
476 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100477
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
482 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 }
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100486 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 data_part_len = (size_t) data_part_len_arg;
488 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
489 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 ASSERT_ALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100494 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 part_offset += part_length, part_count++) {
496 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100497 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 } else if ((data_true_size - part_offset) < data_part_len) {
499 part_length = (data_true_size - part_offset);
500 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100501 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100502 }
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 PSA_ASSERT(psa_aead_update(&operation,
505 (input_data->x + part_offset),
506 part_length, part_data,
507 part_data_size,
508 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (output_data && output_part_length) {
511 memcpy((output_data + output_length), part_data,
512 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 }
514
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 output_length += output_part_length;
516 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100518 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
520 data_true_size, output_data,
521 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 }
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (is_encrypt) {
525 PSA_ASSERT(psa_aead_finish(&operation, final_data,
526 final_output_size,
527 &output_part_length,
528 tag_buffer, tag_length,
529 &tag_size));
530 } else {
531 PSA_ASSERT(psa_aead_verify(&operation, final_data,
532 final_output_size,
533 &output_part_length,
534 (input_data->x + data_true_size),
535 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100536 }
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (output_data && output_part_length) {
539 memcpy((output_data + output_length), final_data,
540 output_part_length);
541 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100542
543 output_length += output_part_length;
544
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100545
546 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
547 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (is_encrypt) {
549 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (output_data && tag_length) {
552 memcpy((output_data + output_length), tag_buffer,
553 tag_length);
554 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100555
556 output_length += tag_length;
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 TEST_EQUAL(output_length,
559 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
560 input_data->len));
561 TEST_LE_U(output_length,
562 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
563 } else {
564 TEST_EQUAL(output_length,
565 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
566 input_data->len));
567 TEST_LE_U(output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100569 }
570
Paul Elliottd3f82412021-06-16 16:52:21 +0100571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 ASSERT_COMPARE(expected_output->x, expected_output->len,
573 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliottd3f82412021-06-16 16:52:21 +0100575
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100576 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100577
578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 psa_destroy_key(key);
580 psa_aead_abort(&operation);
581 mbedtls_free(output_data);
582 mbedtls_free(part_data);
583 mbedtls_free(final_data);
584 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100587}
588
Neil Armstrong4766f992022-02-28 16:23:59 +0100589/*!
590 * \brief Internal Function for MAC multipart tests.
591 * \param key_type_arg Type of key passed in
592 * \param key_data The encryption / decryption key data
593 * \param alg_arg The type of algorithm used
594 * \param input_data Data to encrypt / decrypt
595 * \param data_part_len_arg If not -1, the length of chunks to feed
596 * the data in to be encrypted / decrypted. If
597 * -1, no chunking
598 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000599 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100600 * \param do_zero_parts If non-zero, interleave zero length chunks
601 * with normal length chunks.
602 * \return int Zero on failure, non-zero on success.
603 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
605 int alg_arg,
606 data_t *input_data,
607 int data_part_len_arg,
608 data_t *expected_output,
609 int is_verify,
610 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100611{
612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
613 psa_key_type_t key_type = key_type_arg;
614 psa_algorithm_t alg = alg_arg;
615 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
616 unsigned char mac[PSA_MAC_MAX_SIZE];
617 size_t part_offset = 0;
618 size_t part_length = 0;
619 size_t data_part_len = 0;
620 size_t mac_len = 0;
621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
622 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
623
624 int test_ok = 0;
625 size_t part_count = 0;
626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (is_verify) {
630 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
631 } else {
632 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
633 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 psa_set_key_algorithm(&attributes, alg);
636 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
639 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (is_verify) {
642 status = psa_mac_verify_setup(&operation, key, alg);
643 } else {
644 status = psa_mac_sign_setup(&operation, key, alg);
645 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100650 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100654 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 part_offset += part_length, part_count++) {
656 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100657 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 } else if ((input_data->len - part_offset) < data_part_len) {
659 part_length = (input_data->len - part_offset);
660 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100661 part_length = data_part_len;
662 }
663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 PSA_ASSERT(psa_mac_update(&operation,
665 (input_data->x + part_offset),
666 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100667 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100669 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
671 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100672 }
673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (is_verify) {
675 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
676 expected_output->len));
677 } else {
678 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
679 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 ASSERT_COMPARE(expected_output->x, expected_output->len,
682 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100683 }
684
685 test_ok = 1;
686
687exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 psa_destroy_key(key);
689 psa_mac_abort(&operation);
690 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100693}
694
Neil Armstrong75673ab2022-06-15 17:39:01 +0200695#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100696static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
697 psa_pake_operation_t *server,
698 psa_pake_operation_t *client,
699 int client_input_first,
700 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200701{
702 unsigned char *buffer0 = NULL, *buffer1 = NULL;
703 size_t buffer_length = (
704 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
705 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
706 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200707 /* The output should be exactly this size according to the spec */
708 const size_t expected_size_key_share =
709 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
710 /* The output should be exactly this size according to the spec */
711 const size_t expected_size_zk_public =
712 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
713 /* The output can be smaller: the spec allows stripping leading zeroes */
714 const size_t max_expected_size_zk_proof =
715 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200716 size_t buffer0_off = 0;
717 size_t buffer1_off = 0;
718 size_t s_g1_len, s_g2_len, s_a_len;
719 size_t s_g1_off, s_g2_off, s_a_off;
720 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
721 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
722 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
723 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
724 size_t c_g1_len, c_g2_len, c_a_len;
725 size_t c_g1_off, c_g2_off, c_a_off;
726 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
727 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
728 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
729 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
730 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200731 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 ASSERT_ALLOC(buffer0, buffer_length);
734 ASSERT_ALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737 case 1:
738 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
740 buffer0 + buffer0_off,
741 512 - buffer0_off, &s_g1_len));
742 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200743 s_g1_off = buffer0_off;
744 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
746 buffer0 + buffer0_off,
747 512 - buffer0_off, &s_x1_pk_len));
748 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200749 s_x1_pk_off = buffer0_off;
750 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
752 buffer0 + buffer0_off,
753 512 - buffer0_off, &s_x1_pr_len));
754 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200755 s_x1_pr_off = buffer0_off;
756 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
758 buffer0 + buffer0_off,
759 512 - buffer0_off, &s_g2_len));
760 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200761 s_g2_off = buffer0_off;
762 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
764 buffer0 + buffer0_off,
765 512 - buffer0_off, &s_x2_pk_len));
766 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200767 s_x2_pk_off = buffer0_off;
768 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
770 buffer0 + buffer0_off,
771 512 - buffer0_off, &s_x2_pr_len));
772 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200773 s_x2_pr_off = buffer0_off;
774 buffer0_off += s_x2_pr_len;
775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500777 buffer0[s_x1_pr_off + 8] ^= 1;
778 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200779 expected_status = PSA_ERROR_DATA_INVALID;
780 }
781
Neil Armstrong51009d72022-09-05 17:59:54 +0200782 /*
783 * When injecting errors in inputs, the implementation is
784 * free to detect it right away of with a delay.
785 * This permits delaying the error until the end of the input
786 * sequence, if no error appears then, this will be treated
787 * as an error.
788 */
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200791 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
793 buffer0 + s_g1_off, s_g1_len);
794 if (inject_error == 1 && status != PSA_SUCCESS) {
795 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200796 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 } else {
798 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200799 }
800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
802 buffer0 + s_x1_pk_off,
803 s_x1_pk_len);
804 if (inject_error == 1 && status != PSA_SUCCESS) {
805 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200806 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 } else {
808 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200809 }
810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
812 buffer0 + s_x1_pr_off,
813 s_x1_pr_len);
814 if (inject_error == 1 && status != PSA_SUCCESS) {
815 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200816 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 } else {
818 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200819 }
820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
822 buffer0 + s_g2_off,
823 s_g2_len);
824 if (inject_error == 1 && status != PSA_SUCCESS) {
825 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200826 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 } else {
828 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200829 }
830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
832 buffer0 + s_x2_pk_off,
833 s_x2_pk_len);
834 if (inject_error == 1 && status != PSA_SUCCESS) {
835 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200836 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 } else {
838 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200839 }
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
842 buffer0 + s_x2_pr_off,
843 s_x2_pr_len);
844 if (inject_error == 1 && status != PSA_SUCCESS) {
845 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200846 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 } else {
848 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200849 }
850
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200851 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (inject_error == 1) {
853 TEST_ASSERT(
854 !"One of the last psa_pake_input() calls should have returned the expected error.");
855 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200856 }
857
858 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
860 buffer1 + buffer1_off,
861 512 - buffer1_off, &c_g1_len));
862 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200863 c_g1_off = buffer1_off;
864 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
866 buffer1 + buffer1_off,
867 512 - buffer1_off, &c_x1_pk_len));
868 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200869 c_x1_pk_off = buffer1_off;
870 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
872 buffer1 + buffer1_off,
873 512 - buffer1_off, &c_x1_pr_len));
874 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200875 c_x1_pr_off = buffer1_off;
876 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
878 buffer1 + buffer1_off,
879 512 - buffer1_off, &c_g2_len));
880 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200881 c_g2_off = buffer1_off;
882 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
884 buffer1 + buffer1_off,
885 512 - buffer1_off, &c_x2_pk_len));
886 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200887 c_x2_pk_off = buffer1_off;
888 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
890 buffer1 + buffer1_off,
891 512 - buffer1_off, &c_x2_pr_len));
892 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200893 c_x2_pr_off = buffer1_off;
894 buffer1_off += c_x2_pr_len;
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200897 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
899 buffer0 + s_g1_off, s_g1_len);
900 if (inject_error == 1 && status != PSA_SUCCESS) {
901 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200902 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 } else {
904 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200905 }
906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
908 buffer0 + s_x1_pk_off,
909 s_x1_pk_len);
910 if (inject_error == 1 && status != PSA_SUCCESS) {
911 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200912 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 } else {
914 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200915 }
916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
918 buffer0 + s_x1_pr_off,
919 s_x1_pr_len);
920 if (inject_error == 1 && status != PSA_SUCCESS) {
921 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200922 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 } else {
924 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200925 }
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
928 buffer0 + s_g2_off,
929 s_g2_len);
930 if (inject_error == 1 && status != PSA_SUCCESS) {
931 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200932 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 } else {
934 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200935 }
936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
938 buffer0 + s_x2_pk_off,
939 s_x2_pk_len);
940 if (inject_error == 1 && status != PSA_SUCCESS) {
941 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200942 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 } else {
944 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200945 }
946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
948 buffer0 + s_x2_pr_off,
949 s_x2_pr_len);
950 if (inject_error == 1 && status != PSA_SUCCESS) {
951 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200952 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 } else {
954 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200955 }
956
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200957 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (inject_error == 1) {
959 TEST_ASSERT(
960 !"One of the last psa_pake_input() calls should have returned the expected error.");
961 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200962 }
963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500965 buffer1[c_x1_pr_off + 12] ^= 1;
966 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200967 expected_status = PSA_ERROR_DATA_INVALID;
968 }
969
970 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
972 buffer1 + c_g1_off, c_g1_len);
973 if (inject_error == 2 && status != PSA_SUCCESS) {
974 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200975 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 } else {
977 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200978 }
979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
981 buffer1 + c_x1_pk_off, c_x1_pk_len);
982 if (inject_error == 2 && status != PSA_SUCCESS) {
983 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200984 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 } else {
986 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200987 }
988
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
990 buffer1 + c_x1_pr_off, c_x1_pr_len);
991 if (inject_error == 2 && status != PSA_SUCCESS) {
992 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200993 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 } else {
995 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200996 }
997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
999 buffer1 + c_g2_off, c_g2_len);
1000 if (inject_error == 2 && status != PSA_SUCCESS) {
1001 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001002 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 } else {
1004 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001005 }
1006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1008 buffer1 + c_x2_pk_off, c_x2_pk_len);
1009 if (inject_error == 2 && status != PSA_SUCCESS) {
1010 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001011 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 } else {
1013 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001014 }
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1017 buffer1 + c_x2_pr_off, c_x2_pr_len);
1018 if (inject_error == 2 && status != PSA_SUCCESS) {
1019 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001020 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 } else {
1022 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001023 }
1024
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001025 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (inject_error == 2) {
1027 TEST_ASSERT(
1028 !"One of the last psa_pake_input() calls should have returned the expected error.");
1029 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001030
1031 break;
1032
1033 case 2:
1034 /* Server second round Output */
1035 buffer0_off = 0;
1036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1038 buffer0 + buffer0_off,
1039 512 - buffer0_off, &s_a_len));
1040 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001041 s_a_off = buffer0_off;
1042 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1044 buffer0 + buffer0_off,
1045 512 - buffer0_off, &s_x2s_pk_len));
1046 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001047 s_x2s_pk_off = buffer0_off;
1048 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1050 buffer0 + buffer0_off,
1051 512 - buffer0_off, &s_x2s_pr_len));
1052 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001053 s_x2s_pr_off = buffer0_off;
1054 buffer0_off += s_x2s_pr_len;
1055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001057 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001058 expected_status = PSA_ERROR_DATA_INVALID;
1059 }
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001062 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1064 buffer0 + s_a_off, s_a_len);
1065 if (inject_error == 3 && status != PSA_SUCCESS) {
1066 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001067 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 } else {
1069 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001070 }
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1073 buffer0 + s_x2s_pk_off,
1074 s_x2s_pk_len);
1075 if (inject_error == 3 && status != PSA_SUCCESS) {
1076 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001077 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 } else {
1079 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001080 }
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1083 buffer0 + s_x2s_pr_off,
1084 s_x2s_pr_len);
1085 if (inject_error == 3 && status != PSA_SUCCESS) {
1086 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001087 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 } else {
1089 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001090 }
1091
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001092 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (inject_error == 3) {
1094 TEST_ASSERT(
1095 !"One of the last psa_pake_input() calls should have returned the expected error.");
1096 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001097 }
1098
1099 /* Client second round Output */
1100 buffer1_off = 0;
1101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1103 buffer1 + buffer1_off,
1104 512 - buffer1_off, &c_a_len));
1105 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001106 c_a_off = buffer1_off;
1107 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1109 buffer1 + buffer1_off,
1110 512 - buffer1_off, &c_x2s_pk_len));
1111 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001112 c_x2s_pk_off = buffer1_off;
1113 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1115 buffer1 + buffer1_off,
1116 512 - buffer1_off, &c_x2s_pr_len));
1117 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001118 c_x2s_pr_off = buffer1_off;
1119 buffer1_off += c_x2s_pr_len;
1120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001122 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1124 buffer0 + s_a_off, s_a_len);
1125 if (inject_error == 3 && status != PSA_SUCCESS) {
1126 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001127 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 } else {
1129 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001130 }
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1133 buffer0 + s_x2s_pk_off,
1134 s_x2s_pk_len);
1135 if (inject_error == 3 && status != PSA_SUCCESS) {
1136 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001137 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 } else {
1139 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001140 }
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1143 buffer0 + s_x2s_pr_off,
1144 s_x2s_pr_len);
1145 if (inject_error == 3 && status != PSA_SUCCESS) {
1146 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001147 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 } else {
1149 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001150 }
1151
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001152 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (inject_error == 3) {
1154 TEST_ASSERT(
1155 !"One of the last psa_pake_input() calls should have returned the expected error.");
1156 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001157 }
1158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001160 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001161 expected_status = PSA_ERROR_DATA_INVALID;
1162 }
1163
1164 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1166 buffer1 + c_a_off, c_a_len);
1167 if (inject_error == 4 && status != PSA_SUCCESS) {
1168 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001169 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 } else {
1171 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001172 }
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1175 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1176 if (inject_error == 4 && status != PSA_SUCCESS) {
1177 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001178 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 } else {
1180 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001181 }
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1184 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1185 if (inject_error == 4 && status != PSA_SUCCESS) {
1186 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001187 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 } else {
1189 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001190 }
1191
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001192 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (inject_error == 4) {
1194 TEST_ASSERT(
1195 !"One of the last psa_pake_input() calls should have returned the expected error.");
1196 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001197
1198 break;
1199
1200 }
1201
Neil Armstrongf983caf2022-06-15 15:27:48 +02001202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 mbedtls_free(buffer0);
1204 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001205}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001206#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001209 INJECT_ERR_NONE = 0,
1210 INJECT_ERR_UNINITIALIZED_ACCESS,
1211 INJECT_ERR_DUPLICATE_SETUP,
1212 INJECT_ERR_INVALID_USER,
1213 INJECT_ERR_INVALID_PEER,
1214 INJECT_ERR_SET_USER,
1215 INJECT_ERR_SET_PEER,
1216 INJECT_EMPTY_IO_BUFFER,
1217 INJECT_UNKNOWN_STEP,
1218 INJECT_INVALID_FIRST_STEP,
1219 INJECT_WRONG_BUFFER_SIZE,
1220 INJECT_VALID_OPERATION_AFTER_FAILURE,
1221 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1222 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1223} ecjpake_injected_failure_t;
1224
Paul Elliott01885fa2023-02-09 12:07:30 +00001225#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001226
Paul Elliott6f600372023-02-06 18:41:05 +00001227static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1228 psa_status_t expected_status,
1229 size_t *min_completes,
1230 size_t *max_completes)
1231{
1232
1233 /* This is slightly contrived, but we only really know that with a minimum
1234 value of max_ops that a successful operation should take more than one op
1235 to complete, and likewise that with a max_ops of
1236 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1237 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001238
Paul Elliott6f600372023-02-06 18:41:05 +00001239 if (expected_status == PSA_SUCCESS) {
1240 *min_completes = 2;
1241 } else {
1242 *min_completes = 1;
1243 }
1244
1245 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1246 } else {
1247 *min_completes = 1;
1248 *max_completes = 1;
1249 }
1250}
Paul Elliott01885fa2023-02-09 12:07:30 +00001251#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001252
Gilles Peskinee59236f2018-01-27 23:32:46 +01001253/* END_HEADER */
1254
1255/* BEGIN_DEPENDENCIES
1256 * depends_on:MBEDTLS_PSA_CRYPTO_C
1257 * END_DEPENDENCIES
1258 */
1259
1260/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001261void psa_can_do_hash()
1262{
1263 /* We can't test that this is specific to drivers until partial init has
1264 * been implemented, but we can at least test before/after full init. */
1265 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1266 PSA_INIT();
1267 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1268 PSA_DONE();
1269}
1270/* END_CASE */
1271
1272/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001273void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001274{
1275 size_t max_truncated_mac_size =
1276 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1277
1278 /* Check that the length for a truncated MAC always fits in the algorithm
1279 * encoding. The shifted mask is the maximum truncated value. The
1280 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001282}
1283/* END_CASE */
1284
1285/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286void import_with_policy(int type_arg,
1287 int usage_arg, int alg_arg,
1288 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001289{
1290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1291 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001292 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001293 psa_key_type_t type = type_arg;
1294 psa_key_usage_t usage = usage_arg;
1295 psa_algorithm_t alg = alg_arg;
1296 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001298 psa_status_t status;
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 psa_set_key_type(&attributes, type);
1303 psa_set_key_usage_flags(&attributes, usage);
1304 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 status = psa_import_key(&attributes,
1307 key_material, sizeof(key_material),
1308 &key);
1309 TEST_EQUAL(status, expected_status);
1310 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001311 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1315 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1316 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1317 mbedtls_test_update_key_usage_flags(usage));
1318 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1319 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 PSA_ASSERT(psa_destroy_key(key));
1322 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001323
1324exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001325 /*
1326 * Key attributes may have been returned by psa_get_key_attributes()
1327 * thus reset them as required.
1328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 psa_destroy_key(key);
1332 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001333}
1334/* END_CASE */
1335
1336/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337void import_with_data(data_t *data, int type_arg,
1338 int attr_bits_arg,
1339 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001340{
1341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1342 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001344 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001345 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001346 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001347 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 psa_set_key_type(&attributes, type);
1352 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 status = psa_import_key(&attributes, data->x, data->len, &key);
1355 TEST_EQUAL(status, expected_status);
1356 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001357 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1361 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1362 if (attr_bits != 0) {
1363 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1364 }
1365 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 PSA_ASSERT(psa_destroy_key(key));
1368 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369
1370exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001371 /*
1372 * Key attributes may have been returned by psa_get_key_attributes()
1373 * thus reset them as required.
1374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 psa_destroy_key(key);
1378 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001379}
1380/* END_CASE */
1381
1382/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001383/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001384void import_large_key(int type_arg, int byte_size_arg,
1385 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001386{
1387 psa_key_type_t type = type_arg;
1388 size_t byte_size = byte_size_arg;
1389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1390 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001391 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001392 psa_status_t status;
1393 uint8_t *buffer = NULL;
1394 size_t buffer_size = byte_size + 1;
1395 size_t n;
1396
Steven Cooreman69967ce2021-01-18 18:01:08 +01001397 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001398 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1400 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001403
1404 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1406 psa_set_key_type(&attributes, type);
1407 status = psa_import_key(&attributes, buffer, byte_size, &key);
1408 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1409 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (status == PSA_SUCCESS) {
1412 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1413 TEST_EQUAL(psa_get_key_type(&attributes), type);
1414 TEST_EQUAL(psa_get_key_bits(&attributes),
1415 PSA_BYTES_TO_BITS(byte_size));
1416 ASSERT_NO_SLOT_NUMBER(&attributes);
1417 memset(buffer, 0, byte_size + 1);
1418 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1419 for (n = 0; n < byte_size; n++) {
1420 TEST_EQUAL(buffer[n], 'K');
1421 }
1422 for (n = byte_size; n < buffer_size; n++) {
1423 TEST_EQUAL(buffer[n], 0);
1424 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001425 }
1426
1427exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001428 /*
1429 * Key attributes may have been returned by psa_get_key_attributes()
1430 * thus reset them as required.
1431 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 psa_destroy_key(key);
1435 PSA_DONE();
1436 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001437}
1438/* END_CASE */
1439
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001440/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001441/* Import an RSA key with a valid structure (but not valid numbers
1442 * inside, beyond having sensible size and parity). This is expected to
1443 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001444void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001445{
Ronald Cron5425a212020-08-04 14:58:35 +02001446 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001447 size_t bits = bits_arg;
1448 psa_status_t expected_status = expected_status_arg;
1449 psa_status_t status;
1450 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001451 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001452 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001454 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001455 unsigned char *p;
1456 int ret;
1457 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 PSA_ASSERT(psa_crypto_init());
1461 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1464 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001465 length = ret;
1466
1467 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 psa_set_key_type(&attributes, type);
1469 status = psa_import_key(&attributes, p, length, &key);
1470 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (status == PSA_SUCCESS) {
1473 PSA_ASSERT(psa_destroy_key(key));
1474 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001475
1476exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 mbedtls_free(buffer);
1478 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001479}
1480/* END_CASE */
1481
1482/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483void import_export(data_t *data,
1484 int type_arg,
1485 int usage_arg, int alg_arg,
1486 int lifetime_arg,
1487 int expected_bits,
1488 int export_size_delta,
1489 int expected_export_status_arg,
1490 /*whether reexport must give the original input exactly*/
1491 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001492{
Ronald Cron5425a212020-08-04 14:58:35 +02001493 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001494 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001495 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001496 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001497 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301498 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001499 unsigned char *exported = NULL;
1500 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001501 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001502 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001505 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001506 psa_status_t expected_import_result = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001507
Moran Pekercb088e72018-07-17 17:36:59 +03001508 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 ASSERT_ALLOC(exported, export_size);
1510 if (!canonical_input) {
1511 ASSERT_ALLOC(reexported, export_size);
1512 }
1513 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 psa_set_key_lifetime(&attributes, lifetime);
1516 psa_set_key_usage_flags(&attributes, usage_arg);
1517 psa_set_key_algorithm(&attributes, alg);
1518 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001519
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001520 if (PSA_KEY_TYPE_IS_DH(type) &&
1521 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001522 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1523 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001524 }
1525
1526 if (PSA_KEY_TYPE_IS_DH(type) &&
1527 (data->len != 256 && data->len != 384 &&
1528 data->len != 512 && data->len != 768 && data->len != 1024)) {
1529 expected_import_result = PSA_ERROR_INVALID_ARGUMENT;
1530 }
1531
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001533 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
1534 expected_import_result);
1535
1536 if (expected_import_result != PSA_SUCCESS) {
1537 goto exit;
1538 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001539
1540 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1542 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1543 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1544 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545
1546 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 status = psa_export_key(key, exported, export_size, &exported_length);
1548 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001549
1550 /* The exported length must be set by psa_export_key() to a value between 0
1551 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1553 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1554 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1557 export_size - exported_length));
1558 if (status != PSA_SUCCESS) {
1559 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001560 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001561 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001562
Gilles Peskineea38a922021-02-13 00:05:16 +01001563 /* Run sanity checks on the exported key. For non-canonical inputs,
1564 * this validates the canonical representations. For canonical inputs,
1565 * this doesn't directly validate the implementation, but it still helps
1566 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 if (!psa_key_lifetime_is_external(lifetime)) {
1568 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301569 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 }
Archana4d7ae1d2021-07-07 02:50:22 +05301571 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if (canonical_input) {
1574 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1575 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001576 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1578 &key2));
1579 PSA_ASSERT(psa_export_key(key2,
1580 reexported,
1581 export_size,
1582 &reexported_length));
1583 ASSERT_COMPARE(exported, exported_length,
1584 reexported, reexported_length);
1585 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001586 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 TEST_LE_U(exported_length,
1588 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1589 psa_get_key_bits(&got_attributes)));
1590 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591
1592destroy:
1593 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 PSA_ASSERT(psa_destroy_key(key));
1595 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001596
1597exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001598 /*
1599 * Key attributes may have been returned by psa_get_key_attributes()
1600 * thus reset them as required.
1601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 psa_reset_key_attributes(&got_attributes);
1603 psa_destroy_key(key);
1604 mbedtls_free(exported);
1605 mbedtls_free(reexported);
1606 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001607}
1608/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001609
Moran Pekerf709f4a2018-06-06 17:26:04 +03001610/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611void import_export_public_key(data_t *data,
1612 int type_arg, // key pair or public key
1613 int alg_arg,
1614 int lifetime_arg,
1615 int export_size_delta,
1616 int expected_export_status_arg,
1617 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001618{
Ronald Cron5425a212020-08-04 14:58:35 +02001619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001620 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001621 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001622 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001623 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301624 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001625 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001626 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001627 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001628 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 psa_set_key_lifetime(&attributes, lifetime);
1633 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1634 psa_set_key_algorithm(&attributes, alg);
1635 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001636
1637 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001639
Gilles Peskine49c25912018-10-29 15:15:31 +01001640 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 ASSERT_ALLOC(exported, export_size);
1642 status = psa_export_public_key(key,
1643 exported, export_size,
1644 &exported_length);
1645 TEST_EQUAL(status, expected_export_status);
1646 if (status == PSA_SUCCESS) {
1647 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001648 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1650 bits = psa_get_key_bits(&attributes);
1651 TEST_LE_U(expected_public_key->len,
1652 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1653 TEST_LE_U(expected_public_key->len,
1654 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1655 TEST_LE_U(expected_public_key->len,
1656 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1657 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1658 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001659 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001660exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001661 /*
1662 * Key attributes may have been returned by psa_get_key_attributes()
1663 * thus reset them as required.
1664 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 mbedtls_free(exported);
1668 psa_destroy_key(key);
1669 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001670}
1671/* END_CASE */
1672
Gilles Peskine20035e32018-02-03 22:44:14 +01001673/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001674void import_and_exercise_key(data_t *data,
1675 int type_arg,
1676 int bits_arg,
1677 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001678{
Ronald Cron5425a212020-08-04 14:58:35 +02001679 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001680 psa_key_type_t type = type_arg;
1681 size_t bits = bits_arg;
1682 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001685 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 psa_set_key_usage_flags(&attributes, usage);
1690 psa_set_key_algorithm(&attributes, alg);
1691 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001692
1693 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001695
1696 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1698 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1699 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001700
1701 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001703 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 PSA_ASSERT(psa_destroy_key(key));
1707 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001708
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001709exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001710 /*
1711 * Key attributes may have been returned by psa_get_key_attributes()
1712 * thus reset them as required.
1713 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 psa_reset_key_attributes(&attributes);
1717 psa_destroy_key(key);
1718 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001719}
1720/* END_CASE */
1721
1722/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001723void effective_key_attributes(int type_arg, int expected_type_arg,
1724 int bits_arg, int expected_bits_arg,
1725 int usage_arg, int expected_usage_arg,
1726 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001727{
Ronald Cron5425a212020-08-04 14:58:35 +02001728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001729 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001730 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001731 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001732 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001733 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001734 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001735 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001736 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001737 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 psa_set_key_usage_flags(&attributes, usage);
1742 psa_set_key_algorithm(&attributes, alg);
1743 psa_set_key_type(&attributes, key_type);
1744 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 PSA_ASSERT(psa_generate_key(&attributes, &key));
1747 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1750 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1751 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1752 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1753 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001754
1755exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001756 /*
1757 * Key attributes may have been returned by psa_get_key_attributes()
1758 * thus reset them as required.
1759 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 psa_destroy_key(key);
1763 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001764}
1765/* END_CASE */
1766
1767/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001768void check_key_policy(int type_arg, int bits_arg,
1769 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001770{
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1772 usage_arg,
1773 mbedtls_test_update_key_usage_flags(usage_arg),
1774 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001775 goto exit;
1776}
1777/* END_CASE */
1778
1779/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001780void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001781{
1782 /* Test each valid way of initializing the object, except for `= {0}`, as
1783 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1784 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001785 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001787 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1788 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1793 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1794 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 TEST_EQUAL(psa_get_key_type(&func), 0);
1797 TEST_EQUAL(psa_get_key_type(&init), 0);
1798 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 TEST_EQUAL(psa_get_key_bits(&func), 0);
1801 TEST_EQUAL(psa_get_key_bits(&init), 0);
1802 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1805 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1806 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1809 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1810 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001811}
1812/* END_CASE */
1813
1814/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001815void mac_key_policy(int policy_usage_arg,
1816 int policy_alg_arg,
1817 int key_type_arg,
1818 data_t *key_data,
1819 int exercise_alg_arg,
1820 int expected_status_sign_arg,
1821 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001822{
Ronald Cron5425a212020-08-04 14:58:35 +02001823 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001825 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001826 psa_key_type_t key_type = key_type_arg;
1827 psa_algorithm_t policy_alg = policy_alg_arg;
1828 psa_algorithm_t exercise_alg = exercise_alg_arg;
1829 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001830 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001831 psa_status_t expected_status_sign = expected_status_sign_arg;
1832 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 psa_set_key_usage_flags(&attributes, policy_usage);
1838 psa_set_key_algorithm(&attributes, policy_alg);
1839 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1842 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1845 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1848 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001849
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001850 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001852 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1854 input, 128,
1855 mac, PSA_MAC_MAX_SIZE, &mac_len),
1856 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001857
Neil Armstrong3af9b972022-02-07 12:20:21 +01001858 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 PSA_ASSERT(psa_mac_abort(&operation));
1860 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1861 if (status == PSA_SUCCESS) {
1862 status = psa_mac_update(&operation, input, 128);
1863 if (status == PSA_SUCCESS) {
1864 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1865 &mac_len),
1866 expected_status_sign);
1867 } else {
1868 TEST_EQUAL(status, expected_status_sign);
1869 }
1870 } else {
1871 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001872 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001874
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001875 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 status = psa_mac_verify(key, exercise_alg, input, 128,
1877 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1880 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1881 } else {
1882 TEST_EQUAL(status, expected_status_verify);
1883 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001884
Neil Armstrong3af9b972022-02-07 12:20:21 +01001885 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1887 if (status == PSA_SUCCESS) {
1888 status = psa_mac_update(&operation, input, 128);
1889 if (status == PSA_SUCCESS) {
1890 status = psa_mac_verify_finish(&operation, mac, mac_len);
1891 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1892 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1893 } else {
1894 TEST_EQUAL(status, expected_status_verify);
1895 }
1896 } else {
1897 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001898 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 } else {
1900 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001901 }
1902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001904
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 memset(mac, 0, sizeof(mac));
1906 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1907 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001908
1909exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 psa_mac_abort(&operation);
1911 psa_destroy_key(key);
1912 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001913}
1914/* END_CASE */
1915
1916/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001917void cipher_key_policy(int policy_usage_arg,
1918 int policy_alg,
1919 int key_type,
1920 data_t *key_data,
1921 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922{
Ronald Cron5425a212020-08-04 14:58:35 +02001923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001925 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001926 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001927 size_t output_buffer_size = 0;
1928 size_t input_buffer_size = 0;
1929 size_t output_length = 0;
1930 uint8_t *output = NULL;
1931 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001932 psa_status_t status;
1933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1935 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1936 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 ASSERT_ALLOC(input, input_buffer_size);
1939 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 psa_set_key_usage_flags(&attributes, policy_usage);
1944 psa_set_key_algorithm(&attributes, policy_alg);
1945 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1948 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001949
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001950 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 TEST_EQUAL(policy_usage,
1952 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001953
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001954 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1956 output, output_buffer_size,
1957 &output_length);
1958 if (policy_alg == exercise_alg &&
1959 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1960 PSA_ASSERT(status);
1961 } else {
1962 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1963 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001964
1965 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1967 if (policy_alg == exercise_alg &&
1968 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1969 PSA_ASSERT(status);
1970 } else {
1971 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1972 }
1973 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001974
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001975 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1977 input, input_buffer_size,
1978 &output_length);
1979 if (policy_alg == exercise_alg &&
1980 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1981 PSA_ASSERT(status);
1982 } else {
1983 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1984 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001985
1986 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1988 if (policy_alg == exercise_alg &&
1989 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1990 PSA_ASSERT(status);
1991 } else {
1992 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1993 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994
1995exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 psa_cipher_abort(&operation);
1997 mbedtls_free(input);
1998 mbedtls_free(output);
1999 psa_destroy_key(key);
2000 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002001}
2002/* END_CASE */
2003
2004/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002005void aead_key_policy(int policy_usage_arg,
2006 int policy_alg,
2007 int key_type,
2008 data_t *key_data,
2009 int nonce_length_arg,
2010 int tag_length_arg,
2011 int exercise_alg,
2012 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013{
Ronald Cron5425a212020-08-04 14:58:35 +02002014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002016 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002017 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002018 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002019 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021 size_t nonce_length = nonce_length_arg;
2022 unsigned char tag[16];
2023 size_t tag_length = tag_length_arg;
2024 size_t output_length;
2025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 TEST_LE_U(nonce_length, sizeof(nonce));
2027 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 psa_set_key_usage_flags(&attributes, policy_usage);
2032 psa_set_key_algorithm(&attributes, policy_alg);
2033 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2036 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002038 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 TEST_EQUAL(policy_usage,
2040 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002041
Neil Armstrong752d8112022-02-07 14:51:11 +01002042 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 status = psa_aead_encrypt(key, exercise_alg,
2044 nonce, nonce_length,
2045 NULL, 0,
2046 NULL, 0,
2047 tag, tag_length,
2048 &output_length);
2049 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2050 TEST_EQUAL(status, expected_status);
2051 } else {
2052 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2053 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002054
Neil Armstrong752d8112022-02-07 14:51:11 +01002055 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2057 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2058 TEST_EQUAL(status, expected_status);
2059 } else {
2060 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2061 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002062
2063 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 memset(tag, 0, sizeof(tag));
2065 status = psa_aead_decrypt(key, exercise_alg,
2066 nonce, nonce_length,
2067 NULL, 0,
2068 tag, tag_length,
2069 NULL, 0,
2070 &output_length);
2071 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2072 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2073 } else if (expected_status == PSA_SUCCESS) {
2074 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2075 } else {
2076 TEST_EQUAL(status, expected_status);
2077 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002078
Neil Armstrong752d8112022-02-07 14:51:11 +01002079 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 PSA_ASSERT(psa_aead_abort(&operation));
2081 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2082 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2083 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2084 } else {
2085 TEST_EQUAL(status, expected_status);
2086 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002087
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 PSA_ASSERT(psa_aead_abort(&operation));
2090 psa_destroy_key(key);
2091 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002092}
2093/* END_CASE */
2094
2095/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002096void asymmetric_encryption_key_policy(int policy_usage_arg,
2097 int policy_alg,
2098 int key_type,
2099 data_t *key_data,
2100 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002101{
Ronald Cron5425a212020-08-04 14:58:35 +02002102 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002103 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002104 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002105 psa_status_t status;
2106 size_t key_bits;
2107 size_t buffer_length;
2108 unsigned char *buffer = NULL;
2109 size_t output_length;
2110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 psa_set_key_usage_flags(&attributes, policy_usage);
2114 psa_set_key_algorithm(&attributes, policy_alg);
2115 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2118 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002119
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002120 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 TEST_EQUAL(policy_usage,
2122 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2125 key_bits = psa_get_key_bits(&attributes);
2126 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2127 exercise_alg);
2128 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 status = psa_asymmetric_encrypt(key, exercise_alg,
2131 NULL, 0,
2132 NULL, 0,
2133 buffer, buffer_length,
2134 &output_length);
2135 if (policy_alg == exercise_alg &&
2136 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2137 PSA_ASSERT(status);
2138 } else {
2139 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2140 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (buffer_length != 0) {
2143 memset(buffer, 0, buffer_length);
2144 }
2145 status = psa_asymmetric_decrypt(key, exercise_alg,
2146 buffer, buffer_length,
2147 NULL, 0,
2148 buffer, buffer_length,
2149 &output_length);
2150 if (policy_alg == exercise_alg &&
2151 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2152 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2153 } else {
2154 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2155 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002156
2157exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002158 /*
2159 * Key attributes may have been returned by psa_get_key_attributes()
2160 * thus reset them as required.
2161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 psa_destroy_key(key);
2165 PSA_DONE();
2166 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002167}
2168/* END_CASE */
2169
2170/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171void asymmetric_signature_key_policy(int policy_usage_arg,
2172 int policy_alg,
2173 int key_type,
2174 data_t *key_data,
2175 int exercise_alg,
2176 int payload_length_arg,
2177 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002178{
Ronald Cron5425a212020-08-04 14:58:35 +02002179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002181 psa_key_usage_t policy_usage = policy_usage_arg;
2182 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002183 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002185 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2186 * compatible with the policy and `payload_length_arg` is supposed to be
2187 * a valid input length to sign. If `payload_length_arg <= 0`,
2188 * `exercise_alg` is supposed to be forbidden by the policy. */
2189 int compatible_alg = payload_length_arg > 0;
2190 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002192 size_t signature_length;
2193
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002194 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002195 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 TEST_EQUAL(expected_usage,
2197 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 psa_set_key_usage_flags(&attributes, policy_usage);
2202 psa_set_key_algorithm(&attributes, policy_alg);
2203 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2206 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 status = psa_sign_hash(key, exercise_alg,
2211 payload, payload_length,
2212 signature, sizeof(signature),
2213 &signature_length);
2214 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2215 PSA_ASSERT(status);
2216 } else {
2217 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2218 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 memset(signature, 0, sizeof(signature));
2221 status = psa_verify_hash(key, exercise_alg,
2222 payload, payload_length,
2223 signature, sizeof(signature));
2224 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2225 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2226 } else {
2227 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2228 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2231 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2232 status = psa_sign_message(key, exercise_alg,
2233 payload, payload_length,
2234 signature, sizeof(signature),
2235 &signature_length);
2236 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2237 PSA_ASSERT(status);
2238 } else {
2239 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2240 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 memset(signature, 0, sizeof(signature));
2243 status = psa_verify_message(key, exercise_alg,
2244 payload, payload_length,
2245 signature, sizeof(signature));
2246 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2247 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2248 } else {
2249 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2250 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002251 }
2252
Gilles Peskined5b33222018-06-18 22:20:03 +02002253exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 psa_destroy_key(key);
2255 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002256}
2257/* END_CASE */
2258
Janos Follathba3fab92019-06-11 14:50:16 +01002259/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002260void derive_key_policy(int policy_usage,
2261 int policy_alg,
2262 int key_type,
2263 data_t *key_data,
2264 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002265{
Ronald Cron5425a212020-08-04 14:58:35 +02002266 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002268 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002269 psa_status_t status;
2270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 psa_set_key_usage_flags(&attributes, policy_usage);
2274 psa_set_key_algorithm(&attributes, policy_alg);
2275 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2278 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2283 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2284 PSA_ASSERT(psa_key_derivation_input_bytes(
2285 &operation,
2286 PSA_KEY_DERIVATION_INPUT_SEED,
2287 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002288 }
Janos Follathba3fab92019-06-11 14:50:16 +01002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 status = psa_key_derivation_input_key(&operation,
2291 PSA_KEY_DERIVATION_INPUT_SECRET,
2292 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (policy_alg == exercise_alg &&
2295 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2296 PSA_ASSERT(status);
2297 } else {
2298 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2299 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002300
2301exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 psa_key_derivation_abort(&operation);
2303 psa_destroy_key(key);
2304 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002305}
2306/* END_CASE */
2307
2308/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002309void agreement_key_policy(int policy_usage,
2310 int policy_alg,
2311 int key_type_arg,
2312 data_t *key_data,
2313 int exercise_alg,
2314 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002315{
Ronald Cron5425a212020-08-04 14:58:35 +02002316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002318 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002319 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002320 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002321 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 psa_set_key_usage_flags(&attributes, policy_usage);
2326 psa_set_key_algorithm(&attributes, policy_alg);
2327 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2330 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2333 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002336
2337exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 psa_key_derivation_abort(&operation);
2339 psa_destroy_key(key);
2340 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002341}
2342/* END_CASE */
2343
2344/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002345void key_policy_alg2(int key_type_arg, data_t *key_data,
2346 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002347{
Ronald Cron5425a212020-08-04 14:58:35 +02002348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002349 psa_key_type_t key_type = key_type_arg;
2350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2351 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2352 psa_key_usage_t usage = usage_arg;
2353 psa_algorithm_t alg = alg_arg;
2354 psa_algorithm_t alg2 = alg2_arg;
2355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 psa_set_key_usage_flags(&attributes, usage);
2359 psa_set_key_algorithm(&attributes, alg);
2360 psa_set_key_enrollment_algorithm(&attributes, alg2);
2361 psa_set_key_type(&attributes, key_type);
2362 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2363 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002364
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002365 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 usage = mbedtls_test_update_key_usage_flags(usage);
2367 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2368 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2369 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2370 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002373 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 }
2375 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002376 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002378
2379exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002380 /*
2381 * Key attributes may have been returned by psa_get_key_attributes()
2382 * thus reset them as required.
2383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 psa_destroy_key(key);
2387 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002388}
2389/* END_CASE */
2390
2391/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392void raw_agreement_key_policy(int policy_usage,
2393 int policy_alg,
2394 int key_type_arg,
2395 data_t *key_data,
2396 int exercise_alg,
2397 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002398{
Ronald Cron5425a212020-08-04 14:58:35 +02002399 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002401 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002402 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002403 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002404 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 psa_set_key_usage_flags(&attributes, policy_usage);
2409 psa_set_key_algorithm(&attributes, policy_alg);
2410 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2413 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002418
2419exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 psa_key_derivation_abort(&operation);
2421 psa_destroy_key(key);
2422 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002423}
2424/* END_CASE */
2425
2426/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427void copy_success(int source_usage_arg,
2428 int source_alg_arg, int source_alg2_arg,
2429 unsigned int source_lifetime_arg,
2430 int type_arg, data_t *material,
2431 int copy_attributes,
2432 int target_usage_arg,
2433 int target_alg_arg, int target_alg2_arg,
2434 unsigned int target_lifetime_arg,
2435 int expected_usage_arg,
2436 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002437{
Gilles Peskineca25db92019-04-19 11:43:08 +02002438 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2439 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002440 psa_key_usage_t expected_usage = expected_usage_arg;
2441 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002442 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302443 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2444 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002445 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2446 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002447 uint8_t *export_buffer = NULL;
2448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002450
Gilles Peskineca25db92019-04-19 11:43:08 +02002451 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2453 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2454 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2455 psa_set_key_type(&source_attributes, type_arg);
2456 psa_set_key_lifetime(&source_attributes, source_lifetime);
2457 PSA_ASSERT(psa_import_key(&source_attributes,
2458 material->x, material->len,
2459 &source_key));
2460 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002461
Gilles Peskineca25db92019-04-19 11:43:08 +02002462 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002464 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002465 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (target_usage_arg != -1) {
2469 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2470 }
2471 if (target_alg_arg != -1) {
2472 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2473 }
2474 if (target_alg2_arg != -1) {
2475 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2476 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002477
Archana8a180362021-07-05 02:18:48 +05302478
Gilles Peskine57ab7212019-01-28 13:03:09 +01002479 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 PSA_ASSERT(psa_copy_key(source_key,
2481 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002482
2483 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002485
2486 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2488 TEST_EQUAL(psa_get_key_type(&source_attributes),
2489 psa_get_key_type(&target_attributes));
2490 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2491 psa_get_key_bits(&target_attributes));
2492 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2493 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2494 TEST_EQUAL(expected_alg2,
2495 psa_get_key_enrollment_algorithm(&target_attributes));
2496 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002497 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 ASSERT_ALLOC(export_buffer, material->len);
2499 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2500 material->len, &length));
2501 ASSERT_COMPARE(material->x, material->len,
2502 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002503 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if (!psa_key_lifetime_is_external(target_lifetime)) {
2506 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302507 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 }
2509 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302510 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 }
Archana8a180362021-07-05 02:18:48 +05302512 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002515
2516exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002517 /*
2518 * Source and target key attributes may have been returned by
2519 * psa_get_key_attributes() thus reset them as required.
2520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 psa_reset_key_attributes(&source_attributes);
2522 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 PSA_DONE();
2525 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002526}
2527/* END_CASE */
2528
2529/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002530void copy_fail(int source_usage_arg,
2531 int source_alg_arg, int source_alg2_arg,
2532 int source_lifetime_arg,
2533 int type_arg, data_t *material,
2534 int target_type_arg, int target_bits_arg,
2535 int target_usage_arg,
2536 int target_alg_arg, int target_alg2_arg,
2537 int target_id_arg, int target_lifetime_arg,
2538 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002539{
2540 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2541 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002542 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2543 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002547
2548 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2550 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2551 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2552 psa_set_key_type(&source_attributes, type_arg);
2553 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2554 PSA_ASSERT(psa_import_key(&source_attributes,
2555 material->x, material->len,
2556 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002557
2558 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 psa_set_key_id(&target_attributes, key_id);
2560 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2561 psa_set_key_type(&target_attributes, target_type_arg);
2562 psa_set_key_bits(&target_attributes, target_bits_arg);
2563 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2564 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2565 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002566
2567 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 TEST_EQUAL(psa_copy_key(source_key,
2569 &target_attributes, &target_key),
2570 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002573
Gilles Peskine4a644642019-05-03 17:14:08 +02002574exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 psa_reset_key_attributes(&source_attributes);
2576 psa_reset_key_attributes(&target_attributes);
2577 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002578}
2579/* END_CASE */
2580
2581/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002582void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002583{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002584 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002585 /* Test each valid way of initializing the object, except for `= {0}`, as
2586 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2587 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002588 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002590 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2591 psa_hash_operation_t zero;
2592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002594
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002595 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2597 PSA_ERROR_BAD_STATE);
2598 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2599 PSA_ERROR_BAD_STATE);
2600 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2601 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002602
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002603 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 PSA_ASSERT(psa_hash_abort(&func));
2605 PSA_ASSERT(psa_hash_abort(&init));
2606 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002607}
2608/* END_CASE */
2609
2610/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002611void hash_setup(int alg_arg,
2612 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002613{
2614 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002615 uint8_t *output = NULL;
2616 size_t output_size = 0;
2617 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002618 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002619 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002620 psa_status_t status;
2621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002623
Neil Armstrongedb20862022-02-07 15:47:44 +01002624 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 output_size = PSA_HASH_LENGTH(alg);
2626 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 status = psa_hash_compute(alg, NULL, 0,
2629 output, output_size, &output_length);
2630 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002631
2632 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 status = psa_hash_setup(&operation, alg);
2634 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002635
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002636 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002638
2639 /* If setup failed, reproduce the failure, so as to
2640 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (status != PSA_SUCCESS) {
2642 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2643 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002644
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002645 /* Now the operation object should be reusable. */
2646#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2648 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002649#endif
2650
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002651exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 mbedtls_free(output);
2653 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002654}
2655/* END_CASE */
2656
2657/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002658void hash_compute_fail(int alg_arg, data_t *input,
2659 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002660{
2661 psa_algorithm_t alg = alg_arg;
2662 uint8_t *output = NULL;
2663 size_t output_size = output_size_arg;
2664 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002665 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002666 psa_status_t expected_status = expected_status_arg;
2667 psa_status_t status;
2668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002672
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002673 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 status = psa_hash_compute(alg, input->x, input->len,
2675 output, output_size, &output_length);
2676 TEST_EQUAL(status, expected_status);
2677 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002678
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002679 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 status = psa_hash_setup(&operation, alg);
2681 if (status == PSA_SUCCESS) {
2682 status = psa_hash_update(&operation, input->x, input->len);
2683 if (status == PSA_SUCCESS) {
2684 status = psa_hash_finish(&operation, output, output_size,
2685 &output_length);
2686 if (status == PSA_SUCCESS) {
2687 TEST_LE_U(output_length, output_size);
2688 } else {
2689 TEST_EQUAL(status, expected_status);
2690 }
2691 } else {
2692 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002693 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 } else {
2695 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002696 }
2697
Gilles Peskine0a749c82019-11-28 19:33:58 +01002698exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 PSA_ASSERT(psa_hash_abort(&operation));
2700 mbedtls_free(output);
2701 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002702}
2703/* END_CASE */
2704
2705/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002706void hash_compare_fail(int alg_arg, data_t *input,
2707 data_t *reference_hash,
2708 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002709{
2710 psa_algorithm_t alg = alg_arg;
2711 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002712 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002713 psa_status_t status;
2714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002716
Neil Armstrong55a1be12022-02-07 11:23:20 +01002717 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 status = psa_hash_compare(alg, input->x, input->len,
2719 reference_hash->x, reference_hash->len);
2720 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002721
Neil Armstrong55a1be12022-02-07 11:23:20 +01002722 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 status = psa_hash_setup(&operation, alg);
2724 if (status == PSA_SUCCESS) {
2725 status = psa_hash_update(&operation, input->x, input->len);
2726 if (status == PSA_SUCCESS) {
2727 status = psa_hash_verify(&operation, reference_hash->x,
2728 reference_hash->len);
2729 TEST_EQUAL(status, expected_status);
2730 } else {
2731 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002732 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 } else {
2734 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002735 }
2736
Gilles Peskine88e08462020-01-28 20:43:00 +01002737exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 PSA_ASSERT(psa_hash_abort(&operation));
2739 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002740}
2741/* END_CASE */
2742
2743/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002744void hash_compute_compare(int alg_arg, data_t *input,
2745 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002746{
2747 psa_algorithm_t alg = alg_arg;
2748 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2749 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002750 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002751 size_t i;
2752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002754
Neil Armstrongca30a002022-02-07 11:40:23 +01002755 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2757 output, PSA_HASH_LENGTH(alg),
2758 &output_length));
2759 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2760 ASSERT_COMPARE(output, output_length,
2761 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002762
Neil Armstrongca30a002022-02-07 11:40:23 +01002763 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 PSA_ASSERT(psa_hash_setup(&operation, alg));
2765 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2766 PSA_ASSERT(psa_hash_finish(&operation, output,
2767 PSA_HASH_LENGTH(alg),
2768 &output_length));
2769 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2770 ASSERT_COMPARE(output, output_length,
2771 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002772
2773 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2775 output, sizeof(output),
2776 &output_length));
2777 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2778 ASSERT_COMPARE(output, output_length,
2779 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002780
Neil Armstrongca30a002022-02-07 11:40:23 +01002781 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 PSA_ASSERT(psa_hash_setup(&operation, alg));
2783 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2784 PSA_ASSERT(psa_hash_finish(&operation, output,
2785 sizeof(output), &output_length));
2786 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2787 ASSERT_COMPARE(output, output_length,
2788 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002789
2790 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2792 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002793
Neil Armstrongca30a002022-02-07 11:40:23 +01002794 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 PSA_ASSERT(psa_hash_setup(&operation, alg));
2796 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2797 PSA_ASSERT(psa_hash_verify(&operation, output,
2798 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002799
2800 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2802 output, output_length + 1),
2803 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002804
Neil Armstrongca30a002022-02-07 11:40:23 +01002805 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 PSA_ASSERT(psa_hash_setup(&operation, alg));
2807 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2808 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2809 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002810
2811 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2813 output, output_length - 1),
2814 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002815
Neil Armstrongca30a002022-02-07 11:40:23 +01002816 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 PSA_ASSERT(psa_hash_setup(&operation, alg));
2818 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2819 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2820 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002821
Gilles Peskine0a749c82019-11-28 19:33:58 +01002822 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 for (i = 0; i < output_length; i++) {
2824 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002825 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002826
2827 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2829 output, output_length),
2830 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002831
2832 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 PSA_ASSERT(psa_hash_setup(&operation, alg));
2834 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2835 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2836 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002837
Gilles Peskine0a749c82019-11-28 19:33:58 +01002838 output[i] ^= 1;
2839 }
2840
2841exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 PSA_ASSERT(psa_hash_abort(&operation));
2843 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002844}
2845/* END_CASE */
2846
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002847/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002848void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002849{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002850 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002851 unsigned char input[] = "";
2852 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002853 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002854 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2855 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2857 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002858 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002859 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002860 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002863
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002864 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 PSA_ASSERT(psa_hash_setup(&operation, alg));
2866 ASSERT_OPERATION_IS_ACTIVE(operation);
2867 TEST_EQUAL(psa_hash_setup(&operation, alg),
2868 PSA_ERROR_BAD_STATE);
2869 ASSERT_OPERATION_IS_INACTIVE(operation);
2870 PSA_ASSERT(psa_hash_abort(&operation));
2871 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002872
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002873 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2875 PSA_ERROR_BAD_STATE);
2876 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002877
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002878 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002880 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 ASSERT_OPERATION_IS_ACTIVE(operation);
2882 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2883 PSA_ERROR_BAD_STATE);
2884 ASSERT_OPERATION_IS_INACTIVE(operation);
2885 PSA_ASSERT(psa_hash_abort(&operation));
2886 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002887
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002888 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 PSA_ASSERT(psa_hash_setup(&operation, alg));
2890 PSA_ASSERT(psa_hash_finish(&operation,
2891 hash, sizeof(hash), &hash_len));
2892 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2893 PSA_ERROR_BAD_STATE);
2894 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002895
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002896 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 TEST_EQUAL(psa_hash_verify(&operation,
2898 valid_hash, sizeof(valid_hash)),
2899 PSA_ERROR_BAD_STATE);
2900 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002901
2902 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 PSA_ASSERT(psa_hash_setup(&operation, alg));
2904 PSA_ASSERT(psa_hash_finish(&operation,
2905 hash, sizeof(hash), &hash_len));
2906 TEST_EQUAL(psa_hash_verify(&operation,
2907 valid_hash, sizeof(valid_hash)),
2908 PSA_ERROR_BAD_STATE);
2909 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002910
2911 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 PSA_ASSERT(psa_hash_setup(&operation, alg));
2913 ASSERT_OPERATION_IS_ACTIVE(operation);
2914 PSA_ASSERT(psa_hash_verify(&operation,
2915 valid_hash, sizeof(valid_hash)));
2916 ASSERT_OPERATION_IS_INACTIVE(operation);
2917 TEST_EQUAL(psa_hash_verify(&operation,
2918 valid_hash, sizeof(valid_hash)),
2919 PSA_ERROR_BAD_STATE);
2920 ASSERT_OPERATION_IS_INACTIVE(operation);
2921 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002922
2923 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 TEST_EQUAL(psa_hash_finish(&operation,
2925 hash, sizeof(hash), &hash_len),
2926 PSA_ERROR_BAD_STATE);
2927 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002928
2929 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 PSA_ASSERT(psa_hash_setup(&operation, alg));
2931 PSA_ASSERT(psa_hash_finish(&operation,
2932 hash, sizeof(hash), &hash_len));
2933 TEST_EQUAL(psa_hash_finish(&operation,
2934 hash, sizeof(hash), &hash_len),
2935 PSA_ERROR_BAD_STATE);
2936 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002937
2938 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 PSA_ASSERT(psa_hash_setup(&operation, alg));
2940 PSA_ASSERT(psa_hash_verify(&operation,
2941 valid_hash, sizeof(valid_hash)));
2942 TEST_EQUAL(psa_hash_finish(&operation,
2943 hash, sizeof(hash), &hash_len),
2944 PSA_ERROR_BAD_STATE);
2945 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002946
2947exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002949}
2950/* END_CASE */
2951
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002952/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002953void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002954{
2955 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002956 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2957 * appended to it */
2958 unsigned char hash[] = {
2959 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2960 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2962 };
2963 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002964 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002967
itayzafrir27e69452018-11-01 14:26:34 +02002968 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 PSA_ASSERT(psa_hash_setup(&operation, alg));
2970 ASSERT_OPERATION_IS_ACTIVE(operation);
2971 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2972 PSA_ERROR_INVALID_SIGNATURE);
2973 ASSERT_OPERATION_IS_INACTIVE(operation);
2974 PSA_ASSERT(psa_hash_abort(&operation));
2975 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002976
itayzafrir27e69452018-11-01 14:26:34 +02002977 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 PSA_ASSERT(psa_hash_setup(&operation, alg));
2979 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2980 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002981
itayzafrir27e69452018-11-01 14:26:34 +02002982 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 PSA_ASSERT(psa_hash_setup(&operation, alg));
2984 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2985 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002986
itayzafrirec93d302018-10-18 18:01:10 +03002987exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002989}
2990/* END_CASE */
2991
Ronald Cronee414c72021-03-18 18:50:08 +01002992/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002993void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002994{
2995 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002996 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002998 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002999 size_t hash_len;
3000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003002
itayzafrir58028322018-10-25 10:22:01 +03003003 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 PSA_ASSERT(psa_hash_setup(&operation, alg));
3005 TEST_EQUAL(psa_hash_finish(&operation,
3006 hash, expected_size - 1, &hash_len),
3007 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003008
3009exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003011}
3012/* END_CASE */
3013
Ronald Cronee414c72021-03-18 18:50:08 +01003014/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003016{
3017 psa_algorithm_t alg = PSA_ALG_SHA_256;
3018 unsigned char hash[PSA_HASH_MAX_SIZE];
3019 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3020 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3021 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3022 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3023 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3024 size_t hash_len;
3025
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 PSA_ASSERT(psa_crypto_init());
3027 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3030 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3031 PSA_ASSERT(psa_hash_finish(&op_finished,
3032 hash, sizeof(hash), &hash_len));
3033 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3034 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3037 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3040 PSA_ASSERT(psa_hash_finish(&op_init,
3041 hash, sizeof(hash), &hash_len));
3042 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3043 PSA_ASSERT(psa_hash_finish(&op_finished,
3044 hash, sizeof(hash), &hash_len));
3045 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3046 PSA_ASSERT(psa_hash_finish(&op_aborted,
3047 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003048
3049exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 psa_hash_abort(&op_source);
3051 psa_hash_abort(&op_init);
3052 psa_hash_abort(&op_setup);
3053 psa_hash_abort(&op_finished);
3054 psa_hash_abort(&op_aborted);
3055 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003056}
3057/* END_CASE */
3058
Ronald Cronee414c72021-03-18 18:50:08 +01003059/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003060void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003061{
3062 psa_algorithm_t alg = PSA_ALG_SHA_256;
3063 unsigned char hash[PSA_HASH_MAX_SIZE];
3064 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3065 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3066 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3067 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3068 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3069 size_t hash_len;
3070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3074 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3075 PSA_ASSERT(psa_hash_finish(&op_finished,
3076 hash, sizeof(hash), &hash_len));
3077 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3078 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3081 PSA_ASSERT(psa_hash_finish(&op_target,
3082 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3085 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3086 PSA_ERROR_BAD_STATE);
3087 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3088 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003089
3090exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 psa_hash_abort(&op_target);
3092 psa_hash_abort(&op_init);
3093 psa_hash_abort(&op_setup);
3094 psa_hash_abort(&op_finished);
3095 psa_hash_abort(&op_aborted);
3096 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003097}
3098/* END_CASE */
3099
itayzafrir58028322018-10-25 10:22:01 +03003100/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003101void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003102{
Jaeden Amero252ef282019-02-15 14:05:35 +00003103 const uint8_t input[1] = { 0 };
3104
Jaeden Amero769ce272019-01-04 11:48:03 +00003105 /* Test each valid way of initializing the object, except for `= {0}`, as
3106 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3107 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003108 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003110 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3111 psa_mac_operation_t zero;
3112
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003114
Jaeden Amero252ef282019-02-15 14:05:35 +00003115 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 TEST_EQUAL(psa_mac_update(&func,
3117 input, sizeof(input)),
3118 PSA_ERROR_BAD_STATE);
3119 TEST_EQUAL(psa_mac_update(&init,
3120 input, sizeof(input)),
3121 PSA_ERROR_BAD_STATE);
3122 TEST_EQUAL(psa_mac_update(&zero,
3123 input, sizeof(input)),
3124 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003125
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003126 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 PSA_ASSERT(psa_mac_abort(&func));
3128 PSA_ASSERT(psa_mac_abort(&init));
3129 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003130}
3131/* END_CASE */
3132
3133/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003134void mac_setup(int key_type_arg,
3135 data_t *key,
3136 int alg_arg,
3137 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003138{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003139 psa_key_type_t key_type = key_type_arg;
3140 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003141 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003142 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003143 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3144#if defined(KNOWN_SUPPORTED_MAC_ALG)
3145 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3146#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3151 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 }
3154 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003155
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003156 /* The operation object should be reusable. */
3157#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3159 smoke_test_key_data,
3160 sizeof(smoke_test_key_data),
3161 KNOWN_SUPPORTED_MAC_ALG,
3162 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003163 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 }
3165 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003166#endif
3167
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003170}
3171/* END_CASE */
3172
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003173/* 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 +01003174void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003175{
Ronald Cron5425a212020-08-04 14:58:35 +02003176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003177 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3178 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003179 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003180 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3181 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3183 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003185 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3186 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3187 size_t sign_mac_length = 0;
3188 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3189 const uint8_t verify_mac[] = {
3190 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3191 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3193 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003194
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 PSA_ASSERT(psa_crypto_init());
3196 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3197 psa_set_key_algorithm(&attributes, alg);
3198 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003199
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3201 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003202
Jaeden Amero252ef282019-02-15 14:05:35 +00003203 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3205 PSA_ERROR_BAD_STATE);
3206 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003207
3208 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3210 &sign_mac_length),
3211 PSA_ERROR_BAD_STATE);
3212 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003213
3214 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 TEST_EQUAL(psa_mac_verify_finish(&operation,
3216 verify_mac, sizeof(verify_mac)),
3217 PSA_ERROR_BAD_STATE);
3218 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003219
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003220 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3222 ASSERT_OPERATION_IS_ACTIVE(operation);
3223 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3224 PSA_ERROR_BAD_STATE);
3225 ASSERT_OPERATION_IS_INACTIVE(operation);
3226 PSA_ASSERT(psa_mac_abort(&operation));
3227 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003228
Jaeden Amero252ef282019-02-15 14:05:35 +00003229 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3231 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3232 PSA_ASSERT(psa_mac_sign_finish(&operation,
3233 sign_mac, sizeof(sign_mac),
3234 &sign_mac_length));
3235 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3236 PSA_ERROR_BAD_STATE);
3237 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003238
3239 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3241 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3242 PSA_ASSERT(psa_mac_verify_finish(&operation,
3243 verify_mac, sizeof(verify_mac)));
3244 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3245 PSA_ERROR_BAD_STATE);
3246 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003247
3248 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3250 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3251 PSA_ASSERT(psa_mac_sign_finish(&operation,
3252 sign_mac, sizeof(sign_mac),
3253 &sign_mac_length));
3254 TEST_EQUAL(psa_mac_sign_finish(&operation,
3255 sign_mac, sizeof(sign_mac),
3256 &sign_mac_length),
3257 PSA_ERROR_BAD_STATE);
3258 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003259
3260 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3262 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3263 PSA_ASSERT(psa_mac_verify_finish(&operation,
3264 verify_mac, sizeof(verify_mac)));
3265 TEST_EQUAL(psa_mac_verify_finish(&operation,
3266 verify_mac, sizeof(verify_mac)),
3267 PSA_ERROR_BAD_STATE);
3268 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003269
3270 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3272 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3273 ASSERT_OPERATION_IS_ACTIVE(operation);
3274 TEST_EQUAL(psa_mac_verify_finish(&operation,
3275 verify_mac, sizeof(verify_mac)),
3276 PSA_ERROR_BAD_STATE);
3277 ASSERT_OPERATION_IS_INACTIVE(operation);
3278 PSA_ASSERT(psa_mac_abort(&operation));
3279 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003280
3281 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3283 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3284 ASSERT_OPERATION_IS_ACTIVE(operation);
3285 TEST_EQUAL(psa_mac_sign_finish(&operation,
3286 sign_mac, sizeof(sign_mac),
3287 &sign_mac_length),
3288 PSA_ERROR_BAD_STATE);
3289 ASSERT_OPERATION_IS_INACTIVE(operation);
3290 PSA_ASSERT(psa_mac_abort(&operation));
3291 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003292
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003294
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003295exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003297}
3298/* END_CASE */
3299
3300/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003301void mac_sign_verify_multi(int key_type_arg,
3302 data_t *key_data,
3303 int alg_arg,
3304 data_t *input,
3305 int is_verify,
3306 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003307{
3308 size_t data_part_len = 0;
3309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003311 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003313
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 if (mac_multipart_internal_func(key_type_arg, key_data,
3315 alg_arg,
3316 input, data_part_len,
3317 expected_mac,
3318 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003319 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003321
3322 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003324
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 if (mac_multipart_internal_func(key_type_arg, key_data,
3326 alg_arg,
3327 input, data_part_len,
3328 expected_mac,
3329 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003330 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003332 }
3333
3334 /* Goto is required to silence warnings about unused labels, as we
3335 * don't actually do any test assertions in this function. */
3336 goto exit;
3337}
3338/* END_CASE */
3339
3340/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341void mac_sign(int key_type_arg,
3342 data_t *key_data,
3343 int alg_arg,
3344 data_t *input,
3345 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003346{
Ronald Cron5425a212020-08-04 14:58:35 +02003347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003348 psa_key_type_t key_type = key_type_arg;
3349 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003350 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003352 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003353 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003355 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003356 const size_t output_sizes_to_test[] = {
3357 0,
3358 1,
3359 expected_mac->len - 1,
3360 expected_mac->len,
3361 expected_mac->len + 1,
3362 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003365 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3371 psa_set_key_algorithm(&attributes, alg);
3372 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003373
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3375 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003378 const size_t output_size = output_sizes_to_test[i];
3379 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 (output_size >= expected_mac->len ? PSA_SUCCESS :
3381 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003382
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 mbedtls_test_set_step(output_size);
3384 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003385
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003386 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 TEST_EQUAL(psa_mac_compute(key, alg,
3388 input->x, input->len,
3389 actual_mac, output_size, &mac_length),
3390 expected_status);
3391 if (expected_status == PSA_SUCCESS) {
3392 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3393 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003394 }
3395
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 if (output_size > 0) {
3397 memset(actual_mac, 0, output_size);
3398 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003399
3400 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3402 PSA_ASSERT(psa_mac_update(&operation,
3403 input->x, input->len));
3404 TEST_EQUAL(psa_mac_sign_finish(&operation,
3405 actual_mac, output_size,
3406 &mac_length),
3407 expected_status);
3408 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003409
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 if (expected_status == PSA_SUCCESS) {
3411 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3412 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003413 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003415 actual_mac = NULL;
3416 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003417
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 psa_mac_abort(&operation);
3420 psa_destroy_key(key);
3421 PSA_DONE();
3422 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003423}
3424/* END_CASE */
3425
3426/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003427void mac_verify(int key_type_arg,
3428 data_t *key_data,
3429 int alg_arg,
3430 data_t *input,
3431 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003432{
Ronald Cron5425a212020-08-04 14:58:35 +02003433 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003434 psa_key_type_t key_type = key_type_arg;
3435 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003436 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003438 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003439
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003441
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003443
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3445 psa_set_key_algorithm(&attributes, alg);
3446 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003447
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3449 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003450
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003451 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3453 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003454
3455 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3457 PSA_ASSERT(psa_mac_update(&operation,
3458 input->x, input->len));
3459 PSA_ASSERT(psa_mac_verify_finish(&operation,
3460 expected_mac->x,
3461 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003462
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003463 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 TEST_EQUAL(psa_mac_verify(key, alg,
3465 input->x, input->len,
3466 expected_mac->x,
3467 expected_mac->len - 1),
3468 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003469
3470 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3472 PSA_ASSERT(psa_mac_update(&operation,
3473 input->x, input->len));
3474 TEST_EQUAL(psa_mac_verify_finish(&operation,
3475 expected_mac->x,
3476 expected_mac->len - 1),
3477 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003478
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003479 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3481 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3482 TEST_EQUAL(psa_mac_verify(key, alg,
3483 input->x, input->len,
3484 perturbed_mac, expected_mac->len + 1),
3485 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003486
3487 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003488 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3489 PSA_ASSERT(psa_mac_update(&operation,
3490 input->x, input->len));
3491 TEST_EQUAL(psa_mac_verify_finish(&operation,
3492 perturbed_mac,
3493 expected_mac->len + 1),
3494 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003495
3496 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 for (size_t i = 0; i < expected_mac->len; i++) {
3498 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003499 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003500
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 TEST_EQUAL(psa_mac_verify(key, alg,
3502 input->x, input->len,
3503 perturbed_mac, expected_mac->len),
3504 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003505
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3507 PSA_ASSERT(psa_mac_update(&operation,
3508 input->x, input->len));
3509 TEST_EQUAL(psa_mac_verify_finish(&operation,
3510 perturbed_mac,
3511 expected_mac->len),
3512 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003513 perturbed_mac[i] ^= 1;
3514 }
3515
Gilles Peskine8c9def32018-02-08 10:02:12 +01003516exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 psa_mac_abort(&operation);
3518 psa_destroy_key(key);
3519 PSA_DONE();
3520 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003521}
3522/* END_CASE */
3523
3524/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003525void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003526{
Jaeden Ameroab439972019-02-15 14:12:05 +00003527 const uint8_t input[1] = { 0 };
3528 unsigned char output[1] = { 0 };
3529 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003530 /* Test each valid way of initializing the object, except for `= {0}`, as
3531 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3532 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003533 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003535 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3536 psa_cipher_operation_t zero;
3537
Gilles Peskine449bd832023-01-11 14:50:10 +01003538 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003539
Jaeden Ameroab439972019-02-15 14:12:05 +00003540 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003541 TEST_EQUAL(psa_cipher_update(&func,
3542 input, sizeof(input),
3543 output, sizeof(output),
3544 &output_length),
3545 PSA_ERROR_BAD_STATE);
3546 TEST_EQUAL(psa_cipher_update(&init,
3547 input, sizeof(input),
3548 output, sizeof(output),
3549 &output_length),
3550 PSA_ERROR_BAD_STATE);
3551 TEST_EQUAL(psa_cipher_update(&zero,
3552 input, sizeof(input),
3553 output, sizeof(output),
3554 &output_length),
3555 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003556
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003557 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 PSA_ASSERT(psa_cipher_abort(&func));
3559 PSA_ASSERT(psa_cipher_abort(&init));
3560 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003561}
3562/* END_CASE */
3563
3564/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003565void cipher_setup(int key_type_arg,
3566 data_t *key,
3567 int alg_arg,
3568 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003569{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003570 psa_key_type_t key_type = key_type_arg;
3571 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003572 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003573 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003574 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003575#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003576 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3577#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003578
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003580
Gilles Peskine449bd832023-01-11 14:50:10 +01003581 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3582 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003583 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 }
3585 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003586
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003587 /* The operation object should be reusable. */
3588#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003589 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3590 smoke_test_key_data,
3591 sizeof(smoke_test_key_data),
3592 KNOWN_SUPPORTED_CIPHER_ALG,
3593 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003594 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003595 }
3596 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003597#endif
3598
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003599exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 psa_cipher_abort(&operation);
3601 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003602}
3603/* END_CASE */
3604
Ronald Cronee414c72021-03-18 18:50:08 +01003605/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003606void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003607{
Ronald Cron5425a212020-08-04 14:58:35 +02003608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003609 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3610 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003612 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003613 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003614 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003615 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 0xaa, 0xaa, 0xaa, 0xaa
3617 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003618 const uint8_t text[] = {
3619 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003620 0xbb, 0xbb, 0xbb, 0xbb
3621 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003622 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003623 size_t length = 0;
3624
Gilles Peskine449bd832023-01-11 14:50:10 +01003625 PSA_ASSERT(psa_crypto_init());
3626 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3627 psa_set_key_algorithm(&attributes, alg);
3628 psa_set_key_type(&attributes, key_type);
3629 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3630 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003631
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003632 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003633 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3634 ASSERT_OPERATION_IS_ACTIVE(operation);
3635 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3636 PSA_ERROR_BAD_STATE);
3637 ASSERT_OPERATION_IS_INACTIVE(operation);
3638 PSA_ASSERT(psa_cipher_abort(&operation));
3639 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003640
3641 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003642 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3643 ASSERT_OPERATION_IS_ACTIVE(operation);
3644 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3645 PSA_ERROR_BAD_STATE);
3646 ASSERT_OPERATION_IS_INACTIVE(operation);
3647 PSA_ASSERT(psa_cipher_abort(&operation));
3648 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003649
Jaeden Ameroab439972019-02-15 14:12:05 +00003650 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003651 TEST_EQUAL(psa_cipher_generate_iv(&operation,
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 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003658 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3659 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3660 buffer, sizeof(buffer),
3661 &length));
3662 ASSERT_OPERATION_IS_ACTIVE(operation);
3663 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3664 buffer, sizeof(buffer),
3665 &length),
3666 PSA_ERROR_BAD_STATE);
3667 ASSERT_OPERATION_IS_INACTIVE(operation);
3668 PSA_ASSERT(psa_cipher_abort(&operation));
3669 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003670
3671 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003672 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3673 PSA_ASSERT(psa_cipher_set_iv(&operation,
3674 iv, sizeof(iv)));
3675 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3676 buffer, sizeof(buffer),
3677 &length),
3678 PSA_ERROR_BAD_STATE);
3679 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003680
3681 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 TEST_EQUAL(psa_cipher_set_iv(&operation,
3683 iv, sizeof(iv)),
3684 PSA_ERROR_BAD_STATE);
3685 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003686
3687 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003688 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3689 PSA_ASSERT(psa_cipher_set_iv(&operation,
3690 iv, sizeof(iv)));
3691 ASSERT_OPERATION_IS_ACTIVE(operation);
3692 TEST_EQUAL(psa_cipher_set_iv(&operation,
3693 iv, sizeof(iv)),
3694 PSA_ERROR_BAD_STATE);
3695 ASSERT_OPERATION_IS_INACTIVE(operation);
3696 PSA_ASSERT(psa_cipher_abort(&operation));
3697 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003698
3699 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003700 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3701 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3702 buffer, sizeof(buffer),
3703 &length));
3704 TEST_EQUAL(psa_cipher_set_iv(&operation,
3705 iv, sizeof(iv)),
3706 PSA_ERROR_BAD_STATE);
3707 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003708
3709 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003710 TEST_EQUAL(psa_cipher_update(&operation,
3711 text, sizeof(text),
3712 buffer, sizeof(buffer),
3713 &length),
3714 PSA_ERROR_BAD_STATE);
3715 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003716
3717 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003718 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3719 ASSERT_OPERATION_IS_ACTIVE(operation);
3720 TEST_EQUAL(psa_cipher_update(&operation,
3721 text, sizeof(text),
3722 buffer, sizeof(buffer),
3723 &length),
3724 PSA_ERROR_BAD_STATE);
3725 ASSERT_OPERATION_IS_INACTIVE(operation);
3726 PSA_ASSERT(psa_cipher_abort(&operation));
3727 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003728
3729 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003730 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3731 PSA_ASSERT(psa_cipher_set_iv(&operation,
3732 iv, sizeof(iv)));
3733 PSA_ASSERT(psa_cipher_finish(&operation,
3734 buffer, sizeof(buffer), &length));
3735 TEST_EQUAL(psa_cipher_update(&operation,
3736 text, sizeof(text),
3737 buffer, sizeof(buffer),
3738 &length),
3739 PSA_ERROR_BAD_STATE);
3740 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003741
3742 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003743 TEST_EQUAL(psa_cipher_finish(&operation,
3744 buffer, sizeof(buffer), &length),
3745 PSA_ERROR_BAD_STATE);
3746 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003747
3748 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003749 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003750 /* Not calling update means we are encrypting an empty buffer, which is OK
3751 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 ASSERT_OPERATION_IS_ACTIVE(operation);
3753 TEST_EQUAL(psa_cipher_finish(&operation,
3754 buffer, sizeof(buffer), &length),
3755 PSA_ERROR_BAD_STATE);
3756 ASSERT_OPERATION_IS_INACTIVE(operation);
3757 PSA_ASSERT(psa_cipher_abort(&operation));
3758 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003759
3760 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3762 PSA_ASSERT(psa_cipher_set_iv(&operation,
3763 iv, sizeof(iv)));
3764 PSA_ASSERT(psa_cipher_finish(&operation,
3765 buffer, sizeof(buffer), &length));
3766 TEST_EQUAL(psa_cipher_finish(&operation,
3767 buffer, sizeof(buffer), &length),
3768 PSA_ERROR_BAD_STATE);
3769 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003770
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003772
Jaeden Ameroab439972019-02-15 14:12:05 +00003773exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003774 psa_cipher_abort(&operation);
3775 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003776}
3777/* END_CASE */
3778
3779/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003780void cipher_encrypt_fail(int alg_arg,
3781 int key_type_arg,
3782 data_t *key_data,
3783 data_t *input,
3784 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003785{
Ronald Cron5425a212020-08-04 14:58:35 +02003786 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003787 psa_status_t status;
3788 psa_key_type_t key_type = key_type_arg;
3789 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003790 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003791 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003792 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3793 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003794 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003795 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003796 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003797 size_t function_output_length;
3798 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003799 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3800
Gilles Peskine449bd832023-01-11 14:50:10 +01003801 if (PSA_ERROR_BAD_STATE != expected_status) {
3802 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003803
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3805 psa_set_key_algorithm(&attributes, alg);
3806 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003807
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3809 input->len);
3810 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003811
Gilles Peskine449bd832023-01-11 14:50:10 +01003812 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3813 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003814 }
3815
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003816 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3818 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003819
Gilles Peskine449bd832023-01-11 14:50:10 +01003820 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003821
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003822 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003823 status = psa_cipher_encrypt_setup(&operation, key, alg);
3824 if (status == PSA_SUCCESS) {
3825 if (alg != PSA_ALG_ECB_NO_PADDING) {
3826 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3827 iv, iv_size,
3828 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003829 }
3830
Gilles Peskine449bd832023-01-11 14:50:10 +01003831 status = psa_cipher_update(&operation, input->x, input->len,
3832 output, output_buffer_size,
3833 &function_output_length);
3834 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003835 output_length += function_output_length;
3836
Gilles Peskine449bd832023-01-11 14:50:10 +01003837 status = psa_cipher_finish(&operation, output + output_length,
3838 output_buffer_size - output_length,
3839 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003840
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 TEST_EQUAL(status, expected_status);
3842 } else {
3843 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003844 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 } else {
3846 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003847 }
3848
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003849exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 psa_cipher_abort(&operation);
3851 mbedtls_free(output);
3852 psa_destroy_key(key);
3853 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003854}
3855/* END_CASE */
3856
3857/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003858void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3859 data_t *input, int iv_length,
3860 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003861{
3862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3863 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3865 size_t output_buffer_size = 0;
3866 unsigned char *output = NULL;
3867
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3869 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003870
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003872
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3874 psa_set_key_algorithm(&attributes, alg);
3875 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003876
Gilles Peskine449bd832023-01-11 14:50:10 +01003877 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3878 &key));
3879 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3880 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3881 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003882
3883exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003884 psa_cipher_abort(&operation);
3885 mbedtls_free(output);
3886 psa_destroy_key(key);
3887 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003888}
3889/* END_CASE */
3890
3891/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003892void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3893 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003894{
3895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3896 psa_key_type_t key_type = key_type_arg;
3897 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003898 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3899 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003900 unsigned char *output = NULL;
3901 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003902 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3904
Gilles Peskine449bd832023-01-11 14:50:10 +01003905 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003906
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003907 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 TEST_LE_U(ciphertext->len,
3909 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3910 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3911 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3912 TEST_LE_U(plaintext->len,
3913 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3914 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3915 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003916
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003917
3918 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003919 psa_set_key_usage_flags(&attributes,
3920 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3921 psa_set_key_algorithm(&attributes, alg);
3922 psa_set_key_type(&attributes, key_type);
3923 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3924 &key));
3925 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3926 plaintext->len);
3927 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003928
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003929 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3931 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3932 PSA_ERROR_BAD_STATE);
3933 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3934 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3935 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003936
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003937 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003938 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3939 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3940 &length),
3941 PSA_ERROR_BAD_STATE);
3942 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3943 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3944 &length),
3945 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003946
Gilles Peskine286c3142022-04-20 17:09:38 +02003947 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003949 output_length = 0;
3950 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 PSA_ASSERT(psa_cipher_update(&operation,
3952 plaintext->x, plaintext->len,
3953 output, output_buffer_size,
3954 &length));
3955 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003956 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 PSA_ASSERT(psa_cipher_finish(&operation,
3958 mbedtls_buffer_offset(output, output_length),
3959 output_buffer_size - output_length,
3960 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003961 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3963 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003964
Gilles Peskine286c3142022-04-20 17:09:38 +02003965 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003967 output_length = 0;
3968 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 PSA_ASSERT(psa_cipher_update(&operation,
3970 ciphertext->x, ciphertext->len,
3971 output, output_buffer_size,
3972 &length));
3973 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003974 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 PSA_ASSERT(psa_cipher_finish(&operation,
3976 mbedtls_buffer_offset(output, output_length),
3977 output_buffer_size - output_length,
3978 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003979 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 ASSERT_COMPARE(plaintext->x, plaintext->len,
3981 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003982
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003983 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003984 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3986 output, output_buffer_size,
3987 &output_length));
3988 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3989 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003990
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003991 /* One-shot decryption */
3992 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3994 output, output_buffer_size,
3995 &output_length));
3996 ASSERT_COMPARE(plaintext->x, plaintext->len,
3997 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003998
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003999exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 PSA_ASSERT(psa_cipher_abort(&operation));
4001 mbedtls_free(output);
4002 psa_cipher_abort(&operation);
4003 psa_destroy_key(key);
4004 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004005}
4006/* END_CASE */
4007
4008/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004009void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004010{
4011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4012 psa_algorithm_t alg = alg_arg;
4013 psa_key_type_t key_type = key_type_arg;
4014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4015 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4016 psa_status_t status;
4017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004019
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4021 psa_set_key_algorithm(&attributes, alg);
4022 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004023
4024 /* Usage of either of these two size macros would cause divide by zero
4025 * with incorrect key types previously. Input length should be irrelevant
4026 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4028 0);
4029 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004030
4031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4033 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004034
4035 /* Should fail due to invalid alg type (to support invalid key type).
4036 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004040
4041exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 psa_cipher_abort(&operation);
4043 psa_destroy_key(key);
4044 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004045}
4046/* END_CASE */
4047
4048/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004049void cipher_encrypt_validation(int alg_arg,
4050 int key_type_arg,
4051 data_t *key_data,
4052 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004053{
4054 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4055 psa_key_type_t key_type = key_type_arg;
4056 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004058 unsigned char *output1 = NULL;
4059 size_t output1_buffer_size = 0;
4060 size_t output1_length = 0;
4061 unsigned char *output2 = NULL;
4062 size_t output2_buffer_size = 0;
4063 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004064 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004065 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004066 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004067
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004069
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4071 psa_set_key_algorithm(&attributes, alg);
4072 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004073
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4075 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4076 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4077 ASSERT_ALLOC(output1, output1_buffer_size);
4078 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4081 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004082
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004083 /* The one-shot cipher encryption uses generated iv so validating
4084 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4086 output1_buffer_size, &output1_length));
4087 TEST_LE_U(output1_length,
4088 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4089 TEST_LE_U(output1_length,
4090 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4093 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 PSA_ASSERT(psa_cipher_update(&operation,
4096 input->x, input->len,
4097 output2, output2_buffer_size,
4098 &function_output_length));
4099 TEST_LE_U(function_output_length,
4100 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4101 TEST_LE_U(function_output_length,
4102 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004103 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 PSA_ASSERT(psa_cipher_finish(&operation,
4106 output2 + output2_length,
4107 output2_buffer_size - output2_length,
4108 &function_output_length));
4109 TEST_LE_U(function_output_length,
4110 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4111 TEST_LE_U(function_output_length,
4112 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004113 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004114
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 PSA_ASSERT(psa_cipher_abort(&operation));
4116 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4117 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004118
Gilles Peskine50e586b2018-06-08 14:28:46 +02004119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 psa_cipher_abort(&operation);
4121 mbedtls_free(output1);
4122 mbedtls_free(output2);
4123 psa_destroy_key(key);
4124 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004125}
4126/* END_CASE */
4127
4128/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004129void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4130 data_t *key_data, data_t *iv,
4131 data_t *input,
4132 int first_part_size_arg,
4133 int output1_length_arg, int output2_length_arg,
4134 data_t *expected_output,
4135 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004136{
Ronald Cron5425a212020-08-04 14:58:35 +02004137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004138 psa_key_type_t key_type = key_type_arg;
4139 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004140 psa_status_t status;
4141 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004142 size_t first_part_size = first_part_size_arg;
4143 size_t output1_length = output1_length_arg;
4144 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004145 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004146 size_t output_buffer_size = 0;
4147 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004148 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004149 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4155 psa_set_key_algorithm(&attributes, alg);
4156 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4159 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 if (iv->len > 0) {
4164 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004165 }
4166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4168 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4169 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 TEST_LE_U(first_part_size, input->len);
4172 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4173 output, output_buffer_size,
4174 &function_output_length));
4175 TEST_ASSERT(function_output_length == output1_length);
4176 TEST_LE_U(function_output_length,
4177 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4178 TEST_LE_U(function_output_length,
4179 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004180 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 if (first_part_size < input->len) {
4183 PSA_ASSERT(psa_cipher_update(&operation,
4184 input->x + first_part_size,
4185 input->len - first_part_size,
4186 (output_buffer_size == 0 ? NULL :
4187 output + total_output_length),
4188 output_buffer_size - total_output_length,
4189 &function_output_length));
4190 TEST_ASSERT(function_output_length == output2_length);
4191 TEST_LE_U(function_output_length,
4192 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4193 alg,
4194 input->len - first_part_size));
4195 TEST_LE_U(function_output_length,
4196 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004197 total_output_length += function_output_length;
4198 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 status = psa_cipher_finish(&operation,
4201 (output_buffer_size == 0 ? NULL :
4202 output + total_output_length),
4203 output_buffer_size - total_output_length,
4204 &function_output_length);
4205 TEST_LE_U(function_output_length,
4206 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4207 TEST_LE_U(function_output_length,
4208 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004209 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 if (expected_status == PSA_SUCCESS) {
4213 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004214
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 ASSERT_COMPARE(expected_output->x, expected_output->len,
4216 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004217 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004218
4219exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 psa_cipher_abort(&operation);
4221 mbedtls_free(output);
4222 psa_destroy_key(key);
4223 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004224}
4225/* END_CASE */
4226
4227/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004228void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4229 data_t *key_data, data_t *iv,
4230 data_t *input,
4231 int first_part_size_arg,
4232 int output1_length_arg, int output2_length_arg,
4233 data_t *expected_output,
4234 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004235{
Ronald Cron5425a212020-08-04 14:58:35 +02004236 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004237 psa_key_type_t key_type = key_type_arg;
4238 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004239 psa_status_t status;
4240 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004241 size_t first_part_size = first_part_size_arg;
4242 size_t output1_length = output1_length_arg;
4243 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004244 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004245 size_t output_buffer_size = 0;
4246 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004247 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004248 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004252
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4254 psa_set_key_algorithm(&attributes, alg);
4255 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4258 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 if (iv->len > 0) {
4263 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004264 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4267 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4268 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 TEST_LE_U(first_part_size, input->len);
4271 PSA_ASSERT(psa_cipher_update(&operation,
4272 input->x, first_part_size,
4273 output, output_buffer_size,
4274 &function_output_length));
4275 TEST_ASSERT(function_output_length == output1_length);
4276 TEST_LE_U(function_output_length,
4277 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4278 TEST_LE_U(function_output_length,
4279 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004280 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004281
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 if (first_part_size < input->len) {
4283 PSA_ASSERT(psa_cipher_update(&operation,
4284 input->x + first_part_size,
4285 input->len - first_part_size,
4286 (output_buffer_size == 0 ? NULL :
4287 output + total_output_length),
4288 output_buffer_size - total_output_length,
4289 &function_output_length));
4290 TEST_ASSERT(function_output_length == output2_length);
4291 TEST_LE_U(function_output_length,
4292 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4293 alg,
4294 input->len - first_part_size));
4295 TEST_LE_U(function_output_length,
4296 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004297 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004298 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 status = psa_cipher_finish(&operation,
4301 (output_buffer_size == 0 ? NULL :
4302 output + total_output_length),
4303 output_buffer_size - total_output_length,
4304 &function_output_length);
4305 TEST_LE_U(function_output_length,
4306 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4307 TEST_LE_U(function_output_length,
4308 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004309 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004311
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 if (expected_status == PSA_SUCCESS) {
4313 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004314
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 ASSERT_COMPARE(expected_output->x, expected_output->len,
4316 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004317 }
4318
Gilles Peskine50e586b2018-06-08 14:28:46 +02004319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 psa_cipher_abort(&operation);
4321 mbedtls_free(output);
4322 psa_destroy_key(key);
4323 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004324}
4325/* END_CASE */
4326
Gilles Peskine50e586b2018-06-08 14:28:46 +02004327/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004328void cipher_decrypt_fail(int alg_arg,
4329 int key_type_arg,
4330 data_t *key_data,
4331 data_t *iv,
4332 data_t *input_arg,
4333 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004334{
4335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4336 psa_status_t status;
4337 psa_key_type_t key_type = key_type_arg;
4338 psa_algorithm_t alg = alg_arg;
4339 psa_status_t expected_status = expected_status_arg;
4340 unsigned char *input = NULL;
4341 size_t input_buffer_size = 0;
4342 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004343 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004344 size_t output_buffer_size = 0;
4345 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004346 size_t function_output_length;
4347 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4349
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 if (PSA_ERROR_BAD_STATE != expected_status) {
4351 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4354 psa_set_key_algorithm(&attributes, alg);
4355 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004356
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4358 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004359 }
4360
4361 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4363 if (input_buffer_size > 0) {
4364 ASSERT_ALLOC(input, input_buffer_size);
4365 memcpy(input, iv->x, iv->len);
4366 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004367 }
4368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4370 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004371
Neil Armstrong66a479f2022-02-07 15:41:19 +01004372 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4374 output_buffer_size, &output_length);
4375 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004376
Neil Armstrong66a479f2022-02-07 15:41:19 +01004377 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 status = psa_cipher_decrypt_setup(&operation, key, alg);
4379 if (status == PSA_SUCCESS) {
4380 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4381 input_arg->len) +
4382 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4383 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 if (iv->len > 0) {
4386 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004387
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 if (status != PSA_SUCCESS) {
4389 TEST_EQUAL(status, expected_status);
4390 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004391 }
4392
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 if (status == PSA_SUCCESS) {
4394 status = psa_cipher_update(&operation,
4395 input_arg->x, input_arg->len,
4396 output_multi, output_buffer_size,
4397 &function_output_length);
4398 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004399 output_length = function_output_length;
4400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 status = psa_cipher_finish(&operation,
4402 output_multi + output_length,
4403 output_buffer_size - output_length,
4404 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 TEST_EQUAL(status, expected_status);
4407 } else {
4408 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004409 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 } else {
4411 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004412 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 } else {
4414 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004415 }
4416
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004417exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 psa_cipher_abort(&operation);
4419 mbedtls_free(input);
4420 mbedtls_free(output);
4421 mbedtls_free(output_multi);
4422 psa_destroy_key(key);
4423 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004424}
4425/* END_CASE */
4426
4427/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004428void cipher_decrypt(int alg_arg,
4429 int key_type_arg,
4430 data_t *key_data,
4431 data_t *iv,
4432 data_t *input_arg,
4433 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004434{
4435 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4436 psa_key_type_t key_type = key_type_arg;
4437 psa_algorithm_t alg = alg_arg;
4438 unsigned char *input = NULL;
4439 size_t input_buffer_size = 0;
4440 unsigned char *output = NULL;
4441 size_t output_buffer_size = 0;
4442 size_t output_length = 0;
4443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4444
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4448 psa_set_key_algorithm(&attributes, alg);
4449 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004450
4451 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4453 if (input_buffer_size > 0) {
4454 ASSERT_ALLOC(input, input_buffer_size);
4455 memcpy(input, iv->x, iv->len);
4456 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004457 }
4458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4460 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4463 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4466 output_buffer_size, &output_length));
4467 TEST_LE_U(output_length,
4468 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4469 TEST_LE_U(output_length,
4470 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004471
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 ASSERT_COMPARE(expected_output->x, expected_output->len,
4473 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004474exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 mbedtls_free(input);
4476 mbedtls_free(output);
4477 psa_destroy_key(key);
4478 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004479}
4480/* END_CASE */
4481
4482/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004483void cipher_verify_output(int alg_arg,
4484 int key_type_arg,
4485 data_t *key_data,
4486 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004487{
Ronald Cron5425a212020-08-04 14:58:35 +02004488 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004489 psa_key_type_t key_type = key_type_arg;
4490 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004491 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004492 size_t output1_size = 0;
4493 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004494 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004495 size_t output2_size = 0;
4496 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004497 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004500
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4502 psa_set_key_algorithm(&attributes, alg);
4503 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004504
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4506 &key));
4507 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4508 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4511 output1, output1_size,
4512 &output1_length));
4513 TEST_LE_U(output1_length,
4514 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4515 TEST_LE_U(output1_length,
4516 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004517
4518 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4522 output2, output2_size,
4523 &output2_length));
4524 TEST_LE_U(output2_length,
4525 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4526 TEST_LE_U(output2_length,
4527 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004530
4531exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 mbedtls_free(output1);
4533 mbedtls_free(output2);
4534 psa_destroy_key(key);
4535 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004536}
4537/* END_CASE */
4538
4539/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004540void cipher_verify_output_multipart(int alg_arg,
4541 int key_type_arg,
4542 data_t *key_data,
4543 data_t *input,
4544 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004545{
Ronald Cron5425a212020-08-04 14:58:35 +02004546 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004547 psa_key_type_t key_type = key_type_arg;
4548 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004549 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004551 size_t iv_size = 16;
4552 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004553 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004554 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004555 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004556 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004557 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004558 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004559 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004560 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4561 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004562 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004563
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4567 psa_set_key_algorithm(&attributes, alg);
4568 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4571 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4574 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 if (alg != PSA_ALG_ECB_NO_PADDING) {
4577 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4578 iv, iv_size,
4579 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004580 }
4581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4583 TEST_LE_U(output1_buffer_size,
4584 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4585 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4590 output1, output1_buffer_size,
4591 &function_output_length));
4592 TEST_LE_U(function_output_length,
4593 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4594 TEST_LE_U(function_output_length,
4595 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004596 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 PSA_ASSERT(psa_cipher_update(&operation1,
4599 input->x + first_part_size,
4600 input->len - first_part_size,
4601 output1, output1_buffer_size,
4602 &function_output_length));
4603 TEST_LE_U(function_output_length,
4604 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4605 alg,
4606 input->len - first_part_size));
4607 TEST_LE_U(function_output_length,
4608 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004609 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 PSA_ASSERT(psa_cipher_finish(&operation1,
4612 output1 + output1_length,
4613 output1_buffer_size - output1_length,
4614 &function_output_length));
4615 TEST_LE_U(function_output_length,
4616 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4617 TEST_LE_U(function_output_length,
4618 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004619 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004622
Gilles Peskine048b7f02018-06-08 14:20:49 +02004623 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 TEST_LE_U(output2_buffer_size,
4625 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4626 TEST_LE_U(output2_buffer_size,
4627 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4628 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 if (iv_length > 0) {
4631 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4632 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004633 }
Moran Pekerded84402018-06-06 16:36:50 +03004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4636 output2, output2_buffer_size,
4637 &function_output_length));
4638 TEST_LE_U(function_output_length,
4639 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4640 TEST_LE_U(function_output_length,
4641 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004642 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 PSA_ASSERT(psa_cipher_update(&operation2,
4645 output1 + first_part_size,
4646 output1_length - first_part_size,
4647 output2, output2_buffer_size,
4648 &function_output_length));
4649 TEST_LE_U(function_output_length,
4650 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4651 alg,
4652 output1_length - first_part_size));
4653 TEST_LE_U(function_output_length,
4654 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004655 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004656
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 PSA_ASSERT(psa_cipher_finish(&operation2,
4658 output2 + output2_length,
4659 output2_buffer_size - output2_length,
4660 &function_output_length));
4661 TEST_LE_U(function_output_length,
4662 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4663 TEST_LE_U(function_output_length,
4664 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004665 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004670
4671exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 psa_cipher_abort(&operation1);
4673 psa_cipher_abort(&operation2);
4674 mbedtls_free(output1);
4675 mbedtls_free(output2);
4676 psa_destroy_key(key);
4677 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004678}
4679/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004680
Gilles Peskine20035e32018-02-03 22:44:14 +01004681/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004682void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4683 int alg_arg,
4684 data_t *nonce,
4685 data_t *additional_data,
4686 data_t *input_data,
4687 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004688{
Ronald Cron5425a212020-08-04 14:58:35 +02004689 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004690 psa_key_type_t key_type = key_type_arg;
4691 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004692 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004693 unsigned char *output_data = NULL;
4694 size_t output_size = 0;
4695 size_t output_length = 0;
4696 unsigned char *output_data2 = NULL;
4697 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004698 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004699 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4705 psa_set_key_algorithm(&attributes, alg);
4706 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4709 &key));
4710 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4711 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004712
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4714 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004715 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4716 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4718 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4719 TEST_EQUAL(output_size,
4720 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4721 TEST_LE_U(output_size,
4722 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004723 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 status = psa_aead_encrypt(key, alg,
4727 nonce->x, nonce->len,
4728 additional_data->x,
4729 additional_data->len,
4730 input_data->x, input_data->len,
4731 output_data, output_size,
4732 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004733
4734 /* If the operation is not supported, just skip and not fail in case the
4735 * encryption involves a common limitation of cryptography hardwares and
4736 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 if (status == PSA_ERROR_NOT_SUPPORTED) {
4738 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4739 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004740 }
4741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if (PSA_SUCCESS == expected_result) {
4745 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004746
Gilles Peskine003a4a92019-05-14 16:09:40 +02004747 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4748 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 TEST_EQUAL(input_data->len,
4750 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 TEST_LE_U(input_data->len,
4753 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 TEST_EQUAL(psa_aead_decrypt(key, alg,
4756 nonce->x, nonce->len,
4757 additional_data->x,
4758 additional_data->len,
4759 output_data, output_length,
4760 output_data2, output_length,
4761 &output_length2),
4762 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 ASSERT_COMPARE(input_data->x, input_data->len,
4765 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004766 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004767
Gilles Peskinea1cac842018-06-11 19:33:02 +02004768exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 psa_destroy_key(key);
4770 mbedtls_free(output_data);
4771 mbedtls_free(output_data2);
4772 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004773}
4774/* END_CASE */
4775
4776/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004777void aead_encrypt(int key_type_arg, data_t *key_data,
4778 int alg_arg,
4779 data_t *nonce,
4780 data_t *additional_data,
4781 data_t *input_data,
4782 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004783{
Ronald Cron5425a212020-08-04 14:58:35 +02004784 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004785 psa_key_type_t key_type = key_type_arg;
4786 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004787 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004788 unsigned char *output_data = NULL;
4789 size_t output_size = 0;
4790 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004791 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004792 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004795
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4797 psa_set_key_algorithm(&attributes, alg);
4798 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004799
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4801 &key));
4802 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4803 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4806 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004807 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4808 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 TEST_EQUAL(output_size,
4810 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4811 TEST_LE_U(output_size,
4812 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4813 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 status = psa_aead_encrypt(key, alg,
4816 nonce->x, nonce->len,
4817 additional_data->x, additional_data->len,
4818 input_data->x, input_data->len,
4819 output_data, output_size,
4820 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004821
Ronald Cron28a45ed2021-02-09 20:35:42 +01004822 /* If the operation is not supported, just skip and not fail in case the
4823 * encryption involves a common limitation of cryptography hardwares and
4824 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 if (status == PSA_ERROR_NOT_SUPPORTED) {
4826 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4827 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004828 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004829
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 PSA_ASSERT(status);
4831 ASSERT_COMPARE(expected_result->x, expected_result->len,
4832 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004833
Gilles Peskinea1cac842018-06-11 19:33:02 +02004834exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 psa_destroy_key(key);
4836 mbedtls_free(output_data);
4837 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004838}
4839/* END_CASE */
4840
4841/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004842void aead_decrypt(int key_type_arg, data_t *key_data,
4843 int alg_arg,
4844 data_t *nonce,
4845 data_t *additional_data,
4846 data_t *input_data,
4847 data_t *expected_data,
4848 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004849{
Ronald Cron5425a212020-08-04 14:58:35 +02004850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004851 psa_key_type_t key_type = key_type_arg;
4852 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004853 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004854 unsigned char *output_data = NULL;
4855 size_t output_size = 0;
4856 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004857 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004858 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004859 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4864 psa_set_key_algorithm(&attributes, alg);
4865 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4868 &key));
4869 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4870 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4873 alg);
4874 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4875 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004876 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4877 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 TEST_EQUAL(output_size,
4879 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4880 TEST_LE_U(output_size,
4881 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004882 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 status = psa_aead_decrypt(key, alg,
4886 nonce->x, nonce->len,
4887 additional_data->x,
4888 additional_data->len,
4889 input_data->x, input_data->len,
4890 output_data, output_size,
4891 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004892
Ronald Cron28a45ed2021-02-09 20:35:42 +01004893 /* If the operation is not supported, just skip and not fail in case the
4894 * decryption involves a common limitation of cryptography hardwares and
4895 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 if (status == PSA_ERROR_NOT_SUPPORTED) {
4897 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4898 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004899 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 if (expected_result == PSA_SUCCESS) {
4904 ASSERT_COMPARE(expected_data->x, expected_data->len,
4905 output_data, output_length);
4906 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004907
Gilles Peskinea1cac842018-06-11 19:33:02 +02004908exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 psa_destroy_key(key);
4910 mbedtls_free(output_data);
4911 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004912}
4913/* END_CASE */
4914
4915/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004916void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4917 int alg_arg,
4918 data_t *nonce,
4919 data_t *additional_data,
4920 data_t *input_data,
4921 int do_set_lengths,
4922 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004923{
Paul Elliottd3f82412021-06-16 16:52:21 +01004924 size_t ad_part_len = 0;
4925 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004926 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4929 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004930
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 if (do_set_lengths) {
4932 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004933 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004935 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004937 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004938
4939 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 if (!aead_multipart_internal_func(key_type_arg, key_data,
4941 alg_arg, nonce,
4942 additional_data,
4943 ad_part_len,
4944 input_data, -1,
4945 set_lengths_method,
4946 expected_output,
4947 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004948 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004949 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 /* length(0) part, length(ad_part_len) part, length(0) part... */
4952 mbedtls_test_set_step(1000 + ad_part_len);
4953
4954 if (!aead_multipart_internal_func(key_type_arg, key_data,
4955 alg_arg, nonce,
4956 additional_data,
4957 ad_part_len,
4958 input_data, -1,
4959 set_lengths_method,
4960 expected_output,
4961 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004962 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 }
4964 }
4965
4966 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4967 /* Split data into length(data_part_len) parts. */
4968 mbedtls_test_set_step(2000 + data_part_len);
4969
4970 if (do_set_lengths) {
4971 if (data_part_len & 0x01) {
4972 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4973 } else {
4974 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4975 }
4976 }
4977
4978 if (!aead_multipart_internal_func(key_type_arg, key_data,
4979 alg_arg, nonce,
4980 additional_data, -1,
4981 input_data, data_part_len,
4982 set_lengths_method,
4983 expected_output,
4984 1, 0)) {
4985 break;
4986 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004987
4988 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 if (!aead_multipart_internal_func(key_type_arg, key_data,
4992 alg_arg, nonce,
4993 additional_data, -1,
4994 input_data, data_part_len,
4995 set_lengths_method,
4996 expected_output,
4997 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004998 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005000 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005001
Paul Elliott8fc45162021-06-23 16:06:01 +01005002 /* Goto is required to silence warnings about unused labels, as we
5003 * don't actually do any test assertions in this function. */
5004 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005005}
5006/* END_CASE */
5007
5008/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005009void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5010 int alg_arg,
5011 data_t *nonce,
5012 data_t *additional_data,
5013 data_t *input_data,
5014 int do_set_lengths,
5015 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005016{
Paul Elliottd3f82412021-06-16 16:52:21 +01005017 size_t ad_part_len = 0;
5018 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005019 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005022 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005024
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 if (do_set_lengths) {
5026 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005027 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005029 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005031 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 if (!aead_multipart_internal_func(key_type_arg, key_data,
5034 alg_arg, nonce,
5035 additional_data,
5036 ad_part_len,
5037 input_data, -1,
5038 set_lengths_method,
5039 expected_output,
5040 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005041 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005043
5044 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005046
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 if (!aead_multipart_internal_func(key_type_arg, key_data,
5048 alg_arg, nonce,
5049 additional_data,
5050 ad_part_len,
5051 input_data, -1,
5052 set_lengths_method,
5053 expected_output,
5054 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005055 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005057 }
5058
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005060 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005062
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 if (do_set_lengths) {
5064 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005065 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005067 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005069 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 if (!aead_multipart_internal_func(key_type_arg, key_data,
5072 alg_arg, nonce,
5073 additional_data, -1,
5074 input_data, data_part_len,
5075 set_lengths_method,
5076 expected_output,
5077 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005078 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005080
5081 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 if (!aead_multipart_internal_func(key_type_arg, key_data,
5085 alg_arg, nonce,
5086 additional_data, -1,
5087 input_data, data_part_len,
5088 set_lengths_method,
5089 expected_output,
5090 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005091 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005093 }
5094
Paul Elliott8fc45162021-06-23 16:06:01 +01005095 /* Goto is required to silence warnings about unused labels, as we
5096 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005097 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005098}
5099/* END_CASE */
5100
5101/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005102void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5103 int alg_arg,
5104 int nonce_length,
5105 int expected_nonce_length_arg,
5106 data_t *additional_data,
5107 data_t *input_data,
5108 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005109{
5110
5111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5112 psa_key_type_t key_type = key_type_arg;
5113 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005114 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005115 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5117 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005118 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005119 size_t actual_nonce_length = 0;
5120 size_t expected_nonce_length = expected_nonce_length_arg;
5121 unsigned char *output = NULL;
5122 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005123 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005124 size_t ciphertext_size = 0;
5125 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005126 size_t tag_length = 0;
5127 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5132 psa_set_key_algorithm(&attributes, alg);
5133 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5136 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005151
5152 /* If the operation is not supported, just skip and not fail in case the
5153 * encryption involves a common limitation of cryptography hardwares and
5154 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 if (status == PSA_ERROR_NOT_SUPPORTED) {
5156 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5157 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005158 }
5159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005161
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5163 nonce_length,
5164 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005165
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005169
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 if (expected_status == PSA_SUCCESS) {
5171 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5172 alg));
5173 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005174
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005176
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005178 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5180 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5183 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5186 output, output_size,
5187 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5190 &ciphertext_length, tag_buffer,
5191 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005192 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005193
5194exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 psa_destroy_key(key);
5196 mbedtls_free(output);
5197 mbedtls_free(ciphertext);
5198 psa_aead_abort(&operation);
5199 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005200}
5201/* END_CASE */
5202
5203/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005204void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5205 int alg_arg,
5206 int nonce_length_arg,
5207 int set_lengths_method_arg,
5208 data_t *additional_data,
5209 data_t *input_data,
5210 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005211{
5212
5213 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5214 psa_key_type_t key_type = key_type_arg;
5215 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005216 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005217 uint8_t *nonce_buffer = NULL;
5218 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5219 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5220 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005221 unsigned char *output = NULL;
5222 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005223 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005224 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005225 size_t ciphertext_size = 0;
5226 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005227 size_t tag_length = 0;
5228 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005229 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005230 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5235 psa_set_key_algorithm(&attributes, alg);
5236 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005237
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5239 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005242
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005244
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005246
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005250
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005252
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005254
5255 /* If the operation is not supported, just skip and not fail in case the
5256 * encryption involves a common limitation of cryptography hardwares and
5257 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 if (status == PSA_ERROR_NOT_SUPPORTED) {
5259 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5260 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005261 }
5262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005264
Paul Elliott4023ffd2021-09-10 16:21:22 +01005265 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 if (nonce_length_arg == -1) {
5267 /* Arbitrary size buffer, to test zero length valid buffer. */
5268 ASSERT_ALLOC(nonce_buffer, 4);
5269 nonce_length = 0;
5270 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005271 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 nonce_length = (size_t) nonce_length_arg;
5273 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005274
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 if (nonce_buffer) {
5276 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005277 nonce_buffer[index] = 'a' + index;
5278 }
Paul Elliott66696b52021-08-16 18:42:41 +01005279 }
Paul Elliott863864a2021-07-23 17:28:31 +01005280 }
5281
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5283 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5284 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005285 }
5286
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005288
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 if (expected_status == PSA_SUCCESS) {
5292 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5293 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5294 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005295 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005297 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 }
Paul Elliott863864a2021-07-23 17:28:31 +01005299
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005300 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5302 additional_data->len),
5303 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005304
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5306 output, output_size,
5307 &ciphertext_length),
5308 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005309
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5311 &ciphertext_length, tag_buffer,
5312 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5313 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005314 }
5315
5316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 psa_destroy_key(key);
5318 mbedtls_free(output);
5319 mbedtls_free(ciphertext);
5320 mbedtls_free(nonce_buffer);
5321 psa_aead_abort(&operation);
5322 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005323}
5324/* END_CASE */
5325
5326/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005327void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005328 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005329 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005330 data_t *nonce,
5331 data_t *additional_data,
5332 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005334{
5335
5336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5337 psa_key_type_t key_type = key_type_arg;
5338 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005339 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5341 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5342 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005343 unsigned char *output = NULL;
5344 unsigned char *ciphertext = NULL;
5345 size_t output_size = output_size_arg;
5346 size_t ciphertext_size = 0;
5347 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005348 size_t tag_length = 0;
5349 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5350
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005352
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5354 psa_set_key_algorithm(&attributes, alg);
5355 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005356
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5358 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005361
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005363
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005365
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005367
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005369
5370 /* If the operation is not supported, just skip and not fail in case the
5371 * encryption involves a common limitation of cryptography hardwares and
5372 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 if (status == PSA_ERROR_NOT_SUPPORTED) {
5374 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5375 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005376 }
5377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5381 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005382
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5386 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005387
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 status = psa_aead_update(&operation, input_data->x, input_data->len,
5389 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005394 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5396 &ciphertext_length, tag_buffer,
5397 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005398 }
5399
5400exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 psa_destroy_key(key);
5402 mbedtls_free(output);
5403 mbedtls_free(ciphertext);
5404 psa_aead_abort(&operation);
5405 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005406}
5407/* END_CASE */
5408
Paul Elliott91b021e2021-07-23 18:52:31 +01005409/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005410void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5411 int alg_arg,
5412 int finish_ciphertext_size_arg,
5413 int tag_size_arg,
5414 data_t *nonce,
5415 data_t *additional_data,
5416 data_t *input_data,
5417 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005418{
5419
5420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5421 psa_key_type_t key_type = key_type_arg;
5422 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005423 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5425 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5426 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005427 unsigned char *ciphertext = NULL;
5428 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005429 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005430 size_t ciphertext_size = 0;
5431 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5433 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005434 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005435
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005437
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5439 psa_set_key_algorithm(&attributes, alg);
5440 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005441
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5443 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005452
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005456
5457 /* If the operation is not supported, just skip and not fail in case the
5458 * encryption involves a common limitation of cryptography hardwares and
5459 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 if (status == PSA_ERROR_NOT_SUPPORTED) {
5461 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5462 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005463 }
5464
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005468
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5470 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5473 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5476 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005477
5478 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 status = psa_aead_finish(&operation, finish_ciphertext,
5480 finish_ciphertext_size,
5481 &ciphertext_length, tag_buffer,
5482 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005485
5486exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 psa_destroy_key(key);
5488 mbedtls_free(ciphertext);
5489 mbedtls_free(finish_ciphertext);
5490 mbedtls_free(tag_buffer);
5491 psa_aead_abort(&operation);
5492 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005493}
5494/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005495
5496/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005497void aead_multipart_verify(int key_type_arg, data_t *key_data,
5498 int alg_arg,
5499 data_t *nonce,
5500 data_t *additional_data,
5501 data_t *input_data,
5502 data_t *tag,
5503 int tag_usage_arg,
5504 int expected_setup_status_arg,
5505 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005506{
5507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5508 psa_key_type_t key_type = key_type_arg;
5509 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005510 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5512 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5513 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005514 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005515 unsigned char *plaintext = NULL;
5516 unsigned char *finish_plaintext = NULL;
5517 size_t plaintext_size = 0;
5518 size_t plaintext_length = 0;
5519 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005520 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005521 unsigned char *tag_buffer = NULL;
5522 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005523
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005525
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5527 psa_set_key_algorithm(&attributes, alg);
5528 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005529
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5531 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5536 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005545
5546 /* If the operation is not supported, just skip and not fail in case the
5547 * encryption involves a common limitation of cryptography hardwares and
5548 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 if (status == PSA_ERROR_NOT_SUPPORTED) {
5550 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5551 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005552 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005554
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005556 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 }
Paul Elliott9961a662021-09-17 19:19:02 +01005558
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005562
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 status = psa_aead_set_lengths(&operation, additional_data->len,
5564 input_data->len);
5565 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005566
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5568 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5571 input_data->len,
5572 plaintext, plaintext_size,
5573 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005574
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005576 tag_buffer = tag->x;
5577 tag_size = tag->len;
5578 }
5579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 status = psa_aead_verify(&operation, finish_plaintext,
5581 verify_plaintext_size,
5582 &plaintext_length,
5583 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005586
5587exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 psa_destroy_key(key);
5589 mbedtls_free(plaintext);
5590 mbedtls_free(finish_plaintext);
5591 psa_aead_abort(&operation);
5592 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005593}
5594/* END_CASE */
5595
Paul Elliott9961a662021-09-17 19:19:02 +01005596/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005597void aead_multipart_setup(int key_type_arg, data_t *key_data,
5598 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005599{
5600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5601 psa_key_type_t key_type = key_type_arg;
5602 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005603 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5605 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5606 psa_status_t expected_status = expected_status_arg;
5607
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005609
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 psa_set_key_usage_flags(&attributes,
5611 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5612 psa_set_key_algorithm(&attributes, alg);
5613 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5616 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005617
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005623
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005625
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005627
5628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 psa_destroy_key(key);
5630 psa_aead_abort(&operation);
5631 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005632}
5633/* END_CASE */
5634
5635/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005636void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5637 int alg_arg,
5638 data_t *nonce,
5639 data_t *additional_data,
5640 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005641{
5642 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5643 psa_key_type_t key_type = key_type_arg;
5644 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005645 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005646 unsigned char *output_data = NULL;
5647 unsigned char *final_data = NULL;
5648 size_t output_size = 0;
5649 size_t finish_output_size = 0;
5650 size_t output_length = 0;
5651 size_t key_bits = 0;
5652 size_t tag_length = 0;
5653 size_t tag_size = 0;
5654 size_t nonce_length = 0;
5655 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5656 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5657 size_t output_part_length = 0;
5658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5659
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005661
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 psa_set_key_usage_flags(&attributes,
5663 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5664 psa_set_key_algorithm(&attributes, alg);
5665 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005666
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5668 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005669
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5671 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005672
Gilles Peskine449bd832023-01-11 14:50:10 +01005673 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005674
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005676
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005684
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005686
5687 /* Test all operations error without calling setup first. */
5688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5690 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5695 PSA_AEAD_NONCE_MAX_SIZE,
5696 &nonce_length),
5697 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005700
Paul Elliott481be342021-07-16 17:38:47 +01005701 /* ------------------------------------------------------- */
5702
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5704 input_data->len),
5705 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005708
Paul Elliott481be342021-07-16 17:38:47 +01005709 /* ------------------------------------------------------- */
5710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5712 additional_data->len),
5713 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005716
Paul Elliott481be342021-07-16 17:38:47 +01005717 /* ------------------------------------------------------- */
5718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5720 input_data->len, output_data,
5721 output_size, &output_length),
5722 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005725
Paul Elliott481be342021-07-16 17:38:47 +01005726 /* ------------------------------------------------------- */
5727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5729 finish_output_size,
5730 &output_part_length,
5731 tag_buffer, tag_length,
5732 &tag_size),
5733 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005736
Paul Elliott481be342021-07-16 17:38:47 +01005737 /* ------------------------------------------------------- */
5738
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5740 finish_output_size,
5741 &output_part_length,
5742 tag_buffer,
5743 tag_length),
5744 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005747
5748 /* Test for double setups. */
5749
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5753 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756
Paul Elliott481be342021-07-16 17:38:47 +01005757 /* ------------------------------------------------------- */
5758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5762 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005765
Paul Elliott374a2be2021-07-16 17:53:40 +01005766 /* ------------------------------------------------------- */
5767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5771 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005774
5775 /* ------------------------------------------------------- */
5776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5780 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005783
Paul Elliottc23a9a02021-06-21 18:32:46 +01005784 /* Test for not setting a nonce. */
5785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5789 additional_data->len),
5790 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005793
Paul Elliott7f628422021-09-01 12:08:29 +01005794 /* ------------------------------------------------------- */
5795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5799 input_data->len, output_data,
5800 output_size, &output_length),
5801 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005804
Paul Elliottbdc2c682021-09-21 18:37:10 +01005805 /* ------------------------------------------------------- */
5806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5810 finish_output_size,
5811 &output_part_length,
5812 tag_buffer, tag_length,
5813 &tag_size),
5814 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005817
5818 /* ------------------------------------------------------- */
5819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5823 finish_output_size,
5824 &output_part_length,
5825 tag_buffer,
5826 tag_length),
5827 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005830
Paul Elliottc23a9a02021-06-21 18:32:46 +01005831 /* Test for double setting nonce. */
5832
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005834
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5838 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005841
Paul Elliott374a2be2021-07-16 17:53:40 +01005842 /* Test for double generating nonce. */
5843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5847 PSA_AEAD_NONCE_MAX_SIZE,
5848 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5851 PSA_AEAD_NONCE_MAX_SIZE,
5852 &nonce_length),
5853 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005854
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005857
5858 /* Test for generate nonce then set and vice versa */
5859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5863 PSA_AEAD_NONCE_MAX_SIZE,
5864 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5867 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005870
Andrzej Kurekad837522021-12-15 15:28:49 +01005871 /* Test for generating nonce after calling set lengths */
5872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5876 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5879 PSA_AEAD_NONCE_MAX_SIZE,
5880 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005883
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005884 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 if (operation.alg == PSA_ALG_CCM) {
5889 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5890 input_data->len),
5891 PSA_ERROR_INVALID_ARGUMENT);
5892 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5893 PSA_AEAD_NONCE_MAX_SIZE,
5894 &nonce_length),
5895 PSA_ERROR_BAD_STATE);
5896 } else {
5897 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5898 input_data->len));
5899 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5900 PSA_AEAD_NONCE_MAX_SIZE,
5901 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005902 }
5903
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005905
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005906 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005907#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5911 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5912 input_data->len),
5913 PSA_ERROR_INVALID_ARGUMENT);
5914 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5915 PSA_AEAD_NONCE_MAX_SIZE,
5916 &nonce_length),
5917 PSA_ERROR_BAD_STATE);
5918 } else {
5919 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5920 input_data->len));
5921 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5922 PSA_AEAD_NONCE_MAX_SIZE,
5923 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005924 }
5925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005927#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005928
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005929 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5934 PSA_AEAD_NONCE_MAX_SIZE,
5935 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005936
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 if (operation.alg == PSA_ALG_CCM) {
5938 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5939 input_data->len),
5940 PSA_ERROR_INVALID_ARGUMENT);
5941 } else {
5942 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5943 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005944 }
5945
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005947
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005948 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005949 /* Test for setting nonce after calling set lengths */
5950
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005952
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5954 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005955
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005959
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005960 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005963
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 if (operation.alg == PSA_ALG_CCM) {
5965 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5966 input_data->len),
5967 PSA_ERROR_INVALID_ARGUMENT);
5968 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5969 PSA_ERROR_BAD_STATE);
5970 } else {
5971 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5972 input_data->len));
5973 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005974 }
5975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005977
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005978 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005979#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5983 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5984 input_data->len),
5985 PSA_ERROR_INVALID_ARGUMENT);
5986 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5987 PSA_ERROR_BAD_STATE);
5988 } else {
5989 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5990 input_data->len));
5991 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005992 }
5993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005995#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005996
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005997 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005998
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 if (operation.alg == PSA_ALG_CCM) {
6004 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6005 input_data->len),
6006 PSA_ERROR_INVALID_ARGUMENT);
6007 } else {
6008 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6009 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006010 }
6011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006013
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006014 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006015#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006017
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 if (operation.alg == PSA_ALG_GCM) {
6019 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6020 SIZE_MAX),
6021 PSA_ERROR_INVALID_ARGUMENT);
6022 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6023 PSA_ERROR_BAD_STATE);
6024 } else if (operation.alg != PSA_ALG_CCM) {
6025 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6026 SIZE_MAX));
6027 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006028 }
6029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006031#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006032
Tom Cosgrove1797b052022-12-04 17:19:59 +00006033 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006034#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006038
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (operation.alg == PSA_ALG_GCM) {
6040 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6041 SIZE_MAX),
6042 PSA_ERROR_INVALID_ARGUMENT);
6043 } else if (operation.alg != PSA_ALG_CCM) {
6044 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6045 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006046 }
6047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006049#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006050
6051 /* ------------------------------------------------------- */
6052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6058 PSA_AEAD_NONCE_MAX_SIZE,
6059 &nonce_length),
6060 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006063
Paul Elliott7220cae2021-06-22 17:25:57 +01006064 /* Test for generating nonce in decrypt setup. */
6065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006067
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6069 PSA_AEAD_NONCE_MAX_SIZE,
6070 &nonce_length),
6071 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006072
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006074
Paul Elliottc23a9a02021-06-21 18:32:46 +01006075 /* Test for setting lengths twice. */
6076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006078
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006080
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6082 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6085 input_data->len),
6086 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006089
Andrzej Kurekad837522021-12-15 15:28:49 +01006090 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006093
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6099 additional_data->len),
6100 PSA_ERROR_BAD_STATE);
6101 } else {
6102 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6103 additional_data->len));
6104
6105 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6106 input_data->len),
6107 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006108 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006110
6111 /* ------------------------------------------------------- */
6112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 if (operation.alg == PSA_ALG_CCM) {
6118 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6119 input_data->len, output_data,
6120 output_size, &output_length),
6121 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 } else {
6124 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6125 input_data->len, output_data,
6126 output_size, &output_length));
6127
6128 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6129 input_data->len),
6130 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006131 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006133
6134 /* ------------------------------------------------------- */
6135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006137
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 if (operation.alg == PSA_ALG_CCM) {
6141 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6142 finish_output_size,
6143 &output_part_length,
6144 tag_buffer, tag_length,
6145 &tag_size));
6146 } else {
6147 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6148 finish_output_size,
6149 &output_part_length,
6150 tag_buffer, tag_length,
6151 &tag_size));
6152
6153 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6154 input_data->len),
6155 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006156 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006158
6159 /* Test for setting lengths after generating nonce + already starting data. */
6160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6164 PSA_AEAD_NONCE_MAX_SIZE,
6165 &nonce_length));
6166 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6169 additional_data->len),
6170 PSA_ERROR_BAD_STATE);
6171 } else {
6172 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6173 additional_data->len));
6174
6175 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6176 input_data->len),
6177 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006178 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006180
6181 /* ------------------------------------------------------- */
6182
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6186 PSA_AEAD_NONCE_MAX_SIZE,
6187 &nonce_length));
6188 if (operation.alg == PSA_ALG_CCM) {
6189 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6190 input_data->len, output_data,
6191 output_size, &output_length),
6192 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006193
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 } else {
6195 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6196 input_data->len, output_data,
6197 output_size, &output_length));
6198
6199 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6200 input_data->len),
6201 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006202 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006204
6205 /* ------------------------------------------------------- */
6206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006208
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6210 PSA_AEAD_NONCE_MAX_SIZE,
6211 &nonce_length));
6212 if (operation.alg == PSA_ALG_CCM) {
6213 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6214 finish_output_size,
6215 &output_part_length,
6216 tag_buffer, tag_length,
6217 &tag_size));
6218 } else {
6219 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6220 finish_output_size,
6221 &output_part_length,
6222 tag_buffer, tag_length,
6223 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6226 input_data->len),
6227 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006228 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006230
Paul Elliott243080c2021-07-21 19:01:17 +01006231 /* Test for not sending any additional data or data after setting non zero
6232 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006235
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6239 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006240
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6242 finish_output_size,
6243 &output_part_length,
6244 tag_buffer, tag_length,
6245 &tag_size),
6246 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006247
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006249
Paul Elliott243080c2021-07-21 19:01:17 +01006250 /* Test for not sending any additional data or data after setting non-zero
6251 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6258 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6261 finish_output_size,
6262 &output_part_length,
6263 tag_buffer,
6264 tag_length),
6265 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006268
Paul Elliott243080c2021-07-21 19:01:17 +01006269 /* Test for not sending any additional data after setting a non-zero length
6270 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6277 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6280 input_data->len, output_data,
6281 output_size, &output_length),
6282 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006285
Paul Elliottf94bd992021-09-19 18:15:59 +01006286 /* Test for not sending any data after setting a non-zero length for it.*/
6287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006289
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6293 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6296 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6299 finish_output_size,
6300 &output_part_length,
6301 tag_buffer, tag_length,
6302 &tag_size),
6303 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006306
Paul Elliottb0450fe2021-09-01 15:06:26 +01006307 /* Test for sending too much additional data after setting lengths. */
6308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006314
6315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6317 additional_data->len),
6318 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006321
Paul Elliotta2a09b02021-09-22 14:56:40 +01006322 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6329 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6332 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6335 1),
6336 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006339
Paul Elliottb0450fe2021-09-01 15:06:26 +01006340 /* Test for sending too much data after setting lengths. */
6341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6349 input_data->len, output_data,
6350 output_size, &output_length),
6351 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006354
Paul Elliotta2a09b02021-09-22 14:56:40 +01006355 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006358
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6362 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6365 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6368 input_data->len, output_data,
6369 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6372 1, output_data,
6373 output_size, &output_length),
6374 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006377
Paul Elliottc23a9a02021-06-21 18:32:46 +01006378 /* Test sending additional data after data. */
6379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 if (operation.alg != PSA_ALG_CCM) {
6385 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6386 input_data->len, output_data,
6387 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6390 additional_data->len),
6391 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006392 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006394
Paul Elliott534d0b42021-06-22 19:15:20 +01006395 /* Test calling finish on decryption. */
6396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6402 finish_output_size,
6403 &output_part_length,
6404 tag_buffer, tag_length,
6405 &tag_size),
6406 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006409
6410 /* Test calling verify on encryption. */
6411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6417 finish_output_size,
6418 &output_part_length,
6419 tag_buffer,
6420 tag_length),
6421 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006424
6425
Paul Elliottc23a9a02021-06-21 18:32:46 +01006426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 psa_destroy_key(key);
6428 psa_aead_abort(&operation);
6429 mbedtls_free(output_data);
6430 mbedtls_free(final_data);
6431 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006432}
6433/* END_CASE */
6434
6435/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006436void signature_size(int type_arg,
6437 int bits,
6438 int alg_arg,
6439 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006440{
6441 psa_key_type_t type = type_arg;
6442 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006444
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006446
Gilles Peskinee59236f2018-01-27 23:32:46 +01006447exit:
6448 ;
6449}
6450/* END_CASE */
6451
6452/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006453void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6454 int alg_arg, data_t *input_data,
6455 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006456{
Ronald Cron5425a212020-08-04 14:58:35 +02006457 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006458 psa_key_type_t key_type = key_type_arg;
6459 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006460 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006461 unsigned char *signature = NULL;
6462 size_t signature_size;
6463 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006465
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006467
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6469 psa_set_key_algorithm(&attributes, alg);
6470 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006471
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6473 &key));
6474 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6475 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006476
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006477 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006478 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6480 key_bits, alg);
6481 TEST_ASSERT(signature_size != 0);
6482 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6483 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006484
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006485 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 PSA_ASSERT(psa_sign_hash(key, alg,
6487 input_data->x, input_data->len,
6488 signature, signature_size,
6489 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006490 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 ASSERT_COMPARE(output_data->x, output_data->len,
6492 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006493
6494exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006495 /*
6496 * Key attributes may have been returned by psa_get_key_attributes()
6497 * thus reset them as required.
6498 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006500
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 psa_destroy_key(key);
6502 mbedtls_free(signature);
6503 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006504}
6505/* END_CASE */
6506
Paul Elliott712d5122022-12-07 14:03:10 +00006507/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006508/**
6509 * sign_hash_interruptible() test intentions:
6510 *
6511 * Note: This test can currently only handle ECDSA.
6512 *
6513 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006514 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006515 *
6516 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6517 * expected for different max_ops values.
6518 *
6519 * 3. Test that the number of ops done prior to start and after abort is zero
6520 * and that each successful stage completes some ops (this is not mandated by
6521 * the PSA specification, but is currently the case).
6522 *
6523 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6524 * complete() calls does not alter the number of ops returned.
6525 */
Paul Elliott712d5122022-12-07 14:03:10 +00006526void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6527 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006528 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006529{
6530 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6531 psa_key_type_t key_type = key_type_arg;
6532 psa_algorithm_t alg = alg_arg;
6533 size_t key_bits;
6534 unsigned char *signature = NULL;
6535 size_t signature_size;
6536 size_t signature_length = 0xdeadbeef;
6537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6538 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006539 uint32_t num_ops = 0;
6540 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006541 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006542 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006543 size_t min_completes = 0;
6544 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006545
Paul Elliott712d5122022-12-07 14:03:10 +00006546 psa_sign_hash_interruptible_operation_t operation =
6547 psa_sign_hash_interruptible_operation_init();
6548
6549 PSA_ASSERT(psa_crypto_init());
6550
6551 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6552 psa_set_key_algorithm(&attributes, alg);
6553 psa_set_key_type(&attributes, key_type);
6554
6555 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6556 &key));
6557 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6558 key_bits = psa_get_key_bits(&attributes);
6559
6560 /* Allocate a buffer which has the size advertised by the
6561 * library. */
6562 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6563 key_bits, alg);
6564 TEST_ASSERT(signature_size != 0);
6565 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6566 ASSERT_ALLOC(signature, signature_size);
6567
Paul Elliott0c683352022-12-16 19:16:56 +00006568 psa_interruptible_set_max_ops(max_ops);
6569
Paul Elliott6f600372023-02-06 18:41:05 +00006570 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6571 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006572
Paul Elliott712d5122022-12-07 14:03:10 +00006573 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6574 TEST_ASSERT(num_ops_prior == 0);
6575
6576 /* Start performing the signature. */
6577 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6578 input_data->x, input_data->len));
6579
6580 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6581 TEST_ASSERT(num_ops_prior == 0);
6582
6583 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006584 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006585 status = psa_sign_hash_complete(&operation, signature, signature_size,
6586 &signature_length);
6587
Paul Elliott0c683352022-12-16 19:16:56 +00006588 num_completes++;
6589
Paul Elliott712d5122022-12-07 14:03:10 +00006590 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6591 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006592 /* We are asserting here that every complete makes progress
6593 * (completes some ops), which is true of the internal
6594 * implementation and probably any implementation, however this is
6595 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006596 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006597
Paul Elliott712d5122022-12-07 14:03:10 +00006598 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006599
6600 /* Ensure calling get_num_ops() twice still returns the same
6601 * number of ops as previously reported. */
6602 num_ops = psa_sign_hash_get_num_ops(&operation);
6603
6604 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006605 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006606 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006607
6608 TEST_ASSERT(status == PSA_SUCCESS);
6609
Paul Elliott0c683352022-12-16 19:16:56 +00006610 TEST_LE_U(min_completes, num_completes);
6611 TEST_LE_U(num_completes, max_completes);
6612
Paul Elliott712d5122022-12-07 14:03:10 +00006613 /* Verify that the signature is what is expected. */
6614 ASSERT_COMPARE(output_data->x, output_data->len,
6615 signature, signature_length);
6616
6617 PSA_ASSERT(psa_sign_hash_abort(&operation));
6618
Paul Elliott59ad9452022-12-18 15:09:02 +00006619 num_ops = psa_sign_hash_get_num_ops(&operation);
6620 TEST_ASSERT(num_ops == 0);
6621
Paul Elliott712d5122022-12-07 14:03:10 +00006622exit:
6623
6624 /*
6625 * Key attributes may have been returned by psa_get_key_attributes()
6626 * thus reset them as required.
6627 */
6628 psa_reset_key_attributes(&attributes);
6629
6630 psa_destroy_key(key);
6631 mbedtls_free(signature);
6632 PSA_DONE();
6633}
6634/* END_CASE */
6635
Gilles Peskine20035e32018-02-03 22:44:14 +01006636/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006637void sign_hash_fail(int key_type_arg, data_t *key_data,
6638 int alg_arg, data_t *input_data,
6639 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006640{
Ronald Cron5425a212020-08-04 14:58:35 +02006641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006642 psa_key_type_t key_type = key_type_arg;
6643 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006644 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006645 psa_status_t actual_status;
6646 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006647 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006648 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006652
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006654
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6656 psa_set_key_algorithm(&attributes, alg);
6657 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006658
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6660 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006661
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 actual_status = psa_sign_hash(key, alg,
6663 input_data->x, input_data->len,
6664 signature, signature_size,
6665 &signature_length);
6666 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006667 /* The value of *signature_length is unspecified on error, but
6668 * whatever it is, it should be less than signature_size, so that
6669 * if the caller tries to read *signature_length bytes without
6670 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006672
6673exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 psa_reset_key_attributes(&attributes);
6675 psa_destroy_key(key);
6676 mbedtls_free(signature);
6677 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006678}
6679/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006680
Paul Elliott91007972022-12-16 12:21:24 +00006681/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006682/**
6683 * sign_hash_fail_interruptible() test intentions:
6684 *
6685 * Note: This test can currently only handle ECDSA.
6686 *
6687 * 1. Test that various failure cases for interruptible sign hash fail with the
6688 * correct error codes, and at the correct point (at start or during
6689 * complete).
6690 *
6691 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6692 * expected for different max_ops values.
6693 *
6694 * 3. Test that the number of ops done prior to start and after abort is zero
6695 * and that each successful stage completes some ops (this is not mandated by
6696 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006697 *
6698 * 4. Check that calling complete() when start() fails and complete()
6699 * after completion results in a BAD_STATE error.
6700 *
6701 * 5. Check that calling start() again after start fails results in a BAD_STATE
6702 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006703 */
Paul Elliott91007972022-12-16 12:21:24 +00006704void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6705 int alg_arg, data_t *input_data,
6706 int signature_size_arg,
6707 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006708 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006709 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006710{
6711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6712 psa_key_type_t key_type = key_type_arg;
6713 psa_algorithm_t alg = alg_arg;
6714 size_t signature_size = signature_size_arg;
6715 psa_status_t actual_status;
6716 psa_status_t expected_start_status = expected_start_status_arg;
6717 psa_status_t expected_complete_status = expected_complete_status_arg;
6718 unsigned char *signature = NULL;
6719 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006720 uint32_t num_ops = 0;
6721 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006722 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006723 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006724 size_t min_completes = 0;
6725 size_t max_completes = 0;
6726
Paul Elliott91007972022-12-16 12:21:24 +00006727 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6728 psa_sign_hash_interruptible_operation_t operation =
6729 psa_sign_hash_interruptible_operation_init();
6730
6731 ASSERT_ALLOC(signature, signature_size);
6732
6733 PSA_ASSERT(psa_crypto_init());
6734
6735 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6736 psa_set_key_algorithm(&attributes, alg);
6737 psa_set_key_type(&attributes, key_type);
6738
6739 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6740 &key));
6741
Paul Elliott0c683352022-12-16 19:16:56 +00006742 psa_interruptible_set_max_ops(max_ops);
6743
Paul Elliott6f600372023-02-06 18:41:05 +00006744 interruptible_signverify_get_minmax_completes(max_ops,
6745 expected_complete_status,
6746 &min_completes,
6747 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006748
Paul Elliott91007972022-12-16 12:21:24 +00006749 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6750 TEST_ASSERT(num_ops_prior == 0);
6751
6752 /* Start performing the signature. */
6753 actual_status = psa_sign_hash_start(&operation, key, alg,
6754 input_data->x, input_data->len);
6755
6756 TEST_EQUAL(actual_status, expected_start_status);
6757
Paul Elliottc9774412023-02-06 15:14:07 +00006758 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006759 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006760 * start failed. */
6761 actual_status = psa_sign_hash_complete(&operation, signature,
6762 signature_size,
6763 &signature_length);
6764
6765 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6766
6767 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006768 actual_status = psa_sign_hash_start(&operation, key, alg,
6769 input_data->x, input_data->len);
6770
6771 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6772 }
6773
Paul Elliott91007972022-12-16 12:21:24 +00006774 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6775 TEST_ASSERT(num_ops_prior == 0);
6776
Paul Elliott91007972022-12-16 12:21:24 +00006777 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006778 do {
Paul Elliott91007972022-12-16 12:21:24 +00006779 actual_status = psa_sign_hash_complete(&operation, signature,
6780 signature_size,
6781 &signature_length);
6782
Paul Elliott0c683352022-12-16 19:16:56 +00006783 num_completes++;
6784
Paul Elliott334d7262023-01-20 17:29:41 +00006785 if (actual_status == PSA_SUCCESS ||
6786 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006787 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006788 /* We are asserting here that every complete makes progress
6789 * (completes some ops), which is true of the internal
6790 * implementation and probably any implementation, however this is
6791 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006792 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006793
Paul Elliott91007972022-12-16 12:21:24 +00006794 num_ops_prior = num_ops;
6795 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006796 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006797
Paul Elliottc9774412023-02-06 15:14:07 +00006798 TEST_EQUAL(actual_status, expected_complete_status);
6799
Paul Elliottefebad02023-02-15 16:56:45 +00006800 /* Check that another complete returns BAD_STATE. */
6801 actual_status = psa_sign_hash_complete(&operation, signature,
6802 signature_size,
6803 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006804
Paul Elliottefebad02023-02-15 16:56:45 +00006805 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006806
Paul Elliott91007972022-12-16 12:21:24 +00006807 PSA_ASSERT(psa_sign_hash_abort(&operation));
6808
Paul Elliott59ad9452022-12-18 15:09:02 +00006809 num_ops = psa_sign_hash_get_num_ops(&operation);
6810 TEST_ASSERT(num_ops == 0);
6811
Paul Elliott91007972022-12-16 12:21:24 +00006812 /* The value of *signature_length is unspecified on error, but
6813 * whatever it is, it should be less than signature_size, so that
6814 * if the caller tries to read *signature_length bytes without
6815 * checking the error code then they don't overflow a buffer. */
6816 TEST_LE_U(signature_length, signature_size);
6817
Paul Elliott0c683352022-12-16 19:16:56 +00006818 TEST_LE_U(min_completes, num_completes);
6819 TEST_LE_U(num_completes, max_completes);
6820
Paul Elliott91007972022-12-16 12:21:24 +00006821exit:
6822 psa_reset_key_attributes(&attributes);
6823 psa_destroy_key(key);
6824 mbedtls_free(signature);
6825 PSA_DONE();
6826}
6827/* END_CASE */
6828
mohammad16038cc1cee2018-03-28 01:21:33 +03006829/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006830void sign_verify_hash(int key_type_arg, data_t *key_data,
6831 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006832{
Ronald Cron5425a212020-08-04 14:58:35 +02006833 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006834 psa_key_type_t key_type = key_type_arg;
6835 psa_algorithm_t alg = alg_arg;
6836 size_t key_bits;
6837 unsigned char *signature = NULL;
6838 size_t signature_size;
6839 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006841
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006843
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6845 psa_set_key_algorithm(&attributes, alg);
6846 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006847
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6849 &key));
6850 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6851 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006852
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006853 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006854 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6856 key_bits, alg);
6857 TEST_ASSERT(signature_size != 0);
6858 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6859 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006860
6861 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 PSA_ASSERT(psa_sign_hash(key, alg,
6863 input_data->x, input_data->len,
6864 signature, signature_size,
6865 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006866 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 TEST_LE_U(signature_length, signature_size);
6868 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006869
6870 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 PSA_ASSERT(psa_verify_hash(key, alg,
6872 input_data->x, input_data->len,
6873 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006874
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006876 /* Flip a bit in the input and verify that the signature is now
6877 * detected as invalid. Flip a bit at the beginning, not at the end,
6878 * because ECDSA may ignore the last few bits of the input. */
6879 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 TEST_EQUAL(psa_verify_hash(key, alg,
6881 input_data->x, input_data->len,
6882 signature, signature_length),
6883 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006884 }
6885
6886exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006887 /*
6888 * Key attributes may have been returned by psa_get_key_attributes()
6889 * thus reset them as required.
6890 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 psa_destroy_key(key);
6894 mbedtls_free(signature);
6895 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006896}
6897/* END_CASE */
6898
Paul Elliott712d5122022-12-07 14:03:10 +00006899/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006900/**
6901 * sign_verify_hash_interruptible() test intentions:
6902 *
6903 * Note: This test can currently only handle ECDSA.
6904 *
Paul Elliott8c092052023-03-06 17:49:14 +00006905 * 1. Test that we can sign an input hash with the given keypair and then
6906 * afterwards verify that signature. This is currently the only way to test
6907 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006908 *
6909 * 2. Test that after corrupting the hash, the verification detects an invalid
6910 * signature.
6911 *
6912 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6913 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006914 *
6915 * 4. Test that the number of ops done prior to starting signing and after abort
6916 * is zero and that each successful signing stage completes some ops (this is
6917 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006918 */
Paul Elliott712d5122022-12-07 14:03:10 +00006919void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006920 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006921 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006922{
6923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6924 psa_key_type_t key_type = key_type_arg;
6925 psa_algorithm_t alg = alg_arg;
6926 size_t key_bits;
6927 unsigned char *signature = NULL;
6928 size_t signature_size;
6929 size_t signature_length = 0xdeadbeef;
6930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6931 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006932 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006933 uint32_t num_ops = 0;
6934 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006935 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006936 size_t min_completes = 0;
6937 size_t max_completes = 0;
6938
Paul Elliott712d5122022-12-07 14:03:10 +00006939 psa_sign_hash_interruptible_operation_t sign_operation =
6940 psa_sign_hash_interruptible_operation_init();
6941 psa_verify_hash_interruptible_operation_t verify_operation =
6942 psa_verify_hash_interruptible_operation_init();
6943
6944 PSA_ASSERT(psa_crypto_init());
6945
Paul Elliott0c683352022-12-16 19:16:56 +00006946 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6947 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006948 psa_set_key_algorithm(&attributes, alg);
6949 psa_set_key_type(&attributes, key_type);
6950
6951 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6952 &key));
6953 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6954 key_bits = psa_get_key_bits(&attributes);
6955
6956 /* Allocate a buffer which has the size advertised by the
6957 * library. */
6958 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6959 key_bits, alg);
6960 TEST_ASSERT(signature_size != 0);
6961 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6962 ASSERT_ALLOC(signature, signature_size);
6963
Paul Elliott0c683352022-12-16 19:16:56 +00006964 psa_interruptible_set_max_ops(max_ops);
6965
Paul Elliott6f600372023-02-06 18:41:05 +00006966 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6967 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006968
Paul Elliott7c173082023-02-26 18:44:45 +00006969 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6970 TEST_ASSERT(num_ops_prior == 0);
6971
Paul Elliott712d5122022-12-07 14:03:10 +00006972 /* Start performing the signature. */
6973 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6974 input_data->x, input_data->len));
6975
Paul Elliott7c173082023-02-26 18:44:45 +00006976 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6977 TEST_ASSERT(num_ops_prior == 0);
6978
Paul Elliott712d5122022-12-07 14:03:10 +00006979 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006980 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006981
Paul Elliott0c683352022-12-16 19:16:56 +00006982 status = psa_sign_hash_complete(&sign_operation, signature,
6983 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006984 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006985
6986 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006987
6988 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6989 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6990 /* We are asserting here that every complete makes progress
6991 * (completes some ops), which is true of the internal
6992 * implementation and probably any implementation, however this is
6993 * not mandated by the PSA specification. */
6994 TEST_ASSERT(num_ops > num_ops_prior);
6995
6996 num_ops_prior = num_ops;
6997 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006998 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006999
7000 TEST_ASSERT(status == PSA_SUCCESS);
7001
Paul Elliott0c683352022-12-16 19:16:56 +00007002 TEST_LE_U(min_completes, num_completes);
7003 TEST_LE_U(num_completes, max_completes);
7004
Paul Elliott712d5122022-12-07 14:03:10 +00007005 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7006
Paul Elliott7c173082023-02-26 18:44:45 +00007007 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7008 TEST_ASSERT(num_ops == 0);
7009
Paul Elliott712d5122022-12-07 14:03:10 +00007010 /* Check that the signature length looks sensible. */
7011 TEST_LE_U(signature_length, signature_size);
7012 TEST_ASSERT(signature_length > 0);
7013
Paul Elliott0c683352022-12-16 19:16:56 +00007014 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007015
7016 /* Start verification. */
7017 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7018 input_data->x, input_data->len,
7019 signature, signature_length));
7020
7021 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007022 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007023 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007024
7025 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007026 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007027
7028 TEST_ASSERT(status == PSA_SUCCESS);
7029
Paul Elliott0c683352022-12-16 19:16:56 +00007030 TEST_LE_U(min_completes, num_completes);
7031 TEST_LE_U(num_completes, max_completes);
7032
Paul Elliott712d5122022-12-07 14:03:10 +00007033 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7034
7035 verify_operation = psa_verify_hash_interruptible_operation_init();
7036
7037 if (input_data->len != 0) {
7038 /* Flip a bit in the input and verify that the signature is now
7039 * detected as invalid. Flip a bit at the beginning, not at the end,
7040 * because ECDSA may ignore the last few bits of the input. */
7041 input_data->x[0] ^= 1;
7042
Paul Elliott712d5122022-12-07 14:03:10 +00007043 /* Start verification. */
7044 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7045 input_data->x, input_data->len,
7046 signature, signature_length));
7047
7048 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007049 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007050 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007051 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007052
7053 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7054 }
7055
7056 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7057
7058exit:
7059 /*
7060 * Key attributes may have been returned by psa_get_key_attributes()
7061 * thus reset them as required.
7062 */
7063 psa_reset_key_attributes(&attributes);
7064
7065 psa_destroy_key(key);
7066 mbedtls_free(signature);
7067 PSA_DONE();
7068}
7069/* END_CASE */
7070
Gilles Peskine9911b022018-06-29 17:30:48 +02007071/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007072void verify_hash(int key_type_arg, data_t *key_data,
7073 int alg_arg, data_t *hash_data,
7074 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007075{
Ronald Cron5425a212020-08-04 14:58:35 +02007076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007077 psa_key_type_t key_type = key_type_arg;
7078 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007080
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007082
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007084
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7086 psa_set_key_algorithm(&attributes, alg);
7087 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7090 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007091
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 PSA_ASSERT(psa_verify_hash(key, alg,
7093 hash_data->x, hash_data->len,
7094 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007095
itayzafrir5c753392018-05-08 11:18:38 +03007096exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 psa_reset_key_attributes(&attributes);
7098 psa_destroy_key(key);
7099 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007100}
7101/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007102
Paul Elliott712d5122022-12-07 14:03:10 +00007103/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007104/**
7105 * verify_hash_interruptible() test intentions:
7106 *
7107 * Note: This test can currently only handle ECDSA.
7108 *
7109 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007110 * only). Given this test only does verification it can accept public keys as
7111 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007112 *
7113 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7114 * expected for different max_ops values.
7115 *
7116 * 3. Test that the number of ops done prior to start and after abort is zero
7117 * and that each successful stage completes some ops (this is not mandated by
7118 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007119 *
7120 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7121 * complete() calls does not alter the number of ops returned.
7122 *
7123 * 5. Test that after corrupting the hash, the verification detects an invalid
7124 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007125 */
Paul Elliott712d5122022-12-07 14:03:10 +00007126void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7127 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007128 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007129{
7130 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7131 psa_key_type_t key_type = key_type_arg;
7132 psa_algorithm_t alg = alg_arg;
7133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7134 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007135 uint32_t num_ops = 0;
7136 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007137 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007138 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007139 size_t min_completes = 0;
7140 size_t max_completes = 0;
7141
Paul Elliott712d5122022-12-07 14:03:10 +00007142 psa_verify_hash_interruptible_operation_t operation =
7143 psa_verify_hash_interruptible_operation_init();
7144
7145 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7146
7147 PSA_ASSERT(psa_crypto_init());
7148
7149 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7150 psa_set_key_algorithm(&attributes, alg);
7151 psa_set_key_type(&attributes, key_type);
7152
7153 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7154 &key));
7155
Paul Elliott0c683352022-12-16 19:16:56 +00007156 psa_interruptible_set_max_ops(max_ops);
7157
Paul Elliott6f600372023-02-06 18:41:05 +00007158 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7159 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007160
Paul Elliott712d5122022-12-07 14:03:10 +00007161 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7162
7163 TEST_ASSERT(num_ops_prior == 0);
7164
7165 /* Start verification. */
7166 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7167 hash_data->x, hash_data->len,
7168 signature_data->x, signature_data->len)
7169 );
7170
7171 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7172
7173 TEST_ASSERT(num_ops_prior == 0);
7174
7175 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007176 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007177 status = psa_verify_hash_complete(&operation);
7178
Paul Elliott0c683352022-12-16 19:16:56 +00007179 num_completes++;
7180
Paul Elliott712d5122022-12-07 14:03:10 +00007181 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7182 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007183 /* We are asserting here that every complete makes progress
7184 * (completes some ops), which is true of the internal
7185 * implementation and probably any implementation, however this is
7186 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007187 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007188
Paul Elliott712d5122022-12-07 14:03:10 +00007189 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007190
7191 /* Ensure calling get_num_ops() twice still returns the same
7192 * number of ops as previously reported. */
7193 num_ops = psa_verify_hash_get_num_ops(&operation);
7194
7195 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007196 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007197 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007198
7199 TEST_ASSERT(status == PSA_SUCCESS);
7200
Paul Elliott0c683352022-12-16 19:16:56 +00007201 TEST_LE_U(min_completes, num_completes);
7202 TEST_LE_U(num_completes, max_completes);
7203
Paul Elliott712d5122022-12-07 14:03:10 +00007204 PSA_ASSERT(psa_verify_hash_abort(&operation));
7205
Paul Elliott59ad9452022-12-18 15:09:02 +00007206 num_ops = psa_verify_hash_get_num_ops(&operation);
7207 TEST_ASSERT(num_ops == 0);
7208
Paul Elliott8359c142023-02-24 18:40:10 +00007209 if (hash_data->len != 0) {
7210 /* Flip a bit in the hash and verify that the signature is now detected
7211 * as invalid. Flip a bit at the beginning, not at the end, because
7212 * ECDSA may ignore the last few bits of the input. */
7213 hash_data->x[0] ^= 1;
7214
7215 /* Start verification. */
7216 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7217 hash_data->x, hash_data->len,
7218 signature_data->x, signature_data->len));
7219
7220 /* Continue performing the signature until complete. */
7221 do {
7222 status = psa_verify_hash_complete(&operation);
7223 } while (status == PSA_OPERATION_INCOMPLETE);
7224
7225 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7226 }
7227
Paul Elliott712d5122022-12-07 14:03:10 +00007228exit:
7229 psa_reset_key_attributes(&attributes);
7230 psa_destroy_key(key);
7231 PSA_DONE();
7232}
7233/* END_CASE */
7234
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007235/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007236void verify_hash_fail(int key_type_arg, data_t *key_data,
7237 int alg_arg, data_t *hash_data,
7238 data_t *signature_data,
7239 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007240{
Ronald Cron5425a212020-08-04 14:58:35 +02007241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007242 psa_key_type_t key_type = key_type_arg;
7243 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007244 psa_status_t actual_status;
7245 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007247
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007249
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7251 psa_set_key_algorithm(&attributes, alg);
7252 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007253
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7255 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007256
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 actual_status = psa_verify_hash(key, alg,
7258 hash_data->x, hash_data->len,
7259 signature_data->x, signature_data->len);
7260 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007261
7262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 psa_reset_key_attributes(&attributes);
7264 psa_destroy_key(key);
7265 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007266}
7267/* END_CASE */
7268
Paul Elliott91007972022-12-16 12:21:24 +00007269/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007270/**
7271 * verify_hash_fail_interruptible() test intentions:
7272 *
7273 * Note: This test can currently only handle ECDSA.
7274 *
7275 * 1. Test that various failure cases for interruptible verify hash fail with
7276 * the correct error codes, and at the correct point (at start or during
7277 * complete).
7278 *
7279 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7280 * expected for different max_ops values.
7281 *
7282 * 3. Test that the number of ops done prior to start and after abort is zero
7283 * and that each successful stage completes some ops (this is not mandated by
7284 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007285 *
7286 * 4. Check that calling complete() when start() fails and complete()
7287 * after completion results in a BAD_STATE error.
7288 *
7289 * 5. Check that calling start() again after start fails results in a BAD_STATE
7290 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007291 */
Paul Elliott91007972022-12-16 12:21:24 +00007292void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7293 int alg_arg, data_t *hash_data,
7294 data_t *signature_data,
7295 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007296 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007297 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007298{
7299 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7300 psa_key_type_t key_type = key_type_arg;
7301 psa_algorithm_t alg = alg_arg;
7302 psa_status_t actual_status;
7303 psa_status_t expected_start_status = expected_start_status_arg;
7304 psa_status_t expected_complete_status = expected_complete_status_arg;
7305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007306 uint32_t num_ops = 0;
7307 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007308 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007309 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007310 size_t min_completes = 0;
7311 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007312 psa_verify_hash_interruptible_operation_t operation =
7313 psa_verify_hash_interruptible_operation_init();
7314
7315 PSA_ASSERT(psa_crypto_init());
7316
7317 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7318 psa_set_key_algorithm(&attributes, alg);
7319 psa_set_key_type(&attributes, key_type);
7320
7321 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7322 &key));
7323
Paul Elliott0c683352022-12-16 19:16:56 +00007324 psa_interruptible_set_max_ops(max_ops);
7325
Paul Elliott6f600372023-02-06 18:41:05 +00007326 interruptible_signverify_get_minmax_completes(max_ops,
7327 expected_complete_status,
7328 &min_completes,
7329 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007330
Paul Elliott91007972022-12-16 12:21:24 +00007331 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7332 TEST_ASSERT(num_ops_prior == 0);
7333
7334 /* Start verification. */
7335 actual_status = psa_verify_hash_start(&operation, key, alg,
7336 hash_data->x, hash_data->len,
7337 signature_data->x,
7338 signature_data->len);
7339
7340 TEST_EQUAL(actual_status, expected_start_status);
7341
Paul Elliottc9774412023-02-06 15:14:07 +00007342 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007343 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007344 * start failed. */
7345 actual_status = psa_verify_hash_complete(&operation);
7346
7347 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7348
7349 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007350 actual_status = psa_verify_hash_start(&operation, key, alg,
7351 hash_data->x, hash_data->len,
7352 signature_data->x,
7353 signature_data->len);
7354
7355 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7356 }
7357
Paul Elliott91007972022-12-16 12:21:24 +00007358 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7359 TEST_ASSERT(num_ops_prior == 0);
7360
Paul Elliott91007972022-12-16 12:21:24 +00007361 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007362 do {
Paul Elliott91007972022-12-16 12:21:24 +00007363 actual_status = psa_verify_hash_complete(&operation);
7364
Paul Elliott0c683352022-12-16 19:16:56 +00007365 num_completes++;
7366
Paul Elliott334d7262023-01-20 17:29:41 +00007367 if (actual_status == PSA_SUCCESS ||
7368 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007369 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007370 /* We are asserting here that every complete makes progress
7371 * (completes some ops), which is true of the internal
7372 * implementation and probably any implementation, however this is
7373 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007374 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007375
Paul Elliott91007972022-12-16 12:21:24 +00007376 num_ops_prior = num_ops;
7377 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007378 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007379
Paul Elliottc9774412023-02-06 15:14:07 +00007380 TEST_EQUAL(actual_status, expected_complete_status);
7381
Paul Elliottefebad02023-02-15 16:56:45 +00007382 /* Check that another complete returns BAD_STATE. */
7383 actual_status = psa_verify_hash_complete(&operation);
7384 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007385
Paul Elliott0c683352022-12-16 19:16:56 +00007386 TEST_LE_U(min_completes, num_completes);
7387 TEST_LE_U(num_completes, max_completes);
7388
Paul Elliott91007972022-12-16 12:21:24 +00007389 PSA_ASSERT(psa_verify_hash_abort(&operation));
7390
Paul Elliott59ad9452022-12-18 15:09:02 +00007391 num_ops = psa_verify_hash_get_num_ops(&operation);
7392 TEST_ASSERT(num_ops == 0);
7393
Paul Elliott91007972022-12-16 12:21:24 +00007394exit:
7395 psa_reset_key_attributes(&attributes);
7396 psa_destroy_key(key);
7397 PSA_DONE();
7398}
7399/* END_CASE */
7400
Paul Elliott20a36062022-12-18 13:21:25 +00007401/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007402/**
7403 * interruptible_signverify_hash_state_test() test intentions:
7404 *
7405 * Note: This test can currently only handle ECDSA.
7406 *
7407 * 1. Test that calling the various interruptible sign and verify hash functions
7408 * in incorrect orders returns BAD_STATE errors.
7409 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007410void interruptible_signverify_hash_state_test(int key_type_arg,
7411 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007412{
7413 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7414 psa_key_type_t key_type = key_type_arg;
7415 psa_algorithm_t alg = alg_arg;
7416 size_t key_bits;
7417 unsigned char *signature = NULL;
7418 size_t signature_size;
7419 size_t signature_length = 0xdeadbeef;
7420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7421 psa_sign_hash_interruptible_operation_t sign_operation =
7422 psa_sign_hash_interruptible_operation_init();
7423 psa_verify_hash_interruptible_operation_t verify_operation =
7424 psa_verify_hash_interruptible_operation_init();
7425
7426 PSA_ASSERT(psa_crypto_init());
7427
7428 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7429 PSA_KEY_USAGE_VERIFY_HASH);
7430 psa_set_key_algorithm(&attributes, alg);
7431 psa_set_key_type(&attributes, key_type);
7432
7433 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7434 &key));
7435 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7436 key_bits = psa_get_key_bits(&attributes);
7437
7438 /* Allocate a buffer which has the size advertised by the
7439 * library. */
7440 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7441 key_bits, alg);
7442 TEST_ASSERT(signature_size != 0);
7443 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7444 ASSERT_ALLOC(signature, signature_size);
7445
7446 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7447
7448 /* --- Attempt completes prior to starts --- */
7449 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7450 signature_size,
7451 &signature_length),
7452 PSA_ERROR_BAD_STATE);
7453
7454 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7455
7456 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7457 PSA_ERROR_BAD_STATE);
7458
7459 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7460
7461 /* --- Aborts in all other places. --- */
7462 psa_sign_hash_abort(&sign_operation);
7463
7464 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7465 input_data->x, input_data->len));
7466
7467 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7468
7469 psa_interruptible_set_max_ops(1);
7470
7471 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7472 input_data->x, input_data->len));
7473
7474 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7475 signature_size,
7476 &signature_length),
7477 PSA_OPERATION_INCOMPLETE);
7478
7479 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7480
7481 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7482
7483 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7484 input_data->x, input_data->len));
7485
7486 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7487 signature_size,
7488 &signature_length));
7489
7490 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7491
7492 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7493
7494 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7495 input_data->x, input_data->len,
7496 signature, signature_length));
7497
7498 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7499
7500 psa_interruptible_set_max_ops(1);
7501
7502 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7503 input_data->x, input_data->len,
7504 signature, signature_length));
7505
7506 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7507 PSA_OPERATION_INCOMPLETE);
7508
7509 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7510
7511 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7512
7513 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7514 input_data->x, input_data->len,
7515 signature, signature_length));
7516
7517 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7518
7519 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7520
7521 /* --- Attempt double starts. --- */
7522
7523 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7524 input_data->x, input_data->len));
7525
7526 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7527 input_data->x, input_data->len),
7528 PSA_ERROR_BAD_STATE);
7529
7530 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7531
7532 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7533 input_data->x, input_data->len,
7534 signature, signature_length));
7535
7536 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7537 input_data->x, input_data->len,
7538 signature, signature_length),
7539 PSA_ERROR_BAD_STATE);
7540
7541 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7542
Paul Elliott76d671a2023-02-07 17:45:18 +00007543exit:
7544 /*
7545 * Key attributes may have been returned by psa_get_key_attributes()
7546 * thus reset them as required.
7547 */
7548 psa_reset_key_attributes(&attributes);
7549
7550 psa_destroy_key(key);
7551 mbedtls_free(signature);
7552 PSA_DONE();
7553}
7554/* END_CASE */
7555
7556/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007557/**
Paul Elliottc2033502023-02-26 17:09:14 +00007558 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007559 *
7560 * Note: This test can currently only handle ECDSA.
7561 *
7562 * 1. Test various edge cases in the interruptible sign and verify hash
7563 * interfaces.
7564 */
Paul Elliottc2033502023-02-26 17:09:14 +00007565void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007566 data_t *key_data, int alg_arg, data_t *input_data)
7567{
7568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7569 psa_key_type_t key_type = key_type_arg;
7570 psa_algorithm_t alg = alg_arg;
7571 size_t key_bits;
7572 unsigned char *signature = NULL;
7573 size_t signature_size;
7574 size_t signature_length = 0xdeadbeef;
7575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7576 uint8_t *input_buffer = NULL;
7577 psa_sign_hash_interruptible_operation_t sign_operation =
7578 psa_sign_hash_interruptible_operation_init();
7579 psa_verify_hash_interruptible_operation_t verify_operation =
7580 psa_verify_hash_interruptible_operation_init();
7581
7582 PSA_ASSERT(psa_crypto_init());
7583
7584 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7585 PSA_KEY_USAGE_VERIFY_HASH);
7586 psa_set_key_algorithm(&attributes, alg);
7587 psa_set_key_type(&attributes, key_type);
7588
7589 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7590 &key));
7591 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7592 key_bits = psa_get_key_bits(&attributes);
7593
7594 /* Allocate a buffer which has the size advertised by the
7595 * library. */
7596 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7597 key_bits, alg);
7598 TEST_ASSERT(signature_size != 0);
7599 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7600 ASSERT_ALLOC(signature, signature_size);
7601
Paul Elliott20a36062022-12-18 13:21:25 +00007602 /* --- Change function inputs mid run, to cause an error (sign only,
7603 * verify passes all inputs to start. --- */
7604
7605 psa_interruptible_set_max_ops(1);
7606
7607 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7608 input_data->x, input_data->len));
7609
7610 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7611 signature_size,
7612 &signature_length),
7613 PSA_OPERATION_INCOMPLETE);
7614
7615 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7616 0,
7617 &signature_length),
7618 PSA_ERROR_BUFFER_TOO_SMALL);
7619
Paul Elliottc9774412023-02-06 15:14:07 +00007620 /* And test that this invalidates the operation. */
7621 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7622 0,
7623 &signature_length),
7624 PSA_ERROR_BAD_STATE);
7625
Paul Elliott20a36062022-12-18 13:21:25 +00007626 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7627
Paul Elliottf9c91a72023-02-05 18:06:38 +00007628 /* Trash the hash buffer in between start and complete, to ensure
7629 * no reliance on external buffers. */
7630 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7631
7632 input_buffer = mbedtls_calloc(1, input_data->len);
7633 TEST_ASSERT(input_buffer != NULL);
7634
7635 memcpy(input_buffer, input_data->x, input_data->len);
7636
7637 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7638 input_buffer, input_data->len));
7639
7640 memset(input_buffer, '!', input_data->len);
7641 mbedtls_free(input_buffer);
7642 input_buffer = NULL;
7643
7644 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7645 signature_size,
7646 &signature_length));
7647
7648 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7649
7650 input_buffer = mbedtls_calloc(1, input_data->len);
7651 TEST_ASSERT(input_buffer != NULL);
7652
7653 memcpy(input_buffer, input_data->x, input_data->len);
7654
7655 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7656 input_buffer, input_data->len,
7657 signature, signature_length));
7658
7659 memset(input_buffer, '!', input_data->len);
7660 mbedtls_free(input_buffer);
7661 input_buffer = NULL;
7662
7663 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7664
7665 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7666
Paul Elliott20a36062022-12-18 13:21:25 +00007667exit:
7668 /*
7669 * Key attributes may have been returned by psa_get_key_attributes()
7670 * thus reset them as required.
7671 */
7672 psa_reset_key_attributes(&attributes);
7673
7674 psa_destroy_key(key);
7675 mbedtls_free(signature);
7676 PSA_DONE();
7677}
7678/* END_CASE */
7679
Paul Elliotta4cb9092023-02-07 18:01:55 +00007680/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007681/**
Paul Elliott57702242023-02-26 20:36:10 +00007682 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007683 *
7684 * Note: This test can currently only handle ECDSA.
7685 *
7686 * 1. Test that setting max ops is reflected in both interruptible sign and
7687 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007688 * 2. Test that changing the value of max_ops to unlimited during an operation
7689 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007690 *
7691 * 3. Test that calling get_num_ops() between complete calls gives the same
7692 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007693 */
Paul Elliott57702242023-02-26 20:36:10 +00007694void interruptible_signverify_hash_ops_tests(int key_type_arg,
7695 data_t *key_data, int alg_arg,
7696 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007697{
7698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7699 psa_key_type_t key_type = key_type_arg;
7700 psa_algorithm_t alg = alg_arg;
7701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007702 size_t key_bits;
7703 unsigned char *signature = NULL;
7704 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007705 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007706 uint32_t num_ops = 0;
7707 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7708
Paul Elliotta4cb9092023-02-07 18:01:55 +00007709 psa_sign_hash_interruptible_operation_t sign_operation =
7710 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007711 psa_verify_hash_interruptible_operation_t verify_operation =
7712 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007713
7714 PSA_ASSERT(psa_crypto_init());
7715
7716 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7717 PSA_KEY_USAGE_VERIFY_HASH);
7718 psa_set_key_algorithm(&attributes, alg);
7719 psa_set_key_type(&attributes, key_type);
7720
Paul Elliottf1743e22023-02-15 18:44:16 +00007721 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7722 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7723 key_bits = psa_get_key_bits(&attributes);
7724
7725 /* Allocate a buffer which has the size advertised by the
7726 * library. */
7727 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7728
7729 TEST_ASSERT(signature_size != 0);
7730 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7731 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007732
7733 /* Check that default max ops gets set if we don't set it. */
7734 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7735 input_data->x, input_data->len));
7736
7737 TEST_EQUAL(psa_interruptible_get_max_ops(),
7738 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7739
7740 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7741
Paul Elliottf1743e22023-02-15 18:44:16 +00007742 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7743 input_data->x, input_data->len,
7744 signature, signature_size));
7745
7746 TEST_EQUAL(psa_interruptible_get_max_ops(),
7747 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7748
7749 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7750
Paul Elliotta4cb9092023-02-07 18:01:55 +00007751 /* Check that max ops gets set properly. */
7752
7753 psa_interruptible_set_max_ops(0xbeef);
7754
Paul Elliottf1743e22023-02-15 18:44:16 +00007755 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007756
Paul Elliott9e8819f2023-02-26 19:01:35 +00007757 /* --- Ensure changing the max ops mid operation works (operation should
7758 * complete successfully after setting max ops to unlimited --- */
7759 psa_interruptible_set_max_ops(1);
7760
7761 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7762 input_data->x, input_data->len));
7763
7764 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7765 signature_size,
7766 &signature_length),
7767 PSA_OPERATION_INCOMPLETE);
7768
7769 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7770
7771 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7772 signature_size,
7773 &signature_length));
7774
7775 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7776
7777 psa_interruptible_set_max_ops(1);
7778
7779 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7780 input_data->x, input_data->len,
7781 signature, signature_length));
7782
7783 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7784 PSA_OPERATION_INCOMPLETE);
7785
7786 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7787
7788 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7789
7790 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7791
Paul Elliottc1e04002023-02-26 20:27:23 +00007792 /* --- Test that not calling get_num_ops inbetween complete calls does not
7793 * result in lost ops. ---*/
7794
7795 psa_interruptible_set_max_ops(1);
7796
7797 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7798 input_data->x, input_data->len));
7799
7800 /* Continue performing the signature until complete. */
7801 do {
7802 status = psa_sign_hash_complete(&sign_operation, signature,
7803 signature_size,
7804 &signature_length);
7805
7806 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7807
7808 } while (status == PSA_OPERATION_INCOMPLETE);
7809
7810 PSA_ASSERT(status);
7811
7812 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7813
7814 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7815 input_data->x, input_data->len));
7816
7817 /* Continue performing the signature until complete. */
7818 do {
7819 status = psa_sign_hash_complete(&sign_operation, signature,
7820 signature_size,
7821 &signature_length);
7822 } while (status == PSA_OPERATION_INCOMPLETE);
7823
7824 PSA_ASSERT(status);
7825
7826 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7827
7828 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7829
7830 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7831 input_data->x, input_data->len,
7832 signature, signature_length));
7833
7834 /* Continue performing the verification until complete. */
7835 do {
7836 status = psa_verify_hash_complete(&verify_operation);
7837
7838 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7839
7840 } while (status == PSA_OPERATION_INCOMPLETE);
7841
7842 PSA_ASSERT(status);
7843
7844 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7845
7846 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7847 input_data->x, input_data->len,
7848 signature, signature_length));
7849
7850 /* Continue performing the verification until complete. */
7851 do {
7852 status = psa_verify_hash_complete(&verify_operation);
7853
7854 } while (status == PSA_OPERATION_INCOMPLETE);
7855
7856 PSA_ASSERT(status);
7857
7858 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7859
7860 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7861
Paul Elliotta4cb9092023-02-07 18:01:55 +00007862exit:
7863 /*
7864 * Key attributes may have been returned by psa_get_key_attributes()
7865 * thus reset them as required.
7866 */
7867 psa_reset_key_attributes(&attributes);
7868
7869 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007870 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007871 PSA_DONE();
7872}
7873/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007874
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007875/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007876void sign_message_deterministic(int key_type_arg,
7877 data_t *key_data,
7878 int alg_arg,
7879 data_t *input_data,
7880 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007881{
7882 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7883 psa_key_type_t key_type = key_type_arg;
7884 psa_algorithm_t alg = alg_arg;
7885 size_t key_bits;
7886 unsigned char *signature = NULL;
7887 size_t signature_size;
7888 size_t signature_length = 0xdeadbeef;
7889 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7890
Gilles Peskine449bd832023-01-11 14:50:10 +01007891 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007892
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7894 psa_set_key_algorithm(&attributes, alg);
7895 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007896
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7898 &key));
7899 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7900 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007901
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7903 TEST_ASSERT(signature_size != 0);
7904 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7905 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007906
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 PSA_ASSERT(psa_sign_message(key, alg,
7908 input_data->x, input_data->len,
7909 signature, signature_size,
7910 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007911
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 ASSERT_COMPARE(output_data->x, output_data->len,
7913 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007914
7915exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007917
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 psa_destroy_key(key);
7919 mbedtls_free(signature);
7920 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007921
7922}
7923/* END_CASE */
7924
7925/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007926void sign_message_fail(int key_type_arg,
7927 data_t *key_data,
7928 int alg_arg,
7929 data_t *input_data,
7930 int signature_size_arg,
7931 int expected_status_arg)
7932{
7933 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7934 psa_key_type_t key_type = key_type_arg;
7935 psa_algorithm_t alg = alg_arg;
7936 size_t signature_size = signature_size_arg;
7937 psa_status_t actual_status;
7938 psa_status_t expected_status = expected_status_arg;
7939 unsigned char *signature = NULL;
7940 size_t signature_length = 0xdeadbeef;
7941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7942
7943 ASSERT_ALLOC(signature, signature_size);
7944
7945 PSA_ASSERT(psa_crypto_init());
7946
7947 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7948 psa_set_key_algorithm(&attributes, alg);
7949 psa_set_key_type(&attributes, key_type);
7950
7951 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7952 &key));
7953
7954 actual_status = psa_sign_message(key, alg,
7955 input_data->x, input_data->len,
7956 signature, signature_size,
7957 &signature_length);
7958 TEST_EQUAL(actual_status, expected_status);
7959 /* The value of *signature_length is unspecified on error, but
7960 * whatever it is, it should be less than signature_size, so that
7961 * if the caller tries to read *signature_length bytes without
7962 * checking the error code then they don't overflow a buffer. */
7963 TEST_LE_U(signature_length, signature_size);
7964
7965exit:
7966 psa_reset_key_attributes(&attributes);
7967 psa_destroy_key(key);
7968 mbedtls_free(signature);
7969 PSA_DONE();
7970}
7971/* END_CASE */
7972
7973/* BEGIN_CASE */
7974void sign_verify_message(int key_type_arg,
7975 data_t *key_data,
7976 int alg_arg,
7977 data_t *input_data)
7978{
7979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7980 psa_key_type_t key_type = key_type_arg;
7981 psa_algorithm_t alg = alg_arg;
7982 size_t key_bits;
7983 unsigned char *signature = NULL;
7984 size_t signature_size;
7985 size_t signature_length = 0xdeadbeef;
7986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7987
7988 PSA_ASSERT(psa_crypto_init());
7989
7990 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7991 PSA_KEY_USAGE_VERIFY_MESSAGE);
7992 psa_set_key_algorithm(&attributes, alg);
7993 psa_set_key_type(&attributes, key_type);
7994
7995 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7996 &key));
7997 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7998 key_bits = psa_get_key_bits(&attributes);
7999
8000 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8001 TEST_ASSERT(signature_size != 0);
8002 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
8003 ASSERT_ALLOC(signature, signature_size);
8004
8005 PSA_ASSERT(psa_sign_message(key, alg,
8006 input_data->x, input_data->len,
8007 signature, signature_size,
8008 &signature_length));
8009 TEST_LE_U(signature_length, signature_size);
8010 TEST_ASSERT(signature_length > 0);
8011
8012 PSA_ASSERT(psa_verify_message(key, alg,
8013 input_data->x, input_data->len,
8014 signature, signature_length));
8015
8016 if (input_data->len != 0) {
8017 /* Flip a bit in the input and verify that the signature is now
8018 * detected as invalid. Flip a bit at the beginning, not at the end,
8019 * because ECDSA may ignore the last few bits of the input. */
8020 input_data->x[0] ^= 1;
8021 TEST_EQUAL(psa_verify_message(key, alg,
8022 input_data->x, input_data->len,
8023 signature, signature_length),
8024 PSA_ERROR_INVALID_SIGNATURE);
8025 }
8026
8027exit:
8028 psa_reset_key_attributes(&attributes);
8029
8030 psa_destroy_key(key);
8031 mbedtls_free(signature);
8032 PSA_DONE();
8033}
8034/* END_CASE */
8035
8036/* BEGIN_CASE */
8037void verify_message(int key_type_arg,
8038 data_t *key_data,
8039 int alg_arg,
8040 data_t *input_data,
8041 data_t *signature_data)
8042{
8043 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8044 psa_key_type_t key_type = key_type_arg;
8045 psa_algorithm_t alg = alg_arg;
8046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8047
8048 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8049
8050 PSA_ASSERT(psa_crypto_init());
8051
8052 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8053 psa_set_key_algorithm(&attributes, alg);
8054 psa_set_key_type(&attributes, key_type);
8055
8056 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8057 &key));
8058
8059 PSA_ASSERT(psa_verify_message(key, alg,
8060 input_data->x, input_data->len,
8061 signature_data->x, signature_data->len));
8062
8063exit:
8064 psa_reset_key_attributes(&attributes);
8065 psa_destroy_key(key);
8066 PSA_DONE();
8067}
8068/* END_CASE */
8069
8070/* BEGIN_CASE */
8071void verify_message_fail(int key_type_arg,
8072 data_t *key_data,
8073 int alg_arg,
8074 data_t *hash_data,
8075 data_t *signature_data,
8076 int expected_status_arg)
8077{
8078 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8079 psa_key_type_t key_type = key_type_arg;
8080 psa_algorithm_t alg = alg_arg;
8081 psa_status_t actual_status;
8082 psa_status_t expected_status = expected_status_arg;
8083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8084
8085 PSA_ASSERT(psa_crypto_init());
8086
8087 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8088 psa_set_key_algorithm(&attributes, alg);
8089 psa_set_key_type(&attributes, key_type);
8090
8091 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8092 &key));
8093
8094 actual_status = psa_verify_message(key, alg,
8095 hash_data->x, hash_data->len,
8096 signature_data->x,
8097 signature_data->len);
8098 TEST_EQUAL(actual_status, expected_status);
8099
8100exit:
8101 psa_reset_key_attributes(&attributes);
8102 psa_destroy_key(key);
8103 PSA_DONE();
8104}
8105/* END_CASE */
8106
8107/* BEGIN_CASE */
8108void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008109 data_t *key_data,
8110 int alg_arg,
8111 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008112 data_t *label,
8113 int expected_output_length_arg,
8114 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008115{
Ronald Cron5425a212020-08-04 14:58:35 +02008116 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008117 psa_key_type_t key_type = key_type_arg;
8118 psa_algorithm_t alg = alg_arg;
8119 size_t expected_output_length = expected_output_length_arg;
8120 size_t key_bits;
8121 unsigned char *output = NULL;
8122 size_t output_size;
8123 size_t output_length = ~0;
8124 psa_status_t actual_status;
8125 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008127
Gilles Peskine449bd832023-01-11 14:50:10 +01008128 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008129
Gilles Peskine656896e2018-06-29 19:12:28 +02008130 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8132 psa_set_key_algorithm(&attributes, alg);
8133 psa_set_key_type(&attributes, key_type);
8134 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8135 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008136
8137 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8139 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008140
Gilles Peskine449bd832023-01-11 14:50:10 +01008141 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8142 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8143 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008144
8145 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008146 actual_status = psa_asymmetric_encrypt(key, alg,
8147 input_data->x, input_data->len,
8148 label->x, label->len,
8149 output, output_size,
8150 &output_length);
8151 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008152 if (actual_status == PSA_SUCCESS) {
8153 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008154 } else {
8155 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008156 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008157
Gilles Peskine68428122018-06-30 18:42:41 +02008158 /* If the label is empty, the test framework puts a non-null pointer
8159 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008160 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008161 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008162 if (output_size != 0) {
8163 memset(output, 0, output_size);
8164 }
8165 actual_status = psa_asymmetric_encrypt(key, alg,
8166 input_data->x, input_data->len,
8167 NULL, label->len,
8168 output, output_size,
8169 &output_length);
8170 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008171 if (actual_status == PSA_SUCCESS) {
8172 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008173 } else {
8174 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008175 }
Gilles Peskine68428122018-06-30 18:42:41 +02008176 }
8177
Gilles Peskine656896e2018-06-29 19:12:28 +02008178exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008179 /*
8180 * Key attributes may have been returned by psa_get_key_attributes()
8181 * thus reset them as required.
8182 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008183 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008184
Gilles Peskine449bd832023-01-11 14:50:10 +01008185 psa_destroy_key(key);
8186 mbedtls_free(output);
8187 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008188}
8189/* END_CASE */
8190
8191/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008192void asymmetric_encrypt_decrypt(int key_type_arg,
8193 data_t *key_data,
8194 int alg_arg,
8195 data_t *input_data,
8196 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008197{
Ronald Cron5425a212020-08-04 14:58:35 +02008198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008199 psa_key_type_t key_type = key_type_arg;
8200 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008201 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008202 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008203 size_t output_size;
8204 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008205 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008206 size_t output2_size;
8207 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008209
Gilles Peskine449bd832023-01-11 14:50:10 +01008210 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008211
Gilles Peskine449bd832023-01-11 14:50:10 +01008212 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8213 psa_set_key_algorithm(&attributes, alg);
8214 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008215
Gilles Peskine449bd832023-01-11 14:50:10 +01008216 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8217 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008218
8219 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008220 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8221 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008222
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8224 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8225 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008226
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008227 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 TEST_LE_U(output2_size,
8229 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8230 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8231 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008232
Gilles Peskineeebd7382018-06-08 18:11:54 +02008233 /* We test encryption by checking that encrypt-then-decrypt gives back
8234 * the original plaintext because of the non-optional random
8235 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008236 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8237 input_data->x, input_data->len,
8238 label->x, label->len,
8239 output, output_size,
8240 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008241 /* We don't know what ciphertext length to expect, but check that
8242 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008244
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8246 output, output_length,
8247 label->x, label->len,
8248 output2, output2_size,
8249 &output2_length));
8250 ASSERT_COMPARE(input_data->x, input_data->len,
8251 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008252
8253exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008254 /*
8255 * Key attributes may have been returned by psa_get_key_attributes()
8256 * thus reset them as required.
8257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008258 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008259
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 psa_destroy_key(key);
8261 mbedtls_free(output);
8262 mbedtls_free(output2);
8263 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008264}
8265/* END_CASE */
8266
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008267/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008268void asymmetric_decrypt(int key_type_arg,
8269 data_t *key_data,
8270 int alg_arg,
8271 data_t *input_data,
8272 data_t *label,
8273 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008274{
Ronald Cron5425a212020-08-04 14:58:35 +02008275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008276 psa_key_type_t key_type = key_type_arg;
8277 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008278 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008279 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008280 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008281 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008283
Gilles Peskine449bd832023-01-11 14:50:10 +01008284 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008285
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8287 psa_set_key_algorithm(&attributes, alg);
8288 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008289
Gilles Peskine449bd832023-01-11 14:50:10 +01008290 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8291 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008292
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8294 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008295
8296 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008297 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8298 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8299 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008300
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8302 input_data->x, input_data->len,
8303 label->x, label->len,
8304 output,
8305 output_size,
8306 &output_length));
8307 ASSERT_COMPARE(expected_data->x, expected_data->len,
8308 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008309
Gilles Peskine68428122018-06-30 18:42:41 +02008310 /* If the label is empty, the test framework puts a non-null pointer
8311 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008312 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008313 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 if (output_size != 0) {
8315 memset(output, 0, output_size);
8316 }
8317 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8318 input_data->x, input_data->len,
8319 NULL, label->len,
8320 output,
8321 output_size,
8322 &output_length));
8323 ASSERT_COMPARE(expected_data->x, expected_data->len,
8324 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008325 }
8326
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008327exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 psa_reset_key_attributes(&attributes);
8329 psa_destroy_key(key);
8330 mbedtls_free(output);
8331 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008332}
8333/* END_CASE */
8334
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008335/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008336void asymmetric_decrypt_fail(int key_type_arg,
8337 data_t *key_data,
8338 int alg_arg,
8339 data_t *input_data,
8340 data_t *label,
8341 int output_size_arg,
8342 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008343{
Ronald Cron5425a212020-08-04 14:58:35 +02008344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008345 psa_key_type_t key_type = key_type_arg;
8346 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008347 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008348 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008349 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008350 psa_status_t actual_status;
8351 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008353
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008355
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008357
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8359 psa_set_key_algorithm(&attributes, alg);
8360 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008361
Gilles Peskine449bd832023-01-11 14:50:10 +01008362 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8363 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008364
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 actual_status = psa_asymmetric_decrypt(key, alg,
8366 input_data->x, input_data->len,
8367 label->x, label->len,
8368 output, output_size,
8369 &output_length);
8370 TEST_EQUAL(actual_status, expected_status);
8371 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008372
Gilles Peskine68428122018-06-30 18:42:41 +02008373 /* If the label is empty, the test framework puts a non-null pointer
8374 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008376 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008377 if (output_size != 0) {
8378 memset(output, 0, output_size);
8379 }
8380 actual_status = psa_asymmetric_decrypt(key, alg,
8381 input_data->x, input_data->len,
8382 NULL, label->len,
8383 output, output_size,
8384 &output_length);
8385 TEST_EQUAL(actual_status, expected_status);
8386 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008387 }
8388
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008389exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 psa_reset_key_attributes(&attributes);
8391 psa_destroy_key(key);
8392 mbedtls_free(output);
8393 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008394}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008395/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008396
8397/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008398void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008399{
8400 /* Test each valid way of initializing the object, except for `= {0}`, as
8401 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8402 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008403 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008404 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008405 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008406 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8407 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008408
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008410
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008411 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008412 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8413 PSA_ERROR_BAD_STATE);
8414 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8415 PSA_ERROR_BAD_STATE);
8416 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8417 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008418
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008419 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008420 PSA_ASSERT(psa_key_derivation_abort(&func));
8421 PSA_ASSERT(psa_key_derivation_abort(&init));
8422 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008423}
8424/* END_CASE */
8425
Janos Follath16de4a42019-06-13 16:32:24 +01008426/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008427void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008428{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008429 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008430 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008431 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008432
Gilles Peskine449bd832023-01-11 14:50:10 +01008433 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008434
Gilles Peskine449bd832023-01-11 14:50:10 +01008435 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8436 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008437
8438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008439 psa_key_derivation_abort(&operation);
8440 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008441}
8442/* END_CASE */
8443
Janos Follathaf3c2a02019-06-12 12:34:34 +01008444/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008445void derive_set_capacity(int alg_arg, int capacity_arg,
8446 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008447{
8448 psa_algorithm_t alg = alg_arg;
8449 size_t capacity = capacity_arg;
8450 psa_status_t expected_status = expected_status_arg;
8451 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8452
Gilles Peskine449bd832023-01-11 14:50:10 +01008453 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008454
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008456
Gilles Peskine449bd832023-01-11 14:50:10 +01008457 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8458 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008459
8460exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 psa_key_derivation_abort(&operation);
8462 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008463}
8464/* END_CASE */
8465
8466/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008467void derive_input(int alg_arg,
8468 int step_arg1, int key_type_arg1, data_t *input1,
8469 int expected_status_arg1,
8470 int step_arg2, int key_type_arg2, data_t *input2,
8471 int expected_status_arg2,
8472 int step_arg3, int key_type_arg3, data_t *input3,
8473 int expected_status_arg3,
8474 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008475{
8476 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008477 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8478 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
8479 psa_status_t expected_statuses[] = { expected_status_arg1,
8480 expected_status_arg2,
8481 expected_status_arg3 };
8482 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008483 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8484 MBEDTLS_SVC_KEY_ID_INIT,
8485 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008486 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8488 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008489 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008490 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008491 psa_status_t expected_output_status = expected_output_status_arg;
8492 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008493
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008495
Gilles Peskine449bd832023-01-11 14:50:10 +01008496 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8497 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008498
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008500
Gilles Peskine449bd832023-01-11 14:50:10 +01008501 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8502 mbedtls_test_set_step(i);
8503 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008504 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8506 psa_set_key_type(&attributes, key_types[i]);
8507 PSA_ASSERT(psa_import_key(&attributes,
8508 inputs[i]->x, inputs[i]->len,
8509 &keys[i]));
8510 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8511 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008512 // When taking a private key as secret input, use key agreement
8513 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8515 &operation, keys[i]),
8516 expected_statuses[i]);
8517 } else {
8518 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8519 keys[i]),
8520 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008521 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008522 } else {
8523 TEST_EQUAL(psa_key_derivation_input_bytes(
8524 &operation, steps[i],
8525 inputs[i]->x, inputs[i]->len),
8526 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008527 }
8528 }
8529
Gilles Peskine449bd832023-01-11 14:50:10 +01008530 if (output_key_type != PSA_KEY_TYPE_NONE) {
8531 psa_reset_key_attributes(&attributes);
8532 psa_set_key_type(&attributes, output_key_type);
8533 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008534 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 psa_key_derivation_output_key(&attributes, &operation,
8536 &output_key);
8537 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008538 uint8_t buffer[1];
8539 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 psa_key_derivation_output_bytes(&operation,
8541 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008542 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008543 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008544
Janos Follathaf3c2a02019-06-12 12:34:34 +01008545exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008546 psa_key_derivation_abort(&operation);
8547 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8548 psa_destroy_key(keys[i]);
8549 }
8550 psa_destroy_key(output_key);
8551 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008552}
8553/* END_CASE */
8554
Janos Follathd958bb72019-07-03 15:02:16 +01008555/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008556void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008557{
Janos Follathd958bb72019-07-03 15:02:16 +01008558 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008560 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008561 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008562 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008564 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008565 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008566 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008567 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008568 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8569 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008572
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008574
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8576 psa_set_key_algorithm(&attributes, alg);
8577 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008578
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 PSA_ASSERT(psa_import_key(&attributes,
8580 key_data, sizeof(key_data),
8581 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008582
8583 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8585 input1, input1_length,
8586 input2, input2_length,
8587 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008588 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008590
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008591 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008592 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8593 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008594
Gilles Peskine449bd832023-01-11 14:50:10 +01008595 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008596
Gilles Peskine449bd832023-01-11 14:50:10 +01008597 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8598 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008599
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008600exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008601 psa_key_derivation_abort(&operation);
8602 psa_destroy_key(key);
8603 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008604}
8605/* END_CASE */
8606
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008607/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008608void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008609{
8610 uint8_t output_buffer[16];
8611 size_t buffer_size = 16;
8612 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008613 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008614
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8616 output_buffer, buffer_size)
8617 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008618
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8620 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008621
Gilles Peskine449bd832023-01-11 14:50:10 +01008622 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008623
Gilles Peskine449bd832023-01-11 14:50:10 +01008624 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8625 output_buffer, buffer_size)
8626 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008627
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8629 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008630
8631exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008632 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008633}
8634/* END_CASE */
8635
8636/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008637void derive_output(int alg_arg,
8638 int step1_arg, data_t *input1, int expected_status_arg1,
8639 int step2_arg, data_t *input2, int expected_status_arg2,
8640 int step3_arg, data_t *input3, int expected_status_arg3,
8641 int step4_arg, data_t *input4, int expected_status_arg4,
8642 data_t *key_agreement_peer_key,
8643 int requested_capacity_arg,
8644 data_t *expected_output1,
8645 data_t *expected_output2,
8646 int other_key_input_type,
8647 int key_input_type,
8648 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008649{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008650 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008651 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8652 data_t *inputs[] = { input1, input2, input3, input4 };
8653 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8654 MBEDTLS_SVC_KEY_ID_INIT,
8655 MBEDTLS_SVC_KEY_ID_INIT,
8656 MBEDTLS_SVC_KEY_ID_INIT };
8657 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8658 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008659 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008660 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008661 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008662 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008663 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008665 size_t output_buffer_size = 0;
8666 uint8_t *output_buffer = NULL;
8667 size_t expected_capacity;
8668 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008669 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8670 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8671 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8672 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008673 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008674 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008675 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008676
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8678 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008679 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 }
8681 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008682 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008683 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008684 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008685 ASSERT_ALLOC(output_buffer, output_buffer_size);
8686 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008687
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008688 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008689 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8690 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8691 requested_capacity));
8692 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8693 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008694 case 0:
8695 break;
8696 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008698 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 TEST_EQUAL(psa_key_derivation_input_bytes(
8700 &operation, steps[i],
8701 inputs[i]->x, inputs[i]->len),
8702 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008703
Gilles Peskine449bd832023-01-11 14:50:10 +01008704 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008705 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008707 break;
8708 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8710 psa_set_key_algorithm(&attributes1, alg);
8711 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008712
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 PSA_ASSERT(psa_import_key(&attributes1,
8714 inputs[i]->x, inputs[i]->len,
8715 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008716
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8718 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8719 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8720 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008721 }
8722
Gilles Peskine449bd832023-01-11 14:50:10 +01008723 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8724 steps[i],
8725 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008726 break;
8727 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008729 break;
8730 }
8731 break;
8732 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008733 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008734 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8736 steps[i],
8737 inputs[i]->x,
8738 inputs[i]->len),
8739 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008740 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008741 case 1: // input key, type DERIVE
8742 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8744 psa_set_key_algorithm(&attributes2, alg);
8745 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008746
8747 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008748 if (other_key_input_type == 11) {
8749 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8750 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008751
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 PSA_ASSERT(psa_import_key(&attributes2,
8753 inputs[i]->x, inputs[i]->len,
8754 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008755
Gilles Peskine449bd832023-01-11 14:50:10 +01008756 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8757 steps[i],
8758 keys[i]),
8759 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008760 break;
8761 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008762 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8763 psa_set_key_algorithm(&attributes3, alg);
8764 psa_set_key_type(&attributes3,
8765 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008766
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 PSA_ASSERT(psa_import_key(&attributes3,
8768 inputs[i]->x, inputs[i]->len,
8769 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008770
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 TEST_EQUAL(psa_key_derivation_key_agreement(
8772 &operation,
8773 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8774 keys[i], key_agreement_peer_key->x,
8775 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008776 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008777 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008778 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008779 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008780 }
8781
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008783 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008784 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008785 break;
8786 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 TEST_EQUAL(psa_key_derivation_input_bytes(
8788 &operation, steps[i],
8789 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008790
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008792 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008794 break;
8795 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008796 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008797
Gilles Peskine449bd832023-01-11 14:50:10 +01008798 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8799 &current_capacity));
8800 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008801 expected_capacity = requested_capacity;
8802
Gilles Peskine449bd832023-01-11 14:50:10 +01008803 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008804 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8805
8806 /* For output key derivation secret must be provided using
8807 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008809 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008811
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8813 psa_set_key_algorithm(&attributes4, alg);
8814 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8815 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008816
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8818 &derived_key), expected_status);
8819 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008820 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008822 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 status = psa_key_derivation_output_bytes(&operation,
8824 output_buffer, output_sizes[i]);
8825 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008826 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 TEST_ASSERT(status == PSA_SUCCESS ||
8828 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008829 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 } else if (expected_capacity == 0 ||
8831 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008832 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008833 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008834 expected_capacity = 0;
8835 continue;
8836 }
8837 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 PSA_ASSERT(status);
8839 if (output_sizes[i] != 0) {
8840 ASSERT_COMPARE(output_buffer, output_sizes[i],
8841 expected_outputs[i], output_sizes[i]);
8842 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008843 /* Check the operation status. */
8844 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008845 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8846 &current_capacity));
8847 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008848 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008849 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008851
8852exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 mbedtls_free(output_buffer);
8854 psa_key_derivation_abort(&operation);
8855 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8856 psa_destroy_key(keys[i]);
8857 }
8858 psa_destroy_key(derived_key);
8859 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008860}
8861/* END_CASE */
8862
8863/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008864void derive_full(int alg_arg,
8865 data_t *key_data,
8866 data_t *input1,
8867 data_t *input2,
8868 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008869{
Ronald Cron5425a212020-08-04 14:58:35 +02008870 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008871 psa_algorithm_t alg = alg_arg;
8872 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008873 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008874 unsigned char output_buffer[16];
8875 size_t expected_capacity = requested_capacity;
8876 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008878
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008880
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8882 psa_set_key_algorithm(&attributes, alg);
8883 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008884
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8886 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008887
Gilles Peskine449bd832023-01-11 14:50:10 +01008888 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8889 input1->x, input1->len,
8890 input2->x, input2->len,
8891 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008892 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008893 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008894
Gilles Peskine449bd832023-01-11 14:50:10 +01008895 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8896 &current_capacity));
8897 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008898
8899 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 while (current_capacity > 0) {
8901 size_t read_size = sizeof(output_buffer);
8902 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008903 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 }
8905 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8906 output_buffer,
8907 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008908 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8910 &current_capacity));
8911 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008912 }
8913
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008914 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8916 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008917
Gilles Peskine449bd832023-01-11 14:50:10 +01008918 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008919
8920exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008921 psa_key_derivation_abort(&operation);
8922 psa_destroy_key(key);
8923 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008924}
8925/* END_CASE */
8926
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008927/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008928void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8929 int derivation_step,
8930 int capacity, int expected_capacity_status_arg,
8931 data_t *expected_output,
8932 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008933{
8934 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8935 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008936 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008937 uint8_t *output_buffer = NULL;
8938 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008939 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8940 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8941 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008942
Gilles Peskine449bd832023-01-11 14:50:10 +01008943 ASSERT_ALLOC(output_buffer, expected_output->len);
8944 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008945
Gilles Peskine449bd832023-01-11 14:50:10 +01008946 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8947 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8948 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8951 step, input->x, input->len),
8952 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008953
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008955 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008957
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8959 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008960
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 TEST_EQUAL(status, expected_output_status);
8962 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8963 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8964 expected_output->len);
8965 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008966
8967exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 mbedtls_free(output_buffer);
8969 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008970 PSA_DONE();
8971}
8972/* END_CASE */
8973
Janos Follathe60c9052019-07-03 13:51:30 +01008974/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008975void derive_key_exercise(int alg_arg,
8976 data_t *key_data,
8977 data_t *input1,
8978 data_t *input2,
8979 int derived_type_arg,
8980 int derived_bits_arg,
8981 int derived_usage_arg,
8982 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008983{
Ronald Cron5425a212020-08-04 14:58:35 +02008984 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8985 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008986 psa_algorithm_t alg = alg_arg;
8987 psa_key_type_t derived_type = derived_type_arg;
8988 size_t derived_bits = derived_bits_arg;
8989 psa_key_usage_t derived_usage = derived_usage_arg;
8990 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008992 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008994 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008995
Gilles Peskine449bd832023-01-11 14:50:10 +01008996 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008997
Gilles Peskine449bd832023-01-11 14:50:10 +01008998 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8999 psa_set_key_algorithm(&attributes, alg);
9000 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9001 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9002 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009003
9004 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009005 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9006 input1->x, input1->len,
9007 input2->x, input2->len,
9008 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009009 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 }
Janos Follathe60c9052019-07-03 13:51:30 +01009011
Gilles Peskine449bd832023-01-11 14:50:10 +01009012 psa_set_key_usage_flags(&attributes, derived_usage);
9013 psa_set_key_algorithm(&attributes, derived_alg);
9014 psa_set_key_type(&attributes, derived_type);
9015 psa_set_key_bits(&attributes, derived_bits);
9016 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9017 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009018
9019 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9021 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9022 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009023
9024 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009026 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009028
9029exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009030 /*
9031 * Key attributes may have been returned by psa_get_key_attributes()
9032 * thus reset them as required.
9033 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009035
Gilles Peskine449bd832023-01-11 14:50:10 +01009036 psa_key_derivation_abort(&operation);
9037 psa_destroy_key(base_key);
9038 psa_destroy_key(derived_key);
9039 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009040}
9041/* END_CASE */
9042
Janos Follath42fd8882019-07-03 14:17:09 +01009043/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009044void derive_key_export(int alg_arg,
9045 data_t *key_data,
9046 data_t *input1,
9047 data_t *input2,
9048 int bytes1_arg,
9049 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009050{
Ronald Cron5425a212020-08-04 14:58:35 +02009051 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9052 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009053 psa_algorithm_t alg = alg_arg;
9054 size_t bytes1 = bytes1_arg;
9055 size_t bytes2 = bytes2_arg;
9056 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009057 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009058 uint8_t *output_buffer = NULL;
9059 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009060 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9061 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009062 size_t length;
9063
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 ASSERT_ALLOC(output_buffer, capacity);
9065 ASSERT_ALLOC(export_buffer, capacity);
9066 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009067
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9069 psa_set_key_algorithm(&base_attributes, alg);
9070 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9071 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9072 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009073
9074 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009075 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9076 input1->x, input1->len,
9077 input2->x, input2->len,
9078 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009080 }
Janos Follath42fd8882019-07-03 14:17:09 +01009081
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9083 output_buffer,
9084 capacity));
9085 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009086
9087 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9089 input1->x, input1->len,
9090 input2->x, input2->len,
9091 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009093 }
Janos Follath42fd8882019-07-03 14:17:09 +01009094
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9096 psa_set_key_algorithm(&derived_attributes, 0);
9097 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9098 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9099 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9100 &derived_key));
9101 PSA_ASSERT(psa_export_key(derived_key,
9102 export_buffer, bytes1,
9103 &length));
9104 TEST_EQUAL(length, bytes1);
9105 PSA_ASSERT(psa_destroy_key(derived_key));
9106 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9107 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9108 &derived_key));
9109 PSA_ASSERT(psa_export_key(derived_key,
9110 export_buffer + bytes1, bytes2,
9111 &length));
9112 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009113
9114 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
9116 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009117
9118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009119 mbedtls_free(output_buffer);
9120 mbedtls_free(export_buffer);
9121 psa_key_derivation_abort(&operation);
9122 psa_destroy_key(base_key);
9123 psa_destroy_key(derived_key);
9124 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009125}
9126/* END_CASE */
9127
9128/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009129void derive_key_type(int alg_arg,
9130 data_t *key_data,
9131 data_t *input1,
9132 data_t *input2,
9133 int key_type_arg, int bits_arg,
9134 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009135{
9136 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9137 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9138 const psa_algorithm_t alg = alg_arg;
9139 const psa_key_type_t key_type = key_type_arg;
9140 const size_t bits = bits_arg;
9141 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9142 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009144 uint8_t *export_buffer = NULL;
9145 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9146 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9147 size_t export_length;
9148
Gilles Peskine449bd832023-01-11 14:50:10 +01009149 ASSERT_ALLOC(export_buffer, export_buffer_size);
9150 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009151
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9153 psa_set_key_algorithm(&base_attributes, alg);
9154 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9155 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9156 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009157
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009159 &operation, base_key, alg,
9160 input1->x, input1->len,
9161 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009163 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009164 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009165
Gilles Peskine449bd832023-01-11 14:50:10 +01009166 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9167 psa_set_key_algorithm(&derived_attributes, 0);
9168 psa_set_key_type(&derived_attributes, key_type);
9169 psa_set_key_bits(&derived_attributes, bits);
9170 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9171 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009172
Gilles Peskine449bd832023-01-11 14:50:10 +01009173 PSA_ASSERT(psa_export_key(derived_key,
9174 export_buffer, export_buffer_size,
9175 &export_length));
9176 ASSERT_COMPARE(export_buffer, export_length,
9177 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009178
9179exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009180 mbedtls_free(export_buffer);
9181 psa_key_derivation_abort(&operation);
9182 psa_destroy_key(base_key);
9183 psa_destroy_key(derived_key);
9184 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009185}
9186/* END_CASE */
9187
9188/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009189void derive_key(int alg_arg,
9190 data_t *key_data, data_t *input1, data_t *input2,
9191 int type_arg, int bits_arg,
9192 int expected_status_arg,
9193 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009194{
Ronald Cron5425a212020-08-04 14:58:35 +02009195 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9196 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009197 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009198 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009199 size_t bits = bits_arg;
9200 psa_status_t expected_status = expected_status_arg;
9201 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9202 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9203 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9204
Gilles Peskine449bd832023-01-11 14:50:10 +01009205 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009206
Gilles Peskine449bd832023-01-11 14:50:10 +01009207 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9208 psa_set_key_algorithm(&base_attributes, alg);
9209 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9210 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9211 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009212
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9214 input1->x, input1->len,
9215 input2->x, input2->len,
9216 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009218 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009219
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9221 psa_set_key_algorithm(&derived_attributes, 0);
9222 psa_set_key_type(&derived_attributes, type);
9223 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009224
9225 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 psa_key_derivation_output_key(&derived_attributes,
9227 &operation,
9228 &derived_key);
9229 if (is_large_output > 0) {
9230 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9231 }
9232 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009233
9234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 psa_key_derivation_abort(&operation);
9236 psa_destroy_key(base_key);
9237 psa_destroy_key(derived_key);
9238 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009239}
9240/* END_CASE */
9241
9242/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009243void key_agreement_setup(int alg_arg,
9244 int our_key_type_arg, int our_key_alg_arg,
9245 data_t *our_key_data, data_t *peer_key_data,
9246 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009247{
Ronald Cron5425a212020-08-04 14:58:35 +02009248 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009249 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009250 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009251 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009252 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009254 psa_status_t expected_status = expected_status_arg;
9255 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009256
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009258
Gilles Peskine449bd832023-01-11 14:50:10 +01009259 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9260 psa_set_key_algorithm(&attributes, our_key_alg);
9261 psa_set_key_type(&attributes, our_key_type);
9262 PSA_ASSERT(psa_import_key(&attributes,
9263 our_key_data->x, our_key_data->len,
9264 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009265
Gilles Peskine77f40d82019-04-11 21:27:06 +02009266 /* The tests currently include inputs that should fail at either step.
9267 * Test cases that fail at the setup step should be changed to call
9268 * key_derivation_setup instead, and this function should be renamed
9269 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 status = psa_key_derivation_setup(&operation, alg);
9271 if (status == PSA_SUCCESS) {
9272 TEST_EQUAL(psa_key_derivation_key_agreement(
9273 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9274 our_key,
9275 peer_key_data->x, peer_key_data->len),
9276 expected_status);
9277 } else {
9278 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009279 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009280
9281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009282 psa_key_derivation_abort(&operation);
9283 psa_destroy_key(our_key);
9284 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009285}
9286/* END_CASE */
9287
9288/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009289void raw_key_agreement(int alg_arg,
9290 int our_key_type_arg, data_t *our_key_data,
9291 data_t *peer_key_data,
9292 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009293{
Ronald Cron5425a212020-08-04 14:58:35 +02009294 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009295 psa_algorithm_t alg = alg_arg;
9296 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009297 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009298 unsigned char *output = NULL;
9299 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009300 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009301
Gilles Peskine449bd832023-01-11 14:50:10 +01009302 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009303
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9305 psa_set_key_algorithm(&attributes, alg);
9306 psa_set_key_type(&attributes, our_key_type);
9307 PSA_ASSERT(psa_import_key(&attributes,
9308 our_key_data->x, our_key_data->len,
9309 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009310
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9312 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009313
Gilles Peskine992bee82022-04-13 23:25:52 +02009314 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009315 TEST_LE_U(expected_output->len,
9316 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9317 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9318 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009319
9320 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009321 ASSERT_ALLOC(output, expected_output->len);
9322 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9323 peer_key_data->x, peer_key_data->len,
9324 output, expected_output->len,
9325 &output_length));
9326 ASSERT_COMPARE(output, output_length,
9327 expected_output->x, expected_output->len);
9328 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009329 output = NULL;
9330 output_length = ~0;
9331
9332 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009333 ASSERT_ALLOC(output, expected_output->len + 1);
9334 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9335 peer_key_data->x, peer_key_data->len,
9336 output, expected_output->len + 1,
9337 &output_length));
9338 ASSERT_COMPARE(output, output_length,
9339 expected_output->x, expected_output->len);
9340 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009341 output = NULL;
9342 output_length = ~0;
9343
9344 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009345 ASSERT_ALLOC(output, expected_output->len - 1);
9346 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9347 peer_key_data->x, peer_key_data->len,
9348 output, expected_output->len - 1,
9349 &output_length),
9350 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009351 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009352 TEST_LE_U(output_length, expected_output->len - 1);
9353 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009354 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009355
9356exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009357 mbedtls_free(output);
9358 psa_destroy_key(our_key);
9359 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009360}
9361/* END_CASE */
9362
9363/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009364void key_agreement_capacity(int alg_arg,
9365 int our_key_type_arg, data_t *our_key_data,
9366 data_t *peer_key_data,
9367 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009368{
Ronald Cron5425a212020-08-04 14:58:35 +02009369 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009370 psa_algorithm_t alg = alg_arg;
9371 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009372 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009374 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009375 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009376
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009378
Gilles Peskine449bd832023-01-11 14:50:10 +01009379 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9380 psa_set_key_algorithm(&attributes, alg);
9381 psa_set_key_type(&attributes, our_key_type);
9382 PSA_ASSERT(psa_import_key(&attributes,
9383 our_key_data->x, our_key_data->len,
9384 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009385
Gilles Peskine449bd832023-01-11 14:50:10 +01009386 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9387 PSA_ASSERT(psa_key_derivation_key_agreement(
9388 &operation,
9389 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9390 peer_key_data->x, peer_key_data->len));
9391 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009392 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009393 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9394 PSA_KEY_DERIVATION_INPUT_INFO,
9395 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009396 }
Gilles Peskine59685592018-09-18 12:11:34 +02009397
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009398 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 PSA_ASSERT(psa_key_derivation_get_capacity(
9400 &operation, &actual_capacity));
9401 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009402
Gilles Peskinebf491972018-10-25 22:36:12 +02009403 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 while (actual_capacity > sizeof(output)) {
9405 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9406 output, sizeof(output)));
9407 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009408 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009409 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9410 output, actual_capacity));
9411 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9412 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009413
Gilles Peskine59685592018-09-18 12:11:34 +02009414exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009415 psa_key_derivation_abort(&operation);
9416 psa_destroy_key(our_key);
9417 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009418}
9419/* END_CASE */
9420
9421/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009422void key_agreement_output(int alg_arg,
9423 int our_key_type_arg, data_t *our_key_data,
9424 data_t *peer_key_data,
9425 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009426{
Ronald Cron5425a212020-08-04 14:58:35 +02009427 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009428 psa_algorithm_t alg = alg_arg;
9429 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009430 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009432 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009433
Gilles Peskine449bd832023-01-11 14:50:10 +01009434 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9435 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009436
Gilles Peskine449bd832023-01-11 14:50:10 +01009437 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009438
Gilles Peskine449bd832023-01-11 14:50:10 +01009439 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9440 psa_set_key_algorithm(&attributes, alg);
9441 psa_set_key_type(&attributes, our_key_type);
9442 PSA_ASSERT(psa_import_key(&attributes,
9443 our_key_data->x, our_key_data->len,
9444 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009445
Gilles Peskine449bd832023-01-11 14:50:10 +01009446 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9447 PSA_ASSERT(psa_key_derivation_key_agreement(
9448 &operation,
9449 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9450 peer_key_data->x, peer_key_data->len));
9451 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009452 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009453 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9454 PSA_KEY_DERIVATION_INPUT_INFO,
9455 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009456 }
Gilles Peskine59685592018-09-18 12:11:34 +02009457
Gilles Peskine449bd832023-01-11 14:50:10 +01009458 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9459 actual_output,
9460 expected_output1->len));
9461 ASSERT_COMPARE(actual_output, expected_output1->len,
9462 expected_output1->x, expected_output1->len);
9463 if (expected_output2->len != 0) {
9464 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9465 actual_output,
9466 expected_output2->len));
9467 ASSERT_COMPARE(actual_output, expected_output2->len,
9468 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009469 }
Gilles Peskine59685592018-09-18 12:11:34 +02009470
9471exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 psa_key_derivation_abort(&operation);
9473 psa_destroy_key(our_key);
9474 PSA_DONE();
9475 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009476}
9477/* END_CASE */
9478
9479/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009480void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009481{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009482 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009483 unsigned char *output = NULL;
9484 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009485 size_t i;
9486 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009487
Gilles Peskine449bd832023-01-11 14:50:10 +01009488 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009489
Gilles Peskine449bd832023-01-11 14:50:10 +01009490 ASSERT_ALLOC(output, bytes);
9491 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009492
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009494
Gilles Peskinea50d7392018-06-21 10:22:13 +02009495 /* Run several times, to ensure that every output byte will be
9496 * nonzero at least once with overwhelming probability
9497 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009498 for (run = 0; run < 10; run++) {
9499 if (bytes != 0) {
9500 memset(output, 0, bytes);
9501 }
9502 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009503
Gilles Peskine449bd832023-01-11 14:50:10 +01009504 for (i = 0; i < bytes; i++) {
9505 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009506 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009507 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009508 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009509 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009510
9511 /* Check that every byte was changed to nonzero at least once. This
9512 * validates that psa_generate_random is overwriting every byte of
9513 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009514 for (i = 0; i < bytes; i++) {
9515 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009516 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009517
9518exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 PSA_DONE();
9520 mbedtls_free(output);
9521 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009522}
9523/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009524
9525/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009526void generate_key(int type_arg,
9527 int bits_arg,
9528 int usage_arg,
9529 int alg_arg,
9530 int expected_status_arg,
9531 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009532{
Ronald Cron5425a212020-08-04 14:58:35 +02009533 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009534 psa_key_type_t type = type_arg;
9535 psa_key_usage_t usage = usage_arg;
9536 size_t bits = bits_arg;
9537 psa_algorithm_t alg = alg_arg;
9538 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009540 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009541
Gilles Peskine449bd832023-01-11 14:50:10 +01009542 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009543
Gilles Peskine449bd832023-01-11 14:50:10 +01009544 psa_set_key_usage_flags(&attributes, usage);
9545 psa_set_key_algorithm(&attributes, alg);
9546 psa_set_key_type(&attributes, type);
9547 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009548
9549 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009550 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009551
Gilles Peskine449bd832023-01-11 14:50:10 +01009552 if (is_large_key > 0) {
9553 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9554 }
9555 TEST_EQUAL(status, expected_status);
9556 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009557 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009558 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009559
9560 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009561 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9562 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9563 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009564
Gilles Peskine818ca122018-06-20 18:16:48 +02009565 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009567 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009568 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009569
9570exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009571 /*
9572 * Key attributes may have been returned by psa_get_key_attributes()
9573 * thus reset them as required.
9574 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009575 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009576
Gilles Peskine449bd832023-01-11 14:50:10 +01009577 psa_destroy_key(key);
9578 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009579}
9580/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009581
Ronald Cronee414c72021-03-18 18:50:08 +01009582/* 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 +01009583void generate_key_rsa(int bits_arg,
9584 data_t *e_arg,
9585 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009586{
Ronald Cron5425a212020-08-04 14:58:35 +02009587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009588 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009589 size_t bits = bits_arg;
9590 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9591 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9592 psa_status_t expected_status = expected_status_arg;
9593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9594 uint8_t *exported = NULL;
9595 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009596 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009597 size_t exported_length = SIZE_MAX;
9598 uint8_t *e_read_buffer = NULL;
9599 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009600 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009601 size_t e_read_length = SIZE_MAX;
9602
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 if (e_arg->len == 0 ||
9604 (e_arg->len == 3 &&
9605 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009606 is_default_public_exponent = 1;
9607 e_read_size = 0;
9608 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009609 ASSERT_ALLOC(e_read_buffer, e_read_size);
9610 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009611
Gilles Peskine449bd832023-01-11 14:50:10 +01009612 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009613
Gilles Peskine449bd832023-01-11 14:50:10 +01009614 psa_set_key_usage_flags(&attributes, usage);
9615 psa_set_key_algorithm(&attributes, alg);
9616 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9617 e_arg->x, e_arg->len));
9618 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009619
9620 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009621 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9622 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009624 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009625
9626 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9628 TEST_EQUAL(psa_get_key_type(&attributes), type);
9629 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9630 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9631 e_read_buffer, e_read_size,
9632 &e_read_length));
9633 if (is_default_public_exponent) {
9634 TEST_EQUAL(e_read_length, 0);
9635 } else {
9636 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9637 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009638
9639 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009640 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009641 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009642 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009643
9644 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009645 PSA_ASSERT(psa_export_public_key(key,
9646 exported, exported_size,
9647 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009648 {
9649 uint8_t *p = exported;
9650 uint8_t *end = exported + exported_length;
9651 size_t len;
9652 /* RSAPublicKey ::= SEQUENCE {
9653 * modulus INTEGER, -- n
9654 * publicExponent INTEGER } -- e
9655 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009656 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9657 MBEDTLS_ASN1_SEQUENCE |
9658 MBEDTLS_ASN1_CONSTRUCTED));
9659 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9660 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9661 MBEDTLS_ASN1_INTEGER));
9662 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009663 ++p;
9664 --len;
9665 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009666 if (e_arg->len == 0) {
9667 TEST_EQUAL(len, 3);
9668 TEST_EQUAL(p[0], 1);
9669 TEST_EQUAL(p[1], 0);
9670 TEST_EQUAL(p[2], 1);
9671 } else {
9672 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009673 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009674 }
9675
9676exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009677 /*
9678 * Key attributes may have been returned by psa_get_key_attributes() or
9679 * set by psa_set_key_domain_parameters() thus reset them as required.
9680 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009681 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009682
Gilles Peskine449bd832023-01-11 14:50:10 +01009683 psa_destroy_key(key);
9684 PSA_DONE();
9685 mbedtls_free(e_read_buffer);
9686 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009687}
9688/* END_CASE */
9689
Darryl Greend49a4992018-06-18 17:27:26 +01009690/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009691void persistent_key_load_key_from_storage(data_t *data,
9692 int type_arg, int bits_arg,
9693 int usage_flags_arg, int alg_arg,
9694 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009695{
Gilles Peskine449bd832023-01-11 14:50:10 +01009696 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009697 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9699 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009700 psa_key_type_t type = type_arg;
9701 size_t bits = bits_arg;
9702 psa_key_usage_t usage_flags = usage_flags_arg;
9703 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009704 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009705 unsigned char *first_export = NULL;
9706 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009707 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009708 size_t first_exported_length;
9709 size_t second_exported_length;
9710
Gilles Peskine449bd832023-01-11 14:50:10 +01009711 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9712 ASSERT_ALLOC(first_export, export_size);
9713 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009714 }
Darryl Greend49a4992018-06-18 17:27:26 +01009715
Gilles Peskine449bd832023-01-11 14:50:10 +01009716 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009717
Gilles Peskine449bd832023-01-11 14:50:10 +01009718 psa_set_key_id(&attributes, key_id);
9719 psa_set_key_usage_flags(&attributes, usage_flags);
9720 psa_set_key_algorithm(&attributes, alg);
9721 psa_set_key_type(&attributes, type);
9722 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009723
Gilles Peskine449bd832023-01-11 14:50:10 +01009724 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009725 case IMPORT_KEY:
9726 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009727 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9728 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009729 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009730
Darryl Green0c6575a2018-11-07 16:05:30 +00009731 case GENERATE_KEY:
9732 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009733 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009734 break;
9735
9736 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009737#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009738 {
9739 /* Create base key */
9740 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9741 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9742 psa_set_key_usage_flags(&base_attributes,
9743 PSA_KEY_USAGE_DERIVE);
9744 psa_set_key_algorithm(&base_attributes, derive_alg);
9745 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9746 PSA_ASSERT(psa_import_key(&base_attributes,
9747 data->x, data->len,
9748 &base_key));
9749 /* Derive a key. */
9750 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9751 PSA_ASSERT(psa_key_derivation_input_key(
9752 &operation,
9753 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9754 PSA_ASSERT(psa_key_derivation_input_bytes(
9755 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9756 NULL, 0));
9757 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9758 &operation,
9759 &key));
9760 PSA_ASSERT(psa_key_derivation_abort(&operation));
9761 PSA_ASSERT(psa_destroy_key(base_key));
9762 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9763 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009764#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009765 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009766#endif
9767 break;
9768
9769 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009770 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009771 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009772 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009773 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009774
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009775 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009776 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9777 PSA_ASSERT(psa_export_key(key,
9778 first_export, export_size,
9779 &first_exported_length));
9780 if (generation_method == IMPORT_KEY) {
9781 ASSERT_COMPARE(data->x, data->len,
9782 first_export, first_exported_length);
9783 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009784 }
Darryl Greend49a4992018-06-18 17:27:26 +01009785
9786 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009787 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009788 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009789 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009790
Darryl Greend49a4992018-06-18 17:27:26 +01009791 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009792 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9793 TEST_ASSERT(mbedtls_svc_key_id_equal(
9794 psa_get_key_id(&attributes), key_id));
9795 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9796 PSA_KEY_LIFETIME_PERSISTENT);
9797 TEST_EQUAL(psa_get_key_type(&attributes), type);
9798 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9799 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9800 mbedtls_test_update_key_usage_flags(usage_flags));
9801 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009802
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009803 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009804 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9805 PSA_ASSERT(psa_export_key(key,
9806 second_export, export_size,
9807 &second_exported_length));
9808 ASSERT_COMPARE(first_export, first_exported_length,
9809 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009810 }
9811
9812 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009813 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009814 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009815 }
Darryl Greend49a4992018-06-18 17:27:26 +01009816
9817exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009818 /*
9819 * Key attributes may have been returned by psa_get_key_attributes()
9820 * thus reset them as required.
9821 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009822 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009823
Gilles Peskine449bd832023-01-11 14:50:10 +01009824 mbedtls_free(first_export);
9825 mbedtls_free(second_export);
9826 psa_key_derivation_abort(&operation);
9827 psa_destroy_key(base_key);
9828 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009829 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009830}
9831/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009832
Neil Armstronga557cb82022-06-10 08:58:32 +02009833/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009834void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9835 int primitive_arg, int hash_arg, int role_arg,
9836 int test_input, data_t *pw_data,
9837 int inj_err_type_arg,
9838 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009839{
9840 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9841 psa_pake_operation_t operation = psa_pake_operation_init();
9842 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009843 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009844 psa_key_type_t key_type_pw = key_type_pw_arg;
9845 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009846 psa_algorithm_t hash_alg = hash_arg;
9847 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009848 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9849 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009850 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9851 psa_status_t expected_error = expected_error_arg;
9852 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009853 unsigned char *output_buffer = NULL;
9854 size_t output_len = 0;
9855
Gilles Peskine449bd832023-01-11 14:50:10 +01009856 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009857
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009858 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009859 PSA_PAKE_STEP_KEY_SHARE);
9860 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009861
Gilles Peskine449bd832023-01-11 14:50:10 +01009862 if (pw_data->len > 0) {
9863 psa_set_key_usage_flags(&attributes, key_usage_pw);
9864 psa_set_key_algorithm(&attributes, alg);
9865 psa_set_key_type(&attributes, key_type_pw);
9866 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9867 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009868 }
9869
Gilles Peskine449bd832023-01-11 14:50:10 +01009870 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9871 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9872 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009873
Gilles Peskine449bd832023-01-11 14:50:10 +01009874 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009875
Gilles Peskine449bd832023-01-11 14:50:10 +01009876 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9877 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9878 expected_error);
9879 PSA_ASSERT(psa_pake_abort(&operation));
9880 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9881 expected_error);
9882 PSA_ASSERT(psa_pake_abort(&operation));
9883 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9884 expected_error);
9885 PSA_ASSERT(psa_pake_abort(&operation));
9886 TEST_EQUAL(psa_pake_set_role(&operation, role),
9887 expected_error);
9888 PSA_ASSERT(psa_pake_abort(&operation));
9889 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9890 NULL, 0, NULL),
9891 expected_error);
9892 PSA_ASSERT(psa_pake_abort(&operation));
9893 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9894 expected_error);
9895 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009896 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009897 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009898
Gilles Peskine449bd832023-01-11 14:50:10 +01009899 status = psa_pake_setup(&operation, &cipher_suite);
9900 if (status != PSA_SUCCESS) {
9901 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009902 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009903 }
9904
Gilles Peskine449bd832023-01-11 14:50:10 +01009905 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9906 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9907 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009908 goto exit;
9909 }
9910
Gilles Peskine449bd832023-01-11 14:50:10 +01009911 status = psa_pake_set_role(&operation, role);
9912 if (status != PSA_SUCCESS) {
9913 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009914 goto exit;
9915 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009916
Gilles Peskine449bd832023-01-11 14:50:10 +01009917 if (pw_data->len > 0) {
9918 status = psa_pake_set_password_key(&operation, key);
9919 if (status != PSA_SUCCESS) {
9920 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009921 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009922 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009923 }
9924
Gilles Peskine449bd832023-01-11 14:50:10 +01009925 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9926 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9927 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009928 goto exit;
9929 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009930
Gilles Peskine449bd832023-01-11 14:50:10 +01009931 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9932 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9933 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009934 goto exit;
9935 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009936
Gilles Peskine449bd832023-01-11 14:50:10 +01009937 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009938 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009939 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9940 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009941 goto exit;
9942 }
9943
Gilles Peskine449bd832023-01-11 14:50:10 +01009944 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009945 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009946 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9947 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009948 goto exit;
9949 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009950
Gilles Peskine449bd832023-01-11 14:50:10 +01009951 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9952 PSA_PAKE_STEP_KEY_SHARE);
9953 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9954 PSA_PAKE_STEP_ZK_PUBLIC);
9955 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9956 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009957
Gilles Peskine449bd832023-01-11 14:50:10 +01009958 if (test_input) {
9959 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9960 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9961 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009962 goto exit;
9963 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009964
Gilles Peskine449bd832023-01-11 14:50:10 +01009965 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9966 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9967 output_buffer, size_zk_proof),
9968 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009969 goto exit;
9970 }
9971
Gilles Peskine449bd832023-01-11 14:50:10 +01009972 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9973 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9974 output_buffer, size_zk_proof),
9975 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009976 goto exit;
9977 }
9978
Gilles Peskine449bd832023-01-11 14:50:10 +01009979 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9980 output_buffer, size_key_share);
9981 if (status != PSA_SUCCESS) {
9982 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009983 goto exit;
9984 }
9985
Gilles Peskine449bd832023-01-11 14:50:10 +01009986 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9987 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9988 output_buffer, size_zk_public + 1),
9989 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009990 goto exit;
9991 }
9992
Gilles Peskine449bd832023-01-11 14:50:10 +01009993 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009994 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009995 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9996 output_buffer, size_zk_public + 1);
9997 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9998 output_buffer, size_zk_public),
9999 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010000 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010001 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010002 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010003 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10004 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10005 NULL, 0, NULL),
10006 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010007 goto exit;
10008 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010009
Gilles Peskine449bd832023-01-11 14:50:10 +010010010 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10011 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10012 output_buffer, buf_size, &output_len),
10013 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010014 goto exit;
10015 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010016
Gilles Peskine449bd832023-01-11 14:50:10 +010010017 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10018 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10019 output_buffer, buf_size, &output_len),
10020 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010021 goto exit;
10022 }
10023
Gilles Peskine449bd832023-01-11 14:50:10 +010010024 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10025 output_buffer, buf_size, &output_len);
10026 if (status != PSA_SUCCESS) {
10027 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010028 goto exit;
10029 }
10030
Gilles Peskine449bd832023-01-11 14:50:10 +010010031 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010032
Gilles Peskine449bd832023-01-11 14:50:10 +010010033 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10034 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10035 output_buffer, size_zk_public - 1, &output_len),
10036 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010037 goto exit;
10038 }
10039
Gilles Peskine449bd832023-01-11 14:50:10 +010010040 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010041 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010042 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10043 output_buffer, size_zk_public - 1, &output_len);
10044 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10045 output_buffer, buf_size, &output_len),
10046 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010047 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010048 }
10049 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010050
10051exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010052 PSA_ASSERT(psa_destroy_key(key));
10053 PSA_ASSERT(psa_pake_abort(&operation));
10054 mbedtls_free(output_buffer);
10055 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010056}
10057/* END_CASE */
10058
Neil Armstronga557cb82022-06-10 08:58:32 +020010059/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010060void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10061 int client_input_first, int inject_error,
10062 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010063{
10064 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10065 psa_pake_operation_t server = psa_pake_operation_init();
10066 psa_pake_operation_t client = psa_pake_operation_init();
10067 psa_algorithm_t alg = alg_arg;
10068 psa_algorithm_t hash_alg = hash_arg;
10069 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10071
Gilles Peskine449bd832023-01-11 14:50:10 +010010072 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010073
Gilles Peskine449bd832023-01-11 14:50:10 +010010074 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10075 psa_set_key_algorithm(&attributes, alg);
10076 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10077 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10078 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010079
Gilles Peskine449bd832023-01-11 14:50:10 +010010080 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10081 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10082 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010083
10084
Gilles Peskine449bd832023-01-11 14:50:10 +010010085 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10086 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010087
Gilles Peskine449bd832023-01-11 14:50:10 +010010088 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10089 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010090
Gilles Peskine449bd832023-01-11 14:50:10 +010010091 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10092 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010093
Gilles Peskine449bd832023-01-11 14:50:10 +010010094 ecjpake_do_round(alg, primitive_arg, &server, &client,
10095 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010096
Gilles Peskine449bd832023-01-11 14:50:10 +010010097 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010098 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010099 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010100
Gilles Peskine449bd832023-01-11 14:50:10 +010010101 ecjpake_do_round(alg, primitive_arg, &server, &client,
10102 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010103
10104exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010105 psa_destroy_key(key);
10106 psa_pake_abort(&server);
10107 psa_pake_abort(&client);
10108 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010109}
10110/* END_CASE */
10111
10112/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010113void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10114 int derive_alg_arg, data_t *pw_data,
10115 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010116{
10117 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10118 psa_pake_operation_t server = psa_pake_operation_init();
10119 psa_pake_operation_t client = psa_pake_operation_init();
10120 psa_algorithm_t alg = alg_arg;
10121 psa_algorithm_t hash_alg = hash_arg;
10122 psa_algorithm_t derive_alg = derive_alg_arg;
10123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10125 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010126 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010127 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010128 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010129 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010130
Gilles Peskine449bd832023-01-11 14:50:10 +010010131 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010132
Gilles Peskine449bd832023-01-11 14:50:10 +010010133 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10134 psa_set_key_algorithm(&attributes, alg);
10135 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10136 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10137 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010138
Gilles Peskine449bd832023-01-11 14:50:10 +010010139 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10140 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10141 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010142
Neil Armstrong1e855602022-06-15 11:32:11 +020010143 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010144 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10145 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010146
Gilles Peskine449bd832023-01-11 14:50:10 +010010147 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10148 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10149 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10150 PSA_KEY_DERIVATION_INPUT_SEED,
10151 (const uint8_t *) "", 0));
10152 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10153 PSA_KEY_DERIVATION_INPUT_SEED,
10154 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010155 }
10156
Gilles Peskine449bd832023-01-11 14:50:10 +010010157 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10158 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010159
Gilles Peskine449bd832023-01-11 14:50:10 +010010160 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10161 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010162
Gilles Peskine449bd832023-01-11 14:50:10 +010010163 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10164 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010165
Gilles Peskine449bd832023-01-11 14:50:10 +010010166 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10167 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10168 PSA_ERROR_BAD_STATE);
10169 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10170 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010171 goto exit;
10172 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010173
Neil Armstrongf983caf2022-06-15 15:27:48 +020010174 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010175 ecjpake_do_round(alg, primitive_arg, &server, &client,
10176 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010177
Gilles Peskine449bd832023-01-11 14:50:10 +010010178 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10179 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10180 PSA_ERROR_BAD_STATE);
10181 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10182 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010183 goto exit;
10184 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010185
Neil Armstrongf983caf2022-06-15 15:27:48 +020010186 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010187 ecjpake_do_round(alg, primitive_arg, &server, &client,
10188 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010189
Gilles Peskine449bd832023-01-11 14:50:10 +010010190 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10191 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010192
10193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010194 psa_key_derivation_abort(&server_derive);
10195 psa_key_derivation_abort(&client_derive);
10196 psa_destroy_key(key);
10197 psa_pake_abort(&server);
10198 psa_pake_abort(&client);
10199 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010200}
10201/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010202
10203/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010204void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010205{
10206 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10207 const size_t bits = 256;
10208 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010209 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010210 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010211 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010212
10213 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10214 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010215 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10216 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10217 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10218 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010219 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010220 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10221 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010222
10223 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010224 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10225 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10226 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10227 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10228 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10229 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010230
10231 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010232 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10233 PSA_PAKE_OUTPUT_MAX_SIZE);
10234 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10235 PSA_PAKE_OUTPUT_MAX_SIZE);
10236 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10237 PSA_PAKE_OUTPUT_MAX_SIZE);
10238 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10239 PSA_PAKE_INPUT_MAX_SIZE);
10240 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10241 PSA_PAKE_INPUT_MAX_SIZE);
10242 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10243 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010244}
10245/* END_CASE */