blob: 2c89a2e325e6ea25d6603dfdd18882af0a602ff2 [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
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +053019#include "../library/alignment.h"
Gilles Peskine8e94efe2021-02-13 00:25:53 +010020#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010021#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010022#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053023#if defined(PSA_CRYPTO_DRIVER_TEST)
24#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053025#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
26#else
27#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053028#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010029
Gilles Peskine4023c012021-05-27 13:21:20 +020030/* If this comes up, it's a bug in the test code or in the test data. */
31#define UNUSED 0xdeadbeef
32
Dave Rodgman647791d2021-06-23 12:49:59 +010033/* Assert that an operation is (not) active.
34 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010035#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
36#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010037
Przemek Stekiel7c795482022-11-15 22:26:12 +010038#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010039int ecjpake_operation_setup(psa_pake_operation_t *operation,
40 psa_pake_cipher_suite_t *cipher_suite,
41 psa_pake_role_t role,
42 mbedtls_svc_key_id_t key,
43 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010044{
Gilles Peskine449bd832023-01-11 14:50:10 +010045 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010046
Gilles Peskine449bd832023-01-11 14:50:10 +010047 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010050
Gilles Peskine449bd832023-01-11 14:50:10 +010051 if (key_available) {
52 PSA_ASSERT(psa_pake_set_password_key(operation, key));
53 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010054 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010055exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010056 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010057}
58#endif
59
Jaeden Amerof24c7f82018-06-27 17:20:43 +010060/** An invalid export length that will never be set by psa_export_key(). */
61static const size_t INVALID_EXPORT_LENGTH = ~0U;
62
Gilles Peskinea7aa4422018-08-14 15:17:54 +020063/** Test if a buffer contains a constant byte value.
64 *
65 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020066 *
67 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020068 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020069 * \param size Size of the buffer in bytes.
70 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020071 * \return 1 if the buffer is all-bits-zero.
72 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020073 */
Gilles Peskine449bd832023-01-11 14:50:10 +010074static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020075{
76 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010077 for (i = 0; i < size; i++) {
78 if (((unsigned char *) buffer)[i] != c) {
79 return 0;
80 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020081 }
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020083}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010084#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020085/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010086static int asn1_write_10x(unsigned char **p,
87 unsigned char *start,
88 size_t bits,
89 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020090{
91 int ret;
92 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (bits == 0) {
94 return MBEDTLS_ERR_ASN1_INVALID_DATA;
95 }
96 if (bits <= 8 && x >= 1 << (bits - 1)) {
97 return MBEDTLS_ERR_ASN1_INVALID_DATA;
98 }
99 if (*p < start || *p - start < (ptrdiff_t) len) {
100 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
101 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200102 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 (*p)[len-1] = x;
104 if (bits % 8 == 0) {
105 (*p)[1] |= 1;
106 } else {
107 (*p)[0] |= 1 << (bits % 8);
108 }
109 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
110 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
111 MBEDTLS_ASN1_INTEGER));
112 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200113}
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115static int construct_fake_rsa_key(unsigned char *buffer,
116 size_t buffer_size,
117 unsigned char **p,
118 size_t bits,
119 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200120{
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200122 int ret;
123 int len = 0;
124 /* Construct something that looks like a DER encoding of
125 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
126 * RSAPrivateKey ::= SEQUENCE {
127 * version Version,
128 * modulus INTEGER, -- n
129 * publicExponent INTEGER, -- e
130 * privateExponent INTEGER, -- d
131 * prime1 INTEGER, -- p
132 * prime2 INTEGER, -- q
133 * exponent1 INTEGER, -- d mod (p-1)
134 * exponent2 INTEGER, -- d mod (q-1)
135 * coefficient INTEGER, -- (inverse of q) mod p
136 * otherPrimeInfos OtherPrimeInfos OPTIONAL
137 * }
138 * Or, for a public key, the same structure with only
139 * version, modulus and publicExponent.
140 */
141 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (keypair) {
143 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
144 asn1_write_10x(p, buffer, half_bits, 1));
145 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
146 asn1_write_10x(p, buffer, half_bits, 1));
147 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
148 asn1_write_10x(p, buffer, half_bits, 1));
149 MBEDTLS_ASN1_CHK_ADD(len, /* q */
150 asn1_write_10x(p, buffer, half_bits, 1));
151 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
152 asn1_write_10x(p, buffer, half_bits, 3));
153 MBEDTLS_ASN1_CHK_ADD(len, /* d */
154 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200155 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
157 asn1_write_10x(p, buffer, 17, 1));
158 MBEDTLS_ASN1_CHK_ADD(len, /* n */
159 asn1_write_10x(p, buffer, bits, 1));
160 if (keypair) {
161 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
162 mbedtls_asn1_write_int(p, buffer, 0));
163 }
164 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200165 {
166 const unsigned char tag =
167 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200169 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200171}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100172#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174int exercise_mac_setup(psa_key_type_t key_type,
175 const unsigned char *key_bytes,
176 size_t key_length,
177 psa_algorithm_t alg,
178 psa_mac_operation_t *operation,
179 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100180{
Ronald Cron5425a212020-08-04 14:58:35 +0200181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
185 psa_set_key_algorithm(&attributes, alg);
186 psa_set_key_type(&attributes, key_type);
187 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100190 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* If setup failed, reproduce the failure, so that the caller can
193 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (*status != PSA_SUCCESS) {
195 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 }
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 psa_destroy_key(key);
199 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200
201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 psa_destroy_key(key);
203 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100204}
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int exercise_cipher_setup(psa_key_type_t key_type,
207 const unsigned char *key_bytes,
208 size_t key_length,
209 psa_algorithm_t alg,
210 psa_cipher_operation_t *operation,
211 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100212{
Ronald Cron5425a212020-08-04 14:58:35 +0200213 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
217 psa_set_key_algorithm(&attributes, alg);
218 psa_set_key_type(&attributes, key_type);
219 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100224 /* If setup failed, reproduce the failure, so that the caller can
225 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (*status != PSA_SUCCESS) {
227 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
228 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100229 }
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 psa_destroy_key(key);
232 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100233
234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 psa_destroy_key(key);
236 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100237}
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240{
241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200243 uint8_t buffer[1];
244 size_t length;
245 int ok = 0;
246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 psa_set_key_id(&attributes, key_id);
248 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
249 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
250 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
251 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
252 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200253 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200255 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
257 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
258 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
259 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
260 TEST_EQUAL(psa_get_key_type(&attributes), 0);
261 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
264 PSA_ERROR_INVALID_HANDLE);
265 TEST_EQUAL(psa_export_public_key(key,
266 buffer, sizeof(buffer), &length),
267 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200268
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200269 ok = 1;
270
271exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100272 /*
273 * Key attributes may have been returned by psa_get_key_attributes()
274 * thus reset them as required.
275 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200279}
280
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200281/* Assert that a key isn't reported as having a slot number. */
282#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100283#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200284 do \
285 { \
286 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 TEST_EQUAL(psa_get_key_slot_number( \
288 attributes, \
289 &ASSERT_NO_SLOT_NUMBER_slot_number), \
290 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200291 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200293#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294#define ASSERT_NO_SLOT_NUMBER(attributes) \
295 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200296#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
297
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530298uint64_t parse_hex_string(data_t *hex_string)
299{
300 uint64_t result = 0;
301 for (size_t i = 0; i < hex_string->len; i++) {
302 if (MBEDTLS_IS_BIG_ENDIAN) {
303 result |= ((hex_string->x)[i]) << (i * 8);
304 } else {
305 result |= ((hex_string->x)[i]) << ((hex_string->len - i - 1) * 8);
306 }
307 }
308 return result;
309}
310
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100311/* An overapproximation of the amount of storage needed for a key of the
312 * given type and with the given content. The API doesn't make it easy
313 * to find a good value for the size. The current implementation doesn't
314 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315#define KEY_BITS_FROM_DATA(type, data) \
316 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100317
Darryl Green0c6575a2018-11-07 16:05:30 +0000318typedef enum {
319 IMPORT_KEY = 0,
320 GENERATE_KEY = 1,
321 DERIVE_KEY = 2
322} generate_method;
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100325 DO_NOT_SET_LENGTHS = 0,
326 SET_LENGTHS_BEFORE_NONCE = 1,
327 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100328} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100331 USE_NULL_TAG = 0,
332 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100333} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100334
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530335typedef enum {
336 INPUT_BYTES = 0,
337 INPUT_INTEGER = 1
338} key_derivation_input_method_t;
339
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340/*!
341 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342 * \param key_type_arg Type of key passed in
343 * \param key_data The encryption / decryption key data
344 * \param alg_arg The type of algorithm used
345 * \param nonce Nonce data
346 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100347 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 * feed additional data in to be encrypted /
349 * decrypted. If -1, no chunking.
350 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100351 * \param data_part_len_arg If not -1, the length of chunks to feed
352 * the data in to be encrypted / decrypted. If
353 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100354 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100355 * expected here, this controls whether or not
356 * to set lengths, and in what order with
357 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100358 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100359 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100360 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100361 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100362 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
365 int alg_arg,
366 data_t *nonce,
367 data_t *additional_data,
368 int ad_part_len_arg,
369 data_t *input_data,
370 int data_part_len_arg,
371 set_lengths_method_t set_lengths_method,
372 data_t *expected_output,
373 int is_encrypt,
374 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100375{
376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
377 psa_key_type_t key_type = key_type_arg;
378 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100379 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100380 unsigned char *output_data = NULL;
381 unsigned char *part_data = NULL;
382 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100383 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100384 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 size_t output_size = 0;
386 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100387 size_t output_length = 0;
388 size_t key_bits = 0;
389 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100390 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100391 size_t part_length = 0;
392 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100393 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100394 size_t ad_part_len = 0;
395 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
398 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
399
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100401 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (is_encrypt) {
406 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
407 } else {
408 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100410
411 psa_set_key_algorithm(&attributes, alg);
412 psa_set_key_type(&attributes, key_type);
413
414 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
415 &key));
416
417 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
418 key_bits = psa_get_key_bits(&attributes);
419
420 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
421
422 if (is_encrypt) {
423 /* Tag gets written at end of buffer. */
424 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
425 (input_data->len +
426 tag_length));
427 data_true_size = input_data->len;
428 } else {
429 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
430 (input_data->len -
431 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100432
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100433 /* Do not want to attempt to decrypt tag. */
434 data_true_size = input_data->len - tag_length;
435 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 ASSERT_ALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (is_encrypt) {
440 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
441 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
442 } else {
443 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
444 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100445 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 ASSERT_ALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (is_encrypt) {
450 status = psa_aead_encrypt_setup(&operation, key, alg);
451 } else {
452 status = psa_aead_decrypt_setup(&operation, key, alg);
453 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100454
455 /* If the operation is not supported, just skip and not fail in case the
456 * encryption involves a common limitation of cryptography hardwares and
457 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (status == PSA_ERROR_NOT_SUPPORTED) {
459 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
460 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
466 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
467 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
468 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
469 data_true_size));
470 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
471 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
472 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
475 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100479 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100480 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100483 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 part_offset += part_length, part_count++) {
485 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100486 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100488 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100490 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100491 }
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 PSA_ASSERT(psa_aead_update_ad(&operation,
494 additional_data->x + part_offset,
495 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100496
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100499 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
501 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100502 }
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100505 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 data_part_len = (size_t) data_part_len_arg;
507 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
508 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 ASSERT_ALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100513 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 part_offset += part_length, part_count++) {
515 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100516 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 } else if ((data_true_size - part_offset) < data_part_len) {
518 part_length = (data_true_size - part_offset);
519 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100520 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100521 }
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 PSA_ASSERT(psa_aead_update(&operation,
524 (input_data->x + part_offset),
525 part_length, part_data,
526 part_data_size,
527 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (output_data && output_part_length) {
530 memcpy((output_data + output_length), part_data,
531 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 }
533
Paul Elliottd3f82412021-06-16 16:52:21 +0100534 output_length += output_part_length;
535 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100537 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
539 data_true_size, output_data,
540 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100541 }
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (is_encrypt) {
544 PSA_ASSERT(psa_aead_finish(&operation, final_data,
545 final_output_size,
546 &output_part_length,
547 tag_buffer, tag_length,
548 &tag_size));
549 } else {
550 PSA_ASSERT(psa_aead_verify(&operation, final_data,
551 final_output_size,
552 &output_part_length,
553 (input_data->x + data_true_size),
554 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100555 }
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (output_data && output_part_length) {
558 memcpy((output_data + output_length), final_data,
559 output_part_length);
560 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100561
562 output_length += output_part_length;
563
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100564
565 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
566 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (is_encrypt) {
568 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (output_data && tag_length) {
571 memcpy((output_data + output_length), tag_buffer,
572 tag_length);
573 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100574
575 output_length += tag_length;
576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 TEST_EQUAL(output_length,
578 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
579 input_data->len));
580 TEST_LE_U(output_length,
581 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
582 } else {
583 TEST_EQUAL(output_length,
584 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
585 input_data->len));
586 TEST_LE_U(output_length,
587 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100588 }
589
Paul Elliottd3f82412021-06-16 16:52:21 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 ASSERT_COMPARE(expected_output->x, expected_output->len,
592 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100593
Paul Elliottd3f82412021-06-16 16:52:21 +0100594
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100595 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100596
597exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 psa_destroy_key(key);
599 psa_aead_abort(&operation);
600 mbedtls_free(output_data);
601 mbedtls_free(part_data);
602 mbedtls_free(final_data);
603 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100606}
607
Neil Armstrong4766f992022-02-28 16:23:59 +0100608/*!
609 * \brief Internal Function for MAC multipart tests.
610 * \param key_type_arg Type of key passed in
611 * \param key_data The encryption / decryption key data
612 * \param alg_arg The type of algorithm used
613 * \param input_data Data to encrypt / decrypt
614 * \param data_part_len_arg If not -1, the length of chunks to feed
615 * the data in to be encrypted / decrypted. If
616 * -1, no chunking
617 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000618 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100619 * \param do_zero_parts If non-zero, interleave zero length chunks
620 * with normal length chunks.
621 * \return int Zero on failure, non-zero on success.
622 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
624 int alg_arg,
625 data_t *input_data,
626 int data_part_len_arg,
627 data_t *expected_output,
628 int is_verify,
629 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100630{
631 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
632 psa_key_type_t key_type = key_type_arg;
633 psa_algorithm_t alg = alg_arg;
634 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
635 unsigned char mac[PSA_MAC_MAX_SIZE];
636 size_t part_offset = 0;
637 size_t part_length = 0;
638 size_t data_part_len = 0;
639 size_t mac_len = 0;
640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
641 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
642
643 int test_ok = 0;
644 size_t part_count = 0;
645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (is_verify) {
649 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
650 } else {
651 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
652 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 psa_set_key_algorithm(&attributes, alg);
655 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
658 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (is_verify) {
661 status = psa_mac_verify_setup(&operation, key, alg);
662 } else {
663 status = psa_mac_sign_setup(&operation, key, alg);
664 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100669 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100673 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 part_offset += part_length, part_count++) {
675 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100676 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 } else if ((input_data->len - part_offset) < data_part_len) {
678 part_length = (input_data->len - part_offset);
679 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100680 part_length = data_part_len;
681 }
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 PSA_ASSERT(psa_mac_update(&operation,
684 (input_data->x + part_offset),
685 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100686 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100688 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
690 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100691 }
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (is_verify) {
694 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
695 expected_output->len));
696 } else {
697 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
698 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 ASSERT_COMPARE(expected_output->x, expected_output->len,
701 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100702 }
703
704 test_ok = 1;
705
706exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 psa_destroy_key(key);
708 psa_mac_abort(&operation);
709 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100712}
713
Neil Armstrong75673ab2022-06-15 17:39:01 +0200714#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100715static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
716 psa_pake_operation_t *server,
717 psa_pake_operation_t *client,
718 int client_input_first,
719 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200720{
721 unsigned char *buffer0 = NULL, *buffer1 = NULL;
722 size_t buffer_length = (
723 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
724 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
725 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200726 /* The output should be exactly this size according to the spec */
727 const size_t expected_size_key_share =
728 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
729 /* The output should be exactly this size according to the spec */
730 const size_t expected_size_zk_public =
731 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
732 /* The output can be smaller: the spec allows stripping leading zeroes */
733 const size_t max_expected_size_zk_proof =
734 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200735 size_t buffer0_off = 0;
736 size_t buffer1_off = 0;
737 size_t s_g1_len, s_g2_len, s_a_len;
738 size_t s_g1_off, s_g2_off, s_a_off;
739 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
740 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
741 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
742 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
743 size_t c_g1_len, c_g2_len, c_a_len;
744 size_t c_g1_off, c_g2_off, c_a_off;
745 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
746 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
747 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
748 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
749 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200750 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 ASSERT_ALLOC(buffer0, buffer_length);
753 ASSERT_ALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200756 case 1:
757 /* Server first round Output */
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_g1_len));
761 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200762 s_g1_off = buffer0_off;
763 buffer0_off += s_g1_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_x1_pk_len));
767 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200768 s_x1_pk_off = buffer0_off;
769 buffer0_off += s_x1_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_x1_pr_len));
773 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200774 s_x1_pr_off = buffer0_off;
775 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
777 buffer0 + buffer0_off,
778 512 - buffer0_off, &s_g2_len));
779 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200780 s_g2_off = buffer0_off;
781 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
783 buffer0 + buffer0_off,
784 512 - buffer0_off, &s_x2_pk_len));
785 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200786 s_x2_pk_off = buffer0_off;
787 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
789 buffer0 + buffer0_off,
790 512 - buffer0_off, &s_x2_pr_len));
791 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200792 s_x2_pr_off = buffer0_off;
793 buffer0_off += s_x2_pr_len;
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500796 buffer0[s_x1_pr_off + 8] ^= 1;
797 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200798 expected_status = PSA_ERROR_DATA_INVALID;
799 }
800
Neil Armstrong51009d72022-09-05 17:59:54 +0200801 /*
802 * When injecting errors in inputs, the implementation is
803 * free to detect it right away of with a delay.
804 * This permits delaying the error until the end of the input
805 * sequence, if no error appears then, this will be treated
806 * as an error.
807 */
808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200810 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
812 buffer0 + s_g1_off, s_g1_len);
813 if (inject_error == 1 && status != PSA_SUCCESS) {
814 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200815 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 } else {
817 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200818 }
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
821 buffer0 + s_x1_pk_off,
822 s_x1_pk_len);
823 if (inject_error == 1 && status != PSA_SUCCESS) {
824 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200825 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 } else {
827 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200828 }
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
831 buffer0 + s_x1_pr_off,
832 s_x1_pr_len);
833 if (inject_error == 1 && status != PSA_SUCCESS) {
834 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200835 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 } else {
837 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200838 }
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
841 buffer0 + s_g2_off,
842 s_g2_len);
843 if (inject_error == 1 && status != PSA_SUCCESS) {
844 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200845 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 } else {
847 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200848 }
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
851 buffer0 + s_x2_pk_off,
852 s_x2_pk_len);
853 if (inject_error == 1 && status != PSA_SUCCESS) {
854 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200855 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 } else {
857 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200858 }
859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
861 buffer0 + s_x2_pr_off,
862 s_x2_pr_len);
863 if (inject_error == 1 && status != PSA_SUCCESS) {
864 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200865 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 } else {
867 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200868 }
869
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200870 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (inject_error == 1) {
872 TEST_ASSERT(
873 !"One of the last psa_pake_input() calls should have returned the expected error.");
874 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200875 }
876
877 /* Client first round Output */
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_g1_len));
881 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200882 c_g1_off = buffer1_off;
883 buffer1_off += c_g1_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_x1_pk_len));
887 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200888 c_x1_pk_off = buffer1_off;
889 buffer1_off += c_x1_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_x1_pr_len));
893 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200894 c_x1_pr_off = buffer1_off;
895 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
897 buffer1 + buffer1_off,
898 512 - buffer1_off, &c_g2_len));
899 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200900 c_g2_off = buffer1_off;
901 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
903 buffer1 + buffer1_off,
904 512 - buffer1_off, &c_x2_pk_len));
905 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200906 c_x2_pk_off = buffer1_off;
907 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
909 buffer1 + buffer1_off,
910 512 - buffer1_off, &c_x2_pr_len));
911 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200912 c_x2_pr_off = buffer1_off;
913 buffer1_off += c_x2_pr_len;
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200916 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
918 buffer0 + s_g1_off, s_g1_len);
919 if (inject_error == 1 && status != PSA_SUCCESS) {
920 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200921 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 } else {
923 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200924 }
925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
927 buffer0 + s_x1_pk_off,
928 s_x1_pk_len);
929 if (inject_error == 1 && status != PSA_SUCCESS) {
930 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200931 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 } else {
933 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200934 }
935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
937 buffer0 + s_x1_pr_off,
938 s_x1_pr_len);
939 if (inject_error == 1 && status != PSA_SUCCESS) {
940 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200941 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 } else {
943 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200944 }
945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
947 buffer0 + s_g2_off,
948 s_g2_len);
949 if (inject_error == 1 && status != PSA_SUCCESS) {
950 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200951 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 } else {
953 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200954 }
955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
957 buffer0 + s_x2_pk_off,
958 s_x2_pk_len);
959 if (inject_error == 1 && status != PSA_SUCCESS) {
960 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200961 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 } else {
963 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200964 }
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
967 buffer0 + s_x2_pr_off,
968 s_x2_pr_len);
969 if (inject_error == 1 && status != PSA_SUCCESS) {
970 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200971 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 } else {
973 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200974 }
975
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200976 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (inject_error == 1) {
978 TEST_ASSERT(
979 !"One of the last psa_pake_input() calls should have returned the expected error.");
980 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200981 }
982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500984 buffer1[c_x1_pr_off + 12] ^= 1;
985 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200986 expected_status = PSA_ERROR_DATA_INVALID;
987 }
988
989 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
991 buffer1 + c_g1_off, c_g1_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_ZK_PUBLIC,
1000 buffer1 + c_x1_pk_off, c_x1_pk_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_PROOF,
1009 buffer1 + c_x1_pr_off, c_x1_pr_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_KEY_SHARE,
1018 buffer1 + c_g2_off, c_g2_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
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1027 buffer1 + c_x2_pk_off, c_x2_pk_len);
1028 if (inject_error == 2 && status != PSA_SUCCESS) {
1029 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001030 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 } else {
1032 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001033 }
1034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1036 buffer1 + c_x2_pr_off, c_x2_pr_len);
1037 if (inject_error == 2 && status != PSA_SUCCESS) {
1038 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001039 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 } else {
1041 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001042 }
1043
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001044 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (inject_error == 2) {
1046 TEST_ASSERT(
1047 !"One of the last psa_pake_input() calls should have returned the expected error.");
1048 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001049
1050 break;
1051
1052 case 2:
1053 /* Server second round Output */
1054 buffer0_off = 0;
1055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1057 buffer0 + buffer0_off,
1058 512 - buffer0_off, &s_a_len));
1059 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001060 s_a_off = buffer0_off;
1061 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1063 buffer0 + buffer0_off,
1064 512 - buffer0_off, &s_x2s_pk_len));
1065 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001066 s_x2s_pk_off = buffer0_off;
1067 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1069 buffer0 + buffer0_off,
1070 512 - buffer0_off, &s_x2s_pr_len));
1071 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001072 s_x2s_pr_off = buffer0_off;
1073 buffer0_off += s_x2s_pr_len;
1074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001076 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001077 expected_status = PSA_ERROR_DATA_INVALID;
1078 }
1079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001081 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1083 buffer0 + s_a_off, s_a_len);
1084 if (inject_error == 3 && status != PSA_SUCCESS) {
1085 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001086 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 } else {
1088 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001089 }
1090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1092 buffer0 + s_x2s_pk_off,
1093 s_x2s_pk_len);
1094 if (inject_error == 3 && status != PSA_SUCCESS) {
1095 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001096 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 } else {
1098 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001099 }
1100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1102 buffer0 + s_x2s_pr_off,
1103 s_x2s_pr_len);
1104 if (inject_error == 3 && status != PSA_SUCCESS) {
1105 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001106 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 } else {
1108 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001109 }
1110
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001111 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (inject_error == 3) {
1113 TEST_ASSERT(
1114 !"One of the last psa_pake_input() calls should have returned the expected error.");
1115 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001116 }
1117
1118 /* Client second round Output */
1119 buffer1_off = 0;
1120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1122 buffer1 + buffer1_off,
1123 512 - buffer1_off, &c_a_len));
1124 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001125 c_a_off = buffer1_off;
1126 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1128 buffer1 + buffer1_off,
1129 512 - buffer1_off, &c_x2s_pk_len));
1130 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001131 c_x2s_pk_off = buffer1_off;
1132 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1134 buffer1 + buffer1_off,
1135 512 - buffer1_off, &c_x2s_pr_len));
1136 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001137 c_x2s_pr_off = buffer1_off;
1138 buffer1_off += c_x2s_pr_len;
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001141 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1143 buffer0 + s_a_off, s_a_len);
1144 if (inject_error == 3 && status != PSA_SUCCESS) {
1145 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001146 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 } else {
1148 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001149 }
1150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1152 buffer0 + s_x2s_pk_off,
1153 s_x2s_pk_len);
1154 if (inject_error == 3 && status != PSA_SUCCESS) {
1155 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001156 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 } else {
1158 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001159 }
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1162 buffer0 + s_x2s_pr_off,
1163 s_x2s_pr_len);
1164 if (inject_error == 3 && status != PSA_SUCCESS) {
1165 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001166 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 } else {
1168 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001169 }
1170
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001171 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (inject_error == 3) {
1173 TEST_ASSERT(
1174 !"One of the last psa_pake_input() calls should have returned the expected error.");
1175 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001176 }
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001179 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001180 expected_status = PSA_ERROR_DATA_INVALID;
1181 }
1182
1183 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1185 buffer1 + c_a_off, c_a_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
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1194 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1195 if (inject_error == 4 && status != PSA_SUCCESS) {
1196 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001197 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 } else {
1199 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001200 }
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1203 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1204 if (inject_error == 4 && status != PSA_SUCCESS) {
1205 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001206 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 } else {
1208 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001209 }
1210
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001211 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (inject_error == 4) {
1213 TEST_ASSERT(
1214 !"One of the last psa_pake_input() calls should have returned the expected error.");
1215 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001216
1217 break;
1218
1219 }
1220
Neil Armstrongf983caf2022-06-15 15:27:48 +02001221exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 mbedtls_free(buffer0);
1223 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001224}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001225#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001228 INJECT_ERR_NONE = 0,
1229 INJECT_ERR_UNINITIALIZED_ACCESS,
1230 INJECT_ERR_DUPLICATE_SETUP,
1231 INJECT_ERR_INVALID_USER,
1232 INJECT_ERR_INVALID_PEER,
1233 INJECT_ERR_SET_USER,
1234 INJECT_ERR_SET_PEER,
1235 INJECT_EMPTY_IO_BUFFER,
1236 INJECT_UNKNOWN_STEP,
1237 INJECT_INVALID_FIRST_STEP,
1238 INJECT_WRONG_BUFFER_SIZE,
1239 INJECT_VALID_OPERATION_AFTER_FAILURE,
1240 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1241 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1242} ecjpake_injected_failure_t;
1243
Paul Elliott01885fa2023-02-09 12:07:30 +00001244#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001245
Paul Elliott6f600372023-02-06 18:41:05 +00001246static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1247 psa_status_t expected_status,
1248 size_t *min_completes,
1249 size_t *max_completes)
1250{
1251
1252 /* This is slightly contrived, but we only really know that with a minimum
1253 value of max_ops that a successful operation should take more than one op
1254 to complete, and likewise that with a max_ops of
1255 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1256 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001257
Paul Elliott6f600372023-02-06 18:41:05 +00001258 if (expected_status == PSA_SUCCESS) {
1259 *min_completes = 2;
1260 } else {
1261 *min_completes = 1;
1262 }
1263
1264 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1265 } else {
1266 *min_completes = 1;
1267 *max_completes = 1;
1268 }
1269}
Paul Elliott01885fa2023-02-09 12:07:30 +00001270#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001271
Gilles Peskinee59236f2018-01-27 23:32:46 +01001272/* END_HEADER */
1273
1274/* BEGIN_DEPENDENCIES
1275 * depends_on:MBEDTLS_PSA_CRYPTO_C
1276 * END_DEPENDENCIES
1277 */
1278
1279/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001280void psa_can_do_hash()
1281{
1282 /* We can't test that this is specific to drivers until partial init has
1283 * been implemented, but we can at least test before/after full init. */
1284 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1285 PSA_INIT();
1286 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1287 PSA_DONE();
1288}
1289/* END_CASE */
1290
1291/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001293{
1294 size_t max_truncated_mac_size =
1295 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1296
1297 /* Check that the length for a truncated MAC always fits in the algorithm
1298 * encoding. The shifted mask is the maximum truncated value. The
1299 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001301}
1302/* END_CASE */
1303
1304/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001305void import_with_policy(int type_arg,
1306 int usage_arg, int alg_arg,
1307 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001308{
1309 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1310 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001312 psa_key_type_t type = type_arg;
1313 psa_key_usage_t usage = usage_arg;
1314 psa_algorithm_t alg = alg_arg;
1315 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001317 psa_status_t status;
1318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 psa_set_key_type(&attributes, type);
1322 psa_set_key_usage_flags(&attributes, usage);
1323 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 status = psa_import_key(&attributes,
1326 key_material, sizeof(key_material),
1327 &key);
1328 TEST_EQUAL(status, expected_status);
1329 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001330 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1334 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1335 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1336 mbedtls_test_update_key_usage_flags(usage));
1337 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1338 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 PSA_ASSERT(psa_destroy_key(key));
1341 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001342
1343exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001344 /*
1345 * Key attributes may have been returned by psa_get_key_attributes()
1346 * thus reset them as required.
1347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 psa_destroy_key(key);
1351 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001352}
1353/* END_CASE */
1354
1355/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001356void import_with_data(data_t *data, int type_arg,
1357 int attr_bits_arg,
1358 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001359{
1360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1361 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001363 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001364 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001365 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001366 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 psa_set_key_type(&attributes, type);
1371 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 status = psa_import_key(&attributes, data->x, data->len, &key);
1374 TEST_EQUAL(status, expected_status);
1375 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001376 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1380 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1381 if (attr_bits != 0) {
1382 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1383 }
1384 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 PSA_ASSERT(psa_destroy_key(key));
1387 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001388
1389exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001390 /*
1391 * Key attributes may have been returned by psa_get_key_attributes()
1392 * thus reset them as required.
1393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 psa_destroy_key(key);
1397 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001398}
1399/* END_CASE */
1400
1401/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001402/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403void import_large_key(int type_arg, int byte_size_arg,
1404 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001405{
1406 psa_key_type_t type = type_arg;
1407 size_t byte_size = byte_size_arg;
1408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1409 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001410 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001411 psa_status_t status;
1412 uint8_t *buffer = NULL;
1413 size_t buffer_size = byte_size + 1;
1414 size_t n;
1415
Steven Cooreman69967ce2021-01-18 18:01:08 +01001416 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001417 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1419 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001422
1423 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1425 psa_set_key_type(&attributes, type);
1426 status = psa_import_key(&attributes, buffer, byte_size, &key);
1427 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1428 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (status == PSA_SUCCESS) {
1431 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1432 TEST_EQUAL(psa_get_key_type(&attributes), type);
1433 TEST_EQUAL(psa_get_key_bits(&attributes),
1434 PSA_BYTES_TO_BITS(byte_size));
1435 ASSERT_NO_SLOT_NUMBER(&attributes);
1436 memset(buffer, 0, byte_size + 1);
1437 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1438 for (n = 0; n < byte_size; n++) {
1439 TEST_EQUAL(buffer[n], 'K');
1440 }
1441 for (n = byte_size; n < buffer_size; n++) {
1442 TEST_EQUAL(buffer[n], 0);
1443 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001444 }
1445
1446exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001447 /*
1448 * Key attributes may have been returned by psa_get_key_attributes()
1449 * thus reset them as required.
1450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 psa_destroy_key(key);
1454 PSA_DONE();
1455 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001456}
1457/* END_CASE */
1458
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001459/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001460/* Import an RSA key with a valid structure (but not valid numbers
1461 * inside, beyond having sensible size and parity). This is expected to
1462 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001464{
Ronald Cron5425a212020-08-04 14:58:35 +02001465 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001466 size_t bits = bits_arg;
1467 psa_status_t expected_status = expected_status_arg;
1468 psa_status_t status;
1469 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001470 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001471 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001473 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001474 unsigned char *p;
1475 int ret;
1476 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 PSA_ASSERT(psa_crypto_init());
1480 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1483 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001484 length = ret;
1485
1486 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 psa_set_key_type(&attributes, type);
1488 status = psa_import_key(&attributes, p, length, &key);
1489 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (status == PSA_SUCCESS) {
1492 PSA_ASSERT(psa_destroy_key(key));
1493 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001494
1495exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 mbedtls_free(buffer);
1497 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001498}
1499/* END_CASE */
1500
1501/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502void import_export(data_t *data,
1503 int type_arg,
1504 int usage_arg, int alg_arg,
1505 int lifetime_arg,
1506 int expected_bits,
1507 int export_size_delta,
1508 int expected_export_status_arg,
1509 /*whether reexport must give the original input exactly*/
1510 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001511{
Ronald Cron5425a212020-08-04 14:58:35 +02001512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001514 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001515 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001516 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301517 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001518 unsigned char *exported = NULL;
1519 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001521 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001524 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001525
Moran Pekercb088e72018-07-17 17:36:59 +03001526 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 ASSERT_ALLOC(exported, export_size);
1528 if (!canonical_input) {
1529 ASSERT_ALLOC(reexported, export_size);
1530 }
1531 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 psa_set_key_lifetime(&attributes, lifetime);
1534 psa_set_key_usage_flags(&attributes, usage_arg);
1535 psa_set_key_algorithm(&attributes, alg);
1536 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001537
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540
1541 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1543 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1544 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1545 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001546
1547 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 status = psa_export_key(key, exported, export_size, &exported_length);
1549 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001550
1551 /* The exported length must be set by psa_export_key() to a value between 0
1552 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1554 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1555 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1558 export_size - exported_length));
1559 if (status != PSA_SUCCESS) {
1560 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001561 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001562 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001563
Gilles Peskineea38a922021-02-13 00:05:16 +01001564 /* Run sanity checks on the exported key. For non-canonical inputs,
1565 * this validates the canonical representations. For canonical inputs,
1566 * this doesn't directly validate the implementation, but it still helps
1567 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (!psa_key_lifetime_is_external(lifetime)) {
1569 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301570 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 }
Archana4d7ae1d2021-07-07 02:50:22 +05301572 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (canonical_input) {
1575 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1576 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001577 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1579 &key2));
1580 PSA_ASSERT(psa_export_key(key2,
1581 reexported,
1582 export_size,
1583 &reexported_length));
1584 ASSERT_COMPARE(exported, exported_length,
1585 reexported, reexported_length);
1586 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 TEST_LE_U(exported_length,
1589 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1590 psa_get_key_bits(&got_attributes)));
1591 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001592
1593destroy:
1594 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 PSA_ASSERT(psa_destroy_key(key));
1596 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001597
1598exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001599 /*
1600 * Key attributes may have been returned by psa_get_key_attributes()
1601 * thus reset them as required.
1602 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 psa_reset_key_attributes(&got_attributes);
1604 psa_destroy_key(key);
1605 mbedtls_free(exported);
1606 mbedtls_free(reexported);
1607 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001608}
1609/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001610
Moran Pekerf709f4a2018-06-06 17:26:04 +03001611/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001612void import_export_public_key(data_t *data,
1613 int type_arg, // key pair or public key
1614 int alg_arg,
1615 int lifetime_arg,
1616 int export_size_delta,
1617 int expected_export_status_arg,
1618 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001619{
Ronald Cron5425a212020-08-04 14:58:35 +02001620 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001621 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001622 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001623 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001624 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301625 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001626 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001627 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001628 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001629 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 psa_set_key_lifetime(&attributes, lifetime);
1634 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1635 psa_set_key_algorithm(&attributes, alg);
1636 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001637
1638 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001640
Gilles Peskine49c25912018-10-29 15:15:31 +01001641 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 ASSERT_ALLOC(exported, export_size);
1643 status = psa_export_public_key(key,
1644 exported, export_size,
1645 &exported_length);
1646 TEST_EQUAL(status, expected_export_status);
1647 if (status == PSA_SUCCESS) {
1648 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001649 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1651 bits = psa_get_key_bits(&attributes);
1652 TEST_LE_U(expected_public_key->len,
1653 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1654 TEST_LE_U(expected_public_key->len,
1655 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1656 TEST_LE_U(expected_public_key->len,
1657 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1658 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1659 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001660 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001661exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001662 /*
1663 * Key attributes may have been returned by psa_get_key_attributes()
1664 * thus reset them as required.
1665 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 mbedtls_free(exported);
1669 psa_destroy_key(key);
1670 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001671}
1672/* END_CASE */
1673
Gilles Peskine20035e32018-02-03 22:44:14 +01001674/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001675void import_and_exercise_key(data_t *data,
1676 int type_arg,
1677 int bits_arg,
1678 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001679{
Ronald Cron5425a212020-08-04 14:58:35 +02001680 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001681 psa_key_type_t type = type_arg;
1682 size_t bits = bits_arg;
1683 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001686 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 psa_set_key_usage_flags(&attributes, usage);
1691 psa_set_key_algorithm(&attributes, alg);
1692 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001693
1694 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001696
1697 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1699 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1700 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001701
1702 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001704 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 PSA_ASSERT(psa_destroy_key(key));
1708 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001709
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001710exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001711 /*
1712 * Key attributes may have been returned by psa_get_key_attributes()
1713 * thus reset them as required.
1714 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 psa_reset_key_attributes(&attributes);
1718 psa_destroy_key(key);
1719 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001720}
1721/* END_CASE */
1722
1723/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001724void effective_key_attributes(int type_arg, int expected_type_arg,
1725 int bits_arg, int expected_bits_arg,
1726 int usage_arg, int expected_usage_arg,
1727 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001728{
Ronald Cron5425a212020-08-04 14:58:35 +02001729 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001730 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001731 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001732 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001733 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001734 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001735 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001736 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001737 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 psa_set_key_usage_flags(&attributes, usage);
1743 psa_set_key_algorithm(&attributes, alg);
1744 psa_set_key_type(&attributes, key_type);
1745 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 PSA_ASSERT(psa_generate_key(&attributes, &key));
1748 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1751 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1752 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1753 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1754 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001755
1756exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001757 /*
1758 * Key attributes may have been returned by psa_get_key_attributes()
1759 * thus reset them as required.
1760 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 psa_destroy_key(key);
1764 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001765}
1766/* END_CASE */
1767
1768/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001769void check_key_policy(int type_arg, int bits_arg,
1770 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001771{
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1773 usage_arg,
1774 mbedtls_test_update_key_usage_flags(usage_arg),
1775 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001776 goto exit;
1777}
1778/* END_CASE */
1779
1780/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001781void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001782{
1783 /* Test each valid way of initializing the object, except for `= {0}`, as
1784 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1785 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001786 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001788 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1789 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1794 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1795 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 TEST_EQUAL(psa_get_key_type(&func), 0);
1798 TEST_EQUAL(psa_get_key_type(&init), 0);
1799 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 TEST_EQUAL(psa_get_key_bits(&func), 0);
1802 TEST_EQUAL(psa_get_key_bits(&init), 0);
1803 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1806 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1807 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1810 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1811 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001812}
1813/* END_CASE */
1814
1815/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816void mac_key_policy(int policy_usage_arg,
1817 int policy_alg_arg,
1818 int key_type_arg,
1819 data_t *key_data,
1820 int exercise_alg_arg,
1821 int expected_status_sign_arg,
1822 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001823{
Ronald Cron5425a212020-08-04 14:58:35 +02001824 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001826 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001827 psa_key_type_t key_type = key_type_arg;
1828 psa_algorithm_t policy_alg = policy_alg_arg;
1829 psa_algorithm_t exercise_alg = exercise_alg_arg;
1830 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001831 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001832 psa_status_t expected_status_sign = expected_status_sign_arg;
1833 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001834 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 psa_set_key_usage_flags(&attributes, policy_usage);
1839 psa_set_key_algorithm(&attributes, policy_alg);
1840 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1843 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1846 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1849 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001850
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001851 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001853 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1855 input, 128,
1856 mac, PSA_MAC_MAX_SIZE, &mac_len),
1857 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001858
Neil Armstrong3af9b972022-02-07 12:20:21 +01001859 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 PSA_ASSERT(psa_mac_abort(&operation));
1861 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1862 if (status == PSA_SUCCESS) {
1863 status = psa_mac_update(&operation, input, 128);
1864 if (status == PSA_SUCCESS) {
1865 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1866 &mac_len),
1867 expected_status_sign);
1868 } else {
1869 TEST_EQUAL(status, expected_status_sign);
1870 }
1871 } else {
1872 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001873 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001875
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001876 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 status = psa_mac_verify(key, exercise_alg, input, 128,
1878 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1881 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1882 } else {
1883 TEST_EQUAL(status, expected_status_verify);
1884 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001885
Neil Armstrong3af9b972022-02-07 12:20:21 +01001886 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1888 if (status == PSA_SUCCESS) {
1889 status = psa_mac_update(&operation, input, 128);
1890 if (status == PSA_SUCCESS) {
1891 status = psa_mac_verify_finish(&operation, mac, mac_len);
1892 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1893 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1894 } else {
1895 TEST_EQUAL(status, expected_status_verify);
1896 }
1897 } else {
1898 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001899 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 } else {
1901 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001902 }
1903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 memset(mac, 0, sizeof(mac));
1907 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1908 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001909
1910exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 psa_mac_abort(&operation);
1912 psa_destroy_key(key);
1913 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914}
1915/* END_CASE */
1916
1917/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001918void cipher_key_policy(int policy_usage_arg,
1919 int policy_alg,
1920 int key_type,
1921 data_t *key_data,
1922 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001923{
Ronald Cron5425a212020-08-04 14:58:35 +02001924 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001925 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001926 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001927 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001928 size_t output_buffer_size = 0;
1929 size_t input_buffer_size = 0;
1930 size_t output_length = 0;
1931 uint8_t *output = NULL;
1932 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001933 psa_status_t status;
1934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1936 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1937 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 ASSERT_ALLOC(input, input_buffer_size);
1940 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 psa_set_key_usage_flags(&attributes, policy_usage);
1945 psa_set_key_algorithm(&attributes, policy_alg);
1946 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1949 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001950
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001951 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 TEST_EQUAL(policy_usage,
1953 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001954
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001955 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1957 output, output_buffer_size,
1958 &output_length);
1959 if (policy_alg == exercise_alg &&
1960 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1961 PSA_ASSERT(status);
1962 } else {
1963 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1964 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001965
1966 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1968 if (policy_alg == exercise_alg &&
1969 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1970 PSA_ASSERT(status);
1971 } else {
1972 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1973 }
1974 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001975
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001976 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1978 input, input_buffer_size,
1979 &output_length);
1980 if (policy_alg == exercise_alg &&
1981 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1982 PSA_ASSERT(status);
1983 } else {
1984 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1985 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001986
1987 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1989 if (policy_alg == exercise_alg &&
1990 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1991 PSA_ASSERT(status);
1992 } else {
1993 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1994 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995
1996exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 psa_cipher_abort(&operation);
1998 mbedtls_free(input);
1999 mbedtls_free(output);
2000 psa_destroy_key(key);
2001 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002}
2003/* END_CASE */
2004
2005/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002006void aead_key_policy(int policy_usage_arg,
2007 int policy_alg,
2008 int key_type,
2009 data_t *key_data,
2010 int nonce_length_arg,
2011 int tag_length_arg,
2012 int exercise_alg,
2013 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002014{
Ronald Cron5425a212020-08-04 14:58:35 +02002015 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002017 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002018 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002020 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022 size_t nonce_length = nonce_length_arg;
2023 unsigned char tag[16];
2024 size_t tag_length = tag_length_arg;
2025 size_t output_length;
2026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 TEST_LE_U(nonce_length, sizeof(nonce));
2028 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 psa_set_key_usage_flags(&attributes, policy_usage);
2033 psa_set_key_algorithm(&attributes, policy_alg);
2034 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2037 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002039 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 TEST_EQUAL(policy_usage,
2041 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002042
Neil Armstrong752d8112022-02-07 14:51:11 +01002043 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 status = psa_aead_encrypt(key, exercise_alg,
2045 nonce, nonce_length,
2046 NULL, 0,
2047 NULL, 0,
2048 tag, tag_length,
2049 &output_length);
2050 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2051 TEST_EQUAL(status, expected_status);
2052 } else {
2053 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2054 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002055
Neil Armstrong752d8112022-02-07 14:51:11 +01002056 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2058 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2059 TEST_EQUAL(status, expected_status);
2060 } else {
2061 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2062 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002063
2064 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 memset(tag, 0, sizeof(tag));
2066 status = psa_aead_decrypt(key, exercise_alg,
2067 nonce, nonce_length,
2068 NULL, 0,
2069 tag, tag_length,
2070 NULL, 0,
2071 &output_length);
2072 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2073 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2074 } else if (expected_status == PSA_SUCCESS) {
2075 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2076 } else {
2077 TEST_EQUAL(status, expected_status);
2078 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079
Neil Armstrong752d8112022-02-07 14:51:11 +01002080 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 PSA_ASSERT(psa_aead_abort(&operation));
2082 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2083 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2084 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2085 } else {
2086 TEST_EQUAL(status, expected_status);
2087 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002088
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002089exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 PSA_ASSERT(psa_aead_abort(&operation));
2091 psa_destroy_key(key);
2092 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002093}
2094/* END_CASE */
2095
2096/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002097void asymmetric_encryption_key_policy(int policy_usage_arg,
2098 int policy_alg,
2099 int key_type,
2100 data_t *key_data,
2101 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002102{
Ronald Cron5425a212020-08-04 14:58:35 +02002103 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002105 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002106 psa_status_t status;
2107 size_t key_bits;
2108 size_t buffer_length;
2109 unsigned char *buffer = NULL;
2110 size_t output_length;
2111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 psa_set_key_usage_flags(&attributes, policy_usage);
2115 psa_set_key_algorithm(&attributes, policy_alg);
2116 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2119 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002120
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002121 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 TEST_EQUAL(policy_usage,
2123 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2126 key_bits = psa_get_key_bits(&attributes);
2127 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2128 exercise_alg);
2129 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 status = psa_asymmetric_encrypt(key, exercise_alg,
2132 NULL, 0,
2133 NULL, 0,
2134 buffer, buffer_length,
2135 &output_length);
2136 if (policy_alg == exercise_alg &&
2137 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2138 PSA_ASSERT(status);
2139 } else {
2140 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2141 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 if (buffer_length != 0) {
2144 memset(buffer, 0, buffer_length);
2145 }
2146 status = psa_asymmetric_decrypt(key, exercise_alg,
2147 buffer, buffer_length,
2148 NULL, 0,
2149 buffer, buffer_length,
2150 &output_length);
2151 if (policy_alg == exercise_alg &&
2152 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2153 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2154 } else {
2155 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2156 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002157
2158exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002159 /*
2160 * Key attributes may have been returned by psa_get_key_attributes()
2161 * thus reset them as required.
2162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 psa_destroy_key(key);
2166 PSA_DONE();
2167 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002168}
2169/* END_CASE */
2170
2171/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002172void asymmetric_signature_key_policy(int policy_usage_arg,
2173 int policy_alg,
2174 int key_type,
2175 data_t *key_data,
2176 int exercise_alg,
2177 int payload_length_arg,
2178 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179{
Ronald Cron5425a212020-08-04 14:58:35 +02002180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002182 psa_key_usage_t policy_usage = policy_usage_arg;
2183 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002184 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002186 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2187 * compatible with the policy and `payload_length_arg` is supposed to be
2188 * a valid input length to sign. If `payload_length_arg <= 0`,
2189 * `exercise_alg` is supposed to be forbidden by the policy. */
2190 int compatible_alg = payload_length_arg > 0;
2191 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002193 size_t signature_length;
2194
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002195 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002196 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 TEST_EQUAL(expected_usage,
2198 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 psa_set_key_usage_flags(&attributes, policy_usage);
2203 psa_set_key_algorithm(&attributes, policy_alg);
2204 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2207 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 status = psa_sign_hash(key, exercise_alg,
2212 payload, payload_length,
2213 signature, sizeof(signature),
2214 &signature_length);
2215 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2216 PSA_ASSERT(status);
2217 } else {
2218 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2219 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 memset(signature, 0, sizeof(signature));
2222 status = psa_verify_hash(key, exercise_alg,
2223 payload, payload_length,
2224 signature, sizeof(signature));
2225 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2226 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2227 } else {
2228 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2229 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2232 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2233 status = psa_sign_message(key, exercise_alg,
2234 payload, payload_length,
2235 signature, sizeof(signature),
2236 &signature_length);
2237 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2238 PSA_ASSERT(status);
2239 } else {
2240 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2241 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 memset(signature, 0, sizeof(signature));
2244 status = psa_verify_message(key, exercise_alg,
2245 payload, payload_length,
2246 signature, sizeof(signature));
2247 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2248 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2249 } else {
2250 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2251 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002252 }
2253
Gilles Peskined5b33222018-06-18 22:20:03 +02002254exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 psa_destroy_key(key);
2256 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002257}
2258/* END_CASE */
2259
Janos Follathba3fab92019-06-11 14:50:16 +01002260/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002261void derive_key_policy(int policy_usage,
2262 int policy_alg,
2263 int key_type,
2264 data_t *key_data,
2265 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002266{
Ronald Cron5425a212020-08-04 14:58:35 +02002267 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002269 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002270 psa_status_t status;
2271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 psa_set_key_usage_flags(&attributes, policy_usage);
2275 psa_set_key_algorithm(&attributes, policy_alg);
2276 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2279 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2284 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2285 PSA_ASSERT(psa_key_derivation_input_bytes(
2286 &operation,
2287 PSA_KEY_DERIVATION_INPUT_SEED,
2288 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002289 }
Janos Follathba3fab92019-06-11 14:50:16 +01002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 status = psa_key_derivation_input_key(&operation,
2292 PSA_KEY_DERIVATION_INPUT_SECRET,
2293 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 if (policy_alg == exercise_alg &&
2296 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2297 PSA_ASSERT(status);
2298 } else {
2299 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2300 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002301
2302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 psa_key_derivation_abort(&operation);
2304 psa_destroy_key(key);
2305 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002306}
2307/* END_CASE */
2308
2309/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310void agreement_key_policy(int policy_usage,
2311 int policy_alg,
2312 int key_type_arg,
2313 data_t *key_data,
2314 int exercise_alg,
2315 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002316{
Ronald Cron5425a212020-08-04 14:58:35 +02002317 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002319 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002320 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002321 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002322 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 psa_set_key_usage_flags(&attributes, policy_usage);
2327 psa_set_key_algorithm(&attributes, policy_alg);
2328 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2331 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2334 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002337
2338exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 psa_key_derivation_abort(&operation);
2340 psa_destroy_key(key);
2341 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002342}
2343/* END_CASE */
2344
2345/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002346void key_policy_alg2(int key_type_arg, data_t *key_data,
2347 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002348{
Ronald Cron5425a212020-08-04 14:58:35 +02002349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002350 psa_key_type_t key_type = key_type_arg;
2351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2352 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2353 psa_key_usage_t usage = usage_arg;
2354 psa_algorithm_t alg = alg_arg;
2355 psa_algorithm_t alg2 = alg2_arg;
2356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 psa_set_key_usage_flags(&attributes, usage);
2360 psa_set_key_algorithm(&attributes, alg);
2361 psa_set_key_enrollment_algorithm(&attributes, alg2);
2362 psa_set_key_type(&attributes, key_type);
2363 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2364 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002365
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002366 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 usage = mbedtls_test_update_key_usage_flags(usage);
2368 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2369 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2370 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2371 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002374 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 }
2376 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002377 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002379
2380exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002381 /*
2382 * Key attributes may have been returned by psa_get_key_attributes()
2383 * thus reset them as required.
2384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 psa_destroy_key(key);
2388 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002389}
2390/* END_CASE */
2391
2392/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002393void raw_agreement_key_policy(int policy_usage,
2394 int policy_alg,
2395 int key_type_arg,
2396 data_t *key_data,
2397 int exercise_alg,
2398 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002399{
Ronald Cron5425a212020-08-04 14:58:35 +02002400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002402 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002403 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002404 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002405 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 psa_set_key_usage_flags(&attributes, policy_usage);
2410 psa_set_key_algorithm(&attributes, policy_alg);
2411 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2414 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002419
2420exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 psa_key_derivation_abort(&operation);
2422 psa_destroy_key(key);
2423 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002424}
2425/* END_CASE */
2426
2427/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002428void copy_success(int source_usage_arg,
2429 int source_alg_arg, int source_alg2_arg,
2430 unsigned int source_lifetime_arg,
2431 int type_arg, data_t *material,
2432 int copy_attributes,
2433 int target_usage_arg,
2434 int target_alg_arg, int target_alg2_arg,
2435 unsigned int target_lifetime_arg,
2436 int expected_usage_arg,
2437 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002438{
Gilles Peskineca25db92019-04-19 11:43:08 +02002439 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2440 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002441 psa_key_usage_t expected_usage = expected_usage_arg;
2442 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002443 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302444 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2445 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002446 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2447 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002448 uint8_t *export_buffer = NULL;
2449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002451
Gilles Peskineca25db92019-04-19 11:43:08 +02002452 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2454 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2455 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2456 psa_set_key_type(&source_attributes, type_arg);
2457 psa_set_key_lifetime(&source_attributes, source_lifetime);
2458 PSA_ASSERT(psa_import_key(&source_attributes,
2459 material->x, material->len,
2460 &source_key));
2461 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002462
Gilles Peskineca25db92019-04-19 11:43:08 +02002463 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002465 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002466 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 if (target_usage_arg != -1) {
2470 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2471 }
2472 if (target_alg_arg != -1) {
2473 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2474 }
2475 if (target_alg2_arg != -1) {
2476 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2477 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002478
Archana8a180362021-07-05 02:18:48 +05302479
Gilles Peskine57ab7212019-01-28 13:03:09 +01002480 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 PSA_ASSERT(psa_copy_key(source_key,
2482 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002483
2484 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002486
2487 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2489 TEST_EQUAL(psa_get_key_type(&source_attributes),
2490 psa_get_key_type(&target_attributes));
2491 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2492 psa_get_key_bits(&target_attributes));
2493 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2494 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2495 TEST_EQUAL(expected_alg2,
2496 psa_get_key_enrollment_algorithm(&target_attributes));
2497 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002498 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 ASSERT_ALLOC(export_buffer, material->len);
2500 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2501 material->len, &length));
2502 ASSERT_COMPARE(material->x, material->len,
2503 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002504 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (!psa_key_lifetime_is_external(target_lifetime)) {
2507 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302508 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 }
2510 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302511 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 }
Archana8a180362021-07-05 02:18:48 +05302513 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002516
2517exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002518 /*
2519 * Source and target key attributes may have been returned by
2520 * psa_get_key_attributes() thus reset them as required.
2521 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 psa_reset_key_attributes(&source_attributes);
2523 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 PSA_DONE();
2526 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002527}
2528/* END_CASE */
2529
2530/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531void copy_fail(int source_usage_arg,
2532 int source_alg_arg, int source_alg2_arg,
2533 int source_lifetime_arg,
2534 int type_arg, data_t *material,
2535 int target_type_arg, int target_bits_arg,
2536 int target_usage_arg,
2537 int target_alg_arg, int target_alg2_arg,
2538 int target_id_arg, int target_lifetime_arg,
2539 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002540{
2541 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2542 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002543 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2544 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002548
2549 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2551 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2552 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2553 psa_set_key_type(&source_attributes, type_arg);
2554 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2555 PSA_ASSERT(psa_import_key(&source_attributes,
2556 material->x, material->len,
2557 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002558
2559 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 psa_set_key_id(&target_attributes, key_id);
2561 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2562 psa_set_key_type(&target_attributes, target_type_arg);
2563 psa_set_key_bits(&target_attributes, target_bits_arg);
2564 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2565 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2566 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002567
2568 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 TEST_EQUAL(psa_copy_key(source_key,
2570 &target_attributes, &target_key),
2571 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002574
Gilles Peskine4a644642019-05-03 17:14:08 +02002575exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 psa_reset_key_attributes(&source_attributes);
2577 psa_reset_key_attributes(&target_attributes);
2578 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002579}
2580/* END_CASE */
2581
2582/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002583void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002584{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002585 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002586 /* Test each valid way of initializing the object, except for `= {0}`, as
2587 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2588 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002589 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002591 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2592 psa_hash_operation_t zero;
2593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002595
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002596 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2598 PSA_ERROR_BAD_STATE);
2599 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2600 PSA_ERROR_BAD_STATE);
2601 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2602 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002603
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002604 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 PSA_ASSERT(psa_hash_abort(&func));
2606 PSA_ASSERT(psa_hash_abort(&init));
2607 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002608}
2609/* END_CASE */
2610
2611/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002612void hash_setup(int alg_arg,
2613 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002614{
2615 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002616 uint8_t *output = NULL;
2617 size_t output_size = 0;
2618 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002619 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002620 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002621 psa_status_t status;
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002624
Neil Armstrongedb20862022-02-07 15:47:44 +01002625 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 output_size = PSA_HASH_LENGTH(alg);
2627 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 status = psa_hash_compute(alg, NULL, 0,
2630 output, output_size, &output_length);
2631 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002632
2633 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 status = psa_hash_setup(&operation, alg);
2635 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002636
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002637 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002639
2640 /* If setup failed, reproduce the failure, so as to
2641 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (status != PSA_SUCCESS) {
2643 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2644 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002645
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002646 /* Now the operation object should be reusable. */
2647#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2649 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002650#endif
2651
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002652exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 mbedtls_free(output);
2654 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002655}
2656/* END_CASE */
2657
2658/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002659void hash_compute_fail(int alg_arg, data_t *input,
2660 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002661{
2662 psa_algorithm_t alg = alg_arg;
2663 uint8_t *output = NULL;
2664 size_t output_size = output_size_arg;
2665 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002666 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002667 psa_status_t expected_status = expected_status_arg;
2668 psa_status_t status;
2669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002673
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002674 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 status = psa_hash_compute(alg, input->x, input->len,
2676 output, output_size, &output_length);
2677 TEST_EQUAL(status, expected_status);
2678 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002679
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002680 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 status = psa_hash_setup(&operation, alg);
2682 if (status == PSA_SUCCESS) {
2683 status = psa_hash_update(&operation, input->x, input->len);
2684 if (status == PSA_SUCCESS) {
2685 status = psa_hash_finish(&operation, output, output_size,
2686 &output_length);
2687 if (status == PSA_SUCCESS) {
2688 TEST_LE_U(output_length, output_size);
2689 } else {
2690 TEST_EQUAL(status, expected_status);
2691 }
2692 } else {
2693 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002694 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 } else {
2696 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002697 }
2698
Gilles Peskine0a749c82019-11-28 19:33:58 +01002699exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 PSA_ASSERT(psa_hash_abort(&operation));
2701 mbedtls_free(output);
2702 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002703}
2704/* END_CASE */
2705
2706/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002707void hash_compare_fail(int alg_arg, data_t *input,
2708 data_t *reference_hash,
2709 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002710{
2711 psa_algorithm_t alg = alg_arg;
2712 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002713 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002714 psa_status_t status;
2715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002717
Neil Armstrong55a1be12022-02-07 11:23:20 +01002718 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 status = psa_hash_compare(alg, input->x, input->len,
2720 reference_hash->x, reference_hash->len);
2721 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002722
Neil Armstrong55a1be12022-02-07 11:23:20 +01002723 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 status = psa_hash_setup(&operation, alg);
2725 if (status == PSA_SUCCESS) {
2726 status = psa_hash_update(&operation, input->x, input->len);
2727 if (status == PSA_SUCCESS) {
2728 status = psa_hash_verify(&operation, reference_hash->x,
2729 reference_hash->len);
2730 TEST_EQUAL(status, expected_status);
2731 } else {
2732 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002733 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 } else {
2735 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002736 }
2737
Gilles Peskine88e08462020-01-28 20:43:00 +01002738exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 PSA_ASSERT(psa_hash_abort(&operation));
2740 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002741}
2742/* END_CASE */
2743
2744/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002745void hash_compute_compare(int alg_arg, data_t *input,
2746 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002747{
2748 psa_algorithm_t alg = alg_arg;
2749 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2750 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002751 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002752 size_t i;
2753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002755
Neil Armstrongca30a002022-02-07 11:40:23 +01002756 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2758 output, PSA_HASH_LENGTH(alg),
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 tight 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 PSA_HASH_LENGTH(alg),
2769 &output_length));
2770 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2771 ASSERT_COMPARE(output, output_length,
2772 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002773
2774 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2776 output, sizeof(output),
2777 &output_length));
2778 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2779 ASSERT_COMPARE(output, output_length,
2780 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002781
Neil Armstrongca30a002022-02-07 11:40:23 +01002782 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 PSA_ASSERT(psa_hash_setup(&operation, alg));
2784 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2785 PSA_ASSERT(psa_hash_finish(&operation, output,
2786 sizeof(output), &output_length));
2787 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2788 ASSERT_COMPARE(output, output_length,
2789 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002790
2791 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2793 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002794
Neil Armstrongca30a002022-02-07 11:40:23 +01002795 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 PSA_ASSERT(psa_hash_setup(&operation, alg));
2797 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2798 PSA_ASSERT(psa_hash_verify(&operation, output,
2799 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002800
2801 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2803 output, output_length + 1),
2804 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002805
Neil Armstrongca30a002022-02-07 11:40:23 +01002806 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 PSA_ASSERT(psa_hash_setup(&operation, alg));
2808 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2809 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2810 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002811
2812 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2814 output, output_length - 1),
2815 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002816
Neil Armstrongca30a002022-02-07 11:40:23 +01002817 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 PSA_ASSERT(psa_hash_setup(&operation, alg));
2819 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2820 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2821 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002822
Gilles Peskine0a749c82019-11-28 19:33:58 +01002823 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 for (i = 0; i < output_length; i++) {
2825 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002826 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002827
2828 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2830 output, output_length),
2831 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002832
2833 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 PSA_ASSERT(psa_hash_setup(&operation, alg));
2835 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2836 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2837 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002838
Gilles Peskine0a749c82019-11-28 19:33:58 +01002839 output[i] ^= 1;
2840 }
2841
2842exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 PSA_ASSERT(psa_hash_abort(&operation));
2844 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002845}
2846/* END_CASE */
2847
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002848/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002849void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002850{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002851 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002852 unsigned char input[] = "";
2853 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002854 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002855 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2856 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2858 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002859 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002860 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002861 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002862
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002864
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002865 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 PSA_ASSERT(psa_hash_setup(&operation, alg));
2867 ASSERT_OPERATION_IS_ACTIVE(operation);
2868 TEST_EQUAL(psa_hash_setup(&operation, alg),
2869 PSA_ERROR_BAD_STATE);
2870 ASSERT_OPERATION_IS_INACTIVE(operation);
2871 PSA_ASSERT(psa_hash_abort(&operation));
2872 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002873
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002874 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 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
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002879 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002881 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 ASSERT_OPERATION_IS_ACTIVE(operation);
2883 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2884 PSA_ERROR_BAD_STATE);
2885 ASSERT_OPERATION_IS_INACTIVE(operation);
2886 PSA_ASSERT(psa_hash_abort(&operation));
2887 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002888
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002889 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 PSA_ASSERT(psa_hash_setup(&operation, alg));
2891 PSA_ASSERT(psa_hash_finish(&operation,
2892 hash, sizeof(hash), &hash_len));
2893 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2894 PSA_ERROR_BAD_STATE);
2895 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002896
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002897 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 TEST_EQUAL(psa_hash_verify(&operation,
2899 valid_hash, sizeof(valid_hash)),
2900 PSA_ERROR_BAD_STATE);
2901 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002902
2903 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 PSA_ASSERT(psa_hash_setup(&operation, alg));
2905 PSA_ASSERT(psa_hash_finish(&operation,
2906 hash, sizeof(hash), &hash_len));
2907 TEST_EQUAL(psa_hash_verify(&operation,
2908 valid_hash, sizeof(valid_hash)),
2909 PSA_ERROR_BAD_STATE);
2910 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002911
2912 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 PSA_ASSERT(psa_hash_setup(&operation, alg));
2914 ASSERT_OPERATION_IS_ACTIVE(operation);
2915 PSA_ASSERT(psa_hash_verify(&operation,
2916 valid_hash, sizeof(valid_hash)));
2917 ASSERT_OPERATION_IS_INACTIVE(operation);
2918 TEST_EQUAL(psa_hash_verify(&operation,
2919 valid_hash, sizeof(valid_hash)),
2920 PSA_ERROR_BAD_STATE);
2921 ASSERT_OPERATION_IS_INACTIVE(operation);
2922 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002923
2924 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 TEST_EQUAL(psa_hash_finish(&operation,
2926 hash, sizeof(hash), &hash_len),
2927 PSA_ERROR_BAD_STATE);
2928 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002929
2930 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 PSA_ASSERT(psa_hash_setup(&operation, alg));
2932 PSA_ASSERT(psa_hash_finish(&operation,
2933 hash, sizeof(hash), &hash_len));
2934 TEST_EQUAL(psa_hash_finish(&operation,
2935 hash, sizeof(hash), &hash_len),
2936 PSA_ERROR_BAD_STATE);
2937 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002938
2939 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 PSA_ASSERT(psa_hash_setup(&operation, alg));
2941 PSA_ASSERT(psa_hash_verify(&operation,
2942 valid_hash, sizeof(valid_hash)));
2943 TEST_EQUAL(psa_hash_finish(&operation,
2944 hash, sizeof(hash), &hash_len),
2945 PSA_ERROR_BAD_STATE);
2946 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002947
2948exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002950}
2951/* END_CASE */
2952
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002953/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002955{
2956 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002957 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2958 * appended to it */
2959 unsigned char hash[] = {
2960 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2961 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2963 };
2964 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002965 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002968
itayzafrir27e69452018-11-01 14:26:34 +02002969 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 PSA_ASSERT(psa_hash_setup(&operation, alg));
2971 ASSERT_OPERATION_IS_ACTIVE(operation);
2972 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2973 PSA_ERROR_INVALID_SIGNATURE);
2974 ASSERT_OPERATION_IS_INACTIVE(operation);
2975 PSA_ASSERT(psa_hash_abort(&operation));
2976 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002977
itayzafrir27e69452018-11-01 14:26:34 +02002978 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 PSA_ASSERT(psa_hash_setup(&operation, alg));
2980 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2981 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002982
itayzafrir27e69452018-11-01 14:26:34 +02002983 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 PSA_ASSERT(psa_hash_setup(&operation, alg));
2985 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2986 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002987
itayzafrirec93d302018-10-18 18:01:10 +03002988exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002990}
2991/* END_CASE */
2992
Ronald Cronee414c72021-03-18 18:50:08 +01002993/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002994void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002995{
2996 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002997 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002999 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003000 size_t hash_len;
3001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003003
itayzafrir58028322018-10-25 10:22:01 +03003004 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 PSA_ASSERT(psa_hash_setup(&operation, alg));
3006 TEST_EQUAL(psa_hash_finish(&operation,
3007 hash, expected_size - 1, &hash_len),
3008 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003009
3010exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003012}
3013/* END_CASE */
3014
Ronald Cronee414c72021-03-18 18:50:08 +01003015/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003016void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003017{
3018 psa_algorithm_t alg = PSA_ALG_SHA_256;
3019 unsigned char hash[PSA_HASH_MAX_SIZE];
3020 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3021 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3022 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3023 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3024 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3025 size_t hash_len;
3026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 PSA_ASSERT(psa_crypto_init());
3028 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3031 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3032 PSA_ASSERT(psa_hash_finish(&op_finished,
3033 hash, sizeof(hash), &hash_len));
3034 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3035 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3038 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3041 PSA_ASSERT(psa_hash_finish(&op_init,
3042 hash, sizeof(hash), &hash_len));
3043 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3044 PSA_ASSERT(psa_hash_finish(&op_finished,
3045 hash, sizeof(hash), &hash_len));
3046 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3047 PSA_ASSERT(psa_hash_finish(&op_aborted,
3048 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003049
3050exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 psa_hash_abort(&op_source);
3052 psa_hash_abort(&op_init);
3053 psa_hash_abort(&op_setup);
3054 psa_hash_abort(&op_finished);
3055 psa_hash_abort(&op_aborted);
3056 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003057}
3058/* END_CASE */
3059
Ronald Cronee414c72021-03-18 18:50:08 +01003060/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003061void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003062{
3063 psa_algorithm_t alg = PSA_ALG_SHA_256;
3064 unsigned char hash[PSA_HASH_MAX_SIZE];
3065 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3066 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3067 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3068 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3069 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3070 size_t hash_len;
3071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3075 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3076 PSA_ASSERT(psa_hash_finish(&op_finished,
3077 hash, sizeof(hash), &hash_len));
3078 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3079 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3082 PSA_ASSERT(psa_hash_finish(&op_target,
3083 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3086 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3087 PSA_ERROR_BAD_STATE);
3088 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3089 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003090
3091exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 psa_hash_abort(&op_target);
3093 psa_hash_abort(&op_init);
3094 psa_hash_abort(&op_setup);
3095 psa_hash_abort(&op_finished);
3096 psa_hash_abort(&op_aborted);
3097 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003098}
3099/* END_CASE */
3100
itayzafrir58028322018-10-25 10:22:01 +03003101/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003102void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003103{
Jaeden Amero252ef282019-02-15 14:05:35 +00003104 const uint8_t input[1] = { 0 };
3105
Jaeden Amero769ce272019-01-04 11:48:03 +00003106 /* Test each valid way of initializing the object, except for `= {0}`, as
3107 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3108 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003109 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003111 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3112 psa_mac_operation_t zero;
3113
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003115
Jaeden Amero252ef282019-02-15 14:05:35 +00003116 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 TEST_EQUAL(psa_mac_update(&func,
3118 input, sizeof(input)),
3119 PSA_ERROR_BAD_STATE);
3120 TEST_EQUAL(psa_mac_update(&init,
3121 input, sizeof(input)),
3122 PSA_ERROR_BAD_STATE);
3123 TEST_EQUAL(psa_mac_update(&zero,
3124 input, sizeof(input)),
3125 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003126
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003127 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 PSA_ASSERT(psa_mac_abort(&func));
3129 PSA_ASSERT(psa_mac_abort(&init));
3130 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003131}
3132/* END_CASE */
3133
3134/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003135void mac_setup(int key_type_arg,
3136 data_t *key,
3137 int alg_arg,
3138 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003139{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003140 psa_key_type_t key_type = key_type_arg;
3141 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003142 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003143 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003144 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3145#if defined(KNOWN_SUPPORTED_MAC_ALG)
3146 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3147#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3152 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 }
3155 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003156
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003157 /* The operation object should be reusable. */
3158#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3160 smoke_test_key_data,
3161 sizeof(smoke_test_key_data),
3162 KNOWN_SUPPORTED_MAC_ALG,
3163 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 }
3166 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003167#endif
3168
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003169exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003171}
3172/* END_CASE */
3173
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003174/* 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 +01003175void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003176{
Ronald Cron5425a212020-08-04 14:58:35 +02003177 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003178 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3179 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003180 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003181 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3182 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3184 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003186 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3187 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3188 size_t sign_mac_length = 0;
3189 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3190 const uint8_t verify_mac[] = {
3191 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3192 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3194 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 PSA_ASSERT(psa_crypto_init());
3197 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3198 psa_set_key_algorithm(&attributes, alg);
3199 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3202 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003203
Jaeden Amero252ef282019-02-15 14:05:35 +00003204 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3206 PSA_ERROR_BAD_STATE);
3207 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003208
3209 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3211 &sign_mac_length),
3212 PSA_ERROR_BAD_STATE);
3213 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003214
3215 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 TEST_EQUAL(psa_mac_verify_finish(&operation,
3217 verify_mac, sizeof(verify_mac)),
3218 PSA_ERROR_BAD_STATE);
3219 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003220
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003221 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3223 ASSERT_OPERATION_IS_ACTIVE(operation);
3224 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3225 PSA_ERROR_BAD_STATE);
3226 ASSERT_OPERATION_IS_INACTIVE(operation);
3227 PSA_ASSERT(psa_mac_abort(&operation));
3228 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003229
Jaeden Amero252ef282019-02-15 14:05:35 +00003230 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3232 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3233 PSA_ASSERT(psa_mac_sign_finish(&operation,
3234 sign_mac, sizeof(sign_mac),
3235 &sign_mac_length));
3236 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3237 PSA_ERROR_BAD_STATE);
3238 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003239
3240 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3242 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3243 PSA_ASSERT(psa_mac_verify_finish(&operation,
3244 verify_mac, sizeof(verify_mac)));
3245 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3246 PSA_ERROR_BAD_STATE);
3247 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003248
3249 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3251 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3252 PSA_ASSERT(psa_mac_sign_finish(&operation,
3253 sign_mac, sizeof(sign_mac),
3254 &sign_mac_length));
3255 TEST_EQUAL(psa_mac_sign_finish(&operation,
3256 sign_mac, sizeof(sign_mac),
3257 &sign_mac_length),
3258 PSA_ERROR_BAD_STATE);
3259 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003260
3261 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3263 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3264 PSA_ASSERT(psa_mac_verify_finish(&operation,
3265 verify_mac, sizeof(verify_mac)));
3266 TEST_EQUAL(psa_mac_verify_finish(&operation,
3267 verify_mac, sizeof(verify_mac)),
3268 PSA_ERROR_BAD_STATE);
3269 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003270
3271 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3273 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3274 ASSERT_OPERATION_IS_ACTIVE(operation);
3275 TEST_EQUAL(psa_mac_verify_finish(&operation,
3276 verify_mac, sizeof(verify_mac)),
3277 PSA_ERROR_BAD_STATE);
3278 ASSERT_OPERATION_IS_INACTIVE(operation);
3279 PSA_ASSERT(psa_mac_abort(&operation));
3280 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003281
3282 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3284 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3285 ASSERT_OPERATION_IS_ACTIVE(operation);
3286 TEST_EQUAL(psa_mac_sign_finish(&operation,
3287 sign_mac, sizeof(sign_mac),
3288 &sign_mac_length),
3289 PSA_ERROR_BAD_STATE);
3290 ASSERT_OPERATION_IS_INACTIVE(operation);
3291 PSA_ASSERT(psa_mac_abort(&operation));
3292 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003293
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003295
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003296exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003298}
3299/* END_CASE */
3300
3301/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003302void mac_sign_verify_multi(int key_type_arg,
3303 data_t *key_data,
3304 int alg_arg,
3305 data_t *input,
3306 int is_verify,
3307 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003308{
3309 size_t data_part_len = 0;
3310
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003312 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003314
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 if (mac_multipart_internal_func(key_type_arg, key_data,
3316 alg_arg,
3317 input, data_part_len,
3318 expected_mac,
3319 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003320 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003322
3323 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003325
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 if (mac_multipart_internal_func(key_type_arg, key_data,
3327 alg_arg,
3328 input, data_part_len,
3329 expected_mac,
3330 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003331 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003333 }
3334
3335 /* Goto is required to silence warnings about unused labels, as we
3336 * don't actually do any test assertions in this function. */
3337 goto exit;
3338}
3339/* END_CASE */
3340
3341/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003342void mac_sign(int key_type_arg,
3343 data_t *key_data,
3344 int alg_arg,
3345 data_t *input,
3346 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003347{
Ronald Cron5425a212020-08-04 14:58:35 +02003348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003349 psa_key_type_t key_type = key_type_arg;
3350 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003351 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003353 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003354 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003356 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003357 const size_t output_sizes_to_test[] = {
3358 0,
3359 1,
3360 expected_mac->len - 1,
3361 expected_mac->len,
3362 expected_mac->len + 1,
3363 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003364
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003366 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3372 psa_set_key_algorithm(&attributes, alg);
3373 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003374
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3376 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003379 const size_t output_size = output_sizes_to_test[i];
3380 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 (output_size >= expected_mac->len ? PSA_SUCCESS :
3382 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 mbedtls_test_set_step(output_size);
3385 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003386
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003387 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 TEST_EQUAL(psa_mac_compute(key, alg,
3389 input->x, input->len,
3390 actual_mac, output_size, &mac_length),
3391 expected_status);
3392 if (expected_status == PSA_SUCCESS) {
3393 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3394 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003395 }
3396
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 if (output_size > 0) {
3398 memset(actual_mac, 0, output_size);
3399 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003400
3401 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3403 PSA_ASSERT(psa_mac_update(&operation,
3404 input->x, input->len));
3405 TEST_EQUAL(psa_mac_sign_finish(&operation,
3406 actual_mac, output_size,
3407 &mac_length),
3408 expected_status);
3409 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003410
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 if (expected_status == PSA_SUCCESS) {
3412 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3413 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003414 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003416 actual_mac = NULL;
3417 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003418
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003419exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 psa_mac_abort(&operation);
3421 psa_destroy_key(key);
3422 PSA_DONE();
3423 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003424}
3425/* END_CASE */
3426
3427/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003428void mac_verify(int key_type_arg,
3429 data_t *key_data,
3430 int alg_arg,
3431 data_t *input,
3432 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003433{
Ronald Cron5425a212020-08-04 14:58:35 +02003434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003435 psa_key_type_t key_type = key_type_arg;
3436 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003437 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003439 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003440
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003442
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003444
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3446 psa_set_key_algorithm(&attributes, alg);
3447 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003448
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3450 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003451
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003452 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3454 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003455
3456 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3458 PSA_ASSERT(psa_mac_update(&operation,
3459 input->x, input->len));
3460 PSA_ASSERT(psa_mac_verify_finish(&operation,
3461 expected_mac->x,
3462 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003463
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003464 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 TEST_EQUAL(psa_mac_verify(key, alg,
3466 input->x, input->len,
3467 expected_mac->x,
3468 expected_mac->len - 1),
3469 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003470
3471 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3473 PSA_ASSERT(psa_mac_update(&operation,
3474 input->x, input->len));
3475 TEST_EQUAL(psa_mac_verify_finish(&operation,
3476 expected_mac->x,
3477 expected_mac->len - 1),
3478 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003479
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003480 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3482 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3483 TEST_EQUAL(psa_mac_verify(key, alg,
3484 input->x, input->len,
3485 perturbed_mac, expected_mac->len + 1),
3486 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003487
3488 /* Test a MAC that's too long, multi-part case. */
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 + 1),
3495 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003496
3497 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 for (size_t i = 0; i < expected_mac->len; i++) {
3499 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003500 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003501
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 TEST_EQUAL(psa_mac_verify(key, alg,
3503 input->x, input->len,
3504 perturbed_mac, expected_mac->len),
3505 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003506
Gilles Peskine449bd832023-01-11 14:50:10 +01003507 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3508 PSA_ASSERT(psa_mac_update(&operation,
3509 input->x, input->len));
3510 TEST_EQUAL(psa_mac_verify_finish(&operation,
3511 perturbed_mac,
3512 expected_mac->len),
3513 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003514 perturbed_mac[i] ^= 1;
3515 }
3516
Gilles Peskine8c9def32018-02-08 10:02:12 +01003517exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 psa_mac_abort(&operation);
3519 psa_destroy_key(key);
3520 PSA_DONE();
3521 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003522}
3523/* END_CASE */
3524
3525/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003526void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003527{
Jaeden Ameroab439972019-02-15 14:12:05 +00003528 const uint8_t input[1] = { 0 };
3529 unsigned char output[1] = { 0 };
3530 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003531 /* Test each valid way of initializing the object, except for `= {0}`, as
3532 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3533 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003534 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003536 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3537 psa_cipher_operation_t zero;
3538
Gilles Peskine449bd832023-01-11 14:50:10 +01003539 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003540
Jaeden Ameroab439972019-02-15 14:12:05 +00003541 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003542 TEST_EQUAL(psa_cipher_update(&func,
3543 input, sizeof(input),
3544 output, sizeof(output),
3545 &output_length),
3546 PSA_ERROR_BAD_STATE);
3547 TEST_EQUAL(psa_cipher_update(&init,
3548 input, sizeof(input),
3549 output, sizeof(output),
3550 &output_length),
3551 PSA_ERROR_BAD_STATE);
3552 TEST_EQUAL(psa_cipher_update(&zero,
3553 input, sizeof(input),
3554 output, sizeof(output),
3555 &output_length),
3556 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003557
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003558 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 PSA_ASSERT(psa_cipher_abort(&func));
3560 PSA_ASSERT(psa_cipher_abort(&init));
3561 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003562}
3563/* END_CASE */
3564
3565/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003566void cipher_setup(int key_type_arg,
3567 data_t *key,
3568 int alg_arg,
3569 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003570{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003571 psa_key_type_t key_type = key_type_arg;
3572 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003573 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003574 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003575 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003576#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003577 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3578#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003579
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003581
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3583 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003584 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003585 }
3586 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003587
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003588 /* The operation object should be reusable. */
3589#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3591 smoke_test_key_data,
3592 sizeof(smoke_test_key_data),
3593 KNOWN_SUPPORTED_CIPHER_ALG,
3594 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003595 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003596 }
3597 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003598#endif
3599
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003600exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 psa_cipher_abort(&operation);
3602 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003603}
3604/* END_CASE */
3605
Ronald Cronee414c72021-03-18 18:50:08 +01003606/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003607void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003608{
Ronald Cron5425a212020-08-04 14:58:35 +02003609 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003610 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3611 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003613 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003614 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003615 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003616 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003617 0xaa, 0xaa, 0xaa, 0xaa
3618 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003619 const uint8_t text[] = {
3620 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003621 0xbb, 0xbb, 0xbb, 0xbb
3622 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003623 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003624 size_t length = 0;
3625
Gilles Peskine449bd832023-01-11 14:50:10 +01003626 PSA_ASSERT(psa_crypto_init());
3627 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3628 psa_set_key_algorithm(&attributes, alg);
3629 psa_set_key_type(&attributes, key_type);
3630 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3631 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003632
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003633 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3635 ASSERT_OPERATION_IS_ACTIVE(operation);
3636 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3637 PSA_ERROR_BAD_STATE);
3638 ASSERT_OPERATION_IS_INACTIVE(operation);
3639 PSA_ASSERT(psa_cipher_abort(&operation));
3640 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003641
3642 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003643 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3644 ASSERT_OPERATION_IS_ACTIVE(operation);
3645 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3646 PSA_ERROR_BAD_STATE);
3647 ASSERT_OPERATION_IS_INACTIVE(operation);
3648 PSA_ASSERT(psa_cipher_abort(&operation));
3649 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003650
Jaeden Ameroab439972019-02-15 14:12:05 +00003651 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003652 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3653 buffer, sizeof(buffer),
3654 &length),
3655 PSA_ERROR_BAD_STATE);
3656 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003657
3658 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003659 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3660 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3661 buffer, sizeof(buffer),
3662 &length));
3663 ASSERT_OPERATION_IS_ACTIVE(operation);
3664 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3665 buffer, sizeof(buffer),
3666 &length),
3667 PSA_ERROR_BAD_STATE);
3668 ASSERT_OPERATION_IS_INACTIVE(operation);
3669 PSA_ASSERT(psa_cipher_abort(&operation));
3670 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003671
3672 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003673 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3674 PSA_ASSERT(psa_cipher_set_iv(&operation,
3675 iv, sizeof(iv)));
3676 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3677 buffer, sizeof(buffer),
3678 &length),
3679 PSA_ERROR_BAD_STATE);
3680 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003681
3682 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003683 TEST_EQUAL(psa_cipher_set_iv(&operation,
3684 iv, sizeof(iv)),
3685 PSA_ERROR_BAD_STATE);
3686 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003687
3688 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003689 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3690 PSA_ASSERT(psa_cipher_set_iv(&operation,
3691 iv, sizeof(iv)));
3692 ASSERT_OPERATION_IS_ACTIVE(operation);
3693 TEST_EQUAL(psa_cipher_set_iv(&operation,
3694 iv, sizeof(iv)),
3695 PSA_ERROR_BAD_STATE);
3696 ASSERT_OPERATION_IS_INACTIVE(operation);
3697 PSA_ASSERT(psa_cipher_abort(&operation));
3698 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003699
3700 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003701 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3702 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3703 buffer, sizeof(buffer),
3704 &length));
3705 TEST_EQUAL(psa_cipher_set_iv(&operation,
3706 iv, sizeof(iv)),
3707 PSA_ERROR_BAD_STATE);
3708 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003709
3710 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003711 TEST_EQUAL(psa_cipher_update(&operation,
3712 text, sizeof(text),
3713 buffer, sizeof(buffer),
3714 &length),
3715 PSA_ERROR_BAD_STATE);
3716 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003717
3718 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3720 ASSERT_OPERATION_IS_ACTIVE(operation);
3721 TEST_EQUAL(psa_cipher_update(&operation,
3722 text, sizeof(text),
3723 buffer, sizeof(buffer),
3724 &length),
3725 PSA_ERROR_BAD_STATE);
3726 ASSERT_OPERATION_IS_INACTIVE(operation);
3727 PSA_ASSERT(psa_cipher_abort(&operation));
3728 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003729
3730 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003731 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3732 PSA_ASSERT(psa_cipher_set_iv(&operation,
3733 iv, sizeof(iv)));
3734 PSA_ASSERT(psa_cipher_finish(&operation,
3735 buffer, sizeof(buffer), &length));
3736 TEST_EQUAL(psa_cipher_update(&operation,
3737 text, sizeof(text),
3738 buffer, sizeof(buffer),
3739 &length),
3740 PSA_ERROR_BAD_STATE);
3741 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003742
3743 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003744 TEST_EQUAL(psa_cipher_finish(&operation,
3745 buffer, sizeof(buffer), &length),
3746 PSA_ERROR_BAD_STATE);
3747 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003748
3749 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003750 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003751 /* Not calling update means we are encrypting an empty buffer, which is OK
3752 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003753 ASSERT_OPERATION_IS_ACTIVE(operation);
3754 TEST_EQUAL(psa_cipher_finish(&operation,
3755 buffer, sizeof(buffer), &length),
3756 PSA_ERROR_BAD_STATE);
3757 ASSERT_OPERATION_IS_INACTIVE(operation);
3758 PSA_ASSERT(psa_cipher_abort(&operation));
3759 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003760
3761 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003762 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3763 PSA_ASSERT(psa_cipher_set_iv(&operation,
3764 iv, sizeof(iv)));
3765 PSA_ASSERT(psa_cipher_finish(&operation,
3766 buffer, sizeof(buffer), &length));
3767 TEST_EQUAL(psa_cipher_finish(&operation,
3768 buffer, sizeof(buffer), &length),
3769 PSA_ERROR_BAD_STATE);
3770 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003771
Gilles Peskine449bd832023-01-11 14:50:10 +01003772 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003773
Jaeden Ameroab439972019-02-15 14:12:05 +00003774exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003775 psa_cipher_abort(&operation);
3776 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003777}
3778/* END_CASE */
3779
3780/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003781void cipher_encrypt_fail(int alg_arg,
3782 int key_type_arg,
3783 data_t *key_data,
3784 data_t *input,
3785 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003786{
Ronald Cron5425a212020-08-04 14:58:35 +02003787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003788 psa_status_t status;
3789 psa_key_type_t key_type = key_type_arg;
3790 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003791 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003792 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003793 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3794 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003795 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003796 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003797 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003798 size_t function_output_length;
3799 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3801
Gilles Peskine449bd832023-01-11 14:50:10 +01003802 if (PSA_ERROR_BAD_STATE != expected_status) {
3803 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003804
Gilles Peskine449bd832023-01-11 14:50:10 +01003805 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3806 psa_set_key_algorithm(&attributes, alg);
3807 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003808
Gilles Peskine449bd832023-01-11 14:50:10 +01003809 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3810 input->len);
3811 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3814 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003815 }
3816
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003817 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3819 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003820
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003822
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003823 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003824 status = psa_cipher_encrypt_setup(&operation, key, alg);
3825 if (status == PSA_SUCCESS) {
3826 if (alg != PSA_ALG_ECB_NO_PADDING) {
3827 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3828 iv, iv_size,
3829 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003830 }
3831
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 status = psa_cipher_update(&operation, input->x, input->len,
3833 output, output_buffer_size,
3834 &function_output_length);
3835 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003836 output_length += function_output_length;
3837
Gilles Peskine449bd832023-01-11 14:50:10 +01003838 status = psa_cipher_finish(&operation, output + output_length,
3839 output_buffer_size - output_length,
3840 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003841
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 TEST_EQUAL(status, expected_status);
3843 } else {
3844 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003845 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003846 } else {
3847 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003848 }
3849
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003850exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003851 psa_cipher_abort(&operation);
3852 mbedtls_free(output);
3853 psa_destroy_key(key);
3854 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003855}
3856/* END_CASE */
3857
3858/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003859void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3860 data_t *input, int iv_length,
3861 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003862{
3863 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3864 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3866 size_t output_buffer_size = 0;
3867 unsigned char *output = NULL;
3868
Gilles Peskine449bd832023-01-11 14:50:10 +01003869 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3870 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003871
Gilles Peskine449bd832023-01-11 14:50:10 +01003872 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003873
Gilles Peskine449bd832023-01-11 14:50:10 +01003874 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3875 psa_set_key_algorithm(&attributes, alg);
3876 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003877
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3879 &key));
3880 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3881 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3882 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003883
3884exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003885 psa_cipher_abort(&operation);
3886 mbedtls_free(output);
3887 psa_destroy_key(key);
3888 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003889}
3890/* END_CASE */
3891
3892/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003893void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3894 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003895{
3896 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3897 psa_key_type_t key_type = key_type_arg;
3898 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003899 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3900 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003901 unsigned char *output = NULL;
3902 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003903 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003904 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3905
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003907
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003908 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 TEST_LE_U(ciphertext->len,
3910 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3911 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3912 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3913 TEST_LE_U(plaintext->len,
3914 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3915 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3916 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003917
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003918
3919 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 psa_set_key_usage_flags(&attributes,
3921 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3922 psa_set_key_algorithm(&attributes, alg);
3923 psa_set_key_type(&attributes, key_type);
3924 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3925 &key));
3926 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3927 plaintext->len);
3928 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003929
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003930 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3932 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3933 PSA_ERROR_BAD_STATE);
3934 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3935 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3936 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003937
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003938 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3940 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3941 &length),
3942 PSA_ERROR_BAD_STATE);
3943 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3944 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3945 &length),
3946 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003947
Gilles Peskine286c3142022-04-20 17:09:38 +02003948 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 PSA_ASSERT(psa_cipher_encrypt_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 plaintext->x, plaintext->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(ciphertext->x, ciphertext->len,
3964 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003965
Gilles Peskine286c3142022-04-20 17:09:38 +02003966 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003968 output_length = 0;
3969 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 PSA_ASSERT(psa_cipher_update(&operation,
3971 ciphertext->x, ciphertext->len,
3972 output, output_buffer_size,
3973 &length));
3974 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003975 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 PSA_ASSERT(psa_cipher_finish(&operation,
3977 mbedtls_buffer_offset(output, output_length),
3978 output_buffer_size - output_length,
3979 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003980 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 ASSERT_COMPARE(plaintext->x, plaintext->len,
3982 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003983
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003984 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003985 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3987 output, output_buffer_size,
3988 &output_length));
3989 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3990 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003991
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003992 /* One-shot decryption */
3993 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3995 output, output_buffer_size,
3996 &output_length));
3997 ASSERT_COMPARE(plaintext->x, plaintext->len,
3998 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003999
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004000exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 PSA_ASSERT(psa_cipher_abort(&operation));
4002 mbedtls_free(output);
4003 psa_cipher_abort(&operation);
4004 psa_destroy_key(key);
4005 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004006}
4007/* END_CASE */
4008
4009/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004010void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004011{
4012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4013 psa_algorithm_t alg = alg_arg;
4014 psa_key_type_t key_type = key_type_arg;
4015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4016 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4017 psa_status_t status;
4018
Gilles Peskine449bd832023-01-11 14:50:10 +01004019 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004020
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4022 psa_set_key_algorithm(&attributes, alg);
4023 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004024
4025 /* Usage of either of these two size macros would cause divide by zero
4026 * with incorrect key types previously. Input length should be irrelevant
4027 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4029 0);
4030 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004031
4032
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4034 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004035
4036 /* Should fail due to invalid alg type (to support invalid key type).
4037 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004039
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004041
4042exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 psa_cipher_abort(&operation);
4044 psa_destroy_key(key);
4045 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004046}
4047/* END_CASE */
4048
4049/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004050void cipher_encrypt_validation(int alg_arg,
4051 int key_type_arg,
4052 data_t *key_data,
4053 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004054{
4055 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4056 psa_key_type_t key_type = key_type_arg;
4057 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004059 unsigned char *output1 = NULL;
4060 size_t output1_buffer_size = 0;
4061 size_t output1_length = 0;
4062 unsigned char *output2 = NULL;
4063 size_t output2_buffer_size = 0;
4064 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004065 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004066 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004068
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004070
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4072 psa_set_key_algorithm(&attributes, alg);
4073 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4076 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4077 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4078 ASSERT_ALLOC(output1, output1_buffer_size);
4079 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4082 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004083
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004084 /* The one-shot cipher encryption uses generated iv so validating
4085 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4087 output1_buffer_size, &output1_length));
4088 TEST_LE_U(output1_length,
4089 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4090 TEST_LE_U(output1_length,
4091 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004092
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4094 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 PSA_ASSERT(psa_cipher_update(&operation,
4097 input->x, input->len,
4098 output2, output2_buffer_size,
4099 &function_output_length));
4100 TEST_LE_U(function_output_length,
4101 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4102 TEST_LE_U(function_output_length,
4103 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004104 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004105
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 PSA_ASSERT(psa_cipher_finish(&operation,
4107 output2 + output2_length,
4108 output2_buffer_size - output2_length,
4109 &function_output_length));
4110 TEST_LE_U(function_output_length,
4111 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4112 TEST_LE_U(function_output_length,
4113 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004114 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004115
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 PSA_ASSERT(psa_cipher_abort(&operation));
4117 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4118 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004119
Gilles Peskine50e586b2018-06-08 14:28:46 +02004120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 psa_cipher_abort(&operation);
4122 mbedtls_free(output1);
4123 mbedtls_free(output2);
4124 psa_destroy_key(key);
4125 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004126}
4127/* END_CASE */
4128
4129/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004130void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4131 data_t *key_data, data_t *iv,
4132 data_t *input,
4133 int first_part_size_arg,
4134 int output1_length_arg, int output2_length_arg,
4135 data_t *expected_output,
4136 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004137{
Ronald Cron5425a212020-08-04 14:58:35 +02004138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004139 psa_key_type_t key_type = key_type_arg;
4140 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004141 psa_status_t status;
4142 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004143 size_t first_part_size = first_part_size_arg;
4144 size_t output1_length = output1_length_arg;
4145 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004146 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004147 size_t output_buffer_size = 0;
4148 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004149 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004150 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4156 psa_set_key_algorithm(&attributes, alg);
4157 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4160 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 if (iv->len > 0) {
4165 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004166 }
4167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4169 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4170 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 TEST_LE_U(first_part_size, input->len);
4173 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4174 output, output_buffer_size,
4175 &function_output_length));
4176 TEST_ASSERT(function_output_length == output1_length);
4177 TEST_LE_U(function_output_length,
4178 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4179 TEST_LE_U(function_output_length,
4180 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004181 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 if (first_part_size < input->len) {
4184 PSA_ASSERT(psa_cipher_update(&operation,
4185 input->x + first_part_size,
4186 input->len - first_part_size,
4187 (output_buffer_size == 0 ? NULL :
4188 output + total_output_length),
4189 output_buffer_size - total_output_length,
4190 &function_output_length));
4191 TEST_ASSERT(function_output_length == output2_length);
4192 TEST_LE_U(function_output_length,
4193 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4194 alg,
4195 input->len - first_part_size));
4196 TEST_LE_U(function_output_length,
4197 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004198 total_output_length += function_output_length;
4199 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 status = psa_cipher_finish(&operation,
4202 (output_buffer_size == 0 ? NULL :
4203 output + total_output_length),
4204 output_buffer_size - total_output_length,
4205 &function_output_length);
4206 TEST_LE_U(function_output_length,
4207 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4208 TEST_LE_U(function_output_length,
4209 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004210 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 if (expected_status == PSA_SUCCESS) {
4214 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 ASSERT_COMPARE(expected_output->x, expected_output->len,
4217 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004218 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004219
4220exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 psa_cipher_abort(&operation);
4222 mbedtls_free(output);
4223 psa_destroy_key(key);
4224 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004225}
4226/* END_CASE */
4227
4228/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004229void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4230 data_t *key_data, data_t *iv,
4231 data_t *input,
4232 int first_part_size_arg,
4233 int output1_length_arg, int output2_length_arg,
4234 data_t *expected_output,
4235 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004236{
Ronald Cron5425a212020-08-04 14:58:35 +02004237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004238 psa_key_type_t key_type = key_type_arg;
4239 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004240 psa_status_t status;
4241 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004242 size_t first_part_size = first_part_size_arg;
4243 size_t output1_length = output1_length_arg;
4244 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004245 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004246 size_t output_buffer_size = 0;
4247 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004248 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004249 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4255 psa_set_key_algorithm(&attributes, alg);
4256 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4259 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004260
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 if (iv->len > 0) {
4264 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004265 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4268 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4269 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004270
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 TEST_LE_U(first_part_size, input->len);
4272 PSA_ASSERT(psa_cipher_update(&operation,
4273 input->x, first_part_size,
4274 output, output_buffer_size,
4275 &function_output_length));
4276 TEST_ASSERT(function_output_length == output1_length);
4277 TEST_LE_U(function_output_length,
4278 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4279 TEST_LE_U(function_output_length,
4280 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004281 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004282
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 if (first_part_size < input->len) {
4284 PSA_ASSERT(psa_cipher_update(&operation,
4285 input->x + first_part_size,
4286 input->len - first_part_size,
4287 (output_buffer_size == 0 ? NULL :
4288 output + total_output_length),
4289 output_buffer_size - total_output_length,
4290 &function_output_length));
4291 TEST_ASSERT(function_output_length == output2_length);
4292 TEST_LE_U(function_output_length,
4293 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4294 alg,
4295 input->len - first_part_size));
4296 TEST_LE_U(function_output_length,
4297 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004298 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004299 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 status = psa_cipher_finish(&operation,
4302 (output_buffer_size == 0 ? NULL :
4303 output + total_output_length),
4304 output_buffer_size - total_output_length,
4305 &function_output_length);
4306 TEST_LE_U(function_output_length,
4307 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4308 TEST_LE_U(function_output_length,
4309 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004310 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004312
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 if (expected_status == PSA_SUCCESS) {
4314 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 ASSERT_COMPARE(expected_output->x, expected_output->len,
4317 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004318 }
4319
Gilles Peskine50e586b2018-06-08 14:28:46 +02004320exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 psa_cipher_abort(&operation);
4322 mbedtls_free(output);
4323 psa_destroy_key(key);
4324 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004325}
4326/* END_CASE */
4327
Gilles Peskine50e586b2018-06-08 14:28:46 +02004328/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004329void cipher_decrypt_fail(int alg_arg,
4330 int key_type_arg,
4331 data_t *key_data,
4332 data_t *iv,
4333 data_t *input_arg,
4334 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004335{
4336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4337 psa_status_t status;
4338 psa_key_type_t key_type = key_type_arg;
4339 psa_algorithm_t alg = alg_arg;
4340 psa_status_t expected_status = expected_status_arg;
4341 unsigned char *input = NULL;
4342 size_t input_buffer_size = 0;
4343 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004344 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004345 size_t output_buffer_size = 0;
4346 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004347 size_t function_output_length;
4348 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 if (PSA_ERROR_BAD_STATE != expected_status) {
4352 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4355 psa_set_key_algorithm(&attributes, alg);
4356 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4359 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004360 }
4361
4362 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4364 if (input_buffer_size > 0) {
4365 ASSERT_ALLOC(input, input_buffer_size);
4366 memcpy(input, iv->x, iv->len);
4367 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004368 }
4369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4371 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004372
Neil Armstrong66a479f2022-02-07 15:41:19 +01004373 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4375 output_buffer_size, &output_length);
4376 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004377
Neil Armstrong66a479f2022-02-07 15:41:19 +01004378 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 status = psa_cipher_decrypt_setup(&operation, key, alg);
4380 if (status == PSA_SUCCESS) {
4381 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4382 input_arg->len) +
4383 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4384 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004385
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 if (iv->len > 0) {
4387 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004388
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 if (status != PSA_SUCCESS) {
4390 TEST_EQUAL(status, expected_status);
4391 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004392 }
4393
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 if (status == PSA_SUCCESS) {
4395 status = psa_cipher_update(&operation,
4396 input_arg->x, input_arg->len,
4397 output_multi, output_buffer_size,
4398 &function_output_length);
4399 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004400 output_length = function_output_length;
4401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 status = psa_cipher_finish(&operation,
4403 output_multi + output_length,
4404 output_buffer_size - output_length,
4405 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 TEST_EQUAL(status, expected_status);
4408 } else {
4409 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004410 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 } else {
4412 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004413 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 } else {
4415 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004416 }
4417
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 psa_cipher_abort(&operation);
4420 mbedtls_free(input);
4421 mbedtls_free(output);
4422 mbedtls_free(output_multi);
4423 psa_destroy_key(key);
4424 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004425}
4426/* END_CASE */
4427
4428/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004429void cipher_decrypt(int alg_arg,
4430 int key_type_arg,
4431 data_t *key_data,
4432 data_t *iv,
4433 data_t *input_arg,
4434 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004435{
4436 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4437 psa_key_type_t key_type = key_type_arg;
4438 psa_algorithm_t alg = alg_arg;
4439 unsigned char *input = NULL;
4440 size_t input_buffer_size = 0;
4441 unsigned char *output = NULL;
4442 size_t output_buffer_size = 0;
4443 size_t output_length = 0;
4444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4445
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004447
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4449 psa_set_key_algorithm(&attributes, alg);
4450 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004451
4452 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4454 if (input_buffer_size > 0) {
4455 ASSERT_ALLOC(input, input_buffer_size);
4456 memcpy(input, iv->x, iv->len);
4457 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004458 }
4459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4461 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4464 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4467 output_buffer_size, &output_length));
4468 TEST_LE_U(output_length,
4469 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4470 TEST_LE_U(output_length,
4471 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 ASSERT_COMPARE(expected_output->x, expected_output->len,
4474 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004475exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 mbedtls_free(input);
4477 mbedtls_free(output);
4478 psa_destroy_key(key);
4479 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004480}
4481/* END_CASE */
4482
4483/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004484void cipher_verify_output(int alg_arg,
4485 int key_type_arg,
4486 data_t *key_data,
4487 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004488{
Ronald Cron5425a212020-08-04 14:58:35 +02004489 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004490 psa_key_type_t key_type = key_type_arg;
4491 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004492 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004493 size_t output1_size = 0;
4494 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004495 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004496 size_t output2_size = 0;
4497 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004499
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004501
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4503 psa_set_key_algorithm(&attributes, alg);
4504 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4507 &key));
4508 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4509 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4512 output1, output1_size,
4513 &output1_length));
4514 TEST_LE_U(output1_length,
4515 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4516 TEST_LE_U(output1_length,
4517 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004518
4519 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4523 output2, output2_size,
4524 &output2_length));
4525 TEST_LE_U(output2_length,
4526 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4527 TEST_LE_U(output2_length,
4528 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004529
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004531
4532exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 mbedtls_free(output1);
4534 mbedtls_free(output2);
4535 psa_destroy_key(key);
4536 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004537}
4538/* END_CASE */
4539
4540/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004541void cipher_verify_output_multipart(int alg_arg,
4542 int key_type_arg,
4543 data_t *key_data,
4544 data_t *input,
4545 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004546{
Ronald Cron5425a212020-08-04 14:58:35 +02004547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004548 psa_key_type_t key_type = key_type_arg;
4549 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004550 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004552 size_t iv_size = 16;
4553 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004554 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004555 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004556 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004557 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004558 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004559 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004560 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004561 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4562 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4568 psa_set_key_algorithm(&attributes, alg);
4569 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4572 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4575 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 if (alg != PSA_ALG_ECB_NO_PADDING) {
4578 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4579 iv, iv_size,
4580 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004581 }
4582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4584 TEST_LE_U(output1_buffer_size,
4585 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4586 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4591 output1, output1_buffer_size,
4592 &function_output_length));
4593 TEST_LE_U(function_output_length,
4594 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4595 TEST_LE_U(function_output_length,
4596 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004597 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 PSA_ASSERT(psa_cipher_update(&operation1,
4600 input->x + first_part_size,
4601 input->len - first_part_size,
4602 output1, output1_buffer_size,
4603 &function_output_length));
4604 TEST_LE_U(function_output_length,
4605 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4606 alg,
4607 input->len - first_part_size));
4608 TEST_LE_U(function_output_length,
4609 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004610 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 PSA_ASSERT(psa_cipher_finish(&operation1,
4613 output1 + output1_length,
4614 output1_buffer_size - output1_length,
4615 &function_output_length));
4616 TEST_LE_U(function_output_length,
4617 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4618 TEST_LE_U(function_output_length,
4619 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004620 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004623
Gilles Peskine048b7f02018-06-08 14:20:49 +02004624 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 TEST_LE_U(output2_buffer_size,
4626 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4627 TEST_LE_U(output2_buffer_size,
4628 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4629 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 if (iv_length > 0) {
4632 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4633 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004634 }
Moran Pekerded84402018-06-06 16:36:50 +03004635
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4637 output2, output2_buffer_size,
4638 &function_output_length));
4639 TEST_LE_U(function_output_length,
4640 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4641 TEST_LE_U(function_output_length,
4642 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004643 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 PSA_ASSERT(psa_cipher_update(&operation2,
4646 output1 + first_part_size,
4647 output1_length - first_part_size,
4648 output2, output2_buffer_size,
4649 &function_output_length));
4650 TEST_LE_U(function_output_length,
4651 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4652 alg,
4653 output1_length - first_part_size));
4654 TEST_LE_U(function_output_length,
4655 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004656 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004657
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 PSA_ASSERT(psa_cipher_finish(&operation2,
4659 output2 + output2_length,
4660 output2_buffer_size - output2_length,
4661 &function_output_length));
4662 TEST_LE_U(function_output_length,
4663 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4664 TEST_LE_U(function_output_length,
4665 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004666 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004671
4672exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 psa_cipher_abort(&operation1);
4674 psa_cipher_abort(&operation2);
4675 mbedtls_free(output1);
4676 mbedtls_free(output2);
4677 psa_destroy_key(key);
4678 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004679}
4680/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004681
Gilles Peskine20035e32018-02-03 22:44:14 +01004682/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004683void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4684 int alg_arg,
4685 data_t *nonce,
4686 data_t *additional_data,
4687 data_t *input_data,
4688 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004689{
Ronald Cron5425a212020-08-04 14:58:35 +02004690 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004691 psa_key_type_t key_type = key_type_arg;
4692 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004693 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004694 unsigned char *output_data = NULL;
4695 size_t output_size = 0;
4696 size_t output_length = 0;
4697 unsigned char *output_data2 = NULL;
4698 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004699 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004700 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4706 psa_set_key_algorithm(&attributes, alg);
4707 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4710 &key));
4711 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4712 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4715 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004716 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4717 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4719 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4720 TEST_EQUAL(output_size,
4721 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4722 TEST_LE_U(output_size,
4723 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004724 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 status = psa_aead_encrypt(key, alg,
4728 nonce->x, nonce->len,
4729 additional_data->x,
4730 additional_data->len,
4731 input_data->x, input_data->len,
4732 output_data, output_size,
4733 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004734
4735 /* If the operation is not supported, just skip and not fail in case the
4736 * encryption involves a common limitation of cryptography hardwares and
4737 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if (status == PSA_ERROR_NOT_SUPPORTED) {
4739 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4740 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004741 }
4742
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 if (PSA_SUCCESS == expected_result) {
4746 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004747
Gilles Peskine003a4a92019-05-14 16:09:40 +02004748 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4749 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 TEST_EQUAL(input_data->len,
4751 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 TEST_LE_U(input_data->len,
4754 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 TEST_EQUAL(psa_aead_decrypt(key, alg,
4757 nonce->x, nonce->len,
4758 additional_data->x,
4759 additional_data->len,
4760 output_data, output_length,
4761 output_data2, output_length,
4762 &output_length2),
4763 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 ASSERT_COMPARE(input_data->x, input_data->len,
4766 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004767 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004768
Gilles Peskinea1cac842018-06-11 19:33:02 +02004769exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 psa_destroy_key(key);
4771 mbedtls_free(output_data);
4772 mbedtls_free(output_data2);
4773 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004774}
4775/* END_CASE */
4776
4777/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004778void aead_encrypt(int key_type_arg, data_t *key_data,
4779 int alg_arg,
4780 data_t *nonce,
4781 data_t *additional_data,
4782 data_t *input_data,
4783 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004784{
Ronald Cron5425a212020-08-04 14:58:35 +02004785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004786 psa_key_type_t key_type = key_type_arg;
4787 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004788 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004789 unsigned char *output_data = NULL;
4790 size_t output_size = 0;
4791 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004792 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004793 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4798 psa_set_key_algorithm(&attributes, alg);
4799 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4802 &key));
4803 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4804 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4807 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004808 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4809 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 TEST_EQUAL(output_size,
4811 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4812 TEST_LE_U(output_size,
4813 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4814 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 status = psa_aead_encrypt(key, alg,
4817 nonce->x, nonce->len,
4818 additional_data->x, additional_data->len,
4819 input_data->x, input_data->len,
4820 output_data, output_size,
4821 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004822
Ronald Cron28a45ed2021-02-09 20:35:42 +01004823 /* If the operation is not supported, just skip and not fail in case the
4824 * encryption involves a common limitation of cryptography hardwares and
4825 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 if (status == PSA_ERROR_NOT_SUPPORTED) {
4827 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4828 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004829 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 PSA_ASSERT(status);
4832 ASSERT_COMPARE(expected_result->x, expected_result->len,
4833 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004834
Gilles Peskinea1cac842018-06-11 19:33:02 +02004835exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 psa_destroy_key(key);
4837 mbedtls_free(output_data);
4838 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004839}
4840/* END_CASE */
4841
4842/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004843void aead_decrypt(int key_type_arg, data_t *key_data,
4844 int alg_arg,
4845 data_t *nonce,
4846 data_t *additional_data,
4847 data_t *input_data,
4848 data_t *expected_data,
4849 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004850{
Ronald Cron5425a212020-08-04 14:58:35 +02004851 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004852 psa_key_type_t key_type = key_type_arg;
4853 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004854 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004855 unsigned char *output_data = NULL;
4856 size_t output_size = 0;
4857 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004859 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004860 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4865 psa_set_key_algorithm(&attributes, alg);
4866 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4869 &key));
4870 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4871 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4874 alg);
4875 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4876 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004877 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4878 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 TEST_EQUAL(output_size,
4880 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4881 TEST_LE_U(output_size,
4882 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004883 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 status = psa_aead_decrypt(key, alg,
4887 nonce->x, nonce->len,
4888 additional_data->x,
4889 additional_data->len,
4890 input_data->x, input_data->len,
4891 output_data, output_size,
4892 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004893
Ronald Cron28a45ed2021-02-09 20:35:42 +01004894 /* If the operation is not supported, just skip and not fail in case the
4895 * decryption involves a common limitation of cryptography hardwares and
4896 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 if (status == PSA_ERROR_NOT_SUPPORTED) {
4898 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4899 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004900 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 if (expected_result == PSA_SUCCESS) {
4905 ASSERT_COMPARE(expected_data->x, expected_data->len,
4906 output_data, output_length);
4907 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004908
Gilles Peskinea1cac842018-06-11 19:33:02 +02004909exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 psa_destroy_key(key);
4911 mbedtls_free(output_data);
4912 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004913}
4914/* END_CASE */
4915
4916/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004917void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4918 int alg_arg,
4919 data_t *nonce,
4920 data_t *additional_data,
4921 data_t *input_data,
4922 int do_set_lengths,
4923 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004924{
Paul Elliottd3f82412021-06-16 16:52:21 +01004925 size_t ad_part_len = 0;
4926 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004927 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4930 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004931
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 if (do_set_lengths) {
4933 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004934 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004936 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004938 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004939
4940 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 if (!aead_multipart_internal_func(key_type_arg, key_data,
4942 alg_arg, nonce,
4943 additional_data,
4944 ad_part_len,
4945 input_data, -1,
4946 set_lengths_method,
4947 expected_output,
4948 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004949 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004950 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004951
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 /* length(0) part, length(ad_part_len) part, length(0) part... */
4953 mbedtls_test_set_step(1000 + ad_part_len);
4954
4955 if (!aead_multipart_internal_func(key_type_arg, key_data,
4956 alg_arg, nonce,
4957 additional_data,
4958 ad_part_len,
4959 input_data, -1,
4960 set_lengths_method,
4961 expected_output,
4962 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004963 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 }
4965 }
4966
4967 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4968 /* Split data into length(data_part_len) parts. */
4969 mbedtls_test_set_step(2000 + data_part_len);
4970
4971 if (do_set_lengths) {
4972 if (data_part_len & 0x01) {
4973 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4974 } else {
4975 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4976 }
4977 }
4978
4979 if (!aead_multipart_internal_func(key_type_arg, key_data,
4980 alg_arg, nonce,
4981 additional_data, -1,
4982 input_data, data_part_len,
4983 set_lengths_method,
4984 expected_output,
4985 1, 0)) {
4986 break;
4987 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004988
4989 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004991
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 if (!aead_multipart_internal_func(key_type_arg, key_data,
4993 alg_arg, nonce,
4994 additional_data, -1,
4995 input_data, data_part_len,
4996 set_lengths_method,
4997 expected_output,
4998 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004999 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005001 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005002
Paul Elliott8fc45162021-06-23 16:06:01 +01005003 /* Goto is required to silence warnings about unused labels, as we
5004 * don't actually do any test assertions in this function. */
5005 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005006}
5007/* END_CASE */
5008
5009/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005010void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5011 int alg_arg,
5012 data_t *nonce,
5013 data_t *additional_data,
5014 data_t *input_data,
5015 int do_set_lengths,
5016 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005017{
Paul Elliottd3f82412021-06-16 16:52:21 +01005018 size_t ad_part_len = 0;
5019 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005020 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005021
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005023 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005025
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 if (do_set_lengths) {
5027 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005028 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005030 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005032 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005033
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 if (!aead_multipart_internal_func(key_type_arg, key_data,
5035 alg_arg, nonce,
5036 additional_data,
5037 ad_part_len,
5038 input_data, -1,
5039 set_lengths_method,
5040 expected_output,
5041 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005042 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005044
5045 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005047
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 if (!aead_multipart_internal_func(key_type_arg, key_data,
5049 alg_arg, nonce,
5050 additional_data,
5051 ad_part_len,
5052 input_data, -1,
5053 set_lengths_method,
5054 expected_output,
5055 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005056 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005058 }
5059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005061 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 if (do_set_lengths) {
5065 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005066 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005068 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005070 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 if (!aead_multipart_internal_func(key_type_arg, key_data,
5073 alg_arg, nonce,
5074 additional_data, -1,
5075 input_data, data_part_len,
5076 set_lengths_method,
5077 expected_output,
5078 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005079 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005081
5082 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 if (!aead_multipart_internal_func(key_type_arg, key_data,
5086 alg_arg, nonce,
5087 additional_data, -1,
5088 input_data, data_part_len,
5089 set_lengths_method,
5090 expected_output,
5091 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005092 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005094 }
5095
Paul Elliott8fc45162021-06-23 16:06:01 +01005096 /* Goto is required to silence warnings about unused labels, as we
5097 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005098 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005099}
5100/* END_CASE */
5101
5102/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005103void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5104 int alg_arg,
5105 int nonce_length,
5106 int expected_nonce_length_arg,
5107 data_t *additional_data,
5108 data_t *input_data,
5109 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005110{
5111
5112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5113 psa_key_type_t key_type = key_type_arg;
5114 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005115 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005116 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5118 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005119 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005120 size_t actual_nonce_length = 0;
5121 size_t expected_nonce_length = expected_nonce_length_arg;
5122 unsigned char *output = NULL;
5123 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005124 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005125 size_t ciphertext_size = 0;
5126 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005127 size_t tag_length = 0;
5128 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005131
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5133 psa_set_key_algorithm(&attributes, alg);
5134 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005135
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5137 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005152
5153 /* If the operation is not supported, just skip and not fail in case the
5154 * encryption involves a common limitation of cryptography hardwares and
5155 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 if (status == PSA_ERROR_NOT_SUPPORTED) {
5157 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5158 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005159 }
5160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5164 nonce_length,
5165 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 if (expected_status == PSA_SUCCESS) {
5172 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5173 alg));
5174 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005175
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005179 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5181 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5184 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005185
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5187 output, output_size,
5188 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005189
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5191 &ciphertext_length, tag_buffer,
5192 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005193 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005194
5195exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 psa_destroy_key(key);
5197 mbedtls_free(output);
5198 mbedtls_free(ciphertext);
5199 psa_aead_abort(&operation);
5200 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005201}
5202/* END_CASE */
5203
5204/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005205void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5206 int alg_arg,
5207 int nonce_length_arg,
5208 int set_lengths_method_arg,
5209 data_t *additional_data,
5210 data_t *input_data,
5211 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005212{
5213
5214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5215 psa_key_type_t key_type = key_type_arg;
5216 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005217 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005218 uint8_t *nonce_buffer = NULL;
5219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5220 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5221 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005222 unsigned char *output = NULL;
5223 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005224 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005225 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005226 size_t ciphertext_size = 0;
5227 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005228 size_t tag_length = 0;
5229 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005230 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005231 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5236 psa_set_key_algorithm(&attributes, alg);
5237 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005238
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5240 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005241
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005243
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005245
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005247
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005255
5256 /* If the operation is not supported, just skip and not fail in case the
5257 * encryption involves a common limitation of cryptography hardwares and
5258 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 if (status == PSA_ERROR_NOT_SUPPORTED) {
5260 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5261 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005262 }
5263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005265
Paul Elliott4023ffd2021-09-10 16:21:22 +01005266 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (nonce_length_arg == -1) {
5268 /* Arbitrary size buffer, to test zero length valid buffer. */
5269 ASSERT_ALLOC(nonce_buffer, 4);
5270 nonce_length = 0;
5271 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005272 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 nonce_length = (size_t) nonce_length_arg;
5274 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005275
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 if (nonce_buffer) {
5277 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005278 nonce_buffer[index] = 'a' + index;
5279 }
Paul Elliott66696b52021-08-16 18:42:41 +01005280 }
Paul Elliott863864a2021-07-23 17:28:31 +01005281 }
5282
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5284 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5285 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005286 }
5287
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005289
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005291
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (expected_status == PSA_SUCCESS) {
5293 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5294 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5295 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005296 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005298 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 }
Paul Elliott863864a2021-07-23 17:28:31 +01005300
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005301 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5303 additional_data->len),
5304 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005305
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5307 output, output_size,
5308 &ciphertext_length),
5309 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005310
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5312 &ciphertext_length, tag_buffer,
5313 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5314 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005315 }
5316
5317exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 psa_destroy_key(key);
5319 mbedtls_free(output);
5320 mbedtls_free(ciphertext);
5321 mbedtls_free(nonce_buffer);
5322 psa_aead_abort(&operation);
5323 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005324}
5325/* END_CASE */
5326
5327/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005328void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005329 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005330 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005331 data_t *nonce,
5332 data_t *additional_data,
5333 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005335{
5336
5337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5338 psa_key_type_t key_type = key_type_arg;
5339 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005340 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5342 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5343 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005344 unsigned char *output = NULL;
5345 unsigned char *ciphertext = NULL;
5346 size_t output_size = output_size_arg;
5347 size_t ciphertext_size = 0;
5348 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005349 size_t tag_length = 0;
5350 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5351
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005353
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5355 psa_set_key_algorithm(&attributes, alg);
5356 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5359 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005360
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005370
5371 /* If the operation is not supported, just skip and not fail in case the
5372 * encryption involves a common limitation of cryptography hardwares and
5373 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 if (status == PSA_ERROR_NOT_SUPPORTED) {
5375 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5376 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005377 }
5378
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5382 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005383
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5387 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 status = psa_aead_update(&operation, input_data->x, input_data->len,
5390 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005395 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5397 &ciphertext_length, tag_buffer,
5398 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005399 }
5400
5401exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 psa_destroy_key(key);
5403 mbedtls_free(output);
5404 mbedtls_free(ciphertext);
5405 psa_aead_abort(&operation);
5406 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005407}
5408/* END_CASE */
5409
Paul Elliott91b021e2021-07-23 18:52:31 +01005410/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005411void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5412 int alg_arg,
5413 int finish_ciphertext_size_arg,
5414 int tag_size_arg,
5415 data_t *nonce,
5416 data_t *additional_data,
5417 data_t *input_data,
5418 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005419{
5420
5421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5422 psa_key_type_t key_type = key_type_arg;
5423 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005424 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005425 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5426 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5427 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005428 unsigned char *ciphertext = NULL;
5429 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005430 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005431 size_t ciphertext_size = 0;
5432 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5434 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005435 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005438
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5440 psa_set_key_algorithm(&attributes, alg);
5441 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005442
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5444 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005445
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005449
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005451
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005457
5458 /* If the operation is not supported, just skip and not fail in case the
5459 * encryption involves a common limitation of cryptography hardwares and
5460 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 if (status == PSA_ERROR_NOT_SUPPORTED) {
5462 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5463 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005464 }
5465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005467
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5471 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5474 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5477 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005478
5479 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 status = psa_aead_finish(&operation, finish_ciphertext,
5481 finish_ciphertext_size,
5482 &ciphertext_length, tag_buffer,
5483 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005484
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005486
5487exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 psa_destroy_key(key);
5489 mbedtls_free(ciphertext);
5490 mbedtls_free(finish_ciphertext);
5491 mbedtls_free(tag_buffer);
5492 psa_aead_abort(&operation);
5493 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005494}
5495/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005496
5497/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005498void aead_multipart_verify(int key_type_arg, data_t *key_data,
5499 int alg_arg,
5500 data_t *nonce,
5501 data_t *additional_data,
5502 data_t *input_data,
5503 data_t *tag,
5504 int tag_usage_arg,
5505 int expected_setup_status_arg,
5506 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005507{
5508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5509 psa_key_type_t key_type = key_type_arg;
5510 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005511 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5513 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5514 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005515 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005516 unsigned char *plaintext = NULL;
5517 unsigned char *finish_plaintext = NULL;
5518 size_t plaintext_size = 0;
5519 size_t plaintext_length = 0;
5520 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005521 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005522 unsigned char *tag_buffer = NULL;
5523 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5528 psa_set_key_algorithm(&attributes, alg);
5529 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5532 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005533
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5537 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005546
5547 /* If the operation is not supported, just skip and not fail in case the
5548 * encryption involves a common limitation of cryptography hardwares and
5549 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 if (status == PSA_ERROR_NOT_SUPPORTED) {
5551 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5552 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005553 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005555
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005557 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 }
Paul Elliott9961a662021-09-17 19:19:02 +01005559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 status = psa_aead_set_lengths(&operation, additional_data->len,
5565 input_data->len);
5566 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5569 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5572 input_data->len,
5573 plaintext, plaintext_size,
5574 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005577 tag_buffer = tag->x;
5578 tag_size = tag->len;
5579 }
5580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 status = psa_aead_verify(&operation, finish_plaintext,
5582 verify_plaintext_size,
5583 &plaintext_length,
5584 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005587
5588exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 psa_destroy_key(key);
5590 mbedtls_free(plaintext);
5591 mbedtls_free(finish_plaintext);
5592 psa_aead_abort(&operation);
5593 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005594}
5595/* END_CASE */
5596
Paul Elliott9961a662021-09-17 19:19:02 +01005597/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005598void aead_multipart_setup(int key_type_arg, data_t *key_data,
5599 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005600{
5601 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5602 psa_key_type_t key_type = key_type_arg;
5603 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005604 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5606 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5607 psa_status_t expected_status = expected_status_arg;
5608
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 psa_set_key_usage_flags(&attributes,
5612 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5613 psa_set_key_algorithm(&attributes, alg);
5614 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005615
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5617 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005618
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005622
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005628
5629exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 psa_destroy_key(key);
5631 psa_aead_abort(&operation);
5632 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005633}
5634/* END_CASE */
5635
5636/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005637void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5638 int alg_arg,
5639 data_t *nonce,
5640 data_t *additional_data,
5641 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005642{
5643 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5644 psa_key_type_t key_type = key_type_arg;
5645 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005646 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005647 unsigned char *output_data = NULL;
5648 unsigned char *final_data = NULL;
5649 size_t output_size = 0;
5650 size_t finish_output_size = 0;
5651 size_t output_length = 0;
5652 size_t key_bits = 0;
5653 size_t tag_length = 0;
5654 size_t tag_size = 0;
5655 size_t nonce_length = 0;
5656 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5657 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5658 size_t output_part_length = 0;
5659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5660
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005662
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 psa_set_key_usage_flags(&attributes,
5664 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5665 psa_set_key_algorithm(&attributes, alg);
5666 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005667
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5669 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5672 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
5688 /* Test all operations error without calling setup first. */
5689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5691 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5696 PSA_AEAD_NONCE_MAX_SIZE,
5697 &nonce_length),
5698 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005701
Paul Elliott481be342021-07-16 17:38:47 +01005702 /* ------------------------------------------------------- */
5703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5705 input_data->len),
5706 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005709
Paul Elliott481be342021-07-16 17:38:47 +01005710 /* ------------------------------------------------------- */
5711
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5713 additional_data->len),
5714 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005715
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717
Paul Elliott481be342021-07-16 17:38:47 +01005718 /* ------------------------------------------------------- */
5719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5721 input_data->len, output_data,
5722 output_size, &output_length),
5723 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005726
Paul Elliott481be342021-07-16 17:38:47 +01005727 /* ------------------------------------------------------- */
5728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5730 finish_output_size,
5731 &output_part_length,
5732 tag_buffer, tag_length,
5733 &tag_size),
5734 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005737
Paul Elliott481be342021-07-16 17:38:47 +01005738 /* ------------------------------------------------------- */
5739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5741 finish_output_size,
5742 &output_part_length,
5743 tag_buffer,
5744 tag_length),
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
5749 /* Test for double setups. */
5750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5754 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005757
Paul Elliott481be342021-07-16 17:38:47 +01005758 /* ------------------------------------------------------- */
5759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5763 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005764
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005766
Paul Elliott374a2be2021-07-16 17:53:40 +01005767 /* ------------------------------------------------------- */
5768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5772 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005775
5776 /* ------------------------------------------------------- */
5777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5781 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005782
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005784
Paul Elliottc23a9a02021-06-21 18:32:46 +01005785 /* Test for not setting a nonce. */
5786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5790 additional_data->len),
5791 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005792
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005794
Paul Elliott7f628422021-09-01 12:08:29 +01005795 /* ------------------------------------------------------- */
5796
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5800 input_data->len, output_data,
5801 output_size, &output_length),
5802 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005805
Paul Elliottbdc2c682021-09-21 18:37:10 +01005806 /* ------------------------------------------------------- */
5807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5811 finish_output_size,
5812 &output_part_length,
5813 tag_buffer, tag_length,
5814 &tag_size),
5815 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005818
5819 /* ------------------------------------------------------- */
5820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005822
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5824 finish_output_size,
5825 &output_part_length,
5826 tag_buffer,
5827 tag_length),
5828 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005829
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005831
Paul Elliottc23a9a02021-06-21 18:32:46 +01005832 /* Test for double setting nonce. */
5833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005835
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5839 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005840
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005842
Paul Elliott374a2be2021-07-16 17:53:40 +01005843 /* Test for double generating nonce. */
5844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5848 PSA_AEAD_NONCE_MAX_SIZE,
5849 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5852 PSA_AEAD_NONCE_MAX_SIZE,
5853 &nonce_length),
5854 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005855
5856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005858
5859 /* Test for generate nonce then set and vice versa */
5860
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5864 PSA_AEAD_NONCE_MAX_SIZE,
5865 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5868 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005869
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005871
Andrzej Kurekad837522021-12-15 15:28:49 +01005872 /* Test for generating nonce after calling set lengths */
5873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5877 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5880 PSA_AEAD_NONCE_MAX_SIZE,
5881 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005884
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005885 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005888
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 if (operation.alg == PSA_ALG_CCM) {
5890 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5891 input_data->len),
5892 PSA_ERROR_INVALID_ARGUMENT);
5893 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5894 PSA_AEAD_NONCE_MAX_SIZE,
5895 &nonce_length),
5896 PSA_ERROR_BAD_STATE);
5897 } else {
5898 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5899 input_data->len));
5900 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5901 PSA_AEAD_NONCE_MAX_SIZE,
5902 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005903 }
5904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005906
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005907 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005908#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5912 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5913 input_data->len),
5914 PSA_ERROR_INVALID_ARGUMENT);
5915 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5916 PSA_AEAD_NONCE_MAX_SIZE,
5917 &nonce_length),
5918 PSA_ERROR_BAD_STATE);
5919 } else {
5920 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5921 input_data->len));
5922 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5923 PSA_AEAD_NONCE_MAX_SIZE,
5924 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005925 }
5926
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005928#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005929
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005930 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5935 PSA_AEAD_NONCE_MAX_SIZE,
5936 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005937
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 if (operation.alg == PSA_ALG_CCM) {
5939 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5940 input_data->len),
5941 PSA_ERROR_INVALID_ARGUMENT);
5942 } else {
5943 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5944 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005945 }
5946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005948
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005949 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005950 /* Test for setting nonce after calling set lengths */
5951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5955 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005958
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 UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005962
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) {
5966 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_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, UINT32_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);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005978
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005979 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005980#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5984 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5985 input_data->len),
5986 PSA_ERROR_INVALID_ARGUMENT);
5987 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5988 PSA_ERROR_BAD_STATE);
5989 } else {
5990 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5991 input_data->len));
5992 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005993 }
5994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005996#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005997
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005998 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 if (operation.alg == PSA_ALG_CCM) {
6005 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6006 input_data->len),
6007 PSA_ERROR_INVALID_ARGUMENT);
6008 } else {
6009 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6010 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006011 }
6012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006014
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006015 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006016#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 if (operation.alg == PSA_ALG_GCM) {
6020 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6021 SIZE_MAX),
6022 PSA_ERROR_INVALID_ARGUMENT);
6023 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6024 PSA_ERROR_BAD_STATE);
6025 } else if (operation.alg != PSA_ALG_CCM) {
6026 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6027 SIZE_MAX));
6028 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006029 }
6030
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006032#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006033
Tom Cosgrove1797b052022-12-04 17:19:59 +00006034 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006035#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 if (operation.alg == PSA_ALG_GCM) {
6041 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6042 SIZE_MAX),
6043 PSA_ERROR_INVALID_ARGUMENT);
6044 } else if (operation.alg != PSA_ALG_CCM) {
6045 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6046 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006047 }
6048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006050#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006051
6052 /* ------------------------------------------------------- */
6053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6059 PSA_AEAD_NONCE_MAX_SIZE,
6060 &nonce_length),
6061 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006064
Paul Elliott7220cae2021-06-22 17:25:57 +01006065 /* Test for generating nonce in decrypt setup. */
6066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6070 PSA_AEAD_NONCE_MAX_SIZE,
6071 &nonce_length),
6072 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006075
Paul Elliottc23a9a02021-06-21 18:32:46 +01006076 /* Test for setting lengths twice. */
6077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6083 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6086 input_data->len),
6087 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006090
Andrzej Kurekad837522021-12-15 15:28:49 +01006091 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6100 additional_data->len),
6101 PSA_ERROR_BAD_STATE);
6102 } else {
6103 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6104 additional_data->len));
6105
6106 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6107 input_data->len),
6108 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006109 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006111
6112 /* ------------------------------------------------------- */
6113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006115
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (operation.alg == PSA_ALG_CCM) {
6119 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6120 input_data->len, output_data,
6121 output_size, &output_length),
6122 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006123
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 } else {
6125 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6126 input_data->len, output_data,
6127 output_size, &output_length));
6128
6129 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6130 input_data->len),
6131 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006132 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006134
6135 /* ------------------------------------------------------- */
6136
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 if (operation.alg == PSA_ALG_CCM) {
6142 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6143 finish_output_size,
6144 &output_part_length,
6145 tag_buffer, tag_length,
6146 &tag_size));
6147 } else {
6148 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6149 finish_output_size,
6150 &output_part_length,
6151 tag_buffer, tag_length,
6152 &tag_size));
6153
6154 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6155 input_data->len),
6156 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006157 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006159
6160 /* Test for setting lengths after generating nonce + already starting data. */
6161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6165 PSA_AEAD_NONCE_MAX_SIZE,
6166 &nonce_length));
6167 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006168
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6170 additional_data->len),
6171 PSA_ERROR_BAD_STATE);
6172 } else {
6173 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6174 additional_data->len));
6175
6176 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6177 input_data->len),
6178 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006179 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006181
6182 /* ------------------------------------------------------- */
6183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006185
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6187 PSA_AEAD_NONCE_MAX_SIZE,
6188 &nonce_length));
6189 if (operation.alg == PSA_ALG_CCM) {
6190 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6191 input_data->len, output_data,
6192 output_size, &output_length),
6193 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 } else {
6196 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6197 input_data->len, output_data,
6198 output_size, &output_length));
6199
6200 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6201 input_data->len),
6202 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006203 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006205
6206 /* ------------------------------------------------------- */
6207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6211 PSA_AEAD_NONCE_MAX_SIZE,
6212 &nonce_length));
6213 if (operation.alg == PSA_ALG_CCM) {
6214 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6215 finish_output_size,
6216 &output_part_length,
6217 tag_buffer, tag_length,
6218 &tag_size));
6219 } else {
6220 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6221 finish_output_size,
6222 &output_part_length,
6223 tag_buffer, tag_length,
6224 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6227 input_data->len),
6228 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006229 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006231
Paul Elliott243080c2021-07-21 19:01:17 +01006232 /* Test for not sending any additional data or data after setting non zero
6233 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6240 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6243 finish_output_size,
6244 &output_part_length,
6245 tag_buffer, tag_length,
6246 &tag_size),
6247 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006250
Paul Elliott243080c2021-07-21 19:01:17 +01006251 /* Test for not sending any additional data or data after setting non-zero
6252 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006253
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6259 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006260
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6262 finish_output_size,
6263 &output_part_length,
6264 tag_buffer,
6265 tag_length),
6266 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006267
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006269
Paul Elliott243080c2021-07-21 19:01:17 +01006270 /* Test for not sending any additional data after setting a non-zero length
6271 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6278 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006279
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6281 input_data->len, output_data,
6282 output_size, &output_length),
6283 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006286
Paul Elliottf94bd992021-09-19 18:15:59 +01006287 /* Test for not sending any data after setting a non-zero length for it.*/
6288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6294 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6297 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6300 finish_output_size,
6301 &output_part_length,
6302 tag_buffer, tag_length,
6303 &tag_size),
6304 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006307
Paul Elliottb0450fe2021-09-01 15:06:26 +01006308 /* Test for sending too much additional data after setting lengths. */
6309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006315
6316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6318 additional_data->len),
6319 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006322
Paul Elliotta2a09b02021-09-22 14:56:40 +01006323 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6330 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6333 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6336 1),
6337 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006340
Paul Elliottb0450fe2021-09-01 15:06:26 +01006341 /* Test for sending too much data after setting lengths. */
6342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6350 input_data->len, output_data,
6351 output_size, &output_length),
6352 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006355
Paul Elliotta2a09b02021-09-22 14:56:40 +01006356 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6363 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6366 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6369 input_data->len, output_data,
6370 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006371
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6373 1, output_data,
6374 output_size, &output_length),
6375 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006378
Paul Elliottc23a9a02021-06-21 18:32:46 +01006379 /* Test sending additional data after data. */
6380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 if (operation.alg != PSA_ALG_CCM) {
6386 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6387 input_data->len, output_data,
6388 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6391 additional_data->len),
6392 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006393 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006395
Paul Elliott534d0b42021-06-22 19:15:20 +01006396 /* Test calling finish on decryption. */
6397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6403 finish_output_size,
6404 &output_part_length,
6405 tag_buffer, tag_length,
6406 &tag_size),
6407 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006410
6411 /* Test calling verify on encryption. */
6412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6418 finish_output_size,
6419 &output_part_length,
6420 tag_buffer,
6421 tag_length),
6422 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006425
6426
Paul Elliottc23a9a02021-06-21 18:32:46 +01006427exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 psa_destroy_key(key);
6429 psa_aead_abort(&operation);
6430 mbedtls_free(output_data);
6431 mbedtls_free(final_data);
6432 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006433}
6434/* END_CASE */
6435
6436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006437void signature_size(int type_arg,
6438 int bits,
6439 int alg_arg,
6440 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006441{
6442 psa_key_type_t type = type_arg;
6443 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006445
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006447
Gilles Peskinee59236f2018-01-27 23:32:46 +01006448exit:
6449 ;
6450}
6451/* END_CASE */
6452
6453/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006454void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6455 int alg_arg, data_t *input_data,
6456 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006457{
Ronald Cron5425a212020-08-04 14:58:35 +02006458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006459 psa_key_type_t key_type = key_type_arg;
6460 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006461 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006462 unsigned char *signature = NULL;
6463 size_t signature_size;
6464 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006466
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6470 psa_set_key_algorithm(&attributes, alg);
6471 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006472
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6474 &key));
6475 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6476 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006477
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006478 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006479 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6481 key_bits, alg);
6482 TEST_ASSERT(signature_size != 0);
6483 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6484 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006485
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006486 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 PSA_ASSERT(psa_sign_hash(key, alg,
6488 input_data->x, input_data->len,
6489 signature, signature_size,
6490 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006491 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 ASSERT_COMPARE(output_data->x, output_data->len,
6493 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006494
6495exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006496 /*
6497 * Key attributes may have been returned by psa_get_key_attributes()
6498 * thus reset them as required.
6499 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006501
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 psa_destroy_key(key);
6503 mbedtls_free(signature);
6504 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006505}
6506/* END_CASE */
6507
Paul Elliott712d5122022-12-07 14:03:10 +00006508/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006509/**
6510 * sign_hash_interruptible() test intentions:
6511 *
6512 * Note: This test can currently only handle ECDSA.
6513 *
6514 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006515 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006516 *
6517 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6518 * expected for different max_ops values.
6519 *
6520 * 3. Test that the number of ops done prior to start and after abort is zero
6521 * and that each successful stage completes some ops (this is not mandated by
6522 * the PSA specification, but is currently the case).
6523 *
6524 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6525 * complete() calls does not alter the number of ops returned.
6526 */
Paul Elliott712d5122022-12-07 14:03:10 +00006527void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6528 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006529 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006530{
6531 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6532 psa_key_type_t key_type = key_type_arg;
6533 psa_algorithm_t alg = alg_arg;
6534 size_t key_bits;
6535 unsigned char *signature = NULL;
6536 size_t signature_size;
6537 size_t signature_length = 0xdeadbeef;
6538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6539 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006540 uint32_t num_ops = 0;
6541 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006542 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006543 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006544 size_t min_completes = 0;
6545 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006546
Paul Elliott712d5122022-12-07 14:03:10 +00006547 psa_sign_hash_interruptible_operation_t operation =
6548 psa_sign_hash_interruptible_operation_init();
6549
6550 PSA_ASSERT(psa_crypto_init());
6551
6552 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6553 psa_set_key_algorithm(&attributes, alg);
6554 psa_set_key_type(&attributes, key_type);
6555
6556 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6557 &key));
6558 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6559 key_bits = psa_get_key_bits(&attributes);
6560
6561 /* Allocate a buffer which has the size advertised by the
6562 * library. */
6563 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6564 key_bits, alg);
6565 TEST_ASSERT(signature_size != 0);
6566 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6567 ASSERT_ALLOC(signature, signature_size);
6568
Paul Elliott0c683352022-12-16 19:16:56 +00006569 psa_interruptible_set_max_ops(max_ops);
6570
Paul Elliott6f600372023-02-06 18:41:05 +00006571 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6572 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006573
Paul Elliott712d5122022-12-07 14:03:10 +00006574 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6575 TEST_ASSERT(num_ops_prior == 0);
6576
6577 /* Start performing the signature. */
6578 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6579 input_data->x, input_data->len));
6580
6581 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6582 TEST_ASSERT(num_ops_prior == 0);
6583
6584 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006585 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006586 status = psa_sign_hash_complete(&operation, signature, signature_size,
6587 &signature_length);
6588
Paul Elliott0c683352022-12-16 19:16:56 +00006589 num_completes++;
6590
Paul Elliott712d5122022-12-07 14:03:10 +00006591 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6592 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006593 /* We are asserting here that every complete makes progress
6594 * (completes some ops), which is true of the internal
6595 * implementation and probably any implementation, however this is
6596 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006597 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006598
Paul Elliott712d5122022-12-07 14:03:10 +00006599 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006600
6601 /* Ensure calling get_num_ops() twice still returns the same
6602 * number of ops as previously reported. */
6603 num_ops = psa_sign_hash_get_num_ops(&operation);
6604
6605 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006606 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006607 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006608
6609 TEST_ASSERT(status == PSA_SUCCESS);
6610
Paul Elliott0c683352022-12-16 19:16:56 +00006611 TEST_LE_U(min_completes, num_completes);
6612 TEST_LE_U(num_completes, max_completes);
6613
Paul Elliott712d5122022-12-07 14:03:10 +00006614 /* Verify that the signature is what is expected. */
6615 ASSERT_COMPARE(output_data->x, output_data->len,
6616 signature, signature_length);
6617
6618 PSA_ASSERT(psa_sign_hash_abort(&operation));
6619
Paul Elliott59ad9452022-12-18 15:09:02 +00006620 num_ops = psa_sign_hash_get_num_ops(&operation);
6621 TEST_ASSERT(num_ops == 0);
6622
Paul Elliott712d5122022-12-07 14:03:10 +00006623exit:
6624
6625 /*
6626 * Key attributes may have been returned by psa_get_key_attributes()
6627 * thus reset them as required.
6628 */
6629 psa_reset_key_attributes(&attributes);
6630
6631 psa_destroy_key(key);
6632 mbedtls_free(signature);
6633 PSA_DONE();
6634}
6635/* END_CASE */
6636
Gilles Peskine20035e32018-02-03 22:44:14 +01006637/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006638void sign_hash_fail(int key_type_arg, data_t *key_data,
6639 int alg_arg, data_t *input_data,
6640 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006641{
Ronald Cron5425a212020-08-04 14:58:35 +02006642 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006643 psa_key_type_t key_type = key_type_arg;
6644 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006645 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006646 psa_status_t actual_status;
6647 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006648 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006649 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006650 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006651
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006653
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006655
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6657 psa_set_key_algorithm(&attributes, alg);
6658 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006659
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6661 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006662
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 actual_status = psa_sign_hash(key, alg,
6664 input_data->x, input_data->len,
6665 signature, signature_size,
6666 &signature_length);
6667 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006668 /* The value of *signature_length is unspecified on error, but
6669 * whatever it is, it should be less than signature_size, so that
6670 * if the caller tries to read *signature_length bytes without
6671 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006673
6674exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 psa_reset_key_attributes(&attributes);
6676 psa_destroy_key(key);
6677 mbedtls_free(signature);
6678 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006679}
6680/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006681
Paul Elliott91007972022-12-16 12:21:24 +00006682/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006683/**
6684 * sign_hash_fail_interruptible() test intentions:
6685 *
6686 * Note: This test can currently only handle ECDSA.
6687 *
6688 * 1. Test that various failure cases for interruptible sign hash fail with the
6689 * correct error codes, and at the correct point (at start or during
6690 * complete).
6691 *
6692 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6693 * expected for different max_ops values.
6694 *
6695 * 3. Test that the number of ops done prior to start and after abort is zero
6696 * and that each successful stage completes some ops (this is not mandated by
6697 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006698 *
6699 * 4. Check that calling complete() when start() fails and complete()
6700 * after completion results in a BAD_STATE error.
6701 *
6702 * 5. Check that calling start() again after start fails results in a BAD_STATE
6703 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006704 */
Paul Elliott91007972022-12-16 12:21:24 +00006705void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6706 int alg_arg, data_t *input_data,
6707 int signature_size_arg,
6708 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006709 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006710 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006711{
6712 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6713 psa_key_type_t key_type = key_type_arg;
6714 psa_algorithm_t alg = alg_arg;
6715 size_t signature_size = signature_size_arg;
6716 psa_status_t actual_status;
6717 psa_status_t expected_start_status = expected_start_status_arg;
6718 psa_status_t expected_complete_status = expected_complete_status_arg;
6719 unsigned char *signature = NULL;
6720 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006721 uint32_t num_ops = 0;
6722 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006723 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006724 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006725 size_t min_completes = 0;
6726 size_t max_completes = 0;
6727
Paul Elliott91007972022-12-16 12:21:24 +00006728 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6729 psa_sign_hash_interruptible_operation_t operation =
6730 psa_sign_hash_interruptible_operation_init();
6731
6732 ASSERT_ALLOC(signature, signature_size);
6733
6734 PSA_ASSERT(psa_crypto_init());
6735
6736 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6737 psa_set_key_algorithm(&attributes, alg);
6738 psa_set_key_type(&attributes, key_type);
6739
6740 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6741 &key));
6742
Paul Elliott0c683352022-12-16 19:16:56 +00006743 psa_interruptible_set_max_ops(max_ops);
6744
Paul Elliott6f600372023-02-06 18:41:05 +00006745 interruptible_signverify_get_minmax_completes(max_ops,
6746 expected_complete_status,
6747 &min_completes,
6748 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006749
Paul Elliott91007972022-12-16 12:21:24 +00006750 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6751 TEST_ASSERT(num_ops_prior == 0);
6752
6753 /* Start performing the signature. */
6754 actual_status = psa_sign_hash_start(&operation, key, alg,
6755 input_data->x, input_data->len);
6756
6757 TEST_EQUAL(actual_status, expected_start_status);
6758
Paul Elliottc9774412023-02-06 15:14:07 +00006759 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006760 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006761 * start failed. */
6762 actual_status = psa_sign_hash_complete(&operation, signature,
6763 signature_size,
6764 &signature_length);
6765
6766 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6767
6768 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006769 actual_status = psa_sign_hash_start(&operation, key, alg,
6770 input_data->x, input_data->len);
6771
6772 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6773 }
6774
Paul Elliott91007972022-12-16 12:21:24 +00006775 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6776 TEST_ASSERT(num_ops_prior == 0);
6777
Paul Elliott91007972022-12-16 12:21:24 +00006778 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006779 do {
Paul Elliott91007972022-12-16 12:21:24 +00006780 actual_status = psa_sign_hash_complete(&operation, signature,
6781 signature_size,
6782 &signature_length);
6783
Paul Elliott0c683352022-12-16 19:16:56 +00006784 num_completes++;
6785
Paul Elliott334d7262023-01-20 17:29:41 +00006786 if (actual_status == PSA_SUCCESS ||
6787 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006788 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006789 /* We are asserting here that every complete makes progress
6790 * (completes some ops), which is true of the internal
6791 * implementation and probably any implementation, however this is
6792 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006793 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006794
Paul Elliott91007972022-12-16 12:21:24 +00006795 num_ops_prior = num_ops;
6796 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006797 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006798
Paul Elliottc9774412023-02-06 15:14:07 +00006799 TEST_EQUAL(actual_status, expected_complete_status);
6800
Paul Elliottefebad02023-02-15 16:56:45 +00006801 /* Check that another complete returns BAD_STATE. */
6802 actual_status = psa_sign_hash_complete(&operation, signature,
6803 signature_size,
6804 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006805
Paul Elliottefebad02023-02-15 16:56:45 +00006806 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006807
Paul Elliott91007972022-12-16 12:21:24 +00006808 PSA_ASSERT(psa_sign_hash_abort(&operation));
6809
Paul Elliott59ad9452022-12-18 15:09:02 +00006810 num_ops = psa_sign_hash_get_num_ops(&operation);
6811 TEST_ASSERT(num_ops == 0);
6812
Paul Elliott91007972022-12-16 12:21:24 +00006813 /* The value of *signature_length is unspecified on error, but
6814 * whatever it is, it should be less than signature_size, so that
6815 * if the caller tries to read *signature_length bytes without
6816 * checking the error code then they don't overflow a buffer. */
6817 TEST_LE_U(signature_length, signature_size);
6818
Paul Elliott0c683352022-12-16 19:16:56 +00006819 TEST_LE_U(min_completes, num_completes);
6820 TEST_LE_U(num_completes, max_completes);
6821
Paul Elliott91007972022-12-16 12:21:24 +00006822exit:
6823 psa_reset_key_attributes(&attributes);
6824 psa_destroy_key(key);
6825 mbedtls_free(signature);
6826 PSA_DONE();
6827}
6828/* END_CASE */
6829
mohammad16038cc1cee2018-03-28 01:21:33 +03006830/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006831void sign_verify_hash(int key_type_arg, data_t *key_data,
6832 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006833{
Ronald Cron5425a212020-08-04 14:58:35 +02006834 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006835 psa_key_type_t key_type = key_type_arg;
6836 psa_algorithm_t alg = alg_arg;
6837 size_t key_bits;
6838 unsigned char *signature = NULL;
6839 size_t signature_size;
6840 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006841 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006842
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006844
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6846 psa_set_key_algorithm(&attributes, alg);
6847 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006848
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6850 &key));
6851 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6852 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006853
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006854 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006855 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6857 key_bits, alg);
6858 TEST_ASSERT(signature_size != 0);
6859 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6860 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006861
6862 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 PSA_ASSERT(psa_sign_hash(key, alg,
6864 input_data->x, input_data->len,
6865 signature, signature_size,
6866 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006867 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 TEST_LE_U(signature_length, signature_size);
6869 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006870
6871 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 PSA_ASSERT(psa_verify_hash(key, alg,
6873 input_data->x, input_data->len,
6874 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006877 /* Flip a bit in the input and verify that the signature is now
6878 * detected as invalid. Flip a bit at the beginning, not at the end,
6879 * because ECDSA may ignore the last few bits of the input. */
6880 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 TEST_EQUAL(psa_verify_hash(key, alg,
6882 input_data->x, input_data->len,
6883 signature, signature_length),
6884 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006885 }
6886
6887exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006888 /*
6889 * Key attributes may have been returned by psa_get_key_attributes()
6890 * thus reset them as required.
6891 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006893
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 psa_destroy_key(key);
6895 mbedtls_free(signature);
6896 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006897}
6898/* END_CASE */
6899
Paul Elliott712d5122022-12-07 14:03:10 +00006900/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006901/**
6902 * sign_verify_hash_interruptible() test intentions:
6903 *
6904 * Note: This test can currently only handle ECDSA.
6905 *
Paul Elliott8c092052023-03-06 17:49:14 +00006906 * 1. Test that we can sign an input hash with the given keypair and then
6907 * afterwards verify that signature. This is currently the only way to test
6908 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006909 *
6910 * 2. Test that after corrupting the hash, the verification detects an invalid
6911 * signature.
6912 *
6913 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6914 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006915 *
6916 * 4. Test that the number of ops done prior to starting signing and after abort
6917 * is zero and that each successful signing stage completes some ops (this is
6918 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006919 */
Paul Elliott712d5122022-12-07 14:03:10 +00006920void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006921 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006922 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006923{
6924 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6925 psa_key_type_t key_type = key_type_arg;
6926 psa_algorithm_t alg = alg_arg;
6927 size_t key_bits;
6928 unsigned char *signature = NULL;
6929 size_t signature_size;
6930 size_t signature_length = 0xdeadbeef;
6931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6932 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006933 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006934 uint32_t num_ops = 0;
6935 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006936 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006937 size_t min_completes = 0;
6938 size_t max_completes = 0;
6939
Paul Elliott712d5122022-12-07 14:03:10 +00006940 psa_sign_hash_interruptible_operation_t sign_operation =
6941 psa_sign_hash_interruptible_operation_init();
6942 psa_verify_hash_interruptible_operation_t verify_operation =
6943 psa_verify_hash_interruptible_operation_init();
6944
6945 PSA_ASSERT(psa_crypto_init());
6946
Paul Elliott0c683352022-12-16 19:16:56 +00006947 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6948 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006949 psa_set_key_algorithm(&attributes, alg);
6950 psa_set_key_type(&attributes, key_type);
6951
6952 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6953 &key));
6954 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6955 key_bits = psa_get_key_bits(&attributes);
6956
6957 /* Allocate a buffer which has the size advertised by the
6958 * library. */
6959 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6960 key_bits, alg);
6961 TEST_ASSERT(signature_size != 0);
6962 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6963 ASSERT_ALLOC(signature, signature_size);
6964
Paul Elliott0c683352022-12-16 19:16:56 +00006965 psa_interruptible_set_max_ops(max_ops);
6966
Paul Elliott6f600372023-02-06 18:41:05 +00006967 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6968 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006969
Paul Elliott7c173082023-02-26 18:44:45 +00006970 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6971 TEST_ASSERT(num_ops_prior == 0);
6972
Paul Elliott712d5122022-12-07 14:03:10 +00006973 /* Start performing the signature. */
6974 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6975 input_data->x, input_data->len));
6976
Paul Elliott7c173082023-02-26 18:44:45 +00006977 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6978 TEST_ASSERT(num_ops_prior == 0);
6979
Paul Elliott712d5122022-12-07 14:03:10 +00006980 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006981 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006982
Paul Elliott0c683352022-12-16 19:16:56 +00006983 status = psa_sign_hash_complete(&sign_operation, signature,
6984 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006985 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006986
6987 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006988
6989 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6990 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6991 /* We are asserting here that every complete makes progress
6992 * (completes some ops), which is true of the internal
6993 * implementation and probably any implementation, however this is
6994 * not mandated by the PSA specification. */
6995 TEST_ASSERT(num_ops > num_ops_prior);
6996
6997 num_ops_prior = num_ops;
6998 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006999 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007000
7001 TEST_ASSERT(status == PSA_SUCCESS);
7002
Paul Elliott0c683352022-12-16 19:16:56 +00007003 TEST_LE_U(min_completes, num_completes);
7004 TEST_LE_U(num_completes, max_completes);
7005
Paul Elliott712d5122022-12-07 14:03:10 +00007006 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7007
Paul Elliott7c173082023-02-26 18:44:45 +00007008 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7009 TEST_ASSERT(num_ops == 0);
7010
Paul Elliott712d5122022-12-07 14:03:10 +00007011 /* Check that the signature length looks sensible. */
7012 TEST_LE_U(signature_length, signature_size);
7013 TEST_ASSERT(signature_length > 0);
7014
Paul Elliott0c683352022-12-16 19:16:56 +00007015 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007016
7017 /* Start verification. */
7018 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7019 input_data->x, input_data->len,
7020 signature, signature_length));
7021
7022 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007023 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007024 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007025
7026 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007027 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007028
7029 TEST_ASSERT(status == PSA_SUCCESS);
7030
Paul Elliott0c683352022-12-16 19:16:56 +00007031 TEST_LE_U(min_completes, num_completes);
7032 TEST_LE_U(num_completes, max_completes);
7033
Paul Elliott712d5122022-12-07 14:03:10 +00007034 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7035
7036 verify_operation = psa_verify_hash_interruptible_operation_init();
7037
7038 if (input_data->len != 0) {
7039 /* Flip a bit in the input and verify that the signature is now
7040 * detected as invalid. Flip a bit at the beginning, not at the end,
7041 * because ECDSA may ignore the last few bits of the input. */
7042 input_data->x[0] ^= 1;
7043
Paul Elliott712d5122022-12-07 14:03:10 +00007044 /* Start verification. */
7045 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7046 input_data->x, input_data->len,
7047 signature, signature_length));
7048
7049 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007050 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007051 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007052 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007053
7054 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7055 }
7056
7057 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7058
7059exit:
7060 /*
7061 * Key attributes may have been returned by psa_get_key_attributes()
7062 * thus reset them as required.
7063 */
7064 psa_reset_key_attributes(&attributes);
7065
7066 psa_destroy_key(key);
7067 mbedtls_free(signature);
7068 PSA_DONE();
7069}
7070/* END_CASE */
7071
Gilles Peskine9911b022018-06-29 17:30:48 +02007072/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007073void verify_hash(int key_type_arg, data_t *key_data,
7074 int alg_arg, data_t *hash_data,
7075 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007076{
Ronald Cron5425a212020-08-04 14:58:35 +02007077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007078 psa_key_type_t key_type = key_type_arg;
7079 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007080 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007081
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007083
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007085
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7087 psa_set_key_algorithm(&attributes, alg);
7088 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7091 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007092
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 PSA_ASSERT(psa_verify_hash(key, alg,
7094 hash_data->x, hash_data->len,
7095 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007096
itayzafrir5c753392018-05-08 11:18:38 +03007097exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 psa_reset_key_attributes(&attributes);
7099 psa_destroy_key(key);
7100 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007101}
7102/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007103
Paul Elliott712d5122022-12-07 14:03:10 +00007104/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007105/**
7106 * verify_hash_interruptible() test intentions:
7107 *
7108 * Note: This test can currently only handle ECDSA.
7109 *
7110 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007111 * only). Given this test only does verification it can accept public keys as
7112 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007113 *
7114 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7115 * expected for different max_ops values.
7116 *
7117 * 3. Test that the number of ops done prior to start and after abort is zero
7118 * and that each successful stage completes some ops (this is not mandated by
7119 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007120 *
7121 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7122 * complete() calls does not alter the number of ops returned.
7123 *
7124 * 5. Test that after corrupting the hash, the verification detects an invalid
7125 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007126 */
Paul Elliott712d5122022-12-07 14:03:10 +00007127void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7128 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007129 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007130{
7131 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7132 psa_key_type_t key_type = key_type_arg;
7133 psa_algorithm_t alg = alg_arg;
7134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7135 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007136 uint32_t num_ops = 0;
7137 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007138 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007139 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007140 size_t min_completes = 0;
7141 size_t max_completes = 0;
7142
Paul Elliott712d5122022-12-07 14:03:10 +00007143 psa_verify_hash_interruptible_operation_t operation =
7144 psa_verify_hash_interruptible_operation_init();
7145
7146 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7147
7148 PSA_ASSERT(psa_crypto_init());
7149
7150 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7151 psa_set_key_algorithm(&attributes, alg);
7152 psa_set_key_type(&attributes, key_type);
7153
7154 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7155 &key));
7156
Paul Elliott0c683352022-12-16 19:16:56 +00007157 psa_interruptible_set_max_ops(max_ops);
7158
Paul Elliott6f600372023-02-06 18:41:05 +00007159 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7160 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007161
Paul Elliott712d5122022-12-07 14:03:10 +00007162 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7163
7164 TEST_ASSERT(num_ops_prior == 0);
7165
7166 /* Start verification. */
7167 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7168 hash_data->x, hash_data->len,
7169 signature_data->x, signature_data->len)
7170 );
7171
7172 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7173
7174 TEST_ASSERT(num_ops_prior == 0);
7175
7176 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007177 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007178 status = psa_verify_hash_complete(&operation);
7179
Paul Elliott0c683352022-12-16 19:16:56 +00007180 num_completes++;
7181
Paul Elliott712d5122022-12-07 14:03:10 +00007182 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7183 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007184 /* We are asserting here that every complete makes progress
7185 * (completes some ops), which is true of the internal
7186 * implementation and probably any implementation, however this is
7187 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007188 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007189
Paul Elliott712d5122022-12-07 14:03:10 +00007190 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007191
7192 /* Ensure calling get_num_ops() twice still returns the same
7193 * number of ops as previously reported. */
7194 num_ops = psa_verify_hash_get_num_ops(&operation);
7195
7196 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007197 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007198 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007199
7200 TEST_ASSERT(status == PSA_SUCCESS);
7201
Paul Elliott0c683352022-12-16 19:16:56 +00007202 TEST_LE_U(min_completes, num_completes);
7203 TEST_LE_U(num_completes, max_completes);
7204
Paul Elliott712d5122022-12-07 14:03:10 +00007205 PSA_ASSERT(psa_verify_hash_abort(&operation));
7206
Paul Elliott59ad9452022-12-18 15:09:02 +00007207 num_ops = psa_verify_hash_get_num_ops(&operation);
7208 TEST_ASSERT(num_ops == 0);
7209
Paul Elliott8359c142023-02-24 18:40:10 +00007210 if (hash_data->len != 0) {
7211 /* Flip a bit in the hash and verify that the signature is now detected
7212 * as invalid. Flip a bit at the beginning, not at the end, because
7213 * ECDSA may ignore the last few bits of the input. */
7214 hash_data->x[0] ^= 1;
7215
7216 /* Start verification. */
7217 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7218 hash_data->x, hash_data->len,
7219 signature_data->x, signature_data->len));
7220
7221 /* Continue performing the signature until complete. */
7222 do {
7223 status = psa_verify_hash_complete(&operation);
7224 } while (status == PSA_OPERATION_INCOMPLETE);
7225
7226 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7227 }
7228
Paul Elliott712d5122022-12-07 14:03:10 +00007229exit:
7230 psa_reset_key_attributes(&attributes);
7231 psa_destroy_key(key);
7232 PSA_DONE();
7233}
7234/* END_CASE */
7235
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007236/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007237void verify_hash_fail(int key_type_arg, data_t *key_data,
7238 int alg_arg, data_t *hash_data,
7239 data_t *signature_data,
7240 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007241{
Ronald Cron5425a212020-08-04 14:58:35 +02007242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007243 psa_key_type_t key_type = key_type_arg;
7244 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007245 psa_status_t actual_status;
7246 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007248
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007250
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7252 psa_set_key_algorithm(&attributes, alg);
7253 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007254
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7256 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007257
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 actual_status = psa_verify_hash(key, alg,
7259 hash_data->x, hash_data->len,
7260 signature_data->x, signature_data->len);
7261 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007262
7263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 psa_reset_key_attributes(&attributes);
7265 psa_destroy_key(key);
7266 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007267}
7268/* END_CASE */
7269
Paul Elliott91007972022-12-16 12:21:24 +00007270/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007271/**
7272 * verify_hash_fail_interruptible() test intentions:
7273 *
7274 * Note: This test can currently only handle ECDSA.
7275 *
7276 * 1. Test that various failure cases for interruptible verify hash fail with
7277 * the correct error codes, and at the correct point (at start or during
7278 * complete).
7279 *
7280 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7281 * expected for different max_ops values.
7282 *
7283 * 3. Test that the number of ops done prior to start and after abort is zero
7284 * and that each successful stage completes some ops (this is not mandated by
7285 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007286 *
7287 * 4. Check that calling complete() when start() fails and complete()
7288 * after completion results in a BAD_STATE error.
7289 *
7290 * 5. Check that calling start() again after start fails results in a BAD_STATE
7291 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007292 */
Paul Elliott91007972022-12-16 12:21:24 +00007293void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7294 int alg_arg, data_t *hash_data,
7295 data_t *signature_data,
7296 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007297 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007298 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007299{
7300 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7301 psa_key_type_t key_type = key_type_arg;
7302 psa_algorithm_t alg = alg_arg;
7303 psa_status_t actual_status;
7304 psa_status_t expected_start_status = expected_start_status_arg;
7305 psa_status_t expected_complete_status = expected_complete_status_arg;
7306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007307 uint32_t num_ops = 0;
7308 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007309 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007310 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007311 size_t min_completes = 0;
7312 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007313 psa_verify_hash_interruptible_operation_t operation =
7314 psa_verify_hash_interruptible_operation_init();
7315
7316 PSA_ASSERT(psa_crypto_init());
7317
7318 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7319 psa_set_key_algorithm(&attributes, alg);
7320 psa_set_key_type(&attributes, key_type);
7321
7322 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7323 &key));
7324
Paul Elliott0c683352022-12-16 19:16:56 +00007325 psa_interruptible_set_max_ops(max_ops);
7326
Paul Elliott6f600372023-02-06 18:41:05 +00007327 interruptible_signverify_get_minmax_completes(max_ops,
7328 expected_complete_status,
7329 &min_completes,
7330 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007331
Paul Elliott91007972022-12-16 12:21:24 +00007332 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7333 TEST_ASSERT(num_ops_prior == 0);
7334
7335 /* Start verification. */
7336 actual_status = psa_verify_hash_start(&operation, key, alg,
7337 hash_data->x, hash_data->len,
7338 signature_data->x,
7339 signature_data->len);
7340
7341 TEST_EQUAL(actual_status, expected_start_status);
7342
Paul Elliottc9774412023-02-06 15:14:07 +00007343 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007344 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007345 * start failed. */
7346 actual_status = psa_verify_hash_complete(&operation);
7347
7348 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7349
7350 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007351 actual_status = psa_verify_hash_start(&operation, key, alg,
7352 hash_data->x, hash_data->len,
7353 signature_data->x,
7354 signature_data->len);
7355
7356 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7357 }
7358
Paul Elliott91007972022-12-16 12:21:24 +00007359 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7360 TEST_ASSERT(num_ops_prior == 0);
7361
Paul Elliott91007972022-12-16 12:21:24 +00007362 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007363 do {
Paul Elliott91007972022-12-16 12:21:24 +00007364 actual_status = psa_verify_hash_complete(&operation);
7365
Paul Elliott0c683352022-12-16 19:16:56 +00007366 num_completes++;
7367
Paul Elliott334d7262023-01-20 17:29:41 +00007368 if (actual_status == PSA_SUCCESS ||
7369 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007370 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007371 /* We are asserting here that every complete makes progress
7372 * (completes some ops), which is true of the internal
7373 * implementation and probably any implementation, however this is
7374 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007375 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007376
Paul Elliott91007972022-12-16 12:21:24 +00007377 num_ops_prior = num_ops;
7378 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007379 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007380
Paul Elliottc9774412023-02-06 15:14:07 +00007381 TEST_EQUAL(actual_status, expected_complete_status);
7382
Paul Elliottefebad02023-02-15 16:56:45 +00007383 /* Check that another complete returns BAD_STATE. */
7384 actual_status = psa_verify_hash_complete(&operation);
7385 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007386
Paul Elliott0c683352022-12-16 19:16:56 +00007387 TEST_LE_U(min_completes, num_completes);
7388 TEST_LE_U(num_completes, max_completes);
7389
Paul Elliott91007972022-12-16 12:21:24 +00007390 PSA_ASSERT(psa_verify_hash_abort(&operation));
7391
Paul Elliott59ad9452022-12-18 15:09:02 +00007392 num_ops = psa_verify_hash_get_num_ops(&operation);
7393 TEST_ASSERT(num_ops == 0);
7394
Paul Elliott91007972022-12-16 12:21:24 +00007395exit:
7396 psa_reset_key_attributes(&attributes);
7397 psa_destroy_key(key);
7398 PSA_DONE();
7399}
7400/* END_CASE */
7401
Paul Elliott20a36062022-12-18 13:21:25 +00007402/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007403/**
7404 * interruptible_signverify_hash_state_test() test intentions:
7405 *
7406 * Note: This test can currently only handle ECDSA.
7407 *
7408 * 1. Test that calling the various interruptible sign and verify hash functions
7409 * in incorrect orders returns BAD_STATE errors.
7410 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007411void interruptible_signverify_hash_state_test(int key_type_arg,
7412 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007413{
7414 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7415 psa_key_type_t key_type = key_type_arg;
7416 psa_algorithm_t alg = alg_arg;
7417 size_t key_bits;
7418 unsigned char *signature = NULL;
7419 size_t signature_size;
7420 size_t signature_length = 0xdeadbeef;
7421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7422 psa_sign_hash_interruptible_operation_t sign_operation =
7423 psa_sign_hash_interruptible_operation_init();
7424 psa_verify_hash_interruptible_operation_t verify_operation =
7425 psa_verify_hash_interruptible_operation_init();
7426
7427 PSA_ASSERT(psa_crypto_init());
7428
7429 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7430 PSA_KEY_USAGE_VERIFY_HASH);
7431 psa_set_key_algorithm(&attributes, alg);
7432 psa_set_key_type(&attributes, key_type);
7433
7434 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7435 &key));
7436 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7437 key_bits = psa_get_key_bits(&attributes);
7438
7439 /* Allocate a buffer which has the size advertised by the
7440 * library. */
7441 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7442 key_bits, alg);
7443 TEST_ASSERT(signature_size != 0);
7444 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7445 ASSERT_ALLOC(signature, signature_size);
7446
7447 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7448
7449 /* --- Attempt completes prior to starts --- */
7450 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7451 signature_size,
7452 &signature_length),
7453 PSA_ERROR_BAD_STATE);
7454
7455 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7456
7457 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7458 PSA_ERROR_BAD_STATE);
7459
7460 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7461
7462 /* --- Aborts in all other places. --- */
7463 psa_sign_hash_abort(&sign_operation);
7464
7465 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7466 input_data->x, input_data->len));
7467
7468 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7469
7470 psa_interruptible_set_max_ops(1);
7471
7472 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7473 input_data->x, input_data->len));
7474
7475 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7476 signature_size,
7477 &signature_length),
7478 PSA_OPERATION_INCOMPLETE);
7479
7480 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7481
7482 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7483
7484 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7485 input_data->x, input_data->len));
7486
7487 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7488 signature_size,
7489 &signature_length));
7490
7491 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7492
7493 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7494
7495 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7496 input_data->x, input_data->len,
7497 signature, signature_length));
7498
7499 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7500
7501 psa_interruptible_set_max_ops(1);
7502
7503 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7504 input_data->x, input_data->len,
7505 signature, signature_length));
7506
7507 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7508 PSA_OPERATION_INCOMPLETE);
7509
7510 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7511
7512 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7513
7514 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7515 input_data->x, input_data->len,
7516 signature, signature_length));
7517
7518 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7519
7520 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7521
7522 /* --- Attempt double starts. --- */
7523
7524 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7525 input_data->x, input_data->len));
7526
7527 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7528 input_data->x, input_data->len),
7529 PSA_ERROR_BAD_STATE);
7530
7531 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7532
7533 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7534 input_data->x, input_data->len,
7535 signature, signature_length));
7536
7537 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7538 input_data->x, input_data->len,
7539 signature, signature_length),
7540 PSA_ERROR_BAD_STATE);
7541
7542 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7543
Paul Elliott76d671a2023-02-07 17:45:18 +00007544exit:
7545 /*
7546 * Key attributes may have been returned by psa_get_key_attributes()
7547 * thus reset them as required.
7548 */
7549 psa_reset_key_attributes(&attributes);
7550
7551 psa_destroy_key(key);
7552 mbedtls_free(signature);
7553 PSA_DONE();
7554}
7555/* END_CASE */
7556
7557/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007558/**
Paul Elliottc2033502023-02-26 17:09:14 +00007559 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007560 *
7561 * Note: This test can currently only handle ECDSA.
7562 *
7563 * 1. Test various edge cases in the interruptible sign and verify hash
7564 * interfaces.
7565 */
Paul Elliottc2033502023-02-26 17:09:14 +00007566void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007567 data_t *key_data, int alg_arg, data_t *input_data)
7568{
7569 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7570 psa_key_type_t key_type = key_type_arg;
7571 psa_algorithm_t alg = alg_arg;
7572 size_t key_bits;
7573 unsigned char *signature = NULL;
7574 size_t signature_size;
7575 size_t signature_length = 0xdeadbeef;
7576 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7577 uint8_t *input_buffer = NULL;
7578 psa_sign_hash_interruptible_operation_t sign_operation =
7579 psa_sign_hash_interruptible_operation_init();
7580 psa_verify_hash_interruptible_operation_t verify_operation =
7581 psa_verify_hash_interruptible_operation_init();
7582
7583 PSA_ASSERT(psa_crypto_init());
7584
7585 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7586 PSA_KEY_USAGE_VERIFY_HASH);
7587 psa_set_key_algorithm(&attributes, alg);
7588 psa_set_key_type(&attributes, key_type);
7589
7590 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7591 &key));
7592 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7593 key_bits = psa_get_key_bits(&attributes);
7594
7595 /* Allocate a buffer which has the size advertised by the
7596 * library. */
7597 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7598 key_bits, alg);
7599 TEST_ASSERT(signature_size != 0);
7600 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7601 ASSERT_ALLOC(signature, signature_size);
7602
Paul Elliott20a36062022-12-18 13:21:25 +00007603 /* --- Change function inputs mid run, to cause an error (sign only,
7604 * verify passes all inputs to start. --- */
7605
7606 psa_interruptible_set_max_ops(1);
7607
7608 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7609 input_data->x, input_data->len));
7610
7611 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7612 signature_size,
7613 &signature_length),
7614 PSA_OPERATION_INCOMPLETE);
7615
7616 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7617 0,
7618 &signature_length),
7619 PSA_ERROR_BUFFER_TOO_SMALL);
7620
Paul Elliottc9774412023-02-06 15:14:07 +00007621 /* And test that this invalidates the operation. */
7622 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7623 0,
7624 &signature_length),
7625 PSA_ERROR_BAD_STATE);
7626
Paul Elliott20a36062022-12-18 13:21:25 +00007627 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7628
Paul Elliottf9c91a72023-02-05 18:06:38 +00007629 /* Trash the hash buffer in between start and complete, to ensure
7630 * no reliance on external buffers. */
7631 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
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_sign_hash_start(&sign_operation, key, alg,
7639 input_buffer, input_data->len));
7640
7641 memset(input_buffer, '!', input_data->len);
7642 mbedtls_free(input_buffer);
7643 input_buffer = NULL;
7644
7645 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7646 signature_size,
7647 &signature_length));
7648
7649 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7650
7651 input_buffer = mbedtls_calloc(1, input_data->len);
7652 TEST_ASSERT(input_buffer != NULL);
7653
7654 memcpy(input_buffer, input_data->x, input_data->len);
7655
7656 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7657 input_buffer, input_data->len,
7658 signature, signature_length));
7659
7660 memset(input_buffer, '!', input_data->len);
7661 mbedtls_free(input_buffer);
7662 input_buffer = NULL;
7663
7664 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7665
7666 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7667
Paul Elliott20a36062022-12-18 13:21:25 +00007668exit:
7669 /*
7670 * Key attributes may have been returned by psa_get_key_attributes()
7671 * thus reset them as required.
7672 */
7673 psa_reset_key_attributes(&attributes);
7674
7675 psa_destroy_key(key);
7676 mbedtls_free(signature);
7677 PSA_DONE();
7678}
7679/* END_CASE */
7680
Paul Elliotta4cb9092023-02-07 18:01:55 +00007681/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007682/**
Paul Elliott57702242023-02-26 20:36:10 +00007683 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007684 *
7685 * Note: This test can currently only handle ECDSA.
7686 *
7687 * 1. Test that setting max ops is reflected in both interruptible sign and
7688 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007689 * 2. Test that changing the value of max_ops to unlimited during an operation
7690 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007691 *
7692 * 3. Test that calling get_num_ops() between complete calls gives the same
7693 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007694 */
Paul Elliott57702242023-02-26 20:36:10 +00007695void interruptible_signverify_hash_ops_tests(int key_type_arg,
7696 data_t *key_data, int alg_arg,
7697 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007698{
7699 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7700 psa_key_type_t key_type = key_type_arg;
7701 psa_algorithm_t alg = alg_arg;
7702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007703 size_t key_bits;
7704 unsigned char *signature = NULL;
7705 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007706 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007707 uint32_t num_ops = 0;
7708 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7709
Paul Elliotta4cb9092023-02-07 18:01:55 +00007710 psa_sign_hash_interruptible_operation_t sign_operation =
7711 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007712 psa_verify_hash_interruptible_operation_t verify_operation =
7713 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007714
7715 PSA_ASSERT(psa_crypto_init());
7716
7717 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7718 PSA_KEY_USAGE_VERIFY_HASH);
7719 psa_set_key_algorithm(&attributes, alg);
7720 psa_set_key_type(&attributes, key_type);
7721
Paul Elliottf1743e22023-02-15 18:44:16 +00007722 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7723 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7724 key_bits = psa_get_key_bits(&attributes);
7725
7726 /* Allocate a buffer which has the size advertised by the
7727 * library. */
7728 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7729
7730 TEST_ASSERT(signature_size != 0);
7731 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7732 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007733
7734 /* Check that default max ops gets set if we don't set it. */
7735 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7736 input_data->x, input_data->len));
7737
7738 TEST_EQUAL(psa_interruptible_get_max_ops(),
7739 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7740
7741 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7742
Paul Elliottf1743e22023-02-15 18:44:16 +00007743 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7744 input_data->x, input_data->len,
7745 signature, signature_size));
7746
7747 TEST_EQUAL(psa_interruptible_get_max_ops(),
7748 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7749
7750 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7751
Paul Elliotta4cb9092023-02-07 18:01:55 +00007752 /* Check that max ops gets set properly. */
7753
7754 psa_interruptible_set_max_ops(0xbeef);
7755
Paul Elliottf1743e22023-02-15 18:44:16 +00007756 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007757
Paul Elliott9e8819f2023-02-26 19:01:35 +00007758 /* --- Ensure changing the max ops mid operation works (operation should
7759 * complete successfully after setting max ops to unlimited --- */
7760 psa_interruptible_set_max_ops(1);
7761
7762 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7763 input_data->x, input_data->len));
7764
7765 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7766 signature_size,
7767 &signature_length),
7768 PSA_OPERATION_INCOMPLETE);
7769
7770 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7771
7772 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7773 signature_size,
7774 &signature_length));
7775
7776 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7777
7778 psa_interruptible_set_max_ops(1);
7779
7780 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7781 input_data->x, input_data->len,
7782 signature, signature_length));
7783
7784 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7785 PSA_OPERATION_INCOMPLETE);
7786
7787 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7788
7789 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7790
7791 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7792
Paul Elliottc1e04002023-02-26 20:27:23 +00007793 /* --- Test that not calling get_num_ops inbetween complete calls does not
7794 * result in lost ops. ---*/
7795
7796 psa_interruptible_set_max_ops(1);
7797
7798 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7799 input_data->x, input_data->len));
7800
7801 /* Continue performing the signature until complete. */
7802 do {
7803 status = psa_sign_hash_complete(&sign_operation, signature,
7804 signature_size,
7805 &signature_length);
7806
7807 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7808
7809 } while (status == PSA_OPERATION_INCOMPLETE);
7810
7811 PSA_ASSERT(status);
7812
7813 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7814
7815 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7816 input_data->x, input_data->len));
7817
7818 /* Continue performing the signature until complete. */
7819 do {
7820 status = psa_sign_hash_complete(&sign_operation, signature,
7821 signature_size,
7822 &signature_length);
7823 } while (status == PSA_OPERATION_INCOMPLETE);
7824
7825 PSA_ASSERT(status);
7826
7827 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7828
7829 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7830
7831 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7832 input_data->x, input_data->len,
7833 signature, signature_length));
7834
7835 /* Continue performing the verification until complete. */
7836 do {
7837 status = psa_verify_hash_complete(&verify_operation);
7838
7839 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7840
7841 } while (status == PSA_OPERATION_INCOMPLETE);
7842
7843 PSA_ASSERT(status);
7844
7845 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7846
7847 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7848 input_data->x, input_data->len,
7849 signature, signature_length));
7850
7851 /* Continue performing the verification until complete. */
7852 do {
7853 status = psa_verify_hash_complete(&verify_operation);
7854
7855 } while (status == PSA_OPERATION_INCOMPLETE);
7856
7857 PSA_ASSERT(status);
7858
7859 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7860
7861 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7862
Paul Elliotta4cb9092023-02-07 18:01:55 +00007863exit:
7864 /*
7865 * Key attributes may have been returned by psa_get_key_attributes()
7866 * thus reset them as required.
7867 */
7868 psa_reset_key_attributes(&attributes);
7869
7870 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007871 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007872 PSA_DONE();
7873}
7874/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007875
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007876/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007877void sign_message_deterministic(int key_type_arg,
7878 data_t *key_data,
7879 int alg_arg,
7880 data_t *input_data,
7881 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007882{
7883 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7884 psa_key_type_t key_type = key_type_arg;
7885 psa_algorithm_t alg = alg_arg;
7886 size_t key_bits;
7887 unsigned char *signature = NULL;
7888 size_t signature_size;
7889 size_t signature_length = 0xdeadbeef;
7890 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7891
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007893
Gilles Peskine449bd832023-01-11 14:50:10 +01007894 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7895 psa_set_key_algorithm(&attributes, alg);
7896 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007897
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7899 &key));
7900 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7901 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007902
Gilles Peskine449bd832023-01-11 14:50:10 +01007903 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7904 TEST_ASSERT(signature_size != 0);
7905 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7906 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007907
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 PSA_ASSERT(psa_sign_message(key, alg,
7909 input_data->x, input_data->len,
7910 signature, signature_size,
7911 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007912
Gilles Peskine449bd832023-01-11 14:50:10 +01007913 ASSERT_COMPARE(output_data->x, output_data->len,
7914 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007915
7916exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007917 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007918
Gilles Peskine449bd832023-01-11 14:50:10 +01007919 psa_destroy_key(key);
7920 mbedtls_free(signature);
7921 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007922
7923}
7924/* END_CASE */
7925
7926/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007927void sign_message_fail(int key_type_arg,
7928 data_t *key_data,
7929 int alg_arg,
7930 data_t *input_data,
7931 int signature_size_arg,
7932 int expected_status_arg)
7933{
7934 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7935 psa_key_type_t key_type = key_type_arg;
7936 psa_algorithm_t alg = alg_arg;
7937 size_t signature_size = signature_size_arg;
7938 psa_status_t actual_status;
7939 psa_status_t expected_status = expected_status_arg;
7940 unsigned char *signature = NULL;
7941 size_t signature_length = 0xdeadbeef;
7942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7943
7944 ASSERT_ALLOC(signature, signature_size);
7945
7946 PSA_ASSERT(psa_crypto_init());
7947
7948 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7949 psa_set_key_algorithm(&attributes, alg);
7950 psa_set_key_type(&attributes, key_type);
7951
7952 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7953 &key));
7954
7955 actual_status = psa_sign_message(key, alg,
7956 input_data->x, input_data->len,
7957 signature, signature_size,
7958 &signature_length);
7959 TEST_EQUAL(actual_status, expected_status);
7960 /* The value of *signature_length is unspecified on error, but
7961 * whatever it is, it should be less than signature_size, so that
7962 * if the caller tries to read *signature_length bytes without
7963 * checking the error code then they don't overflow a buffer. */
7964 TEST_LE_U(signature_length, signature_size);
7965
7966exit:
7967 psa_reset_key_attributes(&attributes);
7968 psa_destroy_key(key);
7969 mbedtls_free(signature);
7970 PSA_DONE();
7971}
7972/* END_CASE */
7973
7974/* BEGIN_CASE */
7975void sign_verify_message(int key_type_arg,
7976 data_t *key_data,
7977 int alg_arg,
7978 data_t *input_data)
7979{
7980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7981 psa_key_type_t key_type = key_type_arg;
7982 psa_algorithm_t alg = alg_arg;
7983 size_t key_bits;
7984 unsigned char *signature = NULL;
7985 size_t signature_size;
7986 size_t signature_length = 0xdeadbeef;
7987 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7988
7989 PSA_ASSERT(psa_crypto_init());
7990
7991 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7992 PSA_KEY_USAGE_VERIFY_MESSAGE);
7993 psa_set_key_algorithm(&attributes, alg);
7994 psa_set_key_type(&attributes, key_type);
7995
7996 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7997 &key));
7998 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7999 key_bits = psa_get_key_bits(&attributes);
8000
8001 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8002 TEST_ASSERT(signature_size != 0);
8003 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
8004 ASSERT_ALLOC(signature, signature_size);
8005
8006 PSA_ASSERT(psa_sign_message(key, alg,
8007 input_data->x, input_data->len,
8008 signature, signature_size,
8009 &signature_length));
8010 TEST_LE_U(signature_length, signature_size);
8011 TEST_ASSERT(signature_length > 0);
8012
8013 PSA_ASSERT(psa_verify_message(key, alg,
8014 input_data->x, input_data->len,
8015 signature, signature_length));
8016
8017 if (input_data->len != 0) {
8018 /* Flip a bit in the input and verify that the signature is now
8019 * detected as invalid. Flip a bit at the beginning, not at the end,
8020 * because ECDSA may ignore the last few bits of the input. */
8021 input_data->x[0] ^= 1;
8022 TEST_EQUAL(psa_verify_message(key, alg,
8023 input_data->x, input_data->len,
8024 signature, signature_length),
8025 PSA_ERROR_INVALID_SIGNATURE);
8026 }
8027
8028exit:
8029 psa_reset_key_attributes(&attributes);
8030
8031 psa_destroy_key(key);
8032 mbedtls_free(signature);
8033 PSA_DONE();
8034}
8035/* END_CASE */
8036
8037/* BEGIN_CASE */
8038void verify_message(int key_type_arg,
8039 data_t *key_data,
8040 int alg_arg,
8041 data_t *input_data,
8042 data_t *signature_data)
8043{
8044 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8045 psa_key_type_t key_type = key_type_arg;
8046 psa_algorithm_t alg = alg_arg;
8047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8048
8049 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8050
8051 PSA_ASSERT(psa_crypto_init());
8052
8053 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8054 psa_set_key_algorithm(&attributes, alg);
8055 psa_set_key_type(&attributes, key_type);
8056
8057 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8058 &key));
8059
8060 PSA_ASSERT(psa_verify_message(key, alg,
8061 input_data->x, input_data->len,
8062 signature_data->x, signature_data->len));
8063
8064exit:
8065 psa_reset_key_attributes(&attributes);
8066 psa_destroy_key(key);
8067 PSA_DONE();
8068}
8069/* END_CASE */
8070
8071/* BEGIN_CASE */
8072void verify_message_fail(int key_type_arg,
8073 data_t *key_data,
8074 int alg_arg,
8075 data_t *hash_data,
8076 data_t *signature_data,
8077 int expected_status_arg)
8078{
8079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8080 psa_key_type_t key_type = key_type_arg;
8081 psa_algorithm_t alg = alg_arg;
8082 psa_status_t actual_status;
8083 psa_status_t expected_status = expected_status_arg;
8084 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8085
8086 PSA_ASSERT(psa_crypto_init());
8087
8088 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8089 psa_set_key_algorithm(&attributes, alg);
8090 psa_set_key_type(&attributes, key_type);
8091
8092 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8093 &key));
8094
8095 actual_status = psa_verify_message(key, alg,
8096 hash_data->x, hash_data->len,
8097 signature_data->x,
8098 signature_data->len);
8099 TEST_EQUAL(actual_status, expected_status);
8100
8101exit:
8102 psa_reset_key_attributes(&attributes);
8103 psa_destroy_key(key);
8104 PSA_DONE();
8105}
8106/* END_CASE */
8107
8108/* BEGIN_CASE */
8109void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008110 data_t *key_data,
8111 int alg_arg,
8112 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008113 data_t *label,
8114 int expected_output_length_arg,
8115 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008116{
Ronald Cron5425a212020-08-04 14:58:35 +02008117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008118 psa_key_type_t key_type = key_type_arg;
8119 psa_algorithm_t alg = alg_arg;
8120 size_t expected_output_length = expected_output_length_arg;
8121 size_t key_bits;
8122 unsigned char *output = NULL;
8123 size_t output_size;
8124 size_t output_length = ~0;
8125 psa_status_t actual_status;
8126 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008127 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008128
Gilles Peskine449bd832023-01-11 14:50:10 +01008129 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008130
Gilles Peskine656896e2018-06-29 19:12:28 +02008131 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008132 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8133 psa_set_key_algorithm(&attributes, alg);
8134 psa_set_key_type(&attributes, key_type);
8135 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8136 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008137
8138 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008139 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8140 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008141
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8143 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8144 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008145
8146 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008147 actual_status = psa_asymmetric_encrypt(key, alg,
8148 input_data->x, input_data->len,
8149 label->x, label->len,
8150 output, output_size,
8151 &output_length);
8152 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008153 if (actual_status == PSA_SUCCESS) {
8154 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008155 } else {
8156 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008157 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008158
Gilles Peskine68428122018-06-30 18:42:41 +02008159 /* If the label is empty, the test framework puts a non-null pointer
8160 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008161 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008162 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008163 if (output_size != 0) {
8164 memset(output, 0, output_size);
8165 }
8166 actual_status = psa_asymmetric_encrypt(key, alg,
8167 input_data->x, input_data->len,
8168 NULL, label->len,
8169 output, output_size,
8170 &output_length);
8171 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008172 if (actual_status == PSA_SUCCESS) {
8173 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008174 } else {
8175 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008176 }
Gilles Peskine68428122018-06-30 18:42:41 +02008177 }
8178
Gilles Peskine656896e2018-06-29 19:12:28 +02008179exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008180 /*
8181 * Key attributes may have been returned by psa_get_key_attributes()
8182 * thus reset them as required.
8183 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008184 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008185
Gilles Peskine449bd832023-01-11 14:50:10 +01008186 psa_destroy_key(key);
8187 mbedtls_free(output);
8188 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008189}
8190/* END_CASE */
8191
8192/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008193void asymmetric_encrypt_decrypt(int key_type_arg,
8194 data_t *key_data,
8195 int alg_arg,
8196 data_t *input_data,
8197 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008198{
Ronald Cron5425a212020-08-04 14:58:35 +02008199 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008200 psa_key_type_t key_type = key_type_arg;
8201 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008202 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008203 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008204 size_t output_size;
8205 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008206 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008207 size_t output2_size;
8208 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008210
Gilles Peskine449bd832023-01-11 14:50:10 +01008211 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008212
Gilles Peskine449bd832023-01-11 14:50:10 +01008213 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8214 psa_set_key_algorithm(&attributes, alg);
8215 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008216
Gilles Peskine449bd832023-01-11 14:50:10 +01008217 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8218 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008219
8220 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008221 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8222 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008223
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8225 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8226 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008227
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008228 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 TEST_LE_U(output2_size,
8230 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8231 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8232 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008233
Gilles Peskineeebd7382018-06-08 18:11:54 +02008234 /* We test encryption by checking that encrypt-then-decrypt gives back
8235 * the original plaintext because of the non-optional random
8236 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8238 input_data->x, input_data->len,
8239 label->x, label->len,
8240 output, output_size,
8241 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008242 /* We don't know what ciphertext length to expect, but check that
8243 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008245
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8247 output, output_length,
8248 label->x, label->len,
8249 output2, output2_size,
8250 &output2_length));
8251 ASSERT_COMPARE(input_data->x, input_data->len,
8252 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008253
8254exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008255 /*
8256 * Key attributes may have been returned by psa_get_key_attributes()
8257 * thus reset them as required.
8258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008259 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008260
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 psa_destroy_key(key);
8262 mbedtls_free(output);
8263 mbedtls_free(output2);
8264 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008265}
8266/* END_CASE */
8267
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008268/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008269void asymmetric_decrypt(int key_type_arg,
8270 data_t *key_data,
8271 int alg_arg,
8272 data_t *input_data,
8273 data_t *label,
8274 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008275{
Ronald Cron5425a212020-08-04 14:58:35 +02008276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008277 psa_key_type_t key_type = key_type_arg;
8278 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008279 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008280 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008281 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008282 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008283 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008284
Gilles Peskine449bd832023-01-11 14:50:10 +01008285 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008286
Gilles Peskine449bd832023-01-11 14:50:10 +01008287 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8288 psa_set_key_algorithm(&attributes, alg);
8289 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008290
Gilles Peskine449bd832023-01-11 14:50:10 +01008291 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8292 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008293
Gilles Peskine449bd832023-01-11 14:50:10 +01008294 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8295 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008296
8297 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8299 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8300 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008301
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8303 input_data->x, input_data->len,
8304 label->x, label->len,
8305 output,
8306 output_size,
8307 &output_length));
8308 ASSERT_COMPARE(expected_data->x, expected_data->len,
8309 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008310
Gilles Peskine68428122018-06-30 18:42:41 +02008311 /* If the label is empty, the test framework puts a non-null pointer
8312 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008313 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008314 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008315 if (output_size != 0) {
8316 memset(output, 0, output_size);
8317 }
8318 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8319 input_data->x, input_data->len,
8320 NULL, label->len,
8321 output,
8322 output_size,
8323 &output_length));
8324 ASSERT_COMPARE(expected_data->x, expected_data->len,
8325 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008326 }
8327
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008328exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 psa_reset_key_attributes(&attributes);
8330 psa_destroy_key(key);
8331 mbedtls_free(output);
8332 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008333}
8334/* END_CASE */
8335
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008336/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008337void asymmetric_decrypt_fail(int key_type_arg,
8338 data_t *key_data,
8339 int alg_arg,
8340 data_t *input_data,
8341 data_t *label,
8342 int output_size_arg,
8343 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008344{
Ronald Cron5425a212020-08-04 14:58:35 +02008345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008346 psa_key_type_t key_type = key_type_arg;
8347 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008348 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008349 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008350 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008351 psa_status_t actual_status;
8352 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008354
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008356
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008358
Gilles Peskine449bd832023-01-11 14:50:10 +01008359 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8360 psa_set_key_algorithm(&attributes, alg);
8361 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008362
Gilles Peskine449bd832023-01-11 14:50:10 +01008363 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8364 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008365
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 actual_status = psa_asymmetric_decrypt(key, alg,
8367 input_data->x, input_data->len,
8368 label->x, label->len,
8369 output, output_size,
8370 &output_length);
8371 TEST_EQUAL(actual_status, expected_status);
8372 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008373
Gilles Peskine68428122018-06-30 18:42:41 +02008374 /* If the label is empty, the test framework puts a non-null pointer
8375 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008377 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008378 if (output_size != 0) {
8379 memset(output, 0, output_size);
8380 }
8381 actual_status = psa_asymmetric_decrypt(key, alg,
8382 input_data->x, input_data->len,
8383 NULL, label->len,
8384 output, output_size,
8385 &output_length);
8386 TEST_EQUAL(actual_status, expected_status);
8387 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008388 }
8389
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008390exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 psa_reset_key_attributes(&attributes);
8392 psa_destroy_key(key);
8393 mbedtls_free(output);
8394 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008395}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008396/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008397
8398/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008399void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008400{
8401 /* Test each valid way of initializing the object, except for `= {0}`, as
8402 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8403 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008404 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008405 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008406 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008407 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8408 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008409
Gilles Peskine449bd832023-01-11 14:50:10 +01008410 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008411
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008412 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008413 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8414 PSA_ERROR_BAD_STATE);
8415 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8416 PSA_ERROR_BAD_STATE);
8417 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8418 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008419
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008420 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 PSA_ASSERT(psa_key_derivation_abort(&func));
8422 PSA_ASSERT(psa_key_derivation_abort(&init));
8423 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008424}
8425/* END_CASE */
8426
Janos Follath16de4a42019-06-13 16:32:24 +01008427/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008428void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008429{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008430 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008431 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008432 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008433
Gilles Peskine449bd832023-01-11 14:50:10 +01008434 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008435
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8437 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008438
8439exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008440 psa_key_derivation_abort(&operation);
8441 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008442}
8443/* END_CASE */
8444
Janos Follathaf3c2a02019-06-12 12:34:34 +01008445/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008446void derive_set_capacity(int alg_arg, int capacity_arg,
8447 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008448{
8449 psa_algorithm_t alg = alg_arg;
8450 size_t capacity = capacity_arg;
8451 psa_status_t expected_status = expected_status_arg;
8452 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8453
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008455
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008457
Gilles Peskine449bd832023-01-11 14:50:10 +01008458 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8459 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008460
8461exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 psa_key_derivation_abort(&operation);
8463 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008464}
8465/* END_CASE */
8466
8467/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008468void derive_input(int alg_arg,
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308469 int step_arg1, int key_type_arg1, int input_type_arg1,
8470 data_t *input1, int expected_status_arg1,
8471 int step_arg2, int key_type_arg2, int input_type_arg2,
8472 data_t *input2, int expected_status_arg2,
8473 int step_arg3, int key_type_arg3, int input_type_arg3,
8474 data_t *input3, int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008475 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008476{
8477 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008478 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8479 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05308480 key_derivation_input_method_t input_types[] =
8481 { input_type_arg1, input_type_arg2, input_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 psa_status_t expected_statuses[] = { expected_status_arg1,
8483 expected_status_arg2,
8484 expected_status_arg3 };
8485 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008486 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8487 MBEDTLS_SVC_KEY_ID_INIT,
8488 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008489 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8491 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008492 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008493 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008494 psa_status_t expected_output_status = expected_output_status_arg;
8495 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008496
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008498
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8500 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008501
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008503
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8505 mbedtls_test_set_step(i);
8506 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008507 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008508 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8509 psa_set_key_type(&attributes, key_types[i]);
8510 PSA_ASSERT(psa_import_key(&attributes,
8511 inputs[i]->x, inputs[i]->len,
8512 &keys[i]));
8513 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8514 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008515 // When taking a private key as secret input, use key agreement
8516 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8518 &operation, keys[i]),
8519 expected_statuses[i]);
8520 } else {
8521 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8522 keys[i]),
8523 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008524 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 } else {
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05308526 if (input_types[i] == INPUT_BYTES) {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308527 TEST_EQUAL(psa_key_derivation_input_bytes(
8528 &operation, steps[i],
8529 inputs[i]->x, inputs[i]->len),
8530 expected_statuses[i]);
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05308531 } else if (input_types[i] == INPUT_INTEGER) {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308532 TEST_EQUAL(psa_key_derivation_input_integer(
8533 &operation, steps[i],
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05308534 parse_hex_string(inputs[i])),
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308535 expected_statuses[i]);
8536 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008537 }
8538 }
8539
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 if (output_key_type != PSA_KEY_TYPE_NONE) {
8541 psa_reset_key_attributes(&attributes);
8542 psa_set_key_type(&attributes, output_key_type);
8543 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008544 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 psa_key_derivation_output_key(&attributes, &operation,
8546 &output_key);
8547 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008548 uint8_t buffer[1];
8549 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 psa_key_derivation_output_bytes(&operation,
8551 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008552 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008554
Janos Follathaf3c2a02019-06-12 12:34:34 +01008555exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 psa_key_derivation_abort(&operation);
8557 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8558 psa_destroy_key(keys[i]);
8559 }
8560 psa_destroy_key(output_key);
8561 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008562}
8563/* END_CASE */
8564
Janos Follathd958bb72019-07-03 15:02:16 +01008565/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008566void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008567{
Janos Follathd958bb72019-07-03 15:02:16 +01008568 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008569 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008570 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008571 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008572 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008574 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008576 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008577 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008578 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8579 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008580 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008581 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008582
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008584
Gilles Peskine449bd832023-01-11 14:50:10 +01008585 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8586 psa_set_key_algorithm(&attributes, alg);
8587 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008588
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 PSA_ASSERT(psa_import_key(&attributes,
8590 key_data, sizeof(key_data),
8591 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008592
8593 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8595 input1, input1_length,
8596 input2, input2_length,
8597 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008598 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008600
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008601 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008602 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
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_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008606
Gilles Peskine449bd832023-01-11 14:50:10 +01008607 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8608 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008609
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008610exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 psa_key_derivation_abort(&operation);
8612 psa_destroy_key(key);
8613 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008614}
8615/* END_CASE */
8616
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008617/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008618void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008619{
8620 uint8_t output_buffer[16];
8621 size_t buffer_size = 16;
8622 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008623 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008624
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8626 output_buffer, buffer_size)
8627 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008628
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8630 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008631
Gilles Peskine449bd832023-01-11 14:50:10 +01008632 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008633
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8635 output_buffer, buffer_size)
8636 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008637
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8639 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008640
8641exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008643}
8644/* END_CASE */
8645
8646/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008647void derive_output(int alg_arg,
8648 int step1_arg, data_t *input1, int expected_status_arg1,
8649 int step2_arg, data_t *input2, int expected_status_arg2,
8650 int step3_arg, data_t *input3, int expected_status_arg3,
8651 int step4_arg, data_t *input4, int expected_status_arg4,
8652 data_t *key_agreement_peer_key,
8653 int requested_capacity_arg,
8654 data_t *expected_output1,
8655 data_t *expected_output2,
8656 int other_key_input_type,
8657 int key_input_type,
8658 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008659{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008660 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8662 data_t *inputs[] = { input1, input2, input3, input4 };
8663 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8664 MBEDTLS_SVC_KEY_ID_INIT,
8665 MBEDTLS_SVC_KEY_ID_INIT,
8666 MBEDTLS_SVC_KEY_ID_INIT };
8667 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8668 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008669 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008670 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008671 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008673 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008675 size_t output_buffer_size = 0;
8676 uint8_t *output_buffer = NULL;
8677 size_t expected_capacity;
8678 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008679 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8680 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8681 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8682 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008683 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008684 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008685 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008686
Gilles Peskine449bd832023-01-11 14:50:10 +01008687 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8688 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008689 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 }
8691 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008692 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008694 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 ASSERT_ALLOC(output_buffer, output_buffer_size);
8696 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008697
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008698 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8700 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8701 requested_capacity));
8702 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8703 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008704 case 0:
8705 break;
8706 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008708 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 TEST_EQUAL(psa_key_derivation_input_bytes(
8710 &operation, steps[i],
8711 inputs[i]->x, inputs[i]->len),
8712 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008713
Gilles Peskine449bd832023-01-11 14:50:10 +01008714 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008715 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008717 break;
8718 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8720 psa_set_key_algorithm(&attributes1, alg);
8721 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008722
Gilles Peskine449bd832023-01-11 14:50:10 +01008723 PSA_ASSERT(psa_import_key(&attributes1,
8724 inputs[i]->x, inputs[i]->len,
8725 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008726
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8728 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8729 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8730 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008731 }
8732
Gilles Peskine449bd832023-01-11 14:50:10 +01008733 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8734 steps[i],
8735 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008736 break;
8737 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008738 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008739 break;
8740 }
8741 break;
8742 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008744 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008745 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8746 steps[i],
8747 inputs[i]->x,
8748 inputs[i]->len),
8749 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008750 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008751 case 1: // input key, type DERIVE
8752 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008753 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8754 psa_set_key_algorithm(&attributes2, alg);
8755 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008756
8757 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008758 if (other_key_input_type == 11) {
8759 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8760 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008761
Gilles Peskine449bd832023-01-11 14:50:10 +01008762 PSA_ASSERT(psa_import_key(&attributes2,
8763 inputs[i]->x, inputs[i]->len,
8764 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008765
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8767 steps[i],
8768 keys[i]),
8769 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008770 break;
8771 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8773 psa_set_key_algorithm(&attributes3, alg);
8774 psa_set_key_type(&attributes3,
8775 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008776
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 PSA_ASSERT(psa_import_key(&attributes3,
8778 inputs[i]->x, inputs[i]->len,
8779 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008780
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 TEST_EQUAL(psa_key_derivation_key_agreement(
8782 &operation,
8783 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8784 keys[i], key_agreement_peer_key->x,
8785 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008786 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008787 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008789 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008790 }
8791
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008793 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008795 break;
8796 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 TEST_EQUAL(psa_key_derivation_input_bytes(
8798 &operation, steps[i],
8799 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008800
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008802 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008803 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008804 break;
8805 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008806 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008807
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8809 &current_capacity));
8810 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008811 expected_capacity = requested_capacity;
8812
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008814 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8815
8816 /* For output key derivation secret must be provided using
8817 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008819 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008821
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8823 psa_set_key_algorithm(&attributes4, alg);
8824 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8825 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008826
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8828 &derived_key), expected_status);
8829 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008830 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008831 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008832 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008833 status = psa_key_derivation_output_bytes(&operation,
8834 output_buffer, output_sizes[i]);
8835 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008836 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 TEST_ASSERT(status == PSA_SUCCESS ||
8838 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008839 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 } else if (expected_capacity == 0 ||
8841 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008842 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008843 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008844 expected_capacity = 0;
8845 continue;
8846 }
8847 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 PSA_ASSERT(status);
8849 if (output_sizes[i] != 0) {
8850 ASSERT_COMPARE(output_buffer, output_sizes[i],
8851 expected_outputs[i], output_sizes[i]);
8852 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008853 /* Check the operation status. */
8854 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8856 &current_capacity));
8857 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008858 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008859 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008861
8862exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 mbedtls_free(output_buffer);
8864 psa_key_derivation_abort(&operation);
8865 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8866 psa_destroy_key(keys[i]);
8867 }
8868 psa_destroy_key(derived_key);
8869 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008870}
8871/* END_CASE */
8872
8873/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008874void derive_full(int alg_arg,
8875 data_t *key_data,
8876 data_t *input1,
8877 data_t *input2,
8878 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008879{
Ronald Cron5425a212020-08-04 14:58:35 +02008880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008881 psa_algorithm_t alg = alg_arg;
8882 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008883 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008884 unsigned char output_buffer[16];
8885 size_t expected_capacity = requested_capacity;
8886 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008888
Gilles Peskine449bd832023-01-11 14:50:10 +01008889 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008890
Gilles Peskine449bd832023-01-11 14:50:10 +01008891 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8892 psa_set_key_algorithm(&attributes, alg);
8893 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008894
Gilles Peskine449bd832023-01-11 14:50:10 +01008895 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8896 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008897
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8899 input1->x, input1->len,
8900 input2->x, input2->len,
8901 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008902 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008904
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8906 &current_capacity));
8907 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008908
8909 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 while (current_capacity > 0) {
8911 size_t read_size = sizeof(output_buffer);
8912 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008913 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008914 }
8915 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8916 output_buffer,
8917 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008918 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008919 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8920 &current_capacity));
8921 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008922 }
8923
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008924 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8926 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008927
Gilles Peskine449bd832023-01-11 14:50:10 +01008928 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008929
8930exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008931 psa_key_derivation_abort(&operation);
8932 psa_destroy_key(key);
8933 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008934}
8935/* END_CASE */
8936
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008937/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008938void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8939 int derivation_step,
8940 int capacity, int expected_capacity_status_arg,
8941 data_t *expected_output,
8942 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008943{
8944 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8945 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008946 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008947 uint8_t *output_buffer = NULL;
8948 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008949 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8950 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8951 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008952
Gilles Peskine449bd832023-01-11 14:50:10 +01008953 ASSERT_ALLOC(output_buffer, expected_output->len);
8954 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008955
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8957 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8958 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008959
Gilles Peskine449bd832023-01-11 14:50:10 +01008960 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8961 step, input->x, input->len),
8962 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008963
Gilles Peskine449bd832023-01-11 14:50:10 +01008964 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008965 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008967
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8969 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008970
Gilles Peskine449bd832023-01-11 14:50:10 +01008971 TEST_EQUAL(status, expected_output_status);
8972 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8973 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8974 expected_output->len);
8975 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008976
8977exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 mbedtls_free(output_buffer);
8979 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008980 PSA_DONE();
8981}
8982/* END_CASE */
8983
Janos Follathe60c9052019-07-03 13:51:30 +01008984/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008985void derive_key_exercise(int alg_arg,
8986 data_t *key_data,
8987 data_t *input1,
8988 data_t *input2,
8989 int derived_type_arg,
8990 int derived_bits_arg,
8991 int derived_usage_arg,
8992 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008993{
Ronald Cron5425a212020-08-04 14:58:35 +02008994 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8995 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008996 psa_algorithm_t alg = alg_arg;
8997 psa_key_type_t derived_type = derived_type_arg;
8998 size_t derived_bits = derived_bits_arg;
8999 psa_key_usage_t derived_usage = derived_usage_arg;
9000 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009002 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009004 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009005
Gilles Peskine449bd832023-01-11 14:50:10 +01009006 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009007
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9009 psa_set_key_algorithm(&attributes, alg);
9010 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9011 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9012 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009013
9014 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9016 input1->x, input1->len,
9017 input2->x, input2->len,
9018 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009019 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 }
Janos Follathe60c9052019-07-03 13:51:30 +01009021
Gilles Peskine449bd832023-01-11 14:50:10 +01009022 psa_set_key_usage_flags(&attributes, derived_usage);
9023 psa_set_key_algorithm(&attributes, derived_alg);
9024 psa_set_key_type(&attributes, derived_type);
9025 psa_set_key_bits(&attributes, derived_bits);
9026 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9027 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009028
9029 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009030 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9031 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9032 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009033
9034 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009036 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009037 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009038
9039exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009040 /*
9041 * Key attributes may have been returned by psa_get_key_attributes()
9042 * thus reset them as required.
9043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009045
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 psa_key_derivation_abort(&operation);
9047 psa_destroy_key(base_key);
9048 psa_destroy_key(derived_key);
9049 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009050}
9051/* END_CASE */
9052
Janos Follath42fd8882019-07-03 14:17:09 +01009053/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009054void derive_key_export(int alg_arg,
9055 data_t *key_data,
9056 data_t *input1,
9057 data_t *input2,
9058 int bytes1_arg,
9059 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009060{
Ronald Cron5425a212020-08-04 14:58:35 +02009061 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9062 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009063 psa_algorithm_t alg = alg_arg;
9064 size_t bytes1 = bytes1_arg;
9065 size_t bytes2 = bytes2_arg;
9066 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009067 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009068 uint8_t *output_buffer = NULL;
9069 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009070 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9071 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009072 size_t length;
9073
Gilles Peskine449bd832023-01-11 14:50:10 +01009074 ASSERT_ALLOC(output_buffer, capacity);
9075 ASSERT_ALLOC(export_buffer, capacity);
9076 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009077
Gilles Peskine449bd832023-01-11 14:50:10 +01009078 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9079 psa_set_key_algorithm(&base_attributes, alg);
9080 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9081 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9082 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009083
9084 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9086 input1->x, input1->len,
9087 input2->x, input2->len,
9088 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009089 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 }
Janos Follath42fd8882019-07-03 14:17:09 +01009091
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9093 output_buffer,
9094 capacity));
9095 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009096
9097 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9099 input1->x, input1->len,
9100 input2->x, input2->len,
9101 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 }
Janos Follath42fd8882019-07-03 14:17:09 +01009104
Gilles Peskine449bd832023-01-11 14:50:10 +01009105 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9106 psa_set_key_algorithm(&derived_attributes, 0);
9107 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9108 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9109 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9110 &derived_key));
9111 PSA_ASSERT(psa_export_key(derived_key,
9112 export_buffer, bytes1,
9113 &length));
9114 TEST_EQUAL(length, bytes1);
9115 PSA_ASSERT(psa_destroy_key(derived_key));
9116 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9117 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9118 &derived_key));
9119 PSA_ASSERT(psa_export_key(derived_key,
9120 export_buffer + bytes1, bytes2,
9121 &length));
9122 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009123
9124 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009125 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
9126 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009127
9128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 mbedtls_free(output_buffer);
9130 mbedtls_free(export_buffer);
9131 psa_key_derivation_abort(&operation);
9132 psa_destroy_key(base_key);
9133 psa_destroy_key(derived_key);
9134 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009135}
9136/* END_CASE */
9137
9138/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009139void derive_key_type(int alg_arg,
9140 data_t *key_data,
9141 data_t *input1,
9142 data_t *input2,
9143 int key_type_arg, int bits_arg,
9144 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009145{
9146 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9147 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9148 const psa_algorithm_t alg = alg_arg;
9149 const psa_key_type_t key_type = key_type_arg;
9150 const size_t bits = bits_arg;
9151 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9152 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009154 uint8_t *export_buffer = NULL;
9155 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9156 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9157 size_t export_length;
9158
Gilles Peskine449bd832023-01-11 14:50:10 +01009159 ASSERT_ALLOC(export_buffer, export_buffer_size);
9160 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009161
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9163 psa_set_key_algorithm(&base_attributes, alg);
9164 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9165 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9166 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009167
Gilles Peskine449bd832023-01-11 14:50:10 +01009168 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009169 &operation, base_key, alg,
9170 input1->x, input1->len,
9171 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009175
Gilles Peskine449bd832023-01-11 14:50:10 +01009176 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9177 psa_set_key_algorithm(&derived_attributes, 0);
9178 psa_set_key_type(&derived_attributes, key_type);
9179 psa_set_key_bits(&derived_attributes, bits);
9180 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9181 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009182
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 PSA_ASSERT(psa_export_key(derived_key,
9184 export_buffer, export_buffer_size,
9185 &export_length));
9186 ASSERT_COMPARE(export_buffer, export_length,
9187 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009188
9189exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009190 mbedtls_free(export_buffer);
9191 psa_key_derivation_abort(&operation);
9192 psa_destroy_key(base_key);
9193 psa_destroy_key(derived_key);
9194 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009195}
9196/* END_CASE */
9197
9198/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009199void derive_key(int alg_arg,
9200 data_t *key_data, data_t *input1, data_t *input2,
9201 int type_arg, int bits_arg,
9202 int expected_status_arg,
9203 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009204{
Ronald Cron5425a212020-08-04 14:58:35 +02009205 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9206 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009207 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009208 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009209 size_t bits = bits_arg;
9210 psa_status_t expected_status = expected_status_arg;
9211 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9212 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9213 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9214
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009216
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9218 psa_set_key_algorithm(&base_attributes, alg);
9219 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9220 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9221 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009222
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9224 input1->x, input1->len,
9225 input2->x, input2->len,
9226 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009227 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009228 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009229
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9231 psa_set_key_algorithm(&derived_attributes, 0);
9232 psa_set_key_type(&derived_attributes, type);
9233 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009234
9235 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009236 psa_key_derivation_output_key(&derived_attributes,
9237 &operation,
9238 &derived_key);
9239 if (is_large_output > 0) {
9240 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9241 }
9242 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009243
9244exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009245 psa_key_derivation_abort(&operation);
9246 psa_destroy_key(base_key);
9247 psa_destroy_key(derived_key);
9248 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009249}
9250/* END_CASE */
9251
9252/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009253void key_agreement_setup(int alg_arg,
9254 int our_key_type_arg, int our_key_alg_arg,
9255 data_t *our_key_data, data_t *peer_key_data,
9256 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009257{
Ronald Cron5425a212020-08-04 14:58:35 +02009258 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009259 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009260 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009261 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009262 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009264 psa_status_t expected_status = expected_status_arg;
9265 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009266
Gilles Peskine449bd832023-01-11 14:50:10 +01009267 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009268
Gilles Peskine449bd832023-01-11 14:50:10 +01009269 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9270 psa_set_key_algorithm(&attributes, our_key_alg);
9271 psa_set_key_type(&attributes, our_key_type);
9272 PSA_ASSERT(psa_import_key(&attributes,
9273 our_key_data->x, our_key_data->len,
9274 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009275
Gilles Peskine77f40d82019-04-11 21:27:06 +02009276 /* The tests currently include inputs that should fail at either step.
9277 * Test cases that fail at the setup step should be changed to call
9278 * key_derivation_setup instead, and this function should be renamed
9279 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009280 status = psa_key_derivation_setup(&operation, alg);
9281 if (status == PSA_SUCCESS) {
9282 TEST_EQUAL(psa_key_derivation_key_agreement(
9283 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9284 our_key,
9285 peer_key_data->x, peer_key_data->len),
9286 expected_status);
9287 } else {
9288 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009289 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009290
9291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009292 psa_key_derivation_abort(&operation);
9293 psa_destroy_key(our_key);
9294 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009295}
9296/* END_CASE */
9297
9298/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009299void raw_key_agreement(int alg_arg,
9300 int our_key_type_arg, data_t *our_key_data,
9301 data_t *peer_key_data,
9302 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009303{
Ronald Cron5425a212020-08-04 14:58:35 +02009304 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009305 psa_algorithm_t alg = alg_arg;
9306 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009308 unsigned char *output = NULL;
9309 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009310 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009311
Gilles Peskine449bd832023-01-11 14:50:10 +01009312 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009313
Gilles Peskine449bd832023-01-11 14:50:10 +01009314 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9315 psa_set_key_algorithm(&attributes, alg);
9316 psa_set_key_type(&attributes, our_key_type);
9317 PSA_ASSERT(psa_import_key(&attributes,
9318 our_key_data->x, our_key_data->len,
9319 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009320
Gilles Peskine449bd832023-01-11 14:50:10 +01009321 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9322 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009323
Gilles Peskine992bee82022-04-13 23:25:52 +02009324 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 TEST_LE_U(expected_output->len,
9326 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9327 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9328 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009329
9330 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009331 ASSERT_ALLOC(output, expected_output->len);
9332 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9333 peer_key_data->x, peer_key_data->len,
9334 output, expected_output->len,
9335 &output_length));
9336 ASSERT_COMPARE(output, output_length,
9337 expected_output->x, expected_output->len);
9338 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009339 output = NULL;
9340 output_length = ~0;
9341
9342 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009343 ASSERT_ALLOC(output, expected_output->len + 1);
9344 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9345 peer_key_data->x, peer_key_data->len,
9346 output, expected_output->len + 1,
9347 &output_length));
9348 ASSERT_COMPARE(output, output_length,
9349 expected_output->x, expected_output->len);
9350 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009351 output = NULL;
9352 output_length = ~0;
9353
9354 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009355 ASSERT_ALLOC(output, expected_output->len - 1);
9356 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9357 peer_key_data->x, peer_key_data->len,
9358 output, expected_output->len - 1,
9359 &output_length),
9360 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009361 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009362 TEST_LE_U(output_length, expected_output->len - 1);
9363 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009364 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009365
9366exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009367 mbedtls_free(output);
9368 psa_destroy_key(our_key);
9369 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009370}
9371/* END_CASE */
9372
9373/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009374void key_agreement_capacity(int alg_arg,
9375 int our_key_type_arg, data_t *our_key_data,
9376 data_t *peer_key_data,
9377 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009378{
Ronald Cron5425a212020-08-04 14:58:35 +02009379 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009380 psa_algorithm_t alg = alg_arg;
9381 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009382 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009384 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009385 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009386
Gilles Peskine449bd832023-01-11 14:50:10 +01009387 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009388
Gilles Peskine449bd832023-01-11 14:50:10 +01009389 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9390 psa_set_key_algorithm(&attributes, alg);
9391 psa_set_key_type(&attributes, our_key_type);
9392 PSA_ASSERT(psa_import_key(&attributes,
9393 our_key_data->x, our_key_data->len,
9394 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009395
Gilles Peskine449bd832023-01-11 14:50:10 +01009396 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9397 PSA_ASSERT(psa_key_derivation_key_agreement(
9398 &operation,
9399 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9400 peer_key_data->x, peer_key_data->len));
9401 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009402 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9404 PSA_KEY_DERIVATION_INPUT_INFO,
9405 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009406 }
Gilles Peskine59685592018-09-18 12:11:34 +02009407
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009408 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009409 PSA_ASSERT(psa_key_derivation_get_capacity(
9410 &operation, &actual_capacity));
9411 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009412
Gilles Peskinebf491972018-10-25 22:36:12 +02009413 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009414 while (actual_capacity > sizeof(output)) {
9415 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9416 output, sizeof(output)));
9417 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009418 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9420 output, actual_capacity));
9421 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9422 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009423
Gilles Peskine59685592018-09-18 12:11:34 +02009424exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009425 psa_key_derivation_abort(&operation);
9426 psa_destroy_key(our_key);
9427 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009428}
9429/* END_CASE */
9430
9431/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009432void key_agreement_output(int alg_arg,
9433 int our_key_type_arg, data_t *our_key_data,
9434 data_t *peer_key_data,
9435 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009436{
Ronald Cron5425a212020-08-04 14:58:35 +02009437 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009438 psa_algorithm_t alg = alg_arg;
9439 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009440 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009442 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009443
Gilles Peskine449bd832023-01-11 14:50:10 +01009444 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9445 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009446
Gilles Peskine449bd832023-01-11 14:50:10 +01009447 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009448
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9450 psa_set_key_algorithm(&attributes, alg);
9451 psa_set_key_type(&attributes, our_key_type);
9452 PSA_ASSERT(psa_import_key(&attributes,
9453 our_key_data->x, our_key_data->len,
9454 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009455
Gilles Peskine449bd832023-01-11 14:50:10 +01009456 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9457 PSA_ASSERT(psa_key_derivation_key_agreement(
9458 &operation,
9459 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9460 peer_key_data->x, peer_key_data->len));
9461 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009462 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9464 PSA_KEY_DERIVATION_INPUT_INFO,
9465 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009466 }
Gilles Peskine59685592018-09-18 12:11:34 +02009467
Gilles Peskine449bd832023-01-11 14:50:10 +01009468 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9469 actual_output,
9470 expected_output1->len));
9471 ASSERT_COMPARE(actual_output, expected_output1->len,
9472 expected_output1->x, expected_output1->len);
9473 if (expected_output2->len != 0) {
9474 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9475 actual_output,
9476 expected_output2->len));
9477 ASSERT_COMPARE(actual_output, expected_output2->len,
9478 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009479 }
Gilles Peskine59685592018-09-18 12:11:34 +02009480
9481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 psa_key_derivation_abort(&operation);
9483 psa_destroy_key(our_key);
9484 PSA_DONE();
9485 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009486}
9487/* END_CASE */
9488
9489/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009490void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009491{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009492 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009493 unsigned char *output = NULL;
9494 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009495 size_t i;
9496 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009497
Gilles Peskine449bd832023-01-11 14:50:10 +01009498 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009499
Gilles Peskine449bd832023-01-11 14:50:10 +01009500 ASSERT_ALLOC(output, bytes);
9501 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009502
Gilles Peskine449bd832023-01-11 14:50:10 +01009503 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009504
Gilles Peskinea50d7392018-06-21 10:22:13 +02009505 /* Run several times, to ensure that every output byte will be
9506 * nonzero at least once with overwhelming probability
9507 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 for (run = 0; run < 10; run++) {
9509 if (bytes != 0) {
9510 memset(output, 0, bytes);
9511 }
9512 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009513
Gilles Peskine449bd832023-01-11 14:50:10 +01009514 for (i = 0; i < bytes; i++) {
9515 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009516 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009517 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009518 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009519 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009520
9521 /* Check that every byte was changed to nonzero at least once. This
9522 * validates that psa_generate_random is overwriting every byte of
9523 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 for (i = 0; i < bytes; i++) {
9525 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009526 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009527
9528exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009529 PSA_DONE();
9530 mbedtls_free(output);
9531 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009532}
9533/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009534
9535/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009536void generate_key(int type_arg,
9537 int bits_arg,
9538 int usage_arg,
9539 int alg_arg,
9540 int expected_status_arg,
9541 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009542{
Ronald Cron5425a212020-08-04 14:58:35 +02009543 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009544 psa_key_type_t type = type_arg;
9545 psa_key_usage_t usage = usage_arg;
9546 size_t bits = bits_arg;
9547 psa_algorithm_t alg = alg_arg;
9548 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009549 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009550 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009551
Gilles Peskine449bd832023-01-11 14:50:10 +01009552 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009553
Gilles Peskine449bd832023-01-11 14:50:10 +01009554 psa_set_key_usage_flags(&attributes, usage);
9555 psa_set_key_algorithm(&attributes, alg);
9556 psa_set_key_type(&attributes, type);
9557 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009558
9559 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009560 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009561
Gilles Peskine449bd832023-01-11 14:50:10 +01009562 if (is_large_key > 0) {
9563 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9564 }
9565 TEST_EQUAL(status, expected_status);
9566 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009567 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009568 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009569
9570 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009571 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9572 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9573 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009574
Gilles Peskine818ca122018-06-20 18:16:48 +02009575 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009576 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009577 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009578 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009579
9580exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009581 /*
9582 * Key attributes may have been returned by psa_get_key_attributes()
9583 * thus reset them as required.
9584 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009585 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009586
Gilles Peskine449bd832023-01-11 14:50:10 +01009587 psa_destroy_key(key);
9588 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009589}
9590/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009591
Ronald Cronee414c72021-03-18 18:50:08 +01009592/* 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 +01009593void generate_key_rsa(int bits_arg,
9594 data_t *e_arg,
9595 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009596{
Ronald Cron5425a212020-08-04 14:58:35 +02009597 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009598 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009599 size_t bits = bits_arg;
9600 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9601 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9602 psa_status_t expected_status = expected_status_arg;
9603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9604 uint8_t *exported = NULL;
9605 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009606 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009607 size_t exported_length = SIZE_MAX;
9608 uint8_t *e_read_buffer = NULL;
9609 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009610 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009611 size_t e_read_length = SIZE_MAX;
9612
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 if (e_arg->len == 0 ||
9614 (e_arg->len == 3 &&
9615 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009616 is_default_public_exponent = 1;
9617 e_read_size = 0;
9618 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009619 ASSERT_ALLOC(e_read_buffer, e_read_size);
9620 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009621
Gilles Peskine449bd832023-01-11 14:50:10 +01009622 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009623
Gilles Peskine449bd832023-01-11 14:50:10 +01009624 psa_set_key_usage_flags(&attributes, usage);
9625 psa_set_key_algorithm(&attributes, alg);
9626 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9627 e_arg->x, e_arg->len));
9628 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009629
9630 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9632 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009635
9636 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009637 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9638 TEST_EQUAL(psa_get_key_type(&attributes), type);
9639 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9640 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9641 e_read_buffer, e_read_size,
9642 &e_read_length));
9643 if (is_default_public_exponent) {
9644 TEST_EQUAL(e_read_length, 0);
9645 } else {
9646 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9647 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009648
9649 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009651 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009652 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009653
9654 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009655 PSA_ASSERT(psa_export_public_key(key,
9656 exported, exported_size,
9657 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009658 {
9659 uint8_t *p = exported;
9660 uint8_t *end = exported + exported_length;
9661 size_t len;
9662 /* RSAPublicKey ::= SEQUENCE {
9663 * modulus INTEGER, -- n
9664 * publicExponent INTEGER } -- e
9665 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009666 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9667 MBEDTLS_ASN1_SEQUENCE |
9668 MBEDTLS_ASN1_CONSTRUCTED));
9669 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9670 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9671 MBEDTLS_ASN1_INTEGER));
9672 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009673 ++p;
9674 --len;
9675 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009676 if (e_arg->len == 0) {
9677 TEST_EQUAL(len, 3);
9678 TEST_EQUAL(p[0], 1);
9679 TEST_EQUAL(p[1], 0);
9680 TEST_EQUAL(p[2], 1);
9681 } else {
9682 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009683 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009684 }
9685
9686exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009687 /*
9688 * Key attributes may have been returned by psa_get_key_attributes() or
9689 * set by psa_set_key_domain_parameters() thus reset them as required.
9690 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009691 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009692
Gilles Peskine449bd832023-01-11 14:50:10 +01009693 psa_destroy_key(key);
9694 PSA_DONE();
9695 mbedtls_free(e_read_buffer);
9696 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009697}
9698/* END_CASE */
9699
Darryl Greend49a4992018-06-18 17:27:26 +01009700/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009701void persistent_key_load_key_from_storage(data_t *data,
9702 int type_arg, int bits_arg,
9703 int usage_flags_arg, int alg_arg,
9704 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009705{
Gilles Peskine449bd832023-01-11 14:50:10 +01009706 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009708 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9709 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009710 psa_key_type_t type = type_arg;
9711 size_t bits = bits_arg;
9712 psa_key_usage_t usage_flags = usage_flags_arg;
9713 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009714 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009715 unsigned char *first_export = NULL;
9716 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009717 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009718 size_t first_exported_length;
9719 size_t second_exported_length;
9720
Gilles Peskine449bd832023-01-11 14:50:10 +01009721 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9722 ASSERT_ALLOC(first_export, export_size);
9723 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009724 }
Darryl Greend49a4992018-06-18 17:27:26 +01009725
Gilles Peskine449bd832023-01-11 14:50:10 +01009726 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009727
Gilles Peskine449bd832023-01-11 14:50:10 +01009728 psa_set_key_id(&attributes, key_id);
9729 psa_set_key_usage_flags(&attributes, usage_flags);
9730 psa_set_key_algorithm(&attributes, alg);
9731 psa_set_key_type(&attributes, type);
9732 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009733
Gilles Peskine449bd832023-01-11 14:50:10 +01009734 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009735 case IMPORT_KEY:
9736 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009737 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9738 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009739 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009740
Darryl Green0c6575a2018-11-07 16:05:30 +00009741 case GENERATE_KEY:
9742 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009743 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009744 break;
9745
9746 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009747#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009748 {
9749 /* Create base key */
9750 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9751 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9752 psa_set_key_usage_flags(&base_attributes,
9753 PSA_KEY_USAGE_DERIVE);
9754 psa_set_key_algorithm(&base_attributes, derive_alg);
9755 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9756 PSA_ASSERT(psa_import_key(&base_attributes,
9757 data->x, data->len,
9758 &base_key));
9759 /* Derive a key. */
9760 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9761 PSA_ASSERT(psa_key_derivation_input_key(
9762 &operation,
9763 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9764 PSA_ASSERT(psa_key_derivation_input_bytes(
9765 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9766 NULL, 0));
9767 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9768 &operation,
9769 &key));
9770 PSA_ASSERT(psa_key_derivation_abort(&operation));
9771 PSA_ASSERT(psa_destroy_key(base_key));
9772 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9773 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009774#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009775 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009776#endif
9777 break;
9778
9779 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009780 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009781 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009782 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009783 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009784
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009785 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009786 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9787 PSA_ASSERT(psa_export_key(key,
9788 first_export, export_size,
9789 &first_exported_length));
9790 if (generation_method == IMPORT_KEY) {
9791 ASSERT_COMPARE(data->x, data->len,
9792 first_export, first_exported_length);
9793 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009794 }
Darryl Greend49a4992018-06-18 17:27:26 +01009795
9796 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009797 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009798 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009799 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009800
Darryl Greend49a4992018-06-18 17:27:26 +01009801 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009802 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9803 TEST_ASSERT(mbedtls_svc_key_id_equal(
9804 psa_get_key_id(&attributes), key_id));
9805 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9806 PSA_KEY_LIFETIME_PERSISTENT);
9807 TEST_EQUAL(psa_get_key_type(&attributes), type);
9808 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9809 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9810 mbedtls_test_update_key_usage_flags(usage_flags));
9811 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009812
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009813 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009814 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9815 PSA_ASSERT(psa_export_key(key,
9816 second_export, export_size,
9817 &second_exported_length));
9818 ASSERT_COMPARE(first_export, first_exported_length,
9819 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009820 }
9821
9822 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009823 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009824 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009825 }
Darryl Greend49a4992018-06-18 17:27:26 +01009826
9827exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009828 /*
9829 * Key attributes may have been returned by psa_get_key_attributes()
9830 * thus reset them as required.
9831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009832 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009833
Gilles Peskine449bd832023-01-11 14:50:10 +01009834 mbedtls_free(first_export);
9835 mbedtls_free(second_export);
9836 psa_key_derivation_abort(&operation);
9837 psa_destroy_key(base_key);
9838 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009839 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009840}
9841/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009842
Neil Armstronga557cb82022-06-10 08:58:32 +02009843/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009844void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9845 int primitive_arg, int hash_arg, int role_arg,
9846 int test_input, data_t *pw_data,
9847 int inj_err_type_arg,
9848 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009849{
9850 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9851 psa_pake_operation_t operation = psa_pake_operation_init();
9852 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009853 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009854 psa_key_type_t key_type_pw = key_type_pw_arg;
9855 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009856 psa_algorithm_t hash_alg = hash_arg;
9857 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009860 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9861 psa_status_t expected_error = expected_error_arg;
9862 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009863 unsigned char *output_buffer = NULL;
9864 size_t output_len = 0;
9865
Gilles Peskine449bd832023-01-11 14:50:10 +01009866 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009867
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009868 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009869 PSA_PAKE_STEP_KEY_SHARE);
9870 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009871
Gilles Peskine449bd832023-01-11 14:50:10 +01009872 if (pw_data->len > 0) {
9873 psa_set_key_usage_flags(&attributes, key_usage_pw);
9874 psa_set_key_algorithm(&attributes, alg);
9875 psa_set_key_type(&attributes, key_type_pw);
9876 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9877 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009878 }
9879
Gilles Peskine449bd832023-01-11 14:50:10 +01009880 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9881 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9882 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009883
Gilles Peskine449bd832023-01-11 14:50:10 +01009884 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009885
Gilles Peskine449bd832023-01-11 14:50:10 +01009886 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9887 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9888 expected_error);
9889 PSA_ASSERT(psa_pake_abort(&operation));
9890 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9891 expected_error);
9892 PSA_ASSERT(psa_pake_abort(&operation));
9893 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9894 expected_error);
9895 PSA_ASSERT(psa_pake_abort(&operation));
9896 TEST_EQUAL(psa_pake_set_role(&operation, role),
9897 expected_error);
9898 PSA_ASSERT(psa_pake_abort(&operation));
9899 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9900 NULL, 0, NULL),
9901 expected_error);
9902 PSA_ASSERT(psa_pake_abort(&operation));
9903 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9904 expected_error);
9905 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009906 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009907 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009908
Gilles Peskine449bd832023-01-11 14:50:10 +01009909 status = psa_pake_setup(&operation, &cipher_suite);
9910 if (status != PSA_SUCCESS) {
9911 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009912 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009913 }
9914
Gilles Peskine449bd832023-01-11 14:50:10 +01009915 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9916 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9917 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009918 goto exit;
9919 }
9920
Gilles Peskine449bd832023-01-11 14:50:10 +01009921 status = psa_pake_set_role(&operation, role);
9922 if (status != PSA_SUCCESS) {
9923 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009924 goto exit;
9925 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009926
Gilles Peskine449bd832023-01-11 14:50:10 +01009927 if (pw_data->len > 0) {
9928 status = psa_pake_set_password_key(&operation, key);
9929 if (status != PSA_SUCCESS) {
9930 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009931 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009932 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009933 }
9934
Gilles Peskine449bd832023-01-11 14:50:10 +01009935 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9936 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9937 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009938 goto exit;
9939 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009940
Gilles Peskine449bd832023-01-11 14:50:10 +01009941 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9942 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9943 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009944 goto exit;
9945 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009946
Gilles Peskine449bd832023-01-11 14:50:10 +01009947 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009948 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009949 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9950 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009951 goto exit;
9952 }
9953
Gilles Peskine449bd832023-01-11 14:50:10 +01009954 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009955 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009956 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9957 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009958 goto exit;
9959 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009960
Gilles Peskine449bd832023-01-11 14:50:10 +01009961 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9962 PSA_PAKE_STEP_KEY_SHARE);
9963 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9964 PSA_PAKE_STEP_ZK_PUBLIC);
9965 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9966 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009967
Gilles Peskine449bd832023-01-11 14:50:10 +01009968 if (test_input) {
9969 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9970 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9971 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009972 goto exit;
9973 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009974
Gilles Peskine449bd832023-01-11 14:50:10 +01009975 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9976 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9977 output_buffer, size_zk_proof),
9978 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009979 goto exit;
9980 }
9981
Gilles Peskine449bd832023-01-11 14:50:10 +01009982 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9983 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9984 output_buffer, size_zk_proof),
9985 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009986 goto exit;
9987 }
9988
Gilles Peskine449bd832023-01-11 14:50:10 +01009989 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9990 output_buffer, size_key_share);
9991 if (status != PSA_SUCCESS) {
9992 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009993 goto exit;
9994 }
9995
Gilles Peskine449bd832023-01-11 14:50:10 +01009996 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9997 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9998 output_buffer, size_zk_public + 1),
9999 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010000 goto exit;
10001 }
10002
Gilles Peskine449bd832023-01-11 14:50:10 +010010003 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010004 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010005 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10006 output_buffer, size_zk_public + 1);
10007 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10008 output_buffer, size_zk_public),
10009 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010010 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010011 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010012 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010013 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10014 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10015 NULL, 0, NULL),
10016 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010017 goto exit;
10018 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010019
Gilles Peskine449bd832023-01-11 14:50:10 +010010020 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10021 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10022 output_buffer, buf_size, &output_len),
10023 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010024 goto exit;
10025 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010026
Gilles Peskine449bd832023-01-11 14:50:10 +010010027 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10028 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10029 output_buffer, buf_size, &output_len),
10030 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010031 goto exit;
10032 }
10033
Gilles Peskine449bd832023-01-11 14:50:10 +010010034 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10035 output_buffer, buf_size, &output_len);
10036 if (status != PSA_SUCCESS) {
10037 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010038 goto exit;
10039 }
10040
Gilles Peskine449bd832023-01-11 14:50:10 +010010041 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010042
Gilles Peskine449bd832023-01-11 14:50:10 +010010043 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10044 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10045 output_buffer, size_zk_public - 1, &output_len),
10046 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010047 goto exit;
10048 }
10049
Gilles Peskine449bd832023-01-11 14:50:10 +010010050 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010051 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010052 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10053 output_buffer, size_zk_public - 1, &output_len);
10054 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10055 output_buffer, buf_size, &output_len),
10056 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010057 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010058 }
10059 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010060
10061exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010062 PSA_ASSERT(psa_destroy_key(key));
10063 PSA_ASSERT(psa_pake_abort(&operation));
10064 mbedtls_free(output_buffer);
10065 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010066}
10067/* END_CASE */
10068
Neil Armstronga557cb82022-06-10 08:58:32 +020010069/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010070void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10071 int client_input_first, int inject_error,
10072 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010073{
10074 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10075 psa_pake_operation_t server = psa_pake_operation_init();
10076 psa_pake_operation_t client = psa_pake_operation_init();
10077 psa_algorithm_t alg = alg_arg;
10078 psa_algorithm_t hash_alg = hash_arg;
10079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10080 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10081
Gilles Peskine449bd832023-01-11 14:50:10 +010010082 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010083
Gilles Peskine449bd832023-01-11 14:50:10 +010010084 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10085 psa_set_key_algorithm(&attributes, alg);
10086 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10087 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10088 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010089
Gilles Peskine449bd832023-01-11 14:50:10 +010010090 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10091 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10092 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010093
10094
Gilles Peskine449bd832023-01-11 14:50:10 +010010095 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10096 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010097
Gilles Peskine449bd832023-01-11 14:50:10 +010010098 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10099 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010100
Gilles Peskine449bd832023-01-11 14:50:10 +010010101 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10102 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010103
Gilles Peskine449bd832023-01-11 14:50:10 +010010104 ecjpake_do_round(alg, primitive_arg, &server, &client,
10105 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010106
Gilles Peskine449bd832023-01-11 14:50:10 +010010107 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010108 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010109 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010110
Gilles Peskine449bd832023-01-11 14:50:10 +010010111 ecjpake_do_round(alg, primitive_arg, &server, &client,
10112 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010113
10114exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010115 psa_destroy_key(key);
10116 psa_pake_abort(&server);
10117 psa_pake_abort(&client);
10118 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010119}
10120/* END_CASE */
10121
10122/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010123void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10124 int derive_alg_arg, data_t *pw_data,
10125 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010126{
10127 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10128 psa_pake_operation_t server = psa_pake_operation_init();
10129 psa_pake_operation_t client = psa_pake_operation_init();
10130 psa_algorithm_t alg = alg_arg;
10131 psa_algorithm_t hash_alg = hash_arg;
10132 psa_algorithm_t derive_alg = derive_alg_arg;
10133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10135 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010136 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010137 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010138 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010139 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010140
Gilles Peskine449bd832023-01-11 14:50:10 +010010141 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010142
Gilles Peskine449bd832023-01-11 14:50:10 +010010143 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10144 psa_set_key_algorithm(&attributes, alg);
10145 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10146 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10147 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010148
Gilles Peskine449bd832023-01-11 14:50:10 +010010149 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10150 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10151 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010152
Neil Armstrong1e855602022-06-15 11:32:11 +020010153 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010154 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10155 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010156
Gilles Peskine449bd832023-01-11 14:50:10 +010010157 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10158 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10159 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10160 PSA_KEY_DERIVATION_INPUT_SEED,
10161 (const uint8_t *) "", 0));
10162 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10163 PSA_KEY_DERIVATION_INPUT_SEED,
10164 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010165 }
10166
Gilles Peskine449bd832023-01-11 14:50:10 +010010167 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10168 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010169
Gilles Peskine449bd832023-01-11 14:50:10 +010010170 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10171 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010172
Gilles Peskine449bd832023-01-11 14:50:10 +010010173 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10174 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010175
Gilles Peskine449bd832023-01-11 14:50:10 +010010176 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10177 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10178 PSA_ERROR_BAD_STATE);
10179 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10180 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010181 goto exit;
10182 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010183
Neil Armstrongf983caf2022-06-15 15:27:48 +020010184 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010185 ecjpake_do_round(alg, primitive_arg, &server, &client,
10186 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010187
Gilles Peskine449bd832023-01-11 14:50:10 +010010188 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10189 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10190 PSA_ERROR_BAD_STATE);
10191 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10192 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010193 goto exit;
10194 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010195
Neil Armstrongf983caf2022-06-15 15:27:48 +020010196 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010197 ecjpake_do_round(alg, primitive_arg, &server, &client,
10198 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010199
Gilles Peskine449bd832023-01-11 14:50:10 +010010200 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10201 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010202
10203exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010204 psa_key_derivation_abort(&server_derive);
10205 psa_key_derivation_abort(&client_derive);
10206 psa_destroy_key(key);
10207 psa_pake_abort(&server);
10208 psa_pake_abort(&client);
10209 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010210}
10211/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010212
10213/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010214void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010215{
10216 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10217 const size_t bits = 256;
10218 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010219 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010220 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010221 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010222
10223 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10224 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010225 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10226 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10227 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10228 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010229 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010230 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10231 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010232
10233 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010234 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10235 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10236 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10237 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10238 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10239 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010240
10241 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010242 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10243 PSA_PAKE_OUTPUT_MAX_SIZE);
10244 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10245 PSA_PAKE_OUTPUT_MAX_SIZE);
10246 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10247 PSA_PAKE_OUTPUT_MAX_SIZE);
10248 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10249 PSA_PAKE_INPUT_MAX_SIZE);
10250 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10251 PSA_PAKE_INPUT_MAX_SIZE);
10252 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10253 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010254}
10255/* END_CASE */