blob: 41a3237b3ef576b6bf655e56577838066f051255 [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
Przemek Stekiel8258ea72022-10-19 12:17:19 +020028#include "mbedtls/legacy_or_psa.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010029
Gilles Peskine4023c012021-05-27 13:21:20 +020030/* If this comes up, it's a bug in the test code or in the test data. */
31#define UNUSED 0xdeadbeef
32
Dave Rodgman647791d2021-06-23 12:49:59 +010033/* Assert that an operation is (not) active.
34 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010035#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
36#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010037
Przemek Stekiel7c795482022-11-15 22:26:12 +010038#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010039int ecjpake_operation_setup(psa_pake_operation_t *operation,
40 psa_pake_cipher_suite_t *cipher_suite,
41 psa_pake_role_t role,
42 mbedtls_svc_key_id_t key,
43 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010044{
Gilles Peskine449bd832023-01-11 14:50:10 +010045 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010046
Gilles Peskine449bd832023-01-11 14:50:10 +010047 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010050
Gilles Peskine449bd832023-01-11 14:50:10 +010051 if (key_available) {
52 PSA_ASSERT(psa_pake_set_password_key(operation, key));
53 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010054 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010055exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010056 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010057}
58#endif
59
Jaeden Amerof24c7f82018-06-27 17:20:43 +010060/** An invalid export length that will never be set by psa_export_key(). */
61static const size_t INVALID_EXPORT_LENGTH = ~0U;
62
Gilles Peskinea7aa4422018-08-14 15:17:54 +020063/** Test if a buffer contains a constant byte value.
64 *
65 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020066 *
67 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020068 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020069 * \param size Size of the buffer in bytes.
70 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020071 * \return 1 if the buffer is all-bits-zero.
72 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020073 */
Gilles Peskine449bd832023-01-11 14:50:10 +010074static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020075{
76 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010077 for (i = 0; i < size; i++) {
78 if (((unsigned char *) buffer)[i] != c) {
79 return 0;
80 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020081 }
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020083}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010084#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020085/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010086static int asn1_write_10x(unsigned char **p,
87 unsigned char *start,
88 size_t bits,
89 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020090{
91 int ret;
92 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (bits == 0) {
94 return MBEDTLS_ERR_ASN1_INVALID_DATA;
95 }
96 if (bits <= 8 && x >= 1 << (bits - 1)) {
97 return MBEDTLS_ERR_ASN1_INVALID_DATA;
98 }
99 if (*p < start || *p - start < (ptrdiff_t) len) {
100 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
101 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200102 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 (*p)[len-1] = x;
104 if (bits % 8 == 0) {
105 (*p)[1] |= 1;
106 } else {
107 (*p)[0] |= 1 << (bits % 8);
108 }
109 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
110 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
111 MBEDTLS_ASN1_INTEGER));
112 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200113}
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115static int construct_fake_rsa_key(unsigned char *buffer,
116 size_t buffer_size,
117 unsigned char **p,
118 size_t bits,
119 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200120{
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200122 int ret;
123 int len = 0;
124 /* Construct something that looks like a DER encoding of
125 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
126 * RSAPrivateKey ::= SEQUENCE {
127 * version Version,
128 * modulus INTEGER, -- n
129 * publicExponent INTEGER, -- e
130 * privateExponent INTEGER, -- d
131 * prime1 INTEGER, -- p
132 * prime2 INTEGER, -- q
133 * exponent1 INTEGER, -- d mod (p-1)
134 * exponent2 INTEGER, -- d mod (q-1)
135 * coefficient INTEGER, -- (inverse of q) mod p
136 * otherPrimeInfos OtherPrimeInfos OPTIONAL
137 * }
138 * Or, for a public key, the same structure with only
139 * version, modulus and publicExponent.
140 */
141 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (keypair) {
143 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
144 asn1_write_10x(p, buffer, half_bits, 1));
145 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
146 asn1_write_10x(p, buffer, half_bits, 1));
147 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
148 asn1_write_10x(p, buffer, half_bits, 1));
149 MBEDTLS_ASN1_CHK_ADD(len, /* q */
150 asn1_write_10x(p, buffer, half_bits, 1));
151 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
152 asn1_write_10x(p, buffer, half_bits, 3));
153 MBEDTLS_ASN1_CHK_ADD(len, /* d */
154 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200155 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
157 asn1_write_10x(p, buffer, 17, 1));
158 MBEDTLS_ASN1_CHK_ADD(len, /* n */
159 asn1_write_10x(p, buffer, bits, 1));
160 if (keypair) {
161 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
162 mbedtls_asn1_write_int(p, buffer, 0));
163 }
164 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200165 {
166 const unsigned char tag =
167 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200169 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200171}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100172#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174int exercise_mac_setup(psa_key_type_t key_type,
175 const unsigned char *key_bytes,
176 size_t key_length,
177 psa_algorithm_t alg,
178 psa_mac_operation_t *operation,
179 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100180{
Ronald Cron5425a212020-08-04 14:58:35 +0200181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
185 psa_set_key_algorithm(&attributes, alg);
186 psa_set_key_type(&attributes, key_type);
187 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100190 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* If setup failed, reproduce the failure, so that the caller can
193 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (*status != PSA_SUCCESS) {
195 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 }
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 psa_destroy_key(key);
199 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200
201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 psa_destroy_key(key);
203 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100204}
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int exercise_cipher_setup(psa_key_type_t key_type,
207 const unsigned char *key_bytes,
208 size_t key_length,
209 psa_algorithm_t alg,
210 psa_cipher_operation_t *operation,
211 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100212{
Ronald Cron5425a212020-08-04 14:58:35 +0200213 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
217 psa_set_key_algorithm(&attributes, alg);
218 psa_set_key_type(&attributes, key_type);
219 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100224 /* If setup failed, reproduce the failure, so that the caller can
225 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (*status != PSA_SUCCESS) {
227 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
228 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100229 }
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 psa_destroy_key(key);
232 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100233
234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 psa_destroy_key(key);
236 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100237}
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240{
241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200243 uint8_t buffer[1];
244 size_t length;
245 int ok = 0;
246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 psa_set_key_id(&attributes, key_id);
248 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
249 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
250 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
251 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
252 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200253 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200255 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
257 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
258 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
259 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
260 TEST_EQUAL(psa_get_key_type(&attributes), 0);
261 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
264 PSA_ERROR_INVALID_HANDLE);
265 TEST_EQUAL(psa_export_public_key(key,
266 buffer, sizeof(buffer), &length),
267 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200268
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200269 ok = 1;
270
271exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100272 /*
273 * Key attributes may have been returned by psa_get_key_attributes()
274 * thus reset them as required.
275 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200279}
280
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200281/* Assert that a key isn't reported as having a slot number. */
282#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100283#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200284 do \
285 { \
286 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 TEST_EQUAL(psa_get_key_slot_number( \
288 attributes, \
289 &ASSERT_NO_SLOT_NUMBER_slot_number), \
290 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200291 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200293#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294#define ASSERT_NO_SLOT_NUMBER(attributes) \
295 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200296#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
297
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100298/* An overapproximation of the amount of storage needed for a key of the
299 * given type and with the given content. The API doesn't make it easy
300 * to find a good value for the size. The current implementation doesn't
301 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302#define KEY_BITS_FROM_DATA(type, data) \
303 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100304
Darryl Green0c6575a2018-11-07 16:05:30 +0000305typedef enum {
306 IMPORT_KEY = 0,
307 GENERATE_KEY = 1,
308 DERIVE_KEY = 2
309} generate_method;
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100312 DO_NOT_SET_LENGTHS = 0,
313 SET_LENGTHS_BEFORE_NONCE = 1,
314 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100315} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100318 USE_NULL_TAG = 0,
319 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100320} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100321
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100322/*!
323 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 * \param key_type_arg Type of key passed in
325 * \param key_data The encryption / decryption key data
326 * \param alg_arg The type of algorithm used
327 * \param nonce Nonce data
328 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100329 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100330 * feed additional data in to be encrypted /
331 * decrypted. If -1, no chunking.
332 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100333 * \param data_part_len_arg If not -1, the length of chunks to feed
334 * the data in to be encrypted / decrypted. If
335 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100336 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100337 * expected here, this controls whether or not
338 * to set lengths, and in what order with
339 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100342 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100343 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100344 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
347 int alg_arg,
348 data_t *nonce,
349 data_t *additional_data,
350 int ad_part_len_arg,
351 data_t *input_data,
352 int data_part_len_arg,
353 set_lengths_method_t set_lengths_method,
354 data_t *expected_output,
355 int is_encrypt,
356 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100357{
358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
359 psa_key_type_t key_type = key_type_arg;
360 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100361 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100362 unsigned char *output_data = NULL;
363 unsigned char *part_data = NULL;
364 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100365 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100367 size_t output_size = 0;
368 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100369 size_t output_length = 0;
370 size_t key_bits = 0;
371 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100372 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100373 size_t part_length = 0;
374 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100376 size_t ad_part_len = 0;
377 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100378 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100379 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
380 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
381
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100382 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100383 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (is_encrypt) {
388 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
389 } else {
390 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100391 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100392
393 psa_set_key_algorithm(&attributes, alg);
394 psa_set_key_type(&attributes, key_type);
395
396 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
397 &key));
398
399 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
400 key_bits = psa_get_key_bits(&attributes);
401
402 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
403
404 if (is_encrypt) {
405 /* Tag gets written at end of buffer. */
406 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
407 (input_data->len +
408 tag_length));
409 data_true_size = input_data->len;
410 } else {
411 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
412 (input_data->len -
413 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100414
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100415 /* Do not want to attempt to decrypt tag. */
416 data_true_size = input_data->len - tag_length;
417 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 ASSERT_ALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (is_encrypt) {
422 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
423 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
424 } else {
425 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
426 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100427 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 ASSERT_ALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (is_encrypt) {
432 status = psa_aead_encrypt_setup(&operation, key, alg);
433 } else {
434 status = psa_aead_decrypt_setup(&operation, key, alg);
435 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100436
437 /* If the operation is not supported, just skip and not fail in case the
438 * encryption involves a common limitation of cryptography hardwares and
439 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (status == PSA_ERROR_NOT_SUPPORTED) {
441 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
442 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100443 }
444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
448 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
449 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
450 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
451 data_true_size));
452 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
453 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
454 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
457 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100462 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100465 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 part_offset += part_length, part_count++) {
467 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100468 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100470 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100472 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100473 }
474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 PSA_ASSERT(psa_aead_update_ad(&operation,
476 additional_data->x + part_offset,
477 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100478
Paul Elliottd3f82412021-06-16 16:52:21 +0100479 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
483 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100484 }
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100487 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 data_part_len = (size_t) data_part_len_arg;
489 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
490 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 ASSERT_ALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100495 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 part_offset += part_length, part_count++) {
497 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100498 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 } else if ((data_true_size - part_offset) < data_part_len) {
500 part_length = (data_true_size - part_offset);
501 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100502 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100503 }
504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 PSA_ASSERT(psa_aead_update(&operation,
506 (input_data->x + part_offset),
507 part_length, part_data,
508 part_data_size,
509 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (output_data && output_part_length) {
512 memcpy((output_data + output_length), part_data,
513 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 }
515
Paul Elliottd3f82412021-06-16 16:52:21 +0100516 output_length += output_part_length;
517 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
521 data_true_size, output_data,
522 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100523 }
524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (is_encrypt) {
526 PSA_ASSERT(psa_aead_finish(&operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size));
531 } else {
532 PSA_ASSERT(psa_aead_verify(&operation, final_data,
533 final_output_size,
534 &output_part_length,
535 (input_data->x + data_true_size),
536 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100537 }
538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (output_data && output_part_length) {
540 memcpy((output_data + output_length), final_data,
541 output_part_length);
542 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (is_encrypt) {
550 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (output_data && tag_length) {
553 memcpy((output_data + output_length), tag_buffer,
554 tag_length);
555 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100556
557 output_length += tag_length;
558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 TEST_EQUAL(output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
561 input_data->len));
562 TEST_LE_U(output_length,
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
564 } else {
565 TEST_EQUAL(output_length,
566 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
567 input_data->len));
568 TEST_LE_U(output_length,
569 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100570 }
571
Paul Elliottd3f82412021-06-16 16:52:21 +0100572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 ASSERT_COMPARE(expected_output->x, expected_output->len,
574 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100575
Paul Elliottd3f82412021-06-16 16:52:21 +0100576
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100577 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
579exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 psa_destroy_key(key);
581 psa_aead_abort(&operation);
582 mbedtls_free(output_data);
583 mbedtls_free(part_data);
584 mbedtls_free(final_data);
585 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100588}
589
Neil Armstrong4766f992022-02-28 16:23:59 +0100590/*!
591 * \brief Internal Function for MAC multipart tests.
592 * \param key_type_arg Type of key passed in
593 * \param key_data The encryption / decryption key data
594 * \param alg_arg The type of algorithm used
595 * \param input_data Data to encrypt / decrypt
596 * \param data_part_len_arg If not -1, the length of chunks to feed
597 * the data in to be encrypted / decrypted. If
598 * -1, no chunking
599 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000600 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100601 * \param do_zero_parts If non-zero, interleave zero length chunks
602 * with normal length chunks.
603 * \return int Zero on failure, non-zero on success.
604 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100605static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
606 int alg_arg,
607 data_t *input_data,
608 int data_part_len_arg,
609 data_t *expected_output,
610 int is_verify,
611 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100612{
613 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
614 psa_key_type_t key_type = key_type_arg;
615 psa_algorithm_t alg = alg_arg;
616 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
617 unsigned char mac[PSA_MAC_MAX_SIZE];
618 size_t part_offset = 0;
619 size_t part_length = 0;
620 size_t data_part_len = 0;
621 size_t mac_len = 0;
622 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
623 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
624
625 int test_ok = 0;
626 size_t part_count = 0;
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (is_verify) {
631 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
632 } else {
633 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
634 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 psa_set_key_algorithm(&attributes, alg);
637 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
640 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (is_verify) {
643 status = psa_mac_verify_setup(&operation, key, alg);
644 } else {
645 status = psa_mac_sign_setup(&operation, key, alg);
646 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100651 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100655 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 part_offset += part_length, part_count++) {
657 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100658 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 } else if ((input_data->len - part_offset) < data_part_len) {
660 part_length = (input_data->len - part_offset);
661 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100662 part_length = data_part_len;
663 }
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 PSA_ASSERT(psa_mac_update(&operation,
666 (input_data->x + part_offset),
667 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100668 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100670 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
672 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100673 }
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (is_verify) {
676 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
677 expected_output->len));
678 } else {
679 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
680 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 ASSERT_COMPARE(expected_output->x, expected_output->len,
683 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100684 }
685
686 test_ok = 1;
687
688exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 psa_destroy_key(key);
690 psa_mac_abort(&operation);
691 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100694}
695
Neil Armstrong75673ab2022-06-15 17:39:01 +0200696#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100697static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
698 psa_pake_operation_t *server,
699 psa_pake_operation_t *client,
700 int client_input_first,
701 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200702{
703 unsigned char *buffer0 = NULL, *buffer1 = NULL;
704 size_t buffer_length = (
705 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
706 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
707 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200708 /* The output should be exactly this size according to the spec */
709 const size_t expected_size_key_share =
710 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
711 /* The output should be exactly this size according to the spec */
712 const size_t expected_size_zk_public =
713 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
714 /* The output can be smaller: the spec allows stripping leading zeroes */
715 const size_t max_expected_size_zk_proof =
716 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200717 size_t buffer0_off = 0;
718 size_t buffer1_off = 0;
719 size_t s_g1_len, s_g2_len, s_a_len;
720 size_t s_g1_off, s_g2_off, s_a_off;
721 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
722 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
723 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
724 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
725 size_t c_g1_len, c_g2_len, c_a_len;
726 size_t c_g1_off, c_g2_off, c_a_off;
727 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
728 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
729 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
730 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
731 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200732 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 ASSERT_ALLOC(buffer0, buffer_length);
735 ASSERT_ALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200738 case 1:
739 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
741 buffer0 + buffer0_off,
742 512 - buffer0_off, &s_g1_len));
743 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200744 s_g1_off = buffer0_off;
745 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
747 buffer0 + buffer0_off,
748 512 - buffer0_off, &s_x1_pk_len));
749 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200750 s_x1_pk_off = buffer0_off;
751 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
753 buffer0 + buffer0_off,
754 512 - buffer0_off, &s_x1_pr_len));
755 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200756 s_x1_pr_off = buffer0_off;
757 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
759 buffer0 + buffer0_off,
760 512 - buffer0_off, &s_g2_len));
761 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200762 s_g2_off = buffer0_off;
763 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
765 buffer0 + buffer0_off,
766 512 - buffer0_off, &s_x2_pk_len));
767 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200768 s_x2_pk_off = buffer0_off;
769 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
771 buffer0 + buffer0_off,
772 512 - buffer0_off, &s_x2_pr_len));
773 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200774 s_x2_pr_off = buffer0_off;
775 buffer0_off += s_x2_pr_len;
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500778 buffer0[s_x1_pr_off + 8] ^= 1;
779 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200780 expected_status = PSA_ERROR_DATA_INVALID;
781 }
782
Neil Armstrong51009d72022-09-05 17:59:54 +0200783 /*
784 * When injecting errors in inputs, the implementation is
785 * free to detect it right away of with a delay.
786 * This permits delaying the error until the end of the input
787 * sequence, if no error appears then, this will be treated
788 * as an error.
789 */
790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200792 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
794 buffer0 + s_g1_off, s_g1_len);
795 if (inject_error == 1 && status != PSA_SUCCESS) {
796 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200797 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 } else {
799 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200800 }
801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
803 buffer0 + s_x1_pk_off,
804 s_x1_pk_len);
805 if (inject_error == 1 && status != PSA_SUCCESS) {
806 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200807 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 } else {
809 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200810 }
811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
813 buffer0 + s_x1_pr_off,
814 s_x1_pr_len);
815 if (inject_error == 1 && status != PSA_SUCCESS) {
816 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200817 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 } else {
819 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200820 }
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
823 buffer0 + s_g2_off,
824 s_g2_len);
825 if (inject_error == 1 && status != PSA_SUCCESS) {
826 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200827 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 } else {
829 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200830 }
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
833 buffer0 + s_x2_pk_off,
834 s_x2_pk_len);
835 if (inject_error == 1 && status != PSA_SUCCESS) {
836 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200837 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 } else {
839 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200840 }
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
843 buffer0 + s_x2_pr_off,
844 s_x2_pr_len);
845 if (inject_error == 1 && status != PSA_SUCCESS) {
846 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200847 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 } else {
849 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200850 }
851
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200852 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (inject_error == 1) {
854 TEST_ASSERT(
855 !"One of the last psa_pake_input() calls should have returned the expected error.");
856 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200857 }
858
859 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
861 buffer1 + buffer1_off,
862 512 - buffer1_off, &c_g1_len));
863 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200864 c_g1_off = buffer1_off;
865 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
867 buffer1 + buffer1_off,
868 512 - buffer1_off, &c_x1_pk_len));
869 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200870 c_x1_pk_off = buffer1_off;
871 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
873 buffer1 + buffer1_off,
874 512 - buffer1_off, &c_x1_pr_len));
875 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200876 c_x1_pr_off = buffer1_off;
877 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
879 buffer1 + buffer1_off,
880 512 - buffer1_off, &c_g2_len));
881 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200882 c_g2_off = buffer1_off;
883 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
885 buffer1 + buffer1_off,
886 512 - buffer1_off, &c_x2_pk_len));
887 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200888 c_x2_pk_off = buffer1_off;
889 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
891 buffer1 + buffer1_off,
892 512 - buffer1_off, &c_x2_pr_len));
893 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200894 c_x2_pr_off = buffer1_off;
895 buffer1_off += c_x2_pr_len;
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200898 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
900 buffer0 + s_g1_off, s_g1_len);
901 if (inject_error == 1 && status != PSA_SUCCESS) {
902 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200903 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 } else {
905 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200906 }
907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
909 buffer0 + s_x1_pk_off,
910 s_x1_pk_len);
911 if (inject_error == 1 && status != PSA_SUCCESS) {
912 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200913 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 } else {
915 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200916 }
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
919 buffer0 + s_x1_pr_off,
920 s_x1_pr_len);
921 if (inject_error == 1 && status != PSA_SUCCESS) {
922 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200923 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 } else {
925 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200926 }
927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
929 buffer0 + s_g2_off,
930 s_g2_len);
931 if (inject_error == 1 && status != PSA_SUCCESS) {
932 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200933 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 } else {
935 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200936 }
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
939 buffer0 + s_x2_pk_off,
940 s_x2_pk_len);
941 if (inject_error == 1 && status != PSA_SUCCESS) {
942 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200943 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 } else {
945 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200946 }
947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
949 buffer0 + s_x2_pr_off,
950 s_x2_pr_len);
951 if (inject_error == 1 && status != PSA_SUCCESS) {
952 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200953 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 } else {
955 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200956 }
957
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200958 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (inject_error == 1) {
960 TEST_ASSERT(
961 !"One of the last psa_pake_input() calls should have returned the expected error.");
962 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200963 }
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500966 buffer1[c_x1_pr_off + 12] ^= 1;
967 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200968 expected_status = PSA_ERROR_DATA_INVALID;
969 }
970
971 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
973 buffer1 + c_g1_off, c_g1_len);
974 if (inject_error == 2 && status != PSA_SUCCESS) {
975 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200976 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 } else {
978 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200979 }
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
982 buffer1 + c_x1_pk_off, c_x1_pk_len);
983 if (inject_error == 2 && status != PSA_SUCCESS) {
984 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200985 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 } else {
987 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200988 }
989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
991 buffer1 + c_x1_pr_off, c_x1_pr_len);
992 if (inject_error == 2 && status != PSA_SUCCESS) {
993 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200994 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 } else {
996 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200997 }
998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1000 buffer1 + c_g2_off, c_g2_len);
1001 if (inject_error == 2 && status != PSA_SUCCESS) {
1002 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001003 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 } else {
1005 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001006 }
1007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1009 buffer1 + c_x2_pk_off, c_x2_pk_len);
1010 if (inject_error == 2 && status != PSA_SUCCESS) {
1011 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001012 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 } else {
1014 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001015 }
1016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1018 buffer1 + c_x2_pr_off, c_x2_pr_len);
1019 if (inject_error == 2 && status != PSA_SUCCESS) {
1020 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001021 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 } else {
1023 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001024 }
1025
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001026 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (inject_error == 2) {
1028 TEST_ASSERT(
1029 !"One of the last psa_pake_input() calls should have returned the expected error.");
1030 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001031
1032 break;
1033
1034 case 2:
1035 /* Server second round Output */
1036 buffer0_off = 0;
1037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1039 buffer0 + buffer0_off,
1040 512 - buffer0_off, &s_a_len));
1041 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001042 s_a_off = buffer0_off;
1043 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1045 buffer0 + buffer0_off,
1046 512 - buffer0_off, &s_x2s_pk_len));
1047 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001048 s_x2s_pk_off = buffer0_off;
1049 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1051 buffer0 + buffer0_off,
1052 512 - buffer0_off, &s_x2s_pr_len));
1053 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001054 s_x2s_pr_off = buffer0_off;
1055 buffer0_off += s_x2s_pr_len;
1056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001058 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001059 expected_status = PSA_ERROR_DATA_INVALID;
1060 }
1061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001063 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1065 buffer0 + s_a_off, s_a_len);
1066 if (inject_error == 3 && status != PSA_SUCCESS) {
1067 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001068 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 } else {
1070 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001071 }
1072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1074 buffer0 + s_x2s_pk_off,
1075 s_x2s_pk_len);
1076 if (inject_error == 3 && status != PSA_SUCCESS) {
1077 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001078 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 } else {
1080 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001081 }
1082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1084 buffer0 + s_x2s_pr_off,
1085 s_x2s_pr_len);
1086 if (inject_error == 3 && status != PSA_SUCCESS) {
1087 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001088 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 } else {
1090 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001091 }
1092
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001093 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if (inject_error == 3) {
1095 TEST_ASSERT(
1096 !"One of the last psa_pake_input() calls should have returned the expected error.");
1097 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001098 }
1099
1100 /* Client second round Output */
1101 buffer1_off = 0;
1102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1104 buffer1 + buffer1_off,
1105 512 - buffer1_off, &c_a_len));
1106 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001107 c_a_off = buffer1_off;
1108 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1110 buffer1 + buffer1_off,
1111 512 - buffer1_off, &c_x2s_pk_len));
1112 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001113 c_x2s_pk_off = buffer1_off;
1114 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1116 buffer1 + buffer1_off,
1117 512 - buffer1_off, &c_x2s_pr_len));
1118 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001119 c_x2s_pr_off = buffer1_off;
1120 buffer1_off += c_x2s_pr_len;
1121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001123 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1125 buffer0 + s_a_off, s_a_len);
1126 if (inject_error == 3 && status != PSA_SUCCESS) {
1127 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001128 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 } else {
1130 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001131 }
1132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1134 buffer0 + s_x2s_pk_off,
1135 s_x2s_pk_len);
1136 if (inject_error == 3 && status != PSA_SUCCESS) {
1137 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001138 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 } else {
1140 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001141 }
1142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1144 buffer0 + s_x2s_pr_off,
1145 s_x2s_pr_len);
1146 if (inject_error == 3 && status != PSA_SUCCESS) {
1147 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001148 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 } else {
1150 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001151 }
1152
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001153 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (inject_error == 3) {
1155 TEST_ASSERT(
1156 !"One of the last psa_pake_input() calls should have returned the expected error.");
1157 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001158 }
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001161 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001162 expected_status = PSA_ERROR_DATA_INVALID;
1163 }
1164
1165 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1167 buffer1 + c_a_off, c_a_len);
1168 if (inject_error == 4 && status != PSA_SUCCESS) {
1169 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001170 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 } else {
1172 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001173 }
1174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1176 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1177 if (inject_error == 4 && status != PSA_SUCCESS) {
1178 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001179 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 } else {
1181 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001182 }
1183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1185 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1186 if (inject_error == 4 && status != PSA_SUCCESS) {
1187 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001188 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 } else {
1190 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001191 }
1192
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001193 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (inject_error == 4) {
1195 TEST_ASSERT(
1196 !"One of the last psa_pake_input() calls should have returned the expected error.");
1197 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001198
1199 break;
1200
1201 }
1202
Neil Armstrongf983caf2022-06-15 15:27:48 +02001203exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 mbedtls_free(buffer0);
1205 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001206}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001207#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001210 INJECT_ERR_NONE = 0,
1211 INJECT_ERR_UNINITIALIZED_ACCESS,
1212 INJECT_ERR_DUPLICATE_SETUP,
1213 INJECT_ERR_INVALID_USER,
1214 INJECT_ERR_INVALID_PEER,
1215 INJECT_ERR_SET_USER,
1216 INJECT_ERR_SET_PEER,
1217 INJECT_EMPTY_IO_BUFFER,
1218 INJECT_UNKNOWN_STEP,
1219 INJECT_INVALID_FIRST_STEP,
1220 INJECT_WRONG_BUFFER_SIZE,
1221 INJECT_VALID_OPERATION_AFTER_FAILURE,
1222 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1223 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1224} ecjpake_injected_failure_t;
1225
Paul Elliott01885fa2023-02-09 12:07:30 +00001226#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001227
Paul Elliott6f600372023-02-06 18:41:05 +00001228static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1229 psa_status_t expected_status,
1230 size_t *min_completes,
1231 size_t *max_completes)
1232{
1233
1234 /* This is slightly contrived, but we only really know that with a minimum
1235 value of max_ops that a successful operation should take more than one op
1236 to complete, and likewise that with a max_ops of
1237 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1238 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001239
Paul Elliott6f600372023-02-06 18:41:05 +00001240 if (expected_status == PSA_SUCCESS) {
1241 *min_completes = 2;
1242 } else {
1243 *min_completes = 1;
1244 }
1245
1246 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1247 } else {
1248 *min_completes = 1;
1249 *max_completes = 1;
1250 }
1251}
Paul Elliott01885fa2023-02-09 12:07:30 +00001252#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001253
Gilles Peskinee59236f2018-01-27 23:32:46 +01001254/* END_HEADER */
1255
1256/* BEGIN_DEPENDENCIES
1257 * depends_on:MBEDTLS_PSA_CRYPTO_C
1258 * END_DEPENDENCIES
1259 */
1260
1261/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001262void psa_can_do_hash()
1263{
1264 /* We can't test that this is specific to drivers until partial init has
1265 * been implemented, but we can at least test before/after full init. */
1266 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1267 PSA_INIT();
1268 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1269 PSA_DONE();
1270}
1271/* END_CASE */
1272
1273/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001274void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001275{
1276 size_t max_truncated_mac_size =
1277 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1278
1279 /* Check that the length for a truncated MAC always fits in the algorithm
1280 * encoding. The shifted mask is the maximum truncated value. The
1281 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001283}
1284/* END_CASE */
1285
1286/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287void import_with_policy(int type_arg,
1288 int usage_arg, int alg_arg,
1289 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001290{
1291 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1292 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001293 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001294 psa_key_type_t type = type_arg;
1295 psa_key_usage_t usage = usage_arg;
1296 psa_algorithm_t alg = alg_arg;
1297 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001299 psa_status_t status;
1300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 psa_set_key_type(&attributes, type);
1304 psa_set_key_usage_flags(&attributes, usage);
1305 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 status = psa_import_key(&attributes,
1308 key_material, sizeof(key_material),
1309 &key);
1310 TEST_EQUAL(status, expected_status);
1311 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001312 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1316 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1317 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1318 mbedtls_test_update_key_usage_flags(usage));
1319 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1320 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 PSA_ASSERT(psa_destroy_key(key));
1323 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001324
1325exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001326 /*
1327 * Key attributes may have been returned by psa_get_key_attributes()
1328 * thus reset them as required.
1329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 psa_destroy_key(key);
1333 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001334}
1335/* END_CASE */
1336
1337/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338void import_with_data(data_t *data, int type_arg,
1339 int attr_bits_arg,
1340 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001341{
1342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1343 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001345 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001346 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001347 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001348 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 psa_set_key_type(&attributes, type);
1353 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 status = psa_import_key(&attributes, data->x, data->len, &key);
1356 TEST_EQUAL(status, expected_status);
1357 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001358 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1362 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1363 if (attr_bits != 0) {
1364 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1365 }
1366 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 PSA_ASSERT(psa_destroy_key(key));
1369 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370
1371exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001372 /*
1373 * Key attributes may have been returned by psa_get_key_attributes()
1374 * thus reset them as required.
1375 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 psa_destroy_key(key);
1379 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001380}
1381/* END_CASE */
1382
1383/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001384/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385void import_large_key(int type_arg, int byte_size_arg,
1386 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001387{
1388 psa_key_type_t type = type_arg;
1389 size_t byte_size = byte_size_arg;
1390 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1391 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001392 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001393 psa_status_t status;
1394 uint8_t *buffer = NULL;
1395 size_t buffer_size = byte_size + 1;
1396 size_t n;
1397
Steven Cooreman69967ce2021-01-18 18:01:08 +01001398 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001399 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1401 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001404
1405 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1407 psa_set_key_type(&attributes, type);
1408 status = psa_import_key(&attributes, buffer, byte_size, &key);
1409 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1410 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 if (status == PSA_SUCCESS) {
1413 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1414 TEST_EQUAL(psa_get_key_type(&attributes), type);
1415 TEST_EQUAL(psa_get_key_bits(&attributes),
1416 PSA_BYTES_TO_BITS(byte_size));
1417 ASSERT_NO_SLOT_NUMBER(&attributes);
1418 memset(buffer, 0, byte_size + 1);
1419 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1420 for (n = 0; n < byte_size; n++) {
1421 TEST_EQUAL(buffer[n], 'K');
1422 }
1423 for (n = byte_size; n < buffer_size; n++) {
1424 TEST_EQUAL(buffer[n], 0);
1425 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001426 }
1427
1428exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001429 /*
1430 * Key attributes may have been returned by psa_get_key_attributes()
1431 * thus reset them as required.
1432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 psa_destroy_key(key);
1436 PSA_DONE();
1437 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001438}
1439/* END_CASE */
1440
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001441/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001442/* Import an RSA key with a valid structure (but not valid numbers
1443 * inside, beyond having sensible size and parity). This is expected to
1444 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001445void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001446{
Ronald Cron5425a212020-08-04 14:58:35 +02001447 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001448 size_t bits = bits_arg;
1449 psa_status_t expected_status = expected_status_arg;
1450 psa_status_t status;
1451 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001452 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001453 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001455 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001456 unsigned char *p;
1457 int ret;
1458 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001459 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 PSA_ASSERT(psa_crypto_init());
1462 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1465 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001466 length = ret;
1467
1468 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 psa_set_key_type(&attributes, type);
1470 status = psa_import_key(&attributes, p, length, &key);
1471 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (status == PSA_SUCCESS) {
1474 PSA_ASSERT(psa_destroy_key(key));
1475 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001476
1477exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 mbedtls_free(buffer);
1479 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001480}
1481/* END_CASE */
1482
1483/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484void import_export(data_t *data,
1485 int type_arg,
1486 int usage_arg, int alg_arg,
1487 int lifetime_arg,
1488 int expected_bits,
1489 int export_size_delta,
1490 int expected_export_status_arg,
1491 /*whether reexport must give the original input exactly*/
1492 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001493{
Ronald Cron5425a212020-08-04 14:58:35 +02001494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001495 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001496 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001497 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001498 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301499 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500 unsigned char *exported = NULL;
1501 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001503 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001504 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001505 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001506 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
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
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522
1523 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1525 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1526 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1527 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001528
1529 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 status = psa_export_key(key, exported, export_size, &exported_length);
1531 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001532
1533 /* The exported length must be set by psa_export_key() to a value between 0
1534 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1536 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1537 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1540 export_size - exported_length));
1541 if (status != PSA_SUCCESS) {
1542 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001543 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001544 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545
Gilles Peskineea38a922021-02-13 00:05:16 +01001546 /* Run sanity checks on the exported key. For non-canonical inputs,
1547 * this validates the canonical representations. For canonical inputs,
1548 * this doesn't directly validate the implementation, but it still helps
1549 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (!psa_key_lifetime_is_external(lifetime)) {
1551 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301552 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 }
Archana4d7ae1d2021-07-07 02:50:22 +05301554 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (canonical_input) {
1557 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1558 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001559 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1561 &key2));
1562 PSA_ASSERT(psa_export_key(key2,
1563 reexported,
1564 export_size,
1565 &reexported_length));
1566 ASSERT_COMPARE(exported, exported_length,
1567 reexported, reexported_length);
1568 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001569 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 TEST_LE_U(exported_length,
1571 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1572 psa_get_key_bits(&got_attributes)));
1573 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001574
1575destroy:
1576 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 PSA_ASSERT(psa_destroy_key(key));
1578 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001579
1580exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001581 /*
1582 * Key attributes may have been returned by psa_get_key_attributes()
1583 * thus reset them as required.
1584 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 psa_reset_key_attributes(&got_attributes);
1586 psa_destroy_key(key);
1587 mbedtls_free(exported);
1588 mbedtls_free(reexported);
1589 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001590}
1591/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001592
Moran Pekerf709f4a2018-06-06 17:26:04 +03001593/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001594void import_export_public_key(data_t *data,
1595 int type_arg, // key pair or public key
1596 int alg_arg,
1597 int lifetime_arg,
1598 int export_size_delta,
1599 int expected_export_status_arg,
1600 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001601{
Ronald Cron5425a212020-08-04 14:58:35 +02001602 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001603 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001604 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001605 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001606 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301607 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001608 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001609 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001610 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 psa_set_key_lifetime(&attributes, lifetime);
1616 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1617 psa_set_key_algorithm(&attributes, alg);
1618 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001619
1620 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001622
Gilles Peskine49c25912018-10-29 15:15:31 +01001623 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 ASSERT_ALLOC(exported, export_size);
1625 status = psa_export_public_key(key,
1626 exported, export_size,
1627 &exported_length);
1628 TEST_EQUAL(status, expected_export_status);
1629 if (status == PSA_SUCCESS) {
1630 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001631 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1633 bits = psa_get_key_bits(&attributes);
1634 TEST_LE_U(expected_public_key->len,
1635 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1636 TEST_LE_U(expected_public_key->len,
1637 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1638 TEST_LE_U(expected_public_key->len,
1639 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1640 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1641 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001642 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001643exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001644 /*
1645 * Key attributes may have been returned by psa_get_key_attributes()
1646 * thus reset them as required.
1647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 mbedtls_free(exported);
1651 psa_destroy_key(key);
1652 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001653}
1654/* END_CASE */
1655
Gilles Peskine20035e32018-02-03 22:44:14 +01001656/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001657void import_and_exercise_key(data_t *data,
1658 int type_arg,
1659 int bits_arg,
1660 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001661{
Ronald Cron5425a212020-08-04 14:58:35 +02001662 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001663 psa_key_type_t type = type_arg;
1664 size_t bits = bits_arg;
1665 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001668 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 psa_set_key_usage_flags(&attributes, usage);
1673 psa_set_key_algorithm(&attributes, alg);
1674 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001675
1676 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001678
1679 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1681 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1682 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001683
1684 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001686 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 PSA_ASSERT(psa_destroy_key(key));
1690 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001691
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001692exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001693 /*
1694 * Key attributes may have been returned by psa_get_key_attributes()
1695 * thus reset them as required.
1696 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 psa_reset_key_attributes(&attributes);
1700 psa_destroy_key(key);
1701 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001702}
1703/* END_CASE */
1704
1705/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001706void effective_key_attributes(int type_arg, int expected_type_arg,
1707 int bits_arg, int expected_bits_arg,
1708 int usage_arg, int expected_usage_arg,
1709 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001710{
Ronald Cron5425a212020-08-04 14:58:35 +02001711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001712 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001713 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001714 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001715 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001716 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001717 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001718 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001719 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 psa_set_key_usage_flags(&attributes, usage);
1725 psa_set_key_algorithm(&attributes, alg);
1726 psa_set_key_type(&attributes, key_type);
1727 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 PSA_ASSERT(psa_generate_key(&attributes, &key));
1730 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1733 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1734 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1735 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1736 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001737
1738exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001739 /*
1740 * Key attributes may have been returned by psa_get_key_attributes()
1741 * thus reset them as required.
1742 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 psa_destroy_key(key);
1746 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001747}
1748/* END_CASE */
1749
1750/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001751void check_key_policy(int type_arg, int bits_arg,
1752 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001753{
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1755 usage_arg,
1756 mbedtls_test_update_key_usage_flags(usage_arg),
1757 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001758 goto exit;
1759}
1760/* END_CASE */
1761
1762/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001763void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001764{
1765 /* Test each valid way of initializing the object, except for `= {0}`, as
1766 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1767 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001768 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001770 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1771 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1776 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1777 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 TEST_EQUAL(psa_get_key_type(&func), 0);
1780 TEST_EQUAL(psa_get_key_type(&init), 0);
1781 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 TEST_EQUAL(psa_get_key_bits(&func), 0);
1784 TEST_EQUAL(psa_get_key_bits(&init), 0);
1785 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1788 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1789 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1792 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1793 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001794}
1795/* END_CASE */
1796
1797/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001798void mac_key_policy(int policy_usage_arg,
1799 int policy_alg_arg,
1800 int key_type_arg,
1801 data_t *key_data,
1802 int exercise_alg_arg,
1803 int expected_status_sign_arg,
1804 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001805{
Ronald Cron5425a212020-08-04 14:58:35 +02001806 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001807 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001808 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001809 psa_key_type_t key_type = key_type_arg;
1810 psa_algorithm_t policy_alg = policy_alg_arg;
1811 psa_algorithm_t exercise_alg = exercise_alg_arg;
1812 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001813 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001814 psa_status_t expected_status_sign = expected_status_sign_arg;
1815 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001816 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 psa_set_key_usage_flags(&attributes, policy_usage);
1821 psa_set_key_algorithm(&attributes, policy_alg);
1822 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1825 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1828 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1831 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001832
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001833 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001835 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1837 input, 128,
1838 mac, PSA_MAC_MAX_SIZE, &mac_len),
1839 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001840
Neil Armstrong3af9b972022-02-07 12:20:21 +01001841 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 PSA_ASSERT(psa_mac_abort(&operation));
1843 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1844 if (status == PSA_SUCCESS) {
1845 status = psa_mac_update(&operation, input, 128);
1846 if (status == PSA_SUCCESS) {
1847 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1848 &mac_len),
1849 expected_status_sign);
1850 } else {
1851 TEST_EQUAL(status, expected_status_sign);
1852 }
1853 } else {
1854 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001855 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001857
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001858 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 status = psa_mac_verify(key, exercise_alg, input, 128,
1860 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1863 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1864 } else {
1865 TEST_EQUAL(status, expected_status_verify);
1866 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001867
Neil Armstrong3af9b972022-02-07 12:20:21 +01001868 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1870 if (status == PSA_SUCCESS) {
1871 status = psa_mac_update(&operation, input, 128);
1872 if (status == PSA_SUCCESS) {
1873 status = psa_mac_verify_finish(&operation, mac, mac_len);
1874 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1875 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1876 } else {
1877 TEST_EQUAL(status, expected_status_verify);
1878 }
1879 } else {
1880 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001881 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 } else {
1883 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001884 }
1885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 memset(mac, 0, sizeof(mac));
1889 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1890 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001891
1892exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 psa_mac_abort(&operation);
1894 psa_destroy_key(key);
1895 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001896}
1897/* END_CASE */
1898
1899/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001900void cipher_key_policy(int policy_usage_arg,
1901 int policy_alg,
1902 int key_type,
1903 data_t *key_data,
1904 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001905{
Ronald Cron5425a212020-08-04 14:58:35 +02001906 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001908 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001909 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001910 size_t output_buffer_size = 0;
1911 size_t input_buffer_size = 0;
1912 size_t output_length = 0;
1913 uint8_t *output = NULL;
1914 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001915 psa_status_t status;
1916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1918 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1919 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 ASSERT_ALLOC(input, input_buffer_size);
1922 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 psa_set_key_usage_flags(&attributes, policy_usage);
1927 psa_set_key_algorithm(&attributes, policy_alg);
1928 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1931 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001932
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001933 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 TEST_EQUAL(policy_usage,
1935 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001936
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001937 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1939 output, output_buffer_size,
1940 &output_length);
1941 if (policy_alg == exercise_alg &&
1942 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1943 PSA_ASSERT(status);
1944 } else {
1945 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1946 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001947
1948 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1950 if (policy_alg == exercise_alg &&
1951 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1952 PSA_ASSERT(status);
1953 } else {
1954 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1955 }
1956 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001958 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1960 input, input_buffer_size,
1961 &output_length);
1962 if (policy_alg == exercise_alg &&
1963 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1964 PSA_ASSERT(status);
1965 } else {
1966 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1967 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001968
1969 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1971 if (policy_alg == exercise_alg &&
1972 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1973 PSA_ASSERT(status);
1974 } else {
1975 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1976 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977
1978exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 psa_cipher_abort(&operation);
1980 mbedtls_free(input);
1981 mbedtls_free(output);
1982 psa_destroy_key(key);
1983 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984}
1985/* END_CASE */
1986
1987/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001988void aead_key_policy(int policy_usage_arg,
1989 int policy_alg,
1990 int key_type,
1991 data_t *key_data,
1992 int nonce_length_arg,
1993 int tag_length_arg,
1994 int exercise_alg,
1995 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001996{
Ronald Cron5425a212020-08-04 14:58:35 +02001997 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001998 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001999 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002000 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002001 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002002 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004 size_t nonce_length = nonce_length_arg;
2005 unsigned char tag[16];
2006 size_t tag_length = tag_length_arg;
2007 size_t output_length;
2008
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 TEST_LE_U(nonce_length, sizeof(nonce));
2010 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 psa_set_key_usage_flags(&attributes, policy_usage);
2015 psa_set_key_algorithm(&attributes, policy_alg);
2016 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2019 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002021 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 TEST_EQUAL(policy_usage,
2023 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002024
Neil Armstrong752d8112022-02-07 14:51:11 +01002025 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 status = psa_aead_encrypt(key, exercise_alg,
2027 nonce, nonce_length,
2028 NULL, 0,
2029 NULL, 0,
2030 tag, tag_length,
2031 &output_length);
2032 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2033 TEST_EQUAL(status, expected_status);
2034 } else {
2035 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2036 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037
Neil Armstrong752d8112022-02-07 14:51:11 +01002038 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2040 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2041 TEST_EQUAL(status, expected_status);
2042 } else {
2043 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2044 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002045
2046 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 memset(tag, 0, sizeof(tag));
2048 status = psa_aead_decrypt(key, exercise_alg,
2049 nonce, nonce_length,
2050 NULL, 0,
2051 tag, tag_length,
2052 NULL, 0,
2053 &output_length);
2054 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2055 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2056 } else if (expected_status == PSA_SUCCESS) {
2057 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2058 } else {
2059 TEST_EQUAL(status, expected_status);
2060 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061
Neil Armstrong752d8112022-02-07 14:51:11 +01002062 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 PSA_ASSERT(psa_aead_abort(&operation));
2064 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2065 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2066 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2067 } else {
2068 TEST_EQUAL(status, expected_status);
2069 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002070
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002071exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 PSA_ASSERT(psa_aead_abort(&operation));
2073 psa_destroy_key(key);
2074 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075}
2076/* END_CASE */
2077
2078/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079void asymmetric_encryption_key_policy(int policy_usage_arg,
2080 int policy_alg,
2081 int key_type,
2082 data_t *key_data,
2083 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084{
Ronald Cron5425a212020-08-04 14:58:35 +02002085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002086 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002087 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088 psa_status_t status;
2089 size_t key_bits;
2090 size_t buffer_length;
2091 unsigned char *buffer = NULL;
2092 size_t output_length;
2093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 psa_set_key_usage_flags(&attributes, policy_usage);
2097 psa_set_key_algorithm(&attributes, policy_alg);
2098 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2101 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002102
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002103 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 TEST_EQUAL(policy_usage,
2105 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2108 key_bits = psa_get_key_bits(&attributes);
2109 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2110 exercise_alg);
2111 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 status = psa_asymmetric_encrypt(key, exercise_alg,
2114 NULL, 0,
2115 NULL, 0,
2116 buffer, buffer_length,
2117 &output_length);
2118 if (policy_alg == exercise_alg &&
2119 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2120 PSA_ASSERT(status);
2121 } else {
2122 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2123 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (buffer_length != 0) {
2126 memset(buffer, 0, buffer_length);
2127 }
2128 status = psa_asymmetric_decrypt(key, exercise_alg,
2129 buffer, buffer_length,
2130 NULL, 0,
2131 buffer, buffer_length,
2132 &output_length);
2133 if (policy_alg == exercise_alg &&
2134 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2135 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2136 } else {
2137 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2138 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002139
2140exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002141 /*
2142 * Key attributes may have been returned by psa_get_key_attributes()
2143 * thus reset them as required.
2144 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 psa_destroy_key(key);
2148 PSA_DONE();
2149 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002150}
2151/* END_CASE */
2152
2153/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154void asymmetric_signature_key_policy(int policy_usage_arg,
2155 int policy_alg,
2156 int key_type,
2157 data_t *key_data,
2158 int exercise_alg,
2159 int payload_length_arg,
2160 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002161{
Ronald Cron5425a212020-08-04 14:58:35 +02002162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002164 psa_key_usage_t policy_usage = policy_usage_arg;
2165 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002166 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002168 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2169 * compatible with the policy and `payload_length_arg` is supposed to be
2170 * a valid input length to sign. If `payload_length_arg <= 0`,
2171 * `exercise_alg` is supposed to be forbidden by the policy. */
2172 int compatible_alg = payload_length_arg > 0;
2173 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002175 size_t signature_length;
2176
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002177 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002178 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 TEST_EQUAL(expected_usage,
2180 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 psa_set_key_usage_flags(&attributes, policy_usage);
2185 psa_set_key_algorithm(&attributes, policy_alg);
2186 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2189 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 status = psa_sign_hash(key, exercise_alg,
2194 payload, payload_length,
2195 signature, sizeof(signature),
2196 &signature_length);
2197 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2198 PSA_ASSERT(status);
2199 } else {
2200 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2201 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 memset(signature, 0, sizeof(signature));
2204 status = psa_verify_hash(key, exercise_alg,
2205 payload, payload_length,
2206 signature, sizeof(signature));
2207 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2208 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2209 } else {
2210 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2211 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2214 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2215 status = psa_sign_message(key, exercise_alg,
2216 payload, payload_length,
2217 signature, sizeof(signature),
2218 &signature_length);
2219 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2220 PSA_ASSERT(status);
2221 } else {
2222 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2223 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 memset(signature, 0, sizeof(signature));
2226 status = psa_verify_message(key, exercise_alg,
2227 payload, payload_length,
2228 signature, sizeof(signature));
2229 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2230 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2231 } else {
2232 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2233 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002234 }
2235
Gilles Peskined5b33222018-06-18 22:20:03 +02002236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 psa_destroy_key(key);
2238 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002239}
2240/* END_CASE */
2241
Janos Follathba3fab92019-06-11 14:50:16 +01002242/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002243void derive_key_policy(int policy_usage,
2244 int policy_alg,
2245 int key_type,
2246 data_t *key_data,
2247 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002248{
Ronald Cron5425a212020-08-04 14:58:35 +02002249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002251 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002252 psa_status_t status;
2253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 psa_set_key_usage_flags(&attributes, policy_usage);
2257 psa_set_key_algorithm(&attributes, policy_alg);
2258 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2261 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2266 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2267 PSA_ASSERT(psa_key_derivation_input_bytes(
2268 &operation,
2269 PSA_KEY_DERIVATION_INPUT_SEED,
2270 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002271 }
Janos Follathba3fab92019-06-11 14:50:16 +01002272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 status = psa_key_derivation_input_key(&operation,
2274 PSA_KEY_DERIVATION_INPUT_SECRET,
2275 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (policy_alg == exercise_alg &&
2278 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2279 PSA_ASSERT(status);
2280 } else {
2281 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2282 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002283
2284exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 psa_key_derivation_abort(&operation);
2286 psa_destroy_key(key);
2287 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002288}
2289/* END_CASE */
2290
2291/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002292void agreement_key_policy(int policy_usage,
2293 int policy_alg,
2294 int key_type_arg,
2295 data_t *key_data,
2296 int exercise_alg,
2297 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002298{
Ronald Cron5425a212020-08-04 14:58:35 +02002299 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002301 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002302 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002303 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002304 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 psa_set_key_usage_flags(&attributes, policy_usage);
2309 psa_set_key_algorithm(&attributes, policy_alg);
2310 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2313 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2316 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002319
2320exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 psa_key_derivation_abort(&operation);
2322 psa_destroy_key(key);
2323 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002324}
2325/* END_CASE */
2326
2327/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328void key_policy_alg2(int key_type_arg, data_t *key_data,
2329 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002330{
Ronald Cron5425a212020-08-04 14:58:35 +02002331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002332 psa_key_type_t key_type = key_type_arg;
2333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2334 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2335 psa_key_usage_t usage = usage_arg;
2336 psa_algorithm_t alg = alg_arg;
2337 psa_algorithm_t alg2 = alg2_arg;
2338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 psa_set_key_usage_flags(&attributes, usage);
2342 psa_set_key_algorithm(&attributes, alg);
2343 psa_set_key_enrollment_algorithm(&attributes, alg2);
2344 psa_set_key_type(&attributes, key_type);
2345 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2346 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002347
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002348 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 usage = mbedtls_test_update_key_usage_flags(usage);
2350 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2351 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2352 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2353 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002356 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 }
2358 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002359 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002361
2362exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002363 /*
2364 * Key attributes may have been returned by psa_get_key_attributes()
2365 * thus reset them as required.
2366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 psa_destroy_key(key);
2370 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002371}
2372/* END_CASE */
2373
2374/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002375void raw_agreement_key_policy(int policy_usage,
2376 int policy_alg,
2377 int key_type_arg,
2378 data_t *key_data,
2379 int exercise_alg,
2380 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002381{
Ronald Cron5425a212020-08-04 14:58:35 +02002382 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002384 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002385 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002386 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002387 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 psa_set_key_usage_flags(&attributes, policy_usage);
2392 psa_set_key_algorithm(&attributes, policy_alg);
2393 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2396 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002401
2402exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 psa_key_derivation_abort(&operation);
2404 psa_destroy_key(key);
2405 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002406}
2407/* END_CASE */
2408
2409/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410void copy_success(int source_usage_arg,
2411 int source_alg_arg, int source_alg2_arg,
2412 unsigned int source_lifetime_arg,
2413 int type_arg, data_t *material,
2414 int copy_attributes,
2415 int target_usage_arg,
2416 int target_alg_arg, int target_alg2_arg,
2417 unsigned int target_lifetime_arg,
2418 int expected_usage_arg,
2419 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002420{
Gilles Peskineca25db92019-04-19 11:43:08 +02002421 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2422 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002423 psa_key_usage_t expected_usage = expected_usage_arg;
2424 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002425 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302426 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2427 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002428 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2429 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002430 uint8_t *export_buffer = NULL;
2431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002433
Gilles Peskineca25db92019-04-19 11:43:08 +02002434 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2436 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2437 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2438 psa_set_key_type(&source_attributes, type_arg);
2439 psa_set_key_lifetime(&source_attributes, source_lifetime);
2440 PSA_ASSERT(psa_import_key(&source_attributes,
2441 material->x, material->len,
2442 &source_key));
2443 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002444
Gilles Peskineca25db92019-04-19 11:43:08 +02002445 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002447 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002448 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 if (target_usage_arg != -1) {
2452 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2453 }
2454 if (target_alg_arg != -1) {
2455 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2456 }
2457 if (target_alg2_arg != -1) {
2458 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2459 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002460
Archana8a180362021-07-05 02:18:48 +05302461
Gilles Peskine57ab7212019-01-28 13:03:09 +01002462 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 PSA_ASSERT(psa_copy_key(source_key,
2464 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002465
2466 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468
2469 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2471 TEST_EQUAL(psa_get_key_type(&source_attributes),
2472 psa_get_key_type(&target_attributes));
2473 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2474 psa_get_key_bits(&target_attributes));
2475 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2476 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2477 TEST_EQUAL(expected_alg2,
2478 psa_get_key_enrollment_algorithm(&target_attributes));
2479 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002480 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 ASSERT_ALLOC(export_buffer, material->len);
2482 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2483 material->len, &length));
2484 ASSERT_COMPARE(material->x, material->len,
2485 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002486 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 if (!psa_key_lifetime_is_external(target_lifetime)) {
2489 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302490 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 }
2492 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302493 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 }
Archana8a180362021-07-05 02:18:48 +05302495 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002498
2499exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002500 /*
2501 * Source and target key attributes may have been returned by
2502 * psa_get_key_attributes() thus reset them as required.
2503 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 psa_reset_key_attributes(&source_attributes);
2505 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 PSA_DONE();
2508 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002509}
2510/* END_CASE */
2511
2512/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002513void copy_fail(int source_usage_arg,
2514 int source_alg_arg, int source_alg2_arg,
2515 int source_lifetime_arg,
2516 int type_arg, data_t *material,
2517 int target_type_arg, int target_bits_arg,
2518 int target_usage_arg,
2519 int target_alg_arg, int target_alg2_arg,
2520 int target_id_arg, int target_lifetime_arg,
2521 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002522{
2523 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2524 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002525 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2526 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002530
2531 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2533 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2534 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2535 psa_set_key_type(&source_attributes, type_arg);
2536 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2537 PSA_ASSERT(psa_import_key(&source_attributes,
2538 material->x, material->len,
2539 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002540
2541 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 psa_set_key_id(&target_attributes, key_id);
2543 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2544 psa_set_key_type(&target_attributes, target_type_arg);
2545 psa_set_key_bits(&target_attributes, target_bits_arg);
2546 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2547 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2548 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002549
2550 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 TEST_EQUAL(psa_copy_key(source_key,
2552 &target_attributes, &target_key),
2553 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002556
Gilles Peskine4a644642019-05-03 17:14:08 +02002557exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 psa_reset_key_attributes(&source_attributes);
2559 psa_reset_key_attributes(&target_attributes);
2560 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002561}
2562/* END_CASE */
2563
2564/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002565void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002566{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002567 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002568 /* Test each valid way of initializing the object, except for `= {0}`, as
2569 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2570 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002571 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002573 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2574 psa_hash_operation_t zero;
2575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002577
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002578 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2580 PSA_ERROR_BAD_STATE);
2581 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2582 PSA_ERROR_BAD_STATE);
2583 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2584 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002585
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002586 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 PSA_ASSERT(psa_hash_abort(&func));
2588 PSA_ASSERT(psa_hash_abort(&init));
2589 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002590}
2591/* END_CASE */
2592
2593/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002594void hash_setup(int alg_arg,
2595 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002596{
2597 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002598 uint8_t *output = NULL;
2599 size_t output_size = 0;
2600 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002601 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002602 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002603 psa_status_t status;
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002606
Neil Armstrongedb20862022-02-07 15:47:44 +01002607 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 output_size = PSA_HASH_LENGTH(alg);
2609 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 status = psa_hash_compute(alg, NULL, 0,
2612 output, output_size, &output_length);
2613 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002614
2615 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 status = psa_hash_setup(&operation, alg);
2617 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002618
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002619 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002621
2622 /* If setup failed, reproduce the failure, so as to
2623 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (status != PSA_SUCCESS) {
2625 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2626 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002627
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002628 /* Now the operation object should be reusable. */
2629#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2631 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002632#endif
2633
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002634exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 mbedtls_free(output);
2636 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002637}
2638/* END_CASE */
2639
2640/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002641void hash_compute_fail(int alg_arg, data_t *input,
2642 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002643{
2644 psa_algorithm_t alg = alg_arg;
2645 uint8_t *output = NULL;
2646 size_t output_size = output_size_arg;
2647 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002648 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002649 psa_status_t expected_status = expected_status_arg;
2650 psa_status_t status;
2651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002653
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002655
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002656 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 status = psa_hash_compute(alg, input->x, input->len,
2658 output, output_size, &output_length);
2659 TEST_EQUAL(status, expected_status);
2660 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002661
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002662 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 status = psa_hash_setup(&operation, alg);
2664 if (status == PSA_SUCCESS) {
2665 status = psa_hash_update(&operation, input->x, input->len);
2666 if (status == PSA_SUCCESS) {
2667 status = psa_hash_finish(&operation, output, output_size,
2668 &output_length);
2669 if (status == PSA_SUCCESS) {
2670 TEST_LE_U(output_length, output_size);
2671 } else {
2672 TEST_EQUAL(status, expected_status);
2673 }
2674 } else {
2675 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002676 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 } else {
2678 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002679 }
2680
Gilles Peskine0a749c82019-11-28 19:33:58 +01002681exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 PSA_ASSERT(psa_hash_abort(&operation));
2683 mbedtls_free(output);
2684 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002685}
2686/* END_CASE */
2687
2688/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002689void hash_compare_fail(int alg_arg, data_t *input,
2690 data_t *reference_hash,
2691 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002692{
2693 psa_algorithm_t alg = alg_arg;
2694 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002695 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002696 psa_status_t status;
2697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002699
Neil Armstrong55a1be12022-02-07 11:23:20 +01002700 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 status = psa_hash_compare(alg, input->x, input->len,
2702 reference_hash->x, reference_hash->len);
2703 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002704
Neil Armstrong55a1be12022-02-07 11:23:20 +01002705 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 status = psa_hash_setup(&operation, alg);
2707 if (status == PSA_SUCCESS) {
2708 status = psa_hash_update(&operation, input->x, input->len);
2709 if (status == PSA_SUCCESS) {
2710 status = psa_hash_verify(&operation, reference_hash->x,
2711 reference_hash->len);
2712 TEST_EQUAL(status, expected_status);
2713 } else {
2714 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002715 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 } else {
2717 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002718 }
2719
Gilles Peskine88e08462020-01-28 20:43:00 +01002720exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 PSA_ASSERT(psa_hash_abort(&operation));
2722 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002723}
2724/* END_CASE */
2725
2726/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002727void hash_compute_compare(int alg_arg, data_t *input,
2728 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002729{
2730 psa_algorithm_t alg = alg_arg;
2731 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2732 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002733 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002734 size_t i;
2735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002737
Neil Armstrongca30a002022-02-07 11:40:23 +01002738 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2740 output, PSA_HASH_LENGTH(alg),
2741 &output_length));
2742 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2743 ASSERT_COMPARE(output, output_length,
2744 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002745
Neil Armstrongca30a002022-02-07 11:40:23 +01002746 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 PSA_ASSERT(psa_hash_setup(&operation, alg));
2748 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2749 PSA_ASSERT(psa_hash_finish(&operation, output,
2750 PSA_HASH_LENGTH(alg),
2751 &output_length));
2752 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2753 ASSERT_COMPARE(output, output_length,
2754 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002755
2756 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2758 output, sizeof(output),
2759 &output_length));
2760 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2761 ASSERT_COMPARE(output, output_length,
2762 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002763
Neil Armstrongca30a002022-02-07 11:40:23 +01002764 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 PSA_ASSERT(psa_hash_setup(&operation, alg));
2766 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2767 PSA_ASSERT(psa_hash_finish(&operation, output,
2768 sizeof(output), &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 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2775 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002776
Neil Armstrongca30a002022-02-07 11:40:23 +01002777 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 PSA_ASSERT(psa_hash_setup(&operation, alg));
2779 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2780 PSA_ASSERT(psa_hash_verify(&operation, output,
2781 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002782
2783 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2785 output, output_length + 1),
2786 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002787
Neil Armstrongca30a002022-02-07 11:40:23 +01002788 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 PSA_ASSERT(psa_hash_setup(&operation, alg));
2790 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2791 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2792 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002793
2794 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2796 output, output_length - 1),
2797 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002798
Neil Armstrongca30a002022-02-07 11:40:23 +01002799 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 PSA_ASSERT(psa_hash_setup(&operation, alg));
2801 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2802 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2803 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002804
Gilles Peskine0a749c82019-11-28 19:33:58 +01002805 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 for (i = 0; i < output_length; i++) {
2807 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002808 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002809
2810 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2812 output, output_length),
2813 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002814
2815 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 PSA_ASSERT(psa_hash_setup(&operation, alg));
2817 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2818 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2819 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002820
Gilles Peskine0a749c82019-11-28 19:33:58 +01002821 output[i] ^= 1;
2822 }
2823
2824exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 PSA_ASSERT(psa_hash_abort(&operation));
2826 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002827}
2828/* END_CASE */
2829
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002830/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002831void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002832{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002833 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002834 unsigned char input[] = "";
2835 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002836 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002837 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2838 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2840 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002841 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002842 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002843 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002846
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002847 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 PSA_ASSERT(psa_hash_setup(&operation, alg));
2849 ASSERT_OPERATION_IS_ACTIVE(operation);
2850 TEST_EQUAL(psa_hash_setup(&operation, alg),
2851 PSA_ERROR_BAD_STATE);
2852 ASSERT_OPERATION_IS_INACTIVE(operation);
2853 PSA_ASSERT(psa_hash_abort(&operation));
2854 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002855
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002856 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2858 PSA_ERROR_BAD_STATE);
2859 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002860
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002861 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002863 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 ASSERT_OPERATION_IS_ACTIVE(operation);
2865 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2866 PSA_ERROR_BAD_STATE);
2867 ASSERT_OPERATION_IS_INACTIVE(operation);
2868 PSA_ASSERT(psa_hash_abort(&operation));
2869 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002870
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002871 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 PSA_ASSERT(psa_hash_setup(&operation, alg));
2873 PSA_ASSERT(psa_hash_finish(&operation,
2874 hash, sizeof(hash), &hash_len));
2875 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2876 PSA_ERROR_BAD_STATE);
2877 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002878
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002879 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 TEST_EQUAL(psa_hash_verify(&operation,
2881 valid_hash, sizeof(valid_hash)),
2882 PSA_ERROR_BAD_STATE);
2883 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002884
2885 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 PSA_ASSERT(psa_hash_setup(&operation, alg));
2887 PSA_ASSERT(psa_hash_finish(&operation,
2888 hash, sizeof(hash), &hash_len));
2889 TEST_EQUAL(psa_hash_verify(&operation,
2890 valid_hash, sizeof(valid_hash)),
2891 PSA_ERROR_BAD_STATE);
2892 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002893
2894 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 PSA_ASSERT(psa_hash_setup(&operation, alg));
2896 ASSERT_OPERATION_IS_ACTIVE(operation);
2897 PSA_ASSERT(psa_hash_verify(&operation,
2898 valid_hash, sizeof(valid_hash)));
2899 ASSERT_OPERATION_IS_INACTIVE(operation);
2900 TEST_EQUAL(psa_hash_verify(&operation,
2901 valid_hash, sizeof(valid_hash)),
2902 PSA_ERROR_BAD_STATE);
2903 ASSERT_OPERATION_IS_INACTIVE(operation);
2904 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002905
2906 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 TEST_EQUAL(psa_hash_finish(&operation,
2908 hash, sizeof(hash), &hash_len),
2909 PSA_ERROR_BAD_STATE);
2910 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002911
2912 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 PSA_ASSERT(psa_hash_setup(&operation, alg));
2914 PSA_ASSERT(psa_hash_finish(&operation,
2915 hash, sizeof(hash), &hash_len));
2916 TEST_EQUAL(psa_hash_finish(&operation,
2917 hash, sizeof(hash), &hash_len),
2918 PSA_ERROR_BAD_STATE);
2919 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002920
2921 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 PSA_ASSERT(psa_hash_setup(&operation, alg));
2923 PSA_ASSERT(psa_hash_verify(&operation,
2924 valid_hash, sizeof(valid_hash)));
2925 TEST_EQUAL(psa_hash_finish(&operation,
2926 hash, sizeof(hash), &hash_len),
2927 PSA_ERROR_BAD_STATE);
2928 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002929
2930exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002932}
2933/* END_CASE */
2934
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002935/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002936void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002937{
2938 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002939 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2940 * appended to it */
2941 unsigned char hash[] = {
2942 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2943 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2945 };
2946 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002947 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002950
itayzafrir27e69452018-11-01 14:26:34 +02002951 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 PSA_ASSERT(psa_hash_setup(&operation, alg));
2953 ASSERT_OPERATION_IS_ACTIVE(operation);
2954 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2955 PSA_ERROR_INVALID_SIGNATURE);
2956 ASSERT_OPERATION_IS_INACTIVE(operation);
2957 PSA_ASSERT(psa_hash_abort(&operation));
2958 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002959
itayzafrir27e69452018-11-01 14:26:34 +02002960 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 PSA_ASSERT(psa_hash_setup(&operation, alg));
2962 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2963 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002964
itayzafrir27e69452018-11-01 14:26:34 +02002965 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 PSA_ASSERT(psa_hash_setup(&operation, alg));
2967 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2968 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002969
itayzafrirec93d302018-10-18 18:01:10 +03002970exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002972}
2973/* END_CASE */
2974
Ronald Cronee414c72021-03-18 18:50:08 +01002975/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002976void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002977{
2978 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002979 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002981 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002982 size_t hash_len;
2983
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03002985
itayzafrir58028322018-10-25 10:22:01 +03002986 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 PSA_ASSERT(psa_hash_setup(&operation, alg));
2988 TEST_EQUAL(psa_hash_finish(&operation,
2989 hash, expected_size - 1, &hash_len),
2990 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03002991
2992exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03002994}
2995/* END_CASE */
2996
Ronald Cronee414c72021-03-18 18:50:08 +01002997/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002998void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002999{
3000 psa_algorithm_t alg = PSA_ALG_SHA_256;
3001 unsigned char hash[PSA_HASH_MAX_SIZE];
3002 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3003 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3004 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3005 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3006 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3007 size_t hash_len;
3008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 PSA_ASSERT(psa_crypto_init());
3010 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3013 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3014 PSA_ASSERT(psa_hash_finish(&op_finished,
3015 hash, sizeof(hash), &hash_len));
3016 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3017 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3020 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3023 PSA_ASSERT(psa_hash_finish(&op_init,
3024 hash, sizeof(hash), &hash_len));
3025 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3026 PSA_ASSERT(psa_hash_finish(&op_finished,
3027 hash, sizeof(hash), &hash_len));
3028 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3029 PSA_ASSERT(psa_hash_finish(&op_aborted,
3030 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003031
3032exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 psa_hash_abort(&op_source);
3034 psa_hash_abort(&op_init);
3035 psa_hash_abort(&op_setup);
3036 psa_hash_abort(&op_finished);
3037 psa_hash_abort(&op_aborted);
3038 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003039}
3040/* END_CASE */
3041
Ronald Cronee414c72021-03-18 18:50:08 +01003042/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003043void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003044{
3045 psa_algorithm_t alg = PSA_ALG_SHA_256;
3046 unsigned char hash[PSA_HASH_MAX_SIZE];
3047 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3048 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3049 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3050 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3051 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3052 size_t hash_len;
3053
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3057 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3058 PSA_ASSERT(psa_hash_finish(&op_finished,
3059 hash, sizeof(hash), &hash_len));
3060 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3061 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3064 PSA_ASSERT(psa_hash_finish(&op_target,
3065 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3068 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3069 PSA_ERROR_BAD_STATE);
3070 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3071 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003072
3073exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 psa_hash_abort(&op_target);
3075 psa_hash_abort(&op_init);
3076 psa_hash_abort(&op_setup);
3077 psa_hash_abort(&op_finished);
3078 psa_hash_abort(&op_aborted);
3079 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003080}
3081/* END_CASE */
3082
itayzafrir58028322018-10-25 10:22:01 +03003083/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003084void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003085{
Jaeden Amero252ef282019-02-15 14:05:35 +00003086 const uint8_t input[1] = { 0 };
3087
Jaeden Amero769ce272019-01-04 11:48:03 +00003088 /* Test each valid way of initializing the object, except for `= {0}`, as
3089 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3090 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003091 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003093 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3094 psa_mac_operation_t zero;
3095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003097
Jaeden Amero252ef282019-02-15 14:05:35 +00003098 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 TEST_EQUAL(psa_mac_update(&func,
3100 input, sizeof(input)),
3101 PSA_ERROR_BAD_STATE);
3102 TEST_EQUAL(psa_mac_update(&init,
3103 input, sizeof(input)),
3104 PSA_ERROR_BAD_STATE);
3105 TEST_EQUAL(psa_mac_update(&zero,
3106 input, sizeof(input)),
3107 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003108
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003109 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 PSA_ASSERT(psa_mac_abort(&func));
3111 PSA_ASSERT(psa_mac_abort(&init));
3112 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003113}
3114/* END_CASE */
3115
3116/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003117void mac_setup(int key_type_arg,
3118 data_t *key,
3119 int alg_arg,
3120 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003121{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003122 psa_key_type_t key_type = key_type_arg;
3123 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003124 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003126 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3127#if defined(KNOWN_SUPPORTED_MAC_ALG)
3128 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3129#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3134 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 }
3137 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003138
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003139 /* The operation object should be reusable. */
3140#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3142 smoke_test_key_data,
3143 sizeof(smoke_test_key_data),
3144 KNOWN_SUPPORTED_MAC_ALG,
3145 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 }
3148 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003149#endif
3150
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003153}
3154/* END_CASE */
3155
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003156/* 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 +01003157void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003158{
Ronald Cron5425a212020-08-04 14:58:35 +02003159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003160 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3161 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003162 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003163 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3164 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3166 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003168 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3169 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3170 size_t sign_mac_length = 0;
3171 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3172 const uint8_t verify_mac[] = {
3173 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3174 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3176 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003177
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 PSA_ASSERT(psa_crypto_init());
3179 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3180 psa_set_key_algorithm(&attributes, alg);
3181 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003182
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3184 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003185
Jaeden Amero252ef282019-02-15 14:05:35 +00003186 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3188 PSA_ERROR_BAD_STATE);
3189 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003190
3191 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3193 &sign_mac_length),
3194 PSA_ERROR_BAD_STATE);
3195 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003196
3197 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 TEST_EQUAL(psa_mac_verify_finish(&operation,
3199 verify_mac, sizeof(verify_mac)),
3200 PSA_ERROR_BAD_STATE);
3201 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003202
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003203 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3205 ASSERT_OPERATION_IS_ACTIVE(operation);
3206 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3207 PSA_ERROR_BAD_STATE);
3208 ASSERT_OPERATION_IS_INACTIVE(operation);
3209 PSA_ASSERT(psa_mac_abort(&operation));
3210 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003211
Jaeden Amero252ef282019-02-15 14:05:35 +00003212 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3214 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3215 PSA_ASSERT(psa_mac_sign_finish(&operation,
3216 sign_mac, sizeof(sign_mac),
3217 &sign_mac_length));
3218 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3219 PSA_ERROR_BAD_STATE);
3220 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003221
3222 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3224 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3225 PSA_ASSERT(psa_mac_verify_finish(&operation,
3226 verify_mac, sizeof(verify_mac)));
3227 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3228 PSA_ERROR_BAD_STATE);
3229 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003230
3231 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3233 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3234 PSA_ASSERT(psa_mac_sign_finish(&operation,
3235 sign_mac, sizeof(sign_mac),
3236 &sign_mac_length));
3237 TEST_EQUAL(psa_mac_sign_finish(&operation,
3238 sign_mac, sizeof(sign_mac),
3239 &sign_mac_length),
3240 PSA_ERROR_BAD_STATE);
3241 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003242
3243 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3245 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3246 PSA_ASSERT(psa_mac_verify_finish(&operation,
3247 verify_mac, sizeof(verify_mac)));
3248 TEST_EQUAL(psa_mac_verify_finish(&operation,
3249 verify_mac, sizeof(verify_mac)),
3250 PSA_ERROR_BAD_STATE);
3251 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003252
3253 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3255 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3256 ASSERT_OPERATION_IS_ACTIVE(operation);
3257 TEST_EQUAL(psa_mac_verify_finish(&operation,
3258 verify_mac, sizeof(verify_mac)),
3259 PSA_ERROR_BAD_STATE);
3260 ASSERT_OPERATION_IS_INACTIVE(operation);
3261 PSA_ASSERT(psa_mac_abort(&operation));
3262 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003263
3264 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3266 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3267 ASSERT_OPERATION_IS_ACTIVE(operation);
3268 TEST_EQUAL(psa_mac_sign_finish(&operation,
3269 sign_mac, sizeof(sign_mac),
3270 &sign_mac_length),
3271 PSA_ERROR_BAD_STATE);
3272 ASSERT_OPERATION_IS_INACTIVE(operation);
3273 PSA_ASSERT(psa_mac_abort(&operation));
3274 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003275
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003277
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003278exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003280}
3281/* END_CASE */
3282
3283/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003284void mac_sign_verify_multi(int key_type_arg,
3285 data_t *key_data,
3286 int alg_arg,
3287 data_t *input,
3288 int is_verify,
3289 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003290{
3291 size_t data_part_len = 0;
3292
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003294 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003296
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 if (mac_multipart_internal_func(key_type_arg, key_data,
3298 alg_arg,
3299 input, data_part_len,
3300 expected_mac,
3301 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003302 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003304
3305 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003307
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 if (mac_multipart_internal_func(key_type_arg, key_data,
3309 alg_arg,
3310 input, data_part_len,
3311 expected_mac,
3312 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003313 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003315 }
3316
3317 /* Goto is required to silence warnings about unused labels, as we
3318 * don't actually do any test assertions in this function. */
3319 goto exit;
3320}
3321/* END_CASE */
3322
3323/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003324void mac_sign(int key_type_arg,
3325 data_t *key_data,
3326 int alg_arg,
3327 data_t *input,
3328 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003329{
Ronald Cron5425a212020-08-04 14:58:35 +02003330 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003331 psa_key_type_t key_type = key_type_arg;
3332 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003333 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003335 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003336 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003338 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003339 const size_t output_sizes_to_test[] = {
3340 0,
3341 1,
3342 expected_mac->len - 1,
3343 expected_mac->len,
3344 expected_mac->len + 1,
3345 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003346
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003348 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003350
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3354 psa_set_key_algorithm(&attributes, alg);
3355 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3358 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003359
Gilles Peskine449bd832023-01-11 14:50:10 +01003360 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003361 const size_t output_size = output_sizes_to_test[i];
3362 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 (output_size >= expected_mac->len ? PSA_SUCCESS :
3364 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 mbedtls_test_set_step(output_size);
3367 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003368
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003369 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 TEST_EQUAL(psa_mac_compute(key, alg,
3371 input->x, input->len,
3372 actual_mac, output_size, &mac_length),
3373 expected_status);
3374 if (expected_status == PSA_SUCCESS) {
3375 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3376 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003377 }
3378
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 if (output_size > 0) {
3380 memset(actual_mac, 0, output_size);
3381 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003382
3383 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3385 PSA_ASSERT(psa_mac_update(&operation,
3386 input->x, input->len));
3387 TEST_EQUAL(psa_mac_sign_finish(&operation,
3388 actual_mac, output_size,
3389 &mac_length),
3390 expected_status);
3391 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003392
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 if (expected_status == PSA_SUCCESS) {
3394 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3395 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003396 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003398 actual_mac = NULL;
3399 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003400
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003401exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 psa_mac_abort(&operation);
3403 psa_destroy_key(key);
3404 PSA_DONE();
3405 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003406}
3407/* END_CASE */
3408
3409/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003410void mac_verify(int key_type_arg,
3411 data_t *key_data,
3412 int alg_arg,
3413 data_t *input,
3414 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003415{
Ronald Cron5425a212020-08-04 14:58:35 +02003416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003417 psa_key_type_t key_type = key_type_arg;
3418 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003419 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003421 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003424
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3428 psa_set_key_algorithm(&attributes, alg);
3429 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003430
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3432 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003433
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003434 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3436 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003437
3438 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3440 PSA_ASSERT(psa_mac_update(&operation,
3441 input->x, input->len));
3442 PSA_ASSERT(psa_mac_verify_finish(&operation,
3443 expected_mac->x,
3444 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003445
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003446 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 TEST_EQUAL(psa_mac_verify(key, alg,
3448 input->x, input->len,
3449 expected_mac->x,
3450 expected_mac->len - 1),
3451 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003452
3453 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3455 PSA_ASSERT(psa_mac_update(&operation,
3456 input->x, input->len));
3457 TEST_EQUAL(psa_mac_verify_finish(&operation,
3458 expected_mac->x,
3459 expected_mac->len - 1),
3460 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003461
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003462 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3464 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3465 TEST_EQUAL(psa_mac_verify(key, alg,
3466 input->x, input->len,
3467 perturbed_mac, 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 long, 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 perturbed_mac,
3476 expected_mac->len + 1),
3477 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003478
3479 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 for (size_t i = 0; i < expected_mac->len; i++) {
3481 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003482 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003483
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 TEST_EQUAL(psa_mac_verify(key, alg,
3485 input->x, input->len,
3486 perturbed_mac, expected_mac->len),
3487 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003488
Gilles Peskine449bd832023-01-11 14:50:10 +01003489 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3490 PSA_ASSERT(psa_mac_update(&operation,
3491 input->x, input->len));
3492 TEST_EQUAL(psa_mac_verify_finish(&operation,
3493 perturbed_mac,
3494 expected_mac->len),
3495 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003496 perturbed_mac[i] ^= 1;
3497 }
3498
Gilles Peskine8c9def32018-02-08 10:02:12 +01003499exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 psa_mac_abort(&operation);
3501 psa_destroy_key(key);
3502 PSA_DONE();
3503 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003504}
3505/* END_CASE */
3506
3507/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003508void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003509{
Jaeden Ameroab439972019-02-15 14:12:05 +00003510 const uint8_t input[1] = { 0 };
3511 unsigned char output[1] = { 0 };
3512 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003513 /* Test each valid way of initializing the object, except for `= {0}`, as
3514 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3515 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003516 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003518 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3519 psa_cipher_operation_t zero;
3520
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003522
Jaeden Ameroab439972019-02-15 14:12:05 +00003523 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 TEST_EQUAL(psa_cipher_update(&func,
3525 input, sizeof(input),
3526 output, sizeof(output),
3527 &output_length),
3528 PSA_ERROR_BAD_STATE);
3529 TEST_EQUAL(psa_cipher_update(&init,
3530 input, sizeof(input),
3531 output, sizeof(output),
3532 &output_length),
3533 PSA_ERROR_BAD_STATE);
3534 TEST_EQUAL(psa_cipher_update(&zero,
3535 input, sizeof(input),
3536 output, sizeof(output),
3537 &output_length),
3538 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003539
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003540 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003541 PSA_ASSERT(psa_cipher_abort(&func));
3542 PSA_ASSERT(psa_cipher_abort(&init));
3543 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003544}
3545/* END_CASE */
3546
3547/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003548void cipher_setup(int key_type_arg,
3549 data_t *key,
3550 int alg_arg,
3551 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003552{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003553 psa_key_type_t key_type = key_type_arg;
3554 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003555 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003556 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003557 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003558#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003559 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3560#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003561
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003563
Gilles Peskine449bd832023-01-11 14:50:10 +01003564 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3565 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003566 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 }
3568 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003569
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003570 /* The operation object should be reusable. */
3571#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003572 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3573 smoke_test_key_data,
3574 sizeof(smoke_test_key_data),
3575 KNOWN_SUPPORTED_CIPHER_ALG,
3576 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003577 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003578 }
3579 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003580#endif
3581
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 psa_cipher_abort(&operation);
3584 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003585}
3586/* END_CASE */
3587
Ronald Cronee414c72021-03-18 18:50:08 +01003588/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003589void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003590{
Ronald Cron5425a212020-08-04 14:58:35 +02003591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003592 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3593 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003594 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003595 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003596 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003597 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003598 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003599 0xaa, 0xaa, 0xaa, 0xaa
3600 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003601 const uint8_t text[] = {
3602 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003603 0xbb, 0xbb, 0xbb, 0xbb
3604 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003605 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003606 size_t length = 0;
3607
Gilles Peskine449bd832023-01-11 14:50:10 +01003608 PSA_ASSERT(psa_crypto_init());
3609 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3610 psa_set_key_algorithm(&attributes, alg);
3611 psa_set_key_type(&attributes, key_type);
3612 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3613 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003614
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003615 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3617 ASSERT_OPERATION_IS_ACTIVE(operation);
3618 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3619 PSA_ERROR_BAD_STATE);
3620 ASSERT_OPERATION_IS_INACTIVE(operation);
3621 PSA_ASSERT(psa_cipher_abort(&operation));
3622 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003623
3624 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003625 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3626 ASSERT_OPERATION_IS_ACTIVE(operation);
3627 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3628 PSA_ERROR_BAD_STATE);
3629 ASSERT_OPERATION_IS_INACTIVE(operation);
3630 PSA_ASSERT(psa_cipher_abort(&operation));
3631 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003632
Jaeden Ameroab439972019-02-15 14:12:05 +00003633 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3635 buffer, sizeof(buffer),
3636 &length),
3637 PSA_ERROR_BAD_STATE);
3638 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003639
3640 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003641 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3642 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3643 buffer, sizeof(buffer),
3644 &length));
3645 ASSERT_OPERATION_IS_ACTIVE(operation);
3646 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3647 buffer, sizeof(buffer),
3648 &length),
3649 PSA_ERROR_BAD_STATE);
3650 ASSERT_OPERATION_IS_INACTIVE(operation);
3651 PSA_ASSERT(psa_cipher_abort(&operation));
3652 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003653
3654 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3656 PSA_ASSERT(psa_cipher_set_iv(&operation,
3657 iv, sizeof(iv)));
3658 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3659 buffer, sizeof(buffer),
3660 &length),
3661 PSA_ERROR_BAD_STATE);
3662 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003663
3664 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003665 TEST_EQUAL(psa_cipher_set_iv(&operation,
3666 iv, sizeof(iv)),
3667 PSA_ERROR_BAD_STATE);
3668 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003669
3670 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003671 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3672 PSA_ASSERT(psa_cipher_set_iv(&operation,
3673 iv, sizeof(iv)));
3674 ASSERT_OPERATION_IS_ACTIVE(operation);
3675 TEST_EQUAL(psa_cipher_set_iv(&operation,
3676 iv, sizeof(iv)),
3677 PSA_ERROR_BAD_STATE);
3678 ASSERT_OPERATION_IS_INACTIVE(operation);
3679 PSA_ASSERT(psa_cipher_abort(&operation));
3680 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003681
3682 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003683 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3684 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3685 buffer, sizeof(buffer),
3686 &length));
3687 TEST_EQUAL(psa_cipher_set_iv(&operation,
3688 iv, sizeof(iv)),
3689 PSA_ERROR_BAD_STATE);
3690 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003691
3692 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003693 TEST_EQUAL(psa_cipher_update(&operation,
3694 text, sizeof(text),
3695 buffer, sizeof(buffer),
3696 &length),
3697 PSA_ERROR_BAD_STATE);
3698 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003699
3700 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003701 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3702 ASSERT_OPERATION_IS_ACTIVE(operation);
3703 TEST_EQUAL(psa_cipher_update(&operation,
3704 text, sizeof(text),
3705 buffer, sizeof(buffer),
3706 &length),
3707 PSA_ERROR_BAD_STATE);
3708 ASSERT_OPERATION_IS_INACTIVE(operation);
3709 PSA_ASSERT(psa_cipher_abort(&operation));
3710 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003711
3712 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003713 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3714 PSA_ASSERT(psa_cipher_set_iv(&operation,
3715 iv, sizeof(iv)));
3716 PSA_ASSERT(psa_cipher_finish(&operation,
3717 buffer, sizeof(buffer), &length));
3718 TEST_EQUAL(psa_cipher_update(&operation,
3719 text, sizeof(text),
3720 buffer, sizeof(buffer),
3721 &length),
3722 PSA_ERROR_BAD_STATE);
3723 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003724
3725 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003726 TEST_EQUAL(psa_cipher_finish(&operation,
3727 buffer, sizeof(buffer), &length),
3728 PSA_ERROR_BAD_STATE);
3729 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003730
3731 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003732 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003733 /* Not calling update means we are encrypting an empty buffer, which is OK
3734 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003735 ASSERT_OPERATION_IS_ACTIVE(operation);
3736 TEST_EQUAL(psa_cipher_finish(&operation,
3737 buffer, sizeof(buffer), &length),
3738 PSA_ERROR_BAD_STATE);
3739 ASSERT_OPERATION_IS_INACTIVE(operation);
3740 PSA_ASSERT(psa_cipher_abort(&operation));
3741 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003742
3743 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003744 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3745 PSA_ASSERT(psa_cipher_set_iv(&operation,
3746 iv, sizeof(iv)));
3747 PSA_ASSERT(psa_cipher_finish(&operation,
3748 buffer, sizeof(buffer), &length));
3749 TEST_EQUAL(psa_cipher_finish(&operation,
3750 buffer, sizeof(buffer), &length),
3751 PSA_ERROR_BAD_STATE);
3752 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003753
Gilles Peskine449bd832023-01-11 14:50:10 +01003754 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003755
Jaeden Ameroab439972019-02-15 14:12:05 +00003756exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003757 psa_cipher_abort(&operation);
3758 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003759}
3760/* END_CASE */
3761
3762/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003763void cipher_encrypt_fail(int alg_arg,
3764 int key_type_arg,
3765 data_t *key_data,
3766 data_t *input,
3767 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003768{
Ronald Cron5425a212020-08-04 14:58:35 +02003769 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003770 psa_status_t status;
3771 psa_key_type_t key_type = key_type_arg;
3772 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003773 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003774 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003775 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3776 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003777 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003778 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003779 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003780 size_t function_output_length;
3781 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3783
Gilles Peskine449bd832023-01-11 14:50:10 +01003784 if (PSA_ERROR_BAD_STATE != expected_status) {
3785 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003786
Gilles Peskine449bd832023-01-11 14:50:10 +01003787 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3788 psa_set_key_algorithm(&attributes, alg);
3789 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003790
Gilles Peskine449bd832023-01-11 14:50:10 +01003791 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3792 input->len);
3793 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003794
Gilles Peskine449bd832023-01-11 14:50:10 +01003795 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3796 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003797 }
3798
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003799 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003800 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3801 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003802
Gilles Peskine449bd832023-01-11 14:50:10 +01003803 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003804
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003805 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003806 status = psa_cipher_encrypt_setup(&operation, key, alg);
3807 if (status == PSA_SUCCESS) {
3808 if (alg != PSA_ALG_ECB_NO_PADDING) {
3809 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3810 iv, iv_size,
3811 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003812 }
3813
Gilles Peskine449bd832023-01-11 14:50:10 +01003814 status = psa_cipher_update(&operation, input->x, input->len,
3815 output, output_buffer_size,
3816 &function_output_length);
3817 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003818 output_length += function_output_length;
3819
Gilles Peskine449bd832023-01-11 14:50:10 +01003820 status = psa_cipher_finish(&operation, output + output_length,
3821 output_buffer_size - output_length,
3822 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003823
Gilles Peskine449bd832023-01-11 14:50:10 +01003824 TEST_EQUAL(status, expected_status);
3825 } else {
3826 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003827 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003828 } else {
3829 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003830 }
3831
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003832exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003833 psa_cipher_abort(&operation);
3834 mbedtls_free(output);
3835 psa_destroy_key(key);
3836 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003837}
3838/* END_CASE */
3839
3840/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003841void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3842 data_t *input, int iv_length,
3843 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003844{
3845 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3846 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3848 size_t output_buffer_size = 0;
3849 unsigned char *output = NULL;
3850
Gilles Peskine449bd832023-01-11 14:50:10 +01003851 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3852 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003853
Gilles Peskine449bd832023-01-11 14:50:10 +01003854 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003855
Gilles Peskine449bd832023-01-11 14:50:10 +01003856 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3857 psa_set_key_algorithm(&attributes, alg);
3858 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003859
Gilles Peskine449bd832023-01-11 14:50:10 +01003860 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3861 &key));
3862 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3863 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3864 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003865
3866exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 psa_cipher_abort(&operation);
3868 mbedtls_free(output);
3869 psa_destroy_key(key);
3870 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003871}
3872/* END_CASE */
3873
3874/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003875void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3876 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003877{
3878 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3879 psa_key_type_t key_type = key_type_arg;
3880 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003881 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3882 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003883 unsigned char *output = NULL;
3884 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003885 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3887
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003889
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003890 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 TEST_LE_U(ciphertext->len,
3892 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3893 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3894 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3895 TEST_LE_U(plaintext->len,
3896 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3897 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3898 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003899
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003900
3901 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 psa_set_key_usage_flags(&attributes,
3903 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3904 psa_set_key_algorithm(&attributes, alg);
3905 psa_set_key_type(&attributes, key_type);
3906 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3907 &key));
3908 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3909 plaintext->len);
3910 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003911
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003912 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3914 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3915 PSA_ERROR_BAD_STATE);
3916 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3917 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3918 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003919
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003920 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003921 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3922 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3923 &length),
3924 PSA_ERROR_BAD_STATE);
3925 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3926 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3927 &length),
3928 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003929
Gilles Peskine286c3142022-04-20 17:09:38 +02003930 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003932 output_length = 0;
3933 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 PSA_ASSERT(psa_cipher_update(&operation,
3935 plaintext->x, plaintext->len,
3936 output, output_buffer_size,
3937 &length));
3938 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003939 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 PSA_ASSERT(psa_cipher_finish(&operation,
3941 mbedtls_buffer_offset(output, output_length),
3942 output_buffer_size - output_length,
3943 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003944 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3946 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003947
Gilles Peskine286c3142022-04-20 17:09:38 +02003948 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003950 output_length = 0;
3951 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 PSA_ASSERT(psa_cipher_update(&operation,
3953 ciphertext->x, ciphertext->len,
3954 output, output_buffer_size,
3955 &length));
3956 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003957 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 PSA_ASSERT(psa_cipher_finish(&operation,
3959 mbedtls_buffer_offset(output, output_length),
3960 output_buffer_size - output_length,
3961 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003962 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 ASSERT_COMPARE(plaintext->x, plaintext->len,
3964 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003965
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003966 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003967 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3969 output, output_buffer_size,
3970 &output_length));
3971 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3972 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003973
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003974 /* One-shot decryption */
3975 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3977 output, output_buffer_size,
3978 &output_length));
3979 ASSERT_COMPARE(plaintext->x, plaintext->len,
3980 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003981
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003982exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 PSA_ASSERT(psa_cipher_abort(&operation));
3984 mbedtls_free(output);
3985 psa_cipher_abort(&operation);
3986 psa_destroy_key(key);
3987 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003988}
3989/* END_CASE */
3990
3991/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003992void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01003993{
3994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3995 psa_algorithm_t alg = alg_arg;
3996 psa_key_type_t key_type = key_type_arg;
3997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3998 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3999 psa_status_t status;
4000
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004002
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4004 psa_set_key_algorithm(&attributes, alg);
4005 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004006
4007 /* Usage of either of these two size macros would cause divide by zero
4008 * with incorrect key types previously. Input length should be irrelevant
4009 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4011 0);
4012 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004013
4014
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4016 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004017
4018 /* Should fail due to invalid alg type (to support invalid key type).
4019 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004021
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004023
4024exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 psa_cipher_abort(&operation);
4026 psa_destroy_key(key);
4027 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004028}
4029/* END_CASE */
4030
4031/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004032void cipher_encrypt_validation(int alg_arg,
4033 int key_type_arg,
4034 data_t *key_data,
4035 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004036{
4037 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4038 psa_key_type_t key_type = key_type_arg;
4039 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004041 unsigned char *output1 = NULL;
4042 size_t output1_buffer_size = 0;
4043 size_t output1_length = 0;
4044 unsigned char *output2 = NULL;
4045 size_t output2_buffer_size = 0;
4046 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004047 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004048 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004052
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4054 psa_set_key_algorithm(&attributes, alg);
4055 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4058 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4059 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4060 ASSERT_ALLOC(output1, output1_buffer_size);
4061 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4064 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004065
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004066 /* The one-shot cipher encryption uses generated iv so validating
4067 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4069 output1_buffer_size, &output1_length));
4070 TEST_LE_U(output1_length,
4071 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4072 TEST_LE_U(output1_length,
4073 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4076 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 PSA_ASSERT(psa_cipher_update(&operation,
4079 input->x, input->len,
4080 output2, output2_buffer_size,
4081 &function_output_length));
4082 TEST_LE_U(function_output_length,
4083 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4084 TEST_LE_U(function_output_length,
4085 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004086 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 PSA_ASSERT(psa_cipher_finish(&operation,
4089 output2 + output2_length,
4090 output2_buffer_size - output2_length,
4091 &function_output_length));
4092 TEST_LE_U(function_output_length,
4093 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4094 TEST_LE_U(function_output_length,
4095 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004096 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 PSA_ASSERT(psa_cipher_abort(&operation));
4099 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4100 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004101
Gilles Peskine50e586b2018-06-08 14:28:46 +02004102exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 psa_cipher_abort(&operation);
4104 mbedtls_free(output1);
4105 mbedtls_free(output2);
4106 psa_destroy_key(key);
4107 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004108}
4109/* END_CASE */
4110
4111/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004112void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4113 data_t *key_data, data_t *iv,
4114 data_t *input,
4115 int first_part_size_arg,
4116 int output1_length_arg, int output2_length_arg,
4117 data_t *expected_output,
4118 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004119{
Ronald Cron5425a212020-08-04 14:58:35 +02004120 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004121 psa_key_type_t key_type = key_type_arg;
4122 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004123 psa_status_t status;
4124 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004125 size_t first_part_size = first_part_size_arg;
4126 size_t output1_length = output1_length_arg;
4127 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004128 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004129 size_t output_buffer_size = 0;
4130 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004131 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004132 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004134
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004136
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4138 psa_set_key_algorithm(&attributes, alg);
4139 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4142 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004143
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 if (iv->len > 0) {
4147 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004148 }
4149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4151 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4152 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 TEST_LE_U(first_part_size, input->len);
4155 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4156 output, output_buffer_size,
4157 &function_output_length));
4158 TEST_ASSERT(function_output_length == output1_length);
4159 TEST_LE_U(function_output_length,
4160 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4161 TEST_LE_U(function_output_length,
4162 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004163 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 if (first_part_size < input->len) {
4166 PSA_ASSERT(psa_cipher_update(&operation,
4167 input->x + first_part_size,
4168 input->len - first_part_size,
4169 (output_buffer_size == 0 ? NULL :
4170 output + total_output_length),
4171 output_buffer_size - total_output_length,
4172 &function_output_length));
4173 TEST_ASSERT(function_output_length == output2_length);
4174 TEST_LE_U(function_output_length,
4175 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4176 alg,
4177 input->len - first_part_size));
4178 TEST_LE_U(function_output_length,
4179 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004180 total_output_length += function_output_length;
4181 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 status = psa_cipher_finish(&operation,
4184 (output_buffer_size == 0 ? NULL :
4185 output + total_output_length),
4186 output_buffer_size - total_output_length,
4187 &function_output_length);
4188 TEST_LE_U(function_output_length,
4189 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4190 TEST_LE_U(function_output_length,
4191 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004192 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004194
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 if (expected_status == PSA_SUCCESS) {
4196 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 ASSERT_COMPARE(expected_output->x, expected_output->len,
4199 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004200 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004201
4202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 psa_cipher_abort(&operation);
4204 mbedtls_free(output);
4205 psa_destroy_key(key);
4206 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004207}
4208/* END_CASE */
4209
4210/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004211void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4212 data_t *key_data, data_t *iv,
4213 data_t *input,
4214 int first_part_size_arg,
4215 int output1_length_arg, int output2_length_arg,
4216 data_t *expected_output,
4217 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004218{
Ronald Cron5425a212020-08-04 14:58:35 +02004219 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004220 psa_key_type_t key_type = key_type_arg;
4221 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004222 psa_status_t status;
4223 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004224 size_t first_part_size = first_part_size_arg;
4225 size_t output1_length = output1_length_arg;
4226 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004227 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004228 size_t output_buffer_size = 0;
4229 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004230 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004231 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004235
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4237 psa_set_key_algorithm(&attributes, alg);
4238 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4241 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004242
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004244
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 if (iv->len > 0) {
4246 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004247 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4250 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4251 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004252
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 TEST_LE_U(first_part_size, input->len);
4254 PSA_ASSERT(psa_cipher_update(&operation,
4255 input->x, first_part_size,
4256 output, output_buffer_size,
4257 &function_output_length));
4258 TEST_ASSERT(function_output_length == output1_length);
4259 TEST_LE_U(function_output_length,
4260 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4261 TEST_LE_U(function_output_length,
4262 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004263 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 if (first_part_size < input->len) {
4266 PSA_ASSERT(psa_cipher_update(&operation,
4267 input->x + first_part_size,
4268 input->len - first_part_size,
4269 (output_buffer_size == 0 ? NULL :
4270 output + total_output_length),
4271 output_buffer_size - total_output_length,
4272 &function_output_length));
4273 TEST_ASSERT(function_output_length == output2_length);
4274 TEST_LE_U(function_output_length,
4275 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4276 alg,
4277 input->len - first_part_size));
4278 TEST_LE_U(function_output_length,
4279 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004280 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004281 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004282
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 status = psa_cipher_finish(&operation,
4284 (output_buffer_size == 0 ? NULL :
4285 output + total_output_length),
4286 output_buffer_size - total_output_length,
4287 &function_output_length);
4288 TEST_LE_U(function_output_length,
4289 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4290 TEST_LE_U(function_output_length,
4291 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004292 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004294
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 if (expected_status == PSA_SUCCESS) {
4296 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 ASSERT_COMPARE(expected_output->x, expected_output->len,
4299 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004300 }
4301
Gilles Peskine50e586b2018-06-08 14:28:46 +02004302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 psa_cipher_abort(&operation);
4304 mbedtls_free(output);
4305 psa_destroy_key(key);
4306 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004307}
4308/* END_CASE */
4309
Gilles Peskine50e586b2018-06-08 14:28:46 +02004310/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004311void cipher_decrypt_fail(int alg_arg,
4312 int key_type_arg,
4313 data_t *key_data,
4314 data_t *iv,
4315 data_t *input_arg,
4316 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004317{
4318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4319 psa_status_t status;
4320 psa_key_type_t key_type = key_type_arg;
4321 psa_algorithm_t alg = alg_arg;
4322 psa_status_t expected_status = expected_status_arg;
4323 unsigned char *input = NULL;
4324 size_t input_buffer_size = 0;
4325 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004326 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004327 size_t output_buffer_size = 0;
4328 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004329 size_t function_output_length;
4330 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4332
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 if (PSA_ERROR_BAD_STATE != expected_status) {
4334 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4337 psa_set_key_algorithm(&attributes, alg);
4338 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4341 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004342 }
4343
4344 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4346 if (input_buffer_size > 0) {
4347 ASSERT_ALLOC(input, input_buffer_size);
4348 memcpy(input, iv->x, iv->len);
4349 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004350 }
4351
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4353 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004354
Neil Armstrong66a479f2022-02-07 15:41:19 +01004355 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4357 output_buffer_size, &output_length);
4358 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004359
Neil Armstrong66a479f2022-02-07 15:41:19 +01004360 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 status = psa_cipher_decrypt_setup(&operation, key, alg);
4362 if (status == PSA_SUCCESS) {
4363 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4364 input_arg->len) +
4365 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4366 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 if (iv->len > 0) {
4369 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 if (status != PSA_SUCCESS) {
4372 TEST_EQUAL(status, expected_status);
4373 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004374 }
4375
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 if (status == PSA_SUCCESS) {
4377 status = psa_cipher_update(&operation,
4378 input_arg->x, input_arg->len,
4379 output_multi, output_buffer_size,
4380 &function_output_length);
4381 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004382 output_length = function_output_length;
4383
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 status = psa_cipher_finish(&operation,
4385 output_multi + output_length,
4386 output_buffer_size - output_length,
4387 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004388
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 TEST_EQUAL(status, expected_status);
4390 } else {
4391 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004392 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 } else {
4394 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004395 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 } else {
4397 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004398 }
4399
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004400exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 psa_cipher_abort(&operation);
4402 mbedtls_free(input);
4403 mbedtls_free(output);
4404 mbedtls_free(output_multi);
4405 psa_destroy_key(key);
4406 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004407}
4408/* END_CASE */
4409
4410/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004411void cipher_decrypt(int alg_arg,
4412 int key_type_arg,
4413 data_t *key_data,
4414 data_t *iv,
4415 data_t *input_arg,
4416 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004417{
4418 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4419 psa_key_type_t key_type = key_type_arg;
4420 psa_algorithm_t alg = alg_arg;
4421 unsigned char *input = NULL;
4422 size_t input_buffer_size = 0;
4423 unsigned char *output = NULL;
4424 size_t output_buffer_size = 0;
4425 size_t output_length = 0;
4426 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4427
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4431 psa_set_key_algorithm(&attributes, alg);
4432 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004433
4434 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4436 if (input_buffer_size > 0) {
4437 ASSERT_ALLOC(input, input_buffer_size);
4438 memcpy(input, iv->x, iv->len);
4439 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004440 }
4441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4443 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004444
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4446 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004447
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4449 output_buffer_size, &output_length));
4450 TEST_LE_U(output_length,
4451 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4452 TEST_LE_U(output_length,
4453 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 ASSERT_COMPARE(expected_output->x, expected_output->len,
4456 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004457exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 mbedtls_free(input);
4459 mbedtls_free(output);
4460 psa_destroy_key(key);
4461 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004462}
4463/* END_CASE */
4464
4465/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004466void cipher_verify_output(int alg_arg,
4467 int key_type_arg,
4468 data_t *key_data,
4469 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004470{
Ronald Cron5425a212020-08-04 14:58:35 +02004471 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004472 psa_key_type_t key_type = key_type_arg;
4473 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004474 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004475 size_t output1_size = 0;
4476 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004477 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004478 size_t output2_size = 0;
4479 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4485 psa_set_key_algorithm(&attributes, alg);
4486 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004487
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4489 &key));
4490 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4491 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004492
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4494 output1, output1_size,
4495 &output1_length));
4496 TEST_LE_U(output1_length,
4497 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4498 TEST_LE_U(output1_length,
4499 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004500
4501 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4505 output2, output2_size,
4506 &output2_length));
4507 TEST_LE_U(output2_length,
4508 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4509 TEST_LE_U(output2_length,
4510 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004513
4514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 mbedtls_free(output1);
4516 mbedtls_free(output2);
4517 psa_destroy_key(key);
4518 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004519}
4520/* END_CASE */
4521
4522/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004523void cipher_verify_output_multipart(int alg_arg,
4524 int key_type_arg,
4525 data_t *key_data,
4526 data_t *input,
4527 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004528{
Ronald Cron5425a212020-08-04 14:58:35 +02004529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004530 psa_key_type_t key_type = key_type_arg;
4531 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004532 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004534 size_t iv_size = 16;
4535 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004536 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004537 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004538 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004539 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004540 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004541 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004542 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004543 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4544 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004546
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004548
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4550 psa_set_key_algorithm(&attributes, alg);
4551 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4554 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004555
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4557 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 if (alg != PSA_ALG_ECB_NO_PADDING) {
4560 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4561 iv, iv_size,
4562 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004563 }
4564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4566 TEST_LE_U(output1_buffer_size,
4567 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4568 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4573 output1, output1_buffer_size,
4574 &function_output_length));
4575 TEST_LE_U(function_output_length,
4576 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4577 TEST_LE_U(function_output_length,
4578 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004579 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 PSA_ASSERT(psa_cipher_update(&operation1,
4582 input->x + first_part_size,
4583 input->len - first_part_size,
4584 output1, output1_buffer_size,
4585 &function_output_length));
4586 TEST_LE_U(function_output_length,
4587 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4588 alg,
4589 input->len - first_part_size));
4590 TEST_LE_U(function_output_length,
4591 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004592 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 PSA_ASSERT(psa_cipher_finish(&operation1,
4595 output1 + output1_length,
4596 output1_buffer_size - output1_length,
4597 &function_output_length));
4598 TEST_LE_U(function_output_length,
4599 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4600 TEST_LE_U(function_output_length,
4601 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004602 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004605
Gilles Peskine048b7f02018-06-08 14:20:49 +02004606 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 TEST_LE_U(output2_buffer_size,
4608 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4609 TEST_LE_U(output2_buffer_size,
4610 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4611 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004612
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 if (iv_length > 0) {
4614 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4615 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004616 }
Moran Pekerded84402018-06-06 16:36:50 +03004617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4619 output2, output2_buffer_size,
4620 &function_output_length));
4621 TEST_LE_U(function_output_length,
4622 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4623 TEST_LE_U(function_output_length,
4624 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004625 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 PSA_ASSERT(psa_cipher_update(&operation2,
4628 output1 + first_part_size,
4629 output1_length - first_part_size,
4630 output2, output2_buffer_size,
4631 &function_output_length));
4632 TEST_LE_U(function_output_length,
4633 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4634 alg,
4635 output1_length - first_part_size));
4636 TEST_LE_U(function_output_length,
4637 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004638 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004639
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 PSA_ASSERT(psa_cipher_finish(&operation2,
4641 output2 + output2_length,
4642 output2_buffer_size - output2_length,
4643 &function_output_length));
4644 TEST_LE_U(function_output_length,
4645 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4646 TEST_LE_U(function_output_length,
4647 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004648 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004653
4654exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 psa_cipher_abort(&operation1);
4656 psa_cipher_abort(&operation2);
4657 mbedtls_free(output1);
4658 mbedtls_free(output2);
4659 psa_destroy_key(key);
4660 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004661}
4662/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004663
Gilles Peskine20035e32018-02-03 22:44:14 +01004664/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004665void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4666 int alg_arg,
4667 data_t *nonce,
4668 data_t *additional_data,
4669 data_t *input_data,
4670 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004671{
Ronald Cron5425a212020-08-04 14:58:35 +02004672 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004673 psa_key_type_t key_type = key_type_arg;
4674 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004675 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004676 unsigned char *output_data = NULL;
4677 size_t output_size = 0;
4678 size_t output_length = 0;
4679 unsigned char *output_data2 = NULL;
4680 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004681 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004682 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004683 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4688 psa_set_key_algorithm(&attributes, alg);
4689 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4692 &key));
4693 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4694 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4697 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004698 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4699 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4701 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4702 TEST_EQUAL(output_size,
4703 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4704 TEST_LE_U(output_size,
4705 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004706 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 status = psa_aead_encrypt(key, alg,
4710 nonce->x, nonce->len,
4711 additional_data->x,
4712 additional_data->len,
4713 input_data->x, input_data->len,
4714 output_data, output_size,
4715 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004716
4717 /* If the operation is not supported, just skip and not fail in case the
4718 * encryption involves a common limitation of cryptography hardwares and
4719 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (status == PSA_ERROR_NOT_SUPPORTED) {
4721 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4722 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004723 }
4724
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if (PSA_SUCCESS == expected_result) {
4728 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004729
Gilles Peskine003a4a92019-05-14 16:09:40 +02004730 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4731 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 TEST_EQUAL(input_data->len,
4733 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 TEST_LE_U(input_data->len,
4736 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 TEST_EQUAL(psa_aead_decrypt(key, alg,
4739 nonce->x, nonce->len,
4740 additional_data->x,
4741 additional_data->len,
4742 output_data, output_length,
4743 output_data2, output_length,
4744 &output_length2),
4745 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004746
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 ASSERT_COMPARE(input_data->x, input_data->len,
4748 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004749 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004750
Gilles Peskinea1cac842018-06-11 19:33:02 +02004751exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 psa_destroy_key(key);
4753 mbedtls_free(output_data);
4754 mbedtls_free(output_data2);
4755 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004756}
4757/* END_CASE */
4758
4759/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004760void aead_encrypt(int key_type_arg, data_t *key_data,
4761 int alg_arg,
4762 data_t *nonce,
4763 data_t *additional_data,
4764 data_t *input_data,
4765 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004766{
Ronald Cron5425a212020-08-04 14:58:35 +02004767 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004768 psa_key_type_t key_type = key_type_arg;
4769 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004770 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004771 unsigned char *output_data = NULL;
4772 size_t output_size = 0;
4773 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004775 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004776
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004778
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4780 psa_set_key_algorithm(&attributes, alg);
4781 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4784 &key));
4785 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4786 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004787
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4789 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004790 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4791 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 TEST_EQUAL(output_size,
4793 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4794 TEST_LE_U(output_size,
4795 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4796 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 status = psa_aead_encrypt(key, alg,
4799 nonce->x, nonce->len,
4800 additional_data->x, additional_data->len,
4801 input_data->x, input_data->len,
4802 output_data, output_size,
4803 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004804
Ronald Cron28a45ed2021-02-09 20:35:42 +01004805 /* If the operation is not supported, just skip and not fail in case the
4806 * encryption involves a common limitation of cryptography hardwares and
4807 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 if (status == PSA_ERROR_NOT_SUPPORTED) {
4809 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4810 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004811 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 PSA_ASSERT(status);
4814 ASSERT_COMPARE(expected_result->x, expected_result->len,
4815 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004816
Gilles Peskinea1cac842018-06-11 19:33:02 +02004817exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 psa_destroy_key(key);
4819 mbedtls_free(output_data);
4820 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004821}
4822/* END_CASE */
4823
4824/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004825void aead_decrypt(int key_type_arg, data_t *key_data,
4826 int alg_arg,
4827 data_t *nonce,
4828 data_t *additional_data,
4829 data_t *input_data,
4830 data_t *expected_data,
4831 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004832{
Ronald Cron5425a212020-08-04 14:58:35 +02004833 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004834 psa_key_type_t key_type = key_type_arg;
4835 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004836 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004837 unsigned char *output_data = NULL;
4838 size_t output_size = 0;
4839 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004841 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004842 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004845
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4847 psa_set_key_algorithm(&attributes, alg);
4848 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4851 &key));
4852 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4853 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004854
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4856 alg);
4857 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4858 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004859 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4860 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 TEST_EQUAL(output_size,
4862 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4863 TEST_LE_U(output_size,
4864 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004865 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 status = psa_aead_decrypt(key, alg,
4869 nonce->x, nonce->len,
4870 additional_data->x,
4871 additional_data->len,
4872 input_data->x, input_data->len,
4873 output_data, output_size,
4874 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004875
Ronald Cron28a45ed2021-02-09 20:35:42 +01004876 /* If the operation is not supported, just skip and not fail in case the
4877 * decryption involves a common limitation of cryptography hardwares and
4878 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 if (status == PSA_ERROR_NOT_SUPPORTED) {
4880 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4881 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004882 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 if (expected_result == PSA_SUCCESS) {
4887 ASSERT_COMPARE(expected_data->x, expected_data->len,
4888 output_data, output_length);
4889 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004890
Gilles Peskinea1cac842018-06-11 19:33:02 +02004891exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 psa_destroy_key(key);
4893 mbedtls_free(output_data);
4894 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004895}
4896/* END_CASE */
4897
4898/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004899void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4900 int alg_arg,
4901 data_t *nonce,
4902 data_t *additional_data,
4903 data_t *input_data,
4904 int do_set_lengths,
4905 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004906{
Paul Elliottd3f82412021-06-16 16:52:21 +01004907 size_t ad_part_len = 0;
4908 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004909 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4912 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004913
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 if (do_set_lengths) {
4915 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004916 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004918 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004920 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004921
4922 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 if (!aead_multipart_internal_func(key_type_arg, key_data,
4924 alg_arg, nonce,
4925 additional_data,
4926 ad_part_len,
4927 input_data, -1,
4928 set_lengths_method,
4929 expected_output,
4930 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004931 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004932 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 /* length(0) part, length(ad_part_len) part, length(0) part... */
4935 mbedtls_test_set_step(1000 + ad_part_len);
4936
4937 if (!aead_multipart_internal_func(key_type_arg, key_data,
4938 alg_arg, nonce,
4939 additional_data,
4940 ad_part_len,
4941 input_data, -1,
4942 set_lengths_method,
4943 expected_output,
4944 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004945 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 }
4947 }
4948
4949 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4950 /* Split data into length(data_part_len) parts. */
4951 mbedtls_test_set_step(2000 + data_part_len);
4952
4953 if (do_set_lengths) {
4954 if (data_part_len & 0x01) {
4955 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4956 } else {
4957 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4958 }
4959 }
4960
4961 if (!aead_multipart_internal_func(key_type_arg, key_data,
4962 alg_arg, nonce,
4963 additional_data, -1,
4964 input_data, data_part_len,
4965 set_lengths_method,
4966 expected_output,
4967 1, 0)) {
4968 break;
4969 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004970
4971 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004973
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 if (!aead_multipart_internal_func(key_type_arg, key_data,
4975 alg_arg, nonce,
4976 additional_data, -1,
4977 input_data, data_part_len,
4978 set_lengths_method,
4979 expected_output,
4980 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004981 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004983 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004984
Paul Elliott8fc45162021-06-23 16:06:01 +01004985 /* Goto is required to silence warnings about unused labels, as we
4986 * don't actually do any test assertions in this function. */
4987 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004988}
4989/* END_CASE */
4990
4991/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004992void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
4993 int alg_arg,
4994 data_t *nonce,
4995 data_t *additional_data,
4996 data_t *input_data,
4997 int do_set_lengths,
4998 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004999{
Paul Elliottd3f82412021-06-16 16:52:21 +01005000 size_t ad_part_len = 0;
5001 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005002 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005005 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005007
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 if (do_set_lengths) {
5009 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005010 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005012 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005014 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005015
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 if (!aead_multipart_internal_func(key_type_arg, key_data,
5017 alg_arg, nonce,
5018 additional_data,
5019 ad_part_len,
5020 input_data, -1,
5021 set_lengths_method,
5022 expected_output,
5023 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005024 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005026
5027 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 if (!aead_multipart_internal_func(key_type_arg, key_data,
5031 alg_arg, nonce,
5032 additional_data,
5033 ad_part_len,
5034 input_data, -1,
5035 set_lengths_method,
5036 expected_output,
5037 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005038 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005040 }
5041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005043 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 if (do_set_lengths) {
5047 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005048 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005050 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005052 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005053
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 if (!aead_multipart_internal_func(key_type_arg, key_data,
5055 alg_arg, nonce,
5056 additional_data, -1,
5057 input_data, data_part_len,
5058 set_lengths_method,
5059 expected_output,
5060 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005061 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063
5064 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005066
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 if (!aead_multipart_internal_func(key_type_arg, key_data,
5068 alg_arg, nonce,
5069 additional_data, -1,
5070 input_data, data_part_len,
5071 set_lengths_method,
5072 expected_output,
5073 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005074 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005076 }
5077
Paul Elliott8fc45162021-06-23 16:06:01 +01005078 /* Goto is required to silence warnings about unused labels, as we
5079 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005080 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005081}
5082/* END_CASE */
5083
5084/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005085void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5086 int alg_arg,
5087 int nonce_length,
5088 int expected_nonce_length_arg,
5089 data_t *additional_data,
5090 data_t *input_data,
5091 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005092{
5093
5094 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5095 psa_key_type_t key_type = key_type_arg;
5096 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005097 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005098 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5100 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005101 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005102 size_t actual_nonce_length = 0;
5103 size_t expected_nonce_length = expected_nonce_length_arg;
5104 unsigned char *output = NULL;
5105 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005106 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005107 size_t ciphertext_size = 0;
5108 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005109 size_t tag_length = 0;
5110 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005113
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5115 psa_set_key_algorithm(&attributes, alg);
5116 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5119 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005120
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005122
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005132
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005134
5135 /* If the operation is not supported, just skip and not fail in case the
5136 * encryption involves a common limitation of cryptography hardwares and
5137 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 if (status == PSA_ERROR_NOT_SUPPORTED) {
5139 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5140 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005141 }
5142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5146 nonce_length,
5147 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 if (expected_status == PSA_SUCCESS) {
5154 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5155 alg));
5156 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005161 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5163 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005164
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5166 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5169 output, output_size,
5170 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5173 &ciphertext_length, tag_buffer,
5174 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005175 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005176
5177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 psa_destroy_key(key);
5179 mbedtls_free(output);
5180 mbedtls_free(ciphertext);
5181 psa_aead_abort(&operation);
5182 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005183}
5184/* END_CASE */
5185
5186/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005187void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5188 int alg_arg,
5189 int nonce_length_arg,
5190 int set_lengths_method_arg,
5191 data_t *additional_data,
5192 data_t *input_data,
5193 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005194{
5195
5196 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5197 psa_key_type_t key_type = key_type_arg;
5198 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005199 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005200 uint8_t *nonce_buffer = NULL;
5201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5202 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5203 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005204 unsigned char *output = NULL;
5205 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005206 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005207 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005208 size_t ciphertext_size = 0;
5209 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005210 size_t tag_length = 0;
5211 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005212 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005213 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005214
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005216
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5218 psa_set_key_algorithm(&attributes, alg);
5219 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5222 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005223
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005225
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005227
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005229
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005235
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005237
5238 /* If the operation is not supported, just skip and not fail in case the
5239 * encryption involves a common limitation of cryptography hardwares and
5240 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 if (status == PSA_ERROR_NOT_SUPPORTED) {
5242 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5243 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005244 }
5245
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005247
Paul Elliott4023ffd2021-09-10 16:21:22 +01005248 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 if (nonce_length_arg == -1) {
5250 /* Arbitrary size buffer, to test zero length valid buffer. */
5251 ASSERT_ALLOC(nonce_buffer, 4);
5252 nonce_length = 0;
5253 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005254 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 nonce_length = (size_t) nonce_length_arg;
5256 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005257
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 if (nonce_buffer) {
5259 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005260 nonce_buffer[index] = 'a' + index;
5261 }
Paul Elliott66696b52021-08-16 18:42:41 +01005262 }
Paul Elliott863864a2021-07-23 17:28:31 +01005263 }
5264
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5266 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5267 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005268 }
5269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005273
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 if (expected_status == PSA_SUCCESS) {
5275 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5276 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5277 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005278 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005280 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 }
Paul Elliott863864a2021-07-23 17:28:31 +01005282
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005283 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5285 additional_data->len),
5286 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005287
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5289 output, output_size,
5290 &ciphertext_length),
5291 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005292
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5294 &ciphertext_length, tag_buffer,
5295 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5296 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005297 }
5298
5299exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 psa_destroy_key(key);
5301 mbedtls_free(output);
5302 mbedtls_free(ciphertext);
5303 mbedtls_free(nonce_buffer);
5304 psa_aead_abort(&operation);
5305 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005306}
5307/* END_CASE */
5308
5309/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005310void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005311 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005312 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005313 data_t *nonce,
5314 data_t *additional_data,
5315 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005317{
5318
5319 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5320 psa_key_type_t key_type = key_type_arg;
5321 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005322 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5324 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5325 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005326 unsigned char *output = NULL;
5327 unsigned char *ciphertext = NULL;
5328 size_t output_size = output_size_arg;
5329 size_t ciphertext_size = 0;
5330 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005331 size_t tag_length = 0;
5332 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5333
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005335
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5337 psa_set_key_algorithm(&attributes, alg);
5338 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005339
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5341 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005342
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005346
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005350
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005352
5353 /* If the operation is not supported, just skip and not fail in case the
5354 * encryption involves a common limitation of cryptography hardwares and
5355 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 if (status == PSA_ERROR_NOT_SUPPORTED) {
5357 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5358 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005359 }
5360
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5364 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005365
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005367
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5369 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005370
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 status = psa_aead_update(&operation, input_data->x, input_data->len,
5372 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005377 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5379 &ciphertext_length, tag_buffer,
5380 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005381 }
5382
5383exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 psa_destroy_key(key);
5385 mbedtls_free(output);
5386 mbedtls_free(ciphertext);
5387 psa_aead_abort(&operation);
5388 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005389}
5390/* END_CASE */
5391
Paul Elliott91b021e2021-07-23 18:52:31 +01005392/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005393void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5394 int alg_arg,
5395 int finish_ciphertext_size_arg,
5396 int tag_size_arg,
5397 data_t *nonce,
5398 data_t *additional_data,
5399 data_t *input_data,
5400 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005401{
5402
5403 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5404 psa_key_type_t key_type = key_type_arg;
5405 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005406 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5408 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5409 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005410 unsigned char *ciphertext = NULL;
5411 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005412 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005413 size_t ciphertext_size = 0;
5414 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5416 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005417 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005420
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5422 psa_set_key_algorithm(&attributes, alg);
5423 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5426 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005427
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005429
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005431
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005435
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005437
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005439
5440 /* If the operation is not supported, just skip and not fail in case the
5441 * encryption involves a common limitation of cryptography hardwares and
5442 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 if (status == PSA_ERROR_NOT_SUPPORTED) {
5444 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5445 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005446 }
5447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005449
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005451
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5453 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5456 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5459 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005460
5461 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 status = psa_aead_finish(&operation, finish_ciphertext,
5463 finish_ciphertext_size,
5464 &ciphertext_length, tag_buffer,
5465 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005468
5469exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 psa_destroy_key(key);
5471 mbedtls_free(ciphertext);
5472 mbedtls_free(finish_ciphertext);
5473 mbedtls_free(tag_buffer);
5474 psa_aead_abort(&operation);
5475 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005476}
5477/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005478
5479/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005480void aead_multipart_verify(int key_type_arg, data_t *key_data,
5481 int alg_arg,
5482 data_t *nonce,
5483 data_t *additional_data,
5484 data_t *input_data,
5485 data_t *tag,
5486 int tag_usage_arg,
5487 int expected_setup_status_arg,
5488 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005489{
5490 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5491 psa_key_type_t key_type = key_type_arg;
5492 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005493 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005494 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5495 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5496 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005497 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005498 unsigned char *plaintext = NULL;
5499 unsigned char *finish_plaintext = NULL;
5500 size_t plaintext_size = 0;
5501 size_t plaintext_length = 0;
5502 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005503 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005504 unsigned char *tag_buffer = NULL;
5505 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005508
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5510 psa_set_key_algorithm(&attributes, alg);
5511 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005512
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5514 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005515
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005517
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5519 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005520
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005522
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005528
5529 /* If the operation is not supported, just skip and not fail in case the
5530 * encryption involves a common limitation of cryptography hardwares and
5531 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 if (status == PSA_ERROR_NOT_SUPPORTED) {
5533 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5534 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005535 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005539 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 }
Paul Elliott9961a662021-09-17 19:19:02 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005545
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 status = psa_aead_set_lengths(&operation, additional_data->len,
5547 input_data->len);
5548 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5551 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5554 input_data->len,
5555 plaintext, plaintext_size,
5556 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005559 tag_buffer = tag->x;
5560 tag_size = tag->len;
5561 }
5562
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 status = psa_aead_verify(&operation, finish_plaintext,
5564 verify_plaintext_size,
5565 &plaintext_length,
5566 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005569
5570exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 psa_destroy_key(key);
5572 mbedtls_free(plaintext);
5573 mbedtls_free(finish_plaintext);
5574 psa_aead_abort(&operation);
5575 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005576}
5577/* END_CASE */
5578
Paul Elliott9961a662021-09-17 19:19:02 +01005579/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005580void aead_multipart_setup(int key_type_arg, data_t *key_data,
5581 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005582{
5583 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5584 psa_key_type_t key_type = key_type_arg;
5585 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005586 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5588 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5589 psa_status_t expected_status = expected_status_arg;
5590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005592
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 psa_set_key_usage_flags(&attributes,
5594 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5595 psa_set_key_algorithm(&attributes, alg);
5596 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005597
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5599 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005600
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005602
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005604
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005608
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005610
5611exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 psa_destroy_key(key);
5613 psa_aead_abort(&operation);
5614 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005615}
5616/* END_CASE */
5617
5618/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005619void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5620 int alg_arg,
5621 data_t *nonce,
5622 data_t *additional_data,
5623 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005624{
5625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5626 psa_key_type_t key_type = key_type_arg;
5627 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005628 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005629 unsigned char *output_data = NULL;
5630 unsigned char *final_data = NULL;
5631 size_t output_size = 0;
5632 size_t finish_output_size = 0;
5633 size_t output_length = 0;
5634 size_t key_bits = 0;
5635 size_t tag_length = 0;
5636 size_t tag_size = 0;
5637 size_t nonce_length = 0;
5638 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5639 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5640 size_t output_part_length = 0;
5641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005644
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 psa_set_key_usage_flags(&attributes,
5646 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5647 psa_set_key_algorithm(&attributes, alg);
5648 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005649
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5651 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005652
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5654 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005655
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005657
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005659
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005661
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005665
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005667
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005669
5670 /* Test all operations error without calling setup first. */
5671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5673 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005674
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005676
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5678 PSA_AEAD_NONCE_MAX_SIZE,
5679 &nonce_length),
5680 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683
Paul Elliott481be342021-07-16 17:38:47 +01005684 /* ------------------------------------------------------- */
5685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5687 input_data->len),
5688 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Paul Elliott481be342021-07-16 17:38:47 +01005692 /* ------------------------------------------------------- */
5693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5695 additional_data->len),
5696 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005699
Paul Elliott481be342021-07-16 17:38:47 +01005700 /* ------------------------------------------------------- */
5701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5703 input_data->len, output_data,
5704 output_size, &output_length),
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_finish(&operation, final_data,
5712 finish_output_size,
5713 &output_part_length,
5714 tag_buffer, tag_length,
5715 &tag_size),
5716 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005719
Paul Elliott481be342021-07-16 17:38:47 +01005720 /* ------------------------------------------------------- */
5721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5723 finish_output_size,
5724 &output_part_length,
5725 tag_buffer,
5726 tag_length),
5727 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005730
5731 /* Test for double setups. */
5732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5736 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005739
Paul Elliott481be342021-07-16 17:38:47 +01005740 /* ------------------------------------------------------- */
5741
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5745 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005748
Paul Elliott374a2be2021-07-16 17:53:40 +01005749 /* ------------------------------------------------------- */
5750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5754 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005757
5758 /* ------------------------------------------------------- */
5759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5763 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005764
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005766
Paul Elliottc23a9a02021-06-21 18:32:46 +01005767 /* Test for not setting a nonce. */
5768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5772 additional_data->len),
5773 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005776
Paul Elliott7f628422021-09-01 12:08:29 +01005777 /* ------------------------------------------------------- */
5778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005780
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5782 input_data->len, output_data,
5783 output_size, &output_length),
5784 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005787
Paul Elliottbdc2c682021-09-21 18:37:10 +01005788 /* ------------------------------------------------------- */
5789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5793 finish_output_size,
5794 &output_part_length,
5795 tag_buffer, tag_length,
5796 &tag_size),
5797 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005800
5801 /* ------------------------------------------------------- */
5802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5806 finish_output_size,
5807 &output_part_length,
5808 tag_buffer,
5809 tag_length),
5810 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005813
Paul Elliottc23a9a02021-06-21 18:32:46 +01005814 /* Test for double setting nonce. */
5815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5821 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005822
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005824
Paul Elliott374a2be2021-07-16 17:53:40 +01005825 /* Test for double generating nonce. */
5826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5830 PSA_AEAD_NONCE_MAX_SIZE,
5831 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005832
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5834 PSA_AEAD_NONCE_MAX_SIZE,
5835 &nonce_length),
5836 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005837
5838
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005840
5841 /* Test for generate nonce then set and vice versa */
5842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5846 PSA_AEAD_NONCE_MAX_SIZE,
5847 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5850 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005853
Andrzej Kurekad837522021-12-15 15:28:49 +01005854 /* Test for generating nonce after calling set lengths */
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5859 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005860
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5862 PSA_AEAD_NONCE_MAX_SIZE,
5863 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005866
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005867 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (operation.alg == PSA_ALG_CCM) {
5872 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5873 input_data->len),
5874 PSA_ERROR_INVALID_ARGUMENT);
5875 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5876 PSA_AEAD_NONCE_MAX_SIZE,
5877 &nonce_length),
5878 PSA_ERROR_BAD_STATE);
5879 } else {
5880 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5881 input_data->len));
5882 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5883 PSA_AEAD_NONCE_MAX_SIZE,
5884 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005885 }
5886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005888
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005889 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005890#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5894 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5895 input_data->len),
5896 PSA_ERROR_INVALID_ARGUMENT);
5897 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5898 PSA_AEAD_NONCE_MAX_SIZE,
5899 &nonce_length),
5900 PSA_ERROR_BAD_STATE);
5901 } else {
5902 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5903 input_data->len));
5904 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5905 PSA_AEAD_NONCE_MAX_SIZE,
5906 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005907 }
5908
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005910#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005911
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005912 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5917 PSA_AEAD_NONCE_MAX_SIZE,
5918 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005919
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 if (operation.alg == PSA_ALG_CCM) {
5921 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5922 input_data->len),
5923 PSA_ERROR_INVALID_ARGUMENT);
5924 } else {
5925 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5926 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005927 }
5928
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005930
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005931 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005932 /* Test for setting nonce after calling set lengths */
5933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5937 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005938
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005942
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005943 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 if (operation.alg == PSA_ALG_CCM) {
5948 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5949 input_data->len),
5950 PSA_ERROR_INVALID_ARGUMENT);
5951 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5952 PSA_ERROR_BAD_STATE);
5953 } else {
5954 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5955 input_data->len));
5956 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005957 }
5958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005960
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005961 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005962#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5966 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5967 input_data->len),
5968 PSA_ERROR_INVALID_ARGUMENT);
5969 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5970 PSA_ERROR_BAD_STATE);
5971 } else {
5972 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5973 input_data->len));
5974 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005975 }
5976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005978#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005979
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005980 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (operation.alg == PSA_ALG_CCM) {
5987 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5988 input_data->len),
5989 PSA_ERROR_INVALID_ARGUMENT);
5990 } else {
5991 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5992 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005993 }
5994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005996
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005997 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00005998#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 if (operation.alg == PSA_ALG_GCM) {
6002 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6003 SIZE_MAX),
6004 PSA_ERROR_INVALID_ARGUMENT);
6005 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6006 PSA_ERROR_BAD_STATE);
6007 } else if (operation.alg != PSA_ALG_CCM) {
6008 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6009 SIZE_MAX));
6010 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006011 }
6012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006014#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006015
Tom Cosgrove1797b052022-12-04 17:19:59 +00006016 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006017#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 if (operation.alg == PSA_ALG_GCM) {
6023 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6024 SIZE_MAX),
6025 PSA_ERROR_INVALID_ARGUMENT);
6026 } else if (operation.alg != PSA_ALG_CCM) {
6027 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6028 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006029 }
6030
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006032#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006033
6034 /* ------------------------------------------------------- */
6035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6041 PSA_AEAD_NONCE_MAX_SIZE,
6042 &nonce_length),
6043 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006044
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006046
Paul Elliott7220cae2021-06-22 17:25:57 +01006047 /* Test for generating nonce in decrypt setup. */
6048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6052 PSA_AEAD_NONCE_MAX_SIZE,
6053 &nonce_length),
6054 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006057
Paul Elliottc23a9a02021-06-21 18:32:46 +01006058 /* Test for setting lengths twice. */
6059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6065 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6068 input_data->len),
6069 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006070
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006072
Andrzej Kurekad837522021-12-15 15:28:49 +01006073 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006078
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006080
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6082 additional_data->len),
6083 PSA_ERROR_BAD_STATE);
6084 } else {
6085 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6086 additional_data->len));
6087
6088 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6089 input_data->len),
6090 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006091 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006093
6094 /* ------------------------------------------------------- */
6095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006099
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 if (operation.alg == PSA_ALG_CCM) {
6101 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6102 input_data->len, output_data,
6103 output_size, &output_length),
6104 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 } else {
6107 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6108 input_data->len, output_data,
6109 output_size, &output_length));
6110
6111 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6112 input_data->len),
6113 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006114 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006116
6117 /* ------------------------------------------------------- */
6118
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006120
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 if (operation.alg == PSA_ALG_CCM) {
6124 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6125 finish_output_size,
6126 &output_part_length,
6127 tag_buffer, tag_length,
6128 &tag_size));
6129 } else {
6130 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6131 finish_output_size,
6132 &output_part_length,
6133 tag_buffer, tag_length,
6134 &tag_size));
6135
6136 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6137 input_data->len),
6138 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006139 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006141
6142 /* Test for setting lengths after generating nonce + already starting data. */
6143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6147 PSA_AEAD_NONCE_MAX_SIZE,
6148 &nonce_length));
6149 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6152 additional_data->len),
6153 PSA_ERROR_BAD_STATE);
6154 } else {
6155 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6156 additional_data->len));
6157
6158 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6159 input_data->len),
6160 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006161 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006163
6164 /* ------------------------------------------------------- */
6165
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6169 PSA_AEAD_NONCE_MAX_SIZE,
6170 &nonce_length));
6171 if (operation.alg == PSA_ALG_CCM) {
6172 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6173 input_data->len, output_data,
6174 output_size, &output_length),
6175 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 } else {
6178 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6179 input_data->len, output_data,
6180 output_size, &output_length));
6181
6182 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6183 input_data->len),
6184 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006185 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006187
6188 /* ------------------------------------------------------- */
6189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6193 PSA_AEAD_NONCE_MAX_SIZE,
6194 &nonce_length));
6195 if (operation.alg == PSA_ALG_CCM) {
6196 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6197 finish_output_size,
6198 &output_part_length,
6199 tag_buffer, tag_length,
6200 &tag_size));
6201 } else {
6202 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6203 finish_output_size,
6204 &output_part_length,
6205 tag_buffer, tag_length,
6206 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6209 input_data->len),
6210 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006211 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006213
Paul Elliott243080c2021-07-21 19:01:17 +01006214 /* Test for not sending any additional data or data after setting non zero
6215 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6222 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6225 finish_output_size,
6226 &output_part_length,
6227 tag_buffer, tag_length,
6228 &tag_size),
6229 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006230
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006232
Paul Elliott243080c2021-07-21 19:01:17 +01006233 /* Test for not sending any additional data or data after setting non-zero
6234 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006235
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006239
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6241 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006242
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6244 finish_output_size,
6245 &output_part_length,
6246 tag_buffer,
6247 tag_length),
6248 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006251
Paul Elliott243080c2021-07-21 19:01:17 +01006252 /* Test for not sending any additional data after setting a non-zero length
6253 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6260 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6263 input_data->len, output_data,
6264 output_size, &output_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 Elliottf94bd992021-09-19 18:15:59 +01006269 /* Test for not sending any data after setting a non-zero length for it.*/
6270
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6276 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6279 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6282 finish_output_size,
6283 &output_part_length,
6284 tag_buffer, tag_length,
6285 &tag_size),
6286 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006289
Paul Elliottb0450fe2021-09-01 15:06:26 +01006290 /* Test for sending too much additional data after setting lengths. */
6291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006297
6298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6300 additional_data->len),
6301 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006304
Paul Elliotta2a09b02021-09-22 14:56:40 +01006305 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6312 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6315 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6318 1),
6319 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006322
Paul Elliottb0450fe2021-09-01 15:06:26 +01006323 /* Test for sending too much data after setting lengths. */
6324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6332 input_data->len, output_data,
6333 output_size, &output_length),
6334 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006335
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006337
Paul Elliotta2a09b02021-09-22 14:56:40 +01006338 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6345 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6348 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6351 input_data->len, output_data,
6352 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6355 1, output_data,
6356 output_size, &output_length),
6357 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006358
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006360
Paul Elliottc23a9a02021-06-21 18:32:46 +01006361 /* Test sending additional data after data. */
6362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 if (operation.alg != PSA_ALG_CCM) {
6368 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6369 input_data->len, output_data,
6370 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006371
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6373 additional_data->len),
6374 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006375 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006377
Paul Elliott534d0b42021-06-22 19:15:20 +01006378 /* Test calling finish on decryption. */
6379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6385 finish_output_size,
6386 &output_part_length,
6387 tag_buffer, tag_length,
6388 &tag_size),
6389 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006392
6393 /* Test calling verify on encryption. */
6394
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6400 finish_output_size,
6401 &output_part_length,
6402 tag_buffer,
6403 tag_length),
6404 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006407
6408
Paul Elliottc23a9a02021-06-21 18:32:46 +01006409exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 psa_destroy_key(key);
6411 psa_aead_abort(&operation);
6412 mbedtls_free(output_data);
6413 mbedtls_free(final_data);
6414 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006415}
6416/* END_CASE */
6417
6418/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006419void signature_size(int type_arg,
6420 int bits,
6421 int alg_arg,
6422 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006423{
6424 psa_key_type_t type = type_arg;
6425 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006427
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006429
Gilles Peskinee59236f2018-01-27 23:32:46 +01006430exit:
6431 ;
6432}
6433/* END_CASE */
6434
6435/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006436void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6437 int alg_arg, data_t *input_data,
6438 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006439{
Ronald Cron5425a212020-08-04 14:58:35 +02006440 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006441 psa_key_type_t key_type = key_type_arg;
6442 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006443 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006444 unsigned char *signature = NULL;
6445 size_t signature_size;
6446 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006450
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6452 psa_set_key_algorithm(&attributes, alg);
6453 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6456 &key));
6457 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6458 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006459
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006460 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006461 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6463 key_bits, alg);
6464 TEST_ASSERT(signature_size != 0);
6465 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6466 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006467
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006468 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 PSA_ASSERT(psa_sign_hash(key, alg,
6470 input_data->x, input_data->len,
6471 signature, signature_size,
6472 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006473 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 ASSERT_COMPARE(output_data->x, output_data->len,
6475 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006476
6477exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006478 /*
6479 * Key attributes may have been returned by psa_get_key_attributes()
6480 * thus reset them as required.
6481 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006483
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 psa_destroy_key(key);
6485 mbedtls_free(signature);
6486 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006487}
6488/* END_CASE */
6489
Paul Elliott712d5122022-12-07 14:03:10 +00006490/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006491/**
6492 * sign_hash_interruptible() test intentions:
6493 *
6494 * Note: This test can currently only handle ECDSA.
6495 *
6496 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006497 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006498 *
6499 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6500 * expected for different max_ops values.
6501 *
6502 * 3. Test that the number of ops done prior to start and after abort is zero
6503 * and that each successful stage completes some ops (this is not mandated by
6504 * the PSA specification, but is currently the case).
6505 *
6506 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6507 * complete() calls does not alter the number of ops returned.
6508 */
Paul Elliott712d5122022-12-07 14:03:10 +00006509void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6510 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006511 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006512{
6513 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6514 psa_key_type_t key_type = key_type_arg;
6515 psa_algorithm_t alg = alg_arg;
6516 size_t key_bits;
6517 unsigned char *signature = NULL;
6518 size_t signature_size;
6519 size_t signature_length = 0xdeadbeef;
6520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6521 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006522 uint32_t num_ops = 0;
6523 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006524 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006525 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006526 size_t min_completes = 0;
6527 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006528
Paul Elliott712d5122022-12-07 14:03:10 +00006529 psa_sign_hash_interruptible_operation_t operation =
6530 psa_sign_hash_interruptible_operation_init();
6531
6532 PSA_ASSERT(psa_crypto_init());
6533
6534 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6535 psa_set_key_algorithm(&attributes, alg);
6536 psa_set_key_type(&attributes, key_type);
6537
6538 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6539 &key));
6540 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6541 key_bits = psa_get_key_bits(&attributes);
6542
6543 /* Allocate a buffer which has the size advertised by the
6544 * library. */
6545 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6546 key_bits, alg);
6547 TEST_ASSERT(signature_size != 0);
6548 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6549 ASSERT_ALLOC(signature, signature_size);
6550
Paul Elliott0c683352022-12-16 19:16:56 +00006551 psa_interruptible_set_max_ops(max_ops);
6552
Paul Elliott6f600372023-02-06 18:41:05 +00006553 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6554 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006555
Paul Elliott712d5122022-12-07 14:03:10 +00006556 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6557 TEST_ASSERT(num_ops_prior == 0);
6558
6559 /* Start performing the signature. */
6560 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6561 input_data->x, input_data->len));
6562
6563 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6564 TEST_ASSERT(num_ops_prior == 0);
6565
6566 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006567 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006568 status = psa_sign_hash_complete(&operation, signature, signature_size,
6569 &signature_length);
6570
Paul Elliott0c683352022-12-16 19:16:56 +00006571 num_completes++;
6572
Paul Elliott712d5122022-12-07 14:03:10 +00006573 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6574 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006575 /* We are asserting here that every complete makes progress
6576 * (completes some ops), which is true of the internal
6577 * implementation and probably any implementation, however this is
6578 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006579 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006580
Paul Elliott712d5122022-12-07 14:03:10 +00006581 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006582
6583 /* Ensure calling get_num_ops() twice still returns the same
6584 * number of ops as previously reported. */
6585 num_ops = psa_sign_hash_get_num_ops(&operation);
6586
6587 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006588 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006589 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006590
6591 TEST_ASSERT(status == PSA_SUCCESS);
6592
Paul Elliott0c683352022-12-16 19:16:56 +00006593 TEST_LE_U(min_completes, num_completes);
6594 TEST_LE_U(num_completes, max_completes);
6595
Paul Elliott712d5122022-12-07 14:03:10 +00006596 /* Verify that the signature is what is expected. */
6597 ASSERT_COMPARE(output_data->x, output_data->len,
6598 signature, signature_length);
6599
6600 PSA_ASSERT(psa_sign_hash_abort(&operation));
6601
Paul Elliott59ad9452022-12-18 15:09:02 +00006602 num_ops = psa_sign_hash_get_num_ops(&operation);
6603 TEST_ASSERT(num_ops == 0);
6604
Paul Elliott712d5122022-12-07 14:03:10 +00006605exit:
6606
6607 /*
6608 * Key attributes may have been returned by psa_get_key_attributes()
6609 * thus reset them as required.
6610 */
6611 psa_reset_key_attributes(&attributes);
6612
6613 psa_destroy_key(key);
6614 mbedtls_free(signature);
6615 PSA_DONE();
6616}
6617/* END_CASE */
6618
Gilles Peskine20035e32018-02-03 22:44:14 +01006619/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006620void sign_hash_fail(int key_type_arg, data_t *key_data,
6621 int alg_arg, data_t *input_data,
6622 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006623{
Ronald Cron5425a212020-08-04 14:58:35 +02006624 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006625 psa_key_type_t key_type = key_type_arg;
6626 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006627 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006628 psa_status_t actual_status;
6629 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006630 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006631 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006632 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006633
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006635
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006637
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6639 psa_set_key_algorithm(&attributes, alg);
6640 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6643 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006644
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 actual_status = psa_sign_hash(key, alg,
6646 input_data->x, input_data->len,
6647 signature, signature_size,
6648 &signature_length);
6649 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006650 /* The value of *signature_length is unspecified on error, but
6651 * whatever it is, it should be less than signature_size, so that
6652 * if the caller tries to read *signature_length bytes without
6653 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006655
6656exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 psa_reset_key_attributes(&attributes);
6658 psa_destroy_key(key);
6659 mbedtls_free(signature);
6660 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006661}
6662/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006663
Paul Elliott91007972022-12-16 12:21:24 +00006664/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006665/**
6666 * sign_hash_fail_interruptible() test intentions:
6667 *
6668 * Note: This test can currently only handle ECDSA.
6669 *
6670 * 1. Test that various failure cases for interruptible sign hash fail with the
6671 * correct error codes, and at the correct point (at start or during
6672 * complete).
6673 *
6674 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6675 * expected for different max_ops values.
6676 *
6677 * 3. Test that the number of ops done prior to start and after abort is zero
6678 * and that each successful stage completes some ops (this is not mandated by
6679 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006680 *
6681 * 4. Check that calling complete() when start() fails and complete()
6682 * after completion results in a BAD_STATE error.
6683 *
6684 * 5. Check that calling start() again after start fails results in a BAD_STATE
6685 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006686 */
Paul Elliott91007972022-12-16 12:21:24 +00006687void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6688 int alg_arg, data_t *input_data,
6689 int signature_size_arg,
6690 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006691 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006692 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006693{
6694 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6695 psa_key_type_t key_type = key_type_arg;
6696 psa_algorithm_t alg = alg_arg;
6697 size_t signature_size = signature_size_arg;
6698 psa_status_t actual_status;
6699 psa_status_t expected_start_status = expected_start_status_arg;
6700 psa_status_t expected_complete_status = expected_complete_status_arg;
6701 unsigned char *signature = NULL;
6702 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006703 uint32_t num_ops = 0;
6704 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006705 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006706 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006707 size_t min_completes = 0;
6708 size_t max_completes = 0;
6709
Paul Elliott91007972022-12-16 12:21:24 +00006710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6711 psa_sign_hash_interruptible_operation_t operation =
6712 psa_sign_hash_interruptible_operation_init();
6713
6714 ASSERT_ALLOC(signature, signature_size);
6715
6716 PSA_ASSERT(psa_crypto_init());
6717
6718 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6719 psa_set_key_algorithm(&attributes, alg);
6720 psa_set_key_type(&attributes, key_type);
6721
6722 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6723 &key));
6724
Paul Elliott0c683352022-12-16 19:16:56 +00006725 psa_interruptible_set_max_ops(max_ops);
6726
Paul Elliott6f600372023-02-06 18:41:05 +00006727 interruptible_signverify_get_minmax_completes(max_ops,
6728 expected_complete_status,
6729 &min_completes,
6730 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006731
Paul Elliott91007972022-12-16 12:21:24 +00006732 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6733 TEST_ASSERT(num_ops_prior == 0);
6734
6735 /* Start performing the signature. */
6736 actual_status = psa_sign_hash_start(&operation, key, alg,
6737 input_data->x, input_data->len);
6738
6739 TEST_EQUAL(actual_status, expected_start_status);
6740
Paul Elliottc9774412023-02-06 15:14:07 +00006741 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006742 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006743 * start failed. */
6744 actual_status = psa_sign_hash_complete(&operation, signature,
6745 signature_size,
6746 &signature_length);
6747
6748 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6749
6750 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006751 actual_status = psa_sign_hash_start(&operation, key, alg,
6752 input_data->x, input_data->len);
6753
6754 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6755 }
6756
Paul Elliott91007972022-12-16 12:21:24 +00006757 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6758 TEST_ASSERT(num_ops_prior == 0);
6759
Paul Elliott91007972022-12-16 12:21:24 +00006760 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006761 do {
Paul Elliott91007972022-12-16 12:21:24 +00006762 actual_status = psa_sign_hash_complete(&operation, signature,
6763 signature_size,
6764 &signature_length);
6765
Paul Elliott0c683352022-12-16 19:16:56 +00006766 num_completes++;
6767
Paul Elliott334d7262023-01-20 17:29:41 +00006768 if (actual_status == PSA_SUCCESS ||
6769 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006770 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006771 /* We are asserting here that every complete makes progress
6772 * (completes some ops), which is true of the internal
6773 * implementation and probably any implementation, however this is
6774 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006775 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006776
Paul Elliott91007972022-12-16 12:21:24 +00006777 num_ops_prior = num_ops;
6778 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006779 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006780
Paul Elliottc9774412023-02-06 15:14:07 +00006781 TEST_EQUAL(actual_status, expected_complete_status);
6782
Paul Elliottefebad02023-02-15 16:56:45 +00006783 /* Check that another complete returns BAD_STATE. */
6784 actual_status = psa_sign_hash_complete(&operation, signature,
6785 signature_size,
6786 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006787
Paul Elliottefebad02023-02-15 16:56:45 +00006788 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006789
Paul Elliott91007972022-12-16 12:21:24 +00006790 PSA_ASSERT(psa_sign_hash_abort(&operation));
6791
Paul Elliott59ad9452022-12-18 15:09:02 +00006792 num_ops = psa_sign_hash_get_num_ops(&operation);
6793 TEST_ASSERT(num_ops == 0);
6794
Paul Elliott91007972022-12-16 12:21:24 +00006795 /* The value of *signature_length is unspecified on error, but
6796 * whatever it is, it should be less than signature_size, so that
6797 * if the caller tries to read *signature_length bytes without
6798 * checking the error code then they don't overflow a buffer. */
6799 TEST_LE_U(signature_length, signature_size);
6800
Paul Elliott0c683352022-12-16 19:16:56 +00006801 TEST_LE_U(min_completes, num_completes);
6802 TEST_LE_U(num_completes, max_completes);
6803
Paul Elliott91007972022-12-16 12:21:24 +00006804exit:
6805 psa_reset_key_attributes(&attributes);
6806 psa_destroy_key(key);
6807 mbedtls_free(signature);
6808 PSA_DONE();
6809}
6810/* END_CASE */
6811
mohammad16038cc1cee2018-03-28 01:21:33 +03006812/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006813void sign_verify_hash(int key_type_arg, data_t *key_data,
6814 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006815{
Ronald Cron5425a212020-08-04 14:58:35 +02006816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006817 psa_key_type_t key_type = key_type_arg;
6818 psa_algorithm_t alg = alg_arg;
6819 size_t key_bits;
6820 unsigned char *signature = NULL;
6821 size_t signature_size;
6822 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006823 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006824
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006826
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6828 psa_set_key_algorithm(&attributes, alg);
6829 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006830
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6832 &key));
6833 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6834 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006835
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006836 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006837 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6839 key_bits, alg);
6840 TEST_ASSERT(signature_size != 0);
6841 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6842 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006843
6844 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 PSA_ASSERT(psa_sign_hash(key, alg,
6846 input_data->x, input_data->len,
6847 signature, signature_size,
6848 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006849 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006850 TEST_LE_U(signature_length, signature_size);
6851 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006852
6853 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 PSA_ASSERT(psa_verify_hash(key, alg,
6855 input_data->x, input_data->len,
6856 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006857
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006859 /* Flip a bit in the input and verify that the signature is now
6860 * detected as invalid. Flip a bit at the beginning, not at the end,
6861 * because ECDSA may ignore the last few bits of the input. */
6862 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 TEST_EQUAL(psa_verify_hash(key, alg,
6864 input_data->x, input_data->len,
6865 signature, signature_length),
6866 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006867 }
6868
6869exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006870 /*
6871 * Key attributes may have been returned by psa_get_key_attributes()
6872 * thus reset them as required.
6873 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 psa_destroy_key(key);
6877 mbedtls_free(signature);
6878 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006879}
6880/* END_CASE */
6881
Paul Elliott712d5122022-12-07 14:03:10 +00006882/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006883/**
6884 * sign_verify_hash_interruptible() test intentions:
6885 *
6886 * Note: This test can currently only handle ECDSA.
6887 *
Paul Elliott8c092052023-03-06 17:49:14 +00006888 * 1. Test that we can sign an input hash with the given keypair and then
6889 * afterwards verify that signature. This is currently the only way to test
6890 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006891 *
6892 * 2. Test that after corrupting the hash, the verification detects an invalid
6893 * signature.
6894 *
6895 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6896 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006897 *
6898 * 4. Test that the number of ops done prior to starting signing and after abort
6899 * is zero and that each successful signing stage completes some ops (this is
6900 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006901 */
Paul Elliott712d5122022-12-07 14:03:10 +00006902void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006903 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006904 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006905{
6906 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6907 psa_key_type_t key_type = key_type_arg;
6908 psa_algorithm_t alg = alg_arg;
6909 size_t key_bits;
6910 unsigned char *signature = NULL;
6911 size_t signature_size;
6912 size_t signature_length = 0xdeadbeef;
6913 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6914 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006915 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006916 uint32_t num_ops = 0;
6917 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006918 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006919 size_t min_completes = 0;
6920 size_t max_completes = 0;
6921
Paul Elliott712d5122022-12-07 14:03:10 +00006922 psa_sign_hash_interruptible_operation_t sign_operation =
6923 psa_sign_hash_interruptible_operation_init();
6924 psa_verify_hash_interruptible_operation_t verify_operation =
6925 psa_verify_hash_interruptible_operation_init();
6926
6927 PSA_ASSERT(psa_crypto_init());
6928
Paul Elliott0c683352022-12-16 19:16:56 +00006929 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6930 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006931 psa_set_key_algorithm(&attributes, alg);
6932 psa_set_key_type(&attributes, key_type);
6933
6934 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6935 &key));
6936 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6937 key_bits = psa_get_key_bits(&attributes);
6938
6939 /* Allocate a buffer which has the size advertised by the
6940 * library. */
6941 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6942 key_bits, alg);
6943 TEST_ASSERT(signature_size != 0);
6944 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6945 ASSERT_ALLOC(signature, signature_size);
6946
Paul Elliott0c683352022-12-16 19:16:56 +00006947 psa_interruptible_set_max_ops(max_ops);
6948
Paul Elliott6f600372023-02-06 18:41:05 +00006949 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6950 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006951
Paul Elliott7c173082023-02-26 18:44:45 +00006952 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6953 TEST_ASSERT(num_ops_prior == 0);
6954
Paul Elliott712d5122022-12-07 14:03:10 +00006955 /* Start performing the signature. */
6956 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6957 input_data->x, input_data->len));
6958
Paul Elliott7c173082023-02-26 18:44:45 +00006959 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6960 TEST_ASSERT(num_ops_prior == 0);
6961
Paul Elliott712d5122022-12-07 14:03:10 +00006962 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006963 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006964
Paul Elliott0c683352022-12-16 19:16:56 +00006965 status = psa_sign_hash_complete(&sign_operation, signature,
6966 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006967 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006968
6969 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006970
6971 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6972 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6973 /* We are asserting here that every complete makes progress
6974 * (completes some ops), which is true of the internal
6975 * implementation and probably any implementation, however this is
6976 * not mandated by the PSA specification. */
6977 TEST_ASSERT(num_ops > num_ops_prior);
6978
6979 num_ops_prior = num_ops;
6980 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006981 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006982
6983 TEST_ASSERT(status == PSA_SUCCESS);
6984
Paul Elliott0c683352022-12-16 19:16:56 +00006985 TEST_LE_U(min_completes, num_completes);
6986 TEST_LE_U(num_completes, max_completes);
6987
Paul Elliott712d5122022-12-07 14:03:10 +00006988 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6989
Paul Elliott7c173082023-02-26 18:44:45 +00006990 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6991 TEST_ASSERT(num_ops == 0);
6992
Paul Elliott712d5122022-12-07 14:03:10 +00006993 /* Check that the signature length looks sensible. */
6994 TEST_LE_U(signature_length, signature_size);
6995 TEST_ASSERT(signature_length > 0);
6996
Paul Elliott0c683352022-12-16 19:16:56 +00006997 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006998
6999 /* Start verification. */
7000 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7001 input_data->x, input_data->len,
7002 signature, signature_length));
7003
7004 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007005 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007006 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007007
7008 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007009 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007010
7011 TEST_ASSERT(status == PSA_SUCCESS);
7012
Paul Elliott0c683352022-12-16 19:16:56 +00007013 TEST_LE_U(min_completes, num_completes);
7014 TEST_LE_U(num_completes, max_completes);
7015
Paul Elliott712d5122022-12-07 14:03:10 +00007016 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7017
7018 verify_operation = psa_verify_hash_interruptible_operation_init();
7019
7020 if (input_data->len != 0) {
7021 /* Flip a bit in the input and verify that the signature is now
7022 * detected as invalid. Flip a bit at the beginning, not at the end,
7023 * because ECDSA may ignore the last few bits of the input. */
7024 input_data->x[0] ^= 1;
7025
Paul Elliott712d5122022-12-07 14:03:10 +00007026 /* Start verification. */
7027 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7028 input_data->x, input_data->len,
7029 signature, signature_length));
7030
7031 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007032 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007033 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007034 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007035
7036 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7037 }
7038
7039 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7040
7041exit:
7042 /*
7043 * Key attributes may have been returned by psa_get_key_attributes()
7044 * thus reset them as required.
7045 */
7046 psa_reset_key_attributes(&attributes);
7047
7048 psa_destroy_key(key);
7049 mbedtls_free(signature);
7050 PSA_DONE();
7051}
7052/* END_CASE */
7053
Gilles Peskine9911b022018-06-29 17:30:48 +02007054/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007055void verify_hash(int key_type_arg, data_t *key_data,
7056 int alg_arg, data_t *hash_data,
7057 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007058{
Ronald Cron5425a212020-08-04 14:58:35 +02007059 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007060 psa_key_type_t key_type = key_type_arg;
7061 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007063
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007065
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007067
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7069 psa_set_key_algorithm(&attributes, alg);
7070 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7073 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007074
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 PSA_ASSERT(psa_verify_hash(key, alg,
7076 hash_data->x, hash_data->len,
7077 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007078
itayzafrir5c753392018-05-08 11:18:38 +03007079exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 psa_reset_key_attributes(&attributes);
7081 psa_destroy_key(key);
7082 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007083}
7084/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007085
Paul Elliott712d5122022-12-07 14:03:10 +00007086/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007087/**
7088 * verify_hash_interruptible() test intentions:
7089 *
7090 * Note: This test can currently only handle ECDSA.
7091 *
7092 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007093 * only). Given this test only does verification it can accept public keys as
7094 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007095 *
7096 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7097 * expected for different max_ops values.
7098 *
7099 * 3. Test that the number of ops done prior to start and after abort is zero
7100 * and that each successful stage completes some ops (this is not mandated by
7101 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007102 *
7103 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7104 * complete() calls does not alter the number of ops returned.
7105 *
7106 * 5. Test that after corrupting the hash, the verification detects an invalid
7107 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007108 */
Paul Elliott712d5122022-12-07 14:03:10 +00007109void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7110 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007111 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007112{
7113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7114 psa_key_type_t key_type = key_type_arg;
7115 psa_algorithm_t alg = alg_arg;
7116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7117 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007118 uint32_t num_ops = 0;
7119 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007120 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007121 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007122 size_t min_completes = 0;
7123 size_t max_completes = 0;
7124
Paul Elliott712d5122022-12-07 14:03:10 +00007125 psa_verify_hash_interruptible_operation_t operation =
7126 psa_verify_hash_interruptible_operation_init();
7127
7128 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7129
7130 PSA_ASSERT(psa_crypto_init());
7131
7132 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7133 psa_set_key_algorithm(&attributes, alg);
7134 psa_set_key_type(&attributes, key_type);
7135
7136 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7137 &key));
7138
Paul Elliott0c683352022-12-16 19:16:56 +00007139 psa_interruptible_set_max_ops(max_ops);
7140
Paul Elliott6f600372023-02-06 18:41:05 +00007141 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7142 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007143
Paul Elliott712d5122022-12-07 14:03:10 +00007144 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7145
7146 TEST_ASSERT(num_ops_prior == 0);
7147
7148 /* Start verification. */
7149 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7150 hash_data->x, hash_data->len,
7151 signature_data->x, signature_data->len)
7152 );
7153
7154 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7155
7156 TEST_ASSERT(num_ops_prior == 0);
7157
7158 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007159 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007160 status = psa_verify_hash_complete(&operation);
7161
Paul Elliott0c683352022-12-16 19:16:56 +00007162 num_completes++;
7163
Paul Elliott712d5122022-12-07 14:03:10 +00007164 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7165 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007166 /* We are asserting here that every complete makes progress
7167 * (completes some ops), which is true of the internal
7168 * implementation and probably any implementation, however this is
7169 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007170 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007171
Paul Elliott712d5122022-12-07 14:03:10 +00007172 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007173
7174 /* Ensure calling get_num_ops() twice still returns the same
7175 * number of ops as previously reported. */
7176 num_ops = psa_verify_hash_get_num_ops(&operation);
7177
7178 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007179 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007180 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007181
7182 TEST_ASSERT(status == PSA_SUCCESS);
7183
Paul Elliott0c683352022-12-16 19:16:56 +00007184 TEST_LE_U(min_completes, num_completes);
7185 TEST_LE_U(num_completes, max_completes);
7186
Paul Elliott712d5122022-12-07 14:03:10 +00007187 PSA_ASSERT(psa_verify_hash_abort(&operation));
7188
Paul Elliott59ad9452022-12-18 15:09:02 +00007189 num_ops = psa_verify_hash_get_num_ops(&operation);
7190 TEST_ASSERT(num_ops == 0);
7191
Paul Elliott8359c142023-02-24 18:40:10 +00007192 if (hash_data->len != 0) {
7193 /* Flip a bit in the hash and verify that the signature is now detected
7194 * as invalid. Flip a bit at the beginning, not at the end, because
7195 * ECDSA may ignore the last few bits of the input. */
7196 hash_data->x[0] ^= 1;
7197
7198 /* Start verification. */
7199 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7200 hash_data->x, hash_data->len,
7201 signature_data->x, signature_data->len));
7202
7203 /* Continue performing the signature until complete. */
7204 do {
7205 status = psa_verify_hash_complete(&operation);
7206 } while (status == PSA_OPERATION_INCOMPLETE);
7207
7208 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7209 }
7210
Paul Elliott712d5122022-12-07 14:03:10 +00007211exit:
7212 psa_reset_key_attributes(&attributes);
7213 psa_destroy_key(key);
7214 PSA_DONE();
7215}
7216/* END_CASE */
7217
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007218/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007219void verify_hash_fail(int key_type_arg, data_t *key_data,
7220 int alg_arg, data_t *hash_data,
7221 data_t *signature_data,
7222 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007223{
Ronald Cron5425a212020-08-04 14:58:35 +02007224 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007225 psa_key_type_t key_type = key_type_arg;
7226 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007227 psa_status_t actual_status;
7228 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007229 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007230
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007232
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7234 psa_set_key_algorithm(&attributes, alg);
7235 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7238 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007239
Gilles Peskine449bd832023-01-11 14:50:10 +01007240 actual_status = psa_verify_hash(key, alg,
7241 hash_data->x, hash_data->len,
7242 signature_data->x, signature_data->len);
7243 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007244
7245exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 psa_reset_key_attributes(&attributes);
7247 psa_destroy_key(key);
7248 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007249}
7250/* END_CASE */
7251
Paul Elliott91007972022-12-16 12:21:24 +00007252/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007253/**
7254 * verify_hash_fail_interruptible() test intentions:
7255 *
7256 * Note: This test can currently only handle ECDSA.
7257 *
7258 * 1. Test that various failure cases for interruptible verify hash fail with
7259 * the correct error codes, and at the correct point (at start or during
7260 * complete).
7261 *
7262 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7263 * expected for different max_ops values.
7264 *
7265 * 3. Test that the number of ops done prior to start and after abort is zero
7266 * and that each successful stage completes some ops (this is not mandated by
7267 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007268 *
7269 * 4. Check that calling complete() when start() fails and complete()
7270 * after completion results in a BAD_STATE error.
7271 *
7272 * 5. Check that calling start() again after start fails results in a BAD_STATE
7273 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007274 */
Paul Elliott91007972022-12-16 12:21:24 +00007275void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7276 int alg_arg, data_t *hash_data,
7277 data_t *signature_data,
7278 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007279 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007280 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007281{
7282 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7283 psa_key_type_t key_type = key_type_arg;
7284 psa_algorithm_t alg = alg_arg;
7285 psa_status_t actual_status;
7286 psa_status_t expected_start_status = expected_start_status_arg;
7287 psa_status_t expected_complete_status = expected_complete_status_arg;
7288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007289 uint32_t num_ops = 0;
7290 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007291 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007292 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007293 size_t min_completes = 0;
7294 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007295 psa_verify_hash_interruptible_operation_t operation =
7296 psa_verify_hash_interruptible_operation_init();
7297
7298 PSA_ASSERT(psa_crypto_init());
7299
7300 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7301 psa_set_key_algorithm(&attributes, alg);
7302 psa_set_key_type(&attributes, key_type);
7303
7304 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7305 &key));
7306
Paul Elliott0c683352022-12-16 19:16:56 +00007307 psa_interruptible_set_max_ops(max_ops);
7308
Paul Elliott6f600372023-02-06 18:41:05 +00007309 interruptible_signverify_get_minmax_completes(max_ops,
7310 expected_complete_status,
7311 &min_completes,
7312 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007313
Paul Elliott91007972022-12-16 12:21:24 +00007314 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7315 TEST_ASSERT(num_ops_prior == 0);
7316
7317 /* Start verification. */
7318 actual_status = psa_verify_hash_start(&operation, key, alg,
7319 hash_data->x, hash_data->len,
7320 signature_data->x,
7321 signature_data->len);
7322
7323 TEST_EQUAL(actual_status, expected_start_status);
7324
Paul Elliottc9774412023-02-06 15:14:07 +00007325 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007326 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007327 * start failed. */
7328 actual_status = psa_verify_hash_complete(&operation);
7329
7330 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7331
7332 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007333 actual_status = psa_verify_hash_start(&operation, key, alg,
7334 hash_data->x, hash_data->len,
7335 signature_data->x,
7336 signature_data->len);
7337
7338 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7339 }
7340
Paul Elliott91007972022-12-16 12:21:24 +00007341 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7342 TEST_ASSERT(num_ops_prior == 0);
7343
Paul Elliott91007972022-12-16 12:21:24 +00007344 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007345 do {
Paul Elliott91007972022-12-16 12:21:24 +00007346 actual_status = psa_verify_hash_complete(&operation);
7347
Paul Elliott0c683352022-12-16 19:16:56 +00007348 num_completes++;
7349
Paul Elliott334d7262023-01-20 17:29:41 +00007350 if (actual_status == PSA_SUCCESS ||
7351 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007352 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007353 /* We are asserting here that every complete makes progress
7354 * (completes some ops), which is true of the internal
7355 * implementation and probably any implementation, however this is
7356 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007357 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007358
Paul Elliott91007972022-12-16 12:21:24 +00007359 num_ops_prior = num_ops;
7360 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007361 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007362
Paul Elliottc9774412023-02-06 15:14:07 +00007363 TEST_EQUAL(actual_status, expected_complete_status);
7364
Paul Elliottefebad02023-02-15 16:56:45 +00007365 /* Check that another complete returns BAD_STATE. */
7366 actual_status = psa_verify_hash_complete(&operation);
7367 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007368
Paul Elliott0c683352022-12-16 19:16:56 +00007369 TEST_LE_U(min_completes, num_completes);
7370 TEST_LE_U(num_completes, max_completes);
7371
Paul Elliott91007972022-12-16 12:21:24 +00007372 PSA_ASSERT(psa_verify_hash_abort(&operation));
7373
Paul Elliott59ad9452022-12-18 15:09:02 +00007374 num_ops = psa_verify_hash_get_num_ops(&operation);
7375 TEST_ASSERT(num_ops == 0);
7376
Paul Elliott91007972022-12-16 12:21:24 +00007377exit:
7378 psa_reset_key_attributes(&attributes);
7379 psa_destroy_key(key);
7380 PSA_DONE();
7381}
7382/* END_CASE */
7383
Paul Elliott20a36062022-12-18 13:21:25 +00007384/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007385/**
7386 * interruptible_signverify_hash_state_test() test intentions:
7387 *
7388 * Note: This test can currently only handle ECDSA.
7389 *
7390 * 1. Test that calling the various interruptible sign and verify hash functions
7391 * in incorrect orders returns BAD_STATE errors.
7392 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007393void interruptible_signverify_hash_state_test(int key_type_arg,
7394 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007395{
7396 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7397 psa_key_type_t key_type = key_type_arg;
7398 psa_algorithm_t alg = alg_arg;
7399 size_t key_bits;
7400 unsigned char *signature = NULL;
7401 size_t signature_size;
7402 size_t signature_length = 0xdeadbeef;
7403 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7404 psa_sign_hash_interruptible_operation_t sign_operation =
7405 psa_sign_hash_interruptible_operation_init();
7406 psa_verify_hash_interruptible_operation_t verify_operation =
7407 psa_verify_hash_interruptible_operation_init();
7408
7409 PSA_ASSERT(psa_crypto_init());
7410
7411 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7412 PSA_KEY_USAGE_VERIFY_HASH);
7413 psa_set_key_algorithm(&attributes, alg);
7414 psa_set_key_type(&attributes, key_type);
7415
7416 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7417 &key));
7418 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7419 key_bits = psa_get_key_bits(&attributes);
7420
7421 /* Allocate a buffer which has the size advertised by the
7422 * library. */
7423 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7424 key_bits, alg);
7425 TEST_ASSERT(signature_size != 0);
7426 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7427 ASSERT_ALLOC(signature, signature_size);
7428
7429 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7430
7431 /* --- Attempt completes prior to starts --- */
7432 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7433 signature_size,
7434 &signature_length),
7435 PSA_ERROR_BAD_STATE);
7436
7437 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7438
7439 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7440 PSA_ERROR_BAD_STATE);
7441
7442 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7443
7444 /* --- Aborts in all other places. --- */
7445 psa_sign_hash_abort(&sign_operation);
7446
7447 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7448 input_data->x, input_data->len));
7449
7450 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7451
7452 psa_interruptible_set_max_ops(1);
7453
7454 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7455 input_data->x, input_data->len));
7456
7457 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7458 signature_size,
7459 &signature_length),
7460 PSA_OPERATION_INCOMPLETE);
7461
7462 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7463
7464 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7465
7466 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7467 input_data->x, input_data->len));
7468
7469 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7470 signature_size,
7471 &signature_length));
7472
7473 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7474
7475 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7476
7477 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7478 input_data->x, input_data->len,
7479 signature, signature_length));
7480
7481 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7482
7483 psa_interruptible_set_max_ops(1);
7484
7485 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7486 input_data->x, input_data->len,
7487 signature, signature_length));
7488
7489 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7490 PSA_OPERATION_INCOMPLETE);
7491
7492 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7493
7494 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7495
7496 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7497 input_data->x, input_data->len,
7498 signature, signature_length));
7499
7500 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7501
7502 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7503
7504 /* --- Attempt double starts. --- */
7505
7506 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7507 input_data->x, input_data->len));
7508
7509 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7510 input_data->x, input_data->len),
7511 PSA_ERROR_BAD_STATE);
7512
7513 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7514
7515 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7516 input_data->x, input_data->len,
7517 signature, signature_length));
7518
7519 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7520 input_data->x, input_data->len,
7521 signature, signature_length),
7522 PSA_ERROR_BAD_STATE);
7523
7524 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7525
Paul Elliott76d671a2023-02-07 17:45:18 +00007526exit:
7527 /*
7528 * Key attributes may have been returned by psa_get_key_attributes()
7529 * thus reset them as required.
7530 */
7531 psa_reset_key_attributes(&attributes);
7532
7533 psa_destroy_key(key);
7534 mbedtls_free(signature);
7535 PSA_DONE();
7536}
7537/* END_CASE */
7538
7539/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007540/**
Paul Elliottc2033502023-02-26 17:09:14 +00007541 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007542 *
7543 * Note: This test can currently only handle ECDSA.
7544 *
7545 * 1. Test various edge cases in the interruptible sign and verify hash
7546 * interfaces.
7547 */
Paul Elliottc2033502023-02-26 17:09:14 +00007548void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007549 data_t *key_data, int alg_arg, data_t *input_data)
7550{
7551 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7552 psa_key_type_t key_type = key_type_arg;
7553 psa_algorithm_t alg = alg_arg;
7554 size_t key_bits;
7555 unsigned char *signature = NULL;
7556 size_t signature_size;
7557 size_t signature_length = 0xdeadbeef;
7558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7559 uint8_t *input_buffer = NULL;
7560 psa_sign_hash_interruptible_operation_t sign_operation =
7561 psa_sign_hash_interruptible_operation_init();
7562 psa_verify_hash_interruptible_operation_t verify_operation =
7563 psa_verify_hash_interruptible_operation_init();
7564
7565 PSA_ASSERT(psa_crypto_init());
7566
7567 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7568 PSA_KEY_USAGE_VERIFY_HASH);
7569 psa_set_key_algorithm(&attributes, alg);
7570 psa_set_key_type(&attributes, key_type);
7571
7572 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7573 &key));
7574 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7575 key_bits = psa_get_key_bits(&attributes);
7576
7577 /* Allocate a buffer which has the size advertised by the
7578 * library. */
7579 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7580 key_bits, alg);
7581 TEST_ASSERT(signature_size != 0);
7582 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7583 ASSERT_ALLOC(signature, signature_size);
7584
Paul Elliott20a36062022-12-18 13:21:25 +00007585 /* --- Change function inputs mid run, to cause an error (sign only,
7586 * verify passes all inputs to start. --- */
7587
7588 psa_interruptible_set_max_ops(1);
7589
7590 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7591 input_data->x, input_data->len));
7592
7593 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7594 signature_size,
7595 &signature_length),
7596 PSA_OPERATION_INCOMPLETE);
7597
7598 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7599 0,
7600 &signature_length),
7601 PSA_ERROR_BUFFER_TOO_SMALL);
7602
Paul Elliottc9774412023-02-06 15:14:07 +00007603 /* And test that this invalidates the operation. */
7604 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7605 0,
7606 &signature_length),
7607 PSA_ERROR_BAD_STATE);
7608
Paul Elliott20a36062022-12-18 13:21:25 +00007609 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7610
Paul Elliottf9c91a72023-02-05 18:06:38 +00007611 /* Trash the hash buffer in between start and complete, to ensure
7612 * no reliance on external buffers. */
7613 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7614
7615 input_buffer = mbedtls_calloc(1, input_data->len);
7616 TEST_ASSERT(input_buffer != NULL);
7617
7618 memcpy(input_buffer, input_data->x, input_data->len);
7619
7620 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7621 input_buffer, input_data->len));
7622
7623 memset(input_buffer, '!', input_data->len);
7624 mbedtls_free(input_buffer);
7625 input_buffer = NULL;
7626
7627 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7628 signature_size,
7629 &signature_length));
7630
7631 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7632
7633 input_buffer = mbedtls_calloc(1, input_data->len);
7634 TEST_ASSERT(input_buffer != NULL);
7635
7636 memcpy(input_buffer, input_data->x, input_data->len);
7637
7638 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7639 input_buffer, input_data->len,
7640 signature, signature_length));
7641
7642 memset(input_buffer, '!', input_data->len);
7643 mbedtls_free(input_buffer);
7644 input_buffer = NULL;
7645
7646 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7647
7648 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7649
Paul Elliott20a36062022-12-18 13:21:25 +00007650exit:
7651 /*
7652 * Key attributes may have been returned by psa_get_key_attributes()
7653 * thus reset them as required.
7654 */
7655 psa_reset_key_attributes(&attributes);
7656
7657 psa_destroy_key(key);
7658 mbedtls_free(signature);
7659 PSA_DONE();
7660}
7661/* END_CASE */
7662
Paul Elliotta4cb9092023-02-07 18:01:55 +00007663/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007664/**
Paul Elliott57702242023-02-26 20:36:10 +00007665 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007666 *
7667 * Note: This test can currently only handle ECDSA.
7668 *
7669 * 1. Test that setting max ops is reflected in both interruptible sign and
7670 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007671 * 2. Test that changing the value of max_ops to unlimited during an operation
7672 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007673 *
7674 * 3. Test that calling get_num_ops() between complete calls gives the same
7675 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007676 */
Paul Elliott57702242023-02-26 20:36:10 +00007677void interruptible_signverify_hash_ops_tests(int key_type_arg,
7678 data_t *key_data, int alg_arg,
7679 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007680{
7681 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7682 psa_key_type_t key_type = key_type_arg;
7683 psa_algorithm_t alg = alg_arg;
7684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007685 size_t key_bits;
7686 unsigned char *signature = NULL;
7687 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007688 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007689 uint32_t num_ops = 0;
7690 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7691
Paul Elliotta4cb9092023-02-07 18:01:55 +00007692 psa_sign_hash_interruptible_operation_t sign_operation =
7693 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007694 psa_verify_hash_interruptible_operation_t verify_operation =
7695 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007696
7697 PSA_ASSERT(psa_crypto_init());
7698
7699 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7700 PSA_KEY_USAGE_VERIFY_HASH);
7701 psa_set_key_algorithm(&attributes, alg);
7702 psa_set_key_type(&attributes, key_type);
7703
Paul Elliottf1743e22023-02-15 18:44:16 +00007704 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7705 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7706 key_bits = psa_get_key_bits(&attributes);
7707
7708 /* Allocate a buffer which has the size advertised by the
7709 * library. */
7710 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7711
7712 TEST_ASSERT(signature_size != 0);
7713 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7714 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007715
7716 /* Check that default max ops gets set if we don't set it. */
7717 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7718 input_data->x, input_data->len));
7719
7720 TEST_EQUAL(psa_interruptible_get_max_ops(),
7721 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7722
7723 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7724
Paul Elliottf1743e22023-02-15 18:44:16 +00007725 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7726 input_data->x, input_data->len,
7727 signature, signature_size));
7728
7729 TEST_EQUAL(psa_interruptible_get_max_ops(),
7730 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7731
7732 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7733
Paul Elliotta4cb9092023-02-07 18:01:55 +00007734 /* Check that max ops gets set properly. */
7735
7736 psa_interruptible_set_max_ops(0xbeef);
7737
Paul Elliottf1743e22023-02-15 18:44:16 +00007738 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007739
Paul Elliott9e8819f2023-02-26 19:01:35 +00007740 /* --- Ensure changing the max ops mid operation works (operation should
7741 * complete successfully after setting max ops to unlimited --- */
7742 psa_interruptible_set_max_ops(1);
7743
7744 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7745 input_data->x, input_data->len));
7746
7747 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7748 signature_size,
7749 &signature_length),
7750 PSA_OPERATION_INCOMPLETE);
7751
7752 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7753
7754 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7755 signature_size,
7756 &signature_length));
7757
7758 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7759
7760 psa_interruptible_set_max_ops(1);
7761
7762 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7763 input_data->x, input_data->len,
7764 signature, signature_length));
7765
7766 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7767 PSA_OPERATION_INCOMPLETE);
7768
7769 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7770
7771 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7772
7773 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7774
Paul Elliottc1e04002023-02-26 20:27:23 +00007775 /* --- Test that not calling get_num_ops inbetween complete calls does not
7776 * result in lost ops. ---*/
7777
7778 psa_interruptible_set_max_ops(1);
7779
7780 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7781 input_data->x, input_data->len));
7782
7783 /* Continue performing the signature until complete. */
7784 do {
7785 status = psa_sign_hash_complete(&sign_operation, signature,
7786 signature_size,
7787 &signature_length);
7788
7789 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7790
7791 } while (status == PSA_OPERATION_INCOMPLETE);
7792
7793 PSA_ASSERT(status);
7794
7795 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
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 } while (status == PSA_OPERATION_INCOMPLETE);
7806
7807 PSA_ASSERT(status);
7808
7809 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7810
7811 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7812
7813 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7814 input_data->x, input_data->len,
7815 signature, signature_length));
7816
7817 /* Continue performing the verification until complete. */
7818 do {
7819 status = psa_verify_hash_complete(&verify_operation);
7820
7821 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7822
7823 } while (status == PSA_OPERATION_INCOMPLETE);
7824
7825 PSA_ASSERT(status);
7826
7827 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7828
7829 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7830 input_data->x, input_data->len,
7831 signature, signature_length));
7832
7833 /* Continue performing the verification until complete. */
7834 do {
7835 status = psa_verify_hash_complete(&verify_operation);
7836
7837 } while (status == PSA_OPERATION_INCOMPLETE);
7838
7839 PSA_ASSERT(status);
7840
7841 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7842
7843 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7844
Paul Elliotta4cb9092023-02-07 18:01:55 +00007845exit:
7846 /*
7847 * Key attributes may have been returned by psa_get_key_attributes()
7848 * thus reset them as required.
7849 */
7850 psa_reset_key_attributes(&attributes);
7851
7852 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007853 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007854 PSA_DONE();
7855}
7856/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007857
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007858/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007859void sign_message_deterministic(int key_type_arg,
7860 data_t *key_data,
7861 int alg_arg,
7862 data_t *input_data,
7863 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007864{
7865 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7866 psa_key_type_t key_type = key_type_arg;
7867 psa_algorithm_t alg = alg_arg;
7868 size_t key_bits;
7869 unsigned char *signature = NULL;
7870 size_t signature_size;
7871 size_t signature_length = 0xdeadbeef;
7872 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7873
Gilles Peskine449bd832023-01-11 14:50:10 +01007874 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007875
Gilles Peskine449bd832023-01-11 14:50:10 +01007876 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7877 psa_set_key_algorithm(&attributes, alg);
7878 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007879
Gilles Peskine449bd832023-01-11 14:50:10 +01007880 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7881 &key));
7882 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7883 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007884
Gilles Peskine449bd832023-01-11 14:50:10 +01007885 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7886 TEST_ASSERT(signature_size != 0);
7887 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7888 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007889
Gilles Peskine449bd832023-01-11 14:50:10 +01007890 PSA_ASSERT(psa_sign_message(key, alg,
7891 input_data->x, input_data->len,
7892 signature, signature_size,
7893 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007894
Gilles Peskine449bd832023-01-11 14:50:10 +01007895 ASSERT_COMPARE(output_data->x, output_data->len,
7896 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007897
7898exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007899 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007900
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 psa_destroy_key(key);
7902 mbedtls_free(signature);
7903 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007904
7905}
7906/* END_CASE */
7907
7908/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007909void sign_message_fail(int key_type_arg,
7910 data_t *key_data,
7911 int alg_arg,
7912 data_t *input_data,
7913 int signature_size_arg,
7914 int expected_status_arg)
7915{
7916 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7917 psa_key_type_t key_type = key_type_arg;
7918 psa_algorithm_t alg = alg_arg;
7919 size_t signature_size = signature_size_arg;
7920 psa_status_t actual_status;
7921 psa_status_t expected_status = expected_status_arg;
7922 unsigned char *signature = NULL;
7923 size_t signature_length = 0xdeadbeef;
7924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7925
7926 ASSERT_ALLOC(signature, signature_size);
7927
7928 PSA_ASSERT(psa_crypto_init());
7929
7930 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7931 psa_set_key_algorithm(&attributes, alg);
7932 psa_set_key_type(&attributes, key_type);
7933
7934 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7935 &key));
7936
7937 actual_status = psa_sign_message(key, alg,
7938 input_data->x, input_data->len,
7939 signature, signature_size,
7940 &signature_length);
7941 TEST_EQUAL(actual_status, expected_status);
7942 /* The value of *signature_length is unspecified on error, but
7943 * whatever it is, it should be less than signature_size, so that
7944 * if the caller tries to read *signature_length bytes without
7945 * checking the error code then they don't overflow a buffer. */
7946 TEST_LE_U(signature_length, signature_size);
7947
7948exit:
7949 psa_reset_key_attributes(&attributes);
7950 psa_destroy_key(key);
7951 mbedtls_free(signature);
7952 PSA_DONE();
7953}
7954/* END_CASE */
7955
7956/* BEGIN_CASE */
7957void sign_verify_message(int key_type_arg,
7958 data_t *key_data,
7959 int alg_arg,
7960 data_t *input_data)
7961{
7962 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7963 psa_key_type_t key_type = key_type_arg;
7964 psa_algorithm_t alg = alg_arg;
7965 size_t key_bits;
7966 unsigned char *signature = NULL;
7967 size_t signature_size;
7968 size_t signature_length = 0xdeadbeef;
7969 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7970
7971 PSA_ASSERT(psa_crypto_init());
7972
7973 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7974 PSA_KEY_USAGE_VERIFY_MESSAGE);
7975 psa_set_key_algorithm(&attributes, alg);
7976 psa_set_key_type(&attributes, key_type);
7977
7978 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7979 &key));
7980 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7981 key_bits = psa_get_key_bits(&attributes);
7982
7983 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7984 TEST_ASSERT(signature_size != 0);
7985 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7986 ASSERT_ALLOC(signature, signature_size);
7987
7988 PSA_ASSERT(psa_sign_message(key, alg,
7989 input_data->x, input_data->len,
7990 signature, signature_size,
7991 &signature_length));
7992 TEST_LE_U(signature_length, signature_size);
7993 TEST_ASSERT(signature_length > 0);
7994
7995 PSA_ASSERT(psa_verify_message(key, alg,
7996 input_data->x, input_data->len,
7997 signature, signature_length));
7998
7999 if (input_data->len != 0) {
8000 /* Flip a bit in the input and verify that the signature is now
8001 * detected as invalid. Flip a bit at the beginning, not at the end,
8002 * because ECDSA may ignore the last few bits of the input. */
8003 input_data->x[0] ^= 1;
8004 TEST_EQUAL(psa_verify_message(key, alg,
8005 input_data->x, input_data->len,
8006 signature, signature_length),
8007 PSA_ERROR_INVALID_SIGNATURE);
8008 }
8009
8010exit:
8011 psa_reset_key_attributes(&attributes);
8012
8013 psa_destroy_key(key);
8014 mbedtls_free(signature);
8015 PSA_DONE();
8016}
8017/* END_CASE */
8018
8019/* BEGIN_CASE */
8020void verify_message(int key_type_arg,
8021 data_t *key_data,
8022 int alg_arg,
8023 data_t *input_data,
8024 data_t *signature_data)
8025{
8026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8027 psa_key_type_t key_type = key_type_arg;
8028 psa_algorithm_t alg = alg_arg;
8029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8030
8031 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8032
8033 PSA_ASSERT(psa_crypto_init());
8034
8035 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8036 psa_set_key_algorithm(&attributes, alg);
8037 psa_set_key_type(&attributes, key_type);
8038
8039 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8040 &key));
8041
8042 PSA_ASSERT(psa_verify_message(key, alg,
8043 input_data->x, input_data->len,
8044 signature_data->x, signature_data->len));
8045
8046exit:
8047 psa_reset_key_attributes(&attributes);
8048 psa_destroy_key(key);
8049 PSA_DONE();
8050}
8051/* END_CASE */
8052
8053/* BEGIN_CASE */
8054void verify_message_fail(int key_type_arg,
8055 data_t *key_data,
8056 int alg_arg,
8057 data_t *hash_data,
8058 data_t *signature_data,
8059 int expected_status_arg)
8060{
8061 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8062 psa_key_type_t key_type = key_type_arg;
8063 psa_algorithm_t alg = alg_arg;
8064 psa_status_t actual_status;
8065 psa_status_t expected_status = expected_status_arg;
8066 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8067
8068 PSA_ASSERT(psa_crypto_init());
8069
8070 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8071 psa_set_key_algorithm(&attributes, alg);
8072 psa_set_key_type(&attributes, key_type);
8073
8074 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8075 &key));
8076
8077 actual_status = psa_verify_message(key, alg,
8078 hash_data->x, hash_data->len,
8079 signature_data->x,
8080 signature_data->len);
8081 TEST_EQUAL(actual_status, expected_status);
8082
8083exit:
8084 psa_reset_key_attributes(&attributes);
8085 psa_destroy_key(key);
8086 PSA_DONE();
8087}
8088/* END_CASE */
8089
8090/* BEGIN_CASE */
8091void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008092 data_t *key_data,
8093 int alg_arg,
8094 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008095 data_t *label,
8096 int expected_output_length_arg,
8097 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008098{
Ronald Cron5425a212020-08-04 14:58:35 +02008099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008100 psa_key_type_t key_type = key_type_arg;
8101 psa_algorithm_t alg = alg_arg;
8102 size_t expected_output_length = expected_output_length_arg;
8103 size_t key_bits;
8104 unsigned char *output = NULL;
8105 size_t output_size;
8106 size_t output_length = ~0;
8107 psa_status_t actual_status;
8108 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008110
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008112
Gilles Peskine656896e2018-06-29 19:12:28 +02008113 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008114 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8115 psa_set_key_algorithm(&attributes, alg);
8116 psa_set_key_type(&attributes, key_type);
8117 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8118 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008119
8120 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008121 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8122 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008123
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8125 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8126 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008127
8128 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008129 actual_status = psa_asymmetric_encrypt(key, alg,
8130 input_data->x, input_data->len,
8131 label->x, label->len,
8132 output, output_size,
8133 &output_length);
8134 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008135 if (actual_status == PSA_SUCCESS) {
8136 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008137 } else {
8138 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008139 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008140
Gilles Peskine68428122018-06-30 18:42:41 +02008141 /* If the label is empty, the test framework puts a non-null pointer
8142 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008144 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008145 if (output_size != 0) {
8146 memset(output, 0, output_size);
8147 }
8148 actual_status = psa_asymmetric_encrypt(key, alg,
8149 input_data->x, input_data->len,
8150 NULL, label->len,
8151 output, output_size,
8152 &output_length);
8153 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008154 if (actual_status == PSA_SUCCESS) {
8155 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008156 } else {
8157 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008158 }
Gilles Peskine68428122018-06-30 18:42:41 +02008159 }
8160
Gilles Peskine656896e2018-06-29 19:12:28 +02008161exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008162 /*
8163 * Key attributes may have been returned by psa_get_key_attributes()
8164 * thus reset them as required.
8165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008166 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008167
Gilles Peskine449bd832023-01-11 14:50:10 +01008168 psa_destroy_key(key);
8169 mbedtls_free(output);
8170 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008171}
8172/* END_CASE */
8173
8174/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008175void asymmetric_encrypt_decrypt(int key_type_arg,
8176 data_t *key_data,
8177 int alg_arg,
8178 data_t *input_data,
8179 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008180{
Ronald Cron5425a212020-08-04 14:58:35 +02008181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008182 psa_key_type_t key_type = key_type_arg;
8183 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008184 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008185 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008186 size_t output_size;
8187 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008188 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008189 size_t output2_size;
8190 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008192
Gilles Peskine449bd832023-01-11 14:50:10 +01008193 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008194
Gilles Peskine449bd832023-01-11 14:50:10 +01008195 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8196 psa_set_key_algorithm(&attributes, alg);
8197 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008198
Gilles Peskine449bd832023-01-11 14:50:10 +01008199 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8200 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008201
8202 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008203 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8204 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008205
Gilles Peskine449bd832023-01-11 14:50:10 +01008206 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8207 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8208 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008209
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008210 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008211 TEST_LE_U(output2_size,
8212 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8213 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8214 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008215
Gilles Peskineeebd7382018-06-08 18:11:54 +02008216 /* We test encryption by checking that encrypt-then-decrypt gives back
8217 * the original plaintext because of the non-optional random
8218 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008219 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8220 input_data->x, input_data->len,
8221 label->x, label->len,
8222 output, output_size,
8223 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008224 /* We don't know what ciphertext length to expect, but check that
8225 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008227
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8229 output, output_length,
8230 label->x, label->len,
8231 output2, output2_size,
8232 &output2_length));
8233 ASSERT_COMPARE(input_data->x, input_data->len,
8234 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008235
8236exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008237 /*
8238 * Key attributes may have been returned by psa_get_key_attributes()
8239 * thus reset them as required.
8240 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008241 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008242
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 psa_destroy_key(key);
8244 mbedtls_free(output);
8245 mbedtls_free(output2);
8246 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008247}
8248/* END_CASE */
8249
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008250/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008251void asymmetric_decrypt(int key_type_arg,
8252 data_t *key_data,
8253 int alg_arg,
8254 data_t *input_data,
8255 data_t *label,
8256 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008257{
Ronald Cron5425a212020-08-04 14:58:35 +02008258 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008259 psa_key_type_t key_type = key_type_arg;
8260 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008261 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008262 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008263 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008264 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008265 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008266
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008268
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8270 psa_set_key_algorithm(&attributes, alg);
8271 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008272
Gilles Peskine449bd832023-01-11 14:50:10 +01008273 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8274 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008275
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8277 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008278
8279 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8281 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8282 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008283
Gilles Peskine449bd832023-01-11 14:50:10 +01008284 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8285 input_data->x, input_data->len,
8286 label->x, label->len,
8287 output,
8288 output_size,
8289 &output_length));
8290 ASSERT_COMPARE(expected_data->x, expected_data->len,
8291 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008292
Gilles Peskine68428122018-06-30 18:42:41 +02008293 /* If the label is empty, the test framework puts a non-null pointer
8294 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008295 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008296 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008297 if (output_size != 0) {
8298 memset(output, 0, output_size);
8299 }
8300 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8301 input_data->x, input_data->len,
8302 NULL, label->len,
8303 output,
8304 output_size,
8305 &output_length));
8306 ASSERT_COMPARE(expected_data->x, expected_data->len,
8307 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008308 }
8309
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 psa_reset_key_attributes(&attributes);
8312 psa_destroy_key(key);
8313 mbedtls_free(output);
8314 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008315}
8316/* END_CASE */
8317
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008318/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008319void asymmetric_decrypt_fail(int key_type_arg,
8320 data_t *key_data,
8321 int alg_arg,
8322 data_t *input_data,
8323 data_t *label,
8324 int output_size_arg,
8325 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008326{
Ronald Cron5425a212020-08-04 14:58:35 +02008327 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008328 psa_key_type_t key_type = key_type_arg;
8329 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008330 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008331 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008332 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008333 psa_status_t actual_status;
8334 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008336
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008338
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008340
Gilles Peskine449bd832023-01-11 14:50:10 +01008341 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8342 psa_set_key_algorithm(&attributes, alg);
8343 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008344
Gilles Peskine449bd832023-01-11 14:50:10 +01008345 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8346 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008347
Gilles Peskine449bd832023-01-11 14:50:10 +01008348 actual_status = psa_asymmetric_decrypt(key, alg,
8349 input_data->x, input_data->len,
8350 label->x, label->len,
8351 output, output_size,
8352 &output_length);
8353 TEST_EQUAL(actual_status, expected_status);
8354 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008355
Gilles Peskine68428122018-06-30 18:42:41 +02008356 /* If the label is empty, the test framework puts a non-null pointer
8357 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008359 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008360 if (output_size != 0) {
8361 memset(output, 0, output_size);
8362 }
8363 actual_status = psa_asymmetric_decrypt(key, alg,
8364 input_data->x, input_data->len,
8365 NULL, label->len,
8366 output, output_size,
8367 &output_length);
8368 TEST_EQUAL(actual_status, expected_status);
8369 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008370 }
8371
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008372exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 psa_reset_key_attributes(&attributes);
8374 psa_destroy_key(key);
8375 mbedtls_free(output);
8376 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008377}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008378/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008379
8380/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008381void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008382{
8383 /* Test each valid way of initializing the object, except for `= {0}`, as
8384 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8385 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008386 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008387 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008388 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008389 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8390 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008391
Gilles Peskine449bd832023-01-11 14:50:10 +01008392 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008393
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008394 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008395 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8396 PSA_ERROR_BAD_STATE);
8397 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8398 PSA_ERROR_BAD_STATE);
8399 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8400 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008401
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008402 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008403 PSA_ASSERT(psa_key_derivation_abort(&func));
8404 PSA_ASSERT(psa_key_derivation_abort(&init));
8405 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008406}
8407/* END_CASE */
8408
Janos Follath16de4a42019-06-13 16:32:24 +01008409/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008410void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008411{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008412 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008413 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008414 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008415
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008417
Gilles Peskine449bd832023-01-11 14:50:10 +01008418 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8419 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008420
8421exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008422 psa_key_derivation_abort(&operation);
8423 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008424}
8425/* END_CASE */
8426
Janos Follathaf3c2a02019-06-12 12:34:34 +01008427/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008428void derive_set_capacity(int alg_arg, int capacity_arg,
8429 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008430{
8431 psa_algorithm_t alg = alg_arg;
8432 size_t capacity = capacity_arg;
8433 psa_status_t expected_status = expected_status_arg;
8434 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8435
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008437
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008439
Gilles Peskine449bd832023-01-11 14:50:10 +01008440 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8441 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008442
8443exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 psa_key_derivation_abort(&operation);
8445 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008446}
8447/* END_CASE */
8448
8449/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008450void derive_input(int alg_arg,
8451 int step_arg1, int key_type_arg1, data_t *input1,
8452 int expected_status_arg1,
8453 int step_arg2, int key_type_arg2, data_t *input2,
8454 int expected_status_arg2,
8455 int step_arg3, int key_type_arg3, data_t *input3,
8456 int expected_status_arg3,
8457 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008458{
8459 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8461 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
8462 psa_status_t expected_statuses[] = { expected_status_arg1,
8463 expected_status_arg2,
8464 expected_status_arg3 };
8465 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008466 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8467 MBEDTLS_SVC_KEY_ID_INIT,
8468 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008469 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8470 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8471 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008472 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008473 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008474 psa_status_t expected_output_status = expected_output_status_arg;
8475 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008476
Gilles Peskine449bd832023-01-11 14:50:10 +01008477 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008478
Gilles Peskine449bd832023-01-11 14:50:10 +01008479 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8480 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008481
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008483
Gilles Peskine449bd832023-01-11 14:50:10 +01008484 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8485 mbedtls_test_set_step(i);
8486 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008487 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8489 psa_set_key_type(&attributes, key_types[i]);
8490 PSA_ASSERT(psa_import_key(&attributes,
8491 inputs[i]->x, inputs[i]->len,
8492 &keys[i]));
8493 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8494 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008495 // When taking a private key as secret input, use key agreement
8496 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8498 &operation, keys[i]),
8499 expected_statuses[i]);
8500 } else {
8501 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8502 keys[i]),
8503 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008504 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 } else {
8506 TEST_EQUAL(psa_key_derivation_input_bytes(
8507 &operation, steps[i],
8508 inputs[i]->x, inputs[i]->len),
8509 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008510 }
8511 }
8512
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 if (output_key_type != PSA_KEY_TYPE_NONE) {
8514 psa_reset_key_attributes(&attributes);
8515 psa_set_key_type(&attributes, output_key_type);
8516 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008517 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008518 psa_key_derivation_output_key(&attributes, &operation,
8519 &output_key);
8520 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008521 uint8_t buffer[1];
8522 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008523 psa_key_derivation_output_bytes(&operation,
8524 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008525 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008527
Janos Follathaf3c2a02019-06-12 12:34:34 +01008528exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 psa_key_derivation_abort(&operation);
8530 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8531 psa_destroy_key(keys[i]);
8532 }
8533 psa_destroy_key(output_key);
8534 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008535}
8536/* END_CASE */
8537
Janos Follathd958bb72019-07-03 15:02:16 +01008538/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008539void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008540{
Janos Follathd958bb72019-07-03 15:02:16 +01008541 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008542 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008543 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008544 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008545 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008546 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008547 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008549 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008551 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8552 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008555
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008557
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8559 psa_set_key_algorithm(&attributes, alg);
8560 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008561
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 PSA_ASSERT(psa_import_key(&attributes,
8563 key_data, sizeof(key_data),
8564 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008565
8566 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008567 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8568 input1, input1_length,
8569 input2, input2_length,
8570 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008571 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008572 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008573
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008574 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8576 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008577
Gilles Peskine449bd832023-01-11 14:50:10 +01008578 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008579
Gilles Peskine449bd832023-01-11 14:50:10 +01008580 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8581 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008582
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 psa_key_derivation_abort(&operation);
8585 psa_destroy_key(key);
8586 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008587}
8588/* END_CASE */
8589
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008590/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008591void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008592{
8593 uint8_t output_buffer[16];
8594 size_t buffer_size = 16;
8595 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008596 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008597
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8599 output_buffer, buffer_size)
8600 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008601
Gilles Peskine449bd832023-01-11 14:50:10 +01008602 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8603 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008604
Gilles Peskine449bd832023-01-11 14:50:10 +01008605 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008606
Gilles Peskine449bd832023-01-11 14:50:10 +01008607 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8608 output_buffer, buffer_size)
8609 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008610
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8612 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008613
8614exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008616}
8617/* END_CASE */
8618
8619/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008620void derive_output(int alg_arg,
8621 int step1_arg, data_t *input1, int expected_status_arg1,
8622 int step2_arg, data_t *input2, int expected_status_arg2,
8623 int step3_arg, data_t *input3, int expected_status_arg3,
8624 int step4_arg, data_t *input4, int expected_status_arg4,
8625 data_t *key_agreement_peer_key,
8626 int requested_capacity_arg,
8627 data_t *expected_output1,
8628 data_t *expected_output2,
8629 int other_key_input_type,
8630 int key_input_type,
8631 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008632{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008633 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8635 data_t *inputs[] = { input1, input2, input3, input4 };
8636 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8637 MBEDTLS_SVC_KEY_ID_INIT,
8638 MBEDTLS_SVC_KEY_ID_INIT,
8639 MBEDTLS_SVC_KEY_ID_INIT };
8640 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8641 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008642 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008643 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008644 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008646 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008648 size_t output_buffer_size = 0;
8649 uint8_t *output_buffer = NULL;
8650 size_t expected_capacity;
8651 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008652 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8653 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8654 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8655 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008656 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008657 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008658 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008659
Gilles Peskine449bd832023-01-11 14:50:10 +01008660 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8661 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008662 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 }
8664 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008665 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008667 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008668 ASSERT_ALLOC(output_buffer, output_buffer_size);
8669 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008670
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008671 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8673 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8674 requested_capacity));
8675 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8676 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008677 case 0:
8678 break;
8679 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008681 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 TEST_EQUAL(psa_key_derivation_input_bytes(
8683 &operation, steps[i],
8684 inputs[i]->x, inputs[i]->len),
8685 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008686
Gilles Peskine449bd832023-01-11 14:50:10 +01008687 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008688 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008689 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008690 break;
8691 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008692 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8693 psa_set_key_algorithm(&attributes1, alg);
8694 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008695
Gilles Peskine449bd832023-01-11 14:50:10 +01008696 PSA_ASSERT(psa_import_key(&attributes1,
8697 inputs[i]->x, inputs[i]->len,
8698 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008699
Gilles Peskine449bd832023-01-11 14:50:10 +01008700 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8701 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8702 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8703 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008704 }
8705
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8707 steps[i],
8708 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008709 break;
8710 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008711 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008712 break;
8713 }
8714 break;
8715 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008717 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8719 steps[i],
8720 inputs[i]->x,
8721 inputs[i]->len),
8722 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008723 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008724 case 1: // input key, type DERIVE
8725 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008726 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8727 psa_set_key_algorithm(&attributes2, alg);
8728 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008729
8730 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008731 if (other_key_input_type == 11) {
8732 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8733 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008734
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 PSA_ASSERT(psa_import_key(&attributes2,
8736 inputs[i]->x, inputs[i]->len,
8737 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008738
Gilles Peskine449bd832023-01-11 14:50:10 +01008739 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8740 steps[i],
8741 keys[i]),
8742 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008743 break;
8744 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008745 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8746 psa_set_key_algorithm(&attributes3, alg);
8747 psa_set_key_type(&attributes3,
8748 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008749
Gilles Peskine449bd832023-01-11 14:50:10 +01008750 PSA_ASSERT(psa_import_key(&attributes3,
8751 inputs[i]->x, inputs[i]->len,
8752 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008753
Gilles Peskine449bd832023-01-11 14:50:10 +01008754 TEST_EQUAL(psa_key_derivation_key_agreement(
8755 &operation,
8756 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8757 keys[i], key_agreement_peer_key->x,
8758 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008759 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008760 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008762 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008763 }
8764
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008766 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008768 break;
8769 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008770 TEST_EQUAL(psa_key_derivation_input_bytes(
8771 &operation, steps[i],
8772 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008773
Gilles Peskine449bd832023-01-11 14:50:10 +01008774 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008775 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008777 break;
8778 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008779 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008780
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8782 &current_capacity));
8783 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008784 expected_capacity = requested_capacity;
8785
Gilles Peskine449bd832023-01-11 14:50:10 +01008786 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008787 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8788
8789 /* For output key derivation secret must be provided using
8790 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008792 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008794
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8796 psa_set_key_algorithm(&attributes4, alg);
8797 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8798 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008799
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8801 &derived_key), expected_status);
8802 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008803 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008805 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 status = psa_key_derivation_output_bytes(&operation,
8807 output_buffer, output_sizes[i]);
8808 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008809 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 TEST_ASSERT(status == PSA_SUCCESS ||
8811 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008812 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 } else if (expected_capacity == 0 ||
8814 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008815 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008817 expected_capacity = 0;
8818 continue;
8819 }
8820 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 PSA_ASSERT(status);
8822 if (output_sizes[i] != 0) {
8823 ASSERT_COMPARE(output_buffer, output_sizes[i],
8824 expected_outputs[i], output_sizes[i]);
8825 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008826 /* Check the operation status. */
8827 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008828 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8829 &current_capacity));
8830 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008831 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008832 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008833 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008834
8835exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008836 mbedtls_free(output_buffer);
8837 psa_key_derivation_abort(&operation);
8838 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8839 psa_destroy_key(keys[i]);
8840 }
8841 psa_destroy_key(derived_key);
8842 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008843}
8844/* END_CASE */
8845
8846/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008847void derive_full(int alg_arg,
8848 data_t *key_data,
8849 data_t *input1,
8850 data_t *input2,
8851 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008852{
Ronald Cron5425a212020-08-04 14:58:35 +02008853 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008854 psa_algorithm_t alg = alg_arg;
8855 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008856 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008857 unsigned char output_buffer[16];
8858 size_t expected_capacity = requested_capacity;
8859 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008861
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008863
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8865 psa_set_key_algorithm(&attributes, alg);
8866 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008867
Gilles Peskine449bd832023-01-11 14:50:10 +01008868 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8869 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008870
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8872 input1->x, input1->len,
8873 input2->x, input2->len,
8874 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008875 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008877
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8879 &current_capacity));
8880 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008881
8882 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 while (current_capacity > 0) {
8884 size_t read_size = sizeof(output_buffer);
8885 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008886 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 }
8888 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8889 output_buffer,
8890 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008891 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8893 &current_capacity));
8894 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008895 }
8896
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008897 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8899 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008900
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008902
8903exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 psa_key_derivation_abort(&operation);
8905 psa_destroy_key(key);
8906 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008907}
8908/* END_CASE */
8909
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008910/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008911void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8912 int derivation_step,
8913 int capacity, int expected_capacity_status_arg,
8914 data_t *expected_output,
8915 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008916{
8917 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8918 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008919 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008920 uint8_t *output_buffer = NULL;
8921 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008922 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8923 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8924 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008925
Gilles Peskine449bd832023-01-11 14:50:10 +01008926 ASSERT_ALLOC(output_buffer, expected_output->len);
8927 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8930 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8931 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008932
Gilles Peskine449bd832023-01-11 14:50:10 +01008933 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8934 step, input->x, input->len),
8935 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008936
Gilles Peskine449bd832023-01-11 14:50:10 +01008937 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008938 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008940
Gilles Peskine449bd832023-01-11 14:50:10 +01008941 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8942 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008943
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 TEST_EQUAL(status, expected_output_status);
8945 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8946 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8947 expected_output->len);
8948 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008949
8950exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008951 mbedtls_free(output_buffer);
8952 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008953 PSA_DONE();
8954}
8955/* END_CASE */
8956
Janos Follathe60c9052019-07-03 13:51:30 +01008957/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008958void derive_key_exercise(int alg_arg,
8959 data_t *key_data,
8960 data_t *input1,
8961 data_t *input2,
8962 int derived_type_arg,
8963 int derived_bits_arg,
8964 int derived_usage_arg,
8965 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008966{
Ronald Cron5425a212020-08-04 14:58:35 +02008967 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8968 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008969 psa_algorithm_t alg = alg_arg;
8970 psa_key_type_t derived_type = derived_type_arg;
8971 size_t derived_bits = derived_bits_arg;
8972 psa_key_usage_t derived_usage = derived_usage_arg;
8973 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008975 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008976 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008977 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008978
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008980
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8982 psa_set_key_algorithm(&attributes, alg);
8983 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8984 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8985 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008986
8987 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008988 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8989 input1->x, input1->len,
8990 input2->x, input2->len,
8991 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008992 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 }
Janos Follathe60c9052019-07-03 13:51:30 +01008994
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 psa_set_key_usage_flags(&attributes, derived_usage);
8996 psa_set_key_algorithm(&attributes, derived_alg);
8997 psa_set_key_type(&attributes, derived_type);
8998 psa_set_key_bits(&attributes, derived_bits);
8999 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9000 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009001
9002 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9004 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9005 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009006
9007 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009009 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009011
9012exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009013 /*
9014 * Key attributes may have been returned by psa_get_key_attributes()
9015 * thus reset them as required.
9016 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009018
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 psa_key_derivation_abort(&operation);
9020 psa_destroy_key(base_key);
9021 psa_destroy_key(derived_key);
9022 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009023}
9024/* END_CASE */
9025
Janos Follath42fd8882019-07-03 14:17:09 +01009026/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009027void derive_key_export(int alg_arg,
9028 data_t *key_data,
9029 data_t *input1,
9030 data_t *input2,
9031 int bytes1_arg,
9032 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009033{
Ronald Cron5425a212020-08-04 14:58:35 +02009034 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9035 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009036 psa_algorithm_t alg = alg_arg;
9037 size_t bytes1 = bytes1_arg;
9038 size_t bytes2 = bytes2_arg;
9039 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009040 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009041 uint8_t *output_buffer = NULL;
9042 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009043 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9044 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009045 size_t length;
9046
Gilles Peskine449bd832023-01-11 14:50:10 +01009047 ASSERT_ALLOC(output_buffer, capacity);
9048 ASSERT_ALLOC(export_buffer, capacity);
9049 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009050
Gilles Peskine449bd832023-01-11 14:50:10 +01009051 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9052 psa_set_key_algorithm(&base_attributes, alg);
9053 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9054 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9055 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009056
9057 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009058 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9059 input1->x, input1->len,
9060 input2->x, input2->len,
9061 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009062 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009063 }
Janos Follath42fd8882019-07-03 14:17:09 +01009064
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9066 output_buffer,
9067 capacity));
9068 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009069
9070 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9072 input1->x, input1->len,
9073 input2->x, input2->len,
9074 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009075 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009076 }
Janos Follath42fd8882019-07-03 14:17:09 +01009077
Gilles Peskine449bd832023-01-11 14:50:10 +01009078 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9079 psa_set_key_algorithm(&derived_attributes, 0);
9080 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9081 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9082 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9083 &derived_key));
9084 PSA_ASSERT(psa_export_key(derived_key,
9085 export_buffer, bytes1,
9086 &length));
9087 TEST_EQUAL(length, bytes1);
9088 PSA_ASSERT(psa_destroy_key(derived_key));
9089 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9090 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9091 &derived_key));
9092 PSA_ASSERT(psa_export_key(derived_key,
9093 export_buffer + bytes1, bytes2,
9094 &length));
9095 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009096
9097 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
9099 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009100
9101exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009102 mbedtls_free(output_buffer);
9103 mbedtls_free(export_buffer);
9104 psa_key_derivation_abort(&operation);
9105 psa_destroy_key(base_key);
9106 psa_destroy_key(derived_key);
9107 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009108}
9109/* END_CASE */
9110
9111/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009112void derive_key_type(int alg_arg,
9113 data_t *key_data,
9114 data_t *input1,
9115 data_t *input2,
9116 int key_type_arg, int bits_arg,
9117 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009118{
9119 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9120 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9121 const psa_algorithm_t alg = alg_arg;
9122 const psa_key_type_t key_type = key_type_arg;
9123 const size_t bits = bits_arg;
9124 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9125 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009127 uint8_t *export_buffer = NULL;
9128 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9129 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9130 size_t export_length;
9131
Gilles Peskine449bd832023-01-11 14:50:10 +01009132 ASSERT_ALLOC(export_buffer, export_buffer_size);
9133 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009134
Gilles Peskine449bd832023-01-11 14:50:10 +01009135 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9136 psa_set_key_algorithm(&base_attributes, alg);
9137 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9138 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9139 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009140
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009142 &operation, base_key, alg,
9143 input1->x, input1->len,
9144 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009145 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009148
Gilles Peskine449bd832023-01-11 14:50:10 +01009149 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9150 psa_set_key_algorithm(&derived_attributes, 0);
9151 psa_set_key_type(&derived_attributes, key_type);
9152 psa_set_key_bits(&derived_attributes, bits);
9153 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9154 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009155
Gilles Peskine449bd832023-01-11 14:50:10 +01009156 PSA_ASSERT(psa_export_key(derived_key,
9157 export_buffer, export_buffer_size,
9158 &export_length));
9159 ASSERT_COMPARE(export_buffer, export_length,
9160 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009161
9162exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009163 mbedtls_free(export_buffer);
9164 psa_key_derivation_abort(&operation);
9165 psa_destroy_key(base_key);
9166 psa_destroy_key(derived_key);
9167 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009168}
9169/* END_CASE */
9170
9171/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009172void derive_key(int alg_arg,
9173 data_t *key_data, data_t *input1, data_t *input2,
9174 int type_arg, int bits_arg,
9175 int expected_status_arg,
9176 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009177{
Ronald Cron5425a212020-08-04 14:58:35 +02009178 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9179 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009180 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009181 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009182 size_t bits = bits_arg;
9183 psa_status_t expected_status = expected_status_arg;
9184 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9185 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9186 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9187
Gilles Peskine449bd832023-01-11 14:50:10 +01009188 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009189
Gilles Peskine449bd832023-01-11 14:50:10 +01009190 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9191 psa_set_key_algorithm(&base_attributes, alg);
9192 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9193 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9194 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009195
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9197 input1->x, input1->len,
9198 input2->x, input2->len,
9199 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009200 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009201 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009202
Gilles Peskine449bd832023-01-11 14:50:10 +01009203 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9204 psa_set_key_algorithm(&derived_attributes, 0);
9205 psa_set_key_type(&derived_attributes, type);
9206 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009207
9208 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 psa_key_derivation_output_key(&derived_attributes,
9210 &operation,
9211 &derived_key);
9212 if (is_large_output > 0) {
9213 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9214 }
9215 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009216
9217exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009218 psa_key_derivation_abort(&operation);
9219 psa_destroy_key(base_key);
9220 psa_destroy_key(derived_key);
9221 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009222}
9223/* END_CASE */
9224
9225/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009226void key_agreement_setup(int alg_arg,
9227 int our_key_type_arg, int our_key_alg_arg,
9228 data_t *our_key_data, data_t *peer_key_data,
9229 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009230{
Ronald Cron5425a212020-08-04 14:58:35 +02009231 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009232 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009233 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009234 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009235 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009237 psa_status_t expected_status = expected_status_arg;
9238 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009239
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009241
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9243 psa_set_key_algorithm(&attributes, our_key_alg);
9244 psa_set_key_type(&attributes, our_key_type);
9245 PSA_ASSERT(psa_import_key(&attributes,
9246 our_key_data->x, our_key_data->len,
9247 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009248
Gilles Peskine77f40d82019-04-11 21:27:06 +02009249 /* The tests currently include inputs that should fail at either step.
9250 * Test cases that fail at the setup step should be changed to call
9251 * key_derivation_setup instead, and this function should be renamed
9252 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009253 status = psa_key_derivation_setup(&operation, alg);
9254 if (status == PSA_SUCCESS) {
9255 TEST_EQUAL(psa_key_derivation_key_agreement(
9256 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9257 our_key,
9258 peer_key_data->x, peer_key_data->len),
9259 expected_status);
9260 } else {
9261 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009262 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009263
9264exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009265 psa_key_derivation_abort(&operation);
9266 psa_destroy_key(our_key);
9267 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009268}
9269/* END_CASE */
9270
9271/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009272void raw_key_agreement(int alg_arg,
9273 int our_key_type_arg, data_t *our_key_data,
9274 data_t *peer_key_data,
9275 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009276{
Ronald Cron5425a212020-08-04 14:58:35 +02009277 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009278 psa_algorithm_t alg = alg_arg;
9279 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009281 unsigned char *output = NULL;
9282 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009283 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009284
Gilles Peskine449bd832023-01-11 14:50:10 +01009285 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009286
Gilles Peskine449bd832023-01-11 14:50:10 +01009287 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9288 psa_set_key_algorithm(&attributes, alg);
9289 psa_set_key_type(&attributes, our_key_type);
9290 PSA_ASSERT(psa_import_key(&attributes,
9291 our_key_data->x, our_key_data->len,
9292 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009293
Gilles Peskine449bd832023-01-11 14:50:10 +01009294 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9295 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009296
Gilles Peskine992bee82022-04-13 23:25:52 +02009297 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009298 TEST_LE_U(expected_output->len,
9299 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9300 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9301 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009302
9303 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 ASSERT_ALLOC(output, expected_output->len);
9305 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9306 peer_key_data->x, peer_key_data->len,
9307 output, expected_output->len,
9308 &output_length));
9309 ASSERT_COMPARE(output, output_length,
9310 expected_output->x, expected_output->len);
9311 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009312 output = NULL;
9313 output_length = ~0;
9314
9315 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 ASSERT_ALLOC(output, expected_output->len + 1);
9317 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9318 peer_key_data->x, peer_key_data->len,
9319 output, expected_output->len + 1,
9320 &output_length));
9321 ASSERT_COMPARE(output, output_length,
9322 expected_output->x, expected_output->len);
9323 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009324 output = NULL;
9325 output_length = ~0;
9326
9327 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009328 ASSERT_ALLOC(output, expected_output->len - 1);
9329 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9330 peer_key_data->x, peer_key_data->len,
9331 output, expected_output->len - 1,
9332 &output_length),
9333 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009334 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009335 TEST_LE_U(output_length, expected_output->len - 1);
9336 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009337 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009338
9339exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009340 mbedtls_free(output);
9341 psa_destroy_key(our_key);
9342 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009343}
9344/* END_CASE */
9345
9346/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009347void key_agreement_capacity(int alg_arg,
9348 int our_key_type_arg, data_t *our_key_data,
9349 data_t *peer_key_data,
9350 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009351{
Ronald Cron5425a212020-08-04 14:58:35 +02009352 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009353 psa_algorithm_t alg = alg_arg;
9354 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009355 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009357 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009358 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009359
Gilles Peskine449bd832023-01-11 14:50:10 +01009360 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009361
Gilles Peskine449bd832023-01-11 14:50:10 +01009362 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9363 psa_set_key_algorithm(&attributes, alg);
9364 psa_set_key_type(&attributes, our_key_type);
9365 PSA_ASSERT(psa_import_key(&attributes,
9366 our_key_data->x, our_key_data->len,
9367 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009368
Gilles Peskine449bd832023-01-11 14:50:10 +01009369 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9370 PSA_ASSERT(psa_key_derivation_key_agreement(
9371 &operation,
9372 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9373 peer_key_data->x, peer_key_data->len));
9374 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009375 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9377 PSA_KEY_DERIVATION_INPUT_INFO,
9378 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009379 }
Gilles Peskine59685592018-09-18 12:11:34 +02009380
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009381 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009382 PSA_ASSERT(psa_key_derivation_get_capacity(
9383 &operation, &actual_capacity));
9384 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009385
Gilles Peskinebf491972018-10-25 22:36:12 +02009386 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009387 while (actual_capacity > sizeof(output)) {
9388 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9389 output, sizeof(output)));
9390 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009391 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009392 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9393 output, actual_capacity));
9394 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9395 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009396
Gilles Peskine59685592018-09-18 12:11:34 +02009397exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009398 psa_key_derivation_abort(&operation);
9399 psa_destroy_key(our_key);
9400 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009401}
9402/* END_CASE */
9403
9404/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009405void key_agreement_output(int alg_arg,
9406 int our_key_type_arg, data_t *our_key_data,
9407 data_t *peer_key_data,
9408 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009409{
Ronald Cron5425a212020-08-04 14:58:35 +02009410 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009411 psa_algorithm_t alg = alg_arg;
9412 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009413 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009415 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009416
Gilles Peskine449bd832023-01-11 14:50:10 +01009417 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9418 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009419
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009421
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9423 psa_set_key_algorithm(&attributes, alg);
9424 psa_set_key_type(&attributes, our_key_type);
9425 PSA_ASSERT(psa_import_key(&attributes,
9426 our_key_data->x, our_key_data->len,
9427 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009428
Gilles Peskine449bd832023-01-11 14:50:10 +01009429 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9430 PSA_ASSERT(psa_key_derivation_key_agreement(
9431 &operation,
9432 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9433 peer_key_data->x, peer_key_data->len));
9434 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009435 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9437 PSA_KEY_DERIVATION_INPUT_INFO,
9438 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009439 }
Gilles Peskine59685592018-09-18 12:11:34 +02009440
Gilles Peskine449bd832023-01-11 14:50:10 +01009441 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9442 actual_output,
9443 expected_output1->len));
9444 ASSERT_COMPARE(actual_output, expected_output1->len,
9445 expected_output1->x, expected_output1->len);
9446 if (expected_output2->len != 0) {
9447 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9448 actual_output,
9449 expected_output2->len));
9450 ASSERT_COMPARE(actual_output, expected_output2->len,
9451 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009452 }
Gilles Peskine59685592018-09-18 12:11:34 +02009453
9454exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009455 psa_key_derivation_abort(&operation);
9456 psa_destroy_key(our_key);
9457 PSA_DONE();
9458 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009459}
9460/* END_CASE */
9461
9462/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009463void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009464{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009465 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009466 unsigned char *output = NULL;
9467 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009468 size_t i;
9469 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009470
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009472
Gilles Peskine449bd832023-01-11 14:50:10 +01009473 ASSERT_ALLOC(output, bytes);
9474 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009475
Gilles Peskine449bd832023-01-11 14:50:10 +01009476 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009477
Gilles Peskinea50d7392018-06-21 10:22:13 +02009478 /* Run several times, to ensure that every output byte will be
9479 * nonzero at least once with overwhelming probability
9480 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009481 for (run = 0; run < 10; run++) {
9482 if (bytes != 0) {
9483 memset(output, 0, bytes);
9484 }
9485 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009486
Gilles Peskine449bd832023-01-11 14:50:10 +01009487 for (i = 0; i < bytes; i++) {
9488 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009489 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009490 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009491 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009492 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009493
9494 /* Check that every byte was changed to nonzero at least once. This
9495 * validates that psa_generate_random is overwriting every byte of
9496 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009497 for (i = 0; i < bytes; i++) {
9498 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009499 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009500
9501exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009502 PSA_DONE();
9503 mbedtls_free(output);
9504 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009505}
9506/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009507
9508/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009509void generate_key(int type_arg,
9510 int bits_arg,
9511 int usage_arg,
9512 int alg_arg,
9513 int expected_status_arg,
9514 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009515{
Ronald Cron5425a212020-08-04 14:58:35 +02009516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009517 psa_key_type_t type = type_arg;
9518 psa_key_usage_t usage = usage_arg;
9519 size_t bits = bits_arg;
9520 psa_algorithm_t alg = alg_arg;
9521 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009522 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009523 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009524
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009526
Gilles Peskine449bd832023-01-11 14:50:10 +01009527 psa_set_key_usage_flags(&attributes, usage);
9528 psa_set_key_algorithm(&attributes, alg);
9529 psa_set_key_type(&attributes, type);
9530 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009531
9532 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009533 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009534
Gilles Peskine449bd832023-01-11 14:50:10 +01009535 if (is_large_key > 0) {
9536 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9537 }
9538 TEST_EQUAL(status, expected_status);
9539 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009540 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009542
9543 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009544 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9545 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9546 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009547
Gilles Peskine818ca122018-06-20 18:16:48 +02009548 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009549 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009550 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009551 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009552
9553exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009554 /*
9555 * Key attributes may have been returned by psa_get_key_attributes()
9556 * thus reset them as required.
9557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009558 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009559
Gilles Peskine449bd832023-01-11 14:50:10 +01009560 psa_destroy_key(key);
9561 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009562}
9563/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009564
Ronald Cronee414c72021-03-18 18:50:08 +01009565/* 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 +01009566void generate_key_rsa(int bits_arg,
9567 data_t *e_arg,
9568 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009569{
Ronald Cron5425a212020-08-04 14:58:35 +02009570 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009571 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009572 size_t bits = bits_arg;
9573 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9574 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9575 psa_status_t expected_status = expected_status_arg;
9576 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9577 uint8_t *exported = NULL;
9578 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009580 size_t exported_length = SIZE_MAX;
9581 uint8_t *e_read_buffer = NULL;
9582 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009583 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009584 size_t e_read_length = SIZE_MAX;
9585
Gilles Peskine449bd832023-01-11 14:50:10 +01009586 if (e_arg->len == 0 ||
9587 (e_arg->len == 3 &&
9588 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009589 is_default_public_exponent = 1;
9590 e_read_size = 0;
9591 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009592 ASSERT_ALLOC(e_read_buffer, e_read_size);
9593 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009594
Gilles Peskine449bd832023-01-11 14:50:10 +01009595 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009596
Gilles Peskine449bd832023-01-11 14:50:10 +01009597 psa_set_key_usage_flags(&attributes, usage);
9598 psa_set_key_algorithm(&attributes, alg);
9599 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9600 e_arg->x, e_arg->len));
9601 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009602
9603 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009604 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9605 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009608
9609 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009610 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9611 TEST_EQUAL(psa_get_key_type(&attributes), type);
9612 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9613 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9614 e_read_buffer, e_read_size,
9615 &e_read_length));
9616 if (is_default_public_exponent) {
9617 TEST_EQUAL(e_read_length, 0);
9618 } else {
9619 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9620 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009621
9622 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009623 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009624 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009625 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009626
9627 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009628 PSA_ASSERT(psa_export_public_key(key,
9629 exported, exported_size,
9630 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009631 {
9632 uint8_t *p = exported;
9633 uint8_t *end = exported + exported_length;
9634 size_t len;
9635 /* RSAPublicKey ::= SEQUENCE {
9636 * modulus INTEGER, -- n
9637 * publicExponent INTEGER } -- e
9638 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009639 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9640 MBEDTLS_ASN1_SEQUENCE |
9641 MBEDTLS_ASN1_CONSTRUCTED));
9642 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9643 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9644 MBEDTLS_ASN1_INTEGER));
9645 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009646 ++p;
9647 --len;
9648 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009649 if (e_arg->len == 0) {
9650 TEST_EQUAL(len, 3);
9651 TEST_EQUAL(p[0], 1);
9652 TEST_EQUAL(p[1], 0);
9653 TEST_EQUAL(p[2], 1);
9654 } else {
9655 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009656 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009657 }
9658
9659exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009660 /*
9661 * Key attributes may have been returned by psa_get_key_attributes() or
9662 * set by psa_set_key_domain_parameters() thus reset them as required.
9663 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009664 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009665
Gilles Peskine449bd832023-01-11 14:50:10 +01009666 psa_destroy_key(key);
9667 PSA_DONE();
9668 mbedtls_free(e_read_buffer);
9669 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009670}
9671/* END_CASE */
9672
Darryl Greend49a4992018-06-18 17:27:26 +01009673/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009674void persistent_key_load_key_from_storage(data_t *data,
9675 int type_arg, int bits_arg,
9676 int usage_flags_arg, int alg_arg,
9677 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009678{
Gilles Peskine449bd832023-01-11 14:50:10 +01009679 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009681 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9682 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009683 psa_key_type_t type = type_arg;
9684 size_t bits = bits_arg;
9685 psa_key_usage_t usage_flags = usage_flags_arg;
9686 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009687 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009688 unsigned char *first_export = NULL;
9689 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009690 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009691 size_t first_exported_length;
9692 size_t second_exported_length;
9693
Gilles Peskine449bd832023-01-11 14:50:10 +01009694 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9695 ASSERT_ALLOC(first_export, export_size);
9696 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009697 }
Darryl Greend49a4992018-06-18 17:27:26 +01009698
Gilles Peskine449bd832023-01-11 14:50:10 +01009699 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009700
Gilles Peskine449bd832023-01-11 14:50:10 +01009701 psa_set_key_id(&attributes, key_id);
9702 psa_set_key_usage_flags(&attributes, usage_flags);
9703 psa_set_key_algorithm(&attributes, alg);
9704 psa_set_key_type(&attributes, type);
9705 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009706
Gilles Peskine449bd832023-01-11 14:50:10 +01009707 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009708 case IMPORT_KEY:
9709 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009710 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9711 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009712 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009713
Darryl Green0c6575a2018-11-07 16:05:30 +00009714 case GENERATE_KEY:
9715 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009716 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009717 break;
9718
9719 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009720#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009721 {
9722 /* Create base key */
9723 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9724 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9725 psa_set_key_usage_flags(&base_attributes,
9726 PSA_KEY_USAGE_DERIVE);
9727 psa_set_key_algorithm(&base_attributes, derive_alg);
9728 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9729 PSA_ASSERT(psa_import_key(&base_attributes,
9730 data->x, data->len,
9731 &base_key));
9732 /* Derive a key. */
9733 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9734 PSA_ASSERT(psa_key_derivation_input_key(
9735 &operation,
9736 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9737 PSA_ASSERT(psa_key_derivation_input_bytes(
9738 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9739 NULL, 0));
9740 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9741 &operation,
9742 &key));
9743 PSA_ASSERT(psa_key_derivation_abort(&operation));
9744 PSA_ASSERT(psa_destroy_key(base_key));
9745 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9746 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009747#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009748 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009749#endif
9750 break;
9751
9752 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009753 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009754 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009755 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009756 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009757
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009758 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009759 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9760 PSA_ASSERT(psa_export_key(key,
9761 first_export, export_size,
9762 &first_exported_length));
9763 if (generation_method == IMPORT_KEY) {
9764 ASSERT_COMPARE(data->x, data->len,
9765 first_export, first_exported_length);
9766 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009767 }
Darryl Greend49a4992018-06-18 17:27:26 +01009768
9769 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009770 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009771 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009772 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009773
Darryl Greend49a4992018-06-18 17:27:26 +01009774 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009775 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9776 TEST_ASSERT(mbedtls_svc_key_id_equal(
9777 psa_get_key_id(&attributes), key_id));
9778 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9779 PSA_KEY_LIFETIME_PERSISTENT);
9780 TEST_EQUAL(psa_get_key_type(&attributes), type);
9781 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9782 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9783 mbedtls_test_update_key_usage_flags(usage_flags));
9784 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009785
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009786 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009787 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9788 PSA_ASSERT(psa_export_key(key,
9789 second_export, export_size,
9790 &second_exported_length));
9791 ASSERT_COMPARE(first_export, first_exported_length,
9792 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009793 }
9794
9795 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009796 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009797 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009798 }
Darryl Greend49a4992018-06-18 17:27:26 +01009799
9800exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009801 /*
9802 * Key attributes may have been returned by psa_get_key_attributes()
9803 * thus reset them as required.
9804 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009805 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009806
Gilles Peskine449bd832023-01-11 14:50:10 +01009807 mbedtls_free(first_export);
9808 mbedtls_free(second_export);
9809 psa_key_derivation_abort(&operation);
9810 psa_destroy_key(base_key);
9811 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009812 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009813}
9814/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009815
Neil Armstronga557cb82022-06-10 08:58:32 +02009816/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009817void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9818 int primitive_arg, int hash_arg, int role_arg,
9819 int test_input, data_t *pw_data,
9820 int inj_err_type_arg,
9821 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009822{
9823 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9824 psa_pake_operation_t operation = psa_pake_operation_init();
9825 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009826 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009827 psa_key_type_t key_type_pw = key_type_pw_arg;
9828 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009829 psa_algorithm_t hash_alg = hash_arg;
9830 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009831 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9832 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009833 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9834 psa_status_t expected_error = expected_error_arg;
9835 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009836 unsigned char *output_buffer = NULL;
9837 size_t output_len = 0;
9838
Gilles Peskine449bd832023-01-11 14:50:10 +01009839 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009840
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009841 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009842 PSA_PAKE_STEP_KEY_SHARE);
9843 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009844
Gilles Peskine449bd832023-01-11 14:50:10 +01009845 if (pw_data->len > 0) {
9846 psa_set_key_usage_flags(&attributes, key_usage_pw);
9847 psa_set_key_algorithm(&attributes, alg);
9848 psa_set_key_type(&attributes, key_type_pw);
9849 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9850 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009851 }
9852
Gilles Peskine449bd832023-01-11 14:50:10 +01009853 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9854 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9855 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009856
Gilles Peskine449bd832023-01-11 14:50:10 +01009857 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009858
Gilles Peskine449bd832023-01-11 14:50:10 +01009859 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9860 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9861 expected_error);
9862 PSA_ASSERT(psa_pake_abort(&operation));
9863 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9864 expected_error);
9865 PSA_ASSERT(psa_pake_abort(&operation));
9866 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9867 expected_error);
9868 PSA_ASSERT(psa_pake_abort(&operation));
9869 TEST_EQUAL(psa_pake_set_role(&operation, role),
9870 expected_error);
9871 PSA_ASSERT(psa_pake_abort(&operation));
9872 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9873 NULL, 0, NULL),
9874 expected_error);
9875 PSA_ASSERT(psa_pake_abort(&operation));
9876 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9877 expected_error);
9878 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009879 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009880 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009881
Gilles Peskine449bd832023-01-11 14:50:10 +01009882 status = psa_pake_setup(&operation, &cipher_suite);
9883 if (status != PSA_SUCCESS) {
9884 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009885 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009886 }
9887
Gilles Peskine449bd832023-01-11 14:50:10 +01009888 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9889 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9890 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009891 goto exit;
9892 }
9893
Gilles Peskine449bd832023-01-11 14:50:10 +01009894 status = psa_pake_set_role(&operation, role);
9895 if (status != PSA_SUCCESS) {
9896 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009897 goto exit;
9898 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009899
Gilles Peskine449bd832023-01-11 14:50:10 +01009900 if (pw_data->len > 0) {
9901 status = psa_pake_set_password_key(&operation, key);
9902 if (status != PSA_SUCCESS) {
9903 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009904 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009905 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009906 }
9907
Gilles Peskine449bd832023-01-11 14:50:10 +01009908 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9909 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9910 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009911 goto exit;
9912 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009913
Gilles Peskine449bd832023-01-11 14:50:10 +01009914 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9915 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9916 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009917 goto exit;
9918 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009919
Gilles Peskine449bd832023-01-11 14:50:10 +01009920 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009921 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009922 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9923 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009924 goto exit;
9925 }
9926
Gilles Peskine449bd832023-01-11 14:50:10 +01009927 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009928 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009929 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9930 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009931 goto exit;
9932 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009933
Gilles Peskine449bd832023-01-11 14:50:10 +01009934 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9935 PSA_PAKE_STEP_KEY_SHARE);
9936 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9937 PSA_PAKE_STEP_ZK_PUBLIC);
9938 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9939 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009940
Gilles Peskine449bd832023-01-11 14:50:10 +01009941 if (test_input) {
9942 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9943 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9944 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009945 goto exit;
9946 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009947
Gilles Peskine449bd832023-01-11 14:50:10 +01009948 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9949 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9950 output_buffer, size_zk_proof),
9951 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009952 goto exit;
9953 }
9954
Gilles Peskine449bd832023-01-11 14:50:10 +01009955 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9956 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9957 output_buffer, size_zk_proof),
9958 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009959 goto exit;
9960 }
9961
Gilles Peskine449bd832023-01-11 14:50:10 +01009962 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9963 output_buffer, size_key_share);
9964 if (status != PSA_SUCCESS) {
9965 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009966 goto exit;
9967 }
9968
Gilles Peskine449bd832023-01-11 14:50:10 +01009969 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9970 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9971 output_buffer, size_zk_public + 1),
9972 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009973 goto exit;
9974 }
9975
Gilles Peskine449bd832023-01-11 14:50:10 +01009976 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009977 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009978 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9979 output_buffer, size_zk_public + 1);
9980 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9981 output_buffer, size_zk_public),
9982 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009983 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009984 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009985 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009986 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9987 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9988 NULL, 0, NULL),
9989 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009990 goto exit;
9991 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009992
Gilles Peskine449bd832023-01-11 14:50:10 +01009993 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9994 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9995 output_buffer, buf_size, &output_len),
9996 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009997 goto exit;
9998 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009999
Gilles Peskine449bd832023-01-11 14:50:10 +010010000 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10001 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10002 output_buffer, buf_size, &output_len),
10003 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010004 goto exit;
10005 }
10006
Gilles Peskine449bd832023-01-11 14:50:10 +010010007 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10008 output_buffer, buf_size, &output_len);
10009 if (status != PSA_SUCCESS) {
10010 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010011 goto exit;
10012 }
10013
Gilles Peskine449bd832023-01-11 14:50:10 +010010014 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010015
Gilles Peskine449bd832023-01-11 14:50:10 +010010016 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10017 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10018 output_buffer, size_zk_public - 1, &output_len),
10019 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010020 goto exit;
10021 }
10022
Gilles Peskine449bd832023-01-11 14:50:10 +010010023 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010024 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010025 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10026 output_buffer, size_zk_public - 1, &output_len);
10027 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10028 output_buffer, buf_size, &output_len),
10029 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010030 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010031 }
10032 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010033
10034exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010035 PSA_ASSERT(psa_destroy_key(key));
10036 PSA_ASSERT(psa_pake_abort(&operation));
10037 mbedtls_free(output_buffer);
10038 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010039}
10040/* END_CASE */
10041
Neil Armstronga557cb82022-06-10 08:58:32 +020010042/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010043void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10044 int client_input_first, int inject_error,
10045 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010046{
10047 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10048 psa_pake_operation_t server = psa_pake_operation_init();
10049 psa_pake_operation_t client = psa_pake_operation_init();
10050 psa_algorithm_t alg = alg_arg;
10051 psa_algorithm_t hash_alg = hash_arg;
10052 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10054
Gilles Peskine449bd832023-01-11 14:50:10 +010010055 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010056
Gilles Peskine449bd832023-01-11 14:50:10 +010010057 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10058 psa_set_key_algorithm(&attributes, alg);
10059 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10060 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10061 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010062
Gilles Peskine449bd832023-01-11 14:50:10 +010010063 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10064 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10065 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010066
10067
Gilles Peskine449bd832023-01-11 14:50:10 +010010068 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10069 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010070
Gilles Peskine449bd832023-01-11 14:50:10 +010010071 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10072 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010073
Gilles Peskine449bd832023-01-11 14:50:10 +010010074 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10075 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010076
Gilles Peskine449bd832023-01-11 14:50:10 +010010077 ecjpake_do_round(alg, primitive_arg, &server, &client,
10078 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010079
Gilles Peskine449bd832023-01-11 14:50:10 +010010080 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010082 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010083
Gilles Peskine449bd832023-01-11 14:50:10 +010010084 ecjpake_do_round(alg, primitive_arg, &server, &client,
10085 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010086
10087exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010088 psa_destroy_key(key);
10089 psa_pake_abort(&server);
10090 psa_pake_abort(&client);
10091 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010092}
10093/* END_CASE */
10094
10095/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010096void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10097 int derive_alg_arg, data_t *pw_data,
10098 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010099{
10100 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10101 psa_pake_operation_t server = psa_pake_operation_init();
10102 psa_pake_operation_t client = psa_pake_operation_init();
10103 psa_algorithm_t alg = alg_arg;
10104 psa_algorithm_t hash_alg = hash_arg;
10105 psa_algorithm_t derive_alg = derive_alg_arg;
10106 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10108 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010109 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010110 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010111 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010112 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010113
Gilles Peskine449bd832023-01-11 14:50:10 +010010114 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010115
Gilles Peskine449bd832023-01-11 14:50:10 +010010116 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10117 psa_set_key_algorithm(&attributes, alg);
10118 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10119 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10120 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010121
Gilles Peskine449bd832023-01-11 14:50:10 +010010122 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10123 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10124 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010125
Neil Armstrong1e855602022-06-15 11:32:11 +020010126 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010127 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10128 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010129
Gilles Peskine449bd832023-01-11 14:50:10 +010010130 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10131 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10132 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10133 PSA_KEY_DERIVATION_INPUT_SEED,
10134 (const uint8_t *) "", 0));
10135 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10136 PSA_KEY_DERIVATION_INPUT_SEED,
10137 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010138 }
10139
Gilles Peskine449bd832023-01-11 14:50:10 +010010140 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10141 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010142
Gilles Peskine449bd832023-01-11 14:50:10 +010010143 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10144 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010145
Gilles Peskine449bd832023-01-11 14:50:10 +010010146 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10147 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010148
Gilles Peskine449bd832023-01-11 14:50:10 +010010149 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10150 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10151 PSA_ERROR_BAD_STATE);
10152 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10153 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010154 goto exit;
10155 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010156
Neil Armstrongf983caf2022-06-15 15:27:48 +020010157 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010158 ecjpake_do_round(alg, primitive_arg, &server, &client,
10159 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010160
Gilles Peskine449bd832023-01-11 14:50:10 +010010161 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10162 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10163 PSA_ERROR_BAD_STATE);
10164 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10165 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010166 goto exit;
10167 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010168
Neil Armstrongf983caf2022-06-15 15:27:48 +020010169 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010170 ecjpake_do_round(alg, primitive_arg, &server, &client,
10171 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010172
Gilles Peskine449bd832023-01-11 14:50:10 +010010173 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10174 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010175
10176exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010177 psa_key_derivation_abort(&server_derive);
10178 psa_key_derivation_abort(&client_derive);
10179 psa_destroy_key(key);
10180 psa_pake_abort(&server);
10181 psa_pake_abort(&client);
10182 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010183}
10184/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010185
10186/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010187void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010188{
10189 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10190 const size_t bits = 256;
10191 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010192 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010193 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010194 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010195
10196 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10197 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010198 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10199 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10200 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10201 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010202 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010203 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10204 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010205
10206 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010207 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10208 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10209 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10210 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10211 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10212 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010213
10214 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010215 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10216 PSA_PAKE_OUTPUT_MAX_SIZE);
10217 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10218 PSA_PAKE_OUTPUT_MAX_SIZE);
10219 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10220 PSA_PAKE_OUTPUT_MAX_SIZE);
10221 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10222 PSA_PAKE_INPUT_MAX_SIZE);
10223 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10224 PSA_PAKE_INPUT_MAX_SIZE);
10225 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10226 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010227}
10228/* END_CASE */