blob: 144c2e54c0684b474c44b2e51bd1c72f9a2dedb2 [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
Valerio Settibf999cb2023-12-28 17:48:13 +01009#include "mbedtls/psa_util.h"
10
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020011/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
12 * uses mbedtls_ctr_drbg internally. */
13#include "mbedtls/ctr_drbg.h"
14
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020016#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020017
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +010018#include "psa_crypto_core.h"
19
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
Ryan3a1b7862024-03-01 17:24:04 +000030#if defined(MBEDTLS_THREADING_PTHREAD)
31#include "mbedtls/threading.h"
32#endif
33
Gilles Peskine4023c012021-05-27 13:21:20 +020034/* If this comes up, it's a bug in the test code or in the test data. */
35#define UNUSED 0xdeadbeef
36
Dave Rodgman647791d2021-06-23 12:49:59 +010037/* Assert that an operation is (not) active.
38 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010039#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
40#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010041
42/** An invalid export length that will never be set by psa_export_key(). */
43static const size_t INVALID_EXPORT_LENGTH = ~0U;
44
Gilles Peskinea7aa4422018-08-14 15:17:54 +020045/** Test if a buffer contains a constant byte value.
46 *
47 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048 *
49 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020050 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020051 * \param size Size of the buffer in bytes.
52 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 * \return 1 if the buffer is all-bits-zero.
54 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020055 */
Gilles Peskine449bd832023-01-11 14:50:10 +010056static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020057{
58 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010059 for (i = 0; i < size; i++) {
60 if (((unsigned char *) buffer)[i] != c) {
61 return 0;
62 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020063 }
Gilles Peskine449bd832023-01-11 14:50:10 +010064 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020065}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010066#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020067/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010068static int asn1_write_10x(unsigned char **p,
69 unsigned char *start,
70 size_t bits,
71 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020072{
73 int ret;
74 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if (bits == 0) {
76 return MBEDTLS_ERR_ASN1_INVALID_DATA;
77 }
78 if (bits <= 8 && x >= 1 << (bits - 1)) {
79 return MBEDTLS_ERR_ASN1_INVALID_DATA;
80 }
81 if (*p < start || *p - start < (ptrdiff_t) len) {
82 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
83 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +020084 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +010085 (*p)[len-1] = x;
86 if (bits % 8 == 0) {
87 (*p)[1] |= 1;
88 } else {
89 (*p)[0] |= 1 << (bits % 8);
90 }
91 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
92 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
93 MBEDTLS_ASN1_INTEGER));
94 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +020095}
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int construct_fake_rsa_key(unsigned char *buffer,
98 size_t buffer_size,
99 unsigned char **p,
100 size_t bits,
101 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200102{
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200104 int ret;
105 int len = 0;
106 /* Construct something that looks like a DER encoding of
107 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
108 * RSAPrivateKey ::= SEQUENCE {
109 * version Version,
110 * modulus INTEGER, -- n
111 * publicExponent INTEGER, -- e
112 * privateExponent INTEGER, -- d
113 * prime1 INTEGER, -- p
114 * prime2 INTEGER, -- q
115 * exponent1 INTEGER, -- d mod (p-1)
116 * exponent2 INTEGER, -- d mod (q-1)
117 * coefficient INTEGER, -- (inverse of q) mod p
118 * otherPrimeInfos OtherPrimeInfos OPTIONAL
119 * }
120 * Or, for a public key, the same structure with only
121 * version, modulus and publicExponent.
122 */
123 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (keypair) {
125 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
126 asn1_write_10x(p, buffer, half_bits, 1));
127 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
128 asn1_write_10x(p, buffer, half_bits, 1));
129 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
130 asn1_write_10x(p, buffer, half_bits, 1));
131 MBEDTLS_ASN1_CHK_ADD(len, /* q */
132 asn1_write_10x(p, buffer, half_bits, 1));
133 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
134 asn1_write_10x(p, buffer, half_bits, 3));
135 MBEDTLS_ASN1_CHK_ADD(len, /* d */
136 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200137 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
139 asn1_write_10x(p, buffer, 17, 1));
140 MBEDTLS_ASN1_CHK_ADD(len, /* n */
141 asn1_write_10x(p, buffer, bits, 1));
142 if (keypair) {
143 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
144 mbedtls_asn1_write_int(p, buffer, 0));
145 }
146 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200147 {
148 const unsigned char tag =
149 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200151 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200153}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100154#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200155
Michael Schusterb1e33fb2024-06-04 02:30:22 +0200156static int exercise_mac_setup(psa_key_type_t key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 const unsigned char *key_bytes,
158 size_t key_length,
159 psa_algorithm_t alg,
160 psa_mac_operation_t *operation,
161 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100162{
Ronald Cron5425a212020-08-04 14:58:35 +0200163 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
167 psa_set_key_algorithm(&attributes, alg);
168 psa_set_key_type(&attributes, key_type);
169 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100172 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100174 /* If setup failed, reproduce the failure, so that the caller can
175 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (*status != PSA_SUCCESS) {
177 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178 }
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 psa_destroy_key(key);
181 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182
183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 psa_destroy_key(key);
185 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100186}
187
Michael Schusterb1e33fb2024-06-04 02:30:22 +0200188static int exercise_cipher_setup(psa_key_type_t key_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 const unsigned char *key_bytes,
190 size_t key_length,
191 psa_algorithm_t alg,
192 psa_cipher_operation_t *operation,
193 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100194{
Ronald Cron5425a212020-08-04 14:58:35 +0200195 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
199 psa_set_key_algorithm(&attributes, alg);
200 psa_set_key_type(&attributes, key_type);
201 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100204 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100206 /* If setup failed, reproduce the failure, so that the caller can
207 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (*status != PSA_SUCCESS) {
209 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
210 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100211 }
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 psa_destroy_key(key);
214 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215
216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 psa_destroy_key(key);
218 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100219}
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222{
223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 uint8_t buffer[1];
226 size_t length;
227 int ok = 0;
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 psa_set_key_id(&attributes, key_id);
230 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
231 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
232 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
233 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
234 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200235 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200237 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
239 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
240 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
241 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
242 TEST_EQUAL(psa_get_key_type(&attributes), 0);
243 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
246 PSA_ERROR_INVALID_HANDLE);
247 TEST_EQUAL(psa_export_public_key(key,
248 buffer, sizeof(buffer), &length),
249 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200250
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200251 ok = 1;
252
253exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100254 /*
255 * Key attributes may have been returned by psa_get_key_attributes()
256 * thus reset them as required.
257 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200261}
262
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200263/* Assert that a key isn't reported as having a slot number. */
264#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100265#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200266 do \
267 { \
268 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 TEST_EQUAL(psa_get_key_slot_number( \
270 attributes, \
271 &ASSERT_NO_SLOT_NUMBER_slot_number), \
272 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200273 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200275#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276#define ASSERT_NO_SLOT_NUMBER(attributes) \
277 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200278#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
279
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530280#define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */
281
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100282/* An overapproximation of the amount of storage needed for a key of the
283 * given type and with the given content. The API doesn't make it easy
284 * to find a good value for the size. The current implementation doesn't
285 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286#define KEY_BITS_FROM_DATA(type, data) \
287 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100288
Darryl Green0c6575a2018-11-07 16:05:30 +0000289typedef enum {
290 IMPORT_KEY = 0,
291 GENERATE_KEY = 1,
292 DERIVE_KEY = 2
293} generate_method;
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100296 DO_NOT_SET_LENGTHS = 0,
297 SET_LENGTHS_BEFORE_NONCE = 1,
298 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100299} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100302 USE_NULL_TAG = 0,
303 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100304} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100305
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530306
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100307/*!
308 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100309 * \param key_type_arg Type of key passed in
310 * \param key_data The encryption / decryption key data
311 * \param alg_arg The type of algorithm used
312 * \param nonce Nonce data
313 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100314 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100315 * feed additional data in to be encrypted /
316 * decrypted. If -1, no chunking.
317 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100318 * \param data_part_len_arg If not -1, the length of chunks to feed
319 * the data in to be encrypted / decrypted. If
320 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100321 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100322 * expected here, this controls whether or not
323 * to set lengths, and in what order with
324 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100326 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100327 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100328 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100329 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100330 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
332 int alg_arg,
333 data_t *nonce,
334 data_t *additional_data,
335 int ad_part_len_arg,
336 data_t *input_data,
337 int data_part_len_arg,
338 set_lengths_method_t set_lengths_method,
339 data_t *expected_output,
340 int is_encrypt,
341 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100342{
343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
344 psa_key_type_t key_type = key_type_arg;
345 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100346 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100347 unsigned char *output_data = NULL;
348 unsigned char *part_data = NULL;
349 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100350 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100351 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100352 size_t output_size = 0;
353 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100354 size_t output_length = 0;
355 size_t key_bits = 0;
356 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100357 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 size_t part_length = 0;
359 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100361 size_t ad_part_len = 0;
362 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100363 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
365 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
366
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100367 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100368 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (is_encrypt) {
373 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
374 } else {
375 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100377
378 psa_set_key_algorithm(&attributes, alg);
379 psa_set_key_type(&attributes, key_type);
380
381 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
382 &key));
383
384 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
385 key_bits = psa_get_key_bits(&attributes);
386
387 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
388
389 if (is_encrypt) {
390 /* Tag gets written at end of buffer. */
391 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
392 (input_data->len +
393 tag_length));
394 data_true_size = input_data->len;
395 } else {
396 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
397 (input_data->len -
398 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100399
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 /* Do not want to attempt to decrypt tag. */
401 data_true_size = input_data->len - tag_length;
402 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100403
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100404 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (is_encrypt) {
407 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
408 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
409 } else {
410 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
411 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100412 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100414 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (is_encrypt) {
417 status = psa_aead_encrypt_setup(&operation, key, alg);
418 } else {
419 status = psa_aead_decrypt_setup(&operation, key, alg);
420 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100421
422 /* If the operation is not supported, just skip and not fail in case the
423 * encryption involves a common limitation of cryptography hardwares and
424 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (status == PSA_ERROR_NOT_SUPPORTED) {
426 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
427 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 }
429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
433 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
434 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
435 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
436 data_true_size));
437 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
438 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
439 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
442 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100443 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100447 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 part_offset += part_length, part_count++) {
452 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100453 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100455 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100457 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 }
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 PSA_ASSERT(psa_aead_update_ad(&operation,
461 additional_data->x + part_offset,
462 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100463
Paul Elliottd3f82412021-06-16 16:52:21 +0100464 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100466 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
468 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100469 }
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100472 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 data_part_len = (size_t) data_part_len_arg;
474 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
475 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100476
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100477 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100480 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 part_offset += part_length, part_count++) {
482 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100483 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 } else if ((data_true_size - part_offset) < data_part_len) {
485 part_length = (data_true_size - part_offset);
486 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100487 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 PSA_ASSERT(psa_aead_update(&operation,
491 (input_data->x + part_offset),
492 part_length, part_data,
493 part_data_size,
494 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (output_data && output_part_length) {
497 memcpy((output_data + output_length), part_data,
498 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100499 }
500
Paul Elliottd3f82412021-06-16 16:52:21 +0100501 output_length += output_part_length;
502 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100504 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
506 data_true_size, output_data,
507 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100508 }
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (is_encrypt) {
511 PSA_ASSERT(psa_aead_finish(&operation, final_data,
512 final_output_size,
513 &output_part_length,
514 tag_buffer, tag_length,
515 &tag_size));
516 } else {
517 PSA_ASSERT(psa_aead_verify(&operation, final_data,
518 final_output_size,
519 &output_part_length,
520 (input_data->x + data_true_size),
521 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 }
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (output_data && output_part_length) {
525 memcpy((output_data + output_length), final_data,
526 output_part_length);
527 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100528
529 output_length += output_part_length;
530
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100531
532 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
533 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (is_encrypt) {
535 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (output_data && tag_length) {
538 memcpy((output_data + output_length), tag_buffer,
539 tag_length);
540 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100541
542 output_length += tag_length;
543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 TEST_EQUAL(output_length,
545 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
546 input_data->len));
547 TEST_LE_U(output_length,
548 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
549 } else {
550 TEST_EQUAL(output_length,
551 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
552 input_data->len));
553 TEST_LE_U(output_length,
554 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100555 }
556
Paul Elliottd3f82412021-06-16 16:52:21 +0100557
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100558 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100559 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100560
Paul Elliottd3f82412021-06-16 16:52:21 +0100561
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100562 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100563
564exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 psa_destroy_key(key);
566 psa_aead_abort(&operation);
567 mbedtls_free(output_data);
568 mbedtls_free(part_data);
569 mbedtls_free(final_data);
570 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100573}
574
Neil Armstrong4766f992022-02-28 16:23:59 +0100575/*!
576 * \brief Internal Function for MAC multipart tests.
577 * \param key_type_arg Type of key passed in
578 * \param key_data The encryption / decryption key data
579 * \param alg_arg The type of algorithm used
580 * \param input_data Data to encrypt / decrypt
581 * \param data_part_len_arg If not -1, the length of chunks to feed
582 * the data in to be encrypted / decrypted. If
583 * -1, no chunking
584 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000585 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100586 * \param do_zero_parts If non-zero, interleave zero length chunks
587 * with normal length chunks.
588 * \return int Zero on failure, non-zero on success.
589 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
591 int alg_arg,
592 data_t *input_data,
593 int data_part_len_arg,
594 data_t *expected_output,
595 int is_verify,
596 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100597{
598 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
599 psa_key_type_t key_type = key_type_arg;
600 psa_algorithm_t alg = alg_arg;
601 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
602 unsigned char mac[PSA_MAC_MAX_SIZE];
603 size_t part_offset = 0;
604 size_t part_length = 0;
605 size_t data_part_len = 0;
606 size_t mac_len = 0;
607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
608 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
609
610 int test_ok = 0;
611 size_t part_count = 0;
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (is_verify) {
616 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
617 } else {
618 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
619 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 psa_set_key_algorithm(&attributes, alg);
622 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
625 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (is_verify) {
628 status = psa_mac_verify_setup(&operation, key, alg);
629 } else {
630 status = psa_mac_sign_setup(&operation, key, alg);
631 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100636 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100640 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 part_offset += part_length, part_count++) {
642 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100643 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 } else if ((input_data->len - part_offset) < data_part_len) {
645 part_length = (input_data->len - part_offset);
646 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100647 part_length = data_part_len;
648 }
649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 PSA_ASSERT(psa_mac_update(&operation,
651 (input_data->x + part_offset),
652 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100653 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100655 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
657 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100658 }
659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (is_verify) {
661 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
662 expected_output->len));
663 } else {
664 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
665 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100666
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100667 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100668 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100669 }
670
671 test_ok = 1;
672
673exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 psa_destroy_key(key);
675 psa_mac_abort(&operation);
676 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100679}
680
Neil Armstrong75673ab2022-06-15 17:39:01 +0200681#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100682static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
683 psa_pake_operation_t *server,
684 psa_pake_operation_t *client,
685 int client_input_first,
686 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200687{
688 unsigned char *buffer0 = NULL, *buffer1 = NULL;
689 size_t buffer_length = (
690 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
691 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
692 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200693 /* The output should be exactly this size according to the spec */
694 const size_t expected_size_key_share =
695 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
696 /* The output should be exactly this size according to the spec */
697 const size_t expected_size_zk_public =
698 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
699 /* The output can be smaller: the spec allows stripping leading zeroes */
700 const size_t max_expected_size_zk_proof =
701 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200702 size_t buffer0_off = 0;
703 size_t buffer1_off = 0;
704 size_t s_g1_len, s_g2_len, s_a_len;
705 size_t s_g1_off, s_g2_off, s_a_off;
706 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
707 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
708 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
709 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
710 size_t c_g1_len, c_g2_len, c_a_len;
711 size_t c_g1_off, c_g2_off, c_a_off;
712 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
713 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
714 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
715 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
716 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200717 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200718
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100719 TEST_CALLOC(buffer0, buffer_length);
720 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200723 case 1:
724 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
726 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000727 buffer_length - buffer0_off, &s_g1_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200729 s_g1_off = buffer0_off;
730 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
732 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000733 buffer_length - buffer0_off, &s_x1_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200735 s_x1_pk_off = buffer0_off;
736 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
738 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000739 buffer_length - buffer0_off, &s_x1_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200741 s_x1_pr_off = buffer0_off;
742 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
744 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000745 buffer_length - buffer0_off, &s_g2_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200747 s_g2_off = buffer0_off;
748 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
750 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000751 buffer_length - buffer0_off, &s_x2_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200753 s_x2_pk_off = buffer0_off;
754 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
756 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000757 buffer_length - buffer0_off, &s_x2_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200759 s_x2_pr_off = buffer0_off;
760 buffer0_off += s_x2_pr_len;
761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500763 buffer0[s_x1_pr_off + 8] ^= 1;
764 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200765 expected_status = PSA_ERROR_DATA_INVALID;
766 }
767
Neil Armstrong51009d72022-09-05 17:59:54 +0200768 /*
769 * When injecting errors in inputs, the implementation is
770 * free to detect it right away of with a delay.
771 * This permits delaying the error until the end of the input
772 * sequence, if no error appears then, this will be treated
773 * as an error.
774 */
775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200777 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
779 buffer0 + s_g1_off, s_g1_len);
780 if (inject_error == 1 && status != PSA_SUCCESS) {
781 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200782 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 } else {
784 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200785 }
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
788 buffer0 + s_x1_pk_off,
789 s_x1_pk_len);
790 if (inject_error == 1 && status != PSA_SUCCESS) {
791 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200792 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 } else {
794 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200795 }
796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
798 buffer0 + s_x1_pr_off,
799 s_x1_pr_len);
800 if (inject_error == 1 && status != PSA_SUCCESS) {
801 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200802 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 } else {
804 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200805 }
806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
808 buffer0 + s_g2_off,
809 s_g2_len);
810 if (inject_error == 1 && status != PSA_SUCCESS) {
811 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200812 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 } else {
814 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200815 }
816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
818 buffer0 + s_x2_pk_off,
819 s_x2_pk_len);
820 if (inject_error == 1 && status != PSA_SUCCESS) {
821 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200822 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 } else {
824 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200825 }
826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
828 buffer0 + s_x2_pr_off,
829 s_x2_pr_len);
830 if (inject_error == 1 && status != PSA_SUCCESS) {
831 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200832 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 } else {
834 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200835 }
836
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200837 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (inject_error == 1) {
839 TEST_ASSERT(
840 !"One of the last psa_pake_input() calls should have returned the expected error.");
841 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200842 }
843
844 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
846 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000847 buffer_length - buffer1_off, &c_g1_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200849 c_g1_off = buffer1_off;
850 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
852 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000853 buffer_length - buffer1_off, &c_x1_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200855 c_x1_pk_off = buffer1_off;
856 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
858 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000859 buffer_length - buffer1_off, &c_x1_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200861 c_x1_pr_off = buffer1_off;
862 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
864 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000865 buffer_length - buffer1_off, &c_g2_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200867 c_g2_off = buffer1_off;
868 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
870 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000871 buffer_length - buffer1_off, &c_x2_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200873 c_x2_pk_off = buffer1_off;
874 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
876 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +0000877 buffer_length - buffer1_off, &c_x2_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200879 c_x2_pr_off = buffer1_off;
880 buffer1_off += c_x2_pr_len;
881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200883 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
885 buffer0 + s_g1_off, s_g1_len);
886 if (inject_error == 1 && status != PSA_SUCCESS) {
887 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200888 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 } else {
890 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200891 }
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
894 buffer0 + s_x1_pk_off,
895 s_x1_pk_len);
896 if (inject_error == 1 && status != PSA_SUCCESS) {
897 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200898 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 } else {
900 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200901 }
902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
904 buffer0 + s_x1_pr_off,
905 s_x1_pr_len);
906 if (inject_error == 1 && status != PSA_SUCCESS) {
907 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200908 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 } else {
910 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200911 }
912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
914 buffer0 + s_g2_off,
915 s_g2_len);
916 if (inject_error == 1 && status != PSA_SUCCESS) {
917 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200918 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 } else {
920 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200921 }
922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
924 buffer0 + s_x2_pk_off,
925 s_x2_pk_len);
926 if (inject_error == 1 && status != PSA_SUCCESS) {
927 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200928 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 } else {
930 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200931 }
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
934 buffer0 + s_x2_pr_off,
935 s_x2_pr_len);
936 if (inject_error == 1 && status != PSA_SUCCESS) {
937 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200938 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 } else {
940 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200941 }
942
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200943 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (inject_error == 1) {
945 TEST_ASSERT(
946 !"One of the last psa_pake_input() calls should have returned the expected error.");
947 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200948 }
949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500951 buffer1[c_x1_pr_off + 12] ^= 1;
952 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200953 expected_status = PSA_ERROR_DATA_INVALID;
954 }
955
956 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
958 buffer1 + c_g1_off, c_g1_len);
959 if (inject_error == 2 && 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(server, PSA_PAKE_STEP_ZK_PUBLIC,
967 buffer1 + c_x1_pk_off, c_x1_pk_len);
968 if (inject_error == 2 && status != PSA_SUCCESS) {
969 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200970 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 } else {
972 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200973 }
974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
976 buffer1 + c_x1_pr_off, c_x1_pr_len);
977 if (inject_error == 2 && status != PSA_SUCCESS) {
978 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200979 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 } else {
981 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200982 }
983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
985 buffer1 + c_g2_off, c_g2_len);
986 if (inject_error == 2 && status != PSA_SUCCESS) {
987 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200988 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 } else {
990 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200991 }
992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
994 buffer1 + c_x2_pk_off, c_x2_pk_len);
995 if (inject_error == 2 && status != PSA_SUCCESS) {
996 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200997 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 } else {
999 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001000 }
1001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1003 buffer1 + c_x2_pr_off, c_x2_pr_len);
1004 if (inject_error == 2 && status != PSA_SUCCESS) {
1005 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001006 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 } else {
1008 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001009 }
1010
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001011 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if (inject_error == 2) {
1013 TEST_ASSERT(
1014 !"One of the last psa_pake_input() calls should have returned the expected error.");
1015 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001016
1017 break;
1018
1019 case 2:
1020 /* Server second round Output */
1021 buffer0_off = 0;
1022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1024 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001025 buffer_length - buffer0_off, &s_a_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001027 s_a_off = buffer0_off;
1028 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1030 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001031 buffer_length - buffer0_off, &s_x2s_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001033 s_x2s_pk_off = buffer0_off;
1034 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1036 buffer0 + buffer0_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001037 buffer_length - buffer0_off, &s_x2s_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001039 s_x2s_pr_off = buffer0_off;
1040 buffer0_off += s_x2s_pr_len;
1041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001043 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001044 expected_status = PSA_ERROR_DATA_INVALID;
1045 }
1046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001048 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1050 buffer0 + s_a_off, s_a_len);
1051 if (inject_error == 3 && status != PSA_SUCCESS) {
1052 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001053 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 } else {
1055 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001056 }
1057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1059 buffer0 + s_x2s_pk_off,
1060 s_x2s_pk_len);
1061 if (inject_error == 3 && status != PSA_SUCCESS) {
1062 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001063 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 } else {
1065 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001066 }
1067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1069 buffer0 + s_x2s_pr_off,
1070 s_x2s_pr_len);
1071 if (inject_error == 3 && status != PSA_SUCCESS) {
1072 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001073 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 } else {
1075 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001076 }
1077
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001078 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if (inject_error == 3) {
1080 TEST_ASSERT(
1081 !"One of the last psa_pake_input() calls should have returned the expected error.");
1082 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001083 }
1084
1085 /* Client second round Output */
1086 buffer1_off = 0;
1087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1089 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001090 buffer_length - buffer1_off, &c_a_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001092 c_a_off = buffer1_off;
1093 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1095 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001096 buffer_length - buffer1_off, &c_x2s_pk_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001098 c_x2s_pk_off = buffer1_off;
1099 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1101 buffer1 + buffer1_off,
David Horstmann433a58c2024-01-25 16:29:26 +00001102 buffer_length - buffer1_off, &c_x2s_pr_len));
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001104 c_x2s_pr_off = buffer1_off;
1105 buffer1_off += c_x2s_pr_len;
1106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001108 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1110 buffer0 + s_a_off, s_a_len);
1111 if (inject_error == 3 && status != PSA_SUCCESS) {
1112 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001113 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 } else {
1115 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001116 }
1117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1119 buffer0 + s_x2s_pk_off,
1120 s_x2s_pk_len);
1121 if (inject_error == 3 && status != PSA_SUCCESS) {
1122 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001123 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 } else {
1125 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001126 }
1127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1129 buffer0 + s_x2s_pr_off,
1130 s_x2s_pr_len);
1131 if (inject_error == 3 && status != PSA_SUCCESS) {
1132 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001133 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 } else {
1135 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001136 }
1137
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001138 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if (inject_error == 3) {
1140 TEST_ASSERT(
1141 !"One of the last psa_pake_input() calls should have returned the expected error.");
1142 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001143 }
1144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001146 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001147 expected_status = PSA_ERROR_DATA_INVALID;
1148 }
1149
1150 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1152 buffer1 + c_a_off, c_a_len);
1153 if (inject_error == 4 && status != PSA_SUCCESS) {
1154 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001155 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 } else {
1157 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001158 }
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1161 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1162 if (inject_error == 4 && status != PSA_SUCCESS) {
1163 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001164 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 } else {
1166 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001167 }
1168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1170 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1171 if (inject_error == 4 && status != PSA_SUCCESS) {
1172 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001173 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 } else {
1175 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001176 }
1177
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001178 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (inject_error == 4) {
1180 TEST_ASSERT(
1181 !"One of the last psa_pake_input() calls should have returned the expected error.");
1182 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001183
1184 break;
1185
1186 }
1187
Neil Armstrongf983caf2022-06-15 15:27:48 +02001188exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 mbedtls_free(buffer0);
1190 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001191}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001192#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001195 INJECT_ERR_NONE = 0,
1196 INJECT_ERR_UNINITIALIZED_ACCESS,
1197 INJECT_ERR_DUPLICATE_SETUP,
1198 INJECT_ERR_INVALID_USER,
1199 INJECT_ERR_INVALID_PEER,
1200 INJECT_ERR_SET_USER,
1201 INJECT_ERR_SET_PEER,
1202 INJECT_EMPTY_IO_BUFFER,
1203 INJECT_UNKNOWN_STEP,
1204 INJECT_INVALID_FIRST_STEP,
1205 INJECT_WRONG_BUFFER_SIZE,
1206 INJECT_VALID_OPERATION_AFTER_FAILURE,
1207 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1208 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1209} ecjpake_injected_failure_t;
1210
Paul Elliott01885fa2023-02-09 12:07:30 +00001211#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001212
Paul Elliott6f600372023-02-06 18:41:05 +00001213static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1214 psa_status_t expected_status,
1215 size_t *min_completes,
1216 size_t *max_completes)
1217{
1218
1219 /* This is slightly contrived, but we only really know that with a minimum
1220 value of max_ops that a successful operation should take more than one op
1221 to complete, and likewise that with a max_ops of
1222 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1223 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001224
Paul Elliott6f600372023-02-06 18:41:05 +00001225 if (expected_status == PSA_SUCCESS) {
1226 *min_completes = 2;
1227 } else {
1228 *min_completes = 1;
1229 }
1230
1231 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1232 } else {
1233 *min_completes = 1;
1234 *max_completes = 1;
1235 }
1236}
Paul Elliott01885fa2023-02-09 12:07:30 +00001237#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001238
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01001239#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
1240static int rsa_test_e(mbedtls_svc_key_id_t key,
1241 size_t bits,
1242 const data_t *e_arg)
1243{
1244 uint8_t *exported = NULL;
1245 size_t exported_size =
1246 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
1247 size_t exported_length = SIZE_MAX;
1248 int ok = 0;
1249
1250 TEST_CALLOC(exported, exported_size);
1251 PSA_ASSERT(psa_export_public_key(key,
1252 exported, exported_size,
1253 &exported_length));
1254 uint8_t *p = exported;
1255 uint8_t *end = exported + exported_length;
1256 size_t len;
1257 /* RSAPublicKey ::= SEQUENCE {
1258 * modulus INTEGER, -- n
1259 * publicExponent INTEGER } -- e
1260 */
1261 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
1262 MBEDTLS_ASN1_SEQUENCE |
1263 MBEDTLS_ASN1_CONSTRUCTED));
1264 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
1265 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
1266 MBEDTLS_ASN1_INTEGER));
1267 if (len >= 1 && p[0] == 0) {
1268 ++p;
1269 --len;
1270 }
1271 if (e_arg->len == 0) {
1272 TEST_EQUAL(len, 3);
1273 TEST_EQUAL(p[0], 1);
1274 TEST_EQUAL(p[1], 0);
1275 TEST_EQUAL(p[2], 1);
1276 } else {
Gilles Peskine7a18f962024-02-12 16:48:11 +01001277 const uint8_t *expected = e_arg->x;
1278 size_t expected_len = e_arg->len;
1279 while (expected_len > 0 && *expected == 0) {
1280 ++expected;
1281 --expected_len;
1282 }
1283 TEST_MEMORY_COMPARE(p, len, expected, expected_len);
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01001284 }
1285 ok = 1;
1286
1287exit:
1288 mbedtls_free(exported);
1289 return ok;
1290}
1291#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE */
1292
Gilles Peskine092ce512024-02-20 12:31:24 +01001293static int setup_key_production_parameters(
1294 psa_key_production_parameters_t **params, size_t *params_data_length,
1295 int flags_arg, const data_t *params_data)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01001296{
Gilles Peskine092ce512024-02-20 12:31:24 +01001297 *params_data_length = params_data->len;
Gilles Peskinec81393b2024-02-14 20:51:28 +01001298 /* If there are N bytes of padding at the end of
Gilles Peskine092ce512024-02-20 12:31:24 +01001299 * psa_key_production_parameters_t, then it's enough to allocate
1300 * MIN(sizeof(psa_key_production_parameters_t),
1301 * offsetof(psa_key_production_parameters_t, data) + params_data_length).
Gilles Peskinec81393b2024-02-14 20:51:28 +01001302 *
1303 * For simplicity, here, we allocate up to N more bytes than necessary.
Gilles Peskine092ce512024-02-20 12:31:24 +01001304 * In practice, the current layout of psa_key_production_parameters_t
Gilles Peskinec81393b2024-02-14 20:51:28 +01001305 * makes padding extremely unlikely, so we don't worry about testing
1306 * that the library code doesn't try to access these extra N bytes.
1307 */
Gilles Peskine092ce512024-02-20 12:31:24 +01001308 *params = mbedtls_calloc(1, sizeof(**params) + *params_data_length);
1309 TEST_ASSERT(*params != NULL);
1310 (*params)->flags = (uint32_t) flags_arg;
1311 memcpy((*params)->data, params_data->x, params_data->len);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01001312 return 1;
1313exit:
1314 return 0;
1315}
1316
Ryan3a1b7862024-03-01 17:24:04 +00001317#if defined(MBEDTLS_THREADING_PTHREAD)
Ryan Everett50619992024-03-12 16:55:14 +00001318
1319typedef struct same_key_context {
1320 data_t *data;
1321 mbedtls_svc_key_id_t key;
1322 psa_key_attributes_t *attributes;
1323 int type;
1324 int bits;
1325 /* The following two parameters are used to ensure that when multiple
1326 * threads attempt to load/destroy the key, exactly one thread succeeds. */
1327 int key_loaded;
1328 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(key_loaded_mutex);
1329}
1330same_key_context;
1331
1332/* Attempt to import the key in ctx. This handles any valid error codes
1333 * and reports an error for any invalid codes. This function also insures
1334 * that once imported by some thread, all threads can use the key. */
Michael Schusterb1e33fb2024-06-04 02:30:22 +02001335static void *thread_import_key(void *ctx)
Ryan Everett50619992024-03-12 16:55:14 +00001336{
1337 mbedtls_svc_key_id_t returned_key_id;
1338 same_key_context *skc = (struct same_key_context *) ctx;
1339 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1340
Ryan Everett6c488702024-03-14 17:49:44 +00001341 /* Import the key, exactly one thread must succeed. */
Ryan Everett50619992024-03-12 16:55:14 +00001342 psa_status_t status = psa_import_key(skc->attributes, skc->data->x,
1343 skc->data->len, &returned_key_id);
1344 switch (status) {
1345 case PSA_SUCCESS:
1346 if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) {
1347 if (skc->key_loaded) {
1348 mbedtls_mutex_unlock(&skc->key_loaded_mutex);
1349 /* More than one thread has succeeded, report a failure. */
Ryan Everett3de040f2024-03-14 17:50:06 +00001350 TEST_FAIL("The same key has been loaded into the key store multiple times.");
Ryan Everett50619992024-03-12 16:55:14 +00001351 }
1352 skc->key_loaded = 1;
1353 mbedtls_mutex_unlock(&skc->key_loaded_mutex);
1354 }
1355 break;
1356 case PSA_ERROR_INSUFFICIENT_MEMORY:
1357 /* If all of the key slots are reserved when a thread
1358 * locks the mutex to reserve a new slot, it will return
1359 * PSA_ERROR_INSUFFICIENT_MEMORY; this is correct behaviour.
1360 * There is a chance for this to occur here when the number of
1361 * threads running this function is larger than the number of
1362 * free key slots. Each thread reserves an empty key slot,
1363 * unlocks the mutex, then relocks it to finalize key creation.
1364 * It is at that point where the thread sees that the key
1365 * already exists, releases the reserved slot,
1366 * and returns PSA_ERROR_ALREADY_EXISTS.
1367 * There is no guarantee that the key is loaded upon this return
1368 * code, so we can't test the key information. Just stop this
1369 * thread from executing, note that this is not an error. */
1370 goto exit;
1371 break;
1372 case PSA_ERROR_ALREADY_EXISTS:
1373 /* The key has been loaded by a different thread. */
1374 break;
1375 default:
1376 PSA_ASSERT(status);
1377 }
1378 /* At this point the key must exist, test the key information. */
1379 status = psa_get_key_attributes(skc->key, &got_attributes);
1380 if (status == PSA_ERROR_INSUFFICIENT_MEMORY) {
1381 /* This is not a test failure. The following sequence of events
1382 * causes this to occur:
1383 * 1: This thread successfuly imports a persistent key skc->key.
1384 * 2: N threads reserve an empty key slot in psa_import_key,
1385 * where N is equal to the number of free key slots.
1386 * 3: A final thread attempts to reserve an empty key slot, kicking
1387 * skc->key (which has no registered readers) out of its slot.
1388 * 4: This thread calls psa_get_key_attributes(skc->key,...):
1389 * it sees that skc->key is not in a slot, attempts to load it and
1390 * finds that there are no free slots.
1391 * This thread returns PSA_ERROR_INSUFFICIENT_MEMORY.
1392 *
1393 * The PSA spec allows this behaviour, it is an unavoidable consequence
1394 * of allowing persistent keys to be kicked out of the key store while
1395 * they are still valid. */
1396 goto exit;
1397 }
1398 PSA_ASSERT(status);
1399 TEST_EQUAL(psa_get_key_type(&got_attributes), skc->type);
1400 TEST_EQUAL(psa_get_key_bits(&got_attributes), skc->bits);
1401
1402exit:
1403 /* Key attributes may have been returned by psa_get_key_attributes(),
1404 * reset them as required. */
1405 psa_reset_key_attributes(&got_attributes);
1406 return NULL;
1407}
1408
Michael Schusterb1e33fb2024-06-04 02:30:22 +02001409static void *thread_use_and_destroy_key(void *ctx)
Ryan Everett50619992024-03-12 16:55:14 +00001410{
1411 same_key_context *skc = (struct same_key_context *) ctx;
1412
1413 /* Do something with the key according
1414 * to its type and permitted usage. */
1415 TEST_ASSERT(mbedtls_test_psa_exercise_key(skc->key,
1416 skc->attributes->policy.usage,
1417 skc->attributes->policy.alg, 1));
1418
1419 psa_status_t status = psa_destroy_key(skc->key);
1420 if (status == PSA_SUCCESS) {
1421 if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) {
1422 /* Ensure that we are the only thread to succeed. */
1423 if (skc->key_loaded != 1) {
1424 mbedtls_mutex_unlock(&skc->key_loaded_mutex);
Ryan Everett3de040f2024-03-14 17:50:06 +00001425 TEST_FAIL("The same key has been destroyed multiple times.");
Ryan Everett50619992024-03-12 16:55:14 +00001426 }
1427 skc->key_loaded = 0;
1428 mbedtls_mutex_unlock(&skc->key_loaded_mutex);
1429 }
1430 } else {
1431 TEST_EQUAL(status, PSA_ERROR_INVALID_HANDLE);
1432 }
1433
1434exit:
1435 return NULL;
1436}
1437
Ryan3a1b7862024-03-01 17:24:04 +00001438typedef struct generate_key_context {
1439 psa_key_type_t type;
1440 psa_key_usage_t usage;
1441 size_t bits;
1442 psa_algorithm_t alg;
1443 psa_status_t expected_status;
1444 psa_key_attributes_t *attributes;
1445 int is_large_key;
1446 int reps;
1447}
1448generate_key_context;
Michael Schusterb1e33fb2024-06-04 02:30:22 +02001449static void *thread_generate_key(void *ctx)
Ryan3a1b7862024-03-01 17:24:04 +00001450{
1451 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
1452 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1453 generate_key_context *gkc = (struct generate_key_context *) ctx;
1454
1455 /* If there are race conditions, it is likely the case that they do not
1456 * arise every time the code runs. We repeat the code to increase the
1457 * chance that any race conditions will be hit. */
1458 for (int n = 0; n < gkc->reps; n++) {
1459 /* Generate a key */
1460 psa_status_t status = psa_generate_key(gkc->attributes, &key);
1461
1462 if (gkc->is_large_key > 0) {
1463 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1464 }
1465
1466 TEST_EQUAL(status, gkc->expected_status);
1467 if (gkc->expected_status != PSA_SUCCESS) {
1468 PSA_ASSERT(psa_destroy_key(key));
1469 goto exit;
1470 }
1471
1472 /* Test the key information */
1473 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1474 TEST_EQUAL(psa_get_key_type(&got_attributes), gkc->type);
1475 TEST_EQUAL(psa_get_key_bits(&got_attributes), gkc->bits);
1476
1477 /* Do something with the key according
1478 * to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00001479 if (!mbedtls_test_psa_exercise_key(key, gkc->usage, gkc->alg, 0)) {
Ryan3a1b7862024-03-01 17:24:04 +00001480 psa_destroy_key(key);
1481 goto exit;
1482 }
1483 psa_reset_key_attributes(&got_attributes);
1484
1485 PSA_ASSERT(psa_destroy_key(key));
1486 }
1487exit:
1488 /*
1489 * Key attributes may have been returned by psa_get_key_attributes()
1490 * thus reset them as required.
1491 */
1492 psa_reset_key_attributes(&got_attributes);
1493 return NULL;
1494}
1495#endif /* MBEDTLS_THREADING_PTHREAD */
1496
Gilles Peskinee59236f2018-01-27 23:32:46 +01001497/* END_HEADER */
1498
1499/* BEGIN_DEPENDENCIES
1500 * depends_on:MBEDTLS_PSA_CRYPTO_C
1501 * END_DEPENDENCIES
1502 */
1503
1504/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001505void psa_can_do_hash()
1506{
1507 /* We can't test that this is specific to drivers until partial init has
1508 * been implemented, but we can at least test before/after full init. */
1509 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1510 PSA_INIT();
1511 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1512 PSA_DONE();
1513}
1514/* END_CASE */
1515
1516/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001518{
1519 size_t max_truncated_mac_size =
1520 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1521
1522 /* Check that the length for a truncated MAC always fits in the algorithm
1523 * encoding. The shifted mask is the maximum truncated value. The
1524 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530void import_with_policy(int type_arg,
1531 int usage_arg, int alg_arg,
1532 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001533{
1534 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1535 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001536 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001537 psa_key_type_t type = type_arg;
1538 psa_key_usage_t usage = usage_arg;
1539 psa_algorithm_t alg = alg_arg;
1540 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001542 psa_status_t status;
1543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 psa_set_key_type(&attributes, type);
1547 psa_set_key_usage_flags(&attributes, usage);
1548 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 status = psa_import_key(&attributes,
1551 key_material, sizeof(key_material),
1552 &key);
1553 TEST_EQUAL(status, expected_status);
1554 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1559 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1560 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1561 mbedtls_test_update_key_usage_flags(usage));
1562 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1563 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 PSA_ASSERT(psa_destroy_key(key));
1566 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001567
1568exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001569 /*
1570 * Key attributes may have been returned by psa_get_key_attributes()
1571 * thus reset them as required.
1572 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 psa_destroy_key(key);
1576 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001577}
1578/* END_CASE */
1579
1580/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581void import_with_data(data_t *data, int type_arg,
1582 int attr_bits_arg,
1583 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001584{
1585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1586 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001588 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001589 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001590 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 psa_set_key_type(&attributes, type);
1596 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001599 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1600 *
1601 * This can happen with a type supported only by a driver:
1602 * - the driver sees the invalid data (for example wrong size) and thinks
1603 * "well perhaps this is a key size I don't support" so it returns
1604 * NOT_SUPPORTED which is correct at this point;
1605 * - we fallback to built-ins, which don't support this type, so return
1606 * NOT_SUPPORTED which again is correct at this point.
1607 */
1608 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1609 status == PSA_ERROR_NOT_SUPPORTED) {
1610 ; // OK
1611 } else {
1612 TEST_EQUAL(status, expected_status);
1613 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001615 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1619 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1620 if (attr_bits != 0) {
1621 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1622 }
1623 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 PSA_ASSERT(psa_destroy_key(key));
1626 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627
1628exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001629 /*
1630 * Key attributes may have been returned by psa_get_key_attributes()
1631 * thus reset them as required.
1632 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 psa_destroy_key(key);
1636 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001637}
1638/* END_CASE */
1639
1640/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001641/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001642void import_large_key(int type_arg, int byte_size_arg,
1643 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001644{
1645 psa_key_type_t type = type_arg;
1646 size_t byte_size = byte_size_arg;
1647 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1648 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001649 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001650 psa_status_t status;
1651 uint8_t *buffer = NULL;
1652 size_t buffer_size = byte_size + 1;
1653 size_t n;
1654
Steven Cooreman69967ce2021-01-18 18:01:08 +01001655 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001656 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001657 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001661
1662 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1664 psa_set_key_type(&attributes, type);
1665 status = psa_import_key(&attributes, buffer, byte_size, &key);
1666 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1667 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 if (status == PSA_SUCCESS) {
1670 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1671 TEST_EQUAL(psa_get_key_type(&attributes), type);
1672 TEST_EQUAL(psa_get_key_bits(&attributes),
1673 PSA_BYTES_TO_BITS(byte_size));
1674 ASSERT_NO_SLOT_NUMBER(&attributes);
1675 memset(buffer, 0, byte_size + 1);
1676 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1677 for (n = 0; n < byte_size; n++) {
1678 TEST_EQUAL(buffer[n], 'K');
1679 }
1680 for (n = byte_size; n < buffer_size; n++) {
1681 TEST_EQUAL(buffer[n], 0);
1682 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001683 }
1684
1685exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001686 /*
1687 * Key attributes may have been returned by psa_get_key_attributes()
1688 * thus reset them as required.
1689 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 psa_destroy_key(key);
1693 PSA_DONE();
1694 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001695}
1696/* END_CASE */
1697
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001698/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001699/* Import an RSA key with a valid structure (but not valid numbers
1700 * inside, beyond having sensible size and parity). This is expected to
1701 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001702void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001703{
Ronald Cron5425a212020-08-04 14:58:35 +02001704 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001705 size_t bits = bits_arg;
1706 psa_status_t expected_status = expected_status_arg;
1707 psa_status_t status;
1708 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001709 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001710 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001712 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001713 unsigned char *p;
1714 int ret;
1715 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001719 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1722 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001723 length = ret;
1724
1725 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 psa_set_key_type(&attributes, type);
1727 status = psa_import_key(&attributes, p, length, &key);
1728 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if (status == PSA_SUCCESS) {
1731 PSA_ASSERT(psa_destroy_key(key));
1732 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001733
1734exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 mbedtls_free(buffer);
1736 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001737}
1738/* END_CASE */
1739
1740/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001741void import_export(data_t *data,
1742 int type_arg,
1743 int usage_arg, int alg_arg,
1744 int lifetime_arg,
1745 int expected_bits,
1746 int export_size_delta,
1747 int expected_export_status_arg,
1748 /*whether reexport must give the original input exactly*/
1749 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001750{
Ronald Cron5425a212020-08-04 14:58:35 +02001751 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001752 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001753 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001754 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001755 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301756 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001757 unsigned char *exported = NULL;
1758 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001759 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001760 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001761 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001762 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001763 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001764
Moran Pekercb088e72018-07-17 17:36:59 +03001765 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001766 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001768 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 }
1770 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 psa_set_key_lifetime(&attributes, lifetime);
1773 psa_set_key_usage_flags(&attributes, usage_arg);
1774 psa_set_key_algorithm(&attributes, alg);
1775 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001776
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001777 if (PSA_KEY_TYPE_IS_DH(type) &&
1778 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001779 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1780 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001781 }
1782
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001783 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001784 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001785 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001786
1787 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1789 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1790 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1791 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001792
1793 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 status = psa_export_key(key, exported, export_size, &exported_length);
1795 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001796
1797 /* The exported length must be set by psa_export_key() to a value between 0
1798 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1800 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1801 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1804 export_size - exported_length));
1805 if (status != PSA_SUCCESS) {
1806 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001807 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001808 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001809
Gilles Peskineea38a922021-02-13 00:05:16 +01001810 /* Run sanity checks on the exported key. For non-canonical inputs,
1811 * this validates the canonical representations. For canonical inputs,
1812 * this doesn't directly validate the implementation, but it still helps
1813 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 if (!psa_key_lifetime_is_external(lifetime)) {
Ryan Everett0a271fd2024-03-12 16:34:02 +00001815 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301816 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 }
Archana4d7ae1d2021-07-07 02:50:22 +05301818 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001821 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001823 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1825 &key2));
1826 PSA_ASSERT(psa_export_key(key2,
1827 reexported,
1828 export_size,
1829 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001830 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001831 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 TEST_LE_U(exported_length,
1835 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1836 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001837 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1838 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1839 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1840 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1841 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001842
1843destroy:
1844 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 PSA_ASSERT(psa_destroy_key(key));
1846 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001847
1848exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001849 /*
1850 * Key attributes may have been returned by psa_get_key_attributes()
1851 * thus reset them as required.
1852 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 psa_reset_key_attributes(&got_attributes);
1854 psa_destroy_key(key);
1855 mbedtls_free(exported);
1856 mbedtls_free(reexported);
1857 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001858}
1859/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001860
Moran Pekerf709f4a2018-06-06 17:26:04 +03001861/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862void import_export_public_key(data_t *data,
1863 int type_arg, // key pair or public key
1864 int alg_arg,
1865 int lifetime_arg,
1866 int export_size_delta,
1867 int expected_export_status_arg,
1868 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001869{
Ronald Cron5425a212020-08-04 14:58:35 +02001870 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001871 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001872 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001873 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001874 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301875 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001876 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001877 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001878 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 psa_set_key_lifetime(&attributes, lifetime);
1884 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1885 psa_set_key_algorithm(&attributes, alg);
1886 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001887
1888 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001890
Gilles Peskine49c25912018-10-29 15:15:31 +01001891 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001892 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 status = psa_export_public_key(key,
1894 exported, export_size,
1895 &exported_length);
1896 TEST_EQUAL(status, expected_export_status);
1897 if (status == PSA_SUCCESS) {
1898 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001899 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1901 bits = psa_get_key_bits(&attributes);
1902 TEST_LE_U(expected_public_key->len,
1903 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1904 TEST_LE_U(expected_public_key->len,
1905 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1906 TEST_LE_U(expected_public_key->len,
1907 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001908 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001909 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001910 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001911exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001912 /*
1913 * Key attributes may have been returned by psa_get_key_attributes()
1914 * thus reset them as required.
1915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 mbedtls_free(exported);
1919 psa_destroy_key(key);
1920 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001921}
1922/* END_CASE */
1923
Ryan Everett50619992024-03-12 16:55:14 +00001924
1925#if defined(MBEDTLS_THREADING_PTHREAD)
1926/* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD:MBEDTLS_PSA_CRYPTO_STORAGE_C */
1927void concurrently_use_same_persistent_key(data_t *data,
1928 int type_arg,
1929 int bits_arg,
1930 int alg_arg,
1931 int thread_count_arg)
1932{
1933 size_t thread_count = (size_t) thread_count_arg;
1934 mbedtls_test_thread_t *threads = NULL;
1935 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
1936 same_key_context skc;
1937 skc.data = data;
1938 skc.key = key_id;
1939 skc.type = type_arg;
1940 skc.bits = bits_arg;
1941 skc.key_loaded = 0;
1942 mbedtls_mutex_init(&skc.key_loaded_mutex);
1943 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(skc.type, alg_arg);
1944 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1945
1946 PSA_ASSERT(psa_crypto_init());
1947
1948 psa_set_key_id(&attributes, key_id);
1949 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT);
1950 psa_set_key_usage_flags(&attributes, usage);
1951 psa_set_key_algorithm(&attributes, alg_arg);
1952 psa_set_key_type(&attributes, type_arg);
1953 psa_set_key_bits(&attributes, bits_arg);
1954 skc.attributes = &attributes;
1955
1956 TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count);
1957
1958 /* Test that when multiple threads import the same key,
1959 * exactly one thread succeeds and the rest fail with valid errors.
1960 * Also test that all threads can use the key as soon as it has been
1961 * imported. */
1962 for (size_t i = 0; i < thread_count; i++) {
1963 TEST_EQUAL(
1964 mbedtls_test_thread_create(&threads[i], thread_import_key,
1965 (void *) &skc), 0);
1966 }
1967
1968 /* Join threads. */
1969 for (size_t i = 0; i < thread_count; i++) {
1970 TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0);
1971 }
1972
1973 /* Test that when multiple threads use and destroy a key no corruption
1974 * occurs, and exactly one thread succeeds when destroying the key. */
1975 for (size_t i = 0; i < thread_count; i++) {
1976 TEST_EQUAL(
1977 mbedtls_test_thread_create(&threads[i], thread_use_and_destroy_key,
1978 (void *) &skc), 0);
1979 }
1980
1981 /* Join threads. */
1982 for (size_t i = 0; i < thread_count; i++) {
1983 TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0);
1984 }
1985 /* Ensure that one thread succeeded in destroying the key. */
1986 TEST_ASSERT(!skc.key_loaded);
1987exit:
1988 psa_reset_key_attributes(&attributes);
1989 mbedtls_mutex_free(&skc.key_loaded_mutex);
1990 mbedtls_free(threads);
1991 PSA_DONE();
1992}
1993/* END_CASE */
1994#endif
1995
Gilles Peskine20035e32018-02-03 22:44:14 +01001996/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997void import_and_exercise_key(data_t *data,
1998 int type_arg,
1999 int bits_arg,
2000 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002001{
Ronald Cron5425a212020-08-04 14:58:35 +02002002 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002003 psa_key_type_t type = type_arg;
2004 size_t bits = bits_arg;
2005 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02002007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002008 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 psa_set_key_usage_flags(&attributes, usage);
2013 psa_set_key_algorithm(&attributes, alg);
2014 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002015
2016 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002018
2019 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2021 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
2022 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002023
2024 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00002025 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02002026 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 PSA_ASSERT(psa_destroy_key(key));
2030 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02002031
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002032exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002033 /*
2034 * Key attributes may have been returned by psa_get_key_attributes()
2035 * thus reset them as required.
2036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 psa_reset_key_attributes(&attributes);
2040 psa_destroy_key(key);
2041 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002042}
2043/* END_CASE */
2044
2045/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046void effective_key_attributes(int type_arg, int expected_type_arg,
2047 int bits_arg, int expected_bits_arg,
2048 int usage_arg, int expected_usage_arg,
2049 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02002050{
Ronald Cron5425a212020-08-04 14:58:35 +02002051 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01002052 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002053 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01002054 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002055 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02002056 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002057 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02002058 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002059 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02002061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 psa_set_key_usage_flags(&attributes, usage);
2065 psa_set_key_algorithm(&attributes, alg);
2066 psa_set_key_type(&attributes, key_type);
2067 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 PSA_ASSERT(psa_generate_key(&attributes, &key));
2070 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02002071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2073 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
2074 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
2075 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
2076 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02002077
2078exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002079 /*
2080 * Key attributes may have been returned by psa_get_key_attributes()
2081 * thus reset them as required.
2082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 psa_destroy_key(key);
2086 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002087}
2088/* END_CASE */
2089
2090/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091void check_key_policy(int type_arg, int bits_arg,
2092 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01002093{
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
2095 usage_arg,
2096 mbedtls_test_update_key_usage_flags(usage_arg),
2097 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01002098 goto exit;
2099}
2100/* END_CASE */
2101
2102/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00002104{
2105 /* Test each valid way of initializing the object, except for `= {0}`, as
2106 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2107 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002108 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002110 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
2111 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00002114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
2116 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
2117 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 TEST_EQUAL(psa_get_key_type(&func), 0);
2120 TEST_EQUAL(psa_get_key_type(&init), 0);
2121 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 TEST_EQUAL(psa_get_key_bits(&func), 0);
2124 TEST_EQUAL(psa_get_key_bits(&init), 0);
2125 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
2128 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
2129 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
2132 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
2133 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00002134}
2135/* END_CASE */
2136
2137/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002138void mac_key_policy(int policy_usage_arg,
2139 int policy_alg_arg,
2140 int key_type_arg,
2141 data_t *key_data,
2142 int exercise_alg_arg,
2143 int expected_status_sign_arg,
2144 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02002145{
Ronald Cron5425a212020-08-04 14:58:35 +02002146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00002148 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002149 psa_key_type_t key_type = key_type_arg;
2150 psa_algorithm_t policy_alg = policy_alg_arg;
2151 psa_algorithm_t exercise_alg = exercise_alg_arg;
2152 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002153 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02002154 psa_status_t expected_status_sign = expected_status_sign_arg;
2155 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002156 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02002157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 psa_set_key_usage_flags(&attributes, policy_usage);
2161 psa_set_key_algorithm(&attributes, policy_alg);
2162 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2165 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
2168 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 status = psa_mac_sign_setup(&operation, key, exercise_alg);
2171 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002172
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002173 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002175 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
2177 input, 128,
2178 mac, PSA_MAC_MAX_SIZE, &mac_len),
2179 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002180
Neil Armstrong3af9b972022-02-07 12:20:21 +01002181 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 PSA_ASSERT(psa_mac_abort(&operation));
2183 status = psa_mac_sign_setup(&operation, key, exercise_alg);
2184 if (status == PSA_SUCCESS) {
2185 status = psa_mac_update(&operation, input, 128);
2186 if (status == PSA_SUCCESS) {
2187 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
2188 &mac_len),
2189 expected_status_sign);
2190 } else {
2191 TEST_EQUAL(status, expected_status_sign);
2192 }
2193 } else {
2194 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002195 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01002197
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002198 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 status = psa_mac_verify(key, exercise_alg, input, 128,
2200 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
2203 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2204 } else {
2205 TEST_EQUAL(status, expected_status_verify);
2206 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01002207
Neil Armstrong3af9b972022-02-07 12:20:21 +01002208 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 status = psa_mac_verify_setup(&operation, key, exercise_alg);
2210 if (status == PSA_SUCCESS) {
2211 status = psa_mac_update(&operation, input, 128);
2212 if (status == PSA_SUCCESS) {
2213 status = psa_mac_verify_finish(&operation, mac, mac_len);
2214 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
2215 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2216 } else {
2217 TEST_EQUAL(status, expected_status_verify);
2218 }
2219 } else {
2220 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002221 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 } else {
2223 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002224 }
2225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 memset(mac, 0, sizeof(mac));
2229 status = psa_mac_verify_setup(&operation, key, exercise_alg);
2230 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002231
2232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 psa_mac_abort(&operation);
2234 psa_destroy_key(key);
2235 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002236}
2237/* END_CASE */
2238
2239/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002240void cipher_key_policy(int policy_usage_arg,
2241 int policy_alg,
2242 int key_type,
2243 data_t *key_data,
2244 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002245{
Ronald Cron5425a212020-08-04 14:58:35 +02002246 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002248 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002249 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002250 size_t output_buffer_size = 0;
2251 size_t input_buffer_size = 0;
2252 size_t output_length = 0;
2253 uint8_t *output = NULL;
2254 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002255 psa_status_t status;
2256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
2258 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
2259 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002260
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002261 TEST_CALLOC(input, input_buffer_size);
2262 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 psa_set_key_usage_flags(&attributes, policy_usage);
2267 psa_set_key_algorithm(&attributes, policy_alg);
2268 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2271 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002272
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002273 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 TEST_EQUAL(policy_usage,
2275 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002276
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002277 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
2279 output, output_buffer_size,
2280 &output_length);
2281 if (policy_alg == exercise_alg &&
2282 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2283 PSA_ASSERT(status);
2284 } else {
2285 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2286 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002287
2288 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
2290 if (policy_alg == exercise_alg &&
2291 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2292 PSA_ASSERT(status);
2293 } else {
2294 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2295 }
2296 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002297
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002298 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
2300 input, input_buffer_size,
2301 &output_length);
2302 if (policy_alg == exercise_alg &&
2303 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2304 PSA_ASSERT(status);
2305 } else {
2306 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2307 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002308
2309 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2311 if (policy_alg == exercise_alg &&
2312 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2313 PSA_ASSERT(status);
2314 } else {
2315 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2316 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002317
2318exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 psa_cipher_abort(&operation);
2320 mbedtls_free(input);
2321 mbedtls_free(output);
2322 psa_destroy_key(key);
2323 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002324}
2325/* END_CASE */
2326
2327/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328void aead_key_policy(int policy_usage_arg,
2329 int policy_alg,
2330 int key_type,
2331 data_t *key_data,
2332 int nonce_length_arg,
2333 int tag_length_arg,
2334 int exercise_alg,
2335 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002336{
Ronald Cron5425a212020-08-04 14:58:35 +02002337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002339 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002340 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002341 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002342 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002344 size_t nonce_length = nonce_length_arg;
2345 unsigned char tag[16];
2346 size_t tag_length = tag_length_arg;
2347 size_t output_length;
2348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 TEST_LE_U(nonce_length, sizeof(nonce));
2350 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 psa_set_key_usage_flags(&attributes, policy_usage);
2355 psa_set_key_algorithm(&attributes, policy_alg);
2356 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2359 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002360
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002361 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 TEST_EQUAL(policy_usage,
2363 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002364
Neil Armstrong752d8112022-02-07 14:51:11 +01002365 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 status = psa_aead_encrypt(key, exercise_alg,
2367 nonce, nonce_length,
2368 NULL, 0,
2369 NULL, 0,
2370 tag, tag_length,
2371 &output_length);
2372 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2373 TEST_EQUAL(status, expected_status);
2374 } else {
2375 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2376 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002377
Neil Armstrong752d8112022-02-07 14:51:11 +01002378 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2380 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2381 TEST_EQUAL(status, expected_status);
2382 } else {
2383 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2384 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002385
2386 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 memset(tag, 0, sizeof(tag));
2388 status = psa_aead_decrypt(key, exercise_alg,
2389 nonce, nonce_length,
2390 NULL, 0,
2391 tag, tag_length,
2392 NULL, 0,
2393 &output_length);
2394 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2395 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2396 } else if (expected_status == PSA_SUCCESS) {
2397 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2398 } else {
2399 TEST_EQUAL(status, expected_status);
2400 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002401
Neil Armstrong752d8112022-02-07 14:51:11 +01002402 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 PSA_ASSERT(psa_aead_abort(&operation));
2404 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2405 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2406 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2407 } else {
2408 TEST_EQUAL(status, expected_status);
2409 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002410
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002411exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 PSA_ASSERT(psa_aead_abort(&operation));
2413 psa_destroy_key(key);
2414 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002415}
2416/* END_CASE */
2417
2418/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002419void asymmetric_encryption_key_policy(int policy_usage_arg,
2420 int policy_alg,
2421 int key_type,
2422 data_t *key_data,
Valerio Settif202c292024-01-15 10:42:37 +01002423 int exercise_alg,
2424 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002425{
Ronald Cron5425a212020-08-04 14:58:35 +02002426 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002428 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002429 psa_status_t status;
2430 size_t key_bits;
2431 size_t buffer_length;
2432 unsigned char *buffer = NULL;
2433 size_t output_length;
2434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 psa_set_key_usage_flags(&attributes, policy_usage);
2438 psa_set_key_algorithm(&attributes, policy_alg);
2439 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002440
Valerio Settif202c292024-01-15 10:42:37 +01002441 if (use_opaque_key) {
2442 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2443 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2444 }
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2447 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002448
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002449 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 TEST_EQUAL(policy_usage,
2451 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2454 key_bits = psa_get_key_bits(&attributes);
2455 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2456 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002457 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 status = psa_asymmetric_encrypt(key, exercise_alg,
2460 NULL, 0,
2461 NULL, 0,
2462 buffer, buffer_length,
2463 &output_length);
2464 if (policy_alg == exercise_alg &&
2465 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2466 PSA_ASSERT(status);
2467 } else {
2468 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2469 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 if (buffer_length != 0) {
2472 memset(buffer, 0, buffer_length);
2473 }
2474 status = psa_asymmetric_decrypt(key, exercise_alg,
2475 buffer, buffer_length,
2476 NULL, 0,
2477 buffer, buffer_length,
2478 &output_length);
2479 if (policy_alg == exercise_alg &&
2480 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2481 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2482 } else {
2483 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2484 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002485
2486exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002487 /*
2488 * Key attributes may have been returned by psa_get_key_attributes()
2489 * thus reset them as required.
2490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 psa_destroy_key(key);
2494 PSA_DONE();
2495 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002496}
2497/* END_CASE */
2498
2499/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002500void asymmetric_signature_key_policy(int policy_usage_arg,
2501 int policy_alg,
2502 int key_type,
2503 data_t *key_data,
2504 int exercise_alg,
2505 int payload_length_arg,
2506 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002507{
Ronald Cron5425a212020-08-04 14:58:35 +02002508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002509 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002510 psa_key_usage_t policy_usage = policy_usage_arg;
2511 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002512 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002514 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2515 * compatible with the policy and `payload_length_arg` is supposed to be
2516 * a valid input length to sign. If `payload_length_arg <= 0`,
2517 * `exercise_alg` is supposed to be forbidden by the policy. */
2518 int compatible_alg = payload_length_arg > 0;
2519 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002521 size_t signature_length;
2522
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002523 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002524 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 TEST_EQUAL(expected_usage,
2526 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 psa_set_key_usage_flags(&attributes, policy_usage);
2531 psa_set_key_algorithm(&attributes, policy_alg);
2532 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2535 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 status = psa_sign_hash(key, exercise_alg,
2540 payload, payload_length,
2541 signature, sizeof(signature),
2542 &signature_length);
2543 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2544 PSA_ASSERT(status);
2545 } else {
2546 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2547 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 memset(signature, 0, sizeof(signature));
2550 status = psa_verify_hash(key, exercise_alg,
2551 payload, payload_length,
2552 signature, sizeof(signature));
2553 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2554 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2555 } else {
2556 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2557 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2560 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2561 status = psa_sign_message(key, exercise_alg,
2562 payload, payload_length,
2563 signature, sizeof(signature),
2564 &signature_length);
2565 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2566 PSA_ASSERT(status);
2567 } else {
2568 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2569 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 memset(signature, 0, sizeof(signature));
2572 status = psa_verify_message(key, exercise_alg,
2573 payload, payload_length,
2574 signature, sizeof(signature));
2575 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2576 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2577 } else {
2578 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2579 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002580 }
2581
Gilles Peskined5b33222018-06-18 22:20:03 +02002582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 psa_destroy_key(key);
2584 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002585}
2586/* END_CASE */
2587
Janos Follathba3fab92019-06-11 14:50:16 +01002588/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589void derive_key_policy(int policy_usage,
2590 int policy_alg,
2591 int key_type,
2592 data_t *key_data,
2593 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002594{
Ronald Cron5425a212020-08-04 14:58:35 +02002595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002597 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002598 psa_status_t status;
2599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 psa_set_key_usage_flags(&attributes, policy_usage);
2603 psa_set_key_algorithm(&attributes, policy_alg);
2604 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2607 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2612 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2613 PSA_ASSERT(psa_key_derivation_input_bytes(
2614 &operation,
2615 PSA_KEY_DERIVATION_INPUT_SEED,
2616 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002617 }
Janos Follathba3fab92019-06-11 14:50:16 +01002618
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 status = psa_key_derivation_input_key(&operation,
2620 PSA_KEY_DERIVATION_INPUT_SECRET,
2621 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (policy_alg == exercise_alg &&
2624 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2625 PSA_ASSERT(status);
2626 } else {
2627 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2628 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002629
2630exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 psa_key_derivation_abort(&operation);
2632 psa_destroy_key(key);
2633 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002634}
2635/* END_CASE */
2636
2637/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002638void agreement_key_policy(int policy_usage,
2639 int policy_alg,
2640 int key_type_arg,
2641 data_t *key_data,
2642 int exercise_alg,
2643 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002644{
Ronald Cron5425a212020-08-04 14:58:35 +02002645 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002647 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002648 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002649 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002650 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002653
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 psa_set_key_usage_flags(&attributes, policy_usage);
2655 psa_set_key_algorithm(&attributes, policy_alg);
2656 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2659 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Ryan Everett73e4ea32024-03-12 16:29:55 +00002662 status = mbedtls_test_psa_key_agreement_with_self(&operation, key, 0);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002665
2666exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 psa_key_derivation_abort(&operation);
2668 psa_destroy_key(key);
2669 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002670}
2671/* END_CASE */
2672
2673/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002674void key_policy_alg2(int key_type_arg, data_t *key_data,
2675 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002676{
Ronald Cron5425a212020-08-04 14:58:35 +02002677 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002678 psa_key_type_t key_type = key_type_arg;
2679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2680 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2681 psa_key_usage_t usage = usage_arg;
2682 psa_algorithm_t alg = alg_arg;
2683 psa_algorithm_t alg2 = alg2_arg;
2684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 psa_set_key_usage_flags(&attributes, usage);
2688 psa_set_key_algorithm(&attributes, alg);
2689 psa_set_key_enrollment_algorithm(&attributes, alg2);
2690 psa_set_key_type(&attributes, key_type);
2691 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2692 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002693
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002694 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 usage = mbedtls_test_update_key_usage_flags(usage);
2696 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2697 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2698 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2699 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002700
Ryan Everett0a271fd2024-03-12 16:34:02 +00002701 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002702 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 }
Ryan Everett0a271fd2024-03-12 16:34:02 +00002704 if (!mbedtls_test_psa_exercise_key(key, usage, alg2, 0)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002705 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002707
2708exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002709 /*
2710 * Key attributes may have been returned by psa_get_key_attributes()
2711 * thus reset them as required.
2712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 psa_destroy_key(key);
2716 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002717}
2718/* END_CASE */
2719
2720/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002721void raw_agreement_key_policy(int policy_usage,
2722 int policy_alg,
2723 int key_type_arg,
2724 data_t *key_data,
2725 int exercise_alg,
2726 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002727{
Ronald Cron5425a212020-08-04 14:58:35 +02002728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002729 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002730 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002731 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002732 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002733 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 psa_set_key_usage_flags(&attributes, policy_usage);
2738 psa_set_key_algorithm(&attributes, policy_alg);
2739 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2742 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002743
Ryan Everett81630282024-03-12 16:21:12 +00002744 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key, 0);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002747
2748exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 psa_key_derivation_abort(&operation);
2750 psa_destroy_key(key);
2751 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002752}
2753/* END_CASE */
2754
2755/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002756void copy_success(int source_usage_arg,
2757 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002758 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 int type_arg, data_t *material,
2760 int copy_attributes,
2761 int target_usage_arg,
2762 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002763 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 int expected_usage_arg,
2765 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002766{
Gilles Peskineca25db92019-04-19 11:43:08 +02002767 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2768 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002769 psa_key_usage_t expected_usage = expected_usage_arg;
2770 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002771 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302772 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2773 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002774 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2775 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002776 uint8_t *export_buffer = NULL;
2777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002779
Gilles Peskineca25db92019-04-19 11:43:08 +02002780 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2782 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2783 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2784 psa_set_key_type(&source_attributes, type_arg);
2785 psa_set_key_lifetime(&source_attributes, source_lifetime);
2786 PSA_ASSERT(psa_import_key(&source_attributes,
2787 material->x, material->len,
2788 &source_key));
2789 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002790
Gilles Peskineca25db92019-04-19 11:43:08 +02002791 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002793 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002794 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002796
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 if (target_usage_arg != -1) {
2798 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2799 }
2800 if (target_alg_arg != -1) {
2801 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2802 }
2803 if (target_alg2_arg != -1) {
2804 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2805 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002806
Archana8a180362021-07-05 02:18:48 +05302807
Gilles Peskine57ab7212019-01-28 13:03:09 +01002808 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 PSA_ASSERT(psa_copy_key(source_key,
2810 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002811
2812 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002814
2815 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2817 TEST_EQUAL(psa_get_key_type(&source_attributes),
2818 psa_get_key_type(&target_attributes));
2819 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2820 psa_get_key_bits(&target_attributes));
2821 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2822 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2823 TEST_EQUAL(expected_alg2,
2824 psa_get_key_enrollment_algorithm(&target_attributes));
2825 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002826 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002827 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2829 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002830 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002831 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002832 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 if (!psa_key_lifetime_is_external(target_lifetime)) {
Ryan Everett0a271fd2024-03-12 16:34:02 +00002835 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg, 0)) {
Archana8a180362021-07-05 02:18:48 +05302836 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 }
Ryan Everett0a271fd2024-03-12 16:34:02 +00002838 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2, 0)) {
Archana8a180362021-07-05 02:18:48 +05302839 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 }
Archana8a180362021-07-05 02:18:48 +05302841 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002844
2845exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002846 /*
2847 * Source and target key attributes may have been returned by
2848 * psa_get_key_attributes() thus reset them as required.
2849 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 psa_reset_key_attributes(&source_attributes);
2851 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 PSA_DONE();
2854 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002855}
2856/* END_CASE */
2857
2858/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002859void copy_fail(int source_usage_arg,
2860 int source_alg_arg, int source_alg2_arg,
2861 int source_lifetime_arg,
2862 int type_arg, data_t *material,
2863 int target_type_arg, int target_bits_arg,
2864 int target_usage_arg,
2865 int target_alg_arg, int target_alg2_arg,
2866 int target_id_arg, int target_lifetime_arg,
2867 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002868{
2869 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2870 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002871 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2872 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002876
2877 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2879 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2880 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2881 psa_set_key_type(&source_attributes, type_arg);
2882 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2883 PSA_ASSERT(psa_import_key(&source_attributes,
2884 material->x, material->len,
2885 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002886
2887 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 psa_set_key_id(&target_attributes, key_id);
2889 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2890 psa_set_key_type(&target_attributes, target_type_arg);
2891 psa_set_key_bits(&target_attributes, target_bits_arg);
2892 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2893 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2894 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002895
2896 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 TEST_EQUAL(psa_copy_key(source_key,
2898 &target_attributes, &target_key),
2899 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002902
Gilles Peskine4a644642019-05-03 17:14:08 +02002903exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 psa_reset_key_attributes(&source_attributes);
2905 psa_reset_key_attributes(&target_attributes);
2906 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002907}
2908/* END_CASE */
2909
2910/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002911void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002912{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002913 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002914 /* Test each valid way of initializing the object, except for `= {0}`, as
2915 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2916 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002917 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002919 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2920 psa_hash_operation_t zero;
2921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002923
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002924 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2926 PSA_ERROR_BAD_STATE);
2927 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2928 PSA_ERROR_BAD_STATE);
2929 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2930 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002931
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002932 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 PSA_ASSERT(psa_hash_abort(&func));
2934 PSA_ASSERT(psa_hash_abort(&init));
2935 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002936}
2937/* END_CASE */
2938
2939/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002940void hash_setup(int alg_arg,
2941 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002942{
2943 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002944 uint8_t *output = NULL;
2945 size_t output_size = 0;
2946 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002947 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002948 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002949 psa_status_t status;
2950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002952
Neil Armstrongedb20862022-02-07 15:47:44 +01002953 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002955 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 status = psa_hash_compute(alg, NULL, 0,
2958 output, output_size, &output_length);
2959 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002960
2961 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 status = psa_hash_setup(&operation, alg);
2963 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002964
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002965 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002967
2968 /* If setup failed, reproduce the failure, so as to
2969 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 if (status != PSA_SUCCESS) {
2971 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2972 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002973
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002974 /* Now the operation object should be reusable. */
2975#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2977 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002978#endif
2979
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002980exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 mbedtls_free(output);
2982 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002983}
2984/* END_CASE */
2985
2986/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002987void hash_compute_fail(int alg_arg, data_t *input,
2988 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002989{
2990 psa_algorithm_t alg = alg_arg;
2991 uint8_t *output = NULL;
2992 size_t output_size = output_size_arg;
2993 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002994 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002995 psa_status_t expected_status = expected_status_arg;
2996 psa_status_t status;
2997
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002998 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01003001
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003002 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 status = psa_hash_compute(alg, input->x, input->len,
3004 output, output_size, &output_length);
3005 TEST_EQUAL(status, expected_status);
3006 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003007
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003008 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 status = psa_hash_setup(&operation, alg);
3010 if (status == PSA_SUCCESS) {
3011 status = psa_hash_update(&operation, input->x, input->len);
3012 if (status == PSA_SUCCESS) {
3013 status = psa_hash_finish(&operation, output, output_size,
3014 &output_length);
3015 if (status == PSA_SUCCESS) {
3016 TEST_LE_U(output_length, output_size);
3017 } else {
3018 TEST_EQUAL(status, expected_status);
3019 }
3020 } else {
3021 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003022 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 } else {
3024 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003025 }
3026
Gilles Peskine0a749c82019-11-28 19:33:58 +01003027exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 PSA_ASSERT(psa_hash_abort(&operation));
3029 mbedtls_free(output);
3030 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01003031}
3032/* END_CASE */
3033
3034/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003035void hash_compare_fail(int alg_arg, data_t *input,
3036 data_t *reference_hash,
3037 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01003038{
3039 psa_algorithm_t alg = alg_arg;
3040 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01003041 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01003042 psa_status_t status;
3043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01003045
Neil Armstrong55a1be12022-02-07 11:23:20 +01003046 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 status = psa_hash_compare(alg, input->x, input->len,
3048 reference_hash->x, reference_hash->len);
3049 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01003050
Neil Armstrong55a1be12022-02-07 11:23:20 +01003051 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 status = psa_hash_setup(&operation, alg);
3053 if (status == PSA_SUCCESS) {
3054 status = psa_hash_update(&operation, input->x, input->len);
3055 if (status == PSA_SUCCESS) {
3056 status = psa_hash_verify(&operation, reference_hash->x,
3057 reference_hash->len);
3058 TEST_EQUAL(status, expected_status);
3059 } else {
3060 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01003061 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 } else {
3063 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01003064 }
3065
Gilles Peskine88e08462020-01-28 20:43:00 +01003066exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 PSA_ASSERT(psa_hash_abort(&operation));
3068 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01003069}
3070/* END_CASE */
3071
3072/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003073void hash_compute_compare(int alg_arg, data_t *input,
3074 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01003075{
3076 psa_algorithm_t alg = alg_arg;
3077 uint8_t output[PSA_HASH_MAX_SIZE + 1];
3078 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01003079 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01003080 size_t i;
3081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01003083
Neil Armstrongca30a002022-02-07 11:40:23 +01003084 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
3086 output, PSA_HASH_LENGTH(alg),
3087 &output_length));
3088 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003089 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003090 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003091
Neil Armstrongca30a002022-02-07 11:40:23 +01003092 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 PSA_ASSERT(psa_hash_setup(&operation, alg));
3094 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3095 PSA_ASSERT(psa_hash_finish(&operation, output,
3096 PSA_HASH_LENGTH(alg),
3097 &output_length));
3098 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003099 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003100 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01003101
3102 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
3104 output, sizeof(output),
3105 &output_length));
3106 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003107 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003108 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003109
Neil Armstrongca30a002022-02-07 11:40:23 +01003110 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 PSA_ASSERT(psa_hash_setup(&operation, alg));
3112 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3113 PSA_ASSERT(psa_hash_finish(&operation, output,
3114 sizeof(output), &output_length));
3115 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003116 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003117 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01003118
3119 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
3121 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01003122
Neil Armstrongca30a002022-02-07 11:40:23 +01003123 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 PSA_ASSERT(psa_hash_setup(&operation, alg));
3125 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3126 PSA_ASSERT(psa_hash_verify(&operation, output,
3127 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01003128
3129 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
3131 output, output_length + 1),
3132 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003133
Neil Armstrongca30a002022-02-07 11:40:23 +01003134 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 PSA_ASSERT(psa_hash_setup(&operation, alg));
3136 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3137 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
3138 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003139
3140 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
3142 output, output_length - 1),
3143 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003144
Neil Armstrongca30a002022-02-07 11:40:23 +01003145 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 PSA_ASSERT(psa_hash_setup(&operation, alg));
3147 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3148 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
3149 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003150
Gilles Peskine0a749c82019-11-28 19:33:58 +01003151 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 for (i = 0; i < output_length; i++) {
3153 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003154 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01003155
3156 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
3158 output, output_length),
3159 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003160
3161 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 PSA_ASSERT(psa_hash_setup(&operation, alg));
3163 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3164 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
3165 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003166
Gilles Peskine0a749c82019-11-28 19:33:58 +01003167 output[i] ^= 1;
3168 }
3169
3170exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 PSA_ASSERT(psa_hash_abort(&operation));
3172 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01003173}
3174/* END_CASE */
3175
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003176/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003177void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02003178{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003179 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02003180 unsigned char input[] = "";
3181 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003182 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02003183 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3184 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
3186 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003187 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02003188 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00003189 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02003190
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02003192
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003193 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 PSA_ASSERT(psa_hash_setup(&operation, alg));
3195 ASSERT_OPERATION_IS_ACTIVE(operation);
3196 TEST_EQUAL(psa_hash_setup(&operation, alg),
3197 PSA_ERROR_BAD_STATE);
3198 ASSERT_OPERATION_IS_INACTIVE(operation);
3199 PSA_ASSERT(psa_hash_abort(&operation));
3200 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003201
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003202 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3204 PSA_ERROR_BAD_STATE);
3205 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003206
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003207 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01003209 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 ASSERT_OPERATION_IS_ACTIVE(operation);
3211 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3212 PSA_ERROR_BAD_STATE);
3213 ASSERT_OPERATION_IS_INACTIVE(operation);
3214 PSA_ASSERT(psa_hash_abort(&operation));
3215 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003216
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003217 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 PSA_ASSERT(psa_hash_setup(&operation, alg));
3219 PSA_ASSERT(psa_hash_finish(&operation,
3220 hash, sizeof(hash), &hash_len));
3221 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3222 PSA_ERROR_BAD_STATE);
3223 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003224
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003225 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 TEST_EQUAL(psa_hash_verify(&operation,
3227 valid_hash, sizeof(valid_hash)),
3228 PSA_ERROR_BAD_STATE);
3229 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003230
3231 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 PSA_ASSERT(psa_hash_setup(&operation, alg));
3233 PSA_ASSERT(psa_hash_finish(&operation,
3234 hash, sizeof(hash), &hash_len));
3235 TEST_EQUAL(psa_hash_verify(&operation,
3236 valid_hash, sizeof(valid_hash)),
3237 PSA_ERROR_BAD_STATE);
3238 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003239
3240 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 PSA_ASSERT(psa_hash_setup(&operation, alg));
3242 ASSERT_OPERATION_IS_ACTIVE(operation);
3243 PSA_ASSERT(psa_hash_verify(&operation,
3244 valid_hash, sizeof(valid_hash)));
3245 ASSERT_OPERATION_IS_INACTIVE(operation);
3246 TEST_EQUAL(psa_hash_verify(&operation,
3247 valid_hash, sizeof(valid_hash)),
3248 PSA_ERROR_BAD_STATE);
3249 ASSERT_OPERATION_IS_INACTIVE(operation);
3250 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003251
3252 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 TEST_EQUAL(psa_hash_finish(&operation,
3254 hash, sizeof(hash), &hash_len),
3255 PSA_ERROR_BAD_STATE);
3256 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003257
3258 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 PSA_ASSERT(psa_hash_setup(&operation, alg));
3260 PSA_ASSERT(psa_hash_finish(&operation,
3261 hash, sizeof(hash), &hash_len));
3262 TEST_EQUAL(psa_hash_finish(&operation,
3263 hash, sizeof(hash), &hash_len),
3264 PSA_ERROR_BAD_STATE);
3265 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003266
3267 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 PSA_ASSERT(psa_hash_setup(&operation, alg));
3269 PSA_ASSERT(psa_hash_verify(&operation,
3270 valid_hash, sizeof(valid_hash)));
3271 TEST_EQUAL(psa_hash_finish(&operation,
3272 hash, sizeof(hash), &hash_len),
3273 PSA_ERROR_BAD_STATE);
3274 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003275
3276exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02003278}
3279/* END_CASE */
3280
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003281/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003282void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03003283{
3284 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003285 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3286 * appended to it */
3287 unsigned char hash[] = {
3288 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3289 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
3291 };
3292 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003293 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003294
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03003296
itayzafrir27e69452018-11-01 14:26:34 +02003297 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 PSA_ASSERT(psa_hash_setup(&operation, alg));
3299 ASSERT_OPERATION_IS_ACTIVE(operation);
3300 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
3301 PSA_ERROR_INVALID_SIGNATURE);
3302 ASSERT_OPERATION_IS_INACTIVE(operation);
3303 PSA_ASSERT(psa_hash_abort(&operation));
3304 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03003305
itayzafrir27e69452018-11-01 14:26:34 +02003306 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 PSA_ASSERT(psa_hash_setup(&operation, alg));
3308 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
3309 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03003310
itayzafrir27e69452018-11-01 14:26:34 +02003311 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 PSA_ASSERT(psa_hash_setup(&operation, alg));
3313 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3314 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003315
itayzafrirec93d302018-10-18 18:01:10 +03003316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003318}
3319/* END_CASE */
3320
Ronald Cronee414c72021-03-18 18:50:08 +01003321/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003322void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003323{
3324 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003325 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003327 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003328 size_t hash_len;
3329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003331
itayzafrir58028322018-10-25 10:22:01 +03003332 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 PSA_ASSERT(psa_hash_setup(&operation, alg));
3334 TEST_EQUAL(psa_hash_finish(&operation,
3335 hash, expected_size - 1, &hash_len),
3336 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003337
3338exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003340}
3341/* END_CASE */
3342
Ronald Cronee414c72021-03-18 18:50:08 +01003343/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003344void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003345{
3346 psa_algorithm_t alg = PSA_ALG_SHA_256;
3347 unsigned char hash[PSA_HASH_MAX_SIZE];
3348 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3349 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3350 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3351 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3352 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3353 size_t hash_len;
3354
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 PSA_ASSERT(psa_crypto_init());
3356 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003357
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3359 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3360 PSA_ASSERT(psa_hash_finish(&op_finished,
3361 hash, sizeof(hash), &hash_len));
3362 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3363 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003364
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3366 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3369 PSA_ASSERT(psa_hash_finish(&op_init,
3370 hash, sizeof(hash), &hash_len));
3371 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3372 PSA_ASSERT(psa_hash_finish(&op_finished,
3373 hash, sizeof(hash), &hash_len));
3374 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3375 PSA_ASSERT(psa_hash_finish(&op_aborted,
3376 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003377
3378exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 psa_hash_abort(&op_source);
3380 psa_hash_abort(&op_init);
3381 psa_hash_abort(&op_setup);
3382 psa_hash_abort(&op_finished);
3383 psa_hash_abort(&op_aborted);
3384 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003385}
3386/* END_CASE */
3387
Ronald Cronee414c72021-03-18 18:50:08 +01003388/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003389void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003390{
3391 psa_algorithm_t alg = PSA_ALG_SHA_256;
3392 unsigned char hash[PSA_HASH_MAX_SIZE];
3393 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3394 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3395 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3396 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3397 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3398 size_t hash_len;
3399
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3403 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3404 PSA_ASSERT(psa_hash_finish(&op_finished,
3405 hash, sizeof(hash), &hash_len));
3406 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3407 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3410 PSA_ASSERT(psa_hash_finish(&op_target,
3411 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3414 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3415 PSA_ERROR_BAD_STATE);
3416 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3417 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003418
3419exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 psa_hash_abort(&op_target);
3421 psa_hash_abort(&op_init);
3422 psa_hash_abort(&op_setup);
3423 psa_hash_abort(&op_finished);
3424 psa_hash_abort(&op_aborted);
3425 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003426}
3427/* END_CASE */
3428
itayzafrir58028322018-10-25 10:22:01 +03003429/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003430void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003431{
Jaeden Amero252ef282019-02-15 14:05:35 +00003432 const uint8_t input[1] = { 0 };
3433
Jaeden Amero769ce272019-01-04 11:48:03 +00003434 /* Test each valid way of initializing the object, except for `= {0}`, as
3435 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3436 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003437 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003439 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3440 psa_mac_operation_t zero;
3441
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003443
Jaeden Amero252ef282019-02-15 14:05:35 +00003444 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 TEST_EQUAL(psa_mac_update(&func,
3446 input, sizeof(input)),
3447 PSA_ERROR_BAD_STATE);
3448 TEST_EQUAL(psa_mac_update(&init,
3449 input, sizeof(input)),
3450 PSA_ERROR_BAD_STATE);
3451 TEST_EQUAL(psa_mac_update(&zero,
3452 input, sizeof(input)),
3453 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003454
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003455 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 PSA_ASSERT(psa_mac_abort(&func));
3457 PSA_ASSERT(psa_mac_abort(&init));
3458 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003459}
3460/* END_CASE */
3461
3462/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003463void mac_setup(int key_type_arg,
3464 data_t *key,
3465 int alg_arg,
3466 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003467{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003468 psa_key_type_t key_type = key_type_arg;
3469 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003470 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003471 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003472 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3473#if defined(KNOWN_SUPPORTED_MAC_ALG)
3474 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3475#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003476
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003478
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3480 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003481 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 }
3483 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003484
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003485 /* The operation object should be reusable. */
3486#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3488 smoke_test_key_data,
3489 sizeof(smoke_test_key_data),
3490 KNOWN_SUPPORTED_MAC_ALG,
3491 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003492 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 }
3494 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003495#endif
3496
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003497exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003499}
3500/* END_CASE */
3501
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003502/* 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 +01003503void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003504{
Ronald Cron5425a212020-08-04 14:58:35 +02003505 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003506 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3507 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003508 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003509 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3510 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3512 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003514 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3515 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3516 size_t sign_mac_length = 0;
3517 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3518 const uint8_t verify_mac[] = {
3519 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3520 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3522 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003523
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 PSA_ASSERT(psa_crypto_init());
3525 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3526 psa_set_key_algorithm(&attributes, alg);
3527 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003528
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3530 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003531
Jaeden Amero252ef282019-02-15 14:05:35 +00003532 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3534 PSA_ERROR_BAD_STATE);
3535 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003536
3537 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003538 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3539 &sign_mac_length),
3540 PSA_ERROR_BAD_STATE);
3541 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003542
3543 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 TEST_EQUAL(psa_mac_verify_finish(&operation,
3545 verify_mac, sizeof(verify_mac)),
3546 PSA_ERROR_BAD_STATE);
3547 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003548
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003549 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3551 ASSERT_OPERATION_IS_ACTIVE(operation);
3552 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3553 PSA_ERROR_BAD_STATE);
3554 ASSERT_OPERATION_IS_INACTIVE(operation);
3555 PSA_ASSERT(psa_mac_abort(&operation));
3556 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003557
Jaeden Amero252ef282019-02-15 14:05:35 +00003558 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3560 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3561 PSA_ASSERT(psa_mac_sign_finish(&operation,
3562 sign_mac, sizeof(sign_mac),
3563 &sign_mac_length));
3564 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3565 PSA_ERROR_BAD_STATE);
3566 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003567
3568 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003569 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3570 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3571 PSA_ASSERT(psa_mac_verify_finish(&operation,
3572 verify_mac, sizeof(verify_mac)));
3573 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3574 PSA_ERROR_BAD_STATE);
3575 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003576
3577 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003578 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3579 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3580 PSA_ASSERT(psa_mac_sign_finish(&operation,
3581 sign_mac, sizeof(sign_mac),
3582 &sign_mac_length));
3583 TEST_EQUAL(psa_mac_sign_finish(&operation,
3584 sign_mac, sizeof(sign_mac),
3585 &sign_mac_length),
3586 PSA_ERROR_BAD_STATE);
3587 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003588
3589 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3591 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3592 PSA_ASSERT(psa_mac_verify_finish(&operation,
3593 verify_mac, sizeof(verify_mac)));
3594 TEST_EQUAL(psa_mac_verify_finish(&operation,
3595 verify_mac, sizeof(verify_mac)),
3596 PSA_ERROR_BAD_STATE);
3597 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003598
3599 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3601 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3602 ASSERT_OPERATION_IS_ACTIVE(operation);
3603 TEST_EQUAL(psa_mac_verify_finish(&operation,
3604 verify_mac, sizeof(verify_mac)),
3605 PSA_ERROR_BAD_STATE);
3606 ASSERT_OPERATION_IS_INACTIVE(operation);
3607 PSA_ASSERT(psa_mac_abort(&operation));
3608 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003609
3610 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003611 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3612 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3613 ASSERT_OPERATION_IS_ACTIVE(operation);
3614 TEST_EQUAL(psa_mac_sign_finish(&operation,
3615 sign_mac, sizeof(sign_mac),
3616 &sign_mac_length),
3617 PSA_ERROR_BAD_STATE);
3618 ASSERT_OPERATION_IS_INACTIVE(operation);
3619 PSA_ASSERT(psa_mac_abort(&operation));
3620 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003621
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003623
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003624exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003625 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003626}
3627/* END_CASE */
3628
3629/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003630void mac_sign_verify_multi(int key_type_arg,
3631 data_t *key_data,
3632 int alg_arg,
3633 data_t *input,
3634 int is_verify,
3635 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003636{
3637 size_t data_part_len = 0;
3638
Gilles Peskine449bd832023-01-11 14:50:10 +01003639 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003640 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003641 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003642
Gilles Peskine449bd832023-01-11 14:50:10 +01003643 if (mac_multipart_internal_func(key_type_arg, key_data,
3644 alg_arg,
3645 input, data_part_len,
3646 expected_mac,
3647 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003648 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003649 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003650
3651 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003652 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003653
Gilles Peskine449bd832023-01-11 14:50:10 +01003654 if (mac_multipart_internal_func(key_type_arg, key_data,
3655 alg_arg,
3656 input, data_part_len,
3657 expected_mac,
3658 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003659 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003660 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003661 }
3662
3663 /* Goto is required to silence warnings about unused labels, as we
3664 * don't actually do any test assertions in this function. */
3665 goto exit;
3666}
3667/* END_CASE */
3668
3669/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003670void mac_sign(int key_type_arg,
3671 data_t *key_data,
3672 int alg_arg,
3673 data_t *input,
3674 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003675{
Ronald Cron5425a212020-08-04 14:58:35 +02003676 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003677 psa_key_type_t key_type = key_type_arg;
3678 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003679 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003681 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003682 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003683 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003684 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003685 const size_t output_sizes_to_test[] = {
3686 0,
3687 1,
3688 expected_mac->len - 1,
3689 expected_mac->len,
3690 expected_mac->len + 1,
3691 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003692
Gilles Peskine449bd832023-01-11 14:50:10 +01003693 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003694 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003695 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003696
Gilles Peskine449bd832023-01-11 14:50:10 +01003697 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003698
Gilles Peskine449bd832023-01-11 14:50:10 +01003699 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3700 psa_set_key_algorithm(&attributes, alg);
3701 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003702
Gilles Peskine449bd832023-01-11 14:50:10 +01003703 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3704 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003705
Gilles Peskine449bd832023-01-11 14:50:10 +01003706 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003707 const size_t output_size = output_sizes_to_test[i];
3708 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003709 (output_size >= expected_mac->len ? PSA_SUCCESS :
3710 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003711
Gilles Peskine449bd832023-01-11 14:50:10 +01003712 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003713 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003714
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003715 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003716 TEST_EQUAL(psa_mac_compute(key, alg,
3717 input->x, input->len,
3718 actual_mac, output_size, &mac_length),
3719 expected_status);
3720 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003721 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003722 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003723 }
3724
Gilles Peskine449bd832023-01-11 14:50:10 +01003725 if (output_size > 0) {
3726 memset(actual_mac, 0, output_size);
3727 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003728
3729 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003730 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3731 PSA_ASSERT(psa_mac_update(&operation,
3732 input->x, input->len));
3733 TEST_EQUAL(psa_mac_sign_finish(&operation,
3734 actual_mac, output_size,
3735 &mac_length),
3736 expected_status);
3737 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003738
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003740 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003741 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003742 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003743 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003744 actual_mac = NULL;
3745 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003746
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003747exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 psa_mac_abort(&operation);
3749 psa_destroy_key(key);
3750 PSA_DONE();
3751 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003752}
3753/* END_CASE */
3754
3755/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003756void mac_verify(int key_type_arg,
3757 data_t *key_data,
3758 int alg_arg,
3759 data_t *input,
3760 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003761{
Ronald Cron5425a212020-08-04 14:58:35 +02003762 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003763 psa_key_type_t key_type = key_type_arg;
3764 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003765 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003767 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003768
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003770
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003772
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3774 psa_set_key_algorithm(&attributes, alg);
3775 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003776
Gilles Peskine449bd832023-01-11 14:50:10 +01003777 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3778 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003779
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003780 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003781 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3782 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003783
3784 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3786 PSA_ASSERT(psa_mac_update(&operation,
3787 input->x, input->len));
3788 PSA_ASSERT(psa_mac_verify_finish(&operation,
3789 expected_mac->x,
3790 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003791
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003792 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 TEST_EQUAL(psa_mac_verify(key, alg,
3794 input->x, input->len,
3795 expected_mac->x,
3796 expected_mac->len - 1),
3797 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003798
3799 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003800 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3801 PSA_ASSERT(psa_mac_update(&operation,
3802 input->x, input->len));
3803 TEST_EQUAL(psa_mac_verify_finish(&operation,
3804 expected_mac->x,
3805 expected_mac->len - 1),
3806 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003807
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003808 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003809 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3811 TEST_EQUAL(psa_mac_verify(key, alg,
3812 input->x, input->len,
3813 perturbed_mac, expected_mac->len + 1),
3814 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003815
3816 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3818 PSA_ASSERT(psa_mac_update(&operation,
3819 input->x, input->len));
3820 TEST_EQUAL(psa_mac_verify_finish(&operation,
3821 perturbed_mac,
3822 expected_mac->len + 1),
3823 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003824
3825 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 for (size_t i = 0; i < expected_mac->len; i++) {
3827 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003828 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003829
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 TEST_EQUAL(psa_mac_verify(key, alg,
3831 input->x, input->len,
3832 perturbed_mac, expected_mac->len),
3833 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003834
Gilles Peskine449bd832023-01-11 14:50:10 +01003835 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3836 PSA_ASSERT(psa_mac_update(&operation,
3837 input->x, input->len));
3838 TEST_EQUAL(psa_mac_verify_finish(&operation,
3839 perturbed_mac,
3840 expected_mac->len),
3841 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003842 perturbed_mac[i] ^= 1;
3843 }
3844
Gilles Peskine8c9def32018-02-08 10:02:12 +01003845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003846 psa_mac_abort(&operation);
3847 psa_destroy_key(key);
3848 PSA_DONE();
3849 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003850}
3851/* END_CASE */
3852
3853/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003854void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003855{
Jaeden Ameroab439972019-02-15 14:12:05 +00003856 const uint8_t input[1] = { 0 };
3857 unsigned char output[1] = { 0 };
3858 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003859 /* Test each valid way of initializing the object, except for `= {0}`, as
3860 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3861 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003862 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003863 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003864 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3865 psa_cipher_operation_t zero;
3866
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003868
Jaeden Ameroab439972019-02-15 14:12:05 +00003869 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 TEST_EQUAL(psa_cipher_update(&func,
3871 input, sizeof(input),
3872 output, sizeof(output),
3873 &output_length),
3874 PSA_ERROR_BAD_STATE);
3875 TEST_EQUAL(psa_cipher_update(&init,
3876 input, sizeof(input),
3877 output, sizeof(output),
3878 &output_length),
3879 PSA_ERROR_BAD_STATE);
3880 TEST_EQUAL(psa_cipher_update(&zero,
3881 input, sizeof(input),
3882 output, sizeof(output),
3883 &output_length),
3884 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003885
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003886 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003887 PSA_ASSERT(psa_cipher_abort(&func));
3888 PSA_ASSERT(psa_cipher_abort(&init));
3889 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003890}
3891/* END_CASE */
3892
3893/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003894void cipher_setup(int key_type_arg,
3895 data_t *key,
3896 int alg_arg,
3897 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003898{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003899 psa_key_type_t key_type = key_type_arg;
3900 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003901 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003902 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003903 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003904#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003905 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3906#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003907
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003909
Gilles Peskine449bd832023-01-11 14:50:10 +01003910 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3911 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003912 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 }
3914 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003915
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003916 /* The operation object should be reusable. */
3917#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3919 smoke_test_key_data,
3920 sizeof(smoke_test_key_data),
3921 KNOWN_SUPPORTED_CIPHER_ALG,
3922 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003923 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 }
3925 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003926#endif
3927
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003928exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 psa_cipher_abort(&operation);
3930 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003931}
3932/* END_CASE */
3933
Ronald Cronee414c72021-03-18 18:50:08 +01003934/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003935void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003936{
Ronald Cron5425a212020-08-04 14:58:35 +02003937 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003938 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3939 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003941 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003942 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003943 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003944 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 0xaa, 0xaa, 0xaa, 0xaa
3946 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003947 const uint8_t text[] = {
3948 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 0xbb, 0xbb, 0xbb, 0xbb
3950 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003951 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003952 size_t length = 0;
3953
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 PSA_ASSERT(psa_crypto_init());
3955 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3956 psa_set_key_algorithm(&attributes, alg);
3957 psa_set_key_type(&attributes, key_type);
3958 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3959 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003960
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003961 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3963 ASSERT_OPERATION_IS_ACTIVE(operation);
3964 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3965 PSA_ERROR_BAD_STATE);
3966 ASSERT_OPERATION_IS_INACTIVE(operation);
3967 PSA_ASSERT(psa_cipher_abort(&operation));
3968 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003969
3970 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3972 ASSERT_OPERATION_IS_ACTIVE(operation);
3973 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3974 PSA_ERROR_BAD_STATE);
3975 ASSERT_OPERATION_IS_INACTIVE(operation);
3976 PSA_ASSERT(psa_cipher_abort(&operation));
3977 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003978
Jaeden Ameroab439972019-02-15 14:12:05 +00003979 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3981 buffer, sizeof(buffer),
3982 &length),
3983 PSA_ERROR_BAD_STATE);
3984 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003985
3986 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3988 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3989 buffer, sizeof(buffer),
3990 &length));
3991 ASSERT_OPERATION_IS_ACTIVE(operation);
3992 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3993 buffer, sizeof(buffer),
3994 &length),
3995 PSA_ERROR_BAD_STATE);
3996 ASSERT_OPERATION_IS_INACTIVE(operation);
3997 PSA_ASSERT(psa_cipher_abort(&operation));
3998 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003999
4000 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4002 PSA_ASSERT(psa_cipher_set_iv(&operation,
4003 iv, sizeof(iv)));
4004 TEST_EQUAL(psa_cipher_generate_iv(&operation,
4005 buffer, sizeof(buffer),
4006 &length),
4007 PSA_ERROR_BAD_STATE);
4008 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004009
4010 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 TEST_EQUAL(psa_cipher_set_iv(&operation,
4012 iv, sizeof(iv)),
4013 PSA_ERROR_BAD_STATE);
4014 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004015
4016 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4018 PSA_ASSERT(psa_cipher_set_iv(&operation,
4019 iv, sizeof(iv)));
4020 ASSERT_OPERATION_IS_ACTIVE(operation);
4021 TEST_EQUAL(psa_cipher_set_iv(&operation,
4022 iv, sizeof(iv)),
4023 PSA_ERROR_BAD_STATE);
4024 ASSERT_OPERATION_IS_INACTIVE(operation);
4025 PSA_ASSERT(psa_cipher_abort(&operation));
4026 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004027
4028 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4030 PSA_ASSERT(psa_cipher_generate_iv(&operation,
4031 buffer, sizeof(buffer),
4032 &length));
4033 TEST_EQUAL(psa_cipher_set_iv(&operation,
4034 iv, sizeof(iv)),
4035 PSA_ERROR_BAD_STATE);
4036 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004037
4038 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 TEST_EQUAL(psa_cipher_update(&operation,
4040 text, sizeof(text),
4041 buffer, sizeof(buffer),
4042 &length),
4043 PSA_ERROR_BAD_STATE);
4044 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004045
4046 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4048 ASSERT_OPERATION_IS_ACTIVE(operation);
4049 TEST_EQUAL(psa_cipher_update(&operation,
4050 text, sizeof(text),
4051 buffer, sizeof(buffer),
4052 &length),
4053 PSA_ERROR_BAD_STATE);
4054 ASSERT_OPERATION_IS_INACTIVE(operation);
4055 PSA_ASSERT(psa_cipher_abort(&operation));
4056 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004057
4058 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4060 PSA_ASSERT(psa_cipher_set_iv(&operation,
4061 iv, sizeof(iv)));
4062 PSA_ASSERT(psa_cipher_finish(&operation,
4063 buffer, sizeof(buffer), &length));
4064 TEST_EQUAL(psa_cipher_update(&operation,
4065 text, sizeof(text),
4066 buffer, sizeof(buffer),
4067 &length),
4068 PSA_ERROR_BAD_STATE);
4069 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004070
4071 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 TEST_EQUAL(psa_cipher_finish(&operation,
4073 buffer, sizeof(buffer), &length),
4074 PSA_ERROR_BAD_STATE);
4075 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004076
4077 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00004079 /* Not calling update means we are encrypting an empty buffer, which is OK
4080 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 ASSERT_OPERATION_IS_ACTIVE(operation);
4082 TEST_EQUAL(psa_cipher_finish(&operation,
4083 buffer, sizeof(buffer), &length),
4084 PSA_ERROR_BAD_STATE);
4085 ASSERT_OPERATION_IS_INACTIVE(operation);
4086 PSA_ASSERT(psa_cipher_abort(&operation));
4087 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004088
4089 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4091 PSA_ASSERT(psa_cipher_set_iv(&operation,
4092 iv, sizeof(iv)));
4093 PSA_ASSERT(psa_cipher_finish(&operation,
4094 buffer, sizeof(buffer), &length));
4095 TEST_EQUAL(psa_cipher_finish(&operation,
4096 buffer, sizeof(buffer), &length),
4097 PSA_ERROR_BAD_STATE);
4098 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004099
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02004101
Jaeden Ameroab439972019-02-15 14:12:05 +00004102exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 psa_cipher_abort(&operation);
4104 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004105}
4106/* END_CASE */
4107
4108/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004109void cipher_encrypt_fail(int alg_arg,
4110 int key_type_arg,
4111 data_t *key_data,
4112 data_t *input,
4113 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004114{
Ronald Cron5425a212020-08-04 14:58:35 +02004115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004116 psa_status_t status;
4117 psa_key_type_t key_type = key_type_arg;
4118 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02004119 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004121 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
4122 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004123 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004124 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004125 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004126 size_t function_output_length;
4127 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4129
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 if (PSA_ERROR_BAD_STATE != expected_status) {
4131 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004132
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4134 psa_set_key_algorithm(&attributes, alg);
4135 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004136
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
4138 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004139 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4142 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004143 }
4144
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004145 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
4147 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004148
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004150
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004151 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 status = psa_cipher_encrypt_setup(&operation, key, alg);
4153 if (status == PSA_SUCCESS) {
4154 if (alg != PSA_ALG_ECB_NO_PADDING) {
4155 PSA_ASSERT(psa_cipher_generate_iv(&operation,
4156 iv, iv_size,
4157 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004158 }
4159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 status = psa_cipher_update(&operation, input->x, input->len,
4161 output, output_buffer_size,
4162 &function_output_length);
4163 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004164 output_length += function_output_length;
4165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 status = psa_cipher_finish(&operation, output + output_length,
4167 output_buffer_size - output_length,
4168 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 TEST_EQUAL(status, expected_status);
4171 } else {
4172 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004173 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 } else {
4175 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004176 }
4177
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004178exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 psa_cipher_abort(&operation);
4180 mbedtls_free(output);
4181 psa_destroy_key(key);
4182 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004183}
4184/* END_CASE */
4185
4186/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004187void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
4188 data_t *input, int iv_length,
4189 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02004190{
4191 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4192 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4194 size_t output_buffer_size = 0;
4195 unsigned char *output = NULL;
4196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004198 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02004201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4203 psa_set_key_algorithm(&attributes, alg);
4204 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02004205
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4207 &key));
4208 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4209 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
4210 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02004211
4212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 psa_cipher_abort(&operation);
4214 mbedtls_free(output);
4215 psa_destroy_key(key);
4216 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02004217}
4218/* END_CASE */
4219
4220/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004221void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
4222 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004223{
4224 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4225 psa_key_type_t key_type = key_type_arg;
4226 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004227 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4228 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004229 unsigned char *output = NULL;
4230 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02004231 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004235
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004236 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 TEST_LE_U(ciphertext->len,
4238 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
4239 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
4240 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
4241 TEST_LE_U(plaintext->len,
4242 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
4243 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
4244 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004245
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004246
4247 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 psa_set_key_usage_flags(&attributes,
4249 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4250 psa_set_key_algorithm(&attributes, alg);
4251 psa_set_key_type(&attributes, key_type);
4252 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4253 &key));
4254 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
4255 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004256 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004257
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004258 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4260 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4261 PSA_ERROR_BAD_STATE);
4262 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4263 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4264 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004265
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004266 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4268 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4269 &length),
4270 PSA_ERROR_BAD_STATE);
4271 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4272 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4273 &length),
4274 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004275
Gilles Peskine286c3142022-04-20 17:09:38 +02004276 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004278 output_length = 0;
4279 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 PSA_ASSERT(psa_cipher_update(&operation,
4281 plaintext->x, plaintext->len,
4282 output, output_buffer_size,
4283 &length));
4284 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004285 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 PSA_ASSERT(psa_cipher_finish(&operation,
4287 mbedtls_buffer_offset(output, output_length),
4288 output_buffer_size - output_length,
4289 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004290 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004291 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004292 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004293
Gilles Peskine286c3142022-04-20 17:09:38 +02004294 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004296 output_length = 0;
4297 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 PSA_ASSERT(psa_cipher_update(&operation,
4299 ciphertext->x, ciphertext->len,
4300 output, output_buffer_size,
4301 &length));
4302 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004303 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 PSA_ASSERT(psa_cipher_finish(&operation,
4305 mbedtls_buffer_offset(output, output_length),
4306 output_buffer_size - output_length,
4307 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004308 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004309 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004310 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004311
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004312 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004313 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4315 output, output_buffer_size,
4316 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004317 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004318 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004319
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004320 /* One-shot decryption */
4321 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4323 output, output_buffer_size,
4324 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004325 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004326 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004327
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004328exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 PSA_ASSERT(psa_cipher_abort(&operation));
4330 mbedtls_free(output);
4331 psa_cipher_abort(&operation);
4332 psa_destroy_key(key);
4333 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004334}
4335/* END_CASE */
4336
4337/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004338void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004339{
4340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4341 psa_algorithm_t alg = alg_arg;
4342 psa_key_type_t key_type = key_type_arg;
4343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4344 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4345 psa_status_t status;
4346
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4350 psa_set_key_algorithm(&attributes, alg);
4351 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004352
4353 /* Usage of either of these two size macros would cause divide by zero
4354 * with incorrect key types previously. Input length should be irrelevant
4355 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4357 0);
4358 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004359
4360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4362 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004363
4364 /* Should fail due to invalid alg type (to support invalid key type).
4365 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004369
4370exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 psa_cipher_abort(&operation);
4372 psa_destroy_key(key);
4373 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004374}
4375/* END_CASE */
4376
4377/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004378void cipher_encrypt_validation(int alg_arg,
4379 int key_type_arg,
4380 data_t *key_data,
4381 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004382{
4383 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4384 psa_key_type_t key_type = key_type_arg;
4385 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004387 unsigned char *output1 = NULL;
4388 size_t output1_buffer_size = 0;
4389 size_t output1_length = 0;
4390 unsigned char *output2 = NULL;
4391 size_t output2_buffer_size = 0;
4392 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004393 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004394 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004398
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4400 psa_set_key_algorithm(&attributes, alg);
4401 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4404 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4405 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004406 TEST_CALLOC(output1, output1_buffer_size);
4407 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4410 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004411
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004412 /* The one-shot cipher encryption uses generated iv so validating
4413 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4415 output1_buffer_size, &output1_length));
4416 TEST_LE_U(output1_length,
4417 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4418 TEST_LE_U(output1_length,
4419 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004420
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4422 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 PSA_ASSERT(psa_cipher_update(&operation,
4425 input->x, input->len,
4426 output2, output2_buffer_size,
4427 &function_output_length));
4428 TEST_LE_U(function_output_length,
4429 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4430 TEST_LE_U(function_output_length,
4431 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004432 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004433
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 PSA_ASSERT(psa_cipher_finish(&operation,
4435 output2 + output2_length,
4436 output2_buffer_size - output2_length,
4437 &function_output_length));
4438 TEST_LE_U(function_output_length,
4439 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4440 TEST_LE_U(function_output_length,
4441 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004442 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004445 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004446 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004447
Gilles Peskine50e586b2018-06-08 14:28:46 +02004448exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 psa_cipher_abort(&operation);
4450 mbedtls_free(output1);
4451 mbedtls_free(output2);
4452 psa_destroy_key(key);
4453 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004454}
4455/* END_CASE */
4456
4457/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004458void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4459 data_t *key_data, data_t *iv,
4460 data_t *input,
4461 int first_part_size_arg,
4462 int output1_length_arg, int output2_length_arg,
4463 data_t *expected_output,
4464 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004465{
Ronald Cron5425a212020-08-04 14:58:35 +02004466 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004467 psa_key_type_t key_type = key_type_arg;
4468 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004469 psa_status_t status;
4470 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004471 size_t first_part_size = first_part_size_arg;
4472 size_t output1_length = output1_length_arg;
4473 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004474 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004475 size_t output_buffer_size = 0;
4476 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004477 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004478 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004482
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4484 psa_set_key_algorithm(&attributes, alg);
4485 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004486
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4488 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004491
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 if (iv->len > 0) {
4493 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004494 }
4495
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4497 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004498 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004499
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 TEST_LE_U(first_part_size, input->len);
4501 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4502 output, output_buffer_size,
4503 &function_output_length));
4504 TEST_ASSERT(function_output_length == output1_length);
4505 TEST_LE_U(function_output_length,
4506 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4507 TEST_LE_U(function_output_length,
4508 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004509 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 if (first_part_size < input->len) {
4512 PSA_ASSERT(psa_cipher_update(&operation,
4513 input->x + first_part_size,
4514 input->len - first_part_size,
4515 (output_buffer_size == 0 ? NULL :
4516 output + total_output_length),
4517 output_buffer_size - total_output_length,
4518 &function_output_length));
4519 TEST_ASSERT(function_output_length == output2_length);
4520 TEST_LE_U(function_output_length,
4521 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4522 alg,
4523 input->len - first_part_size));
4524 TEST_LE_U(function_output_length,
4525 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004526 total_output_length += function_output_length;
4527 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 status = psa_cipher_finish(&operation,
4530 (output_buffer_size == 0 ? NULL :
4531 output + total_output_length),
4532 output_buffer_size - total_output_length,
4533 &function_output_length);
4534 TEST_LE_U(function_output_length,
4535 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4536 TEST_LE_U(function_output_length,
4537 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004538 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 if (expected_status == PSA_SUCCESS) {
4542 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004543
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004544 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004545 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004546 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004547
4548exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 psa_cipher_abort(&operation);
4550 mbedtls_free(output);
4551 psa_destroy_key(key);
4552 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004553}
4554/* END_CASE */
4555
4556/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004557void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4558 data_t *key_data, data_t *iv,
4559 data_t *input,
4560 int first_part_size_arg,
4561 int output1_length_arg, int output2_length_arg,
4562 data_t *expected_output,
4563 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004564{
Ronald Cron5425a212020-08-04 14:58:35 +02004565 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004566 psa_key_type_t key_type = key_type_arg;
4567 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004568 psa_status_t status;
4569 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004570 size_t first_part_size = first_part_size_arg;
4571 size_t output1_length = output1_length_arg;
4572 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004573 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004574 size_t output_buffer_size = 0;
4575 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004576 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004577 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004578 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4583 psa_set_key_algorithm(&attributes, alg);
4584 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4587 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 if (iv->len > 0) {
4592 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004593 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4596 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004597 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 TEST_LE_U(first_part_size, input->len);
4600 PSA_ASSERT(psa_cipher_update(&operation,
4601 input->x, first_part_size,
4602 output, output_buffer_size,
4603 &function_output_length));
4604 TEST_ASSERT(function_output_length == output1_length);
4605 TEST_LE_U(function_output_length,
4606 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4607 TEST_LE_U(function_output_length,
4608 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004609 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 if (first_part_size < input->len) {
4612 PSA_ASSERT(psa_cipher_update(&operation,
4613 input->x + first_part_size,
4614 input->len - first_part_size,
4615 (output_buffer_size == 0 ? NULL :
4616 output + total_output_length),
4617 output_buffer_size - total_output_length,
4618 &function_output_length));
4619 TEST_ASSERT(function_output_length == output2_length);
4620 TEST_LE_U(function_output_length,
4621 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4622 alg,
4623 input->len - first_part_size));
4624 TEST_LE_U(function_output_length,
4625 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004626 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004627 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 status = psa_cipher_finish(&operation,
4630 (output_buffer_size == 0 ? NULL :
4631 output + total_output_length),
4632 output_buffer_size - total_output_length,
4633 &function_output_length);
4634 TEST_LE_U(function_output_length,
4635 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4636 TEST_LE_U(function_output_length,
4637 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004638 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 if (expected_status == PSA_SUCCESS) {
4642 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004643
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004644 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004645 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004646 }
4647
Gilles Peskine50e586b2018-06-08 14:28:46 +02004648exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 psa_cipher_abort(&operation);
4650 mbedtls_free(output);
4651 psa_destroy_key(key);
4652 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004653}
4654/* END_CASE */
4655
Gilles Peskine50e586b2018-06-08 14:28:46 +02004656/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004657void cipher_decrypt_fail(int alg_arg,
4658 int key_type_arg,
4659 data_t *key_data,
4660 data_t *iv,
4661 data_t *input_arg,
4662 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004663{
4664 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4665 psa_status_t status;
4666 psa_key_type_t key_type = key_type_arg;
4667 psa_algorithm_t alg = alg_arg;
4668 psa_status_t expected_status = expected_status_arg;
4669 unsigned char *input = NULL;
4670 size_t input_buffer_size = 0;
4671 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004672 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004673 size_t output_buffer_size = 0;
4674 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004675 size_t function_output_length;
4676 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 if (PSA_ERROR_BAD_STATE != expected_status) {
4680 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4683 psa_set_key_algorithm(&attributes, alg);
4684 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4687 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004688 }
4689
4690 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4692 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004693 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 memcpy(input, iv->x, iv->len);
4695 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004696 }
4697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004699 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004700
Neil Armstrong66a479f2022-02-07 15:41:19 +01004701 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4703 output_buffer_size, &output_length);
4704 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004705
Neil Armstrong66a479f2022-02-07 15:41:19 +01004706 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 status = psa_cipher_decrypt_setup(&operation, key, alg);
4708 if (status == PSA_SUCCESS) {
4709 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4710 input_arg->len) +
4711 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004712 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 if (iv->len > 0) {
4715 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if (status != PSA_SUCCESS) {
4718 TEST_EQUAL(status, expected_status);
4719 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004720 }
4721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 if (status == PSA_SUCCESS) {
4723 status = psa_cipher_update(&operation,
4724 input_arg->x, input_arg->len,
4725 output_multi, output_buffer_size,
4726 &function_output_length);
4727 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004728 output_length = function_output_length;
4729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 status = psa_cipher_finish(&operation,
4731 output_multi + output_length,
4732 output_buffer_size - output_length,
4733 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 TEST_EQUAL(status, expected_status);
4736 } else {
4737 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004738 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 } else {
4740 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004741 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 } else {
4743 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004744 }
4745
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004746exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 psa_cipher_abort(&operation);
4748 mbedtls_free(input);
4749 mbedtls_free(output);
4750 mbedtls_free(output_multi);
4751 psa_destroy_key(key);
4752 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004753}
4754/* END_CASE */
4755
4756/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004757void cipher_decrypt(int alg_arg,
4758 int key_type_arg,
4759 data_t *key_data,
4760 data_t *iv,
4761 data_t *input_arg,
4762 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004763{
4764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4765 psa_key_type_t key_type = key_type_arg;
4766 psa_algorithm_t alg = alg_arg;
4767 unsigned char *input = NULL;
4768 size_t input_buffer_size = 0;
4769 unsigned char *output = NULL;
4770 size_t output_buffer_size = 0;
4771 size_t output_length = 0;
4772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4773
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004775
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4777 psa_set_key_algorithm(&attributes, alg);
4778 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004779
4780 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4782 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004783 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 memcpy(input, iv->x, iv->len);
4785 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004786 }
4787
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004789 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4792 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4795 output_buffer_size, &output_length));
4796 TEST_LE_U(output_length,
4797 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4798 TEST_LE_U(output_length,
4799 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004800
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004801 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004802 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004803exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 mbedtls_free(input);
4805 mbedtls_free(output);
4806 psa_destroy_key(key);
4807 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004808}
4809/* END_CASE */
4810
4811/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004812void cipher_verify_output(int alg_arg,
4813 int key_type_arg,
4814 data_t *key_data,
4815 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004816{
Ronald Cron5425a212020-08-04 14:58:35 +02004817 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004818 psa_key_type_t key_type = key_type_arg;
4819 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004820 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004821 size_t output1_size = 0;
4822 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004823 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004824 size_t output2_size = 0;
4825 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004826 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004827
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004829
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4831 psa_set_key_algorithm(&attributes, alg);
4832 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4835 &key));
4836 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004837 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4840 output1, output1_size,
4841 &output1_length));
4842 TEST_LE_U(output1_length,
4843 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4844 TEST_LE_U(output1_length,
4845 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004846
4847 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004848 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4851 output2, output2_size,
4852 &output2_length));
4853 TEST_LE_U(output2_length,
4854 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4855 TEST_LE_U(output2_length,
4856 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004857
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004858 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004859
4860exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 mbedtls_free(output1);
4862 mbedtls_free(output2);
4863 psa_destroy_key(key);
4864 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004865}
4866/* END_CASE */
4867
4868/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004869void cipher_verify_output_multipart(int alg_arg,
4870 int key_type_arg,
4871 data_t *key_data,
4872 data_t *input,
4873 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004874{
Ronald Cron5425a212020-08-04 14:58:35 +02004875 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004876 psa_key_type_t key_type = key_type_arg;
4877 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004878 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004880 size_t iv_size = 16;
4881 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004882 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004883 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004884 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004885 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004886 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004887 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004888 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004889 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4890 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004892
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4896 psa_set_key_algorithm(&attributes, alg);
4897 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004898
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4900 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4903 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004904
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (alg != PSA_ALG_ECB_NO_PADDING) {
4906 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4907 iv, iv_size,
4908 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004909 }
4910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4912 TEST_LE_U(output1_buffer_size,
4913 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004914 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4919 output1, output1_buffer_size,
4920 &function_output_length));
4921 TEST_LE_U(function_output_length,
4922 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4923 TEST_LE_U(function_output_length,
4924 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004925 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 PSA_ASSERT(psa_cipher_update(&operation1,
4928 input->x + first_part_size,
4929 input->len - first_part_size,
David Horstmannb8dc2452024-02-06 17:03:13 +00004930 output1 + output1_length,
4931 output1_buffer_size - output1_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 &function_output_length));
4933 TEST_LE_U(function_output_length,
4934 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4935 alg,
4936 input->len - first_part_size));
4937 TEST_LE_U(function_output_length,
4938 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004939 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004940
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 PSA_ASSERT(psa_cipher_finish(&operation1,
4942 output1 + output1_length,
4943 output1_buffer_size - output1_length,
4944 &function_output_length));
4945 TEST_LE_U(function_output_length,
4946 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4947 TEST_LE_U(function_output_length,
4948 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004949 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004952
Gilles Peskine048b7f02018-06-08 14:20:49 +02004953 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 TEST_LE_U(output2_buffer_size,
4955 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4956 TEST_LE_U(output2_buffer_size,
4957 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004958 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 if (iv_length > 0) {
4961 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4962 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004963 }
Moran Pekerded84402018-06-06 16:36:50 +03004964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4966 output2, output2_buffer_size,
4967 &function_output_length));
4968 TEST_LE_U(function_output_length,
4969 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4970 TEST_LE_U(function_output_length,
4971 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004972 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004973
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 PSA_ASSERT(psa_cipher_update(&operation2,
4975 output1 + first_part_size,
4976 output1_length - first_part_size,
David Horstmannb8dc2452024-02-06 17:03:13 +00004977 output2 + output2_length,
4978 output2_buffer_size - output2_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 &function_output_length));
4980 TEST_LE_U(function_output_length,
4981 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4982 alg,
4983 output1_length - first_part_size));
4984 TEST_LE_U(function_output_length,
4985 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004986 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 PSA_ASSERT(psa_cipher_finish(&operation2,
4989 output2 + output2_length,
4990 output2_buffer_size - output2_length,
4991 &function_output_length));
4992 TEST_LE_U(function_output_length,
4993 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4994 TEST_LE_U(function_output_length,
4995 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004996 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004997
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004999
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005000 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02005001
5002exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 psa_cipher_abort(&operation1);
5004 psa_cipher_abort(&operation2);
5005 mbedtls_free(output1);
5006 mbedtls_free(output2);
5007 psa_destroy_key(key);
5008 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02005009}
5010/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02005011
Gilles Peskine20035e32018-02-03 22:44:14 +01005012/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005013void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
5014 int alg_arg,
5015 data_t *nonce,
5016 data_t *additional_data,
5017 data_t *input_data,
5018 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02005019{
Ronald Cron5425a212020-08-04 14:58:35 +02005020 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005021 psa_key_type_t key_type = key_type_arg;
5022 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01005023 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005024 unsigned char *output_data = NULL;
5025 size_t output_size = 0;
5026 size_t output_length = 0;
5027 unsigned char *output_data2 = NULL;
5028 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01005029 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02005030 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02005034
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5036 psa_set_key_algorithm(&attributes, alg);
5037 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005038
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5040 &key));
5041 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5042 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005043
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
5045 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005046 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
5047 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
5049 expected_result != PSA_ERROR_NOT_SUPPORTED) {
5050 TEST_EQUAL(output_size,
5051 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
5052 TEST_LE_U(output_size,
5053 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01005054 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005055 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005056
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 status = psa_aead_encrypt(key, alg,
5058 nonce->x, nonce->len,
5059 additional_data->x,
5060 additional_data->len,
5061 input_data->x, input_data->len,
5062 output_data, output_size,
5063 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01005064
5065 /* If the operation is not supported, just skip and not fail in case the
5066 * encryption involves a common limitation of cryptography hardwares and
5067 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 if (status == PSA_ERROR_NOT_SUPPORTED) {
5069 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5070 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01005071 }
5072
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005076 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005077
Gilles Peskine003a4a92019-05-14 16:09:40 +02005078 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
5079 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 TEST_EQUAL(input_data->len,
5081 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02005082
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 TEST_LE_U(input_data->len,
5084 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01005085
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 TEST_EQUAL(psa_aead_decrypt(key, alg,
5087 nonce->x, nonce->len,
5088 additional_data->x,
5089 additional_data->len,
5090 output_data, output_length,
5091 output_data2, output_length,
5092 &output_length2),
5093 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02005094
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005095 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005096 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005097 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005098
Gilles Peskinea1cac842018-06-11 19:33:02 +02005099exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 psa_destroy_key(key);
5101 mbedtls_free(output_data);
5102 mbedtls_free(output_data2);
5103 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005104}
5105/* END_CASE */
5106
5107/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005108void aead_encrypt(int key_type_arg, data_t *key_data,
5109 int alg_arg,
5110 data_t *nonce,
5111 data_t *additional_data,
5112 data_t *input_data,
5113 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02005114{
Ronald Cron5425a212020-08-04 14:58:35 +02005115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005116 psa_key_type_t key_type = key_type_arg;
5117 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01005118 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005119 unsigned char *output_data = NULL;
5120 size_t output_size = 0;
5121 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01005123 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02005126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5128 psa_set_key_algorithm(&attributes, alg);
5129 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5132 &key));
5133 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5134 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005135
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
5137 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005138 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
5139 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 TEST_EQUAL(output_size,
5141 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
5142 TEST_LE_U(output_size,
5143 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005144 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 status = psa_aead_encrypt(key, alg,
5147 nonce->x, nonce->len,
5148 additional_data->x, additional_data->len,
5149 input_data->x, input_data->len,
5150 output_data, output_size,
5151 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005152
Ronald Cron28a45ed2021-02-09 20:35:42 +01005153 /* 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->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01005159 }
Steven Cooremand588ea12021-01-11 19:36:04 +01005160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005162 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005163 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02005164
Gilles Peskinea1cac842018-06-11 19:33:02 +02005165exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 psa_destroy_key(key);
5167 mbedtls_free(output_data);
5168 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005169}
5170/* END_CASE */
5171
5172/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005173void aead_decrypt(int key_type_arg, data_t *key_data,
5174 int alg_arg,
5175 data_t *nonce,
5176 data_t *additional_data,
5177 data_t *input_data,
5178 data_t *expected_data,
5179 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02005180{
Ronald Cron5425a212020-08-04 14:58:35 +02005181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005182 psa_key_type_t key_type = key_type_arg;
5183 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01005184 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005185 unsigned char *output_data = NULL;
5186 size_t output_size = 0;
5187 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02005189 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01005190 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5195 psa_set_key_algorithm(&attributes, alg);
5196 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005197
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5199 &key));
5200 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5201 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005202
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
5204 alg);
5205 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
5206 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01005207 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
5208 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 TEST_EQUAL(output_size,
5210 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
5211 TEST_LE_U(output_size,
5212 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01005213 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005214 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005215
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 status = psa_aead_decrypt(key, alg,
5217 nonce->x, nonce->len,
5218 additional_data->x,
5219 additional_data->len,
5220 input_data->x, input_data->len,
5221 output_data, output_size,
5222 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01005223
Ronald Cron28a45ed2021-02-09 20:35:42 +01005224 /* If the operation is not supported, just skip and not fail in case the
5225 * decryption involves a common limitation of cryptography hardwares and
5226 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 if (status == PSA_ERROR_NOT_SUPPORTED) {
5228 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5229 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01005230 }
Steven Cooremand588ea12021-01-11 19:36:04 +01005231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005235 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005236 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02005238
Gilles Peskinea1cac842018-06-11 19:33:02 +02005239exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 psa_destroy_key(key);
5241 mbedtls_free(output_data);
5242 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005243}
5244/* END_CASE */
5245
5246/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005247void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
5248 int alg_arg,
5249 data_t *nonce,
5250 data_t *additional_data,
5251 data_t *input_data,
5252 int do_set_lengths,
5253 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005254{
Paul Elliottd3f82412021-06-16 16:52:21 +01005255 size_t ad_part_len = 0;
5256 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005257 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
5260 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005261
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 if (do_set_lengths) {
5263 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005264 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005266 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005268 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005269
5270 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 if (!aead_multipart_internal_func(key_type_arg, key_data,
5272 alg_arg, nonce,
5273 additional_data,
5274 ad_part_len,
5275 input_data, -1,
5276 set_lengths_method,
5277 expected_output,
5278 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005279 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005280 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005281
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 /* length(0) part, length(ad_part_len) part, length(0) part... */
5283 mbedtls_test_set_step(1000 + ad_part_len);
5284
5285 if (!aead_multipart_internal_func(key_type_arg, key_data,
5286 alg_arg, nonce,
5287 additional_data,
5288 ad_part_len,
5289 input_data, -1,
5290 set_lengths_method,
5291 expected_output,
5292 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005293 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 }
5295 }
5296
5297 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
5298 /* Split data into length(data_part_len) parts. */
5299 mbedtls_test_set_step(2000 + data_part_len);
5300
5301 if (do_set_lengths) {
5302 if (data_part_len & 0x01) {
5303 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5304 } else {
5305 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
5306 }
5307 }
5308
5309 if (!aead_multipart_internal_func(key_type_arg, key_data,
5310 alg_arg, nonce,
5311 additional_data, -1,
5312 input_data, data_part_len,
5313 set_lengths_method,
5314 expected_output,
5315 1, 0)) {
5316 break;
5317 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005318
5319 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005321
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 if (!aead_multipart_internal_func(key_type_arg, key_data,
5323 alg_arg, nonce,
5324 additional_data, -1,
5325 input_data, data_part_len,
5326 set_lengths_method,
5327 expected_output,
5328 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005329 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005331 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005332
Paul Elliott8fc45162021-06-23 16:06:01 +01005333 /* Goto is required to silence warnings about unused labels, as we
5334 * don't actually do any test assertions in this function. */
5335 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005336}
5337/* END_CASE */
5338
5339/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005340void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5341 int alg_arg,
5342 data_t *nonce,
5343 data_t *additional_data,
5344 data_t *input_data,
5345 int do_set_lengths,
5346 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005347{
Paul Elliottd3f82412021-06-16 16:52:21 +01005348 size_t ad_part_len = 0;
5349 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005350 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005351
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005353 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005355
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 if (do_set_lengths) {
5357 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005358 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005360 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005362 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005363
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 if (!aead_multipart_internal_func(key_type_arg, key_data,
5365 alg_arg, nonce,
5366 additional_data,
5367 ad_part_len,
5368 input_data, -1,
5369 set_lengths_method,
5370 expected_output,
5371 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005372 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005374
5375 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 if (!aead_multipart_internal_func(key_type_arg, key_data,
5379 alg_arg, nonce,
5380 additional_data,
5381 ad_part_len,
5382 input_data, -1,
5383 set_lengths_method,
5384 expected_output,
5385 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005386 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005388 }
5389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005391 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 if (do_set_lengths) {
5395 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005396 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005398 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005400 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 if (!aead_multipart_internal_func(key_type_arg, key_data,
5403 alg_arg, nonce,
5404 additional_data, -1,
5405 input_data, data_part_len,
5406 set_lengths_method,
5407 expected_output,
5408 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005409 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005411
5412 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 if (!aead_multipart_internal_func(key_type_arg, key_data,
5416 alg_arg, nonce,
5417 additional_data, -1,
5418 input_data, data_part_len,
5419 set_lengths_method,
5420 expected_output,
5421 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005422 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005424 }
5425
Paul Elliott8fc45162021-06-23 16:06:01 +01005426 /* Goto is required to silence warnings about unused labels, as we
5427 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005428 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005429}
5430/* END_CASE */
5431
5432/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005433void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5434 int alg_arg,
5435 int nonce_length,
5436 int expected_nonce_length_arg,
5437 data_t *additional_data,
5438 data_t *input_data,
5439 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005440{
5441
5442 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5443 psa_key_type_t key_type = key_type_arg;
5444 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005445 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
David Horstmann52402ec2023-12-11 15:09:46 +00005446 /* Some tests try to get more than the maximum nonce length,
5447 * so allocate double. */
5448 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE * 2];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5450 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005451 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005452 size_t actual_nonce_length = 0;
5453 size_t expected_nonce_length = expected_nonce_length_arg;
5454 unsigned char *output = NULL;
5455 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005456 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005457 size_t ciphertext_size = 0;
5458 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005459 size_t tag_length = 0;
5460 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5465 psa_set_key_algorithm(&attributes, alg);
5466 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005467
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5469 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005470
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005474
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005475 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005476
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005480
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005481 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005482
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005484
5485 /* If the operation is not supported, just skip and not fail in case the
5486 * encryption involves a common limitation of cryptography hardwares and
5487 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 if (status == PSA_ERROR_NOT_SUPPORTED) {
5489 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5490 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005491 }
5492
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005494
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5496 nonce_length,
5497 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005498
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005500
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 if (expected_status == PSA_SUCCESS) {
5504 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5505 alg));
5506 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005507
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005511 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5513 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5516 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005517
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5519 output, output_size,
5520 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5523 &ciphertext_length, tag_buffer,
5524 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005525 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005526
5527exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 psa_destroy_key(key);
5529 mbedtls_free(output);
5530 mbedtls_free(ciphertext);
5531 psa_aead_abort(&operation);
5532 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005533}
5534/* END_CASE */
5535
5536/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005537void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5538 int alg_arg,
5539 int nonce_length_arg,
5540 int set_lengths_method_arg,
5541 data_t *additional_data,
5542 data_t *input_data,
5543 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005544{
5545
5546 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5547 psa_key_type_t key_type = key_type_arg;
5548 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005549 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005550 uint8_t *nonce_buffer = NULL;
5551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5552 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5553 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005554 unsigned char *output = NULL;
5555 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005556 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005557 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005558 size_t ciphertext_size = 0;
5559 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005560 size_t tag_length = 0;
5561 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005562 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005563 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005566
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5568 psa_set_key_algorithm(&attributes, alg);
5569 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5572 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005573
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005577
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005578 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005583
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005584 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005587
5588 /* If the operation is not supported, just skip and not fail in case the
5589 * encryption involves a common limitation of cryptography hardwares and
5590 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 if (status == PSA_ERROR_NOT_SUPPORTED) {
5592 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5593 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005594 }
5595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005597
Paul Elliott4023ffd2021-09-10 16:21:22 +01005598 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 if (nonce_length_arg == -1) {
5600 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005601 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 nonce_length = 0;
5603 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005604 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005606 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005607
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 if (nonce_buffer) {
5609 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005610 nonce_buffer[index] = 'a' + index;
5611 }
Paul Elliott66696b52021-08-16 18:42:41 +01005612 }
Paul Elliott863864a2021-07-23 17:28:31 +01005613 }
5614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5616 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5617 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005618 }
5619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005623
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 if (expected_status == PSA_SUCCESS) {
5625 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5626 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5627 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005628 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005630 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 }
Paul Elliott863864a2021-07-23 17:28:31 +01005632
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005633 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5635 additional_data->len),
5636 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005637
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5639 output, output_size,
5640 &ciphertext_length),
5641 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5644 &ciphertext_length, tag_buffer,
5645 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5646 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005647 }
5648
5649exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 psa_destroy_key(key);
5651 mbedtls_free(output);
5652 mbedtls_free(ciphertext);
5653 mbedtls_free(nonce_buffer);
5654 psa_aead_abort(&operation);
5655 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005656}
5657/* END_CASE */
5658
5659/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005660void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005661 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005662 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005663 data_t *nonce,
5664 data_t *additional_data,
5665 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005667{
5668
5669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5670 psa_key_type_t key_type = key_type_arg;
5671 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005672 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5674 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5675 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005676 unsigned char *output = NULL;
5677 unsigned char *ciphertext = NULL;
5678 size_t output_size = output_size_arg;
5679 size_t ciphertext_size = 0;
5680 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005681 size_t tag_length = 0;
5682 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5687 psa_set_key_algorithm(&attributes, alg);
5688 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5691 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005694
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005695 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005698
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005699 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005702
5703 /* If the operation is not supported, just skip and not fail in case the
5704 * encryption involves a common limitation of cryptography hardwares and
5705 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 if (status == PSA_ERROR_NOT_SUPPORTED) {
5707 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5708 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005709 }
5710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5714 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005715
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5719 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 status = psa_aead_update(&operation, input_data->x, input_data->len,
5722 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005727 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5729 &ciphertext_length, tag_buffer,
5730 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005731 }
5732
5733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 psa_destroy_key(key);
5735 mbedtls_free(output);
5736 mbedtls_free(ciphertext);
5737 psa_aead_abort(&operation);
5738 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005739}
5740/* END_CASE */
5741
Paul Elliott91b021e2021-07-23 18:52:31 +01005742/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005743void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5744 int alg_arg,
5745 int finish_ciphertext_size_arg,
5746 int tag_size_arg,
5747 data_t *nonce,
5748 data_t *additional_data,
5749 data_t *input_data,
5750 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005751{
5752
5753 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5754 psa_key_type_t key_type = key_type_arg;
5755 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005756 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5758 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5759 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005760 unsigned char *ciphertext = NULL;
5761 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005762 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005763 size_t ciphertext_size = 0;
5764 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5766 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005767 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5772 psa_set_key_algorithm(&attributes, alg);
5773 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5776 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005781
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005782 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005783
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005784 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005785
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005786 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005789
5790 /* If the operation is not supported, just skip and not fail in case the
5791 * encryption involves a common limitation of cryptography hardwares and
5792 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 if (status == PSA_ERROR_NOT_SUPPORTED) {
5794 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5795 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005796 }
5797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5803 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5806 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5809 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005810
5811 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 status = psa_aead_finish(&operation, finish_ciphertext,
5813 finish_ciphertext_size,
5814 &ciphertext_length, tag_buffer,
5815 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005818
5819exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 psa_destroy_key(key);
5821 mbedtls_free(ciphertext);
5822 mbedtls_free(finish_ciphertext);
5823 mbedtls_free(tag_buffer);
5824 psa_aead_abort(&operation);
5825 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005826}
5827/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005828
5829/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005830void aead_multipart_verify(int key_type_arg, data_t *key_data,
5831 int alg_arg,
5832 data_t *nonce,
5833 data_t *additional_data,
5834 data_t *input_data,
5835 data_t *tag,
5836 int tag_usage_arg,
5837 int expected_setup_status_arg,
5838 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005839{
5840 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5841 psa_key_type_t key_type = key_type_arg;
5842 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005843 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005844 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5845 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5846 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005847 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005848 unsigned char *plaintext = NULL;
5849 unsigned char *finish_plaintext = NULL;
5850 size_t plaintext_size = 0;
5851 size_t plaintext_length = 0;
5852 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005853 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005854 unsigned char *tag_buffer = NULL;
5855 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5860 psa_set_key_algorithm(&attributes, alg);
5861 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5864 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5869 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005870
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005871 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005874
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005875 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005878
5879 /* If the operation is not supported, just skip and not fail in case the
5880 * encryption involves a common limitation of cryptography hardwares and
5881 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 if (status == PSA_ERROR_NOT_SUPPORTED) {
5883 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5884 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005885 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005889 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 }
Paul Elliott9961a662021-09-17 19:19:02 +01005891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 status = psa_aead_set_lengths(&operation, additional_data->len,
5897 input_data->len);
5898 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005899
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5901 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005902
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5904 input_data->len,
5905 plaintext, plaintext_size,
5906 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005907
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005909 tag_buffer = tag->x;
5910 tag_size = tag->len;
5911 }
5912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 status = psa_aead_verify(&operation, finish_plaintext,
5914 verify_plaintext_size,
5915 &plaintext_length,
5916 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005917
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005919
5920exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 psa_destroy_key(key);
5922 mbedtls_free(plaintext);
5923 mbedtls_free(finish_plaintext);
5924 psa_aead_abort(&operation);
5925 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005926}
5927/* END_CASE */
5928
Paul Elliott9961a662021-09-17 19:19:02 +01005929/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005930void aead_multipart_setup(int key_type_arg, data_t *key_data,
5931 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005932{
5933 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5934 psa_key_type_t key_type = key_type_arg;
5935 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005936 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5938 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5939 psa_status_t expected_status = expected_status_arg;
5940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 psa_set_key_usage_flags(&attributes,
5944 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5945 psa_set_key_algorithm(&attributes, alg);
5946 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5949 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005950
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005952
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005960
5961exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 psa_destroy_key(key);
5963 psa_aead_abort(&operation);
5964 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005965}
5966/* END_CASE */
5967
5968/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005969void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5970 int alg_arg,
5971 data_t *nonce,
5972 data_t *additional_data,
5973 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005974{
5975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5976 psa_key_type_t key_type = key_type_arg;
5977 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005978 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005979 unsigned char *output_data = NULL;
5980 unsigned char *final_data = NULL;
5981 size_t output_size = 0;
5982 size_t finish_output_size = 0;
5983 size_t output_length = 0;
5984 size_t key_bits = 0;
5985 size_t tag_length = 0;
5986 size_t tag_size = 0;
5987 size_t nonce_length = 0;
5988 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5989 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5990 size_t output_part_length = 0;
5991 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 psa_set_key_usage_flags(&attributes,
5996 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5997 psa_set_key_algorithm(&attributes, alg);
5998 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6001 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6004 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006009
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006011
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006012 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006013
Gilles Peskine449bd832023-01-11 14:50:10 +01006014 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006015
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006017
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006018 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006019
6020 /* Test all operations error without calling setup first. */
6021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6023 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6028 PSA_AEAD_NONCE_MAX_SIZE,
6029 &nonce_length),
6030 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006031
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006033
Paul Elliott481be342021-07-16 17:38:47 +01006034 /* ------------------------------------------------------- */
6035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6037 input_data->len),
6038 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006041
Paul Elliott481be342021-07-16 17:38:47 +01006042 /* ------------------------------------------------------- */
6043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6045 additional_data->len),
6046 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006049
Paul Elliott481be342021-07-16 17:38:47 +01006050 /* ------------------------------------------------------- */
6051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6053 input_data->len, output_data,
6054 output_size, &output_length),
6055 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006058
Paul Elliott481be342021-07-16 17:38:47 +01006059 /* ------------------------------------------------------- */
6060
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6062 finish_output_size,
6063 &output_part_length,
6064 tag_buffer, tag_length,
6065 &tag_size),
6066 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006067
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006069
Paul Elliott481be342021-07-16 17:38:47 +01006070 /* ------------------------------------------------------- */
6071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6073 finish_output_size,
6074 &output_part_length,
6075 tag_buffer,
6076 tag_length),
6077 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006078
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006080
6081 /* Test for double setups. */
6082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
6086 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006089
Paul Elliott481be342021-07-16 17:38:47 +01006090 /* ------------------------------------------------------- */
6091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006093
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
6095 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006098
Paul Elliott374a2be2021-07-16 17:53:40 +01006099 /* ------------------------------------------------------- */
6100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
6104 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006107
6108 /* ------------------------------------------------------- */
6109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
6113 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006116
Paul Elliottc23a9a02021-06-21 18:32:46 +01006117 /* Test for not setting a nonce. */
6118
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006120
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6122 additional_data->len),
6123 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006124
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006126
Paul Elliott7f628422021-09-01 12:08:29 +01006127 /* ------------------------------------------------------- */
6128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01006130
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6132 input_data->len, output_data,
6133 output_size, &output_length),
6134 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01006135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01006137
Paul Elliottbdc2c682021-09-21 18:37:10 +01006138 /* ------------------------------------------------------- */
6139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01006141
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6143 finish_output_size,
6144 &output_part_length,
6145 tag_buffer, tag_length,
6146 &tag_size),
6147 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006150
6151 /* ------------------------------------------------------- */
6152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01006154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6156 finish_output_size,
6157 &output_part_length,
6158 tag_buffer,
6159 tag_length),
6160 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006163
Paul Elliottc23a9a02021-06-21 18:32:46 +01006164 /* Test for double setting nonce. */
6165
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6171 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006174
Paul Elliott374a2be2021-07-16 17:53:40 +01006175 /* Test for double generating nonce. */
6176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006178
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6180 PSA_AEAD_NONCE_MAX_SIZE,
6181 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01006182
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6184 PSA_AEAD_NONCE_MAX_SIZE,
6185 &nonce_length),
6186 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006187
6188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006190
6191 /* Test for generate nonce then set and vice versa */
6192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6196 PSA_AEAD_NONCE_MAX_SIZE,
6197 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01006198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6200 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006201
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006203
Andrzej Kurekad837522021-12-15 15:28:49 +01006204 /* Test for generating nonce after calling set lengths */
6205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6209 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6212 PSA_AEAD_NONCE_MAX_SIZE,
6213 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006216
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006217 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 if (operation.alg == PSA_ALG_CCM) {
6222 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6223 input_data->len),
6224 PSA_ERROR_INVALID_ARGUMENT);
6225 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6226 PSA_AEAD_NONCE_MAX_SIZE,
6227 &nonce_length),
6228 PSA_ERROR_BAD_STATE);
6229 } else {
6230 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6231 input_data->len));
6232 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6233 PSA_AEAD_NONCE_MAX_SIZE,
6234 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006235 }
6236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006238
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006239 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00006240#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006242
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6244 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6245 input_data->len),
6246 PSA_ERROR_INVALID_ARGUMENT);
6247 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6248 PSA_AEAD_NONCE_MAX_SIZE,
6249 &nonce_length),
6250 PSA_ERROR_BAD_STATE);
6251 } else {
6252 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6253 input_data->len));
6254 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6255 PSA_AEAD_NONCE_MAX_SIZE,
6256 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006257 }
6258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00006260#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01006261
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006262 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6267 PSA_AEAD_NONCE_MAX_SIZE,
6268 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 if (operation.alg == PSA_ALG_CCM) {
6271 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6272 input_data->len),
6273 PSA_ERROR_INVALID_ARGUMENT);
6274 } else {
6275 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6276 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006277 }
6278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006280
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006281 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006282 /* Test for setting nonce after calling set lengths */
6283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6287 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006292
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006293 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 if (operation.alg == PSA_ALG_CCM) {
6298 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6299 input_data->len),
6300 PSA_ERROR_INVALID_ARGUMENT);
6301 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6302 PSA_ERROR_BAD_STATE);
6303 } else {
6304 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6305 input_data->len));
6306 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006307 }
6308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006310
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006311 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00006312#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006314
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6316 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6317 input_data->len),
6318 PSA_ERROR_INVALID_ARGUMENT);
6319 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6320 PSA_ERROR_BAD_STATE);
6321 } else {
6322 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6323 input_data->len));
6324 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006325 }
6326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006328#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006329
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006330 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006335
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 if (operation.alg == PSA_ALG_CCM) {
6337 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6338 input_data->len),
6339 PSA_ERROR_INVALID_ARGUMENT);
6340 } else {
6341 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6342 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006343 }
6344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006346
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006347 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006348#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 if (operation.alg == PSA_ALG_GCM) {
6352 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6353 SIZE_MAX),
6354 PSA_ERROR_INVALID_ARGUMENT);
6355 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6356 PSA_ERROR_BAD_STATE);
6357 } else if (operation.alg != PSA_ALG_CCM) {
6358 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6359 SIZE_MAX));
6360 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006361 }
6362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006364#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006365
Tom Cosgrove1797b052022-12-04 17:19:59 +00006366 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006367#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006371
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 if (operation.alg == PSA_ALG_GCM) {
6373 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6374 SIZE_MAX),
6375 PSA_ERROR_INVALID_ARGUMENT);
6376 } else if (operation.alg != PSA_ALG_CCM) {
6377 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6378 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006379 }
6380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006382#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006383
6384 /* ------------------------------------------------------- */
6385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6391 PSA_AEAD_NONCE_MAX_SIZE,
6392 &nonce_length),
6393 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006394
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006396
Paul Elliott7220cae2021-06-22 17:25:57 +01006397 /* Test for generating nonce in decrypt setup. */
6398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6402 PSA_AEAD_NONCE_MAX_SIZE,
6403 &nonce_length),
6404 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006407
Paul Elliottc23a9a02021-06-21 18:32:46 +01006408 /* Test for setting lengths twice. */
6409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6415 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6418 input_data->len),
6419 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006422
Andrzej Kurekad837522021-12-15 15:28:49 +01006423 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006430
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6432 additional_data->len),
6433 PSA_ERROR_BAD_STATE);
6434 } else {
6435 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6436 additional_data->len));
6437
6438 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6439 input_data->len),
6440 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006441 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006443
6444 /* ------------------------------------------------------- */
6445
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006449
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 if (operation.alg == PSA_ALG_CCM) {
6451 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6452 input_data->len, output_data,
6453 output_size, &output_length),
6454 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006455
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 } else {
6457 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6458 input_data->len, output_data,
6459 output_size, &output_length));
6460
6461 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6462 input_data->len),
6463 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006464 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006466
6467 /* ------------------------------------------------------- */
6468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006470
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006472
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 if (operation.alg == PSA_ALG_CCM) {
6474 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6475 finish_output_size,
6476 &output_part_length,
6477 tag_buffer, tag_length,
6478 &tag_size));
6479 } else {
6480 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6481 finish_output_size,
6482 &output_part_length,
6483 tag_buffer, tag_length,
6484 &tag_size));
6485
6486 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6487 input_data->len),
6488 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006489 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006491
6492 /* Test for setting lengths after generating nonce + already starting data. */
6493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006495
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6497 PSA_AEAD_NONCE_MAX_SIZE,
6498 &nonce_length));
6499 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006500
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6502 additional_data->len),
6503 PSA_ERROR_BAD_STATE);
6504 } else {
6505 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6506 additional_data->len));
6507
6508 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6509 input_data->len),
6510 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006511 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006513
6514 /* ------------------------------------------------------- */
6515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006517
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6519 PSA_AEAD_NONCE_MAX_SIZE,
6520 &nonce_length));
6521 if (operation.alg == PSA_ALG_CCM) {
6522 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6523 input_data->len, output_data,
6524 output_size, &output_length),
6525 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006526
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 } else {
6528 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6529 input_data->len, output_data,
6530 output_size, &output_length));
6531
6532 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6533 input_data->len),
6534 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006535 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006536 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006537
6538 /* ------------------------------------------------------- */
6539
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6543 PSA_AEAD_NONCE_MAX_SIZE,
6544 &nonce_length));
6545 if (operation.alg == PSA_ALG_CCM) {
6546 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6547 finish_output_size,
6548 &output_part_length,
6549 tag_buffer, tag_length,
6550 &tag_size));
6551 } else {
6552 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6553 finish_output_size,
6554 &output_part_length,
6555 tag_buffer, tag_length,
6556 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006557
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6559 input_data->len),
6560 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006561 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006563
Paul Elliott243080c2021-07-21 19:01:17 +01006564 /* Test for not sending any additional data or data after setting non zero
6565 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006566
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006568
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006570
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6572 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006573
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6575 finish_output_size,
6576 &output_part_length,
6577 tag_buffer, tag_length,
6578 &tag_size),
6579 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006580
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006582
Paul Elliott243080c2021-07-21 19:01:17 +01006583 /* Test for not sending any additional data or data after setting non-zero
6584 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006589
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6591 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006592
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6594 finish_output_size,
6595 &output_part_length,
6596 tag_buffer,
6597 tag_length),
6598 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006599
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006601
Paul Elliott243080c2021-07-21 19:01:17 +01006602 /* Test for not sending any additional data after setting a non-zero length
6603 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006606
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006608
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6610 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006611
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6613 input_data->len, output_data,
6614 output_size, &output_length),
6615 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006616
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006618
Paul Elliottf94bd992021-09-19 18:15:59 +01006619 /* Test for not sending any data after setting a non-zero length for it.*/
6620
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006622
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006624
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6626 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006627
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6629 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006630
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6632 finish_output_size,
6633 &output_part_length,
6634 tag_buffer, tag_length,
6635 &tag_size),
6636 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006637
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006639
Paul Elliottb0450fe2021-09-01 15:06:26 +01006640 /* Test for sending too much additional data after setting lengths. */
6641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006643
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006645
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006647
6648
Gilles Peskine449bd832023-01-11 14:50:10 +01006649 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6650 additional_data->len),
6651 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006652
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006654
Paul Elliotta2a09b02021-09-22 14:56:40 +01006655 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006658
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006660
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6662 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006663
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6665 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006666
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6668 1),
6669 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006672
Paul Elliottb0450fe2021-09-01 15:06:26 +01006673 /* Test for sending too much data after setting lengths. */
6674
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006676
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006678
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006680
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6682 input_data->len, output_data,
6683 output_size, &output_length),
6684 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006685
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006687
Paul Elliotta2a09b02021-09-22 14:56:40 +01006688 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006689
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006691
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006693
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6695 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006696
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6698 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006699
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6701 input_data->len, output_data,
6702 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6705 1, output_data,
6706 output_size, &output_length),
6707 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006708
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006710
Paul Elliottc23a9a02021-06-21 18:32:46 +01006711 /* Test sending additional data after data. */
6712
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006714
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006716
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 if (operation.alg != PSA_ALG_CCM) {
6718 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6719 input_data->len, output_data,
6720 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006721
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6723 additional_data->len),
6724 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006725 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006727
Paul Elliott534d0b42021-06-22 19:15:20 +01006728 /* Test calling finish on decryption. */
6729
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006731
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006733
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6735 finish_output_size,
6736 &output_part_length,
6737 tag_buffer, tag_length,
6738 &tag_size),
6739 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006740
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006742
6743 /* Test calling verify on encryption. */
6744
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006746
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006748
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6750 finish_output_size,
6751 &output_part_length,
6752 tag_buffer,
6753 tag_length),
6754 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006755
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006757
6758
Paul Elliottc23a9a02021-06-21 18:32:46 +01006759exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 psa_destroy_key(key);
6761 psa_aead_abort(&operation);
6762 mbedtls_free(output_data);
6763 mbedtls_free(final_data);
6764 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006765}
6766/* END_CASE */
6767
6768/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006769void signature_size(int type_arg,
6770 int bits,
6771 int alg_arg,
6772 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006773{
6774 psa_key_type_t type = type_arg;
6775 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006777
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006779
Gilles Peskinee59236f2018-01-27 23:32:46 +01006780exit:
6781 ;
6782}
6783/* END_CASE */
6784
6785/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006786void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6787 int alg_arg, data_t *input_data,
6788 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006789{
Ronald Cron5425a212020-08-04 14:58:35 +02006790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006791 psa_key_type_t key_type = key_type_arg;
6792 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006793 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006794 unsigned char *signature = NULL;
6795 size_t signature_size;
6796 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006798
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006800
Gilles Peskine449bd832023-01-11 14:50:10 +01006801 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6802 psa_set_key_algorithm(&attributes, alg);
6803 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006804
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6806 &key));
6807 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6808 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006809
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006810 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006811 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6813 key_bits, alg);
6814 TEST_ASSERT(signature_size != 0);
6815 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006816 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006817
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006818 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 PSA_ASSERT(psa_sign_hash(key, alg,
6820 input_data->x, input_data->len,
6821 signature, signature_size,
6822 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006823 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006824 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006825 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006826
6827exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006828 /*
6829 * Key attributes may have been returned by psa_get_key_attributes()
6830 * thus reset them as required.
6831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006833
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 psa_destroy_key(key);
6835 mbedtls_free(signature);
6836 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006837}
6838/* END_CASE */
6839
Paul Elliott712d5122022-12-07 14:03:10 +00006840/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006841/**
6842 * sign_hash_interruptible() test intentions:
6843 *
6844 * Note: This test can currently only handle ECDSA.
6845 *
6846 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006847 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006848 *
6849 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6850 * expected for different max_ops values.
6851 *
6852 * 3. Test that the number of ops done prior to start and after abort is zero
6853 * and that each successful stage completes some ops (this is not mandated by
6854 * the PSA specification, but is currently the case).
6855 *
6856 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6857 * complete() calls does not alter the number of ops returned.
6858 */
Paul Elliott712d5122022-12-07 14:03:10 +00006859void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6860 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006861 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006862{
6863 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6864 psa_key_type_t key_type = key_type_arg;
6865 psa_algorithm_t alg = alg_arg;
6866 size_t key_bits;
6867 unsigned char *signature = NULL;
6868 size_t signature_size;
6869 size_t signature_length = 0xdeadbeef;
6870 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6871 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006872 uint32_t num_ops = 0;
6873 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006874 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006875 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006876 size_t min_completes = 0;
6877 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006878
Paul Elliott712d5122022-12-07 14:03:10 +00006879 psa_sign_hash_interruptible_operation_t operation =
6880 psa_sign_hash_interruptible_operation_init();
6881
6882 PSA_ASSERT(psa_crypto_init());
6883
6884 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6885 psa_set_key_algorithm(&attributes, alg);
6886 psa_set_key_type(&attributes, key_type);
6887
6888 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6889 &key));
6890 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6891 key_bits = psa_get_key_bits(&attributes);
6892
6893 /* Allocate a buffer which has the size advertised by the
6894 * library. */
6895 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6896 key_bits, alg);
6897 TEST_ASSERT(signature_size != 0);
6898 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006899 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006900
Paul Elliott0c683352022-12-16 19:16:56 +00006901 psa_interruptible_set_max_ops(max_ops);
6902
Paul Elliott6f600372023-02-06 18:41:05 +00006903 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6904 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006905
Paul Elliott712d5122022-12-07 14:03:10 +00006906 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6907 TEST_ASSERT(num_ops_prior == 0);
6908
6909 /* Start performing the signature. */
6910 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6911 input_data->x, input_data->len));
6912
6913 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6914 TEST_ASSERT(num_ops_prior == 0);
6915
6916 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006917 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006918 status = psa_sign_hash_complete(&operation, signature, signature_size,
6919 &signature_length);
6920
Paul Elliott0c683352022-12-16 19:16:56 +00006921 num_completes++;
6922
Paul Elliott712d5122022-12-07 14:03:10 +00006923 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6924 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006925 /* We are asserting here that every complete makes progress
6926 * (completes some ops), which is true of the internal
6927 * implementation and probably any implementation, however this is
6928 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006929 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006930
Paul Elliott712d5122022-12-07 14:03:10 +00006931 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006932
6933 /* Ensure calling get_num_ops() twice still returns the same
6934 * number of ops as previously reported. */
6935 num_ops = psa_sign_hash_get_num_ops(&operation);
6936
6937 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006938 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006939 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006940
6941 TEST_ASSERT(status == PSA_SUCCESS);
6942
Paul Elliott0c683352022-12-16 19:16:56 +00006943 TEST_LE_U(min_completes, num_completes);
6944 TEST_LE_U(num_completes, max_completes);
6945
Paul Elliott712d5122022-12-07 14:03:10 +00006946 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006947 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006948 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006949
6950 PSA_ASSERT(psa_sign_hash_abort(&operation));
6951
Paul Elliott59ad9452022-12-18 15:09:02 +00006952 num_ops = psa_sign_hash_get_num_ops(&operation);
6953 TEST_ASSERT(num_ops == 0);
6954
Paul Elliott712d5122022-12-07 14:03:10 +00006955exit:
6956
6957 /*
6958 * Key attributes may have been returned by psa_get_key_attributes()
6959 * thus reset them as required.
6960 */
6961 psa_reset_key_attributes(&attributes);
6962
6963 psa_destroy_key(key);
6964 mbedtls_free(signature);
6965 PSA_DONE();
6966}
6967/* END_CASE */
6968
Gilles Peskine20035e32018-02-03 22:44:14 +01006969/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006970void sign_hash_fail(int key_type_arg, data_t *key_data,
6971 int alg_arg, data_t *input_data,
6972 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006973{
Ronald Cron5425a212020-08-04 14:58:35 +02006974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006975 psa_key_type_t key_type = key_type_arg;
6976 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006977 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006978 psa_status_t actual_status;
6979 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006980 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006981 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006983
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006984 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006985
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006987
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6989 psa_set_key_algorithm(&attributes, alg);
6990 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006991
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6993 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006994
Gilles Peskine449bd832023-01-11 14:50:10 +01006995 actual_status = psa_sign_hash(key, alg,
6996 input_data->x, input_data->len,
6997 signature, signature_size,
6998 &signature_length);
6999 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02007000 /* The value of *signature_length is unspecified on error, but
7001 * whatever it is, it should be less than signature_size, so that
7002 * if the caller tries to read *signature_length bytes without
7003 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01007005
7006exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 psa_reset_key_attributes(&attributes);
7008 psa_destroy_key(key);
7009 mbedtls_free(signature);
7010 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01007011}
7012/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03007013
Paul Elliott91007972022-12-16 12:21:24 +00007014/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007015/**
7016 * sign_hash_fail_interruptible() test intentions:
7017 *
7018 * Note: This test can currently only handle ECDSA.
7019 *
7020 * 1. Test that various failure cases for interruptible sign hash fail with the
7021 * correct error codes, and at the correct point (at start or during
7022 * complete).
7023 *
7024 * 2. Test the number of calls to psa_sign_hash_complete() required are as
7025 * expected for different max_ops values.
7026 *
7027 * 3. Test that the number of ops done prior to start and after abort is zero
7028 * and that each successful stage completes some ops (this is not mandated by
7029 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007030 *
7031 * 4. Check that calling complete() when start() fails and complete()
7032 * after completion results in a BAD_STATE error.
7033 *
7034 * 5. Check that calling start() again after start fails results in a BAD_STATE
7035 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007036 */
Paul Elliott91007972022-12-16 12:21:24 +00007037void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7038 int alg_arg, data_t *input_data,
7039 int signature_size_arg,
7040 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007041 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007042 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007043{
7044 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7045 psa_key_type_t key_type = key_type_arg;
7046 psa_algorithm_t alg = alg_arg;
7047 size_t signature_size = signature_size_arg;
7048 psa_status_t actual_status;
7049 psa_status_t expected_start_status = expected_start_status_arg;
7050 psa_status_t expected_complete_status = expected_complete_status_arg;
7051 unsigned char *signature = NULL;
7052 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007053 uint32_t num_ops = 0;
7054 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007055 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007056 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007057 size_t min_completes = 0;
7058 size_t max_completes = 0;
7059
Paul Elliott91007972022-12-16 12:21:24 +00007060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7061 psa_sign_hash_interruptible_operation_t operation =
7062 psa_sign_hash_interruptible_operation_init();
7063
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007064 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00007065
7066 PSA_ASSERT(psa_crypto_init());
7067
7068 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
7069 psa_set_key_algorithm(&attributes, alg);
7070 psa_set_key_type(&attributes, key_type);
7071
7072 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7073 &key));
7074
Paul Elliott0c683352022-12-16 19:16:56 +00007075 psa_interruptible_set_max_ops(max_ops);
7076
Paul Elliott6f600372023-02-06 18:41:05 +00007077 interruptible_signverify_get_minmax_completes(max_ops,
7078 expected_complete_status,
7079 &min_completes,
7080 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007081
Paul Elliott91007972022-12-16 12:21:24 +00007082 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
7083 TEST_ASSERT(num_ops_prior == 0);
7084
7085 /* Start performing the signature. */
7086 actual_status = psa_sign_hash_start(&operation, key, alg,
7087 input_data->x, input_data->len);
7088
7089 TEST_EQUAL(actual_status, expected_start_status);
7090
Paul Elliottc9774412023-02-06 15:14:07 +00007091 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007092 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007093 * start failed. */
7094 actual_status = psa_sign_hash_complete(&operation, signature,
7095 signature_size,
7096 &signature_length);
7097
7098 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7099
7100 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007101 actual_status = psa_sign_hash_start(&operation, key, alg,
7102 input_data->x, input_data->len);
7103
7104 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7105 }
7106
Paul Elliott91007972022-12-16 12:21:24 +00007107 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
7108 TEST_ASSERT(num_ops_prior == 0);
7109
Paul Elliott91007972022-12-16 12:21:24 +00007110 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007111 do {
Paul Elliott91007972022-12-16 12:21:24 +00007112 actual_status = psa_sign_hash_complete(&operation, signature,
7113 signature_size,
7114 &signature_length);
7115
Paul Elliott0c683352022-12-16 19:16:56 +00007116 num_completes++;
7117
Paul Elliott334d7262023-01-20 17:29:41 +00007118 if (actual_status == PSA_SUCCESS ||
7119 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007120 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007121 /* We are asserting here that every complete makes progress
7122 * (completes some ops), which is true of the internal
7123 * implementation and probably any implementation, however this is
7124 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007125 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007126
Paul Elliott91007972022-12-16 12:21:24 +00007127 num_ops_prior = num_ops;
7128 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007129 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007130
Paul Elliottc9774412023-02-06 15:14:07 +00007131 TEST_EQUAL(actual_status, expected_complete_status);
7132
Paul Elliottefebad02023-02-15 16:56:45 +00007133 /* Check that another complete returns BAD_STATE. */
7134 actual_status = psa_sign_hash_complete(&operation, signature,
7135 signature_size,
7136 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00007137
Paul Elliottefebad02023-02-15 16:56:45 +00007138 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007139
Paul Elliott91007972022-12-16 12:21:24 +00007140 PSA_ASSERT(psa_sign_hash_abort(&operation));
7141
Paul Elliott59ad9452022-12-18 15:09:02 +00007142 num_ops = psa_sign_hash_get_num_ops(&operation);
7143 TEST_ASSERT(num_ops == 0);
7144
Paul Elliott91007972022-12-16 12:21:24 +00007145 /* The value of *signature_length is unspecified on error, but
7146 * whatever it is, it should be less than signature_size, so that
7147 * if the caller tries to read *signature_length bytes without
7148 * checking the error code then they don't overflow a buffer. */
7149 TEST_LE_U(signature_length, signature_size);
7150
Paul Elliott0c683352022-12-16 19:16:56 +00007151 TEST_LE_U(min_completes, num_completes);
7152 TEST_LE_U(num_completes, max_completes);
7153
Paul Elliott91007972022-12-16 12:21:24 +00007154exit:
7155 psa_reset_key_attributes(&attributes);
7156 psa_destroy_key(key);
7157 mbedtls_free(signature);
7158 PSA_DONE();
7159}
7160/* END_CASE */
7161
mohammad16038cc1cee2018-03-28 01:21:33 +03007162/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007163void sign_verify_hash(int key_type_arg, data_t *key_data,
7164 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02007165{
Ronald Cron5425a212020-08-04 14:58:35 +02007166 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02007167 psa_key_type_t key_type = key_type_arg;
7168 psa_algorithm_t alg = alg_arg;
7169 size_t key_bits;
7170 unsigned char *signature = NULL;
7171 size_t signature_size;
7172 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02007174
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02007176
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
7178 psa_set_key_algorithm(&attributes, alg);
7179 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02007180
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7182 &key));
7183 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7184 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02007185
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007186 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02007187 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7189 key_bits, alg);
7190 TEST_ASSERT(signature_size != 0);
7191 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007192 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02007193
7194 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 PSA_ASSERT(psa_sign_hash(key, alg,
7196 input_data->x, input_data->len,
7197 signature, signature_size,
7198 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02007199 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 TEST_LE_U(signature_length, signature_size);
7201 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02007202
7203 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 PSA_ASSERT(psa_verify_hash(key, alg,
7205 input_data->x, input_data->len,
7206 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02007207
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02007209 /* Flip a bit in the input and verify that the signature is now
7210 * detected as invalid. Flip a bit at the beginning, not at the end,
7211 * because ECDSA may ignore the last few bits of the input. */
7212 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 TEST_EQUAL(psa_verify_hash(key, alg,
7214 input_data->x, input_data->len,
7215 signature, signature_length),
7216 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02007217 }
7218
7219exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007220 /*
7221 * Key attributes may have been returned by psa_get_key_attributes()
7222 * thus reset them as required.
7223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007225
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 psa_destroy_key(key);
7227 mbedtls_free(signature);
7228 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02007229}
7230/* END_CASE */
7231
Paul Elliott712d5122022-12-07 14:03:10 +00007232/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007233/**
7234 * sign_verify_hash_interruptible() test intentions:
7235 *
7236 * Note: This test can currently only handle ECDSA.
7237 *
Paul Elliott8c092052023-03-06 17:49:14 +00007238 * 1. Test that we can sign an input hash with the given keypair and then
7239 * afterwards verify that signature. This is currently the only way to test
7240 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00007241 *
7242 * 2. Test that after corrupting the hash, the verification detects an invalid
7243 * signature.
7244 *
7245 * 3. Test the number of calls to psa_sign_hash_complete() required are as
7246 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00007247 *
7248 * 4. Test that the number of ops done prior to starting signing and after abort
7249 * is zero and that each successful signing stage completes some ops (this is
7250 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00007251 */
Paul Elliott712d5122022-12-07 14:03:10 +00007252void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00007253 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007254 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007255{
7256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7257 psa_key_type_t key_type = key_type_arg;
7258 psa_algorithm_t alg = alg_arg;
7259 size_t key_bits;
7260 unsigned char *signature = NULL;
7261 size_t signature_size;
7262 size_t signature_length = 0xdeadbeef;
7263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7264 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007265 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00007266 uint32_t num_ops = 0;
7267 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007268 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007269 size_t min_completes = 0;
7270 size_t max_completes = 0;
7271
Paul Elliott712d5122022-12-07 14:03:10 +00007272 psa_sign_hash_interruptible_operation_t sign_operation =
7273 psa_sign_hash_interruptible_operation_init();
7274 psa_verify_hash_interruptible_operation_t verify_operation =
7275 psa_verify_hash_interruptible_operation_init();
7276
7277 PSA_ASSERT(psa_crypto_init());
7278
Paul Elliott0c683352022-12-16 19:16:56 +00007279 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7280 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00007281 psa_set_key_algorithm(&attributes, alg);
7282 psa_set_key_type(&attributes, key_type);
7283
7284 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7285 &key));
7286 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7287 key_bits = psa_get_key_bits(&attributes);
7288
7289 /* Allocate a buffer which has the size advertised by the
7290 * library. */
7291 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7292 key_bits, alg);
7293 TEST_ASSERT(signature_size != 0);
7294 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007295 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00007296
Paul Elliott0c683352022-12-16 19:16:56 +00007297 psa_interruptible_set_max_ops(max_ops);
7298
Paul Elliott6f600372023-02-06 18:41:05 +00007299 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7300 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007301
Paul Elliott7c173082023-02-26 18:44:45 +00007302 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7303 TEST_ASSERT(num_ops_prior == 0);
7304
Paul Elliott712d5122022-12-07 14:03:10 +00007305 /* Start performing the signature. */
7306 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7307 input_data->x, input_data->len));
7308
Paul Elliott7c173082023-02-26 18:44:45 +00007309 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7310 TEST_ASSERT(num_ops_prior == 0);
7311
Paul Elliott712d5122022-12-07 14:03:10 +00007312 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007313 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007314
Paul Elliott0c683352022-12-16 19:16:56 +00007315 status = psa_sign_hash_complete(&sign_operation, signature,
7316 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007317 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007318
7319 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007320
7321 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7322 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7323 /* We are asserting here that every complete makes progress
7324 * (completes some ops), which is true of the internal
7325 * implementation and probably any implementation, however this is
7326 * not mandated by the PSA specification. */
7327 TEST_ASSERT(num_ops > num_ops_prior);
7328
7329 num_ops_prior = num_ops;
7330 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007331 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007332
7333 TEST_ASSERT(status == PSA_SUCCESS);
7334
Paul Elliott0c683352022-12-16 19:16:56 +00007335 TEST_LE_U(min_completes, num_completes);
7336 TEST_LE_U(num_completes, max_completes);
7337
Paul Elliott712d5122022-12-07 14:03:10 +00007338 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7339
Paul Elliott7c173082023-02-26 18:44:45 +00007340 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7341 TEST_ASSERT(num_ops == 0);
7342
Paul Elliott712d5122022-12-07 14:03:10 +00007343 /* Check that the signature length looks sensible. */
7344 TEST_LE_U(signature_length, signature_size);
7345 TEST_ASSERT(signature_length > 0);
7346
Paul Elliott0c683352022-12-16 19:16:56 +00007347 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007348
7349 /* Start verification. */
7350 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7351 input_data->x, input_data->len,
7352 signature, signature_length));
7353
7354 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007355 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007356 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007357
7358 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007359 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007360
7361 TEST_ASSERT(status == PSA_SUCCESS);
7362
Paul Elliott0c683352022-12-16 19:16:56 +00007363 TEST_LE_U(min_completes, num_completes);
7364 TEST_LE_U(num_completes, max_completes);
7365
Paul Elliott712d5122022-12-07 14:03:10 +00007366 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7367
7368 verify_operation = psa_verify_hash_interruptible_operation_init();
7369
7370 if (input_data->len != 0) {
7371 /* Flip a bit in the input and verify that the signature is now
7372 * detected as invalid. Flip a bit at the beginning, not at the end,
7373 * because ECDSA may ignore the last few bits of the input. */
7374 input_data->x[0] ^= 1;
7375
Paul Elliott712d5122022-12-07 14:03:10 +00007376 /* Start verification. */
7377 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7378 input_data->x, input_data->len,
7379 signature, signature_length));
7380
7381 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007382 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007383 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007384 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007385
7386 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7387 }
7388
7389 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7390
7391exit:
7392 /*
7393 * Key attributes may have been returned by psa_get_key_attributes()
7394 * thus reset them as required.
7395 */
7396 psa_reset_key_attributes(&attributes);
7397
7398 psa_destroy_key(key);
7399 mbedtls_free(signature);
7400 PSA_DONE();
7401}
7402/* END_CASE */
7403
Gilles Peskine9911b022018-06-29 17:30:48 +02007404/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007405void verify_hash(int key_type_arg, data_t *key_data,
7406 int alg_arg, data_t *hash_data,
7407 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007408{
Ronald Cron5425a212020-08-04 14:58:35 +02007409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007410 psa_key_type_t key_type = key_type_arg;
7411 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007413
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007415
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007417
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7419 psa_set_key_algorithm(&attributes, alg);
7420 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007421
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7423 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007424
Gilles Peskine449bd832023-01-11 14:50:10 +01007425 PSA_ASSERT(psa_verify_hash(key, alg,
7426 hash_data->x, hash_data->len,
7427 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007428
itayzafrir5c753392018-05-08 11:18:38 +03007429exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007430 psa_reset_key_attributes(&attributes);
7431 psa_destroy_key(key);
7432 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007433}
7434/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007435
Paul Elliott712d5122022-12-07 14:03:10 +00007436/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007437/**
7438 * verify_hash_interruptible() test intentions:
7439 *
7440 * Note: This test can currently only handle ECDSA.
7441 *
7442 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007443 * only). Given this test only does verification it can accept public keys as
7444 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007445 *
7446 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7447 * expected for different max_ops values.
7448 *
7449 * 3. Test that the number of ops done prior to start and after abort is zero
7450 * and that each successful stage completes some ops (this is not mandated by
7451 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007452 *
7453 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7454 * complete() calls does not alter the number of ops returned.
7455 *
7456 * 5. Test that after corrupting the hash, the verification detects an invalid
7457 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007458 */
Paul Elliott712d5122022-12-07 14:03:10 +00007459void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7460 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007461 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007462{
7463 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7464 psa_key_type_t key_type = key_type_arg;
7465 psa_algorithm_t alg = alg_arg;
7466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7467 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007468 uint32_t num_ops = 0;
7469 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007470 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007471 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007472 size_t min_completes = 0;
7473 size_t max_completes = 0;
7474
Paul Elliott712d5122022-12-07 14:03:10 +00007475 psa_verify_hash_interruptible_operation_t operation =
7476 psa_verify_hash_interruptible_operation_init();
7477
7478 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7479
7480 PSA_ASSERT(psa_crypto_init());
7481
7482 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7483 psa_set_key_algorithm(&attributes, alg);
7484 psa_set_key_type(&attributes, key_type);
7485
7486 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7487 &key));
7488
Paul Elliott0c683352022-12-16 19:16:56 +00007489 psa_interruptible_set_max_ops(max_ops);
7490
Paul Elliott6f600372023-02-06 18:41:05 +00007491 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7492 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007493
Paul Elliott712d5122022-12-07 14:03:10 +00007494 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7495
7496 TEST_ASSERT(num_ops_prior == 0);
7497
7498 /* Start verification. */
7499 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7500 hash_data->x, hash_data->len,
7501 signature_data->x, signature_data->len)
7502 );
7503
7504 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7505
7506 TEST_ASSERT(num_ops_prior == 0);
7507
7508 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007509 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007510 status = psa_verify_hash_complete(&operation);
7511
Paul Elliott0c683352022-12-16 19:16:56 +00007512 num_completes++;
7513
Paul Elliott712d5122022-12-07 14:03:10 +00007514 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7515 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007516 /* We are asserting here that every complete makes progress
7517 * (completes some ops), which is true of the internal
7518 * implementation and probably any implementation, however this is
7519 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007520 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007521
Paul Elliott712d5122022-12-07 14:03:10 +00007522 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007523
7524 /* Ensure calling get_num_ops() twice still returns the same
7525 * number of ops as previously reported. */
7526 num_ops = psa_verify_hash_get_num_ops(&operation);
7527
7528 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007529 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007530 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007531
7532 TEST_ASSERT(status == PSA_SUCCESS);
7533
Paul Elliott0c683352022-12-16 19:16:56 +00007534 TEST_LE_U(min_completes, num_completes);
7535 TEST_LE_U(num_completes, max_completes);
7536
Paul Elliott712d5122022-12-07 14:03:10 +00007537 PSA_ASSERT(psa_verify_hash_abort(&operation));
7538
Paul Elliott59ad9452022-12-18 15:09:02 +00007539 num_ops = psa_verify_hash_get_num_ops(&operation);
7540 TEST_ASSERT(num_ops == 0);
7541
Paul Elliott8359c142023-02-24 18:40:10 +00007542 if (hash_data->len != 0) {
7543 /* Flip a bit in the hash and verify that the signature is now detected
7544 * as invalid. Flip a bit at the beginning, not at the end, because
7545 * ECDSA may ignore the last few bits of the input. */
7546 hash_data->x[0] ^= 1;
7547
7548 /* Start verification. */
7549 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7550 hash_data->x, hash_data->len,
7551 signature_data->x, signature_data->len));
7552
7553 /* Continue performing the signature until complete. */
7554 do {
7555 status = psa_verify_hash_complete(&operation);
7556 } while (status == PSA_OPERATION_INCOMPLETE);
7557
7558 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7559 }
7560
Paul Elliott712d5122022-12-07 14:03:10 +00007561exit:
7562 psa_reset_key_attributes(&attributes);
7563 psa_destroy_key(key);
7564 PSA_DONE();
7565}
7566/* END_CASE */
7567
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007568/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007569void verify_hash_fail(int key_type_arg, data_t *key_data,
7570 int alg_arg, data_t *hash_data,
7571 data_t *signature_data,
7572 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007573{
Ronald Cron5425a212020-08-04 14:58:35 +02007574 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007575 psa_key_type_t key_type = key_type_arg;
7576 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007577 psa_status_t actual_status;
7578 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007580
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007582
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7584 psa_set_key_algorithm(&attributes, alg);
7585 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007586
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7588 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007589
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 actual_status = psa_verify_hash(key, alg,
7591 hash_data->x, hash_data->len,
7592 signature_data->x, signature_data->len);
7593 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007594
7595exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 psa_reset_key_attributes(&attributes);
7597 psa_destroy_key(key);
7598 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007599}
7600/* END_CASE */
7601
Paul Elliott91007972022-12-16 12:21:24 +00007602/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007603/**
7604 * verify_hash_fail_interruptible() test intentions:
7605 *
7606 * Note: This test can currently only handle ECDSA.
7607 *
7608 * 1. Test that various failure cases for interruptible verify hash fail with
7609 * the correct error codes, and at the correct point (at start or during
7610 * complete).
7611 *
7612 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7613 * expected for different max_ops values.
7614 *
7615 * 3. Test that the number of ops done prior to start and after abort is zero
7616 * and that each successful stage completes some ops (this is not mandated by
7617 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007618 *
7619 * 4. Check that calling complete() when start() fails and complete()
7620 * after completion results in a BAD_STATE error.
7621 *
7622 * 5. Check that calling start() again after start fails results in a BAD_STATE
7623 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007624 */
Paul Elliott91007972022-12-16 12:21:24 +00007625void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7626 int alg_arg, data_t *hash_data,
7627 data_t *signature_data,
7628 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007629 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007630 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007631{
7632 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7633 psa_key_type_t key_type = key_type_arg;
7634 psa_algorithm_t alg = alg_arg;
7635 psa_status_t actual_status;
7636 psa_status_t expected_start_status = expected_start_status_arg;
7637 psa_status_t expected_complete_status = expected_complete_status_arg;
7638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007639 uint32_t num_ops = 0;
7640 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007641 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007642 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007643 size_t min_completes = 0;
7644 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007645 psa_verify_hash_interruptible_operation_t operation =
7646 psa_verify_hash_interruptible_operation_init();
7647
7648 PSA_ASSERT(psa_crypto_init());
7649
7650 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7651 psa_set_key_algorithm(&attributes, alg);
7652 psa_set_key_type(&attributes, key_type);
7653
7654 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7655 &key));
7656
Paul Elliott0c683352022-12-16 19:16:56 +00007657 psa_interruptible_set_max_ops(max_ops);
7658
Paul Elliott6f600372023-02-06 18:41:05 +00007659 interruptible_signverify_get_minmax_completes(max_ops,
7660 expected_complete_status,
7661 &min_completes,
7662 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007663
Paul Elliott91007972022-12-16 12:21:24 +00007664 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7665 TEST_ASSERT(num_ops_prior == 0);
7666
7667 /* Start verification. */
7668 actual_status = psa_verify_hash_start(&operation, key, alg,
7669 hash_data->x, hash_data->len,
7670 signature_data->x,
7671 signature_data->len);
7672
7673 TEST_EQUAL(actual_status, expected_start_status);
7674
Paul Elliottc9774412023-02-06 15:14:07 +00007675 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007676 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007677 * start failed. */
7678 actual_status = psa_verify_hash_complete(&operation);
7679
7680 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7681
7682 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007683 actual_status = psa_verify_hash_start(&operation, key, alg,
7684 hash_data->x, hash_data->len,
7685 signature_data->x,
7686 signature_data->len);
7687
7688 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7689 }
7690
Paul Elliott91007972022-12-16 12:21:24 +00007691 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7692 TEST_ASSERT(num_ops_prior == 0);
7693
Paul Elliott91007972022-12-16 12:21:24 +00007694 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007695 do {
Paul Elliott91007972022-12-16 12:21:24 +00007696 actual_status = psa_verify_hash_complete(&operation);
7697
Paul Elliott0c683352022-12-16 19:16:56 +00007698 num_completes++;
7699
Paul Elliott334d7262023-01-20 17:29:41 +00007700 if (actual_status == PSA_SUCCESS ||
7701 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007702 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007703 /* We are asserting here that every complete makes progress
7704 * (completes some ops), which is true of the internal
7705 * implementation and probably any implementation, however this is
7706 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007707 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007708
Paul Elliott91007972022-12-16 12:21:24 +00007709 num_ops_prior = num_ops;
7710 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007711 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007712
Paul Elliottc9774412023-02-06 15:14:07 +00007713 TEST_EQUAL(actual_status, expected_complete_status);
7714
Paul Elliottefebad02023-02-15 16:56:45 +00007715 /* Check that another complete returns BAD_STATE. */
7716 actual_status = psa_verify_hash_complete(&operation);
7717 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007718
Paul Elliott0c683352022-12-16 19:16:56 +00007719 TEST_LE_U(min_completes, num_completes);
7720 TEST_LE_U(num_completes, max_completes);
7721
Paul Elliott91007972022-12-16 12:21:24 +00007722 PSA_ASSERT(psa_verify_hash_abort(&operation));
7723
Paul Elliott59ad9452022-12-18 15:09:02 +00007724 num_ops = psa_verify_hash_get_num_ops(&operation);
7725 TEST_ASSERT(num_ops == 0);
7726
Paul Elliott91007972022-12-16 12:21:24 +00007727exit:
7728 psa_reset_key_attributes(&attributes);
7729 psa_destroy_key(key);
7730 PSA_DONE();
7731}
7732/* END_CASE */
7733
Paul Elliott20a36062022-12-18 13:21:25 +00007734/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007735/**
7736 * interruptible_signverify_hash_state_test() test intentions:
7737 *
7738 * Note: This test can currently only handle ECDSA.
7739 *
7740 * 1. Test that calling the various interruptible sign and verify hash functions
7741 * in incorrect orders returns BAD_STATE errors.
7742 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007743void interruptible_signverify_hash_state_test(int key_type_arg,
7744 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007745{
7746 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7747 psa_key_type_t key_type = key_type_arg;
7748 psa_algorithm_t alg = alg_arg;
7749 size_t key_bits;
7750 unsigned char *signature = NULL;
7751 size_t signature_size;
7752 size_t signature_length = 0xdeadbeef;
7753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7754 psa_sign_hash_interruptible_operation_t sign_operation =
7755 psa_sign_hash_interruptible_operation_init();
7756 psa_verify_hash_interruptible_operation_t verify_operation =
7757 psa_verify_hash_interruptible_operation_init();
7758
7759 PSA_ASSERT(psa_crypto_init());
7760
7761 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7762 PSA_KEY_USAGE_VERIFY_HASH);
7763 psa_set_key_algorithm(&attributes, alg);
7764 psa_set_key_type(&attributes, key_type);
7765
7766 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7767 &key));
7768 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7769 key_bits = psa_get_key_bits(&attributes);
7770
7771 /* Allocate a buffer which has the size advertised by the
7772 * library. */
7773 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7774 key_bits, alg);
7775 TEST_ASSERT(signature_size != 0);
7776 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007777 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007778
7779 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7780
7781 /* --- Attempt completes prior to starts --- */
7782 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7783 signature_size,
7784 &signature_length),
7785 PSA_ERROR_BAD_STATE);
7786
7787 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7788
7789 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7790 PSA_ERROR_BAD_STATE);
7791
7792 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7793
7794 /* --- Aborts in all other places. --- */
7795 psa_sign_hash_abort(&sign_operation);
7796
7797 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7798 input_data->x, input_data->len));
7799
7800 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7801
7802 psa_interruptible_set_max_ops(1);
7803
7804 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7805 input_data->x, input_data->len));
7806
7807 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7808 signature_size,
7809 &signature_length),
7810 PSA_OPERATION_INCOMPLETE);
7811
7812 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7813
7814 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7815
7816 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7817 input_data->x, input_data->len));
7818
7819 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7820 signature_size,
7821 &signature_length));
7822
7823 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7824
7825 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7826
7827 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7828 input_data->x, input_data->len,
7829 signature, signature_length));
7830
7831 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7832
7833 psa_interruptible_set_max_ops(1);
7834
7835 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7836 input_data->x, input_data->len,
7837 signature, signature_length));
7838
7839 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7840 PSA_OPERATION_INCOMPLETE);
7841
7842 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7843
7844 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7845
7846 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7847 input_data->x, input_data->len,
7848 signature, signature_length));
7849
7850 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7851
7852 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7853
7854 /* --- Attempt double starts. --- */
7855
7856 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7857 input_data->x, input_data->len));
7858
7859 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7860 input_data->x, input_data->len),
7861 PSA_ERROR_BAD_STATE);
7862
7863 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7864
7865 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7866 input_data->x, input_data->len,
7867 signature, signature_length));
7868
7869 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7870 input_data->x, input_data->len,
7871 signature, signature_length),
7872 PSA_ERROR_BAD_STATE);
7873
7874 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7875
Paul Elliott76d671a2023-02-07 17:45:18 +00007876exit:
7877 /*
7878 * Key attributes may have been returned by psa_get_key_attributes()
7879 * thus reset them as required.
7880 */
7881 psa_reset_key_attributes(&attributes);
7882
7883 psa_destroy_key(key);
7884 mbedtls_free(signature);
7885 PSA_DONE();
7886}
7887/* END_CASE */
7888
7889/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007890/**
Paul Elliottc2033502023-02-26 17:09:14 +00007891 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007892 *
7893 * Note: This test can currently only handle ECDSA.
7894 *
7895 * 1. Test various edge cases in the interruptible sign and verify hash
7896 * interfaces.
7897 */
Paul Elliottc2033502023-02-26 17:09:14 +00007898void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007899 data_t *key_data, int alg_arg, data_t *input_data)
7900{
7901 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7902 psa_key_type_t key_type = key_type_arg;
7903 psa_algorithm_t alg = alg_arg;
7904 size_t key_bits;
7905 unsigned char *signature = NULL;
7906 size_t signature_size;
7907 size_t signature_length = 0xdeadbeef;
7908 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7909 uint8_t *input_buffer = NULL;
7910 psa_sign_hash_interruptible_operation_t sign_operation =
7911 psa_sign_hash_interruptible_operation_init();
7912 psa_verify_hash_interruptible_operation_t verify_operation =
7913 psa_verify_hash_interruptible_operation_init();
7914
7915 PSA_ASSERT(psa_crypto_init());
7916
7917 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7918 PSA_KEY_USAGE_VERIFY_HASH);
7919 psa_set_key_algorithm(&attributes, alg);
7920 psa_set_key_type(&attributes, key_type);
7921
7922 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7923 &key));
7924 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7925 key_bits = psa_get_key_bits(&attributes);
7926
7927 /* Allocate a buffer which has the size advertised by the
7928 * library. */
7929 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7930 key_bits, alg);
7931 TEST_ASSERT(signature_size != 0);
7932 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007933 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007934
Paul Elliott20a36062022-12-18 13:21:25 +00007935 /* --- Change function inputs mid run, to cause an error (sign only,
7936 * verify passes all inputs to start. --- */
7937
7938 psa_interruptible_set_max_ops(1);
7939
7940 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7941 input_data->x, input_data->len));
7942
7943 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7944 signature_size,
7945 &signature_length),
7946 PSA_OPERATION_INCOMPLETE);
7947
7948 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7949 0,
7950 &signature_length),
7951 PSA_ERROR_BUFFER_TOO_SMALL);
7952
Paul Elliottc9774412023-02-06 15:14:07 +00007953 /* And test that this invalidates the operation. */
7954 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7955 0,
7956 &signature_length),
7957 PSA_ERROR_BAD_STATE);
7958
Paul Elliott20a36062022-12-18 13:21:25 +00007959 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7960
Paul Elliottf9c91a72023-02-05 18:06:38 +00007961 /* Trash the hash buffer in between start and complete, to ensure
7962 * no reliance on external buffers. */
7963 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7964
Paul Elliott6c68df42023-10-23 15:33:37 +01007965 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007966
7967 memcpy(input_buffer, input_data->x, input_data->len);
7968
7969 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7970 input_buffer, input_data->len));
7971
7972 memset(input_buffer, '!', input_data->len);
7973 mbedtls_free(input_buffer);
7974 input_buffer = NULL;
7975
7976 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7977 signature_size,
7978 &signature_length));
7979
7980 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7981
Paul Elliott6c68df42023-10-23 15:33:37 +01007982 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007983
7984 memcpy(input_buffer, input_data->x, input_data->len);
7985
7986 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7987 input_buffer, input_data->len,
7988 signature, signature_length));
7989
7990 memset(input_buffer, '!', input_data->len);
7991 mbedtls_free(input_buffer);
7992 input_buffer = NULL;
7993
7994 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7995
7996 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7997
Paul Elliott20a36062022-12-18 13:21:25 +00007998exit:
7999 /*
8000 * Key attributes may have been returned by psa_get_key_attributes()
8001 * thus reset them as required.
8002 */
8003 psa_reset_key_attributes(&attributes);
8004
8005 psa_destroy_key(key);
8006 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01008007 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00008008 PSA_DONE();
8009}
8010/* END_CASE */
8011
Paul Elliotta4cb9092023-02-07 18:01:55 +00008012/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00008013/**
Paul Elliott57702242023-02-26 20:36:10 +00008014 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00008015 *
8016 * Note: This test can currently only handle ECDSA.
8017 *
8018 * 1. Test that setting max ops is reflected in both interruptible sign and
8019 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00008020 * 2. Test that changing the value of max_ops to unlimited during an operation
8021 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00008022 *
8023 * 3. Test that calling get_num_ops() between complete calls gives the same
8024 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00008025 */
Paul Elliott57702242023-02-26 20:36:10 +00008026void interruptible_signverify_hash_ops_tests(int key_type_arg,
8027 data_t *key_data, int alg_arg,
8028 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00008029{
8030 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8031 psa_key_type_t key_type = key_type_arg;
8032 psa_algorithm_t alg = alg_arg;
8033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00008034 size_t key_bits;
8035 unsigned char *signature = NULL;
8036 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00008037 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00008038 uint32_t num_ops = 0;
8039 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8040
Paul Elliotta4cb9092023-02-07 18:01:55 +00008041 psa_sign_hash_interruptible_operation_t sign_operation =
8042 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00008043 psa_verify_hash_interruptible_operation_t verify_operation =
8044 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00008045
8046 PSA_ASSERT(psa_crypto_init());
8047
8048 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
8049 PSA_KEY_USAGE_VERIFY_HASH);
8050 psa_set_key_algorithm(&attributes, alg);
8051 psa_set_key_type(&attributes, key_type);
8052
Paul Elliottf1743e22023-02-15 18:44:16 +00008053 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
8054 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8055 key_bits = psa_get_key_bits(&attributes);
8056
8057 /* Allocate a buffer which has the size advertised by the
8058 * library. */
8059 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8060
8061 TEST_ASSERT(signature_size != 0);
8062 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008063 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00008064
8065 /* Check that default max ops gets set if we don't set it. */
8066 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8067 input_data->x, input_data->len));
8068
8069 TEST_EQUAL(psa_interruptible_get_max_ops(),
8070 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8071
8072 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8073
Paul Elliottf1743e22023-02-15 18:44:16 +00008074 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8075 input_data->x, input_data->len,
8076 signature, signature_size));
8077
8078 TEST_EQUAL(psa_interruptible_get_max_ops(),
8079 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8080
8081 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8082
Paul Elliotta4cb9092023-02-07 18:01:55 +00008083 /* Check that max ops gets set properly. */
8084
8085 psa_interruptible_set_max_ops(0xbeef);
8086
Paul Elliottf1743e22023-02-15 18:44:16 +00008087 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00008088
Paul Elliott9e8819f2023-02-26 19:01:35 +00008089 /* --- Ensure changing the max ops mid operation works (operation should
8090 * complete successfully after setting max ops to unlimited --- */
8091 psa_interruptible_set_max_ops(1);
8092
8093 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8094 input_data->x, input_data->len));
8095
8096 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
8097 signature_size,
8098 &signature_length),
8099 PSA_OPERATION_INCOMPLETE);
8100
8101 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8102
8103 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
8104 signature_size,
8105 &signature_length));
8106
8107 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8108
8109 psa_interruptible_set_max_ops(1);
8110
8111 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8112 input_data->x, input_data->len,
8113 signature, signature_length));
8114
8115 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
8116 PSA_OPERATION_INCOMPLETE);
8117
8118 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8119
8120 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
8121
8122 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8123
Paul Elliottc1e04002023-02-26 20:27:23 +00008124 /* --- Test that not calling get_num_ops inbetween complete calls does not
8125 * result in lost ops. ---*/
8126
8127 psa_interruptible_set_max_ops(1);
8128
8129 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8130 input_data->x, input_data->len));
8131
8132 /* Continue performing the signature until complete. */
8133 do {
8134 status = psa_sign_hash_complete(&sign_operation, signature,
8135 signature_size,
8136 &signature_length);
8137
8138 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
8139
8140 } while (status == PSA_OPERATION_INCOMPLETE);
8141
8142 PSA_ASSERT(status);
8143
8144 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8145
8146 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8147 input_data->x, input_data->len));
8148
8149 /* Continue performing the signature until complete. */
8150 do {
8151 status = psa_sign_hash_complete(&sign_operation, signature,
8152 signature_size,
8153 &signature_length);
8154 } while (status == PSA_OPERATION_INCOMPLETE);
8155
8156 PSA_ASSERT(status);
8157
8158 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
8159
8160 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8161
8162 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8163 input_data->x, input_data->len,
8164 signature, signature_length));
8165
8166 /* Continue performing the verification until complete. */
8167 do {
8168 status = psa_verify_hash_complete(&verify_operation);
8169
8170 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
8171
8172 } while (status == PSA_OPERATION_INCOMPLETE);
8173
8174 PSA_ASSERT(status);
8175
8176 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8177
8178 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8179 input_data->x, input_data->len,
8180 signature, signature_length));
8181
8182 /* Continue performing the verification until complete. */
8183 do {
8184 status = psa_verify_hash_complete(&verify_operation);
8185
8186 } while (status == PSA_OPERATION_INCOMPLETE);
8187
8188 PSA_ASSERT(status);
8189
8190 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
8191
8192 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8193
Paul Elliotta4cb9092023-02-07 18:01:55 +00008194exit:
8195 /*
8196 * Key attributes may have been returned by psa_get_key_attributes()
8197 * thus reset them as required.
8198 */
8199 psa_reset_key_attributes(&attributes);
8200
8201 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00008202 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00008203 PSA_DONE();
8204}
8205/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00008206
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008207/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008208void sign_message_deterministic(int key_type_arg,
8209 data_t *key_data,
8210 int alg_arg,
8211 data_t *input_data,
8212 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02008213{
8214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8215 psa_key_type_t key_type = key_type_arg;
8216 psa_algorithm_t alg = alg_arg;
8217 size_t key_bits;
8218 unsigned char *signature = NULL;
8219 size_t signature_size;
8220 size_t signature_length = 0xdeadbeef;
8221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8222
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02008224
Gilles Peskine449bd832023-01-11 14:50:10 +01008225 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8226 psa_set_key_algorithm(&attributes, alg);
8227 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008228
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8230 &key));
8231 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8232 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008233
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8235 TEST_ASSERT(signature_size != 0);
8236 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008237 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008238
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 PSA_ASSERT(psa_sign_message(key, alg,
8240 input_data->x, input_data->len,
8241 signature, signature_size,
8242 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02008243
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008244 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008245 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008246
8247exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008249
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 psa_destroy_key(key);
8251 mbedtls_free(signature);
8252 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02008253
8254}
8255/* END_CASE */
8256
8257/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008258void sign_message_fail(int key_type_arg,
8259 data_t *key_data,
8260 int alg_arg,
8261 data_t *input_data,
8262 int signature_size_arg,
8263 int expected_status_arg)
8264{
8265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8266 psa_key_type_t key_type = key_type_arg;
8267 psa_algorithm_t alg = alg_arg;
8268 size_t signature_size = signature_size_arg;
8269 psa_status_t actual_status;
8270 psa_status_t expected_status = expected_status_arg;
8271 unsigned char *signature = NULL;
8272 size_t signature_length = 0xdeadbeef;
8273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8274
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008275 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008276
8277 PSA_ASSERT(psa_crypto_init());
8278
8279 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8280 psa_set_key_algorithm(&attributes, alg);
8281 psa_set_key_type(&attributes, key_type);
8282
8283 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8284 &key));
8285
8286 actual_status = psa_sign_message(key, alg,
8287 input_data->x, input_data->len,
8288 signature, signature_size,
8289 &signature_length);
8290 TEST_EQUAL(actual_status, expected_status);
8291 /* The value of *signature_length is unspecified on error, but
8292 * whatever it is, it should be less than signature_size, so that
8293 * if the caller tries to read *signature_length bytes without
8294 * checking the error code then they don't overflow a buffer. */
8295 TEST_LE_U(signature_length, signature_size);
8296
8297exit:
8298 psa_reset_key_attributes(&attributes);
8299 psa_destroy_key(key);
8300 mbedtls_free(signature);
8301 PSA_DONE();
8302}
8303/* END_CASE */
8304
8305/* BEGIN_CASE */
8306void sign_verify_message(int key_type_arg,
8307 data_t *key_data,
8308 int alg_arg,
8309 data_t *input_data)
8310{
8311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8312 psa_key_type_t key_type = key_type_arg;
8313 psa_algorithm_t alg = alg_arg;
8314 size_t key_bits;
8315 unsigned char *signature = NULL;
8316 size_t signature_size;
8317 size_t signature_length = 0xdeadbeef;
8318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8319
8320 PSA_ASSERT(psa_crypto_init());
8321
8322 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8323 PSA_KEY_USAGE_VERIFY_MESSAGE);
8324 psa_set_key_algorithm(&attributes, alg);
8325 psa_set_key_type(&attributes, key_type);
8326
8327 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8328 &key));
8329 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8330 key_bits = psa_get_key_bits(&attributes);
8331
8332 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8333 TEST_ASSERT(signature_size != 0);
8334 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008335 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008336
8337 PSA_ASSERT(psa_sign_message(key, alg,
8338 input_data->x, input_data->len,
8339 signature, signature_size,
8340 &signature_length));
8341 TEST_LE_U(signature_length, signature_size);
8342 TEST_ASSERT(signature_length > 0);
8343
8344 PSA_ASSERT(psa_verify_message(key, alg,
8345 input_data->x, input_data->len,
8346 signature, signature_length));
8347
8348 if (input_data->len != 0) {
8349 /* Flip a bit in the input and verify that the signature is now
8350 * detected as invalid. Flip a bit at the beginning, not at the end,
8351 * because ECDSA may ignore the last few bits of the input. */
8352 input_data->x[0] ^= 1;
8353 TEST_EQUAL(psa_verify_message(key, alg,
8354 input_data->x, input_data->len,
8355 signature, signature_length),
8356 PSA_ERROR_INVALID_SIGNATURE);
8357 }
8358
8359exit:
8360 psa_reset_key_attributes(&attributes);
8361
8362 psa_destroy_key(key);
8363 mbedtls_free(signature);
8364 PSA_DONE();
8365}
8366/* END_CASE */
8367
8368/* BEGIN_CASE */
8369void verify_message(int key_type_arg,
8370 data_t *key_data,
8371 int alg_arg,
8372 data_t *input_data,
8373 data_t *signature_data)
8374{
8375 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8376 psa_key_type_t key_type = key_type_arg;
8377 psa_algorithm_t alg = alg_arg;
8378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8379
8380 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8381
8382 PSA_ASSERT(psa_crypto_init());
8383
8384 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8385 psa_set_key_algorithm(&attributes, alg);
8386 psa_set_key_type(&attributes, key_type);
8387
8388 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8389 &key));
8390
8391 PSA_ASSERT(psa_verify_message(key, alg,
8392 input_data->x, input_data->len,
8393 signature_data->x, signature_data->len));
8394
8395exit:
8396 psa_reset_key_attributes(&attributes);
8397 psa_destroy_key(key);
8398 PSA_DONE();
8399}
8400/* END_CASE */
8401
8402/* BEGIN_CASE */
8403void verify_message_fail(int key_type_arg,
8404 data_t *key_data,
8405 int alg_arg,
8406 data_t *hash_data,
8407 data_t *signature_data,
8408 int expected_status_arg)
8409{
8410 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8411 psa_key_type_t key_type = key_type_arg;
8412 psa_algorithm_t alg = alg_arg;
8413 psa_status_t actual_status;
8414 psa_status_t expected_status = expected_status_arg;
8415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8416
8417 PSA_ASSERT(psa_crypto_init());
8418
8419 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8420 psa_set_key_algorithm(&attributes, alg);
8421 psa_set_key_type(&attributes, key_type);
8422
8423 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8424 &key));
8425
8426 actual_status = psa_verify_message(key, alg,
8427 hash_data->x, hash_data->len,
8428 signature_data->x,
8429 signature_data->len);
8430 TEST_EQUAL(actual_status, expected_status);
8431
8432exit:
8433 psa_reset_key_attributes(&attributes);
8434 psa_destroy_key(key);
8435 PSA_DONE();
8436}
8437/* END_CASE */
8438
8439/* BEGIN_CASE */
8440void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008441 data_t *key_data,
8442 int alg_arg,
8443 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 data_t *label,
8445 int expected_output_length_arg,
8446 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008447{
Ronald Cron5425a212020-08-04 14:58:35 +02008448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008449 psa_key_type_t key_type = key_type_arg;
8450 psa_algorithm_t alg = alg_arg;
8451 size_t expected_output_length = expected_output_length_arg;
8452 size_t key_bits;
8453 unsigned char *output = NULL;
8454 size_t output_size;
8455 size_t output_length = ~0;
8456 psa_status_t actual_status;
8457 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008459
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008461
Gilles Peskine656896e2018-06-29 19:12:28 +02008462 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8464 psa_set_key_algorithm(&attributes, alg);
8465 psa_set_key_type(&attributes, key_type);
8466 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8467 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008468
8469 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8471 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008472
Gilles Peskine449bd832023-01-11 14:50:10 +01008473 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8474 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008475 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008476
8477 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008478 actual_status = psa_asymmetric_encrypt(key, alg,
8479 input_data->x, input_data->len,
8480 label->x, label->len,
8481 output, output_size,
8482 &output_length);
8483 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008484 if (actual_status == PSA_SUCCESS) {
8485 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008486 } else {
8487 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008488 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008489
Gilles Peskine68428122018-06-30 18:42:41 +02008490 /* If the label is empty, the test framework puts a non-null pointer
8491 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008493 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 if (output_size != 0) {
8495 memset(output, 0, output_size);
8496 }
8497 actual_status = psa_asymmetric_encrypt(key, alg,
8498 input_data->x, input_data->len,
8499 NULL, label->len,
8500 output, output_size,
8501 &output_length);
8502 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008503 if (actual_status == PSA_SUCCESS) {
8504 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008505 } else {
8506 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008507 }
Gilles Peskine68428122018-06-30 18:42:41 +02008508 }
8509
Gilles Peskine656896e2018-06-29 19:12:28 +02008510exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008511 /*
8512 * Key attributes may have been returned by psa_get_key_attributes()
8513 * thus reset them as required.
8514 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008515 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008516
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 psa_destroy_key(key);
8518 mbedtls_free(output);
8519 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008520}
8521/* END_CASE */
8522
8523/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008524void asymmetric_encrypt_decrypt(int key_type_arg,
8525 data_t *key_data,
8526 int alg_arg,
8527 data_t *input_data,
8528 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008529{
Ronald Cron5425a212020-08-04 14:58:35 +02008530 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008531 psa_key_type_t key_type = key_type_arg;
8532 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008533 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008534 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008535 size_t output_size;
8536 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008537 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008538 size_t output2_size;
8539 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008541
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008543
Gilles Peskine449bd832023-01-11 14:50:10 +01008544 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8545 psa_set_key_algorithm(&attributes, alg);
8546 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008547
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8549 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008550
8551 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8553 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008554
Gilles Peskine449bd832023-01-11 14:50:10 +01008555 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8556 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008557 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008558
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008559 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008560 TEST_LE_U(output2_size,
8561 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8562 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008563 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008564
Gilles Peskineeebd7382018-06-08 18:11:54 +02008565 /* We test encryption by checking that encrypt-then-decrypt gives back
8566 * the original plaintext because of the non-optional random
8567 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8569 input_data->x, input_data->len,
8570 label->x, label->len,
8571 output, output_size,
8572 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008573 /* We don't know what ciphertext length to expect, but check that
8574 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008576
Gilles Peskine449bd832023-01-11 14:50:10 +01008577 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8578 output, output_length,
8579 label->x, label->len,
8580 output2, output2_size,
8581 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008582 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008583 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008584
8585exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008586 /*
8587 * Key attributes may have been returned by psa_get_key_attributes()
8588 * thus reset them as required.
8589 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008590 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008591
Gilles Peskine449bd832023-01-11 14:50:10 +01008592 psa_destroy_key(key);
8593 mbedtls_free(output);
8594 mbedtls_free(output2);
8595 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008596}
8597/* END_CASE */
8598
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008599/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008600void asymmetric_decrypt(int key_type_arg,
8601 data_t *key_data,
8602 int alg_arg,
8603 data_t *input_data,
8604 data_t *label,
8605 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008606{
Ronald Cron5425a212020-08-04 14:58:35 +02008607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008608 psa_key_type_t key_type = key_type_arg;
8609 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008610 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008611 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008612 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008613 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008615
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008617
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8619 psa_set_key_algorithm(&attributes, alg);
8620 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008621
Gilles Peskine449bd832023-01-11 14:50:10 +01008622 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8623 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008624
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8626 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008627
8628 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8630 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008631 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8634 input_data->x, input_data->len,
8635 label->x, label->len,
8636 output,
8637 output_size,
8638 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008639 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008640 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008641
Gilles Peskine68428122018-06-30 18:42:41 +02008642 /* If the label is empty, the test framework puts a non-null pointer
8643 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008645 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 if (output_size != 0) {
8647 memset(output, 0, output_size);
8648 }
8649 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8650 input_data->x, input_data->len,
8651 NULL, label->len,
8652 output,
8653 output_size,
8654 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008655 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008656 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008657 }
8658
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008659exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008660 psa_reset_key_attributes(&attributes);
8661 psa_destroy_key(key);
8662 mbedtls_free(output);
8663 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008664}
8665/* END_CASE */
8666
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008667/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008668void asymmetric_decrypt_fail(int key_type_arg,
8669 data_t *key_data,
8670 int alg_arg,
8671 data_t *input_data,
8672 data_t *label,
8673 int output_size_arg,
8674 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008675{
Ronald Cron5425a212020-08-04 14:58:35 +02008676 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008677 psa_key_type_t key_type = key_type_arg;
8678 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008679 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008680 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008681 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008682 psa_status_t actual_status;
8683 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008685
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008686 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008687
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008689
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8691 psa_set_key_algorithm(&attributes, alg);
8692 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008693
Gilles Peskine449bd832023-01-11 14:50:10 +01008694 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8695 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008696
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 actual_status = psa_asymmetric_decrypt(key, alg,
8698 input_data->x, input_data->len,
8699 label->x, label->len,
8700 output, output_size,
8701 &output_length);
8702 TEST_EQUAL(actual_status, expected_status);
8703 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008704
Gilles Peskine68428122018-06-30 18:42:41 +02008705 /* If the label is empty, the test framework puts a non-null pointer
8706 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008708 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 if (output_size != 0) {
8710 memset(output, 0, output_size);
8711 }
8712 actual_status = psa_asymmetric_decrypt(key, alg,
8713 input_data->x, input_data->len,
8714 NULL, label->len,
8715 output, output_size,
8716 &output_length);
8717 TEST_EQUAL(actual_status, expected_status);
8718 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008719 }
8720
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008721exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 psa_reset_key_attributes(&attributes);
8723 psa_destroy_key(key);
8724 mbedtls_free(output);
8725 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008726}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008727/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008728
8729/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008730void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008731{
8732 /* Test each valid way of initializing the object, except for `= {0}`, as
8733 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8734 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008735 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008736 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008737 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008738 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8739 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008740
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008742
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008743 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8745 PSA_ERROR_BAD_STATE);
8746 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8747 PSA_ERROR_BAD_STATE);
8748 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8749 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008750
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008751 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 PSA_ASSERT(psa_key_derivation_abort(&func));
8753 PSA_ASSERT(psa_key_derivation_abort(&init));
8754 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008755}
8756/* END_CASE */
8757
Janos Follath16de4a42019-06-13 16:32:24 +01008758/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008759void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008760{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008761 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008762 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008763 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008764
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008766
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8768 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008769
8770exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 psa_key_derivation_abort(&operation);
8772 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008773}
8774/* END_CASE */
8775
Janos Follathaf3c2a02019-06-12 12:34:34 +01008776/* BEGIN_CASE */
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05308777void derive_set_capacity(int alg_arg, int64_t capacity_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01008778 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008779{
8780 psa_algorithm_t alg = alg_arg;
8781 size_t capacity = capacity_arg;
8782 psa_status_t expected_status = expected_status_arg;
8783 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8784
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008786
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008788
Gilles Peskine449bd832023-01-11 14:50:10 +01008789 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8790 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008791
8792exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 psa_key_derivation_abort(&operation);
8794 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008795}
8796/* END_CASE */
8797
8798/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308799void parse_binary_string_test(data_t *input, int output)
8800{
8801 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308802 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308803 TEST_EQUAL(value, output);
8804}
8805/* END_CASE */
8806
8807/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008808void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308809 int step_arg1, int key_type_arg1, data_t *input1,
8810 int expected_status_arg1,
8811 int step_arg2, int key_type_arg2, data_t *input2,
8812 int expected_status_arg2,
8813 int step_arg3, int key_type_arg3, data_t *input3,
8814 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008816{
8817 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308819 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 psa_status_t expected_statuses[] = { expected_status_arg1,
8821 expected_status_arg2,
8822 expected_status_arg3 };
8823 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008824 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8825 MBEDTLS_SVC_KEY_ID_INIT,
8826 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008827 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8829 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008830 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008831 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008832 psa_status_t expected_output_status = expected_output_status_arg;
8833 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008834
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008836
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8838 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008839
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008841
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8843 mbedtls_test_set_step(i);
8844 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008845 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308846 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8847 key_types[i] != INPUT_INTEGER) {
8848 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008849 PSA_ASSERT(psa_import_key(&attributes,
8850 inputs[i]->x, inputs[i]->len,
8851 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308852 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008854 // When taking a private key as secret input, use key agreement
8855 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
Ryan Everett73e4ea32024-03-12 16:29:55 +00008857 &operation, keys[i], 0),
Gilles Peskine449bd832023-01-11 14:50:10 +01008858 expected_statuses[i]);
8859 } else {
8860 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8861 keys[i]),
8862 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008863 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308865 if (key_types[i] == INPUT_INTEGER) {
8866 TEST_EQUAL(psa_key_derivation_input_integer(
8867 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308868 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308869 expected_statuses[i]);
8870 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308871 TEST_EQUAL(psa_key_derivation_input_bytes(
8872 &operation, steps[i],
8873 inputs[i]->x, inputs[i]->len),
8874 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308875 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008876 }
8877 }
8878
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 if (output_key_type != PSA_KEY_TYPE_NONE) {
8880 psa_reset_key_attributes(&attributes);
8881 psa_set_key_type(&attributes, output_key_type);
8882 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008883 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 psa_key_derivation_output_key(&attributes, &operation,
8885 &output_key);
8886 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008887 uint8_t buffer[1];
8888 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008889 psa_key_derivation_output_bytes(&operation,
8890 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008891 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008893
Janos Follathaf3c2a02019-06-12 12:34:34 +01008894exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008895 psa_key_derivation_abort(&operation);
8896 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8897 psa_destroy_key(keys[i]);
8898 }
8899 psa_destroy_key(output_key);
8900 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008901}
8902/* END_CASE */
8903
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308904/* BEGIN_CASE*/
8905void derive_input_invalid_cost(int alg_arg, int64_t cost)
8906{
8907 psa_algorithm_t alg = alg_arg;
8908 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8909
8910 PSA_ASSERT(psa_crypto_init());
8911 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8912
8913 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8914 PSA_KEY_DERIVATION_INPUT_COST,
8915 cost),
8916 PSA_ERROR_NOT_SUPPORTED);
8917
8918exit:
8919 psa_key_derivation_abort(&operation);
8920 PSA_DONE();
8921}
8922/* END_CASE*/
8923
Janos Follathd958bb72019-07-03 15:02:16 +01008924/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008925void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008926{
Janos Follathd958bb72019-07-03 15:02:16 +01008927 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008928 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008929 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008930 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008931 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008933 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008934 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008935 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008936 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008937 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8938 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008941
Gilles Peskine449bd832023-01-11 14:50:10 +01008942 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008943
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8945 psa_set_key_algorithm(&attributes, alg);
8946 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008947
Gilles Peskine449bd832023-01-11 14:50:10 +01008948 PSA_ASSERT(psa_import_key(&attributes,
8949 key_data, sizeof(key_data),
8950 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008951
8952 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008953 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8954 input1, input1_length,
8955 input2, input2_length,
Ryan Everettc1cc6682024-03-12 16:17:43 +00008956 capacity, 0)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008957 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008959
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008960 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8962 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008963
Gilles Peskine449bd832023-01-11 14:50:10 +01008964 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008965
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8967 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008968
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008970 psa_key_derivation_abort(&operation);
8971 psa_destroy_key(key);
8972 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008973}
8974/* END_CASE */
8975
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008976/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008977void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008978{
8979 uint8_t output_buffer[16];
8980 size_t buffer_size = 16;
8981 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008982 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008983
Gilles Peskine449bd832023-01-11 14:50:10 +01008984 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8985 output_buffer, buffer_size)
8986 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008987
Gilles Peskine449bd832023-01-11 14:50:10 +01008988 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8989 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008990
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008992
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8994 output_buffer, buffer_size)
8995 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008996
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8998 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008999
9000exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03009002}
9003/* END_CASE */
9004
9005/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009006void derive_output(int alg_arg,
9007 int step1_arg, data_t *input1, int expected_status_arg1,
9008 int step2_arg, data_t *input2, int expected_status_arg2,
9009 int step3_arg, data_t *input3, int expected_status_arg3,
9010 int step4_arg, data_t *input4, int expected_status_arg4,
9011 data_t *key_agreement_peer_key,
9012 int requested_capacity_arg,
9013 data_t *expected_output1,
9014 data_t *expected_output2,
9015 int other_key_input_type,
9016 int key_input_type,
9017 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009018{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009019 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
9021 data_t *inputs[] = { input1, input2, input3, input4 };
9022 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
9023 MBEDTLS_SVC_KEY_ID_INIT,
9024 MBEDTLS_SVC_KEY_ID_INIT,
9025 MBEDTLS_SVC_KEY_ID_INIT };
9026 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
9027 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009028 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009029 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009030 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009032 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01009033 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009034 size_t output_buffer_size = 0;
9035 uint8_t *output_buffer = NULL;
9036 size_t expected_capacity;
9037 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009038 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
9039 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
9040 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
9041 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009042 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009043 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02009044 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009045
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
9047 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009048 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009049 }
9050 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009051 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009052 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009053 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009054 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009055 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009056
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009057 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009058 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9059 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
9060 requested_capacity));
9061 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
9062 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02009063 case 0:
9064 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309065 case PSA_KEY_DERIVATION_INPUT_COST:
9066 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05309067 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05309068 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05309069 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309070 if (statuses[i] != PSA_SUCCESS) {
9071 goto exit;
9072 }
9073 break;
9074 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02009075 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01009076 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009077 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01009078 TEST_EQUAL(psa_key_derivation_input_bytes(
9079 &operation, steps[i],
9080 inputs[i]->x, inputs[i]->len),
9081 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02009082
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02009084 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009086 break;
9087 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
9089 psa_set_key_algorithm(&attributes1, alg);
9090 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009091
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 PSA_ASSERT(psa_import_key(&attributes1,
9093 inputs[i]->x, inputs[i]->len,
9094 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009095
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
9097 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
9098 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
9099 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009100 }
9101
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309102 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309104 keys[i]),
9105 statuses[i]);
9106
9107 if (statuses[i] != PSA_SUCCESS) {
9108 goto exit;
9109 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009110 break;
9111 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009112 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009113 break;
9114 }
9115 break;
9116 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01009117 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009118 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01009119 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9120 steps[i],
9121 inputs[i]->x,
9122 inputs[i]->len),
9123 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009124 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02009125 case 1: // input key, type DERIVE
9126 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
9128 psa_set_key_algorithm(&attributes2, alg);
9129 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009130
9131 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01009132 if (other_key_input_type == 11) {
9133 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
9134 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 PSA_ASSERT(psa_import_key(&attributes2,
9137 inputs[i]->x, inputs[i]->len,
9138 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009139
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 TEST_EQUAL(psa_key_derivation_input_key(&operation,
9141 steps[i],
9142 keys[i]),
9143 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009144 break;
9145 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
9147 psa_set_key_algorithm(&attributes3, alg);
9148 psa_set_key_type(&attributes3,
9149 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009150
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 PSA_ASSERT(psa_import_key(&attributes3,
9152 inputs[i]->x, inputs[i]->len,
9153 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009154
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 TEST_EQUAL(psa_key_derivation_key_agreement(
9156 &operation,
9157 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
9158 keys[i], key_agreement_peer_key->x,
9159 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009160 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009161 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009162 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009163 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01009164 }
9165
Gilles Peskine449bd832023-01-11 14:50:10 +01009166 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009167 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009168 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009169 break;
9170 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009171 TEST_EQUAL(psa_key_derivation_input_bytes(
9172 &operation, steps[i],
9173 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02009174
Gilles Peskine449bd832023-01-11 14:50:10 +01009175 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02009176 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009177 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009178 break;
9179 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01009180 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009181
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9183 &current_capacity));
9184 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009185 expected_capacity = requested_capacity;
9186
Gilles Peskine449bd832023-01-11 14:50:10 +01009187 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009188 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
9189
9190 /* For output key derivation secret must be provided using
9191 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009193 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01009194 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009195
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
9197 psa_set_key_algorithm(&attributes4, alg);
9198 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
9199 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009200
Gilles Peskine449bd832023-01-11 14:50:10 +01009201 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
9202 &derived_key), expected_status);
9203 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009204 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009205 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009206 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009207 status = psa_key_derivation_output_bytes(&operation,
9208 output_buffer, output_sizes[i]);
9209 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009210 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009211 TEST_ASSERT(status == PSA_SUCCESS ||
9212 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009213 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 } else if (expected_capacity == 0 ||
9215 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009216 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009218 expected_capacity = 0;
9219 continue;
9220 }
9221 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009222 PSA_ASSERT(status);
9223 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009224 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009225 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009227 /* Check the operation status. */
9228 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009229 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9230 &current_capacity));
9231 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009232 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009233 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009234 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009235
9236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009237 mbedtls_free(output_buffer);
9238 psa_key_derivation_abort(&operation);
9239 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
9240 psa_destroy_key(keys[i]);
9241 }
9242 psa_destroy_key(derived_key);
9243 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009244}
9245/* END_CASE */
9246
9247/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009248void derive_full(int alg_arg,
9249 data_t *key_data,
9250 data_t *input1,
9251 data_t *input2,
9252 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02009253{
Ronald Cron5425a212020-08-04 14:58:35 +02009254 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009255 psa_algorithm_t alg = alg_arg;
9256 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05309258 unsigned char output_buffer[32];
Gilles Peskined54931c2018-07-17 21:06:59 +02009259 size_t expected_capacity = requested_capacity;
9260 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009262
Gilles Peskine449bd832023-01-11 14:50:10 +01009263 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02009264
Gilles Peskine449bd832023-01-11 14:50:10 +01009265 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9266 psa_set_key_algorithm(&attributes, alg);
9267 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02009268
Gilles Peskine449bd832023-01-11 14:50:10 +01009269 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9270 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02009271
Gilles Peskine449bd832023-01-11 14:50:10 +01009272 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
9273 input1->x, input1->len,
9274 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009275 requested_capacity, 0)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01009276 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009277 }
Janos Follath47f27ed2019-06-25 13:24:52 +01009278
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9280 &current_capacity));
9281 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009282
9283 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009284 while (current_capacity > 0) {
9285 size_t read_size = sizeof(output_buffer);
9286 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02009287 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 }
9289 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9290 output_buffer,
9291 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02009292 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9294 &current_capacity));
9295 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009296 }
9297
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009298 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
9300 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02009301
Gilles Peskine449bd832023-01-11 14:50:10 +01009302 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02009303
9304exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009305 psa_key_derivation_abort(&operation);
9306 psa_destroy_key(key);
9307 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02009308}
9309/* END_CASE */
9310
Stephan Koch78109f52023-04-12 14:19:36 +02009311/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01009312void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
9313 int derivation_step,
9314 int capacity, int expected_capacity_status_arg,
9315 data_t *expected_output,
9316 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009317{
9318 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9319 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009320 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009321 uint8_t *output_buffer = NULL;
9322 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009323 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9324 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9325 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009326
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009327 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009328 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009329
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9331 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9332 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009333
Gilles Peskine449bd832023-01-11 14:50:10 +01009334 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9335 step, input->x, input->len),
9336 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009337
Gilles Peskine449bd832023-01-11 14:50:10 +01009338 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009339 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009340 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009341
Gilles Peskine449bd832023-01-11 14:50:10 +01009342 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9343 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009344
Gilles Peskine449bd832023-01-11 14:50:10 +01009345 TEST_EQUAL(status, expected_output_status);
9346 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009347 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009348 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009349 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009350
9351exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009352 mbedtls_free(output_buffer);
9353 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009354 PSA_DONE();
9355}
9356/* END_CASE */
9357
Janos Follathe60c9052019-07-03 13:51:30 +01009358/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009359void derive_key_exercise(int alg_arg,
9360 data_t *key_data,
9361 data_t *input1,
9362 data_t *input2,
9363 int derived_type_arg,
9364 int derived_bits_arg,
9365 int derived_usage_arg,
9366 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009367{
Ronald Cron5425a212020-08-04 14:58:35 +02009368 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9369 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009370 psa_algorithm_t alg = alg_arg;
9371 psa_key_type_t derived_type = derived_type_arg;
9372 size_t derived_bits = derived_bits_arg;
9373 psa_key_usage_t derived_usage = derived_usage_arg;
9374 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009376 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009378 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009379
Gilles Peskine449bd832023-01-11 14:50:10 +01009380 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009381
Gilles Peskine449bd832023-01-11 14:50:10 +01009382 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9383 psa_set_key_algorithm(&attributes, alg);
9384 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9385 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9386 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009387
9388 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009389 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9390 input1->x, input1->len,
9391 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009392 capacity, 0)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009393 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009394 }
Janos Follathe60c9052019-07-03 13:51:30 +01009395
Gilles Peskine449bd832023-01-11 14:50:10 +01009396 psa_set_key_usage_flags(&attributes, derived_usage);
9397 psa_set_key_algorithm(&attributes, derived_alg);
9398 psa_set_key_type(&attributes, derived_type);
9399 psa_set_key_bits(&attributes, derived_bits);
9400 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9401 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009402
9403 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9405 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9406 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009407
9408 /* Exercise the derived key. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00009409 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg, 0)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009410 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009412
9413exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009414 /*
9415 * Key attributes may have been returned by psa_get_key_attributes()
9416 * thus reset them as required.
9417 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009419
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 psa_key_derivation_abort(&operation);
9421 psa_destroy_key(base_key);
9422 psa_destroy_key(derived_key);
9423 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009424}
9425/* END_CASE */
9426
Janos Follath42fd8882019-07-03 14:17:09 +01009427/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009428void derive_key_export(int alg_arg,
9429 data_t *key_data,
9430 data_t *input1,
9431 data_t *input2,
9432 int bytes1_arg,
9433 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009434{
Ronald Cron5425a212020-08-04 14:58:35 +02009435 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9436 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009437 psa_algorithm_t alg = alg_arg;
9438 size_t bytes1 = bytes1_arg;
9439 size_t bytes2 = bytes2_arg;
9440 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009441 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009442 uint8_t *output_buffer = NULL;
9443 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009444 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9445 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009446 size_t length;
9447
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009448 TEST_CALLOC(output_buffer, capacity);
9449 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009450 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009451
Gilles Peskine449bd832023-01-11 14:50:10 +01009452 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9453 psa_set_key_algorithm(&base_attributes, alg);
9454 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9455 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9456 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009457
9458 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009459 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9460 input1->x, input1->len,
9461 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009462 capacity, 0)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009463 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009464 }
Janos Follath42fd8882019-07-03 14:17:09 +01009465
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9467 output_buffer,
9468 capacity));
9469 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009470
9471 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9473 input1->x, input1->len,
9474 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009475 capacity, 0)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009476 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 }
Janos Follath42fd8882019-07-03 14:17:09 +01009478
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9480 psa_set_key_algorithm(&derived_attributes, 0);
9481 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9482 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9483 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9484 &derived_key));
9485 PSA_ASSERT(psa_export_key(derived_key,
9486 export_buffer, bytes1,
9487 &length));
9488 TEST_EQUAL(length, bytes1);
9489 PSA_ASSERT(psa_destroy_key(derived_key));
9490 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9491 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9492 &derived_key));
9493 PSA_ASSERT(psa_export_key(derived_key,
9494 export_buffer + bytes1, bytes2,
9495 &length));
9496 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009497
9498 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009499 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009500 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009501
9502exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009503 mbedtls_free(output_buffer);
9504 mbedtls_free(export_buffer);
9505 psa_key_derivation_abort(&operation);
9506 psa_destroy_key(base_key);
9507 psa_destroy_key(derived_key);
9508 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009509}
9510/* END_CASE */
9511
9512/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009513void derive_key_type(int alg_arg,
9514 data_t *key_data,
9515 data_t *input1,
9516 data_t *input2,
9517 int key_type_arg, int bits_arg,
9518 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009519{
9520 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9521 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9522 const psa_algorithm_t alg = alg_arg;
9523 const psa_key_type_t key_type = key_type_arg;
9524 const size_t bits = bits_arg;
9525 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9526 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009527 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009528 uint8_t *export_buffer = NULL;
9529 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9530 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9531 size_t export_length;
9532
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009533 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009534 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009535
Gilles Peskine449bd832023-01-11 14:50:10 +01009536 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9537 psa_set_key_algorithm(&base_attributes, alg);
9538 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9539 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9540 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009541
Gilles Peskine449bd832023-01-11 14:50:10 +01009542 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009543 &operation, base_key, alg,
9544 input1->x, input1->len,
9545 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009546 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009547 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009548 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009549
Gilles Peskine449bd832023-01-11 14:50:10 +01009550 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9551 psa_set_key_algorithm(&derived_attributes, 0);
9552 psa_set_key_type(&derived_attributes, key_type);
9553 psa_set_key_bits(&derived_attributes, bits);
9554 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9555 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009556
Gilles Peskine449bd832023-01-11 14:50:10 +01009557 PSA_ASSERT(psa_export_key(derived_key,
9558 export_buffer, export_buffer_size,
9559 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009560 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009561 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009562
9563exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009564 mbedtls_free(export_buffer);
9565 psa_key_derivation_abort(&operation);
9566 psa_destroy_key(base_key);
9567 psa_destroy_key(derived_key);
9568 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009569}
9570/* END_CASE */
9571
9572/* BEGIN_CASE */
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009573void derive_key_ext(int alg_arg,
9574 data_t *key_data,
9575 data_t *input1,
9576 data_t *input2,
9577 int key_type_arg, int bits_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009578 int flags_arg,
Gilles Peskine092ce512024-02-20 12:31:24 +01009579 data_t *params_data,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009580 psa_status_t expected_status,
9581 data_t *expected_export)
9582{
9583 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9584 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9585 const psa_algorithm_t alg = alg_arg;
9586 const psa_key_type_t key_type = key_type_arg;
9587 const size_t bits = bits_arg;
Gilles Peskine092ce512024-02-20 12:31:24 +01009588 psa_key_production_parameters_t *params = NULL;
9589 size_t params_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009590 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9591 const size_t export_buffer_size =
9592 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
9593 uint8_t *export_buffer = NULL;
9594 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9595 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9596 size_t export_length;
9597
9598 TEST_CALLOC(export_buffer, export_buffer_size);
9599 PSA_ASSERT(psa_crypto_init());
9600
9601 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9602 psa_set_key_algorithm(&base_attributes, alg);
9603 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9604 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9605 &base_key));
9606
9607 if (mbedtls_test_psa_setup_key_derivation_wrap(
9608 &operation, base_key, alg,
9609 input1->x, input1->len,
9610 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009611 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009612 goto exit;
9613 }
9614
9615 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9616 psa_set_key_algorithm(&derived_attributes, 0);
9617 psa_set_key_type(&derived_attributes, key_type);
9618 psa_set_key_bits(&derived_attributes, bits);
Gilles Peskine092ce512024-02-20 12:31:24 +01009619 if (!setup_key_production_parameters(&params, &params_data_length,
9620 flags_arg, params_data)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009621 goto exit;
9622 }
9623
9624 TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01009625 params, params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009626 &derived_key),
9627 expected_status);
9628
9629 if (expected_status == PSA_SUCCESS) {
9630 PSA_ASSERT(psa_export_key(derived_key,
9631 export_buffer, export_buffer_size,
9632 &export_length));
9633 TEST_MEMORY_COMPARE(export_buffer, export_length,
9634 expected_export->x, expected_export->len);
9635 }
9636
9637exit:
9638 mbedtls_free(export_buffer);
Gilles Peskine092ce512024-02-20 12:31:24 +01009639 mbedtls_free(params);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009640 psa_key_derivation_abort(&operation);
9641 psa_destroy_key(base_key);
9642 psa_destroy_key(derived_key);
9643 PSA_DONE();
9644}
9645/* END_CASE */
9646
9647/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009648void derive_key(int alg_arg,
9649 data_t *key_data, data_t *input1, data_t *input2,
9650 int type_arg, int bits_arg,
9651 int expected_status_arg,
9652 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009653{
Ronald Cron5425a212020-08-04 14:58:35 +02009654 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9655 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009656 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009657 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009658 size_t bits = bits_arg;
9659 psa_status_t expected_status = expected_status_arg;
9660 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9661 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9662 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9663
Gilles Peskine449bd832023-01-11 14:50:10 +01009664 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009665
Gilles Peskine449bd832023-01-11 14:50:10 +01009666 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9667 psa_set_key_algorithm(&base_attributes, alg);
9668 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9669 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9670 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009671
Gilles Peskine449bd832023-01-11 14:50:10 +01009672 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9673 input1->x, input1->len,
9674 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009675 SIZE_MAX, 0)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009676 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009677 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009678
Gilles Peskine449bd832023-01-11 14:50:10 +01009679 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9680 psa_set_key_algorithm(&derived_attributes, 0);
9681 psa_set_key_type(&derived_attributes, type);
9682 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009683
9684 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 psa_key_derivation_output_key(&derived_attributes,
9686 &operation,
9687 &derived_key);
9688 if (is_large_output > 0) {
9689 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9690 }
9691 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009692
9693exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009694 psa_key_derivation_abort(&operation);
9695 psa_destroy_key(base_key);
9696 psa_destroy_key(derived_key);
9697 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009698}
9699/* END_CASE */
9700
9701/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009702void key_agreement_setup(int alg_arg,
9703 int our_key_type_arg, int our_key_alg_arg,
9704 data_t *our_key_data, data_t *peer_key_data,
9705 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009706{
Ronald Cron5425a212020-08-04 14:58:35 +02009707 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009708 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009709 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009710 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009711 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009713 psa_status_t expected_status = expected_status_arg;
9714 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009715
Gilles Peskine449bd832023-01-11 14:50:10 +01009716 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009717
Gilles Peskine449bd832023-01-11 14:50:10 +01009718 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9719 psa_set_key_algorithm(&attributes, our_key_alg);
9720 psa_set_key_type(&attributes, our_key_type);
9721 PSA_ASSERT(psa_import_key(&attributes,
9722 our_key_data->x, our_key_data->len,
9723 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009724
Gilles Peskine77f40d82019-04-11 21:27:06 +02009725 /* The tests currently include inputs that should fail at either step.
9726 * Test cases that fail at the setup step should be changed to call
9727 * key_derivation_setup instead, and this function should be renamed
9728 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009729 status = psa_key_derivation_setup(&operation, alg);
9730 if (status == PSA_SUCCESS) {
9731 TEST_EQUAL(psa_key_derivation_key_agreement(
9732 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9733 our_key,
9734 peer_key_data->x, peer_key_data->len),
9735 expected_status);
9736 } else {
9737 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009738 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009739
9740exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009741 psa_key_derivation_abort(&operation);
9742 psa_destroy_key(our_key);
9743 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009744}
9745/* END_CASE */
9746
9747/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009748void raw_key_agreement(int alg_arg,
9749 int our_key_type_arg, data_t *our_key_data,
9750 data_t *peer_key_data,
9751 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009752{
Ronald Cron5425a212020-08-04 14:58:35 +02009753 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009754 psa_algorithm_t alg = alg_arg;
9755 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009756 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009757 unsigned char *output = NULL;
9758 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009759 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009760
Gilles Peskine449bd832023-01-11 14:50:10 +01009761 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009762
Gilles Peskine449bd832023-01-11 14:50:10 +01009763 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9764 psa_set_key_algorithm(&attributes, alg);
9765 psa_set_key_type(&attributes, our_key_type);
9766 PSA_ASSERT(psa_import_key(&attributes,
9767 our_key_data->x, our_key_data->len,
9768 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009769
Gilles Peskine449bd832023-01-11 14:50:10 +01009770 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9771 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009772
Gilles Peskine992bee82022-04-13 23:25:52 +02009773 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009774 TEST_LE_U(expected_output->len,
9775 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9776 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9777 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009778
9779 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009780 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009781 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9782 peer_key_data->x, peer_key_data->len,
9783 output, expected_output->len,
9784 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009785 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009786 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009787 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009788 output = NULL;
9789 output_length = ~0;
9790
9791 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009792 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009793 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9794 peer_key_data->x, peer_key_data->len,
9795 output, expected_output->len + 1,
9796 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009797 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009798 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009799 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009800 output = NULL;
9801 output_length = ~0;
9802
9803 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009804 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009805 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9806 peer_key_data->x, peer_key_data->len,
9807 output, expected_output->len - 1,
9808 &output_length),
9809 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009810 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009811 TEST_LE_U(output_length, expected_output->len - 1);
9812 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009813 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009814
9815exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009816 mbedtls_free(output);
9817 psa_destroy_key(our_key);
9818 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009819}
9820/* END_CASE */
9821
9822/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009823void key_agreement_capacity(int alg_arg,
9824 int our_key_type_arg, data_t *our_key_data,
9825 data_t *peer_key_data,
9826 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009827{
Ronald Cron5425a212020-08-04 14:58:35 +02009828 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009829 psa_algorithm_t alg = alg_arg;
9830 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009831 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009832 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009833 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009834 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009835
Gilles Peskine449bd832023-01-11 14:50:10 +01009836 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009837
Gilles Peskine449bd832023-01-11 14:50:10 +01009838 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9839 psa_set_key_algorithm(&attributes, alg);
9840 psa_set_key_type(&attributes, our_key_type);
9841 PSA_ASSERT(psa_import_key(&attributes,
9842 our_key_data->x, our_key_data->len,
9843 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009844
Gilles Peskine449bd832023-01-11 14:50:10 +01009845 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9846 PSA_ASSERT(psa_key_derivation_key_agreement(
9847 &operation,
9848 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9849 peer_key_data->x, peer_key_data->len));
9850 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009851 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009852 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9853 PSA_KEY_DERIVATION_INPUT_INFO,
9854 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009855 }
Gilles Peskine59685592018-09-18 12:11:34 +02009856
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009857 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009858 PSA_ASSERT(psa_key_derivation_get_capacity(
9859 &operation, &actual_capacity));
9860 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009861
Gilles Peskinebf491972018-10-25 22:36:12 +02009862 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009863 while (actual_capacity > sizeof(output)) {
9864 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9865 output, sizeof(output)));
9866 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009867 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009868 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9869 output, actual_capacity));
9870 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9871 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009872
Gilles Peskine59685592018-09-18 12:11:34 +02009873exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009874 psa_key_derivation_abort(&operation);
9875 psa_destroy_key(our_key);
9876 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009877}
9878/* END_CASE */
9879
Valerio Settiad819672023-12-29 12:14:41 +01009880/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9881void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009882{
9883 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009884 psa_ecc_family_t ecc_family = psa_family_arg;
9885 size_t bits = bits_arg;
9886 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009887
Valerio Settiad819672023-12-29 12:14:41 +01009888 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9889 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009890 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009891}
9892/* END_CASE */
9893
Valerio Settiac739522024-01-04 10:22:01 +01009894/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9895void ecc_conversion_functions_fail()
9896{
9897 size_t bits;
9898
Valerio Settidb6e0292024-01-05 10:15:45 +01009899 /* Invalid legacy curve identifiers. */
9900 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9901 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009902 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9903 TEST_EQUAL(0, bits);
9904
9905 /* Invalid PSA EC family. */
9906 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9907 /* Invalid bit-size for a valid EC family. */
9908 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9909
9910 /* Twisted-Edward curves are not supported yet. */
9911 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9912 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9913 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9914 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9915}
9916/* END_CASE */
9917
9918
Gilles Peskine59685592018-09-18 12:11:34 +02009919/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009920void key_agreement_output(int alg_arg,
9921 int our_key_type_arg, data_t *our_key_data,
9922 data_t *peer_key_data,
9923 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009924{
Ronald Cron5425a212020-08-04 14:58:35 +02009925 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009926 psa_algorithm_t alg = alg_arg;
9927 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009928 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009930 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009931
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009932 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009933 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009934
Gilles Peskine449bd832023-01-11 14:50:10 +01009935 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009936
Gilles Peskine449bd832023-01-11 14:50:10 +01009937 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9938 psa_set_key_algorithm(&attributes, alg);
9939 psa_set_key_type(&attributes, our_key_type);
9940 PSA_ASSERT(psa_import_key(&attributes,
9941 our_key_data->x, our_key_data->len,
9942 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009943
Gilles Peskine449bd832023-01-11 14:50:10 +01009944 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9945 PSA_ASSERT(psa_key_derivation_key_agreement(
9946 &operation,
9947 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9948 peer_key_data->x, peer_key_data->len));
9949 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009950 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009951 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9952 PSA_KEY_DERIVATION_INPUT_INFO,
9953 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009954 }
Gilles Peskine59685592018-09-18 12:11:34 +02009955
Gilles Peskine449bd832023-01-11 14:50:10 +01009956 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9957 actual_output,
9958 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009959 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009960 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009961 if (expected_output2->len != 0) {
9962 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9963 actual_output,
9964 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009965 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009966 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009967 }
Gilles Peskine59685592018-09-18 12:11:34 +02009968
9969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009970 psa_key_derivation_abort(&operation);
9971 psa_destroy_key(our_key);
9972 PSA_DONE();
9973 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009974}
9975/* END_CASE */
9976
9977/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009978void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009979{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009980 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009981 unsigned char *output = NULL;
9982 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009983 size_t i;
9984 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009985
Gilles Peskine449bd832023-01-11 14:50:10 +01009986 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009987
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009988 TEST_CALLOC(output, bytes);
9989 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009990
Gilles Peskine449bd832023-01-11 14:50:10 +01009991 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009992
Gilles Peskinea50d7392018-06-21 10:22:13 +02009993 /* Run several times, to ensure that every output byte will be
9994 * nonzero at least once with overwhelming probability
9995 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009996 for (run = 0; run < 10; run++) {
9997 if (bytes != 0) {
9998 memset(output, 0, bytes);
9999 }
10000 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +020010001
Gilles Peskine449bd832023-01-11 14:50:10 +010010002 for (i = 0; i < bytes; i++) {
10003 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +020010004 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +010010005 }
Gilles Peskinea50d7392018-06-21 10:22:13 +020010006 }
Gilles Peskine05d69892018-06-19 22:00:52 +020010007 }
Gilles Peskinea50d7392018-06-21 10:22:13 +020010008
10009 /* Check that every byte was changed to nonzero at least once. This
10010 * validates that psa_generate_random is overwriting every byte of
10011 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010012 for (i = 0; i < bytes; i++) {
10013 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +020010014 }
Gilles Peskine05d69892018-06-19 22:00:52 +020010015
10016exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010017 PSA_DONE();
10018 mbedtls_free(output);
10019 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +020010020}
10021/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +020010022
Ryan3a1b7862024-03-01 17:24:04 +000010023#if defined MBEDTLS_THREADING_PTHREAD
10024
10025/* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */
10026void concurrently_generate_keys(int type_arg,
10027 int bits_arg,
10028 int usage_arg,
10029 int alg_arg,
10030 int expected_status_arg,
10031 int is_large_key_arg,
10032 int arg_thread_count,
10033 int reps_arg)
10034{
10035 size_t thread_count = (size_t) arg_thread_count;
10036 mbedtls_test_thread_t *threads = NULL;
10037 generate_key_context gkc;
10038 gkc.type = type_arg;
10039 gkc.usage = usage_arg;
10040 gkc.bits = bits_arg;
10041 gkc.alg = alg_arg;
10042 gkc.expected_status = expected_status_arg;
10043 gkc.is_large_key = is_large_key_arg;
10044 gkc.reps = reps_arg;
10045 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10046
10047 PSA_ASSERT(psa_crypto_init());
10048
10049 psa_set_key_usage_flags(&attributes, usage_arg);
10050 psa_set_key_algorithm(&attributes, alg_arg);
10051 psa_set_key_type(&attributes, type_arg);
10052 psa_set_key_bits(&attributes, bits_arg);
10053 gkc.attributes = &attributes;
10054
10055 TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count);
10056
10057 /* Split threads to generate key then destroy key. */
10058 for (size_t i = 0; i < thread_count; i++) {
10059 TEST_EQUAL(
10060 mbedtls_test_thread_create(&threads[i], thread_generate_key,
10061 (void *) &gkc), 0);
10062 }
10063
10064 /* Join threads. */
10065 for (size_t i = 0; i < thread_count; i++) {
10066 TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0);
10067 }
10068
10069exit:
10070 mbedtls_free(threads);
10071 PSA_DONE();
10072}
10073/* END_CASE */
10074#endif
10075
Gilles Peskine12313cd2018-06-20 00:20:32 +020010076/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010077void generate_key(int type_arg,
10078 int bits_arg,
10079 int usage_arg,
10080 int alg_arg,
10081 int expected_status_arg,
10082 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +020010083{
Ronald Cron5425a212020-08-04 14:58:35 +020010084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +020010085 psa_key_type_t type = type_arg;
10086 psa_key_usage_t usage = usage_arg;
10087 size_t bits = bits_arg;
10088 psa_algorithm_t alg = alg_arg;
10089 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +020010090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +020010091 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +020010092
Gilles Peskine449bd832023-01-11 14:50:10 +010010093 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +020010094
Gilles Peskine449bd832023-01-11 14:50:10 +010010095 psa_set_key_usage_flags(&attributes, usage);
10096 psa_set_key_algorithm(&attributes, alg);
10097 psa_set_key_type(&attributes, type);
10098 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +020010099
10100 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010101 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +010010102
Gilles Peskine449bd832023-01-11 14:50:10 +010010103 if (is_large_key > 0) {
10104 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
10105 }
10106 TEST_EQUAL(status, expected_status);
10107 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +020010108 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010109 }
Gilles Peskine12313cd2018-06-20 00:20:32 +020010110
10111 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +010010112 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
10113 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
10114 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +020010115
Gilles Peskine818ca122018-06-20 18:16:48 +020010116 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010117 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine02b75072018-07-01 22:31:34 +020010118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010119 }
Gilles Peskine12313cd2018-06-20 00:20:32 +020010120
10121exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010122 /*
10123 * Key attributes may have been returned by psa_get_key_attributes()
10124 * thus reset them as required.
10125 */
Gilles Peskine449bd832023-01-11 14:50:10 +010010126 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010127
Gilles Peskine449bd832023-01-11 14:50:10 +010010128 psa_destroy_key(key);
10129 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +020010130}
10131/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +030010132
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010133/* BEGIN_CASE */
10134void generate_key_ext(int type_arg,
10135 int bits_arg,
10136 int usage_arg,
10137 int alg_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +010010138 int flags_arg,
Gilles Peskine092ce512024-02-20 12:31:24 +010010139 data_t *params_data,
Gilles Peskine449bd832023-01-11 14:50:10 +010010140 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +020010141{
Ronald Cron5425a212020-08-04 14:58:35 +020010142 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010143 psa_key_type_t type = type_arg;
10144 psa_key_usage_t usage = usage_arg;
Gilles Peskinee56e8782019-04-26 17:34:02 +020010145 size_t bits = bits_arg;
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010146 psa_algorithm_t alg = alg_arg;
Gilles Peskinee56e8782019-04-26 17:34:02 +020010147 psa_status_t expected_status = expected_status_arg;
10148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine092ce512024-02-20 12:31:24 +010010149 psa_key_production_parameters_t *params = NULL;
10150 size_t params_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010151 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinee56e8782019-04-26 17:34:02 +020010152
Gilles Peskine449bd832023-01-11 14:50:10 +010010153 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +020010154
Gilles Peskine449bd832023-01-11 14:50:10 +010010155 psa_set_key_usage_flags(&attributes, usage);
10156 psa_set_key_algorithm(&attributes, alg);
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010157 psa_set_key_type(&attributes, type);
Gilles Peskine449bd832023-01-11 14:50:10 +010010158 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +020010159
Gilles Peskine092ce512024-02-20 12:31:24 +010010160 if (!setup_key_production_parameters(&params, &params_data_length,
10161 flags_arg, params_data)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010162 goto exit;
10163 }
10164
Gilles Peskinee56e8782019-04-26 17:34:02 +020010165 /* Generate a key */
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010166 psa_status_t status = psa_generate_key_ext(&attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +010010167 params, params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010168 &key);
10169
10170 TEST_EQUAL(status, expected_status);
Gilles Peskine449bd832023-01-11 14:50:10 +010010171 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +020010172 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010173 }
Gilles Peskinee56e8782019-04-26 17:34:02 +020010174
10175 /* Test the key information */
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010176 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
10177 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
10178 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Pengyu Lvd90fbf72023-12-08 17:13:22 +080010179
Gilles Peskine7a18f962024-02-12 16:48:11 +010010180#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
10181 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +010010182 TEST_ASSERT(rsa_test_e(key, bits, params_data));
Gilles Peskine449bd832023-01-11 14:50:10 +010010183 }
Pengyu Lv9e976f32023-12-06 16:58:05 +080010184#endif
Gilles Peskinee56e8782019-04-26 17:34:02 +020010185
10186 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010187 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +020010188 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010189 }
Gilles Peskinee56e8782019-04-26 17:34:02 +020010190
Gilles Peskinee56e8782019-04-26 17:34:02 +020010191exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010192 /*
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010193 * Key attributes may have been returned by psa_get_key_attributes()
10194 * thus reset them as required.
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010195 */
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010196 psa_reset_key_attributes(&got_attributes);
Gilles Peskine092ce512024-02-20 12:31:24 +010010197 mbedtls_free(params);
Gilles Peskine449bd832023-01-11 14:50:10 +010010198 psa_destroy_key(key);
10199 PSA_DONE();
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010200}
10201/* END_CASE */
10202
10203/* BEGIN_CASE */
Gilles Peskine092ce512024-02-20 12:31:24 +010010204void key_production_parameters_init()
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010205{
Gilles Peskine092ce512024-02-20 12:31:24 +010010206 psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT;
10207 psa_key_production_parameters_t zero;
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010208 memset(&zero, 0, sizeof(zero));
10209
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010210 TEST_EQUAL(init.flags, 0);
10211 TEST_EQUAL(zero.flags, 0);
Gilles Peskinee56e8782019-04-26 17:34:02 +020010212}
10213/* END_CASE */
10214
Darryl Greend49a4992018-06-18 17:27:26 +010010215/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010010216void persistent_key_load_key_from_storage(data_t *data,
10217 int type_arg, int bits_arg,
10218 int usage_flags_arg, int alg_arg,
10219 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +010010220{
Gilles Peskine449bd832023-01-11 14:50:10 +010010221 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010222 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +020010223 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10224 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010225 psa_key_type_t type = type_arg;
10226 size_t bits = bits_arg;
10227 psa_key_usage_t usage_flags = usage_flags_arg;
10228 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +020010229 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +010010230 unsigned char *first_export = NULL;
10231 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010010232 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -070010233 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +010010234 size_t second_exported_length;
10235
Gilles Peskine449bd832023-01-11 14:50:10 +010010236 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010237 TEST_CALLOC(first_export, export_size);
10238 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010239 }
Darryl Greend49a4992018-06-18 17:27:26 +010010240
Gilles Peskine449bd832023-01-11 14:50:10 +010010241 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010242
Gilles Peskine449bd832023-01-11 14:50:10 +010010243 psa_set_key_id(&attributes, key_id);
10244 psa_set_key_usage_flags(&attributes, usage_flags);
10245 psa_set_key_algorithm(&attributes, alg);
10246 psa_set_key_type(&attributes, type);
10247 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +010010248
Gilles Peskine449bd832023-01-11 14:50:10 +010010249 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010250 case IMPORT_KEY:
10251 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010252 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
10253 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010254 break;
Darryl Greend49a4992018-06-18 17:27:26 +010010255
Darryl Green0c6575a2018-11-07 16:05:30 +000010256 case GENERATE_KEY:
10257 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010258 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010259 break;
10260
10261 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +010010262#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +010010263 {
10264 /* Create base key */
10265 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
10266 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
10267 psa_set_key_usage_flags(&base_attributes,
10268 PSA_KEY_USAGE_DERIVE);
10269 psa_set_key_algorithm(&base_attributes, derive_alg);
10270 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
10271 PSA_ASSERT(psa_import_key(&base_attributes,
10272 data->x, data->len,
10273 &base_key));
10274 /* Derive a key. */
10275 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
10276 PSA_ASSERT(psa_key_derivation_input_key(
10277 &operation,
10278 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
10279 PSA_ASSERT(psa_key_derivation_input_bytes(
10280 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
10281 NULL, 0));
10282 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
10283 &operation,
10284 &key));
10285 PSA_ASSERT(psa_key_derivation_abort(&operation));
10286 PSA_ASSERT(psa_destroy_key(base_key));
10287 base_key = MBEDTLS_SVC_KEY_ID_INIT;
10288 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010289#else
Gilles Peskine449bd832023-01-11 14:50:10 +010010290 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010291#endif
10292 break;
10293
10294 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +010010295 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010296 break;
Darryl Green0c6575a2018-11-07 16:05:30 +000010297 }
Gilles Peskine449bd832023-01-11 14:50:10 +010010298 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +010010299
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010300 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010301 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10302 PSA_ASSERT(psa_export_key(key,
10303 first_export, export_size,
10304 &first_exported_length));
10305 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010306 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010307 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +010010308 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010309 }
Darryl Greend49a4992018-06-18 17:27:26 +010010310
10311 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +010010312 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010313 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +010010314 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010315
Darryl Greend49a4992018-06-18 17:27:26 +010010316 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +010010317 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
10318 TEST_ASSERT(mbedtls_svc_key_id_equal(
10319 psa_get_key_id(&attributes), key_id));
10320 TEST_EQUAL(psa_get_key_lifetime(&attributes),
10321 PSA_KEY_LIFETIME_PERSISTENT);
10322 TEST_EQUAL(psa_get_key_type(&attributes), type);
10323 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
10324 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
10325 mbedtls_test_update_key_usage_flags(usage_flags));
10326 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +010010327
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010328 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010329 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10330 PSA_ASSERT(psa_export_key(key,
10331 second_export, export_size,
10332 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010333 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010334 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +000010335 }
10336
10337 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010338 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg, 0)) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010339 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010340 }
Darryl Greend49a4992018-06-18 17:27:26 +010010341
10342exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010343 /*
10344 * Key attributes may have been returned by psa_get_key_attributes()
10345 * thus reset them as required.
10346 */
Gilles Peskine449bd832023-01-11 14:50:10 +010010347 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010348
Gilles Peskine449bd832023-01-11 14:50:10 +010010349 mbedtls_free(first_export);
10350 mbedtls_free(second_export);
10351 psa_key_derivation_abort(&operation);
10352 psa_destroy_key(base_key);
10353 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010354 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +010010355}
10356/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +020010357
Neil Armstronga557cb82022-06-10 08:58:32 +020010358/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010359void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
10360 int primitive_arg, int hash_arg, int role_arg,
10361 int test_input, data_t *pw_data,
10362 int inj_err_type_arg,
10363 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010364{
10365 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10366 psa_pake_operation_t operation = psa_pake_operation_init();
10367 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010368 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +020010369 psa_key_type_t key_type_pw = key_type_pw_arg;
10370 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010371 psa_algorithm_t hash_alg = hash_arg;
10372 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010375 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
10376 psa_status_t expected_error = expected_error_arg;
10377 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010378 unsigned char *output_buffer = NULL;
10379 size_t output_len = 0;
10380
Gilles Peskine449bd832023-01-11 14:50:10 +010010381 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010382
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010383 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +010010384 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010385 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010386
Gilles Peskine449bd832023-01-11 14:50:10 +010010387 if (pw_data->len > 0) {
10388 psa_set_key_usage_flags(&attributes, key_usage_pw);
10389 psa_set_key_algorithm(&attributes, alg);
10390 psa_set_key_type(&attributes, key_type_pw);
10391 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10392 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010393 }
10394
Gilles Peskine449bd832023-01-11 14:50:10 +010010395 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10396 psa_pake_cs_set_primitive(&cipher_suite, primitive);
10397 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010398
Gilles Peskine449bd832023-01-11 14:50:10 +010010399 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +020010400
Gilles Peskine449bd832023-01-11 14:50:10 +010010401 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
10402 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10403 expected_error);
10404 PSA_ASSERT(psa_pake_abort(&operation));
10405 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10406 expected_error);
10407 PSA_ASSERT(psa_pake_abort(&operation));
10408 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10409 expected_error);
10410 PSA_ASSERT(psa_pake_abort(&operation));
10411 TEST_EQUAL(psa_pake_set_role(&operation, role),
10412 expected_error);
10413 PSA_ASSERT(psa_pake_abort(&operation));
10414 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10415 NULL, 0, NULL),
10416 expected_error);
10417 PSA_ASSERT(psa_pake_abort(&operation));
10418 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10419 expected_error);
10420 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010421 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010422 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010423
Gilles Peskine449bd832023-01-11 14:50:10 +010010424 status = psa_pake_setup(&operation, &cipher_suite);
10425 if (status != PSA_SUCCESS) {
10426 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010427 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010428 }
10429
Gilles Peskine449bd832023-01-11 14:50:10 +010010430 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10431 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10432 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010433 goto exit;
10434 }
10435
Gilles Peskine449bd832023-01-11 14:50:10 +010010436 status = psa_pake_set_role(&operation, role);
10437 if (status != PSA_SUCCESS) {
10438 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010439 goto exit;
10440 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010441
Gilles Peskine449bd832023-01-11 14:50:10 +010010442 if (pw_data->len > 0) {
10443 status = psa_pake_set_password_key(&operation, key);
10444 if (status != PSA_SUCCESS) {
10445 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010446 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010447 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010448 }
10449
Gilles Peskine449bd832023-01-11 14:50:10 +010010450 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10451 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10452 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010453 goto exit;
10454 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010455
Gilles Peskine449bd832023-01-11 14:50:10 +010010456 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10457 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10458 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010459 goto exit;
10460 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010461
Gilles Peskine449bd832023-01-11 14:50:10 +010010462 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010463 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010464 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10465 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010466 goto exit;
10467 }
10468
Gilles Peskine449bd832023-01-11 14:50:10 +010010469 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010470 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010471 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10472 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010473 goto exit;
10474 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010475
Gilles Peskine449bd832023-01-11 14:50:10 +010010476 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10477 PSA_PAKE_STEP_KEY_SHARE);
10478 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10479 PSA_PAKE_STEP_ZK_PUBLIC);
10480 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10481 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010482
Gilles Peskine449bd832023-01-11 14:50:10 +010010483 if (test_input) {
10484 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10485 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10486 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010487 goto exit;
10488 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010489
Gilles Peskine449bd832023-01-11 14:50:10 +010010490 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10491 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10492 output_buffer, size_zk_proof),
10493 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010494 goto exit;
10495 }
10496
Gilles Peskine449bd832023-01-11 14:50:10 +010010497 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10498 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10499 output_buffer, size_zk_proof),
10500 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010501 goto exit;
10502 }
10503
Gilles Peskine449bd832023-01-11 14:50:10 +010010504 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10505 output_buffer, size_key_share);
10506 if (status != PSA_SUCCESS) {
10507 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010508 goto exit;
10509 }
10510
Gilles Peskine449bd832023-01-11 14:50:10 +010010511 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10512 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10513 output_buffer, size_zk_public + 1),
10514 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010515 goto exit;
10516 }
10517
Gilles Peskine449bd832023-01-11 14:50:10 +010010518 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010519 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010520 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10521 output_buffer, size_zk_public + 1);
10522 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10523 output_buffer, size_zk_public),
10524 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010525 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010526 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010527 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010528 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10529 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10530 NULL, 0, NULL),
10531 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010532 goto exit;
10533 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010534
Gilles Peskine449bd832023-01-11 14:50:10 +010010535 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10536 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10537 output_buffer, buf_size, &output_len),
10538 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010539 goto exit;
10540 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010541
Gilles Peskine449bd832023-01-11 14:50:10 +010010542 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10543 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10544 output_buffer, buf_size, &output_len),
10545 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010546 goto exit;
10547 }
10548
Gilles Peskine449bd832023-01-11 14:50:10 +010010549 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10550 output_buffer, buf_size, &output_len);
10551 if (status != PSA_SUCCESS) {
10552 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010553 goto exit;
10554 }
10555
Gilles Peskine449bd832023-01-11 14:50:10 +010010556 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010557
Gilles Peskine449bd832023-01-11 14:50:10 +010010558 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10559 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10560 output_buffer, size_zk_public - 1, &output_len),
10561 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010562 goto exit;
10563 }
10564
Gilles Peskine449bd832023-01-11 14:50:10 +010010565 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010566 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010567 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10568 output_buffer, size_zk_public - 1, &output_len);
10569 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10570 output_buffer, buf_size, &output_len),
10571 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010572 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010573 }
10574 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010575
10576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010577 PSA_ASSERT(psa_destroy_key(key));
10578 PSA_ASSERT(psa_pake_abort(&operation));
10579 mbedtls_free(output_buffer);
10580 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010581}
10582/* END_CASE */
10583
Neil Armstronga557cb82022-06-10 08:58:32 +020010584/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010585void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10586 int client_input_first, int inject_error,
10587 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010588{
10589 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10590 psa_pake_operation_t server = psa_pake_operation_init();
10591 psa_pake_operation_t client = psa_pake_operation_init();
10592 psa_algorithm_t alg = alg_arg;
10593 psa_algorithm_t hash_alg = hash_arg;
10594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10595 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10596
Gilles Peskine449bd832023-01-11 14:50:10 +010010597 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010598
Gilles Peskine449bd832023-01-11 14:50:10 +010010599 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10600 psa_set_key_algorithm(&attributes, alg);
10601 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10602 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10603 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010604
Gilles Peskine449bd832023-01-11 14:50:10 +010010605 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10606 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10607 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010608
10609
Gilles Peskine449bd832023-01-11 14:50:10 +010010610 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10611 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010612
Gilles Peskine449bd832023-01-11 14:50:10 +010010613 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10614 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010615
Gilles Peskine449bd832023-01-11 14:50:10 +010010616 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10617 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010618
Gilles Peskine449bd832023-01-11 14:50:10 +010010619 ecjpake_do_round(alg, primitive_arg, &server, &client,
10620 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010621
Gilles Peskine449bd832023-01-11 14:50:10 +010010622 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010624 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010625
Gilles Peskine449bd832023-01-11 14:50:10 +010010626 ecjpake_do_round(alg, primitive_arg, &server, &client,
10627 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010628
10629exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010630 psa_destroy_key(key);
10631 psa_pake_abort(&server);
10632 psa_pake_abort(&client);
10633 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010634}
10635/* END_CASE */
10636
10637/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010638void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10639 int derive_alg_arg, data_t *pw_data,
10640 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010641{
10642 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10643 psa_pake_operation_t server = psa_pake_operation_init();
10644 psa_pake_operation_t client = psa_pake_operation_init();
10645 psa_algorithm_t alg = alg_arg;
10646 psa_algorithm_t hash_alg = hash_arg;
10647 psa_algorithm_t derive_alg = derive_alg_arg;
10648 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10650 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010651 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010652 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010653 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010654 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010655
Gilles Peskine449bd832023-01-11 14:50:10 +010010656 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010657
Gilles Peskine449bd832023-01-11 14:50:10 +010010658 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10659 psa_set_key_algorithm(&attributes, alg);
10660 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10661 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10662 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010663
Gilles Peskine449bd832023-01-11 14:50:10 +010010664 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10665 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10666 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010667
Neil Armstrong1e855602022-06-15 11:32:11 +020010668 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010669 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10670 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010671
Gilles Peskine449bd832023-01-11 14:50:10 +010010672 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10673 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10674 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10675 PSA_KEY_DERIVATION_INPUT_SEED,
10676 (const uint8_t *) "", 0));
10677 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10678 PSA_KEY_DERIVATION_INPUT_SEED,
10679 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010680 }
10681
Gilles Peskine449bd832023-01-11 14:50:10 +010010682 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10683 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010684
Gilles Peskine449bd832023-01-11 14:50:10 +010010685 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10686 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010687
Gilles Peskine449bd832023-01-11 14:50:10 +010010688 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10689 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010690
Gilles Peskine449bd832023-01-11 14:50:10 +010010691 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10692 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10693 PSA_ERROR_BAD_STATE);
10694 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10695 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010696 goto exit;
10697 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010698
Neil Armstrongf983caf2022-06-15 15:27:48 +020010699 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010700 ecjpake_do_round(alg, primitive_arg, &server, &client,
10701 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010702
Gilles Peskine449bd832023-01-11 14:50:10 +010010703 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10704 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10705 PSA_ERROR_BAD_STATE);
10706 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10707 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010708 goto exit;
10709 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010710
Neil Armstrongf983caf2022-06-15 15:27:48 +020010711 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010712 ecjpake_do_round(alg, primitive_arg, &server, &client,
10713 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010714
Gilles Peskine449bd832023-01-11 14:50:10 +010010715 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10716 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010717
10718exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010719 psa_key_derivation_abort(&server_derive);
10720 psa_key_derivation_abort(&client_derive);
10721 psa_destroy_key(key);
10722 psa_pake_abort(&server);
10723 psa_pake_abort(&client);
10724 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010725}
10726/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010727
10728/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010729void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010730{
10731 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10732 const size_t bits = 256;
10733 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010734 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010735 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010736 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010737
10738 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10739 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010740 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10741 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10742 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10743 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010744 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010745 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10746 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010747
10748 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010749 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10750 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10751 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10752 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10753 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10754 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010755
10756 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010757 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10758 PSA_PAKE_OUTPUT_MAX_SIZE);
10759 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10760 PSA_PAKE_OUTPUT_MAX_SIZE);
10761 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10762 PSA_PAKE_OUTPUT_MAX_SIZE);
10763 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10764 PSA_PAKE_INPUT_MAX_SIZE);
10765 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10766 PSA_PAKE_INPUT_MAX_SIZE);
10767 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10768 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010769}
10770/* END_CASE */