blob: b9ebe6766f6cd46b5916eae5fc6e43cec95a1a2a [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 Peskine01bf6312022-11-23 14:15:57 +01007#include "common.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02009/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
10 * uses mbedtls_ctr_drbg internally. */
11#include "mbedtls/ctr_drbg.h"
12
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020013#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020014#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015
David Horstmannb0a01b12023-10-30 20:16:41 +000016/* For psa_can_do_hash() and buffer copying functions */
17#include "psa_crypto_core.h"
18
Gilles Peskine8e94efe2021-02-13 00:25:53 +010019#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010020#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010021#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010022
Gilles Peskinef6279312021-05-27 13:21:20 +020023/* If this comes up, it's a bug in the test code or in the test data. */
24#define UNUSED 0xdeadbeef
25
Dave Rodgman34b147d2021-06-23 12:49:59 +010026/* Assert that an operation is (not) active.
27 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010028#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
29#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Dave Rodgman34b147d2021-06-23 12:49:59 +010030
Jaeden Amerof24c7f82018-06-27 17:20:43 +010031/** An invalid export length that will never be set by psa_export_key(). */
32static const size_t INVALID_EXPORT_LENGTH = ~0U;
33
Gilles Peskinea7aa4422018-08-14 15:17:54 +020034/** Test if a buffer contains a constant byte value.
35 *
36 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037 *
38 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020039 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 * \param size Size of the buffer in bytes.
41 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 * \return 1 if the buffer is all-bits-zero.
43 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020044 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046{
47 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048 for (i = 0; i < size; i++) {
49 if (((unsigned char *) buffer)[i] != c) {
50 return 0;
51 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020052 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054}
Andrzej Kureke0015962022-01-17 15:29:38 +010055#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020056/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057static int asn1_write_10x(unsigned char **p,
58 unsigned char *start,
59 size_t bits,
60 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020061{
62 int ret;
63 int len = bits / 8 + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 if (bits == 0) {
65 return MBEDTLS_ERR_ASN1_INVALID_DATA;
66 }
67 if (bits <= 8 && x >= 1 << (bits - 1)) {
68 return MBEDTLS_ERR_ASN1_INVALID_DATA;
69 }
70 if (*p < start || *p - start < (ptrdiff_t) len) {
71 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
72 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +020073 *p -= len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 (*p)[len-1] = x;
75 if (bits % 8 == 0) {
76 (*p)[1] |= 1;
77 } else {
78 (*p)[0] |= 1 << (bits % 8);
79 }
80 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
81 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
82 MBEDTLS_ASN1_INTEGER));
83 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +020084}
85
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086static int construct_fake_rsa_key(unsigned char *buffer,
87 size_t buffer_size,
88 unsigned char **p,
89 size_t bits,
90 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020091{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +020093 int ret;
94 int len = 0;
95 /* Construct something that looks like a DER encoding of
96 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
97 * RSAPrivateKey ::= SEQUENCE {
98 * version Version,
99 * modulus INTEGER, -- n
100 * publicExponent INTEGER, -- e
101 * privateExponent INTEGER, -- d
102 * prime1 INTEGER, -- p
103 * prime2 INTEGER, -- q
104 * exponent1 INTEGER, -- d mod (p-1)
105 * exponent2 INTEGER, -- d mod (q-1)
106 * coefficient INTEGER, -- (inverse of q) mod p
107 * otherPrimeInfos OtherPrimeInfos OPTIONAL
108 * }
109 * Or, for a public key, the same structure with only
110 * version, modulus and publicExponent.
111 */
112 *p = buffer + buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 if (keypair) {
114 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
115 asn1_write_10x(p, buffer, half_bits, 1));
116 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
117 asn1_write_10x(p, buffer, half_bits, 1));
118 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
119 asn1_write_10x(p, buffer, half_bits, 1));
120 MBEDTLS_ASN1_CHK_ADD(len, /* q */
121 asn1_write_10x(p, buffer, half_bits, 1));
122 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
123 asn1_write_10x(p, buffer, half_bits, 3));
124 MBEDTLS_ASN1_CHK_ADD(len, /* d */
125 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200126 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
128 asn1_write_10x(p, buffer, 17, 1));
129 MBEDTLS_ASN1_CHK_ADD(len, /* n */
130 asn1_write_10x(p, buffer, bits, 1));
131 if (keypair) {
132 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
133 mbedtls_asn1_write_int(p, buffer, 0));
134 }
135 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200136 {
137 const unsigned char tag =
138 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200140 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142}
Andrzej Kureke0015962022-01-17 15:29:38 +0100143#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145int exercise_mac_setup(psa_key_type_t key_type,
146 const unsigned char *key_bytes,
147 size_t key_length,
148 psa_algorithm_t alg,
149 psa_mac_operation_t *operation,
150 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151{
Ronald Cron5425a212020-08-04 14:58:35 +0200152 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
156 psa_set_key_algorithm(&attributes, alg);
157 psa_set_key_type(&attributes, key_type);
158 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100161 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100163 /* If setup failed, reproduce the failure, so that the caller can
164 * test the resulting state of the operation object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 if (*status != PSA_SUCCESS) {
166 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100167 }
168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 psa_destroy_key(key);
170 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100171
172exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 psa_destroy_key(key);
174 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175}
176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177int exercise_cipher_setup(psa_key_type_t key_type,
178 const unsigned char *key_bytes,
179 size_t key_length,
180 psa_algorithm_t alg,
181 psa_cipher_operation_t *operation,
182 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183{
Ronald Cron5425a212020-08-04 14:58:35 +0200184 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
188 psa_set_key_algorithm(&attributes, alg);
189 psa_set_key_type(&attributes, key_type);
190 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100193 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100195 /* If setup failed, reproduce the failure, so that the caller can
196 * test the resulting state of the operation object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 if (*status != PSA_SUCCESS) {
198 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
199 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 psa_destroy_key(key);
203 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100204
205exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 psa_destroy_key(key);
207 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100208}
209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 psa_set_key_id(&attributes, key_id);
219 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
220 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
221 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
222 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
223 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200226 TEST_EQUAL(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
228 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
229 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
230 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
231 TEST_EQUAL(psa_get_key_type(&attributes), 0);
232 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
235 PSA_ERROR_INVALID_HANDLE);
236 TEST_EQUAL(psa_export_public_key(key,
237 buffer, sizeof(buffer), &length),
238 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 TEST_EQUAL(psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number), \
261 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200262 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265#define ASSERT_NO_SLOT_NUMBER(attributes) \
266 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273#define KEY_BITS_FROM_DATA(type, data) \
274 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Gilles Peskinee59236f2018-01-27 23:32:46 +0100282/* END_HEADER */
283
284/* BEGIN_DEPENDENCIES
285 * depends_on:MBEDTLS_PSA_CRYPTO_C
286 * END_DEPENDENCIES
287 */
288
289/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200291{
292 size_t max_truncated_mac_size =
293 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
294
295 /* Check that the length for a truncated MAC always fits in the algorithm
296 * encoding. The shifted mask is the maximum truncated value. The
297 * untruncated algorithm may be one byte larger. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskine841b14b2019-11-26 17:37:37 +0100299
300#if defined(MBEDTLS_TEST_DEPRECATED)
301 /* Check deprecated constants. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 TEST_EQUAL(PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR);
303 TEST_EQUAL(PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS);
304 TEST_EQUAL(PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST);
305 TEST_EQUAL(PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA);
306 TEST_EQUAL(PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED);
307 TEST_EQUAL(PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH);
308 TEST_EQUAL(PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH);
309 TEST_EQUAL(PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskineb87b7192019-12-04 16:24:10 +0100310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 TEST_EQUAL(PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1);
312 TEST_EQUAL(PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1);
313 TEST_EQUAL(PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1);
314 TEST_EQUAL(PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1);
315 TEST_EQUAL(PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1);
316 TEST_EQUAL(PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1);
317 TEST_EQUAL(PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1);
318 TEST_EQUAL(PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1);
319 TEST_EQUAL(PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1);
320 TEST_EQUAL(PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1);
321 TEST_EQUAL(PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2);
322 TEST_EQUAL(PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1);
323 TEST_EQUAL(PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1);
324 TEST_EQUAL(PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1);
325 TEST_EQUAL(PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1);
326 TEST_EQUAL(PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1);
327 TEST_EQUAL(PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1);
328 TEST_EQUAL(PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1);
329 TEST_EQUAL(PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1);
330 TEST_EQUAL(PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1);
331 TEST_EQUAL(PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1);
332 TEST_EQUAL(PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1);
333 TEST_EQUAL(PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1);
334 TEST_EQUAL(PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2);
335 TEST_EQUAL(PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2);
336 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
337 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
338 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
339 TEST_EQUAL(PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY);
340 TEST_EQUAL(PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY);
Paul Elliott8ff510a2020-06-02 17:19:28 +0100341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 TEST_EQUAL(PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1);
343 TEST_EQUAL(PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1);
344 TEST_EQUAL(PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2);
345 TEST_EQUAL(PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1);
346 TEST_EQUAL(PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1);
347 TEST_EQUAL(PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2);
348 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
349 TEST_EQUAL(PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY);
Gilles Peskineb87b7192019-12-04 16:24:10 +0100350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 TEST_EQUAL(PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919);
352 TEST_EQUAL(PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919);
353 TEST_EQUAL(PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919);
354 TEST_EQUAL(PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919);
355 TEST_EQUAL(PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919);
Paul Elliott75e27032020-06-03 15:17:39 +0100356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 TEST_EQUAL(PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919);
358 TEST_EQUAL(PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM);
Gilles Peskineb87b7192019-12-04 16:24:10 +0100359#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200360}
361/* END_CASE */
362
363/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364void import_with_policy(int type_arg,
365 int usage_arg, int alg_arg,
366 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +0200367{
368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
369 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200371 psa_key_type_t type = type_arg;
372 psa_key_usage_t usage = usage_arg;
373 psa_algorithm_t alg = alg_arg;
374 psa_status_t expected_status = expected_status_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +0200376 psa_status_t status;
377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +0200379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 psa_set_key_type(&attributes, type);
381 psa_set_key_usage_flags(&attributes, usage);
382 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 status = psa_import_key(&attributes,
385 key_material, sizeof(key_material),
386 &key);
387 TEST_EQUAL(status, expected_status);
388 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +0200389 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 }
Gilles Peskine6edfa292019-07-31 15:53:45 +0200391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
393 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
394 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
395 mbedtls_test_update_key_usage_flags(usage));
396 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
397 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 PSA_ASSERT(psa_destroy_key(key));
400 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200401
402exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100403 /*
404 * Key attributes may have been returned by psa_get_key_attributes()
405 * thus reset them as required.
406 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 psa_destroy_key(key);
410 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +0200411}
412/* END_CASE */
413
414/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415void import_with_data(data_t *data, int type_arg,
416 int attr_bits_arg,
417 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200418{
419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
420 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200422 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200423 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200424 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100425 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 psa_set_key_type(&attributes, type);
430 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 status = psa_import_key(&attributes, data->x, data->len, &key);
433 TEST_EQUAL(status, expected_status);
434 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200435 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
439 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
440 if (attr_bits != 0) {
441 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
442 }
443 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 PSA_ASSERT(psa_destroy_key(key));
446 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100447
448exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100449 /*
450 * Key attributes may have been returned by psa_get_key_attributes()
451 * thus reset them as required.
452 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 psa_destroy_key(key);
456 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100457}
458/* END_CASE */
459
460/* BEGIN_CASE */
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100461/* Construct and attempt to import a large unstructured key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462void import_large_key(int type_arg, int byte_size_arg,
463 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +0200464{
465 psa_key_type_t type = type_arg;
466 size_t byte_size = byte_size_arg;
467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
468 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200469 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200470 psa_status_t status;
471 uint8_t *buffer = NULL;
472 size_t buffer_size = byte_size + 1;
473 size_t n;
474
Steven Cooreman69967ce2021-01-18 18:01:08 +0100475 /* Skip the test case if the target running the test cannot
Shaun Case0e7791f2021-12-20 21:14:10 -0800476 * accommodate large keys due to heap size constraints */
Tom Cosgrove20e27de2023-09-04 11:09:08 +0100477 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +0200479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +0200481
482 /* Try importing the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
484 psa_set_key_type(&attributes, type);
485 status = psa_import_key(&attributes, buffer, byte_size, &key);
486 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
487 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +0200488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 if (status == PSA_SUCCESS) {
490 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
491 TEST_EQUAL(psa_get_key_type(&attributes), type);
492 TEST_EQUAL(psa_get_key_bits(&attributes),
493 PSA_BYTES_TO_BITS(byte_size));
494 ASSERT_NO_SLOT_NUMBER(&attributes);
495 memset(buffer, 0, byte_size + 1);
496 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
497 for (n = 0; n < byte_size; n++) {
498 TEST_EQUAL(buffer[n], 'K');
499 }
500 for (n = byte_size; n < buffer_size; n++) {
501 TEST_EQUAL(buffer[n], 0);
502 }
Gilles Peskinec744d992019-07-30 17:26:54 +0200503 }
504
505exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100506 /*
507 * Key attributes may have been returned by psa_get_key_attributes()
508 * thus reset them as required.
509 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 psa_destroy_key(key);
513 PSA_DONE();
514 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +0200515}
516/* END_CASE */
517
Andrzej Kureke0015962022-01-17 15:29:38 +0100518/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100519/* Import an RSA key with a valid structure (but not valid numbers
520 * inside, beyond having sensible size and parity). This is expected to
521 * fail for large keys. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200523{
Ronald Cron5425a212020-08-04 14:58:35 +0200524 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200525 size_t bits = bits_arg;
526 psa_status_t expected_status = expected_status_arg;
527 psa_status_t status;
528 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200529 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200530 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100531 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200532 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200533 unsigned char *p;
534 int ret;
535 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200536 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100539 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
542 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200543 length = ret;
544
545 /* Try importing the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 psa_set_key_type(&attributes, type);
547 status = psa_import_key(&attributes, p, length, &key);
548 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +0200549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 if (status == PSA_SUCCESS) {
551 PSA_ASSERT(psa_destroy_key(key));
552 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200553
554exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 mbedtls_free(buffer);
556 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200557}
558/* END_CASE */
559
560/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561void import_export(data_t *data,
562 int type_arg,
563 int usage_arg, int alg_arg,
564 int expected_bits,
565 int export_size_delta,
566 int expected_export_status_arg,
567 /*whether reexport must give the original input exactly*/
568 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569{
Ronald Cron5425a212020-08-04 14:58:35 +0200570 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200572 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200573 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100574 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575 unsigned char *exported = NULL;
576 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100577 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100578 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200580 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200581 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100582
Moran Pekercb088e72018-07-17 17:36:59 +0300583 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100584 TEST_CALLOC(exported, export_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 if (!canonical_input) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100586 TEST_CALLOC(reexported, export_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587 }
588 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590 psa_set_key_usage_flags(&attributes, usage_arg);
591 psa_set_key_algorithm(&attributes, alg);
592 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -0700593
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596
597 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100598 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
599 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
600 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
601 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602
603 /* Export the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100604 status = psa_export_key(key, exported, export_size, &exported_length);
605 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100606
607 /* The exported length must be set by psa_export_key() to a value between 0
608 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100609 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
610 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
611 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100613 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
614 export_size - exported_length));
615 if (status != PSA_SUCCESS) {
616 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100617 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200618 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619
Gilles Peskineea38a922021-02-13 00:05:16 +0100620 /* Run sanity checks on the exported key. For non-canonical inputs,
621 * this validates the canonical representations. For canonical inputs,
622 * this doesn't directly validate the implementation, but it still helps
623 * by cross-validating the test data with the sanity check code. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Gilles Peskine8f609232018-08-11 01:24:55 +0200625 goto exit;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100626 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100627
628 if (canonical_input) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100629 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630 } else {
631 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
632 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
633 &key2));
634 PSA_ASSERT(psa_export_key(key2,
635 reexported,
636 export_size,
637 &reexported_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100638 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +0100639 reexported, reexported_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 PSA_ASSERT(psa_destroy_key(key2));
641 }
642 TEST_ASSERT(exported_length <=
643 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
644 psa_get_key_bits(&got_attributes)));
645 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646
647destroy:
648 /* Destroy the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 PSA_ASSERT(psa_destroy_key(key));
650 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100651
652exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100653 /*
654 * Key attributes may have been returned by psa_get_key_attributes()
655 * thus reset them as required.
656 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 mbedtls_free(exported);
660 mbedtls_free(reexported);
661 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662}
663/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100664
Moran Pekerf709f4a2018-06-06 17:26:04 +0300665/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100666void import_export_public_key(data_t *data,
667 int type_arg, // key pair or public key
668 int alg_arg,
669 int export_size_delta,
670 int expected_export_status_arg,
671 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +0300672{
Ronald Cron5425a212020-08-04 14:58:35 +0200673 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300674 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200675 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200676 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300677 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300678 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100679 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100680 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +0300684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100685 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
686 psa_set_key_algorithm(&attributes, alg);
687 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +0300688
689 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +0300691
Gilles Peskine49c25912018-10-29 15:15:31 +0100692 /* Export the public key */
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100693 TEST_CALLOC(exported, export_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694 status = psa_export_public_key(key,
695 exported, export_size,
696 &exported_length);
697 TEST_EQUAL(status, expected_export_status);
698 if (status == PSA_SUCCESS) {
699 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100700 size_t bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100701 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
702 bits = psa_get_key_bits(&attributes);
703 TEST_LE_U(expected_public_key->len,
704 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
705 TEST_LE_U(expected_public_key->len,
706 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
707 TEST_LE_U(expected_public_key->len,
708 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100709 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +0100710 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100711 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300712
713exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100714 /*
715 * Key attributes may have been returned by psa_get_key_attributes()
716 * thus reset them as required.
717 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100718 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100720 mbedtls_free(exported);
721 psa_destroy_key(key);
722 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +0300723}
724/* END_CASE */
725
Gilles Peskine20035e32018-02-03 22:44:14 +0100726/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727void import_and_exercise_key(data_t *data,
728 int type_arg,
729 int bits_arg,
730 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200731{
Ronald Cron5425a212020-08-04 14:58:35 +0200732 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200733 psa_key_type_t type = type_arg;
734 size_t bits = bits_arg;
735 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100736 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +0200737 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200738 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100740 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200741
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100742 psa_set_key_usage_flags(&attributes, usage);
743 psa_set_key_algorithm(&attributes, alg);
744 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200745
746 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200748
749 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100750 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
751 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
752 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200753
754 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100755 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +0200756 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100757 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100759 PSA_ASSERT(psa_destroy_key(key));
760 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200761
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200762exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100763 /*
764 * Key attributes may have been returned by psa_get_key_attributes()
765 * thus reset them as required.
766 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100767 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100769 psa_reset_key_attributes(&attributes);
770 psa_destroy_key(key);
771 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200772}
773/* END_CASE */
774
775/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100776void effective_key_attributes(int type_arg, int expected_type_arg,
777 int bits_arg, int expected_bits_arg,
778 int usage_arg, int expected_usage_arg,
779 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +0200780{
Ronald Cron5425a212020-08-04 14:58:35 +0200781 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100782 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100783 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100784 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100785 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200786 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100787 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200788 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100789 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100792 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +0200793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 psa_set_key_usage_flags(&attributes, usage);
795 psa_set_key_algorithm(&attributes, alg);
796 psa_set_key_type(&attributes, key_type);
797 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +0200798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100799 PSA_ASSERT(psa_generate_key(&attributes, &key));
800 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +0200801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
803 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
804 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
805 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
806 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +0200807
808exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100809 /*
810 * Key attributes may have been returned by psa_get_key_attributes()
811 * thus reset them as required.
812 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100813 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815 psa_destroy_key(key);
816 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +0200817}
818/* END_CASE */
819
820/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821void check_key_policy(int type_arg, int bits_arg,
822 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +0100823{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100824 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
825 usage_arg,
826 mbedtls_test_update_key_usage_flags(usage_arg),
827 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +0100828 goto exit;
829}
830/* END_CASE */
831
832/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100833void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +0000834{
835 /* Test each valid way of initializing the object, except for `= {0}`, as
836 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
837 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -0800838 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200840 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
841 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +0000844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
846 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
847 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100849 TEST_EQUAL(psa_get_key_type(&func), 0);
850 TEST_EQUAL(psa_get_key_type(&init), 0);
851 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100853 TEST_EQUAL(psa_get_key_bits(&func), 0);
854 TEST_EQUAL(psa_get_key_bits(&init), 0);
855 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
858 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
859 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100861 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
862 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
863 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +0000864}
865/* END_CASE */
866
867/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868void mac_key_policy(int policy_usage_arg,
869 int policy_alg_arg,
870 int key_type_arg,
871 data_t *key_data,
872 int exercise_alg_arg,
873 int expected_status_sign_arg,
874 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +0200875{
Ronald Cron5425a212020-08-04 14:58:35 +0200876 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000878 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200879 psa_key_type_t key_type = key_type_arg;
880 psa_algorithm_t policy_alg = policy_alg_arg;
881 psa_algorithm_t exercise_alg = exercise_alg_arg;
882 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200883 psa_status_t status;
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200884 psa_status_t expected_status_sign = expected_status_sign_arg;
885 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200886 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +0200889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100890 psa_set_key_usage_flags(&attributes, policy_usage);
891 psa_set_key_algorithm(&attributes, policy_alg);
892 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +0200893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
895 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +0200896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100897 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
898 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900 status = psa_mac_sign_setup(&operation, key, exercise_alg);
901 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100902
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200903 /* Calculate the MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100904 uint8_t input[128] = { 0 };
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200905 size_t mac_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
907 input, 128,
908 mac, PSA_MAC_MAX_SIZE, &mac_len),
909 expected_status_sign);
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200910
911 /* Verify correct MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100912 status = psa_mac_verify(key, exercise_alg, input, 128,
913 mac, mac_len);
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100915 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
916 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
917 } else {
918 TEST_EQUAL(status, expected_status_verify);
919 }
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100921 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +0200922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100923 memset(mac, 0, sizeof(mac));
924 status = psa_mac_verify_setup(&operation, key, exercise_alg);
925 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200926
927exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 psa_mac_abort(&operation);
929 psa_destroy_key(key);
930 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200931}
932/* END_CASE */
933
934/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935void cipher_key_policy(int policy_usage_arg,
936 int policy_alg,
937 int key_type,
938 data_t *key_data,
939 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940{
Ronald Cron5425a212020-08-04 14:58:35 +0200941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000943 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200944 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945 psa_status_t status;
946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949 psa_set_key_usage_flags(&attributes, policy_usage);
950 psa_set_key_algorithm(&attributes, policy_alg);
951 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100953 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
954 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200956 /* Check if no key usage flag implication is done */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957 TEST_EQUAL(policy_usage,
958 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100960 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
961 if (policy_alg == exercise_alg &&
962 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
963 PSA_ASSERT(status);
964 } else {
965 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
966 }
967 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100969 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
970 if (policy_alg == exercise_alg &&
971 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
972 PSA_ASSERT(status);
973 } else {
974 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
975 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976
977exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100978 psa_cipher_abort(&operation);
979 psa_destroy_key(key);
980 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200981}
982/* END_CASE */
983
984/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100985void aead_key_policy(int policy_usage_arg,
986 int policy_alg,
987 int key_type,
988 data_t *key_data,
989 int nonce_length_arg,
990 int tag_length_arg,
991 int exercise_alg,
992 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200993{
Ronald Cron5425a212020-08-04 14:58:35 +0200994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200996 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100998 psa_status_t expected_status = expected_status_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001000 size_t nonce_length = nonce_length_arg;
1001 unsigned char tag[16];
1002 size_t tag_length = tag_length_arg;
1003 size_t output_length;
1004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001005 TEST_LE_U(nonce_length, sizeof(nonce));
1006 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001008 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 psa_set_key_usage_flags(&attributes, policy_usage);
1011 psa_set_key_algorithm(&attributes, policy_alg);
1012 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001014 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1015 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001017 /* Check if no key usage implication is done */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001018 TEST_EQUAL(policy_usage,
1019 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001021 status = psa_aead_encrypt(key, exercise_alg,
1022 nonce, nonce_length,
1023 NULL, 0,
1024 NULL, 0,
1025 tag, tag_length,
1026 &output_length);
1027 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1028 TEST_EQUAL(status, expected_status);
1029 } else {
1030 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1031 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001033 memset(tag, 0, sizeof(tag));
1034 status = psa_aead_decrypt(key, exercise_alg,
1035 nonce, nonce_length,
1036 NULL, 0,
1037 tag, tag_length,
1038 NULL, 0,
1039 &output_length);
1040 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
1041 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1042 } else if (expected_status == PSA_SUCCESS) {
1043 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1044 } else {
1045 TEST_EQUAL(status, expected_status);
1046 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001047
1048exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001049 psa_destroy_key(key);
1050 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001051}
1052/* END_CASE */
1053
1054/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001055void asymmetric_encryption_key_policy(int policy_usage_arg,
1056 int policy_alg,
1057 int key_type,
1058 data_t *key_data,
1059 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001060{
Ronald Cron5425a212020-08-04 14:58:35 +02001061 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001063 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064 psa_status_t status;
1065 size_t key_bits;
1066 size_t buffer_length;
1067 unsigned char *buffer = NULL;
1068 size_t output_length;
1069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001070 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001072 psa_set_key_usage_flags(&attributes, policy_usage);
1073 psa_set_key_algorithm(&attributes, policy_alg);
1074 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001076 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1077 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001078
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001079 /* Check if no key usage implication is done */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 TEST_EQUAL(policy_usage,
1081 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001083 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1084 key_bits = psa_get_key_bits(&attributes);
1085 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
1086 exercise_alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01001087 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001089 status = psa_asymmetric_encrypt(key, exercise_alg,
1090 NULL, 0,
1091 NULL, 0,
1092 buffer, buffer_length,
1093 &output_length);
1094 if (policy_alg == exercise_alg &&
1095 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1096 PSA_ASSERT(status);
1097 } else {
1098 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1099 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101 if (buffer_length != 0) {
1102 memset(buffer, 0, buffer_length);
1103 }
1104 status = psa_asymmetric_decrypt(key, exercise_alg,
1105 buffer, buffer_length,
1106 NULL, 0,
1107 buffer, buffer_length,
1108 &output_length);
1109 if (policy_alg == exercise_alg &&
1110 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1111 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
1112 } else {
1113 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1114 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001115
1116exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001117 /*
1118 * Key attributes may have been returned by psa_get_key_attributes()
1119 * thus reset them as required.
1120 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001121 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 psa_destroy_key(key);
1124 PSA_DONE();
1125 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126}
1127/* END_CASE */
1128
1129/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001130void asymmetric_signature_key_policy(int policy_usage_arg,
1131 int policy_alg,
1132 int key_type,
1133 data_t *key_data,
1134 int exercise_alg,
1135 int payload_length_arg,
1136 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001137{
Ronald Cron5425a212020-08-04 14:58:35 +02001138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001140 psa_key_usage_t policy_usage = policy_usage_arg;
1141 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001142 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001144 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1145 * compatible with the policy and `payload_length_arg` is supposed to be
1146 * a valid input length to sign. If `payload_length_arg <= 0`,
1147 * `exercise_alg` is supposed to be forbidden by the policy. */
1148 int compatible_alg = payload_length_arg > 0;
1149 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001150 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001151 size_t signature_length;
1152
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001153 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001154 in the expected usage flags. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155 TEST_EQUAL(expected_usage,
1156 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001158 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001160 psa_set_key_usage_flags(&attributes, policy_usage);
1161 psa_set_key_algorithm(&attributes, policy_alg);
1162 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1165 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001167 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001169 status = psa_sign_hash(key, exercise_alg,
1170 payload, payload_length,
1171 signature, sizeof(signature),
1172 &signature_length);
1173 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
1174 PSA_ASSERT(status);
1175 } else {
1176 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1177 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001179 memset(signature, 0, sizeof(signature));
1180 status = psa_verify_hash(key, exercise_alg,
1181 payload, payload_length,
1182 signature, sizeof(signature));
1183 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
1184 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1185 } else {
1186 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1187 }
Gilles Peskined5b33222018-06-18 22:20:03 +02001188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
1190 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
1191 status = psa_sign_message(key, exercise_alg,
1192 payload, payload_length,
1193 signature, sizeof(signature),
1194 &signature_length);
1195 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
1196 PSA_ASSERT(status);
1197 } else {
1198 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1199 }
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 memset(signature, 0, sizeof(signature));
1202 status = psa_verify_message(key, exercise_alg,
1203 payload, payload_length,
1204 signature, sizeof(signature));
1205 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
1206 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1207 } else {
1208 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1209 }
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001210 }
1211
Gilles Peskined5b33222018-06-18 22:20:03 +02001212exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001213 psa_destroy_key(key);
1214 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001215}
1216/* END_CASE */
1217
Janos Follathba3fab92019-06-11 14:50:16 +01001218/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219void derive_key_policy(int policy_usage,
1220 int policy_alg,
1221 int key_type,
1222 data_t *key_data,
1223 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02001224{
Ronald Cron5425a212020-08-04 14:58:35 +02001225 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001227 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001228 psa_status_t status;
1229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001230 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02001231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001232 psa_set_key_usage_flags(&attributes, policy_usage);
1233 psa_set_key_algorithm(&attributes, policy_alg);
1234 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02001235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001236 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1237 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02001238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001239 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01001240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001241 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
1242 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
1243 PSA_ASSERT(psa_key_derivation_input_bytes(
1244 &operation,
1245 PSA_KEY_DERIVATION_INPUT_SEED,
1246 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01001247 }
Janos Follathba3fab92019-06-11 14:50:16 +01001248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001249 status = psa_key_derivation_input_key(&operation,
1250 PSA_KEY_DERIVATION_INPUT_SECRET,
1251 key);
Janos Follathba3fab92019-06-11 14:50:16 +01001252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001253 if (policy_alg == exercise_alg &&
1254 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
1255 PSA_ASSERT(status);
1256 } else {
1257 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1258 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02001259
1260exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001261 psa_key_derivation_abort(&operation);
1262 psa_destroy_key(key);
1263 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02001264}
1265/* END_CASE */
1266
1267/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001268void agreement_key_policy(int policy_usage,
1269 int policy_alg,
1270 int key_type_arg,
1271 data_t *key_data,
1272 int exercise_alg,
1273 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02001274{
Ronald Cron5425a212020-08-04 14:58:35 +02001275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001277 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001278 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001279 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001280 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001282 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02001283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 psa_set_key_usage_flags(&attributes, policy_usage);
1285 psa_set_key_algorithm(&attributes, policy_alg);
1286 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02001287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1289 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02001290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001291 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
1292 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02001293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02001295
1296exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001297 psa_key_derivation_abort(&operation);
1298 psa_destroy_key(key);
1299 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02001300}
1301/* END_CASE */
1302
1303/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001304void key_policy_alg2(int key_type_arg, data_t *key_data,
1305 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001306{
Ronald Cron5425a212020-08-04 14:58:35 +02001307 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001308 psa_key_type_t key_type = key_type_arg;
1309 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1310 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1311 psa_key_usage_t usage = usage_arg;
1312 psa_algorithm_t alg = alg_arg;
1313 psa_algorithm_t alg2 = alg2_arg;
1314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 psa_set_key_usage_flags(&attributes, usage);
1318 psa_set_key_algorithm(&attributes, alg);
1319 psa_set_key_enrollment_algorithm(&attributes, alg2);
1320 psa_set_key_type(&attributes, key_type);
1321 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1322 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001323
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001324 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001325 usage = mbedtls_test_update_key_usage_flags(usage);
1326 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1327 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
1328 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1329 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001331 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001332 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 }
1334 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001335 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001336 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001337
1338exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001339 /*
1340 * Key attributes may have been returned by psa_get_key_attributes()
1341 * thus reset them as required.
1342 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001343 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001345 psa_destroy_key(key);
1346 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001347}
1348/* END_CASE */
1349
1350/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001351void raw_agreement_key_policy(int policy_usage,
1352 int policy_alg,
1353 int key_type_arg,
1354 data_t *key_data,
1355 int exercise_alg,
1356 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001357{
Ronald Cron5425a212020-08-04 14:58:35 +02001358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001360 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001361 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001362 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001363 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001365 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001367 psa_set_key_usage_flags(&attributes, policy_usage);
1368 psa_set_key_algorithm(&attributes, policy_alg);
1369 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001371 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1372 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001374 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001376 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001377
1378exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 psa_key_derivation_abort(&operation);
1380 psa_destroy_key(key);
1381 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001382}
1383/* END_CASE */
1384
1385/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001386void copy_success(int source_usage_arg,
1387 int source_alg_arg, int source_alg2_arg,
1388 int type_arg, data_t *material,
1389 int copy_attributes,
1390 int target_usage_arg,
1391 int target_alg_arg, int target_alg2_arg,
1392 int expected_usage_arg,
1393 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01001394{
Gilles Peskineca25db92019-04-19 11:43:08 +02001395 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1396 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001397 psa_key_usage_t expected_usage = expected_usage_arg;
1398 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001399 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001400 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1401 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001402 uint8_t *export_buffer = NULL;
1403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001404 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01001405
Gilles Peskineca25db92019-04-19 11:43:08 +02001406 /* Prepare the source key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
1408 psa_set_key_algorithm(&source_attributes, source_alg_arg);
1409 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
1410 psa_set_key_type(&source_attributes, type_arg);
1411 PSA_ASSERT(psa_import_key(&source_attributes,
1412 material->x, material->len,
1413 &source_key));
1414 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001415
Gilles Peskineca25db92019-04-19 11:43:08 +02001416 /* Prepare the target attributes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001417 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02001418 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001419 /* Set volatile lifetime to reset the key identifier to 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001420 psa_set_key_lifetime(&target_attributes, PSA_KEY_LIFETIME_VOLATILE);
Ronald Cron65f38a32020-10-23 17:11:13 +02001421 }
1422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001423 if (target_usage_arg != -1) {
1424 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
1425 }
1426 if (target_alg_arg != -1) {
1427 psa_set_key_algorithm(&target_attributes, target_alg_arg);
1428 }
1429 if (target_alg2_arg != -1) {
1430 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
1431 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001432
1433 /* Copy the key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001434 PSA_ASSERT(psa_copy_key(source_key,
1435 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001436
1437 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001438 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001439
1440 /* Test that the target slot has the expected content and policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001441 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
1442 TEST_EQUAL(psa_get_key_type(&source_attributes),
1443 psa_get_key_type(&target_attributes));
1444 TEST_EQUAL(psa_get_key_bits(&source_attributes),
1445 psa_get_key_bits(&target_attributes));
1446 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
1447 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
1448 TEST_EQUAL(expected_alg2,
1449 psa_get_key_enrollment_algorithm(&target_attributes));
1450 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01001451 size_t length;
Tom Cosgrove30ceb232023-09-04 11:20:19 +01001452 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001453 PSA_ASSERT(psa_export_key(target_key, export_buffer,
1454 material->len, &length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01001455 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01001456 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01001457 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01001460 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001461 }
1462 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001463 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001466 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001467
1468exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001469 /*
1470 * Source and target key attributes may have been returned by
1471 * psa_get_key_attributes() thus reset them as required.
1472 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001473 psa_reset_key_attributes(&source_attributes);
1474 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001476 PSA_DONE();
1477 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01001478}
1479/* END_CASE */
1480
1481/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001482void copy_fail(int source_usage_arg,
1483 int source_alg_arg, int source_alg2_arg,
1484 int type_arg, data_t *material,
1485 int target_type_arg, int target_bits_arg,
1486 int target_usage_arg,
1487 int target_alg_arg, int target_alg2_arg,
1488 int target_id_arg, int target_lifetime_arg,
1489 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02001490{
1491 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1492 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001493 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1494 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001495 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02001496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02001498
1499 /* Prepare the source key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001500 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
1501 psa_set_key_algorithm(&source_attributes, source_alg_arg);
1502 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
1503 psa_set_key_type(&source_attributes, type_arg);
1504 PSA_ASSERT(psa_import_key(&source_attributes,
1505 material->x, material->len,
1506 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02001507
1508 /* Prepare the target attributes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001509 psa_set_key_id(&target_attributes, key_id);
1510 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
1511 psa_set_key_type(&target_attributes, target_type_arg);
1512 psa_set_key_bits(&target_attributes, target_bits_arg);
1513 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
1514 psa_set_key_algorithm(&target_attributes, target_alg_arg);
1515 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02001516
1517 /* Try to copy the key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001518 TEST_EQUAL(psa_copy_key(source_key,
1519 &target_attributes, &target_key),
1520 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02001523
Gilles Peskine4a644642019-05-03 17:14:08 +02001524exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001525 psa_reset_key_attributes(&source_attributes);
1526 psa_reset_key_attributes(&target_attributes);
1527 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02001528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001532void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00001533{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001534 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001535 /* Test each valid way of initializing the object, except for `= {0}`, as
1536 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1537 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08001538 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00001540 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1541 psa_hash_operation_t zero;
1542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00001544
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001545 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001546 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
1547 PSA_ERROR_BAD_STATE);
1548 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
1549 PSA_ERROR_BAD_STATE);
1550 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
1551 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001552
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001553 /* A default hash operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001554 PSA_ASSERT(psa_hash_abort(&func));
1555 PSA_ASSERT(psa_hash_abort(&init));
1556 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00001557}
1558/* END_CASE */
1559
1560/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001561void hash_setup(int alg_arg,
1562 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001563{
1564 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001565 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001566 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001567 psa_status_t status;
1568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001569 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001571 status = psa_hash_setup(&operation, alg);
1572 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001573
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001574 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001575 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001576
1577 /* If setup failed, reproduce the failure, so as to
1578 * test the resulting state of the operation object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001579 if (status != PSA_SUCCESS) {
1580 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
1581 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001582
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001583 /* Now the operation object should be reusable. */
1584#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001585 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
1586 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001587#endif
1588
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001589exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001590 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001591}
1592/* END_CASE */
1593
1594/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001595void hash_compute_fail(int alg_arg, data_t *input,
1596 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01001597{
1598 psa_algorithm_t alg = alg_arg;
1599 uint8_t *output = NULL;
1600 size_t output_size = output_size_arg;
1601 size_t output_length = INVALID_EXPORT_LENGTH;
1602 psa_status_t expected_status = expected_status_arg;
1603 psa_status_t status;
1604
Tom Cosgrove30ceb232023-09-04 11:20:19 +01001605 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001607 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01001608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 status = psa_hash_compute(alg, input->x, input->len,
1610 output, output_size, &output_length);
1611 TEST_EQUAL(status, expected_status);
1612 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001613
1614exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001615 mbedtls_free(output);
1616 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01001617}
1618/* END_CASE */
1619
1620/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621void hash_compare_fail(int alg_arg, data_t *input,
1622 data_t *reference_hash,
1623 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01001624{
1625 psa_algorithm_t alg = alg_arg;
1626 psa_status_t expected_status = expected_status_arg;
1627 psa_status_t status;
1628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001629 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01001630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001631 status = psa_hash_compare(alg, input->x, input->len,
1632 reference_hash->x, reference_hash->len);
1633 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01001634
1635exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001636 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01001637}
1638/* END_CASE */
1639
1640/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001641void hash_compute_compare(int alg_arg, data_t *input,
1642 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01001643{
1644 psa_algorithm_t alg = alg_arg;
1645 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1646 size_t output_length = INVALID_EXPORT_LENGTH;
1647 size_t i;
1648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001649 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01001650
1651 /* Compute with tight buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001652 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
1653 output, PSA_HASH_LENGTH(alg),
1654 &output_length));
1655 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01001656 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01001657 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001658
1659 /* Compute with larger buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001660 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
1661 output, sizeof(output),
1662 &output_length));
1663 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01001664 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01001665 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001666
1667 /* Compare with correct hash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001668 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
1669 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01001670
1671 /* Compare with trailing garbage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001672 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
1673 output, output_length + 1),
1674 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001675
1676 /* Compare with truncated hash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001677 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
1678 output, output_length - 1),
1679 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001680
1681 /* Compare with corrupted value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001682 for (i = 0; i < output_length; i++) {
1683 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001684 output[i] ^= 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001685 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
1686 output, output_length),
1687 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001688 output[i] ^= 1;
1689 }
1690
1691exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001692 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01001693}
1694/* END_CASE */
1695
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001696/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001697void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02001698{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001699 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001700 unsigned char input[] = "";
1701 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001702 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001703 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1704 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001705 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
1706 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001707 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001708 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001709 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001711 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02001712
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001713 /* Call setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 PSA_ASSERT(psa_hash_setup(&operation, alg));
1715 ASSERT_OPERATION_IS_ACTIVE(operation);
1716 TEST_EQUAL(psa_hash_setup(&operation, alg),
1717 PSA_ERROR_BAD_STATE);
1718 ASSERT_OPERATION_IS_INACTIVE(operation);
1719 PSA_ASSERT(psa_hash_abort(&operation));
1720 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001721
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001722 /* Call update without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001723 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
1724 PSA_ERROR_BAD_STATE);
1725 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02001726
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001727 /* Check that update calls abort on error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman54f73512021-06-24 18:14:52 +01001729 operation.id = UINT_MAX;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001730 ASSERT_OPERATION_IS_ACTIVE(operation);
1731 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
1732 PSA_ERROR_BAD_STATE);
1733 ASSERT_OPERATION_IS_INACTIVE(operation);
1734 PSA_ASSERT(psa_hash_abort(&operation));
1735 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001736
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001737 /* Call update after finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001738 PSA_ASSERT(psa_hash_setup(&operation, alg));
1739 PSA_ASSERT(psa_hash_finish(&operation,
1740 hash, sizeof(hash), &hash_len));
1741 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
1742 PSA_ERROR_BAD_STATE);
1743 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02001744
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001745 /* Call verify without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001746 TEST_EQUAL(psa_hash_verify(&operation,
1747 valid_hash, sizeof(valid_hash)),
1748 PSA_ERROR_BAD_STATE);
1749 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001750
1751 /* Call verify after finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001752 PSA_ASSERT(psa_hash_setup(&operation, alg));
1753 PSA_ASSERT(psa_hash_finish(&operation,
1754 hash, sizeof(hash), &hash_len));
1755 TEST_EQUAL(psa_hash_verify(&operation,
1756 valid_hash, sizeof(valid_hash)),
1757 PSA_ERROR_BAD_STATE);
1758 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001759
1760 /* Call verify twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001761 PSA_ASSERT(psa_hash_setup(&operation, alg));
1762 ASSERT_OPERATION_IS_ACTIVE(operation);
1763 PSA_ASSERT(psa_hash_verify(&operation,
1764 valid_hash, sizeof(valid_hash)));
1765 ASSERT_OPERATION_IS_INACTIVE(operation);
1766 TEST_EQUAL(psa_hash_verify(&operation,
1767 valid_hash, sizeof(valid_hash)),
1768 PSA_ERROR_BAD_STATE);
1769 ASSERT_OPERATION_IS_INACTIVE(operation);
1770 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001771
1772 /* Call finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001773 TEST_EQUAL(psa_hash_finish(&operation,
1774 hash, sizeof(hash), &hash_len),
1775 PSA_ERROR_BAD_STATE);
1776 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001777
1778 /* Call finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001779 PSA_ASSERT(psa_hash_setup(&operation, alg));
1780 PSA_ASSERT(psa_hash_finish(&operation,
1781 hash, sizeof(hash), &hash_len));
1782 TEST_EQUAL(psa_hash_finish(&operation,
1783 hash, sizeof(hash), &hash_len),
1784 PSA_ERROR_BAD_STATE);
1785 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001786
1787 /* Call finish after calling verify. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001788 PSA_ASSERT(psa_hash_setup(&operation, alg));
1789 PSA_ASSERT(psa_hash_verify(&operation,
1790 valid_hash, sizeof(valid_hash)));
1791 TEST_EQUAL(psa_hash_finish(&operation,
1792 hash, sizeof(hash), &hash_len),
1793 PSA_ERROR_BAD_STATE);
1794 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02001795
1796exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001797 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02001798}
1799/* END_CASE */
1800
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001801/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001802void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03001803{
1804 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001805 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1806 * appended to it */
1807 unsigned char hash[] = {
1808 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1809 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001810 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
1811 };
1812 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00001813 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001815 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03001816
itayzafrir27e69452018-11-01 14:26:34 +02001817 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001818 PSA_ASSERT(psa_hash_setup(&operation, alg));
1819 ASSERT_OPERATION_IS_ACTIVE(operation);
1820 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
1821 PSA_ERROR_INVALID_SIGNATURE);
1822 ASSERT_OPERATION_IS_INACTIVE(operation);
1823 PSA_ASSERT(psa_hash_abort(&operation));
1824 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03001825
itayzafrir27e69452018-11-01 14:26:34 +02001826 /* psa_hash_verify with a non-matching hash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001827 PSA_ASSERT(psa_hash_setup(&operation, alg));
1828 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
1829 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03001830
itayzafrir27e69452018-11-01 14:26:34 +02001831 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001832 PSA_ASSERT(psa_hash_setup(&operation, alg));
1833 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
1834 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03001835
itayzafrirec93d302018-10-18 18:01:10 +03001836exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001837 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03001838}
1839/* END_CASE */
1840
Ronald Cronee414c72021-03-18 18:50:08 +01001841/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001842void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03001843{
1844 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001845 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00001847 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001848 size_t hash_len;
1849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001850 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03001851
itayzafrir58028322018-10-25 10:22:01 +03001852 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001853 PSA_ASSERT(psa_hash_setup(&operation, alg));
1854 TEST_EQUAL(psa_hash_finish(&operation,
1855 hash, expected_size - 1, &hash_len),
1856 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03001857
1858exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001859 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03001860}
1861/* END_CASE */
1862
Ronald Cronee414c72021-03-18 18:50:08 +01001863/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001864void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001865{
1866 psa_algorithm_t alg = PSA_ALG_SHA_256;
1867 unsigned char hash[PSA_HASH_MAX_SIZE];
1868 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1869 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1870 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1871 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1872 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1873 size_t hash_len;
1874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001875 PSA_ASSERT(psa_crypto_init());
1876 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001878 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
1879 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
1880 PSA_ASSERT(psa_hash_finish(&op_finished,
1881 hash, sizeof(hash), &hash_len));
1882 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
1883 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001885 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
1886 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001888 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
1889 PSA_ASSERT(psa_hash_finish(&op_init,
1890 hash, sizeof(hash), &hash_len));
1891 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
1892 PSA_ASSERT(psa_hash_finish(&op_finished,
1893 hash, sizeof(hash), &hash_len));
1894 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
1895 PSA_ASSERT(psa_hash_finish(&op_aborted,
1896 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001897
1898exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001899 psa_hash_abort(&op_source);
1900 psa_hash_abort(&op_init);
1901 psa_hash_abort(&op_setup);
1902 psa_hash_abort(&op_finished);
1903 psa_hash_abort(&op_aborted);
1904 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001905}
1906/* END_CASE */
1907
Ronald Cronee414c72021-03-18 18:50:08 +01001908/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001909void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001910{
1911 psa_algorithm_t alg = PSA_ALG_SHA_256;
1912 unsigned char hash[PSA_HASH_MAX_SIZE];
1913 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1914 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1915 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1916 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1917 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1918 size_t hash_len;
1919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001920 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001922 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
1923 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
1924 PSA_ASSERT(psa_hash_finish(&op_finished,
1925 hash, sizeof(hash), &hash_len));
1926 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
1927 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001929 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
1930 PSA_ASSERT(psa_hash_finish(&op_target,
1931 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001933 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
1934 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
1935 PSA_ERROR_BAD_STATE);
1936 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
1937 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001938
1939exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 psa_hash_abort(&op_target);
1941 psa_hash_abort(&op_init);
1942 psa_hash_abort(&op_setup);
1943 psa_hash_abort(&op_finished);
1944 psa_hash_abort(&op_aborted);
1945 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001946}
1947/* END_CASE */
1948
itayzafrir58028322018-10-25 10:22:01 +03001949/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001950void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00001951{
Jaeden Amero252ef282019-02-15 14:05:35 +00001952 const uint8_t input[1] = { 0 };
1953
Jaeden Amero769ce272019-01-04 11:48:03 +00001954 /* Test each valid way of initializing the object, except for `= {0}`, as
1955 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1956 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08001957 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001958 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00001959 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1960 psa_mac_operation_t zero;
1961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001962 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00001963
Jaeden Amero252ef282019-02-15 14:05:35 +00001964 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001965 TEST_EQUAL(psa_mac_update(&func,
1966 input, sizeof(input)),
1967 PSA_ERROR_BAD_STATE);
1968 TEST_EQUAL(psa_mac_update(&init,
1969 input, sizeof(input)),
1970 PSA_ERROR_BAD_STATE);
1971 TEST_EQUAL(psa_mac_update(&zero,
1972 input, sizeof(input)),
1973 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00001974
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001975 /* A default MAC operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001976 PSA_ASSERT(psa_mac_abort(&func));
1977 PSA_ASSERT(psa_mac_abort(&init));
1978 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00001979}
1980/* END_CASE */
1981
1982/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001983void mac_setup(int key_type_arg,
1984 data_t *key,
1985 int alg_arg,
1986 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001987{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001988 psa_key_type_t key_type = key_type_arg;
1989 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001990 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001991 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001992 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1993#if defined(KNOWN_SUPPORTED_MAC_ALG)
1994 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1995#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001999 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
2000 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002001 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002002 }
2003 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002004
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002005 /* The operation object should be reusable. */
2006#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002007 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
2008 smoke_test_key_data,
2009 sizeof(smoke_test_key_data),
2010 KNOWN_SUPPORTED_MAC_ALG,
2011 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002012 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002013 }
2014 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002015#endif
2016
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002017exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002018 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002019}
2020/* END_CASE */
2021
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002022/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002023void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00002024{
Ronald Cron5425a212020-08-04 14:58:35 +02002025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002026 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2027 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002028 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002029 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2030 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002031 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
2032 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002034 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2035 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2036 size_t sign_mac_length = 0;
2037 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2038 const uint8_t verify_mac[] = {
2039 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2040 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002041 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
2042 };
Jaeden Amero252ef282019-02-15 14:05:35 +00002043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002044 PSA_ASSERT(psa_crypto_init());
2045 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
2046 psa_set_key_algorithm(&attributes, alg);
2047 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002049 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
2050 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002051
Jaeden Amero252ef282019-02-15 14:05:35 +00002052 /* Call update without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002053 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
2054 PSA_ERROR_BAD_STATE);
2055 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002056
2057 /* Call sign finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
2059 &sign_mac_length),
2060 PSA_ERROR_BAD_STATE);
2061 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002062
2063 /* Call verify finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002064 TEST_EQUAL(psa_mac_verify_finish(&operation,
2065 verify_mac, sizeof(verify_mac)),
2066 PSA_ERROR_BAD_STATE);
2067 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002068
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002069 /* Call setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002070 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2071 ASSERT_OPERATION_IS_ACTIVE(operation);
2072 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
2073 PSA_ERROR_BAD_STATE);
2074 ASSERT_OPERATION_IS_INACTIVE(operation);
2075 PSA_ASSERT(psa_mac_abort(&operation));
2076 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002077
Jaeden Amero252ef282019-02-15 14:05:35 +00002078 /* Call update after sign finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002079 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2080 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2081 PSA_ASSERT(psa_mac_sign_finish(&operation,
2082 sign_mac, sizeof(sign_mac),
2083 &sign_mac_length));
2084 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
2085 PSA_ERROR_BAD_STATE);
2086 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002087
2088 /* Call update after verify finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002089 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2090 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2091 PSA_ASSERT(psa_mac_verify_finish(&operation,
2092 verify_mac, sizeof(verify_mac)));
2093 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
2094 PSA_ERROR_BAD_STATE);
2095 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002096
2097 /* Call sign finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002098 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2099 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2100 PSA_ASSERT(psa_mac_sign_finish(&operation,
2101 sign_mac, sizeof(sign_mac),
2102 &sign_mac_length));
2103 TEST_EQUAL(psa_mac_sign_finish(&operation,
2104 sign_mac, sizeof(sign_mac),
2105 &sign_mac_length),
2106 PSA_ERROR_BAD_STATE);
2107 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002108
2109 /* Call verify finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002110 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2111 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2112 PSA_ASSERT(psa_mac_verify_finish(&operation,
2113 verify_mac, sizeof(verify_mac)));
2114 TEST_EQUAL(psa_mac_verify_finish(&operation,
2115 verify_mac, sizeof(verify_mac)),
2116 PSA_ERROR_BAD_STATE);
2117 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002118
2119 /* Setup sign but try verify. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002120 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2121 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2122 ASSERT_OPERATION_IS_ACTIVE(operation);
2123 TEST_EQUAL(psa_mac_verify_finish(&operation,
2124 verify_mac, sizeof(verify_mac)),
2125 PSA_ERROR_BAD_STATE);
2126 ASSERT_OPERATION_IS_INACTIVE(operation);
2127 PSA_ASSERT(psa_mac_abort(&operation));
2128 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00002129
2130 /* Setup verify but try sign. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002131 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2132 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2133 ASSERT_OPERATION_IS_ACTIVE(operation);
2134 TEST_EQUAL(psa_mac_sign_finish(&operation,
2135 sign_mac, sizeof(sign_mac),
2136 &sign_mac_length),
2137 PSA_ERROR_BAD_STATE);
2138 ASSERT_OPERATION_IS_INACTIVE(operation);
2139 PSA_ASSERT(psa_mac_abort(&operation));
2140 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002142 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002143
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002144exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002145 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002146}
2147/* END_CASE */
2148
2149/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150void mac_sign(int key_type_arg,
2151 data_t *key_data,
2152 int alg_arg,
2153 data_t *input,
2154 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002155{
Ronald Cron5425a212020-08-04 14:58:35 +02002156 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002157 psa_key_type_t key_type = key_type_arg;
2158 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002159 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002161 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002162 size_t mac_buffer_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002164 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002165 const size_t output_sizes_to_test[] = {
2166 0,
2167 1,
2168 expected_mac->len - 1,
2169 expected_mac->len,
2170 expected_mac->len + 1,
2171 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002174 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002175 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002177 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002179 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
2180 psa_set_key_algorithm(&attributes, alg);
2181 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002183 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2184 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002186 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02002187 const size_t output_size = output_sizes_to_test[i];
2188 psa_status_t expected_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002189 (output_size >= expected_mac->len ? PSA_SUCCESS :
2190 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002192 mbedtls_test_set_step(output_size);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002193 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002194
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002195 /* Calculate the MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002196 TEST_EQUAL(psa_mac_compute(key, alg,
2197 input->x, input->len,
2198 actual_mac, output_size, &mac_length),
2199 expected_status);
2200 if (expected_status == PSA_SUCCESS) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002201 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002202 actual_mac, mac_length);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002203 }
2204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002205 if (output_size > 0) {
2206 memset(actual_mac, 0, output_size);
2207 }
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002208
2209 /* Calculate the MAC, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002210 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2211 PSA_ASSERT(psa_mac_update(&operation,
2212 input->x, input->len));
2213 TEST_EQUAL(psa_mac_sign_finish(&operation,
2214 actual_mac, output_size,
2215 &mac_length),
2216 expected_status);
2217 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02002218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002219 if (expected_status == PSA_SUCCESS) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002220 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002221 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02002222 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002223 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02002224 actual_mac = NULL;
2225 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002226
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002227exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002228 psa_mac_abort(&operation);
2229 psa_destroy_key(key);
2230 PSA_DONE();
2231 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002232}
2233/* END_CASE */
2234
2235/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002236void mac_verify(int key_type_arg,
2237 data_t *key_data,
2238 int alg_arg,
2239 data_t *input,
2240 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002241{
Ronald Cron5425a212020-08-04 14:58:35 +02002242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002243 psa_key_type_t key_type = key_type_arg;
2244 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002245 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002247 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02002250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01002252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002253 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
2254 psa_set_key_algorithm(&attributes, alg);
2255 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07002256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002257 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2258 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002259
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002260 /* Verify correct MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002261 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
2262 expected_mac->x, expected_mac->len));
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002263
2264 /* Verify correct MAC, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2266 PSA_ASSERT(psa_mac_update(&operation,
2267 input->x, input->len));
2268 PSA_ASSERT(psa_mac_verify_finish(&operation,
2269 expected_mac->x,
2270 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01002271
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002272 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002273 TEST_EQUAL(psa_mac_verify(key, alg,
2274 input->x, input->len,
2275 expected_mac->x,
2276 expected_mac->len - 1),
2277 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002278
2279 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002280 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2281 PSA_ASSERT(psa_mac_update(&operation,
2282 input->x, input->len));
2283 TEST_EQUAL(psa_mac_verify_finish(&operation,
2284 expected_mac->x,
2285 expected_mac->len - 1),
2286 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002287
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002288 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002289 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002290 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
2291 TEST_EQUAL(psa_mac_verify(key, alg,
2292 input->x, input->len,
2293 perturbed_mac, expected_mac->len + 1),
2294 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002295
2296 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002297 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2298 PSA_ASSERT(psa_mac_update(&operation,
2299 input->x, input->len));
2300 TEST_EQUAL(psa_mac_verify_finish(&operation,
2301 perturbed_mac,
2302 expected_mac->len + 1),
2303 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002304
2305 /* Test changing one byte. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 for (size_t i = 0; i < expected_mac->len; i++) {
2307 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002308 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002310 TEST_EQUAL(psa_mac_verify(key, alg,
2311 input->x, input->len,
2312 perturbed_mac, expected_mac->len),
2313 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2316 PSA_ASSERT(psa_mac_update(&operation,
2317 input->x, input->len));
2318 TEST_EQUAL(psa_mac_verify_finish(&operation,
2319 perturbed_mac,
2320 expected_mac->len),
2321 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002322 perturbed_mac[i] ^= 1;
2323 }
2324
Gilles Peskine8c9def32018-02-08 10:02:12 +01002325exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002326 psa_mac_abort(&operation);
2327 psa_destroy_key(key);
2328 PSA_DONE();
2329 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002330}
2331/* END_CASE */
2332
2333/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002334void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00002335{
Jaeden Ameroab439972019-02-15 14:12:05 +00002336 const uint8_t input[1] = { 0 };
2337 unsigned char output[1] = { 0 };
2338 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002339 /* Test each valid way of initializing the object, except for `= {0}`, as
2340 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2341 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08002342 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002343 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00002344 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2345 psa_cipher_operation_t zero;
2346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002347 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00002348
Jaeden Ameroab439972019-02-15 14:12:05 +00002349 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002350 TEST_EQUAL(psa_cipher_update(&func,
2351 input, sizeof(input),
2352 output, sizeof(output),
2353 &output_length),
2354 PSA_ERROR_BAD_STATE);
2355 TEST_EQUAL(psa_cipher_update(&init,
2356 input, sizeof(input),
2357 output, sizeof(output),
2358 &output_length),
2359 PSA_ERROR_BAD_STATE);
2360 TEST_EQUAL(psa_cipher_update(&zero,
2361 input, sizeof(input),
2362 output, sizeof(output),
2363 &output_length),
2364 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00002365
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002366 /* A default cipher operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002367 PSA_ASSERT(psa_cipher_abort(&func));
2368 PSA_ASSERT(psa_cipher_abort(&init));
2369 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00002370}
2371/* END_CASE */
2372
2373/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002374void cipher_setup(int key_type_arg,
2375 data_t *key,
2376 int alg_arg,
2377 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002378{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002379 psa_key_type_t key_type = key_type_arg;
2380 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002381 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002382 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002383 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002384#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002385 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2386#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002388 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002390 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
2391 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002392 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 }
2394 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002395
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002396 /* The operation object should be reusable. */
2397#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2399 smoke_test_key_data,
2400 sizeof(smoke_test_key_data),
2401 KNOWN_SUPPORTED_CIPHER_ALG,
2402 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002403 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002404 }
2405 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002406#endif
2407
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002408exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002409 psa_cipher_abort(&operation);
2410 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002411}
2412/* END_CASE */
2413
Ronald Cronee414c72021-03-18 18:50:08 +01002414/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00002416{
Ronald Cron5425a212020-08-04 14:58:35 +02002417 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002418 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2419 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002422 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002423 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002425 0xaa, 0xaa, 0xaa, 0xaa
2426 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002427 const uint8_t text[] = {
2428 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002429 0xbb, 0xbb, 0xbb, 0xbb
2430 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002431 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002432 size_t length = 0;
2433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002434 PSA_ASSERT(psa_crypto_init());
2435 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
2436 psa_set_key_algorithm(&attributes, alg);
2437 psa_set_key_type(&attributes, key_type);
2438 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
2439 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00002440
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002441 /* Call encrypt setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002442 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2443 ASSERT_OPERATION_IS_ACTIVE(operation);
2444 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
2445 PSA_ERROR_BAD_STATE);
2446 ASSERT_OPERATION_IS_INACTIVE(operation);
2447 PSA_ASSERT(psa_cipher_abort(&operation));
2448 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002449
2450 /* Call decrypt setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002451 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
2452 ASSERT_OPERATION_IS_ACTIVE(operation);
2453 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
2454 PSA_ERROR_BAD_STATE);
2455 ASSERT_OPERATION_IS_INACTIVE(operation);
2456 PSA_ASSERT(psa_cipher_abort(&operation));
2457 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002458
Jaeden Ameroab439972019-02-15 14:12:05 +00002459 /* Generate an IV without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002460 TEST_EQUAL(psa_cipher_generate_iv(&operation,
2461 buffer, sizeof(buffer),
2462 &length),
2463 PSA_ERROR_BAD_STATE);
2464 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002465
2466 /* Generate an IV twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002467 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2468 PSA_ASSERT(psa_cipher_generate_iv(&operation,
2469 buffer, sizeof(buffer),
2470 &length));
2471 ASSERT_OPERATION_IS_ACTIVE(operation);
2472 TEST_EQUAL(psa_cipher_generate_iv(&operation,
2473 buffer, sizeof(buffer),
2474 &length),
2475 PSA_ERROR_BAD_STATE);
2476 ASSERT_OPERATION_IS_INACTIVE(operation);
2477 PSA_ASSERT(psa_cipher_abort(&operation));
2478 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002479
2480 /* Generate an IV after it's already set. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002481 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2482 PSA_ASSERT(psa_cipher_set_iv(&operation,
2483 iv, sizeof(iv)));
2484 TEST_EQUAL(psa_cipher_generate_iv(&operation,
2485 buffer, sizeof(buffer),
2486 &length),
2487 PSA_ERROR_BAD_STATE);
2488 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002489
2490 /* Set an IV without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002491 TEST_EQUAL(psa_cipher_set_iv(&operation,
2492 iv, sizeof(iv)),
2493 PSA_ERROR_BAD_STATE);
2494 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002495
2496 /* Set an IV after it's already set. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002497 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2498 PSA_ASSERT(psa_cipher_set_iv(&operation,
2499 iv, sizeof(iv)));
2500 ASSERT_OPERATION_IS_ACTIVE(operation);
2501 TEST_EQUAL(psa_cipher_set_iv(&operation,
2502 iv, sizeof(iv)),
2503 PSA_ERROR_BAD_STATE);
2504 ASSERT_OPERATION_IS_INACTIVE(operation);
2505 PSA_ASSERT(psa_cipher_abort(&operation));
2506 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002507
2508 /* Set an IV after it's already generated. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002509 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2510 PSA_ASSERT(psa_cipher_generate_iv(&operation,
2511 buffer, sizeof(buffer),
2512 &length));
2513 TEST_EQUAL(psa_cipher_set_iv(&operation,
2514 iv, sizeof(iv)),
2515 PSA_ERROR_BAD_STATE);
2516 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002517
2518 /* Call update without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002519 TEST_EQUAL(psa_cipher_update(&operation,
2520 text, sizeof(text),
2521 buffer, sizeof(buffer),
2522 &length),
2523 PSA_ERROR_BAD_STATE);
2524 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002525
2526 /* Call update without an IV where an IV is required. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002527 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2528 ASSERT_OPERATION_IS_ACTIVE(operation);
2529 TEST_EQUAL(psa_cipher_update(&operation,
2530 text, sizeof(text),
2531 buffer, sizeof(buffer),
2532 &length),
2533 PSA_ERROR_BAD_STATE);
2534 ASSERT_OPERATION_IS_INACTIVE(operation);
2535 PSA_ASSERT(psa_cipher_abort(&operation));
2536 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002537
2538 /* Call update after finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002539 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2540 PSA_ASSERT(psa_cipher_set_iv(&operation,
2541 iv, sizeof(iv)));
2542 PSA_ASSERT(psa_cipher_finish(&operation,
2543 buffer, sizeof(buffer), &length));
2544 TEST_EQUAL(psa_cipher_update(&operation,
2545 text, sizeof(text),
2546 buffer, sizeof(buffer),
2547 &length),
2548 PSA_ERROR_BAD_STATE);
2549 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002550
2551 /* Call finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 TEST_EQUAL(psa_cipher_finish(&operation,
2553 buffer, sizeof(buffer), &length),
2554 PSA_ERROR_BAD_STATE);
2555 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002556
2557 /* Call finish without an IV where an IV is required. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002558 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00002559 /* Not calling update means we are encrypting an empty buffer, which is OK
2560 * for cipher modes with padding. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002561 ASSERT_OPERATION_IS_ACTIVE(operation);
2562 TEST_EQUAL(psa_cipher_finish(&operation,
2563 buffer, sizeof(buffer), &length),
2564 PSA_ERROR_BAD_STATE);
2565 ASSERT_OPERATION_IS_INACTIVE(operation);
2566 PSA_ASSERT(psa_cipher_abort(&operation));
2567 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002568
2569 /* Call finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002570 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2571 PSA_ASSERT(psa_cipher_set_iv(&operation,
2572 iv, sizeof(iv)));
2573 PSA_ASSERT(psa_cipher_finish(&operation,
2574 buffer, sizeof(buffer), &length));
2575 TEST_EQUAL(psa_cipher_finish(&operation,
2576 buffer, sizeof(buffer), &length),
2577 PSA_ERROR_BAD_STATE);
2578 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002581
Jaeden Ameroab439972019-02-15 14:12:05 +00002582exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002583 psa_cipher_abort(&operation);
2584 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002585}
2586/* END_CASE */
2587
2588/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002589void cipher_encrypt_fail(int alg_arg,
2590 int key_type_arg,
2591 data_t *key_data,
2592 data_t *input,
2593 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02002594{
Ronald Cron5425a212020-08-04 14:58:35 +02002595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002596 psa_status_t status;
2597 psa_key_type_t key_type = key_type_arg;
2598 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002599 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002600 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002601 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002602 size_t output_length = 0;
2603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002605 if (PSA_ERROR_BAD_STATE != expected_status) {
2606 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002608 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2609 psa_set_key_algorithm(&attributes, alg);
2610 psa_set_key_type(&attributes, key_type);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
2613 input->len);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002614 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002616 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2617 &key));
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002618 }
2619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002620 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
2621 output_buffer_size, &output_length);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002623 TEST_EQUAL(status, expected_status);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002624
2625exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002626 mbedtls_free(output);
2627 psa_destroy_key(key);
2628 PSA_DONE();
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002629}
2630/* END_CASE */
2631
2632/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002633void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
2634 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002635{
2636 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2637 psa_key_type_t key_type = key_type_arg;
2638 psa_algorithm_t alg = alg_arg;
Ronald Cron33c69682021-07-15 09:38:11 +02002639 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2640 uint8_t iv[1] = { 0x5a };
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002641 unsigned char *output = NULL;
2642 size_t output_buffer_size = 0;
Gilles Peskine4da5a852022-04-20 17:09:38 +02002643 size_t output_length, length;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002644 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002647
Gilles Peskine5f504202022-04-20 16:55:03 +02002648 /* Validate size macros */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649 TEST_LE_U(ciphertext->len,
2650 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
2651 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
2652 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
2653 TEST_LE_U(plaintext->len,
2654 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
2655 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
2656 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine69d98172022-04-20 17:07:52 +02002657
Gilles Peskine5f504202022-04-20 16:55:03 +02002658
2659 /* Set up key and output buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002660 psa_set_key_usage_flags(&attributes,
2661 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
2662 psa_set_key_algorithm(&attributes, alg);
2663 psa_set_key_type(&attributes, key_type);
2664 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2665 &key));
2666 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
2667 plaintext->len);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002668 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002669
Gilles Peskine5f504202022-04-20 16:55:03 +02002670 /* set_iv() is not allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002671 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2672 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
2673 PSA_ERROR_BAD_STATE);
2674 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
2675 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
2676 PSA_ERROR_BAD_STATE);
Gilles Peskine5f504202022-04-20 16:55:03 +02002677
2678 /* generate_iv() is not allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002679 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2680 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
2681 &length),
2682 PSA_ERROR_BAD_STATE);
2683 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
2684 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
2685 &length),
2686 PSA_ERROR_BAD_STATE);
Ronald Cron33c69682021-07-15 09:38:11 +02002687
Gilles Peskine4da5a852022-04-20 17:09:38 +02002688 /* Multipart encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002689 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002690 output_length = 0;
2691 length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002692 PSA_ASSERT(psa_cipher_update(&operation,
2693 plaintext->x, plaintext->len,
2694 output, output_buffer_size,
2695 &length));
2696 TEST_LE_U(length, output_buffer_size);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002697 output_length += length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002698 PSA_ASSERT(psa_cipher_finish(&operation,
2699 mbedtls_buffer_offset(output, output_length),
2700 output_buffer_size - output_length,
2701 &length));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002702 output_length += length;
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002703 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002704 output, output_length);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002705
2706 /* Multipart encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002707 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002708 output_length = 0;
2709 length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002710 PSA_ASSERT(psa_cipher_update(&operation,
2711 ciphertext->x, ciphertext->len,
2712 output, output_buffer_size,
2713 &length));
2714 TEST_LE_U(length, output_buffer_size);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002715 output_length += length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 PSA_ASSERT(psa_cipher_finish(&operation,
2717 mbedtls_buffer_offset(output, output_length),
2718 output_buffer_size - output_length,
2719 &length));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002720 output_length += length;
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002721 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002722 output, output_length);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002723
Gilles Peskine5f504202022-04-20 16:55:03 +02002724 /* One-shot encryption */
Gilles Peskine69d98172022-04-20 17:07:52 +02002725 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002726 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
2727 output, output_buffer_size,
2728 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002729 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002730 output, output_length);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002731
Gilles Peskine69d98172022-04-20 17:07:52 +02002732 /* One-shot decryption */
2733 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002734 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
2735 output, output_buffer_size,
2736 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002737 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002738 output, output_length);
Gilles Peskine5f504202022-04-20 16:55:03 +02002739
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002740exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002741 mbedtls_free(output);
2742 psa_cipher_abort(&operation);
2743 psa_destroy_key(key);
2744 PSA_DONE();
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002745}
2746/* END_CASE */
2747
2748/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002749void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotted33ef12021-07-14 12:31:21 +01002750{
2751 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2752 psa_algorithm_t alg = alg_arg;
2753 psa_key_type_t key_type = key_type_arg;
2754 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2755 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2756 psa_status_t status;
2757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002758 PSA_ASSERT(psa_crypto_init());
Paul Elliotted33ef12021-07-14 12:31:21 +01002759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002760 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2761 psa_set_key_algorithm(&attributes, alg);
2762 psa_set_key_type(&attributes, key_type);
Paul Elliotted33ef12021-07-14 12:31:21 +01002763
2764 /* Usage of either of these two size macros would cause divide by zero
2765 * with incorrect key types previously. Input length should be irrelevant
2766 * here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002767 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
2768 0);
2769 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotted33ef12021-07-14 12:31:21 +01002770
2771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2773 &key));
Paul Elliotted33ef12021-07-14 12:31:21 +01002774
2775 /* Should fail due to invalid alg type (to support invalid key type).
2776 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002777 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotted33ef12021-07-14 12:31:21 +01002778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002779 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotted33ef12021-07-14 12:31:21 +01002780
2781exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 psa_cipher_abort(&operation);
2783 psa_destroy_key(key);
2784 PSA_DONE();
Paul Elliotted33ef12021-07-14 12:31:21 +01002785}
2786/* END_CASE */
2787
2788/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002789void cipher_encrypt_validation(int alg_arg,
2790 int key_type_arg,
2791 data_t *key_data,
2792 data_t *input)
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002793{
2794 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2795 psa_key_type_t key_type = key_type_arg;
2796 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002797 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002798 unsigned char *output1 = NULL;
2799 size_t output1_buffer_size = 0;
2800 size_t output1_length = 0;
2801 unsigned char *output2 = NULL;
2802 size_t output2_buffer_size = 0;
2803 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002804 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002805 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002808 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002810 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2811 psa_set_key_algorithm(&attributes, alg);
2812 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03002813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002814 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
2815 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
2816 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002817 TEST_CALLOC(output1, output1_buffer_size);
2818 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002820 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2821 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002823 /* The one-shot cipher encryption uses generated iv so validating
2824 the output is not possible. Validating with multipart encryption. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002825 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
2826 output1_buffer_size, &output1_length));
2827 TEST_LE_U(output1_length,
2828 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
2829 TEST_LE_U(output1_length,
2830 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002832 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2833 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 PSA_ASSERT(psa_cipher_update(&operation,
2836 input->x, input->len,
2837 output2, output2_buffer_size,
2838 &function_output_length));
2839 TEST_LE_U(function_output_length,
2840 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
2841 TEST_LE_U(function_output_length,
2842 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002843 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002845 PSA_ASSERT(psa_cipher_finish(&operation,
2846 output2 + output2_length,
2847 output2_buffer_size - output2_length,
2848 &function_output_length));
2849 TEST_LE_U(function_output_length,
2850 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
2851 TEST_LE_U(function_output_length,
2852 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002853 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002855 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002856 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002857 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002858
Gilles Peskine50e586b2018-06-08 14:28:46 +02002859exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002860 psa_cipher_abort(&operation);
2861 mbedtls_free(output1);
2862 mbedtls_free(output2);
2863 psa_destroy_key(key);
2864 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02002865}
2866/* END_CASE */
2867
2868/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002869void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
2870 data_t *key_data, data_t *iv,
2871 data_t *input,
2872 int first_part_size_arg,
2873 int output1_length_arg, int output2_length_arg,
2874 data_t *expected_output,
2875 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02002876{
Ronald Cron5425a212020-08-04 14:58:35 +02002877 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002878 psa_key_type_t key_type = key_type_arg;
2879 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002880 psa_status_t status;
2881 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002882 size_t first_part_size = first_part_size_arg;
2883 size_t output1_length = output1_length_arg;
2884 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002885 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002886 size_t output_buffer_size = 0;
2887 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002888 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002889 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002890 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002892 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02002893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002894 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2895 psa_set_key_algorithm(&attributes, alg);
2896 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03002897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002898 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2899 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002900
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002901 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002903 if (iv->len > 0) {
2904 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002905 }
2906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002907 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
2908 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002909 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02002910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002911 TEST_LE_U(first_part_size, input->len);
2912 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
2913 output, output_buffer_size,
2914 &function_output_length));
2915 TEST_ASSERT(function_output_length == output1_length);
2916 TEST_LE_U(function_output_length,
2917 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
2918 TEST_LE_U(function_output_length,
2919 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002920 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 if (first_part_size < input->len) {
2923 PSA_ASSERT(psa_cipher_update(&operation,
2924 input->x + first_part_size,
2925 input->len - first_part_size,
2926 (output_buffer_size == 0 ? NULL :
2927 output + total_output_length),
2928 output_buffer_size - total_output_length,
2929 &function_output_length));
2930 TEST_ASSERT(function_output_length == output2_length);
2931 TEST_LE_U(function_output_length,
2932 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
2933 alg,
2934 input->len - first_part_size));
2935 TEST_LE_U(function_output_length,
2936 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002937 total_output_length += function_output_length;
2938 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002940 status = psa_cipher_finish(&operation,
2941 (output_buffer_size == 0 ? NULL :
2942 output + total_output_length),
2943 output_buffer_size - total_output_length,
2944 &function_output_length);
2945 TEST_LE_U(function_output_length,
2946 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
2947 TEST_LE_U(function_output_length,
2948 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002949 total_output_length += function_output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002950 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02002951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002952 if (expected_status == PSA_SUCCESS) {
2953 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002954
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002955 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002956 output, total_output_length);
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002957 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002958
2959exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002960 psa_cipher_abort(&operation);
2961 mbedtls_free(output);
2962 psa_destroy_key(key);
2963 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964}
2965/* END_CASE */
2966
2967/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002968void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
2969 data_t *key_data, data_t *iv,
2970 data_t *input,
2971 int first_part_size_arg,
2972 int output1_length_arg, int output2_length_arg,
2973 data_t *expected_output,
2974 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02002975{
Ronald Cron5425a212020-08-04 14:58:35 +02002976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002977 psa_key_type_t key_type = key_type_arg;
2978 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002979 psa_status_t status;
2980 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002981 size_t first_part_size = first_part_size_arg;
2982 size_t output1_length = output1_length_arg;
2983 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002984 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002985 size_t output_buffer_size = 0;
2986 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002987 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002988 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002991 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02002992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002993 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
2994 psa_set_key_algorithm(&attributes, alg);
2995 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03002996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002997 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2998 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003000 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02003001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003002 if (iv->len > 0) {
3003 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003004 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003006 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
3007 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003008 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02003009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003010 TEST_LE_U(first_part_size, input->len);
3011 PSA_ASSERT(psa_cipher_update(&operation,
3012 input->x, first_part_size,
3013 output, output_buffer_size,
3014 &function_output_length));
3015 TEST_ASSERT(function_output_length == output1_length);
3016 TEST_LE_U(function_output_length,
3017 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
3018 TEST_LE_U(function_output_length,
3019 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003020 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003022 if (first_part_size < input->len) {
3023 PSA_ASSERT(psa_cipher_update(&operation,
3024 input->x + first_part_size,
3025 input->len - first_part_size,
3026 (output_buffer_size == 0 ? NULL :
3027 output + total_output_length),
3028 output_buffer_size - total_output_length,
3029 &function_output_length));
3030 TEST_ASSERT(function_output_length == output2_length);
3031 TEST_LE_U(function_output_length,
3032 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3033 alg,
3034 input->len - first_part_size));
3035 TEST_LE_U(function_output_length,
3036 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02003037 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003038 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003040 status = psa_cipher_finish(&operation,
3041 (output_buffer_size == 0 ? NULL :
3042 output + total_output_length),
3043 output_buffer_size - total_output_length,
3044 &function_output_length);
3045 TEST_LE_U(function_output_length,
3046 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3047 TEST_LE_U(function_output_length,
3048 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003049 total_output_length += function_output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003050 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02003051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003052 if (expected_status == PSA_SUCCESS) {
3053 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02003054
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003055 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003056 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02003057 }
3058
Gilles Peskine50e586b2018-06-08 14:28:46 +02003059exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003060 psa_cipher_abort(&operation);
3061 mbedtls_free(output);
3062 psa_destroy_key(key);
3063 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02003064}
3065/* END_CASE */
3066
Gilles Peskine50e586b2018-06-08 14:28:46 +02003067/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003068void cipher_decrypt_fail(int alg_arg,
3069 int key_type_arg,
3070 data_t *key_data,
3071 data_t *iv,
3072 data_t *input_arg,
3073 int expected_status_arg)
3074{
3075 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3076 psa_status_t status;
3077 psa_key_type_t key_type = key_type_arg;
3078 psa_algorithm_t alg = alg_arg;
3079 psa_status_t expected_status = expected_status_arg;
3080 unsigned char *input = NULL;
3081 size_t input_buffer_size = 0;
3082 unsigned char *output = NULL;
3083 size_t output_buffer_size = 0;
3084 size_t output_length = 0;
3085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3086
3087 if (PSA_ERROR_BAD_STATE != expected_status) {
3088 PSA_ASSERT(psa_crypto_init());
3089
3090 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3091 psa_set_key_algorithm(&attributes, alg);
3092 psa_set_key_type(&attributes, key_type);
3093
3094 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3095 &key));
3096 }
3097
3098 /* Allocate input buffer and copy the iv and the plaintext */
3099 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
3100 if (input_buffer_size > 0) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003101 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102 memcpy(input, iv->x, iv->len);
3103 memcpy(input + iv->len, input_arg->x, input_arg->len);
3104 }
3105
3106 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003107 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003108
3109 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
3110 output_buffer_size, &output_length);
3111 TEST_EQUAL(status, expected_status);
3112
3113exit:
3114 mbedtls_free(input);
3115 mbedtls_free(output);
3116 psa_destroy_key(key);
3117 PSA_DONE();
3118}
3119/* END_CASE */
3120
3121/* BEGIN_CASE */
3122void cipher_decrypt(int alg_arg,
3123 int key_type_arg,
3124 data_t *key_data,
3125 data_t *iv,
3126 data_t *input_arg,
3127 data_t *expected_output)
3128{
3129 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3130 psa_key_type_t key_type = key_type_arg;
3131 psa_algorithm_t alg = alg_arg;
3132 unsigned char *input = NULL;
3133 size_t input_buffer_size = 0;
3134 unsigned char *output = NULL;
3135 size_t output_buffer_size = 0;
3136 size_t output_length = 0;
3137 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3138
3139 PSA_ASSERT(psa_crypto_init());
3140
3141 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3142 psa_set_key_algorithm(&attributes, alg);
3143 psa_set_key_type(&attributes, key_type);
3144
3145 /* Allocate input buffer and copy the iv and the plaintext */
3146 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
3147 if (input_buffer_size > 0) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003148 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003149 memcpy(input, iv->x, iv->len);
3150 memcpy(input + iv->len, input_arg->x, input_arg->len);
3151 }
3152
3153 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003154 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155
3156 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3157 &key));
3158
3159 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
3160 output_buffer_size, &output_length));
3161 TEST_LE_U(output_length,
3162 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
3163 TEST_LE_U(output_length,
3164 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
3165
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003166 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003167 output, output_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003168exit:
3169 mbedtls_free(input);
3170 mbedtls_free(output);
3171 psa_destroy_key(key);
3172 PSA_DONE();
3173}
3174/* END_CASE */
3175
3176/* BEGIN_CASE */
3177void cipher_verify_output(int alg_arg,
gabor-mezei-arm43611b02021-06-25 15:49:14 +02003178 int key_type_arg,
3179 data_t *key_data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003180 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02003181{
Ronald Cron5425a212020-08-04 14:58:35 +02003182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003183 psa_key_type_t key_type = key_type_arg;
3184 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003185 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003186 size_t output1_size = 0;
3187 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003188 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003189 size_t output2_size = 0;
3190 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003193 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02003194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003195 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3196 psa_set_key_algorithm(&attributes, alg);
3197 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03003198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003199 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3200 &key));
3201 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003202 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03003203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003204 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
3205 output1, output1_size,
3206 &output1_length));
3207 TEST_LE_U(output1_length,
3208 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
3209 TEST_LE_U(output1_length,
3210 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03003211
3212 output2_size = output1_length;
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003213 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03003214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003215 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
3216 output2, output2_size,
3217 &output2_length));
3218 TEST_LE_U(output2_length,
3219 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
3220 TEST_LE_U(output2_length,
3221 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03003222
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003223 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03003224
3225exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003226 mbedtls_free(output1);
3227 mbedtls_free(output2);
3228 psa_destroy_key(key);
3229 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03003230}
3231/* END_CASE */
3232
3233/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003234void cipher_verify_output_multipart(int alg_arg,
3235 int key_type_arg,
3236 data_t *key_data,
3237 data_t *input,
3238 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03003239{
Ronald Cron5425a212020-08-04 14:58:35 +02003240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003241 psa_key_type_t key_type = key_type_arg;
3242 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003243 size_t first_part_size = first_part_size_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003244 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03003245 size_t iv_size = 16;
3246 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003247 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003248 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003249 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003250 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003251 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003252 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003253 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003254 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3255 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003258 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03003259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003260 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3261 psa_set_key_algorithm(&attributes, alg);
3262 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03003263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003264 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3265 &key));
Moran Pekerded84402018-06-06 16:36:50 +03003266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003267 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
3268 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03003269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003270 if (alg != PSA_ALG_ECB_NO_PADDING) {
3271 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
3272 iv, iv_size,
3273 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003274 }
3275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003276 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3277 TEST_LE_U(output1_buffer_size,
3278 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003279 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03003280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003281 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003283 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
3284 output1, output1_buffer_size,
3285 &function_output_length));
3286 TEST_LE_U(function_output_length,
3287 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
3288 TEST_LE_U(function_output_length,
3289 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003290 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003292 PSA_ASSERT(psa_cipher_update(&operation1,
3293 input->x + first_part_size,
3294 input->len - first_part_size,
3295 output1, output1_buffer_size,
3296 &function_output_length));
3297 TEST_LE_U(function_output_length,
3298 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3299 alg,
3300 input->len - first_part_size));
3301 TEST_LE_U(function_output_length,
3302 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003303 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003305 PSA_ASSERT(psa_cipher_finish(&operation1,
3306 output1 + output1_length,
3307 output1_buffer_size - output1_length,
3308 &function_output_length));
3309 TEST_LE_U(function_output_length,
3310 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3311 TEST_LE_U(function_output_length,
3312 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02003313 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003315 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02003316
Gilles Peskine048b7f02018-06-08 14:20:49 +02003317 output2_buffer_size = output1_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003318 TEST_LE_U(output2_buffer_size,
3319 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
3320 TEST_LE_U(output2_buffer_size,
3321 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003322 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02003323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003324 if (iv_length > 0) {
3325 PSA_ASSERT(psa_cipher_set_iv(&operation2,
3326 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003327 }
Moran Pekerded84402018-06-06 16:36:50 +03003328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
3330 output2, output2_buffer_size,
3331 &function_output_length));
3332 TEST_LE_U(function_output_length,
3333 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
3334 TEST_LE_U(function_output_length,
3335 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003336 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003338 PSA_ASSERT(psa_cipher_update(&operation2,
3339 output1 + first_part_size,
3340 output1_length - first_part_size,
3341 output2, output2_buffer_size,
3342 &function_output_length));
3343 TEST_LE_U(function_output_length,
3344 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3345 alg,
3346 output1_length - first_part_size));
3347 TEST_LE_U(function_output_length,
3348 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003349 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003351 PSA_ASSERT(psa_cipher_finish(&operation2,
3352 output2 + output2_length,
3353 output2_buffer_size - output2_length,
3354 &function_output_length));
3355 TEST_LE_U(function_output_length,
3356 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3357 TEST_LE_U(function_output_length,
3358 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02003359 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02003362
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003363 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02003364
3365exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003366 psa_cipher_abort(&operation1);
3367 psa_cipher_abort(&operation2);
3368 mbedtls_free(output1);
3369 mbedtls_free(output2);
3370 psa_destroy_key(key);
3371 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02003372}
3373/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003374
Gilles Peskine20035e32018-02-03 22:44:14 +01003375/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003376void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
3377 int alg_arg,
3378 data_t *nonce,
3379 data_t *additional_data,
3380 data_t *input_data,
3381 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003382{
Ronald Cron5425a212020-08-04 14:58:35 +02003383 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003384 psa_key_type_t key_type = key_type_arg;
3385 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003386 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003387 unsigned char *output_data = NULL;
3388 size_t output_size = 0;
3389 size_t output_length = 0;
3390 unsigned char *output_data2 = NULL;
3391 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003392 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003393 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003394 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003396 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003398 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3399 psa_set_key_algorithm(&attributes, alg);
3400 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003402 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3403 &key));
3404 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3405 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003407 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3408 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003409 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3410 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003411 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3412 expected_result != PSA_ERROR_NOT_SUPPORTED) {
3413 TEST_EQUAL(output_size,
3414 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3415 TEST_ASSERT(output_size <=
3416 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01003417 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003418 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003420 status = psa_aead_encrypt(key, alg,
3421 nonce->x, nonce->len,
3422 additional_data->x,
3423 additional_data->len,
3424 input_data->x, input_data->len,
3425 output_data, output_size,
3426 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01003427
3428 /* If the operation is not supported, just skip and not fail in case the
3429 * encryption involves a common limitation of cryptography hardwares and
3430 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003431 if (status == PSA_ERROR_NOT_SUPPORTED) {
3432 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3433 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01003434 }
3435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003436 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003439 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003440
Gilles Peskine003a4a92019-05-14 16:09:40 +02003441 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3442 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003443 TEST_EQUAL(input_data->len,
3444 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02003445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 TEST_ASSERT(input_data->len <=
3447 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01003448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003449 TEST_EQUAL(psa_aead_decrypt(key, alg,
3450 nonce->x, nonce->len,
3451 additional_data->x,
3452 additional_data->len,
3453 output_data, output_length,
3454 output_data2, output_length,
3455 &output_length2),
3456 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02003457
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003458 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003459 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003461
Gilles Peskinea1cac842018-06-11 19:33:02 +02003462exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003463 psa_destroy_key(key);
3464 mbedtls_free(output_data);
3465 mbedtls_free(output_data2);
3466 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003467}
3468/* END_CASE */
3469
3470/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003471void aead_encrypt(int key_type_arg, data_t *key_data,
3472 int alg_arg,
3473 data_t *nonce,
3474 data_t *additional_data,
3475 data_t *input_data,
3476 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003477{
Ronald Cron5425a212020-08-04 14:58:35 +02003478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479 psa_key_type_t key_type = key_type_arg;
3480 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003481 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003482 unsigned char *output_data = NULL;
3483 size_t output_size = 0;
3484 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003486 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003490 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3491 psa_set_key_algorithm(&attributes, alg);
3492 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003494 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3495 &key));
3496 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3497 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003499 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3500 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003501 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3502 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003503 TEST_EQUAL(output_size,
3504 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3505 TEST_ASSERT(output_size <=
3506 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003507 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003509 status = psa_aead_encrypt(key, alg,
3510 nonce->x, nonce->len,
3511 additional_data->x, additional_data->len,
3512 input_data->x, input_data->len,
3513 output_data, output_size,
3514 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003515
Ronald Cron28a45ed2021-02-09 20:35:42 +01003516 /* If the operation is not supported, just skip and not fail in case the
3517 * encryption involves a common limitation of cryptography hardwares and
3518 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003519 if (status == PSA_ERROR_NOT_SUPPORTED) {
3520 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3521 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01003522 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003524 PSA_ASSERT(status);
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003525 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003526 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003527
Gilles Peskinea1cac842018-06-11 19:33:02 +02003528exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003529 psa_destroy_key(key);
3530 mbedtls_free(output_data);
3531 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003532}
3533/* END_CASE */
3534
3535/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003536void aead_decrypt(int key_type_arg, data_t *key_data,
3537 int alg_arg,
3538 data_t *nonce,
3539 data_t *additional_data,
3540 data_t *input_data,
3541 data_t *expected_data,
3542 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003543{
Ronald Cron5425a212020-08-04 14:58:35 +02003544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003545 psa_key_type_t key_type = key_type_arg;
3546 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003547 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003548 unsigned char *output_data = NULL;
3549 size_t output_size = 0;
3550 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003552 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003553 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003555 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003557 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3558 psa_set_key_algorithm(&attributes, alg);
3559 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003561 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3562 &key));
3563 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3564 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003566 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3567 alg);
3568 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3569 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01003570 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3571 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003572 TEST_EQUAL(output_size,
3573 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3574 TEST_ASSERT(output_size <=
3575 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01003576 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003577 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 status = psa_aead_decrypt(key, alg,
3580 nonce->x, nonce->len,
3581 additional_data->x,
3582 additional_data->len,
3583 input_data->x, input_data->len,
3584 output_data, output_size,
3585 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01003586
Ronald Cron28a45ed2021-02-09 20:35:42 +01003587 /* If the operation is not supported, just skip and not fail in case the
3588 * decryption involves a common limitation of cryptography hardwares and
3589 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003590 if (status == PSA_ERROR_NOT_SUPPORTED) {
3591 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3592 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01003593 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003595 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003597 if (expected_result == PSA_SUCCESS) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003598 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003599 output_data, output_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003600 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02003601
Gilles Peskinea1cac842018-06-11 19:33:02 +02003602exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 psa_destroy_key(key);
3604 mbedtls_free(output_data);
3605 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003606}
3607/* END_CASE */
3608
3609/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003610void signature_size(int type_arg,
3611 int bits,
3612 int alg_arg,
3613 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003614{
3615 psa_key_type_t type = type_arg;
3616 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003617 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01003618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003619 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01003620#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003621 TEST_EQUAL(actual_size,
3622 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg));
Gilles Peskine841b14b2019-11-26 17:37:37 +01003623#endif /* MBEDTLS_TEST_DEPRECATED */
3624
Gilles Peskinee59236f2018-01-27 23:32:46 +01003625exit:
3626 ;
3627}
3628/* END_CASE */
3629
3630/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003631void sign_hash_deterministic(int key_type_arg, data_t *key_data,
3632 int alg_arg, data_t *input_data,
3633 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003634{
Ronald Cron5425a212020-08-04 14:58:35 +02003635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003636 psa_key_type_t key_type = key_type_arg;
3637 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003638 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003639 unsigned char *signature = NULL;
3640 size_t signature_size;
3641 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003644 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01003645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003646 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3647 psa_set_key_algorithm(&attributes, alg);
3648 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07003649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003650 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3651 &key));
3652 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3653 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01003654
Shaun Case0e7791f2021-12-20 21:14:10 -08003655 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003656 * library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003657 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
3658 key_bits, alg);
3659 TEST_ASSERT(signature_size != 0);
3660 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003661 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003662
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003663 /* Perform the signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003664 PSA_ASSERT(psa_sign_hash(key, alg,
3665 input_data->x, input_data->len,
3666 signature, signature_size,
3667 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003668 /* Verify that the signature is what is expected. */
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003669 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003670 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003671
Gilles Peskine0627f982019-11-26 19:12:16 +01003672#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003673 memset(signature, 0, signature_size);
Gilles Peskine895242b2019-11-29 12:15:40 +01003674 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003675 PSA_ASSERT(psa_asymmetric_sign(key, alg,
3676 input_data->x, input_data->len,
3677 signature, signature_size,
3678 &signature_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003679 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003680 signature, signature_length);
Gilles Peskine0627f982019-11-26 19:12:16 +01003681#endif /* MBEDTLS_TEST_DEPRECATED */
3682
Gilles Peskine20035e32018-02-03 22:44:14 +01003683exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003684 /*
3685 * Key attributes may have been returned by psa_get_key_attributes()
3686 * thus reset them as required.
3687 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003688 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003690 psa_destroy_key(key);
3691 mbedtls_free(signature);
3692 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01003693}
3694/* END_CASE */
3695
3696/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003697void sign_hash_fail(int key_type_arg, data_t *key_data,
3698 int alg_arg, data_t *input_data,
3699 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01003700{
Ronald Cron5425a212020-08-04 14:58:35 +02003701 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003702 psa_key_type_t key_type = key_type_arg;
3703 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003704 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003705 psa_status_t actual_status;
3706 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003707 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003708 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003710
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003711 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003713 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01003714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003715 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3716 psa_set_key_algorithm(&attributes, alg);
3717 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07003718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3720 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01003721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003722 actual_status = psa_sign_hash(key, alg,
3723 input_data->x, input_data->len,
3724 signature, signature_size,
3725 &signature_length);
3726 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003727 /* The value of *signature_length is unspecified on error, but
3728 * whatever it is, it should be less than signature_size, so that
3729 * if the caller tries to read *signature_length bytes without
3730 * checking the error code then they don't overflow a buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003731 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003732
Gilles Peskine895242b2019-11-29 12:15:40 +01003733#if defined(MBEDTLS_TEST_DEPRECATED)
3734 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003735 TEST_EQUAL(psa_asymmetric_sign(key, alg,
3736 input_data->x, input_data->len,
3737 signature, signature_size,
3738 &signature_length),
3739 expected_status);
3740 TEST_LE_U(signature_length, signature_size);
Gilles Peskine895242b2019-11-29 12:15:40 +01003741#endif /* MBEDTLS_TEST_DEPRECATED */
3742
Gilles Peskine20035e32018-02-03 22:44:14 +01003743exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003744 psa_reset_key_attributes(&attributes);
3745 psa_destroy_key(key);
3746 mbedtls_free(signature);
3747 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01003748}
3749/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003750
3751/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003752void sign_verify_hash(int key_type_arg, data_t *key_data,
3753 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02003754{
Ronald Cron5425a212020-08-04 14:58:35 +02003755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003756 psa_key_type_t key_type = key_type_arg;
3757 psa_algorithm_t alg = alg_arg;
3758 size_t key_bits;
3759 unsigned char *signature = NULL;
3760 size_t signature_size;
3761 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003762 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003764 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02003765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003766 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3767 psa_set_key_algorithm(&attributes, alg);
3768 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02003769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003770 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3771 &key));
3772 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3773 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02003774
Shaun Case0e7791f2021-12-20 21:14:10 -08003775 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02003776 * library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003777 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
3778 key_bits, alg);
3779 TEST_ASSERT(signature_size != 0);
3780 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003781 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02003782
3783 /* Perform the signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003784 PSA_ASSERT(psa_sign_hash(key, alg,
3785 input_data->x, input_data->len,
3786 signature, signature_size,
3787 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003788 /* Check that the signature length looks sensible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003789 TEST_LE_U(signature_length, signature_size);
3790 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02003791
3792 /* Use the library to verify that the signature is correct. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003793 PSA_ASSERT(psa_verify_hash(key, alg,
3794 input_data->x, input_data->len,
3795 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003797 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02003798 /* Flip a bit in the input and verify that the signature is now
3799 * detected as invalid. Flip a bit at the beginning, not at the end,
3800 * because ECDSA may ignore the last few bits of the input. */
3801 input_data->x[0] ^= 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003802 TEST_EQUAL(psa_verify_hash(key, alg,
3803 input_data->x, input_data->len,
3804 signature, signature_length),
3805 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02003806 }
3807
3808exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003809 /*
3810 * Key attributes may have been returned by psa_get_key_attributes()
3811 * thus reset them as required.
3812 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003813 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003815 psa_destroy_key(key);
3816 mbedtls_free(signature);
3817 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02003818}
3819/* END_CASE */
3820
3821/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003822void verify_hash(int key_type_arg, data_t *key_data,
3823 int alg_arg, data_t *hash_data,
3824 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03003825{
Ronald Cron5425a212020-08-04 14:58:35 +02003826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003827 psa_key_type_t key_type = key_type_arg;
3828 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003829 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003831 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003833 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03003834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003835 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3836 psa_set_key_algorithm(&attributes, alg);
3837 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03003838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003839 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3840 &key));
itayzafrir5c753392018-05-08 11:18:38 +03003841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003842 PSA_ASSERT(psa_verify_hash(key, alg,
3843 hash_data->x, hash_data->len,
3844 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01003845
3846#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003847 PSA_ASSERT(psa_asymmetric_verify(key, alg,
3848 hash_data->x, hash_data->len,
3849 signature_data->x,
3850 signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01003851
3852#endif /* MBEDTLS_TEST_DEPRECATED */
3853
itayzafrir5c753392018-05-08 11:18:38 +03003854exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003855 psa_reset_key_attributes(&attributes);
3856 psa_destroy_key(key);
3857 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03003858}
3859/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003860
3861/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003862void verify_hash_fail(int key_type_arg, data_t *key_data,
3863 int alg_arg, data_t *hash_data,
3864 data_t *signature_data,
3865 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003866{
Ronald Cron5425a212020-08-04 14:58:35 +02003867 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868 psa_key_type_t key_type = key_type_arg;
3869 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003870 psa_status_t actual_status;
3871 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003872 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003874 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003876 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3877 psa_set_key_algorithm(&attributes, alg);
3878 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3881 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003883 actual_status = psa_verify_hash(key, alg,
3884 hash_data->x, hash_data->len,
3885 signature_data->x, signature_data->len);
3886 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887
Gilles Peskine895242b2019-11-29 12:15:40 +01003888#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003889 TEST_EQUAL(psa_asymmetric_verify(key, alg,
3890 hash_data->x, hash_data->len,
3891 signature_data->x, signature_data->len),
3892 expected_status);
Gilles Peskine895242b2019-11-29 12:15:40 +01003893#endif /* MBEDTLS_TEST_DEPRECATED */
3894
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003895exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003896 psa_reset_key_attributes(&attributes);
3897 psa_destroy_key(key);
3898 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899}
3900/* END_CASE */
3901
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003902/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003903void sign_message_deterministic(int key_type_arg,
3904 data_t *key_data,
3905 int alg_arg,
3906 data_t *input_data,
3907 data_t *output_data)
gabor-mezei-armabd72582021-04-15 18:19:50 +02003908{
3909 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3910 psa_key_type_t key_type = key_type_arg;
3911 psa_algorithm_t alg = alg_arg;
3912 size_t key_bits;
3913 unsigned char *signature = NULL;
3914 size_t signature_size;
3915 size_t signature_length = 0xdeadbeef;
3916 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003918 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armabd72582021-04-15 18:19:50 +02003919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003920 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3921 psa_set_key_algorithm(&attributes, alg);
3922 psa_set_key_type(&attributes, key_type);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003924 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3925 &key));
3926 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3927 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003929 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
3930 TEST_ASSERT(signature_size != 0);
3931 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003932 TEST_CALLOC(signature, signature_size);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003934 PSA_ASSERT(psa_sign_message(key, alg,
3935 input_data->x, input_data->len,
3936 signature, signature_size,
3937 &signature_length));
gabor-mezei-armabd72582021-04-15 18:19:50 +02003938
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003939 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003940 signature, signature_length);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003941
3942exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003943 psa_reset_key_attributes(&attributes);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003945 psa_destroy_key(key);
3946 mbedtls_free(signature);
3947 PSA_DONE();
gabor-mezei-armabd72582021-04-15 18:19:50 +02003948
3949}
3950/* END_CASE */
3951
3952/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003953void sign_message_fail(int key_type_arg,
3954 data_t *key_data,
3955 int alg_arg,
3956 data_t *input_data,
3957 int signature_size_arg,
3958 int expected_status_arg)
3959{
3960 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3961 psa_key_type_t key_type = key_type_arg;
3962 psa_algorithm_t alg = alg_arg;
3963 size_t signature_size = signature_size_arg;
3964 psa_status_t actual_status;
3965 psa_status_t expected_status = expected_status_arg;
3966 unsigned char *signature = NULL;
3967 size_t signature_length = 0xdeadbeef;
3968 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3969
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003970 TEST_CALLOC(signature, signature_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003971
3972 PSA_ASSERT(psa_crypto_init());
3973
3974 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3975 psa_set_key_algorithm(&attributes, alg);
3976 psa_set_key_type(&attributes, key_type);
3977
3978 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3979 &key));
3980
3981 actual_status = psa_sign_message(key, alg,
3982 input_data->x, input_data->len,
3983 signature, signature_size,
3984 &signature_length);
3985 TEST_EQUAL(actual_status, expected_status);
3986 /* The value of *signature_length is unspecified on error, but
3987 * whatever it is, it should be less than signature_size, so that
3988 * if the caller tries to read *signature_length bytes without
3989 * checking the error code then they don't overflow a buffer. */
3990 TEST_LE_U(signature_length, signature_size);
3991
3992exit:
3993 psa_reset_key_attributes(&attributes);
3994 psa_destroy_key(key);
3995 mbedtls_free(signature);
3996 PSA_DONE();
3997}
3998/* END_CASE */
3999
4000/* BEGIN_CASE */
4001void sign_verify_message(int key_type_arg,
4002 data_t *key_data,
4003 int alg_arg,
4004 data_t *input_data)
4005{
4006 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4007 psa_key_type_t key_type = key_type_arg;
4008 psa_algorithm_t alg = alg_arg;
4009 size_t key_bits;
4010 unsigned char *signature = NULL;
4011 size_t signature_size;
4012 size_t signature_length = 0xdeadbeef;
4013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4014
4015 PSA_ASSERT(psa_crypto_init());
4016
4017 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4018 PSA_KEY_USAGE_VERIFY_MESSAGE);
4019 psa_set_key_algorithm(&attributes, alg);
4020 psa_set_key_type(&attributes, key_type);
4021
4022 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4023 &key));
4024 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4025 key_bits = psa_get_key_bits(&attributes);
4026
4027 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
4028 TEST_ASSERT(signature_size != 0);
4029 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004030 TEST_CALLOC(signature, signature_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004031
4032 PSA_ASSERT(psa_sign_message(key, alg,
4033 input_data->x, input_data->len,
4034 signature, signature_size,
4035 &signature_length));
4036 TEST_LE_U(signature_length, signature_size);
4037 TEST_ASSERT(signature_length > 0);
4038
4039 PSA_ASSERT(psa_verify_message(key, alg,
4040 input_data->x, input_data->len,
4041 signature, signature_length));
4042
4043 if (input_data->len != 0) {
4044 /* Flip a bit in the input and verify that the signature is now
4045 * detected as invalid. Flip a bit at the beginning, not at the end,
4046 * because ECDSA may ignore the last few bits of the input. */
4047 input_data->x[0] ^= 1;
4048 TEST_EQUAL(psa_verify_message(key, alg,
4049 input_data->x, input_data->len,
4050 signature, signature_length),
4051 PSA_ERROR_INVALID_SIGNATURE);
4052 }
4053
4054exit:
4055 psa_reset_key_attributes(&attributes);
4056
4057 psa_destroy_key(key);
4058 mbedtls_free(signature);
4059 PSA_DONE();
4060}
4061/* END_CASE */
4062
4063/* BEGIN_CASE */
4064void verify_message(int key_type_arg,
4065 data_t *key_data,
4066 int alg_arg,
4067 data_t *input_data,
4068 data_t *signature_data)
4069{
4070 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4071 psa_key_type_t key_type = key_type_arg;
4072 psa_algorithm_t alg = alg_arg;
4073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4074
4075 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
4076
4077 PSA_ASSERT(psa_crypto_init());
4078
4079 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
4080 psa_set_key_algorithm(&attributes, alg);
4081 psa_set_key_type(&attributes, key_type);
4082
4083 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4084 &key));
4085
4086 PSA_ASSERT(psa_verify_message(key, alg,
4087 input_data->x, input_data->len,
4088 signature_data->x, signature_data->len));
4089
4090exit:
4091 psa_reset_key_attributes(&attributes);
4092 psa_destroy_key(key);
4093 PSA_DONE();
4094}
4095/* END_CASE */
4096
4097/* BEGIN_CASE */
4098void verify_message_fail(int key_type_arg,
4099 data_t *key_data,
4100 int alg_arg,
4101 data_t *hash_data,
4102 data_t *signature_data,
4103 int expected_status_arg)
4104{
4105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4106 psa_key_type_t key_type = key_type_arg;
4107 psa_algorithm_t alg = alg_arg;
4108 psa_status_t actual_status;
4109 psa_status_t expected_status = expected_status_arg;
4110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4111
4112 PSA_ASSERT(psa_crypto_init());
4113
4114 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
4115 psa_set_key_algorithm(&attributes, alg);
4116 psa_set_key_type(&attributes, key_type);
4117
4118 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4119 &key));
4120
4121 actual_status = psa_verify_message(key, alg,
4122 hash_data->x, hash_data->len,
4123 signature_data->x,
4124 signature_data->len);
4125 TEST_EQUAL(actual_status, expected_status);
4126
4127exit:
4128 psa_reset_key_attributes(&attributes);
4129 psa_destroy_key(key);
4130 PSA_DONE();
4131}
4132/* END_CASE */
4133
4134/* BEGIN_CASE */
4135void asymmetric_encrypt(int key_type_arg,
gabor-mezei-armabd72582021-04-15 18:19:50 +02004136 data_t *key_data,
4137 int alg_arg,
4138 data_t *input_data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004139 data_t *label,
4140 int expected_output_length_arg,
4141 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02004142{
Ronald Cron5425a212020-08-04 14:58:35 +02004143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004144 psa_key_type_t key_type = key_type_arg;
4145 psa_algorithm_t alg = alg_arg;
4146 size_t expected_output_length = expected_output_length_arg;
4147 size_t key_bits;
4148 unsigned char *output = NULL;
4149 size_t output_size;
4150 size_t output_length = ~0;
4151 psa_status_t actual_status;
4152 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004155 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004156
Gilles Peskine656896e2018-06-29 19:12:28 +02004157 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004158 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4159 psa_set_key_algorithm(&attributes, alg);
4160 psa_set_key_type(&attributes, key_type);
4161 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4162 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02004163
4164 /* Determine the maximum output length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004165 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4166 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004168 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4169 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004170 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02004171
4172 /* Encrypt the input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004173 actual_status = psa_asymmetric_encrypt(key, alg,
4174 input_data->x, input_data->len,
4175 label->x, label->len,
4176 output, output_size,
4177 &output_length);
4178 TEST_EQUAL(actual_status, expected_status);
oberon-sk8a23f492023-02-13 13:42:02 +01004179 if (actual_status == PSA_SUCCESS) {
4180 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch6ed14362023-02-22 13:39:21 +01004181 } else {
4182 TEST_LE_U(output_length, output_size);
oberon-sk8a23f492023-02-13 13:42:02 +01004183 }
Gilles Peskine656896e2018-06-29 19:12:28 +02004184
Gilles Peskine68428122018-06-30 18:42:41 +02004185 /* If the label is empty, the test framework puts a non-null pointer
4186 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004187 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004188 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004189 if (output_size != 0) {
4190 memset(output, 0, output_size);
4191 }
4192 actual_status = psa_asymmetric_encrypt(key, alg,
4193 input_data->x, input_data->len,
4194 NULL, label->len,
4195 output, output_size,
4196 &output_length);
4197 TEST_EQUAL(actual_status, expected_status);
oberon-sk8a23f492023-02-13 13:42:02 +01004198 if (actual_status == PSA_SUCCESS) {
4199 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch6ed14362023-02-22 13:39:21 +01004200 } else {
4201 TEST_LE_U(output_length, output_size);
oberon-sk8a23f492023-02-13 13:42:02 +01004202 }
Gilles Peskine68428122018-06-30 18:42:41 +02004203 }
4204
Gilles Peskine656896e2018-06-29 19:12:28 +02004205exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004206 /*
4207 * Key attributes may have been returned by psa_get_key_attributes()
4208 * thus reset them as required.
4209 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004210 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004212 psa_destroy_key(key);
4213 mbedtls_free(output);
4214 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02004215}
4216/* END_CASE */
4217
4218/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004219void asymmetric_encrypt_decrypt(int key_type_arg,
4220 data_t *key_data,
4221 int alg_arg,
4222 data_t *input_data,
4223 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004224{
Ronald Cron5425a212020-08-04 14:58:35 +02004225 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004226 psa_key_type_t key_type = key_type_arg;
4227 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004228 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004229 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004230 size_t output_size;
4231 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004232 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004233 size_t output2_size;
4234 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004237 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004239 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4240 psa_set_key_algorithm(&attributes, alg);
4241 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004243 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4244 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004245
4246 /* Determine the maximum ciphertext length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004247 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4248 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004250 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4251 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004252 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01004253
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004254 output2_size = input_data->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255 TEST_LE_U(output2_size,
4256 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
4257 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004258 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004259
Gilles Peskineeebd7382018-06-08 18:11:54 +02004260 /* We test encryption by checking that encrypt-then-decrypt gives back
4261 * the original plaintext because of the non-optional random
4262 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004263 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
4264 input_data->x, input_data->len,
4265 label->x, label->len,
4266 output, output_size,
4267 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004268 /* We don't know what ciphertext length to expect, but check that
4269 * it looks sensible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004270 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004272 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4273 output, output_length,
4274 label->x, label->len,
4275 output2, output2_size,
4276 &output2_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004277 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004278 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004279
4280exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004281 /*
4282 * Key attributes may have been returned by psa_get_key_attributes()
4283 * thus reset them as required.
4284 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004285 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004287 psa_destroy_key(key);
4288 mbedtls_free(output);
4289 mbedtls_free(output2);
4290 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004291}
4292/* END_CASE */
4293
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004294/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295void asymmetric_decrypt(int key_type_arg,
4296 data_t *key_data,
4297 int alg_arg,
4298 data_t *input_data,
4299 data_t *label,
4300 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004301{
Ronald Cron5425a212020-08-04 14:58:35 +02004302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004303 psa_key_type_t key_type = key_type_arg;
4304 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004305 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004306 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004307 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004308 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004309 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004311 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4314 psa_set_key_algorithm(&attributes, alg);
4315 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004317 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4318 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004320 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4321 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004322
4323 /* Determine the maximum ciphertext length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4325 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004326 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01004327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004328 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4329 input_data->x, input_data->len,
4330 label->x, label->len,
4331 output,
4332 output_size,
4333 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004334 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004335 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004336
Gilles Peskine68428122018-06-30 18:42:41 +02004337 /* If the label is empty, the test framework puts a non-null pointer
4338 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004339 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004340 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341 if (output_size != 0) {
4342 memset(output, 0, output_size);
4343 }
4344 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4345 input_data->x, input_data->len,
4346 NULL, label->len,
4347 output,
4348 output_size,
4349 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004350 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004351 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02004352 }
4353
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004354exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004355 psa_reset_key_attributes(&attributes);
4356 psa_destroy_key(key);
4357 mbedtls_free(output);
4358 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004359}
4360/* END_CASE */
4361
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004362/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004363void asymmetric_decrypt_fail(int key_type_arg,
4364 data_t *key_data,
4365 int alg_arg,
4366 data_t *input_data,
4367 data_t *label,
4368 int output_size_arg,
4369 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004370{
Ronald Cron5425a212020-08-04 14:58:35 +02004371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004372 psa_key_type_t key_type = key_type_arg;
4373 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004374 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004375 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004376 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004377 psa_status_t actual_status;
4378 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004379 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004380
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004381 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004383 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004385 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4386 psa_set_key_algorithm(&attributes, alg);
4387 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004389 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4390 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004392 actual_status = psa_asymmetric_decrypt(key, alg,
4393 input_data->x, input_data->len,
4394 label->x, label->len,
4395 output, output_size,
4396 &output_length);
4397 TEST_EQUAL(actual_status, expected_status);
4398 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004399
Gilles Peskine68428122018-06-30 18:42:41 +02004400 /* If the label is empty, the test framework puts a non-null pointer
4401 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004402 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004403 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004404 if (output_size != 0) {
4405 memset(output, 0, output_size);
4406 }
4407 actual_status = psa_asymmetric_decrypt(key, alg,
4408 input_data->x, input_data->len,
4409 NULL, label->len,
4410 output, output_size,
4411 &output_length);
4412 TEST_EQUAL(actual_status, expected_status);
4413 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02004414 }
4415
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004416exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004417 psa_reset_key_attributes(&attributes);
4418 psa_destroy_key(key);
4419 mbedtls_free(output);
4420 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004421}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004422/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004423
4424/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004425void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00004426{
4427 /* Test each valid way of initializing the object, except for `= {0}`, as
4428 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4429 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08004430 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004431 size_t capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004432 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004433 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4434 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004436 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00004437
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 /* A default operation should not be able to report its capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004439 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
4440 PSA_ERROR_BAD_STATE);
4441 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
4442 PSA_ERROR_BAD_STATE);
4443 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
4444 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004445
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004446 /* A default operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004447 PSA_ASSERT(psa_key_derivation_abort(&func));
4448 PSA_ASSERT(psa_key_derivation_abort(&init));
4449 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00004450}
4451/* END_CASE */
4452
Janos Follath16de4a42019-06-13 16:32:24 +01004453/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004454void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02004455{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004456 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004457 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004458 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004460 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02004461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004462 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
4463 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02004464
4465exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004466 psa_key_derivation_abort(&operation);
4467 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02004468}
4469/* END_CASE */
4470
Janos Follathaf3c2a02019-06-12 12:34:34 +01004471/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004472void derive_set_capacity(int alg_arg, int capacity_arg,
4473 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01004474{
4475 psa_algorithm_t alg = alg_arg;
4476 size_t capacity = capacity_arg;
4477 psa_status_t expected_status = expected_status_arg;
4478 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01004481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01004483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004484 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
4485 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01004486
4487exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004488 psa_key_derivation_abort(&operation);
4489 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01004490}
4491/* END_CASE */
4492
4493/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004494void derive_input(int alg_arg,
4495 int step_arg1, int key_type_arg1, data_t *input1,
4496 int expected_status_arg1,
4497 int step_arg2, int key_type_arg2, data_t *input2,
4498 int expected_status_arg2,
4499 int step_arg3, int key_type_arg3, data_t *input3,
4500 int expected_status_arg3,
4501 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01004502{
4503 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004504 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
4505 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
4506 psa_status_t expected_statuses[] = { expected_status_arg1,
4507 expected_status_arg2,
4508 expected_status_arg3 };
4509 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02004510 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4511 MBEDTLS_SVC_KEY_ID_INIT,
4512 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004513 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4515 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004516 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004517 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004518 psa_status_t expected_output_status = expected_output_status_arg;
4519 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004521 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01004522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4524 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01004525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004526 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01004527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
4529 mbedtls_test_set_step(i);
4530 if (steps[i] == 0) {
Gilles Peskinef6279312021-05-27 13:21:20 +02004531 /* Skip this step */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004532 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
4533 psa_set_key_type(&attributes, key_types[i]);
4534 PSA_ASSERT(psa_import_key(&attributes,
4535 inputs[i]->x, inputs[i]->len,
4536 &keys[i]));
4537 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
4538 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004539 // When taking a private key as secret input, use key agreement
4540 // to add the shared secret to the derivation
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004541 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
4542 &operation, keys[i]),
4543 expected_statuses[i]);
4544 } else {
4545 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
4546 keys[i]),
4547 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004548 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004549 } else {
4550 TEST_EQUAL(psa_key_derivation_input_bytes(
4551 &operation, steps[i],
4552 inputs[i]->x, inputs[i]->len),
4553 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01004554 }
4555 }
4556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004557 if (output_key_type != PSA_KEY_TYPE_NONE) {
4558 psa_reset_key_attributes(&attributes);
4559 psa_set_key_type(&attributes, output_key_type);
4560 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004561 actual_output_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004562 psa_key_derivation_output_key(&attributes, &operation,
4563 &output_key);
4564 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004565 uint8_t buffer[1];
4566 actual_output_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004567 psa_key_derivation_output_bytes(&operation,
4568 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004569 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004571
Janos Follathaf3c2a02019-06-12 12:34:34 +01004572exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004573 psa_key_derivation_abort(&operation);
4574 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
4575 psa_destroy_key(keys[i]);
4576 }
4577 psa_destroy_key(output_key);
4578 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01004579}
4580/* END_CASE */
4581
Janos Follathd958bb72019-07-03 15:02:16 +01004582/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004583void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004584{
Janos Follathd958bb72019-07-03 15:02:16 +01004585 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004587 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004588 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004589 unsigned char input1[] = "Input 1";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004590 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01004591 unsigned char input2[] = "Input 2";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004592 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004593 uint8_t buffer[42];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004594 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004595 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4596 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004597 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004600 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004602 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4603 psa_set_key_algorithm(&attributes, alg);
4604 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004606 PSA_ASSERT(psa_import_key(&attributes,
4607 key_data, sizeof(key_data),
4608 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004609
4610 /* valid key derivation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004611 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
4612 input1, input1_length,
4613 input2, input2_length,
4614 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01004615 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004616 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004617
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004618 /* state of operation shouldn't allow additional generation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004619 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
4620 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004622 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004624 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
4625 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004626
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004627exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004628 psa_key_derivation_abort(&operation);
4629 psa_destroy_key(key);
4630 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004631}
4632/* END_CASE */
4633
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004634/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004636{
4637 uint8_t output_buffer[16];
4638 size_t buffer_size = 16;
4639 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004640 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004642 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
4643 output_buffer, buffer_size)
4644 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004646 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
4647 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004649 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004651 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
4652 output_buffer, buffer_size)
4653 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004655 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
4656 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004657
4658exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004659 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004660}
4661/* END_CASE */
4662
4663/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004664void derive_output(int alg_arg,
4665 int step1_arg, data_t *input1,
4666 int step2_arg, data_t *input2,
4667 int step3_arg, data_t *input3,
4668 int requested_capacity_arg,
4669 data_t *expected_output1,
4670 data_t *expected_output2)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004671{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004672 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004673 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg };
4674 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02004675 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4676 MBEDTLS_SVC_KEY_ID_INIT,
4677 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004678 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004679 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004680 uint8_t *expected_outputs[2] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004681 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004682 size_t output_sizes[2] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004683 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004684 size_t output_buffer_size = 0;
4685 uint8_t *output_buffer = NULL;
4686 size_t expected_capacity;
4687 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004689 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004690 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004692 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
4693 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004694 output_buffer_size = output_sizes[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004695 }
4696 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004697 expected_outputs[i] = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004698 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004699 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004700 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4704 psa_set_key_algorithm(&attributes, alg);
4705 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004706
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004707 /* Extraction phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004708 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
4709 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
4710 requested_capacity));
4711 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
4712 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02004713 case 0:
4714 break;
4715 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004716 PSA_ASSERT(psa_import_key(&attributes,
4717 inputs[i]->x, inputs[i]->len,
4718 &keys[i]));
gabor-mezei-armceface22021-01-21 12:26:17 +01004719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004720 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
4721 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes));
4722 TEST_ASSERT(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes)) <=
4723 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
gabor-mezei-armceface22021-01-21 12:26:17 +01004724 }
4725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004726 PSA_ASSERT(psa_key_derivation_input_key(
4727 &operation, steps[i], keys[i]));
Gilles Peskine1468da72019-05-29 17:35:49 +02004728 break;
4729 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004730 PSA_ASSERT(psa_key_derivation_input_bytes(
4731 &operation, steps[i],
4732 inputs[i]->x, inputs[i]->len));
Gilles Peskine1468da72019-05-29 17:35:49 +02004733 break;
4734 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004735 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004737 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4738 &current_capacity));
4739 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004740 expected_capacity = requested_capacity;
4741
4742 /* Expansion phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004743 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004744 /* Read some bytes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004745 status = psa_key_derivation_output_bytes(&operation,
4746 output_buffer, output_sizes[i]);
4747 if (expected_capacity == 0 && output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004748 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004749 TEST_ASSERT(status == PSA_SUCCESS ||
4750 status == PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004751 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004752 } else if (expected_capacity == 0 ||
4753 output_sizes[i] > expected_capacity) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004754 /* Capacity exceeded. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004755 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004756 expected_capacity = 0;
4757 continue;
4758 }
4759 /* Success. Check the read data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004760 PSA_ASSERT(status);
4761 if (output_sizes[i] != 0) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004762 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004763 expected_outputs[i], output_sizes[i]);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004764 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004766 expected_capacity -= output_sizes[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004767 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4768 &current_capacity));
4769 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004770 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004771 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004772
4773exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004774 mbedtls_free(output_buffer);
4775 psa_key_derivation_abort(&operation);
4776 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
4777 psa_destroy_key(keys[i]);
4778 }
4779 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004780}
4781/* END_CASE */
4782
4783/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004784void derive_full(int alg_arg,
4785 data_t *key_data,
4786 data_t *input1,
4787 data_t *input2,
4788 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02004789{
Ronald Cron5425a212020-08-04 14:58:35 +02004790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004791 psa_algorithm_t alg = alg_arg;
4792 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004793 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004794 unsigned char output_buffer[16];
4795 size_t expected_capacity = requested_capacity;
4796 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004799 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02004800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004801 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4802 psa_set_key_algorithm(&attributes, alg);
4803 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02004804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004805 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4806 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02004807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004808 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
4809 input1->x, input1->len,
4810 input2->x, input2->len,
4811 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01004812 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004813 }
Janos Follath47f27ed2019-06-25 13:24:52 +01004814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004815 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4816 &current_capacity));
4817 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02004818
4819 /* Expansion phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820 while (current_capacity > 0) {
4821 size_t read_size = sizeof(output_buffer);
4822 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02004823 read_size = current_capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004824 }
4825 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
4826 output_buffer,
4827 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02004828 expected_capacity -= read_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004829 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4830 &current_capacity));
4831 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02004832 }
4833
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004834 /* Check that the operation refuses to go over capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004835 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
4836 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02004837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004838 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02004839
4840exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004841 psa_key_derivation_abort(&operation);
4842 psa_destroy_key(key);
4843 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02004844}
4845/* END_CASE */
4846
Janos Follathe60c9052019-07-03 13:51:30 +01004847/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004848void derive_key_exercise(int alg_arg,
4849 data_t *key_data,
4850 data_t *input1,
4851 data_t *input2,
4852 int derived_type_arg,
4853 int derived_bits_arg,
4854 int derived_usage_arg,
4855 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02004856{
Ronald Cron5425a212020-08-04 14:58:35 +02004857 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4858 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004859 psa_algorithm_t alg = alg_arg;
4860 psa_key_type_t derived_type = derived_type_arg;
4861 size_t derived_bits = derived_bits_arg;
4862 psa_key_usage_t derived_usage = derived_usage_arg;
4863 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004864 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004866 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004867 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004869 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02004870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004871 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4872 psa_set_key_algorithm(&attributes, alg);
4873 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
4874 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4875 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004876
4877 /* Derive a key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004878 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4879 input1->x, input1->len,
4880 input2->x, input2->len,
4881 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01004882 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004883 }
Janos Follathe60c9052019-07-03 13:51:30 +01004884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004885 psa_set_key_usage_flags(&attributes, derived_usage);
4886 psa_set_key_algorithm(&attributes, derived_alg);
4887 psa_set_key_type(&attributes, derived_type);
4888 psa_set_key_bits(&attributes, derived_bits);
4889 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
4890 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004891
4892 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004893 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
4894 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
4895 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004896
4897 /* Exercise the derived key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004898 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02004899 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004900 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02004901
4902exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004903 /*
4904 * Key attributes may have been returned by psa_get_key_attributes()
4905 * thus reset them as required.
4906 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004907 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004909 psa_key_derivation_abort(&operation);
4910 psa_destroy_key(base_key);
4911 psa_destroy_key(derived_key);
4912 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02004913}
4914/* END_CASE */
4915
Janos Follath42fd8882019-07-03 14:17:09 +01004916/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004917void derive_key_export(int alg_arg,
4918 data_t *key_data,
4919 data_t *input1,
4920 data_t *input2,
4921 int bytes1_arg,
4922 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02004923{
Ronald Cron5425a212020-08-04 14:58:35 +02004924 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4925 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004926 psa_algorithm_t alg = alg_arg;
4927 size_t bytes1 = bytes1_arg;
4928 size_t bytes2 = bytes2_arg;
4929 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004931 uint8_t *output_buffer = NULL;
4932 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004933 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4934 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004935 size_t length;
4936
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004937 TEST_CALLOC(output_buffer, capacity);
4938 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004939 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02004940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004941 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
4942 psa_set_key_algorithm(&base_attributes, alg);
4943 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
4944 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
4945 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004946
4947 /* Derive some material and output it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004948 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4949 input1->x, input1->len,
4950 input2->x, input2->len,
4951 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01004952 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004953 }
Janos Follath42fd8882019-07-03 14:17:09 +01004954
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004955 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
4956 output_buffer,
4957 capacity));
4958 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004959
4960 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004961 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4962 input1->x, input1->len,
4963 input2->x, input2->len,
4964 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01004965 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004966 }
Janos Follath42fd8882019-07-03 14:17:09 +01004967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004968 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
4969 psa_set_key_algorithm(&derived_attributes, 0);
4970 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
4971 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
4972 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
4973 &derived_key));
4974 PSA_ASSERT(psa_export_key(derived_key,
4975 export_buffer, bytes1,
4976 &length));
4977 TEST_EQUAL(length, bytes1);
4978 PSA_ASSERT(psa_destroy_key(derived_key));
4979 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
4980 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
4981 &derived_key));
4982 PSA_ASSERT(psa_export_key(derived_key,
4983 export_buffer + bytes1, bytes2,
4984 &length));
4985 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004986
4987 /* Compare the outputs from the two runs. */
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004988 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004989 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004990
4991exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004992 mbedtls_free(output_buffer);
4993 mbedtls_free(export_buffer);
4994 psa_key_derivation_abort(&operation);
4995 psa_destroy_key(base_key);
4996 psa_destroy_key(derived_key);
4997 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02004998}
4999/* END_CASE */
5000
5001/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005002void derive_key(int alg_arg,
5003 data_t *key_data, data_t *input1, data_t *input2,
5004 int type_arg, int bits_arg,
5005 int expected_status_arg,
5006 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02005007{
Ronald Cron5425a212020-08-04 14:58:35 +02005008 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5009 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005010 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005011 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005012 size_t bits = bits_arg;
5013 psa_status_t expected_status = expected_status_arg;
5014 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5015 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5016 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5017
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005018 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02005019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005020 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
5021 psa_set_key_algorithm(&base_attributes, alg);
5022 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
5023 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
5024 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02005025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005026 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
5027 input1->x, input1->len,
5028 input2->x, input2->len,
5029 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02005030 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005031 }
Gilles Peskinec744d992019-07-30 17:26:54 +02005032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005033 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
5034 psa_set_key_algorithm(&derived_attributes, 0);
5035 psa_set_key_type(&derived_attributes, type);
5036 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01005037
5038 psa_status_t status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005039 psa_key_derivation_output_key(&derived_attributes,
5040 &operation,
5041 &derived_key);
5042 if (is_large_output > 0) {
5043 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
5044 }
5045 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02005046
5047exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005048 psa_key_derivation_abort(&operation);
5049 psa_destroy_key(base_key);
5050 psa_destroy_key(derived_key);
5051 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02005052}
5053/* END_CASE */
5054
5055/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005056void key_agreement_setup(int alg_arg,
5057 int our_key_type_arg, int our_key_alg_arg,
5058 data_t *our_key_data, data_t *peer_key_data,
5059 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005060{
Ronald Cron5425a212020-08-04 14:58:35 +02005061 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005062 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005063 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005064 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005065 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005066 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005067 psa_status_t expected_status = expected_status_arg;
5068 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005070 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02005071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005072 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5073 psa_set_key_algorithm(&attributes, our_key_alg);
5074 psa_set_key_type(&attributes, our_key_type);
5075 PSA_ASSERT(psa_import_key(&attributes,
5076 our_key_data->x, our_key_data->len,
5077 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02005078
Gilles Peskine77f40d82019-04-11 21:27:06 +02005079 /* The tests currently include inputs that should fail at either step.
5080 * Test cases that fail at the setup step should be changed to call
5081 * key_derivation_setup instead, and this function should be renamed
5082 * to key_agreement_fail. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005083 status = psa_key_derivation_setup(&operation, alg);
5084 if (status == PSA_SUCCESS) {
5085 TEST_EQUAL(psa_key_derivation_key_agreement(
5086 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5087 our_key,
5088 peer_key_data->x, peer_key_data->len),
5089 expected_status);
5090 } else {
5091 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02005092 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005093
5094exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005095 psa_key_derivation_abort(&operation);
5096 psa_destroy_key(our_key);
5097 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02005098}
5099/* END_CASE */
5100
5101/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005102void raw_key_agreement(int alg_arg,
5103 int our_key_type_arg, data_t *our_key_data,
5104 data_t *peer_key_data,
5105 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02005106{
Ronald Cron5425a212020-08-04 14:58:35 +02005107 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005108 psa_algorithm_t alg = alg_arg;
5109 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005111 unsigned char *output = NULL;
5112 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005113 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005115 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02005116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005117 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5118 psa_set_key_algorithm(&attributes, alg);
5119 psa_set_key_type(&attributes, our_key_type);
5120 PSA_ASSERT(psa_import_key(&attributes,
5121 our_key_data->x, our_key_data->len,
5122 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02005123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005124 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
5125 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01005126
Gilles Peskine7d150292022-04-13 23:25:52 +02005127 /* Validate size macros */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005128 TEST_LE_U(expected_output->len,
5129 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
5130 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
5131 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine7d150292022-04-13 23:25:52 +02005132
5133 /* Good case with exact output size */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005134 TEST_CALLOC(output, expected_output->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005135 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
5136 peer_key_data->x, peer_key_data->len,
5137 output, expected_output->len,
5138 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005139 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005140 expected_output->x, expected_output->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005141 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005142 output = NULL;
5143 output_length = ~0;
5144
5145 /* Larger buffer */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005146 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005147 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
5148 peer_key_data->x, peer_key_data->len,
5149 output, expected_output->len + 1,
5150 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005151 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005152 expected_output->x, expected_output->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005153 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005154 output = NULL;
5155 output_length = ~0;
5156
5157 /* Buffer too small */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005158 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005159 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
5160 peer_key_data->x, peer_key_data->len,
5161 output, expected_output->len - 1,
5162 &output_length),
5163 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine7d150292022-04-13 23:25:52 +02005164 /* Not required by the spec, but good robustness */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005165 TEST_LE_U(output_length, expected_output->len - 1);
5166 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005167 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005168
5169exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005170 mbedtls_free(output);
5171 psa_destroy_key(our_key);
5172 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02005173}
5174/* END_CASE */
5175
5176/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005177void key_agreement_capacity(int alg_arg,
5178 int our_key_type_arg, data_t *our_key_data,
5179 data_t *peer_key_data,
5180 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02005181{
Ronald Cron5425a212020-08-04 14:58:35 +02005182 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005183 psa_algorithm_t alg = alg_arg;
5184 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005185 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005186 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005187 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005188 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005190 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02005191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005192 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5193 psa_set_key_algorithm(&attributes, alg);
5194 psa_set_key_type(&attributes, our_key_type);
5195 PSA_ASSERT(psa_import_key(&attributes,
5196 our_key_data->x, our_key_data->len,
5197 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02005198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005199 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
5200 PSA_ASSERT(psa_key_derivation_key_agreement(
5201 &operation,
5202 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5203 peer_key_data->x, peer_key_data->len));
5204 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005205 /* The test data is for info="" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005206 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
5207 PSA_KEY_DERIVATION_INPUT_INFO,
5208 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005209 }
Gilles Peskine59685592018-09-18 12:11:34 +02005210
Shaun Case0e7791f2021-12-20 21:14:10 -08005211 /* Test the advertised capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005212 PSA_ASSERT(psa_key_derivation_get_capacity(
5213 &operation, &actual_capacity));
5214 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02005215
Gilles Peskinebf491972018-10-25 22:36:12 +02005216 /* Test the actual capacity by reading the output. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005217 while (actual_capacity > sizeof(output)) {
5218 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5219 output, sizeof(output)));
5220 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02005221 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005222 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5223 output, actual_capacity));
5224 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
5225 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02005226
Gilles Peskine59685592018-09-18 12:11:34 +02005227exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005228 psa_key_derivation_abort(&operation);
5229 psa_destroy_key(our_key);
5230 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02005231}
5232/* END_CASE */
5233
5234/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005235void key_agreement_output(int alg_arg,
5236 int our_key_type_arg, data_t *our_key_data,
5237 data_t *peer_key_data,
5238 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02005239{
Ronald Cron5425a212020-08-04 14:58:35 +02005240 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005241 psa_algorithm_t alg = alg_arg;
5242 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005243 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005245 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005246
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005247 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005248 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02005249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005250 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02005251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005252 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5253 psa_set_key_algorithm(&attributes, alg);
5254 psa_set_key_type(&attributes, our_key_type);
5255 PSA_ASSERT(psa_import_key(&attributes,
5256 our_key_data->x, our_key_data->len,
5257 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02005258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005259 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
5260 PSA_ASSERT(psa_key_derivation_key_agreement(
5261 &operation,
5262 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5263 peer_key_data->x, peer_key_data->len));
5264 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005265 /* The test data is for info="" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005266 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
5267 PSA_KEY_DERIVATION_INPUT_INFO,
5268 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005269 }
Gilles Peskine59685592018-09-18 12:11:34 +02005270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005271 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5272 actual_output,
5273 expected_output1->len));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005274 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005275 expected_output1->x, expected_output1->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005276 if (expected_output2->len != 0) {
5277 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5278 actual_output,
5279 expected_output2->len));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005280 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005281 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005282 }
Gilles Peskine59685592018-09-18 12:11:34 +02005283
5284exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005285 psa_key_derivation_abort(&operation);
5286 psa_destroy_key(our_key);
5287 PSA_DONE();
5288 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02005289}
5290/* END_CASE */
5291
5292/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005293void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02005294{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005295 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005296 unsigned char *output = NULL;
5297 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005298 size_t i;
5299 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005301 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00005302
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005303 TEST_CALLOC(output, bytes);
5304 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02005305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005306 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02005307
Gilles Peskinea50d7392018-06-21 10:22:13 +02005308 /* Run several times, to ensure that every output byte will be
5309 * nonzero at least once with overwhelming probability
5310 * (2^(-8*number_of_runs)). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005311 for (run = 0; run < 10; run++) {
5312 if (bytes != 0) {
5313 memset(output, 0, bytes);
5314 }
5315 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02005316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 for (i = 0; i < bytes; i++) {
5318 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02005319 ++changed[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005320 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005321 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005322 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005323
5324 /* Check that every byte was changed to nonzero at least once. This
5325 * validates that psa_generate_random is overwriting every byte of
5326 * the output buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005327 for (i = 0; i < bytes; i++) {
5328 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02005329 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005330
5331exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005332 PSA_DONE();
5333 mbedtls_free(output);
5334 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02005335}
5336/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005337
5338/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005339void generate_key(int type_arg,
5340 int bits_arg,
5341 int usage_arg,
5342 int alg_arg,
5343 int expected_status_arg,
5344 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005345{
Ronald Cron5425a212020-08-04 14:58:35 +02005346 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005347 psa_key_type_t type = type_arg;
5348 psa_key_usage_t usage = usage_arg;
5349 size_t bits = bits_arg;
5350 psa_algorithm_t alg = alg_arg;
5351 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005353 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005355 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02005356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005357 psa_set_key_usage_flags(&attributes, usage);
5358 psa_set_key_algorithm(&attributes, alg);
5359 psa_set_key_type(&attributes, type);
5360 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005361
5362 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005363 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01005364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005365 if (is_large_key > 0) {
5366 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
5367 }
5368 TEST_EQUAL(status, expected_status);
5369 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005370 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005371 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005372
5373 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005374 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
5375 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
5376 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005377
Gilles Peskine818ca122018-06-20 18:16:48 +02005378 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005379 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02005380 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005381 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005382
5383exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005384 /*
5385 * Key attributes may have been returned by psa_get_key_attributes()
5386 * thus reset them as required.
5387 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005388 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005390 psa_destroy_key(key);
5391 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02005392}
5393/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005394
Ronald Cronee414c72021-03-18 18:50:08 +01005395/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005396void generate_key_rsa(int bits_arg,
5397 data_t *e_arg,
5398 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02005399{
Ronald Cron5425a212020-08-04 14:58:35 +02005400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005401 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005402 size_t bits = bits_arg;
5403 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5404 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5405 psa_status_t expected_status = expected_status_arg;
5406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5407 uint8_t *exported = NULL;
5408 size_t exported_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005409 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005410 size_t exported_length = SIZE_MAX;
5411 uint8_t *e_read_buffer = NULL;
5412 int is_default_public_exponent = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005413 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005414 size_t e_read_length = SIZE_MAX;
5415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005416 if (e_arg->len == 0 ||
5417 (e_arg->len == 3 &&
5418 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005419 is_default_public_exponent = 1;
5420 e_read_size = 0;
5421 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005422 TEST_CALLOC(e_read_buffer, e_read_size);
5423 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005425 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02005426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005427 psa_set_key_usage_flags(&attributes, usage);
5428 psa_set_key_algorithm(&attributes, alg);
5429 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
5430 e_arg->x, e_arg->len));
5431 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005432
5433 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005434 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
5435 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005436 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005437 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005438
5439 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005440 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5441 TEST_EQUAL(psa_get_key_type(&attributes), type);
5442 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
5443 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
5444 e_read_buffer, e_read_size,
5445 &e_read_length));
5446 if (is_default_public_exponent) {
5447 TEST_EQUAL(e_read_length, 0);
5448 } else {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005449 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005451
5452 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005453 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005454 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005455 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005456
5457 /* Export the key and check the public exponent. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005458 PSA_ASSERT(psa_export_public_key(key,
5459 exported, exported_size,
5460 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02005461 {
5462 uint8_t *p = exported;
5463 uint8_t *end = exported + exported_length;
5464 size_t len;
5465 /* RSAPublicKey ::= SEQUENCE {
5466 * modulus INTEGER, -- n
5467 * publicExponent INTEGER } -- e
5468 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005469 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
5470 MBEDTLS_ASN1_SEQUENCE |
5471 MBEDTLS_ASN1_CONSTRUCTED));
5472 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
5473 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
5474 MBEDTLS_ASN1_INTEGER));
5475 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005476 ++p;
5477 --len;
5478 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005479 if (e_arg->len == 0) {
5480 TEST_EQUAL(len, 3);
5481 TEST_EQUAL(p[0], 1);
5482 TEST_EQUAL(p[1], 0);
5483 TEST_EQUAL(p[2], 1);
5484 } else {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005485 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005486 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005487 }
5488
5489exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005490 /*
5491 * Key attributes may have been returned by psa_get_key_attributes() or
5492 * set by psa_set_key_domain_parameters() thus reset them as required.
5493 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005494 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005496 psa_destroy_key(key);
5497 PSA_DONE();
5498 mbedtls_free(e_read_buffer);
5499 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005500}
5501/* END_CASE */
5502
Darryl Greend49a4992018-06-18 17:27:26 +01005503/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504void persistent_key_load_key_from_storage(data_t *data,
5505 int type_arg, int bits_arg,
5506 int usage_flags_arg, int alg_arg,
5507 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01005508{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005509 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5512 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005513 psa_key_type_t type = type_arg;
5514 size_t bits = bits_arg;
5515 psa_key_usage_t usage_flags = usage_flags_arg;
5516 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005518 unsigned char *first_export = NULL;
5519 unsigned char *second_export = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005520 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01005521 size_t first_exported_length;
5522 size_t second_exported_length;
5523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005524 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005525 TEST_CALLOC(first_export, export_size);
5526 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005527 }
Darryl Greend49a4992018-06-18 17:27:26 +01005528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005529 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01005530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005531 psa_set_key_id(&attributes, key_id);
5532 psa_set_key_usage_flags(&attributes, usage_flags);
5533 psa_set_key_algorithm(&attributes, alg);
5534 psa_set_key_type(&attributes, type);
5535 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01005536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005537 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00005538 case IMPORT_KEY:
5539 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005540 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
5541 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00005542 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005543
Darryl Green0c6575a2018-11-07 16:05:30 +00005544 case GENERATE_KEY:
5545 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005546 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00005547 break;
5548
5549 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005550#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005551 {
5552 /* Create base key */
5553 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
5554 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5555 psa_set_key_usage_flags(&base_attributes,
5556 PSA_KEY_USAGE_DERIVE);
5557 psa_set_key_algorithm(&base_attributes, derive_alg);
5558 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
5559 PSA_ASSERT(psa_import_key(&base_attributes,
5560 data->x, data->len,
5561 &base_key));
5562 /* Derive a key. */
5563 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
5564 PSA_ASSERT(psa_key_derivation_input_key(
5565 &operation,
5566 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
5567 PSA_ASSERT(psa_key_derivation_input_bytes(
5568 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
5569 NULL, 0));
5570 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
5571 &operation,
5572 &key));
5573 PSA_ASSERT(psa_key_derivation_abort(&operation));
5574 PSA_ASSERT(psa_destroy_key(base_key));
5575 base_key = MBEDTLS_SVC_KEY_ID_INIT;
5576 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005577#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005578 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005579#endif
5580 break;
5581
5582 default:
Agathiyan Bragadeesh27e29892023-07-14 17:28:27 +01005583 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005584 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005585 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005586 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01005587
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005588 /* Export the key if permitted by the key policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005589 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
5590 PSA_ASSERT(psa_export_key(key,
5591 first_export, export_size,
5592 &first_exported_length));
5593 if (generation_method == IMPORT_KEY) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005594 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005595 first_export, first_exported_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005596 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005597 }
Darryl Greend49a4992018-06-18 17:27:26 +01005598
5599 /* Shutdown and restart */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005600 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005601 PSA_DONE();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005602 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01005603
Darryl Greend49a4992018-06-18 17:27:26 +01005604 /* Check key slot still contains key data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005605 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5606 TEST_ASSERT(mbedtls_svc_key_id_equal(
5607 psa_get_key_id(&attributes), key_id));
5608 TEST_EQUAL(psa_get_key_lifetime(&attributes),
5609 PSA_KEY_LIFETIME_PERSISTENT);
5610 TEST_EQUAL(psa_get_key_type(&attributes), type);
5611 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
5612 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
5613 mbedtls_test_update_key_usage_flags(usage_flags));
5614 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01005615
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005616 /* Export the key again if permitted by the key policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005617 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
5618 PSA_ASSERT(psa_export_key(key,
5619 second_export, export_size,
5620 &second_exported_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005621 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005622 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00005623 }
5624
5625 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005626 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00005627 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005628 }
Darryl Greend49a4992018-06-18 17:27:26 +01005629
5630exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005631 /*
5632 * Key attributes may have been returned by psa_get_key_attributes()
5633 * thus reset them as required.
5634 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005635 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005637 mbedtls_free(first_export);
5638 mbedtls_free(second_export);
5639 psa_key_derivation_abort(&operation);
5640 psa_destroy_key(base_key);
5641 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005642 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005643}
5644/* END_CASE */
David Horstmannb0a01b12023-10-30 20:16:41 +00005645
5646/* BEGIN_CASE */
5647void psa_crypto_copy_input(int src_len, int dst_len, int exp_ret)
5648{
5649 uint8_t data[] = {0x12, 0x34, 0x56, 0x78};
5650 uint8_t *src_buffer = NULL;
5651 uint8_t *dst_buffer = NULL;
5652 psa_status_t ret;
5653
5654 TEST_CALLOC(src_buffer, src_len);
5655 TEST_CALLOC(dst_buffer, dst_len);
5656
5657 for (int i = 0; i < src_len; i++) {
5658 src_buffer[i] = data[i % sizeof(data)];
5659 }
5660
5661 ret = psa_crypto_copy_input(src_buffer, src_len, dst_buffer, dst_len);
5662 TEST_EQUAL((int) ret, exp_ret);
5663
5664 if (exp_ret == (int) PSA_SUCCESS) {
5665 /* Note: We compare the first src_len bytes of each buffer, as this is what was copied. */
5666 TEST_MEMORY_COMPARE(src_buffer, src_len, dst_buffer, src_len);
5667 }
5668
5669exit:
5670 mbedtls_free(src_buffer);
5671 mbedtls_free(dst_buffer);
5672}
5673/* END_CASE */