blob: 796a62a5f143fe8eb60e7bb4f99c94cce4367c40 [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#include "psa_crypto_core.h"
17
Gilles Peskine8e94efe2021-02-13 00:25:53 +010018#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010019#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010020#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010021
Gilles Peskinef6279312021-05-27 13:21:20 +020022/* If this comes up, it's a bug in the test code or in the test data. */
23#define UNUSED 0xdeadbeef
24
Dave Rodgman34b147d2021-06-23 12:49:59 +010025/* Assert that an operation is (not) active.
26 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010027#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
28#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Dave Rodgman34b147d2021-06-23 12:49:59 +010029
Jaeden Amerof24c7f82018-06-27 17:20:43 +010030/** An invalid export length that will never be set by psa_export_key(). */
31static const size_t INVALID_EXPORT_LENGTH = ~0U;
32
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033/** Test if a buffer contains a constant byte value.
34 *
35 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020036 *
37 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 * \param size Size of the buffer in bytes.
40 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 * \return 1 if the buffer is all-bits-zero.
42 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045{
46 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047 for (i = 0; i < size; i++) {
48 if (((unsigned char *) buffer)[i] != c) {
49 return 0;
50 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020051 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020053}
Andrzej Kureke0015962022-01-17 15:29:38 +010054#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056static int asn1_write_10x(unsigned char **p,
57 unsigned char *start,
58 size_t bits,
59 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060{
61 int ret;
62 int len = bits / 8 + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063 if (bits == 0) {
64 return MBEDTLS_ERR_ASN1_INVALID_DATA;
65 }
66 if (bits <= 8 && x >= 1 << (bits - 1)) {
67 return MBEDTLS_ERR_ASN1_INVALID_DATA;
68 }
69 if (*p < start || *p - start < (ptrdiff_t) len) {
70 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
71 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +020072 *p -= len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 (*p)[len-1] = x;
74 if (bits % 8 == 0) {
75 (*p)[1] |= 1;
76 } else {
77 (*p)[0] |= 1 << (bits % 8);
78 }
79 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
80 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
81 MBEDTLS_ASN1_INTEGER));
82 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +020083}
84
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085static int construct_fake_rsa_key(unsigned char *buffer,
86 size_t buffer_size,
87 unsigned char **p,
88 size_t bits,
89 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020090{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +020092 int ret;
93 int len = 0;
94 /* Construct something that looks like a DER encoding of
95 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
96 * RSAPrivateKey ::= SEQUENCE {
97 * version Version,
98 * modulus INTEGER, -- n
99 * publicExponent INTEGER, -- e
100 * privateExponent INTEGER, -- d
101 * prime1 INTEGER, -- p
102 * prime2 INTEGER, -- q
103 * exponent1 INTEGER, -- d mod (p-1)
104 * exponent2 INTEGER, -- d mod (q-1)
105 * coefficient INTEGER, -- (inverse of q) mod p
106 * otherPrimeInfos OtherPrimeInfos OPTIONAL
107 * }
108 * Or, for a public key, the same structure with only
109 * version, modulus and publicExponent.
110 */
111 *p = buffer + buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if (keypair) {
113 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
114 asn1_write_10x(p, buffer, half_bits, 1));
115 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
116 asn1_write_10x(p, buffer, half_bits, 1));
117 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
118 asn1_write_10x(p, buffer, half_bits, 1));
119 MBEDTLS_ASN1_CHK_ADD(len, /* q */
120 asn1_write_10x(p, buffer, half_bits, 1));
121 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x(p, buffer, half_bits, 3));
123 MBEDTLS_ASN1_CHK_ADD(len, /* d */
124 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200125 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
127 asn1_write_10x(p, buffer, 17, 1));
128 MBEDTLS_ASN1_CHK_ADD(len, /* n */
129 asn1_write_10x(p, buffer, bits, 1));
130 if (keypair) {
131 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
132 mbedtls_asn1_write_int(p, buffer, 0));
133 }
134 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200135 {
136 const unsigned char tag =
137 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200139 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200141}
Andrzej Kureke0015962022-01-17 15:29:38 +0100142#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144int exercise_mac_setup(psa_key_type_t key_type,
145 const unsigned char *key_bytes,
146 size_t key_length,
147 psa_algorithm_t alg,
148 psa_mac_operation_t *operation,
149 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100150{
Ronald Cron5425a212020-08-04 14:58:35 +0200151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
155 psa_set_key_algorithm(&attributes, alg);
156 psa_set_key_type(&attributes, key_type);
157 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100160 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100162 /* If setup failed, reproduce the failure, so that the caller can
163 * test the resulting state of the operation object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 if (*status != PSA_SUCCESS) {
165 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 psa_destroy_key(key);
169 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
171exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 psa_destroy_key(key);
173 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100174}
175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176int exercise_cipher_setup(psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
187 psa_set_key_algorithm(&attributes, alg);
188 psa_set_key_type(&attributes, key_type);
189 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 if (*status != PSA_SUCCESS) {
197 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
198 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100199 }
200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 psa_destroy_key(key);
202 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203
204exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 psa_destroy_key(key);
206 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207}
208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200210{
211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200213 uint8_t buffer[1];
214 size_t length;
215 int ok = 0;
216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 psa_set_key_id(&attributes, key_id);
218 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
219 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
220 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
221 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
222 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200223 TEST_EQUAL(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200225 TEST_EQUAL(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
227 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
228 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
229 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
230 TEST_EQUAL(psa_get_key_type(&attributes), 0);
231 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
234 PSA_ERROR_INVALID_HANDLE);
235 TEST_EQUAL(psa_export_public_key(key,
236 buffer, sizeof(buffer), &length),
237 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200238
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239 ok = 1;
240
241exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100242 /*
243 * Key attributes may have been returned by psa_get_key_attributes()
244 * thus reset them as required.
245 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249}
250
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200251/* Assert that a key isn't reported as having a slot number. */
252#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200254 do \
255 { \
256 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 TEST_EQUAL(psa_get_key_slot_number( \
258 attributes, \
259 &ASSERT_NO_SLOT_NUMBER_slot_number), \
260 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200261 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200263#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264#define ASSERT_NO_SLOT_NUMBER(attributes) \
265 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200266#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
267
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100268/* An overapproximation of the amount of storage needed for a key of the
269 * given type and with the given content. The API doesn't make it easy
270 * to find a good value for the size. The current implementation doesn't
271 * care about the value anyway. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272#define KEY_BITS_FROM_DATA(type, data) \
273 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100274
Darryl Green0c6575a2018-11-07 16:05:30 +0000275typedef enum {
276 IMPORT_KEY = 0,
277 GENERATE_KEY = 1,
278 DERIVE_KEY = 2
279} generate_method;
280
Gilles Peskinee59236f2018-01-27 23:32:46 +0100281/* END_HEADER */
282
283/* BEGIN_DEPENDENCIES
284 * depends_on:MBEDTLS_PSA_CRYPTO_C
285 * END_DEPENDENCIES
286 */
287
288/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200290{
291 size_t max_truncated_mac_size =
292 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
293
294 /* Check that the length for a truncated MAC always fits in the algorithm
295 * encoding. The shifted mask is the maximum truncated value. The
296 * untruncated algorithm may be one byte larger. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskine841b14b2019-11-26 17:37:37 +0100298
299#if defined(MBEDTLS_TEST_DEPRECATED)
300 /* Check deprecated constants. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 TEST_EQUAL(PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR);
302 TEST_EQUAL(PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS);
303 TEST_EQUAL(PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST);
304 TEST_EQUAL(PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA);
305 TEST_EQUAL(PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED);
306 TEST_EQUAL(PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH);
307 TEST_EQUAL(PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH);
308 TEST_EQUAL(PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskineb87b7192019-12-04 16:24:10 +0100309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 TEST_EQUAL(PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1);
311 TEST_EQUAL(PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1);
312 TEST_EQUAL(PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1);
313 TEST_EQUAL(PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1);
314 TEST_EQUAL(PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1);
315 TEST_EQUAL(PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1);
316 TEST_EQUAL(PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1);
317 TEST_EQUAL(PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1);
318 TEST_EQUAL(PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1);
319 TEST_EQUAL(PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1);
320 TEST_EQUAL(PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2);
321 TEST_EQUAL(PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1);
322 TEST_EQUAL(PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1);
323 TEST_EQUAL(PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1);
324 TEST_EQUAL(PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1);
325 TEST_EQUAL(PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1);
326 TEST_EQUAL(PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1);
327 TEST_EQUAL(PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1);
328 TEST_EQUAL(PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1);
329 TEST_EQUAL(PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1);
330 TEST_EQUAL(PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1);
331 TEST_EQUAL(PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1);
332 TEST_EQUAL(PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1);
333 TEST_EQUAL(PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2);
334 TEST_EQUAL(PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2);
335 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
336 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
337 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
338 TEST_EQUAL(PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY);
339 TEST_EQUAL(PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY);
Paul Elliott8ff510a2020-06-02 17:19:28 +0100340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 TEST_EQUAL(PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1);
342 TEST_EQUAL(PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1);
343 TEST_EQUAL(PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2);
344 TEST_EQUAL(PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1);
345 TEST_EQUAL(PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1);
346 TEST_EQUAL(PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2);
347 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
348 TEST_EQUAL(PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY);
Gilles Peskineb87b7192019-12-04 16:24:10 +0100349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 TEST_EQUAL(PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919);
351 TEST_EQUAL(PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919);
352 TEST_EQUAL(PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919);
353 TEST_EQUAL(PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919);
354 TEST_EQUAL(PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919);
Paul Elliott75e27032020-06-03 15:17:39 +0100355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 TEST_EQUAL(PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919);
357 TEST_EQUAL(PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM);
Gilles Peskineb87b7192019-12-04 16:24:10 +0100358#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200359}
360/* END_CASE */
361
362/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363void import_with_policy(int type_arg,
364 int usage_arg, int alg_arg,
365 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +0200366{
367 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
368 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200369 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200370 psa_key_type_t type = type_arg;
371 psa_key_usage_t usage = usage_arg;
372 psa_algorithm_t alg = alg_arg;
373 psa_status_t expected_status = expected_status_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +0200375 psa_status_t status;
376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +0200378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 psa_set_key_type(&attributes, type);
380 psa_set_key_usage_flags(&attributes, usage);
381 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 status = psa_import_key(&attributes,
384 key_material, sizeof(key_material),
385 &key);
386 TEST_EQUAL(status, expected_status);
387 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +0200388 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 }
Gilles Peskine6edfa292019-07-31 15:53:45 +0200390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
392 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
393 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
394 mbedtls_test_update_key_usage_flags(usage));
395 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
396 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 PSA_ASSERT(psa_destroy_key(key));
399 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200400
401exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100402 /*
403 * Key attributes may have been returned by psa_get_key_attributes()
404 * thus reset them as required.
405 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 psa_destroy_key(key);
409 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +0200410}
411/* END_CASE */
412
413/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414void import_with_data(data_t *data, int type_arg,
415 int attr_bits_arg,
416 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200417{
418 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
419 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200421 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200422 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200423 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100424 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 psa_set_key_type(&attributes, type);
429 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 status = psa_import_key(&attributes, data->x, data->len, &key);
432 TEST_EQUAL(status, expected_status);
433 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200434 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
438 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
439 if (attr_bits != 0) {
440 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
441 }
442 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 PSA_ASSERT(psa_destroy_key(key));
445 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100446
447exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100448 /*
449 * Key attributes may have been returned by psa_get_key_attributes()
450 * thus reset them as required.
451 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 psa_destroy_key(key);
455 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100456}
457/* END_CASE */
458
459/* BEGIN_CASE */
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100460/* Construct and attempt to import a large unstructured key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461void import_large_key(int type_arg, int byte_size_arg,
462 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +0200463{
464 psa_key_type_t type = type_arg;
465 size_t byte_size = byte_size_arg;
466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
467 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200468 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200469 psa_status_t status;
470 uint8_t *buffer = NULL;
471 size_t buffer_size = byte_size + 1;
472 size_t n;
473
Steven Cooreman69967ce2021-01-18 18:01:08 +0100474 /* Skip the test case if the target running the test cannot
Shaun Case0e7791f2021-12-20 21:14:10 -0800475 * accommodate large keys due to heap size constraints */
Tom Cosgrove20e27de2023-09-04 11:09:08 +0100476 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +0200478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100479 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +0200480
481 /* Try importing the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
483 psa_set_key_type(&attributes, type);
484 status = psa_import_key(&attributes, buffer, byte_size, &key);
485 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
486 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +0200487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 if (status == PSA_SUCCESS) {
489 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
490 TEST_EQUAL(psa_get_key_type(&attributes), type);
491 TEST_EQUAL(psa_get_key_bits(&attributes),
492 PSA_BYTES_TO_BITS(byte_size));
493 ASSERT_NO_SLOT_NUMBER(&attributes);
494 memset(buffer, 0, byte_size + 1);
495 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
496 for (n = 0; n < byte_size; n++) {
497 TEST_EQUAL(buffer[n], 'K');
498 }
499 for (n = byte_size; n < buffer_size; n++) {
500 TEST_EQUAL(buffer[n], 0);
501 }
Gilles Peskinec744d992019-07-30 17:26:54 +0200502 }
503
504exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100505 /*
506 * Key attributes may have been returned by psa_get_key_attributes()
507 * thus reset them as required.
508 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 psa_destroy_key(key);
512 PSA_DONE();
513 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +0200514}
515/* END_CASE */
516
Andrzej Kureke0015962022-01-17 15:29:38 +0100517/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100518/* Import an RSA key with a valid structure (but not valid numbers
519 * inside, beyond having sensible size and parity). This is expected to
520 * fail for large keys. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200522{
Ronald Cron5425a212020-08-04 14:58:35 +0200523 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200524 size_t bits = bits_arg;
525 psa_status_t expected_status = expected_status_arg;
526 psa_status_t status;
527 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200528 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200529 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200531 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200532 unsigned char *p;
533 int ret;
534 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100537 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100538 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
541 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200542 length = ret;
543
544 /* Try importing the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 psa_set_key_type(&attributes, type);
546 status = psa_import_key(&attributes, p, length, &key);
547 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +0200548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549 if (status == PSA_SUCCESS) {
550 PSA_ASSERT(psa_destroy_key(key));
551 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200552
553exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 mbedtls_free(buffer);
555 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200556}
557/* END_CASE */
558
559/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560void import_export(data_t *data,
561 int type_arg,
562 int usage_arg, int alg_arg,
563 int expected_bits,
564 int export_size_delta,
565 int expected_export_status_arg,
566 /*whether reexport must give the original input exactly*/
567 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568{
Ronald Cron5425a212020-08-04 14:58:35 +0200569 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200571 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200572 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100574 unsigned char *exported = NULL;
575 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100576 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100577 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200580 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100581
Moran Pekercb088e72018-07-17 17:36:59 +0300582 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100583 TEST_CALLOC(exported, export_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 if (!canonical_input) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100585 TEST_CALLOC(reexported, export_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 }
587 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589 psa_set_key_usage_flags(&attributes, usage_arg);
590 psa_set_key_algorithm(&attributes, alg);
591 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -0700592
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100594 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595
596 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100597 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
598 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
599 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
600 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601
602 /* Export the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603 status = psa_export_key(key, exported, export_size, &exported_length);
604 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100605
606 /* The exported length must be set by psa_export_key() to a value between 0
607 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100608 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
609 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
610 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100612 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
613 export_size - exported_length));
614 if (status != PSA_SUCCESS) {
615 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100616 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200617 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618
Gilles Peskineea38a922021-02-13 00:05:16 +0100619 /* Run sanity checks on the exported key. For non-canonical inputs,
620 * this validates the canonical representations. For canonical inputs,
621 * this doesn't directly validate the implementation, but it still helps
622 * by cross-validating the test data with the sanity check code. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100623 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Gilles Peskine8f609232018-08-11 01:24:55 +0200624 goto exit;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100626
627 if (canonical_input) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100628 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 } else {
630 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
631 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
632 &key2));
633 PSA_ASSERT(psa_export_key(key2,
634 reexported,
635 export_size,
636 &reexported_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100637 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +0100638 reexported, reexported_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 PSA_ASSERT(psa_destroy_key(key2));
640 }
641 TEST_ASSERT(exported_length <=
642 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
643 psa_get_key_bits(&got_attributes)));
644 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645
646destroy:
647 /* Destroy the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100648 PSA_ASSERT(psa_destroy_key(key));
649 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650
651exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100652 /*
653 * Key attributes may have been returned by psa_get_key_attributes()
654 * thus reset them as required.
655 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100658 mbedtls_free(exported);
659 mbedtls_free(reexported);
660 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661}
662/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100663
Moran Pekerf709f4a2018-06-06 17:26:04 +0300664/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665void import_export_public_key(data_t *data,
666 int type_arg, // key pair or public key
667 int alg_arg,
668 int export_size_delta,
669 int expected_export_status_arg,
670 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +0300671{
Ronald Cron5425a212020-08-04 14:58:35 +0200672 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300673 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200674 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200675 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300676 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300677 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100678 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100679 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100682 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +0300683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
685 psa_set_key_algorithm(&attributes, alg);
686 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +0300687
688 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +0300690
Gilles Peskine49c25912018-10-29 15:15:31 +0100691 /* Export the public key */
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100692 TEST_CALLOC(exported, export_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 status = psa_export_public_key(key,
694 exported, export_size,
695 &exported_length);
696 TEST_EQUAL(status, expected_export_status);
697 if (status == PSA_SUCCESS) {
698 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100699 size_t bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100700 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
701 bits = psa_get_key_bits(&attributes);
702 TEST_LE_U(expected_public_key->len,
703 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
704 TEST_LE_U(expected_public_key->len,
705 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
706 TEST_LE_U(expected_public_key->len,
707 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100708 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +0100709 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100710 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300711
712exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100713 /*
714 * Key attributes may have been returned by psa_get_key_attributes()
715 * thus reset them as required.
716 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100719 mbedtls_free(exported);
720 psa_destroy_key(key);
721 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +0300722}
723/* END_CASE */
724
Gilles Peskine20035e32018-02-03 22:44:14 +0100725/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726void import_and_exercise_key(data_t *data,
727 int type_arg,
728 int bits_arg,
729 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200730{
Ronald Cron5425a212020-08-04 14:58:35 +0200731 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732 psa_key_type_t type = type_arg;
733 size_t bits = bits_arg;
734 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +0200736 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200737 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100739 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 psa_set_key_usage_flags(&attributes, usage);
742 psa_set_key_algorithm(&attributes, alg);
743 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200744
745 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200747
748 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
750 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
751 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200752
753 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +0200755 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 PSA_ASSERT(psa_destroy_key(key));
759 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200760
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200761exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100762 /*
763 * Key attributes may have been returned by psa_get_key_attributes()
764 * thus reset them as required.
765 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100766 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 psa_reset_key_attributes(&attributes);
769 psa_destroy_key(key);
770 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200771}
772/* END_CASE */
773
774/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100775void effective_key_attributes(int type_arg, int expected_type_arg,
776 int bits_arg, int expected_bits_arg,
777 int usage_arg, int expected_usage_arg,
778 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +0200779{
Ronald Cron5425a212020-08-04 14:58:35 +0200780 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100781 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100782 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100783 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100784 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200785 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100786 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200787 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100788 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +0200792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100793 psa_set_key_usage_flags(&attributes, usage);
794 psa_set_key_algorithm(&attributes, alg);
795 psa_set_key_type(&attributes, key_type);
796 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +0200797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 PSA_ASSERT(psa_generate_key(&attributes, &key));
799 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +0200800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100801 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
802 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
803 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
804 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
805 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +0200806
807exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100808 /*
809 * Key attributes may have been returned by psa_get_key_attributes()
810 * thus reset them as required.
811 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100812 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100814 psa_destroy_key(key);
815 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +0200816}
817/* END_CASE */
818
819/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100820void check_key_policy(int type_arg, int bits_arg,
821 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +0100822{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100823 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
824 usage_arg,
825 mbedtls_test_update_key_usage_flags(usage_arg),
826 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +0100827 goto exit;
828}
829/* END_CASE */
830
831/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +0000833{
834 /* Test each valid way of initializing the object, except for `= {0}`, as
835 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
836 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -0800837 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100838 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200839 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
840 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100842 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +0000843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100844 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
845 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
846 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 TEST_EQUAL(psa_get_key_type(&func), 0);
849 TEST_EQUAL(psa_get_key_type(&init), 0);
850 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100852 TEST_EQUAL(psa_get_key_bits(&func), 0);
853 TEST_EQUAL(psa_get_key_bits(&init), 0);
854 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
857 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
858 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100860 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
861 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
862 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +0000863}
864/* END_CASE */
865
866/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100867void mac_key_policy(int policy_usage_arg,
868 int policy_alg_arg,
869 int key_type_arg,
870 data_t *key_data,
871 int exercise_alg_arg,
872 int expected_status_sign_arg,
873 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +0200874{
Ronald Cron5425a212020-08-04 14:58:35 +0200875 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000877 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200878 psa_key_type_t key_type = key_type_arg;
879 psa_algorithm_t policy_alg = policy_alg_arg;
880 psa_algorithm_t exercise_alg = exercise_alg_arg;
881 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200882 psa_status_t status;
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200883 psa_status_t expected_status_sign = expected_status_sign_arg;
884 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200885 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100887 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +0200888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100889 psa_set_key_usage_flags(&attributes, policy_usage);
890 psa_set_key_algorithm(&attributes, policy_alg);
891 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +0200892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100893 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
894 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +0200895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
897 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100899 status = psa_mac_sign_setup(&operation, key, exercise_alg);
900 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100901
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200902 /* Calculate the MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100903 uint8_t input[128] = { 0 };
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200904 size_t mac_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100905 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
906 input, 128,
907 mac, PSA_MAC_MAX_SIZE, &mac_len),
908 expected_status_sign);
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200909
910 /* Verify correct MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100911 status = psa_mac_verify(key, exercise_alg, input, 128,
912 mac, mac_len);
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100914 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
915 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
916 } else {
917 TEST_EQUAL(status, expected_status_verify);
918 }
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +0200921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100922 memset(mac, 0, sizeof(mac));
923 status = psa_mac_verify_setup(&operation, key, exercise_alg);
924 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200925
926exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100927 psa_mac_abort(&operation);
928 psa_destroy_key(key);
929 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200930}
931/* END_CASE */
932
933/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100934void cipher_key_policy(int policy_usage_arg,
935 int policy_alg,
936 int key_type,
937 data_t *key_data,
938 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200939{
Ronald Cron5425a212020-08-04 14:58:35 +0200940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000942 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200943 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200944 psa_status_t status;
945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100946 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100948 psa_set_key_usage_flags(&attributes, policy_usage);
949 psa_set_key_algorithm(&attributes, policy_alg);
950 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100952 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
953 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200954
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200955 /* Check if no key usage flag implication is done */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 TEST_EQUAL(policy_usage,
957 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100959 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
960 if (policy_alg == exercise_alg &&
961 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
962 PSA_ASSERT(status);
963 } else {
964 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
965 }
966 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100968 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
969 if (policy_alg == exercise_alg &&
970 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
971 PSA_ASSERT(status);
972 } else {
973 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
974 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200975
976exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100977 psa_cipher_abort(&operation);
978 psa_destroy_key(key);
979 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200980}
981/* END_CASE */
982
983/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100984void aead_key_policy(int policy_usage_arg,
985 int policy_alg,
986 int key_type,
987 data_t *key_data,
988 int nonce_length_arg,
989 int tag_length_arg,
990 int exercise_alg,
991 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200992{
Ronald Cron5425a212020-08-04 14:58:35 +0200993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200995 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200996 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100997 psa_status_t expected_status = expected_status_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100998 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200999 size_t nonce_length = nonce_length_arg;
1000 unsigned char tag[16];
1001 size_t tag_length = tag_length_arg;
1002 size_t output_length;
1003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001004 TEST_LE_U(nonce_length, sizeof(nonce));
1005 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001007 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001008
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001009 psa_set_key_usage_flags(&attributes, policy_usage);
1010 psa_set_key_algorithm(&attributes, policy_alg);
1011 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001013 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1014 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001015
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001016 /* Check if no key usage implication is done */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001017 TEST_EQUAL(policy_usage,
1018 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001020 status = psa_aead_encrypt(key, exercise_alg,
1021 nonce, nonce_length,
1022 NULL, 0,
1023 NULL, 0,
1024 tag, tag_length,
1025 &output_length);
1026 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1027 TEST_EQUAL(status, expected_status);
1028 } else {
1029 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1030 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001032 memset(tag, 0, sizeof(tag));
1033 status = psa_aead_decrypt(key, exercise_alg,
1034 nonce, nonce_length,
1035 NULL, 0,
1036 tag, tag_length,
1037 NULL, 0,
1038 &output_length);
1039 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
1040 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1041 } else if (expected_status == PSA_SUCCESS) {
1042 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1043 } else {
1044 TEST_EQUAL(status, expected_status);
1045 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001046
1047exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001048 psa_destroy_key(key);
1049 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001050}
1051/* END_CASE */
1052
1053/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001054void asymmetric_encryption_key_policy(int policy_usage_arg,
1055 int policy_alg,
1056 int key_type,
1057 data_t *key_data,
1058 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001059{
Ronald Cron5425a212020-08-04 14:58:35 +02001060 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001061 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001062 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001063 psa_status_t status;
1064 size_t key_bits;
1065 size_t buffer_length;
1066 unsigned char *buffer = NULL;
1067 size_t output_length;
1068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001069 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071 psa_set_key_usage_flags(&attributes, policy_usage);
1072 psa_set_key_algorithm(&attributes, policy_alg);
1073 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1076 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001078 /* Check if no key usage implication is done */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001079 TEST_EQUAL(policy_usage,
1080 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001082 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1083 key_bits = psa_get_key_bits(&attributes);
1084 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
1085 exercise_alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01001086 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001088 status = psa_asymmetric_encrypt(key, exercise_alg,
1089 NULL, 0,
1090 NULL, 0,
1091 buffer, buffer_length,
1092 &output_length);
1093 if (policy_alg == exercise_alg &&
1094 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1095 PSA_ASSERT(status);
1096 } else {
1097 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1098 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001100 if (buffer_length != 0) {
1101 memset(buffer, 0, buffer_length);
1102 }
1103 status = psa_asymmetric_decrypt(key, exercise_alg,
1104 buffer, buffer_length,
1105 NULL, 0,
1106 buffer, buffer_length,
1107 &output_length);
1108 if (policy_alg == exercise_alg &&
1109 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1110 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
1111 } else {
1112 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1113 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001114
1115exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001116 /*
1117 * Key attributes may have been returned by psa_get_key_attributes()
1118 * thus reset them as required.
1119 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001120 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001122 psa_destroy_key(key);
1123 PSA_DONE();
1124 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001125}
1126/* END_CASE */
1127
1128/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129void asymmetric_signature_key_policy(int policy_usage_arg,
1130 int policy_alg,
1131 int key_type,
1132 data_t *key_data,
1133 int exercise_alg,
1134 int payload_length_arg,
1135 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001136{
Ronald Cron5425a212020-08-04 14:58:35 +02001137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001138 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001139 psa_key_usage_t policy_usage = policy_usage_arg;
1140 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001143 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1144 * compatible with the policy and `payload_length_arg` is supposed to be
1145 * a valid input length to sign. If `payload_length_arg <= 0`,
1146 * `exercise_alg` is supposed to be forbidden by the policy. */
1147 int compatible_alg = payload_length_arg > 0;
1148 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001149 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001150 size_t signature_length;
1151
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001152 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001153 in the expected usage flags. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001154 TEST_EQUAL(expected_usage,
1155 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001157 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001159 psa_set_key_usage_flags(&attributes, policy_usage);
1160 psa_set_key_algorithm(&attributes, policy_alg);
1161 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001163 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1164 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001168 status = psa_sign_hash(key, exercise_alg,
1169 payload, payload_length,
1170 signature, sizeof(signature),
1171 &signature_length);
1172 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
1173 PSA_ASSERT(status);
1174 } else {
1175 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1176 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 memset(signature, 0, sizeof(signature));
1179 status = psa_verify_hash(key, exercise_alg,
1180 payload, payload_length,
1181 signature, sizeof(signature));
1182 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
1183 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1184 } else {
1185 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1186 }
Gilles Peskined5b33222018-06-18 22:20:03 +02001187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001188 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
1189 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
1190 status = psa_sign_message(key, exercise_alg,
1191 payload, payload_length,
1192 signature, sizeof(signature),
1193 &signature_length);
1194 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
1195 PSA_ASSERT(status);
1196 } else {
1197 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1198 }
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001200 memset(signature, 0, sizeof(signature));
1201 status = psa_verify_message(key, exercise_alg,
1202 payload, payload_length,
1203 signature, sizeof(signature));
1204 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
1205 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1206 } else {
1207 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1208 }
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001209 }
1210
Gilles Peskined5b33222018-06-18 22:20:03 +02001211exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001212 psa_destroy_key(key);
1213 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001214}
1215/* END_CASE */
1216
Janos Follathba3fab92019-06-11 14:50:16 +01001217/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001218void derive_key_policy(int policy_usage,
1219 int policy_alg,
1220 int key_type,
1221 data_t *key_data,
1222 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02001223{
Ronald Cron5425a212020-08-04 14:58:35 +02001224 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001226 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001227 psa_status_t status;
1228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001229 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02001230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001231 psa_set_key_usage_flags(&attributes, policy_usage);
1232 psa_set_key_algorithm(&attributes, policy_alg);
1233 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02001234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001235 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1236 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02001237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001238 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01001239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001240 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
1241 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
1242 PSA_ASSERT(psa_key_derivation_input_bytes(
1243 &operation,
1244 PSA_KEY_DERIVATION_INPUT_SEED,
1245 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01001246 }
Janos Follathba3fab92019-06-11 14:50:16 +01001247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001248 status = psa_key_derivation_input_key(&operation,
1249 PSA_KEY_DERIVATION_INPUT_SECRET,
1250 key);
Janos Follathba3fab92019-06-11 14:50:16 +01001251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001252 if (policy_alg == exercise_alg &&
1253 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
1254 PSA_ASSERT(status);
1255 } else {
1256 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1257 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02001258
1259exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001260 psa_key_derivation_abort(&operation);
1261 psa_destroy_key(key);
1262 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02001263}
1264/* END_CASE */
1265
1266/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267void agreement_key_policy(int policy_usage,
1268 int policy_alg,
1269 int key_type_arg,
1270 data_t *key_data,
1271 int exercise_alg,
1272 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02001273{
Ronald Cron5425a212020-08-04 14:58:35 +02001274 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001276 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001277 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001278 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001279 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02001282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001283 psa_set_key_usage_flags(&attributes, policy_usage);
1284 psa_set_key_algorithm(&attributes, policy_alg);
1285 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02001286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001287 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1288 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02001289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001290 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
1291 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02001292
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001293 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02001294
1295exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001296 psa_key_derivation_abort(&operation);
1297 psa_destroy_key(key);
1298 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02001299}
1300/* END_CASE */
1301
1302/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001303void key_policy_alg2(int key_type_arg, data_t *key_data,
1304 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001305{
Ronald Cron5425a212020-08-04 14:58:35 +02001306 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001307 psa_key_type_t key_type = key_type_arg;
1308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1309 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1310 psa_key_usage_t usage = usage_arg;
1311 psa_algorithm_t alg = alg_arg;
1312 psa_algorithm_t alg2 = alg2_arg;
1313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001316 psa_set_key_usage_flags(&attributes, usage);
1317 psa_set_key_algorithm(&attributes, alg);
1318 psa_set_key_enrollment_algorithm(&attributes, alg2);
1319 psa_set_key_type(&attributes, key_type);
1320 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1321 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001322
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001323 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001324 usage = mbedtls_test_update_key_usage_flags(usage);
1325 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1326 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
1327 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1328 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001330 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001331 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001332 }
1333 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001334 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001335 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001336
1337exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001338 /*
1339 * Key attributes may have been returned by psa_get_key_attributes()
1340 * thus reset them as required.
1341 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001342 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001344 psa_destroy_key(key);
1345 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001346}
1347/* END_CASE */
1348
1349/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001350void raw_agreement_key_policy(int policy_usage,
1351 int policy_alg,
1352 int key_type_arg,
1353 data_t *key_data,
1354 int exercise_alg,
1355 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001356{
Ronald Cron5425a212020-08-04 14:58:35 +02001357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001358 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001359 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001360 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001361 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001362 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001364 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001366 psa_set_key_usage_flags(&attributes, policy_usage);
1367 psa_set_key_algorithm(&attributes, policy_alg);
1368 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001370 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1371 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001375 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001376
1377exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001378 psa_key_derivation_abort(&operation);
1379 psa_destroy_key(key);
1380 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001381}
1382/* END_CASE */
1383
1384/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001385void copy_success(int source_usage_arg,
1386 int source_alg_arg, int source_alg2_arg,
1387 int type_arg, data_t *material,
1388 int copy_attributes,
1389 int target_usage_arg,
1390 int target_alg_arg, int target_alg2_arg,
1391 int expected_usage_arg,
1392 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01001393{
Gilles Peskineca25db92019-04-19 11:43:08 +02001394 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1395 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001396 psa_key_usage_t expected_usage = expected_usage_arg;
1397 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001398 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001399 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1400 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001401 uint8_t *export_buffer = NULL;
1402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001403 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01001404
Gilles Peskineca25db92019-04-19 11:43:08 +02001405 /* Prepare the source key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001406 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
1407 psa_set_key_algorithm(&source_attributes, source_alg_arg);
1408 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
1409 psa_set_key_type(&source_attributes, type_arg);
1410 PSA_ASSERT(psa_import_key(&source_attributes,
1411 material->x, material->len,
1412 &source_key));
1413 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001414
Gilles Peskineca25db92019-04-19 11:43:08 +02001415 /* Prepare the target attributes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001416 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02001417 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001418 /* Set volatile lifetime to reset the key identifier to 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001419 psa_set_key_lifetime(&target_attributes, PSA_KEY_LIFETIME_VOLATILE);
Ronald Cron65f38a32020-10-23 17:11:13 +02001420 }
1421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001422 if (target_usage_arg != -1) {
1423 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
1424 }
1425 if (target_alg_arg != -1) {
1426 psa_set_key_algorithm(&target_attributes, target_alg_arg);
1427 }
1428 if (target_alg2_arg != -1) {
1429 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
1430 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001431
1432 /* Copy the key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 PSA_ASSERT(psa_copy_key(source_key,
1434 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001435
1436 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001438
1439 /* Test that the target slot has the expected content and policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001440 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
1441 TEST_EQUAL(psa_get_key_type(&source_attributes),
1442 psa_get_key_type(&target_attributes));
1443 TEST_EQUAL(psa_get_key_bits(&source_attributes),
1444 psa_get_key_bits(&target_attributes));
1445 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
1446 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
1447 TEST_EQUAL(expected_alg2,
1448 psa_get_key_enrollment_algorithm(&target_attributes));
1449 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01001450 size_t length;
Tom Cosgrove30ceb232023-09-04 11:20:19 +01001451 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 PSA_ASSERT(psa_export_key(target_key, export_buffer,
1453 material->len, &length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01001454 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01001455 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01001456 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001458 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01001459 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001460 }
1461 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001462 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001465 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001466
1467exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001468 /*
1469 * Source and target key attributes may have been returned by
1470 * psa_get_key_attributes() thus reset them as required.
1471 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001472 psa_reset_key_attributes(&source_attributes);
1473 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001475 PSA_DONE();
1476 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01001477}
1478/* END_CASE */
1479
1480/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481void copy_fail(int source_usage_arg,
1482 int source_alg_arg, int source_alg2_arg,
1483 int type_arg, data_t *material,
1484 int target_type_arg, int target_bits_arg,
1485 int target_usage_arg,
1486 int target_alg_arg, int target_alg2_arg,
1487 int target_id_arg, int target_lifetime_arg,
1488 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02001489{
1490 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1491 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001492 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1493 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02001495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001496 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02001497
1498 /* Prepare the source key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001499 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
1500 psa_set_key_algorithm(&source_attributes, source_alg_arg);
1501 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
1502 psa_set_key_type(&source_attributes, type_arg);
1503 PSA_ASSERT(psa_import_key(&source_attributes,
1504 material->x, material->len,
1505 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02001506
1507 /* Prepare the target attributes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001508 psa_set_key_id(&target_attributes, key_id);
1509 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
1510 psa_set_key_type(&target_attributes, target_type_arg);
1511 psa_set_key_bits(&target_attributes, target_bits_arg);
1512 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
1513 psa_set_key_algorithm(&target_attributes, target_alg_arg);
1514 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02001515
1516 /* Try to copy the key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001517 TEST_EQUAL(psa_copy_key(source_key,
1518 &target_attributes, &target_key),
1519 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001521 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02001522
Gilles Peskine4a644642019-05-03 17:14:08 +02001523exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001524 psa_reset_key_attributes(&source_attributes);
1525 psa_reset_key_attributes(&target_attributes);
1526 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02001527}
1528/* END_CASE */
1529
1530/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001531void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00001532{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001533 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001534 /* Test each valid way of initializing the object, except for `= {0}`, as
1535 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1536 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08001537 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001538 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00001539 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1540 psa_hash_operation_t zero;
1541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001542 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00001543
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001544 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001545 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
1546 PSA_ERROR_BAD_STATE);
1547 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
1548 PSA_ERROR_BAD_STATE);
1549 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
1550 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001551
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001552 /* A default hash operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001553 PSA_ASSERT(psa_hash_abort(&func));
1554 PSA_ASSERT(psa_hash_abort(&init));
1555 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00001556}
1557/* END_CASE */
1558
1559/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001560void hash_setup(int alg_arg,
1561 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001562{
1563 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001564 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001565 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001566 psa_status_t status;
1567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001568 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001570 status = psa_hash_setup(&operation, alg);
1571 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001572
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001573 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001574 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001575
1576 /* If setup failed, reproduce the failure, so as to
1577 * test the resulting state of the operation object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001578 if (status != PSA_SUCCESS) {
1579 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
1580 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001581
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001582 /* Now the operation object should be reusable. */
1583#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
1585 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001586#endif
1587
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001588exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001589 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001590}
1591/* END_CASE */
1592
1593/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001594void hash_compute_fail(int alg_arg, data_t *input,
1595 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01001596{
1597 psa_algorithm_t alg = alg_arg;
1598 uint8_t *output = NULL;
1599 size_t output_size = output_size_arg;
1600 size_t output_length = INVALID_EXPORT_LENGTH;
1601 psa_status_t expected_status = expected_status_arg;
1602 psa_status_t status;
1603
Tom Cosgrove30ceb232023-09-04 11:20:19 +01001604 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001606 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01001607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001608 status = psa_hash_compute(alg, input->x, input->len,
1609 output, output_size, &output_length);
1610 TEST_EQUAL(status, expected_status);
1611 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001612
1613exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001614 mbedtls_free(output);
1615 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01001616}
1617/* END_CASE */
1618
1619/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001620void hash_compare_fail(int alg_arg, data_t *input,
1621 data_t *reference_hash,
1622 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01001623{
1624 psa_algorithm_t alg = alg_arg;
1625 psa_status_t expected_status = expected_status_arg;
1626 psa_status_t status;
1627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001628 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01001629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001630 status = psa_hash_compare(alg, input->x, input->len,
1631 reference_hash->x, reference_hash->len);
1632 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01001633
1634exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001635 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01001636}
1637/* END_CASE */
1638
1639/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001640void hash_compute_compare(int alg_arg, data_t *input,
1641 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01001642{
1643 psa_algorithm_t alg = alg_arg;
1644 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1645 size_t output_length = INVALID_EXPORT_LENGTH;
1646 size_t i;
1647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001648 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01001649
1650 /* Compute with tight buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001651 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
1652 output, PSA_HASH_LENGTH(alg),
1653 &output_length));
1654 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01001655 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01001656 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001657
1658 /* Compute with larger buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001659 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
1660 output, sizeof(output),
1661 &output_length));
1662 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01001663 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01001664 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001665
1666 /* Compare with correct hash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001667 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
1668 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01001669
1670 /* Compare with trailing garbage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001671 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
1672 output, output_length + 1),
1673 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001674
1675 /* Compare with truncated hash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001676 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
1677 output, output_length - 1),
1678 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001679
1680 /* Compare with corrupted value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001681 for (i = 0; i < output_length; i++) {
1682 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001683 output[i] ^= 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001684 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
1685 output, output_length),
1686 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001687 output[i] ^= 1;
1688 }
1689
1690exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001691 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01001692}
1693/* END_CASE */
1694
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001695/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001696void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02001697{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001698 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001699 unsigned char input[] = "";
1700 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001701 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001702 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1703 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
1705 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001706 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001707 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001708 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001710 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02001711
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001712 /* Call setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001713 PSA_ASSERT(psa_hash_setup(&operation, alg));
1714 ASSERT_OPERATION_IS_ACTIVE(operation);
1715 TEST_EQUAL(psa_hash_setup(&operation, alg),
1716 PSA_ERROR_BAD_STATE);
1717 ASSERT_OPERATION_IS_INACTIVE(operation);
1718 PSA_ASSERT(psa_hash_abort(&operation));
1719 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001720
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001721 /* Call update without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001722 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
1723 PSA_ERROR_BAD_STATE);
1724 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02001725
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001726 /* Check that update calls abort on error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001727 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman54f73512021-06-24 18:14:52 +01001728 operation.id = UINT_MAX;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001729 ASSERT_OPERATION_IS_ACTIVE(operation);
1730 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
1731 PSA_ERROR_BAD_STATE);
1732 ASSERT_OPERATION_IS_INACTIVE(operation);
1733 PSA_ASSERT(psa_hash_abort(&operation));
1734 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001735
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001736 /* Call update after finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001737 PSA_ASSERT(psa_hash_setup(&operation, alg));
1738 PSA_ASSERT(psa_hash_finish(&operation,
1739 hash, sizeof(hash), &hash_len));
1740 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
1741 PSA_ERROR_BAD_STATE);
1742 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02001743
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001744 /* Call verify without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001745 TEST_EQUAL(psa_hash_verify(&operation,
1746 valid_hash, sizeof(valid_hash)),
1747 PSA_ERROR_BAD_STATE);
1748 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001749
1750 /* Call verify after finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001751 PSA_ASSERT(psa_hash_setup(&operation, alg));
1752 PSA_ASSERT(psa_hash_finish(&operation,
1753 hash, sizeof(hash), &hash_len));
1754 TEST_EQUAL(psa_hash_verify(&operation,
1755 valid_hash, sizeof(valid_hash)),
1756 PSA_ERROR_BAD_STATE);
1757 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001758
1759 /* Call verify twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001760 PSA_ASSERT(psa_hash_setup(&operation, alg));
1761 ASSERT_OPERATION_IS_ACTIVE(operation);
1762 PSA_ASSERT(psa_hash_verify(&operation,
1763 valid_hash, sizeof(valid_hash)));
1764 ASSERT_OPERATION_IS_INACTIVE(operation);
1765 TEST_EQUAL(psa_hash_verify(&operation,
1766 valid_hash, sizeof(valid_hash)),
1767 PSA_ERROR_BAD_STATE);
1768 ASSERT_OPERATION_IS_INACTIVE(operation);
1769 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001770
1771 /* Call finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001772 TEST_EQUAL(psa_hash_finish(&operation,
1773 hash, sizeof(hash), &hash_len),
1774 PSA_ERROR_BAD_STATE);
1775 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001776
1777 /* Call finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001778 PSA_ASSERT(psa_hash_setup(&operation, alg));
1779 PSA_ASSERT(psa_hash_finish(&operation,
1780 hash, sizeof(hash), &hash_len));
1781 TEST_EQUAL(psa_hash_finish(&operation,
1782 hash, sizeof(hash), &hash_len),
1783 PSA_ERROR_BAD_STATE);
1784 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001785
1786 /* Call finish after calling verify. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001787 PSA_ASSERT(psa_hash_setup(&operation, alg));
1788 PSA_ASSERT(psa_hash_verify(&operation,
1789 valid_hash, sizeof(valid_hash)));
1790 TEST_EQUAL(psa_hash_finish(&operation,
1791 hash, sizeof(hash), &hash_len),
1792 PSA_ERROR_BAD_STATE);
1793 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02001794
1795exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001796 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02001797}
1798/* END_CASE */
1799
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001800/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001801void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03001802{
1803 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001804 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1805 * appended to it */
1806 unsigned char hash[] = {
1807 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1808 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001809 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
1810 };
1811 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00001812 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03001815
itayzafrir27e69452018-11-01 14:26:34 +02001816 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001817 PSA_ASSERT(psa_hash_setup(&operation, alg));
1818 ASSERT_OPERATION_IS_ACTIVE(operation);
1819 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
1820 PSA_ERROR_INVALID_SIGNATURE);
1821 ASSERT_OPERATION_IS_INACTIVE(operation);
1822 PSA_ASSERT(psa_hash_abort(&operation));
1823 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03001824
itayzafrir27e69452018-11-01 14:26:34 +02001825 /* psa_hash_verify with a non-matching hash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001826 PSA_ASSERT(psa_hash_setup(&operation, alg));
1827 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
1828 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03001829
itayzafrir27e69452018-11-01 14:26:34 +02001830 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001831 PSA_ASSERT(psa_hash_setup(&operation, alg));
1832 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
1833 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03001834
itayzafrirec93d302018-10-18 18:01:10 +03001835exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001836 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03001837}
1838/* END_CASE */
1839
Ronald Cronee414c72021-03-18 18:50:08 +01001840/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001841void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03001842{
1843 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001844 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001845 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00001846 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001847 size_t hash_len;
1848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001849 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03001850
itayzafrir58028322018-10-25 10:22:01 +03001851 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001852 PSA_ASSERT(psa_hash_setup(&operation, alg));
1853 TEST_EQUAL(psa_hash_finish(&operation,
1854 hash, expected_size - 1, &hash_len),
1855 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03001856
1857exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001858 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03001859}
1860/* END_CASE */
1861
Ronald Cronee414c72021-03-18 18:50:08 +01001862/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001863void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001864{
1865 psa_algorithm_t alg = PSA_ALG_SHA_256;
1866 unsigned char hash[PSA_HASH_MAX_SIZE];
1867 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1868 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1869 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1870 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1871 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1872 size_t hash_len;
1873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001874 PSA_ASSERT(psa_crypto_init());
1875 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001877 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
1878 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
1879 PSA_ASSERT(psa_hash_finish(&op_finished,
1880 hash, sizeof(hash), &hash_len));
1881 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
1882 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001884 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
1885 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001887 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
1888 PSA_ASSERT(psa_hash_finish(&op_init,
1889 hash, sizeof(hash), &hash_len));
1890 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
1891 PSA_ASSERT(psa_hash_finish(&op_finished,
1892 hash, sizeof(hash), &hash_len));
1893 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
1894 PSA_ASSERT(psa_hash_finish(&op_aborted,
1895 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001896
1897exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001898 psa_hash_abort(&op_source);
1899 psa_hash_abort(&op_init);
1900 psa_hash_abort(&op_setup);
1901 psa_hash_abort(&op_finished);
1902 psa_hash_abort(&op_aborted);
1903 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001904}
1905/* END_CASE */
1906
Ronald Cronee414c72021-03-18 18:50:08 +01001907/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001909{
1910 psa_algorithm_t alg = PSA_ALG_SHA_256;
1911 unsigned char hash[PSA_HASH_MAX_SIZE];
1912 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1913 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1914 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1915 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1916 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1917 size_t hash_len;
1918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001921 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
1922 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
1923 PSA_ASSERT(psa_hash_finish(&op_finished,
1924 hash, sizeof(hash), &hash_len));
1925 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
1926 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001928 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
1929 PSA_ASSERT(psa_hash_finish(&op_target,
1930 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001932 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
1933 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
1934 PSA_ERROR_BAD_STATE);
1935 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
1936 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001937
1938exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001939 psa_hash_abort(&op_target);
1940 psa_hash_abort(&op_init);
1941 psa_hash_abort(&op_setup);
1942 psa_hash_abort(&op_finished);
1943 psa_hash_abort(&op_aborted);
1944 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001945}
1946/* END_CASE */
1947
itayzafrir58028322018-10-25 10:22:01 +03001948/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001949void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00001950{
Jaeden Amero252ef282019-02-15 14:05:35 +00001951 const uint8_t input[1] = { 0 };
1952
Jaeden Amero769ce272019-01-04 11:48:03 +00001953 /* Test each valid way of initializing the object, except for `= {0}`, as
1954 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1955 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08001956 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001957 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00001958 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1959 psa_mac_operation_t zero;
1960
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001961 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00001962
Jaeden Amero252ef282019-02-15 14:05:35 +00001963 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001964 TEST_EQUAL(psa_mac_update(&func,
1965 input, sizeof(input)),
1966 PSA_ERROR_BAD_STATE);
1967 TEST_EQUAL(psa_mac_update(&init,
1968 input, sizeof(input)),
1969 PSA_ERROR_BAD_STATE);
1970 TEST_EQUAL(psa_mac_update(&zero,
1971 input, sizeof(input)),
1972 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00001973
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001974 /* A default MAC operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975 PSA_ASSERT(psa_mac_abort(&func));
1976 PSA_ASSERT(psa_mac_abort(&init));
1977 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00001978}
1979/* END_CASE */
1980
1981/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001982void mac_setup(int key_type_arg,
1983 data_t *key,
1984 int alg_arg,
1985 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001986{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001987 psa_key_type_t key_type = key_type_arg;
1988 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001989 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001990 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001991 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1992#if defined(KNOWN_SUPPORTED_MAC_ALG)
1993 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1994#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001996 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001998 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
1999 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002000 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002001 }
2002 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002003
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002004 /* The operation object should be reusable. */
2005#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002006 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
2007 smoke_test_key_data,
2008 sizeof(smoke_test_key_data),
2009 KNOWN_SUPPORTED_MAC_ALG,
2010 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002011 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 }
2013 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002014#endif
2015
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002016exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002017 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002018}
2019/* END_CASE */
2020
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002021/* 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 +01002022void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00002023{
Ronald Cron5425a212020-08-04 14:58:35 +02002024 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002025 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2026 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002027 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002028 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2029 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002030 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
2031 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002033 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2034 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2035 size_t sign_mac_length = 0;
2036 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2037 const uint8_t verify_mac[] = {
2038 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2039 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
2041 };
Jaeden Amero252ef282019-02-15 14:05:35 +00002042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002043 PSA_ASSERT(psa_crypto_init());
2044 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
2045 psa_set_key_algorithm(&attributes, alg);
2046 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
2049 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002050
Jaeden Amero252ef282019-02-15 14:05:35 +00002051 /* Call update without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002052 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
2053 PSA_ERROR_BAD_STATE);
2054 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002055
2056 /* Call sign finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002057 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
2058 &sign_mac_length),
2059 PSA_ERROR_BAD_STATE);
2060 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002061
2062 /* Call verify finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063 TEST_EQUAL(psa_mac_verify_finish(&operation,
2064 verify_mac, sizeof(verify_mac)),
2065 PSA_ERROR_BAD_STATE);
2066 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002067
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002068 /* Call setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002069 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2070 ASSERT_OPERATION_IS_ACTIVE(operation);
2071 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
2072 PSA_ERROR_BAD_STATE);
2073 ASSERT_OPERATION_IS_INACTIVE(operation);
2074 PSA_ASSERT(psa_mac_abort(&operation));
2075 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002076
Jaeden Amero252ef282019-02-15 14:05:35 +00002077 /* Call update after sign finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002078 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2079 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2080 PSA_ASSERT(psa_mac_sign_finish(&operation,
2081 sign_mac, sizeof(sign_mac),
2082 &sign_mac_length));
2083 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
2084 PSA_ERROR_BAD_STATE);
2085 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002086
2087 /* Call update after verify finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002088 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2089 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2090 PSA_ASSERT(psa_mac_verify_finish(&operation,
2091 verify_mac, sizeof(verify_mac)));
2092 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
2093 PSA_ERROR_BAD_STATE);
2094 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002095
2096 /* Call sign finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002097 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2098 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2099 PSA_ASSERT(psa_mac_sign_finish(&operation,
2100 sign_mac, sizeof(sign_mac),
2101 &sign_mac_length));
2102 TEST_EQUAL(psa_mac_sign_finish(&operation,
2103 sign_mac, sizeof(sign_mac),
2104 &sign_mac_length),
2105 PSA_ERROR_BAD_STATE);
2106 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002107
2108 /* Call verify finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2110 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2111 PSA_ASSERT(psa_mac_verify_finish(&operation,
2112 verify_mac, sizeof(verify_mac)));
2113 TEST_EQUAL(psa_mac_verify_finish(&operation,
2114 verify_mac, sizeof(verify_mac)),
2115 PSA_ERROR_BAD_STATE);
2116 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002117
2118 /* Setup sign but try verify. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002119 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2120 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2121 ASSERT_OPERATION_IS_ACTIVE(operation);
2122 TEST_EQUAL(psa_mac_verify_finish(&operation,
2123 verify_mac, sizeof(verify_mac)),
2124 PSA_ERROR_BAD_STATE);
2125 ASSERT_OPERATION_IS_INACTIVE(operation);
2126 PSA_ASSERT(psa_mac_abort(&operation));
2127 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00002128
2129 /* Setup verify but try sign. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002130 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2131 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2132 ASSERT_OPERATION_IS_ACTIVE(operation);
2133 TEST_EQUAL(psa_mac_sign_finish(&operation,
2134 sign_mac, sizeof(sign_mac),
2135 &sign_mac_length),
2136 PSA_ERROR_BAD_STATE);
2137 ASSERT_OPERATION_IS_INACTIVE(operation);
2138 PSA_ASSERT(psa_mac_abort(&operation));
2139 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002141 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002142
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002143exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002144 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002145}
2146/* END_CASE */
2147
2148/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002149void mac_sign(int key_type_arg,
2150 data_t *key_data,
2151 int alg_arg,
2152 data_t *input,
2153 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002154{
Ronald Cron5425a212020-08-04 14:58:35 +02002155 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002156 psa_key_type_t key_type = key_type_arg;
2157 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002158 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002160 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002161 size_t mac_buffer_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002163 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002164 const size_t output_sizes_to_test[] = {
2165 0,
2166 1,
2167 expected_mac->len - 1,
2168 expected_mac->len,
2169 expected_mac->len + 1,
2170 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002172 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002173 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002174 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002176 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002178 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
2179 psa_set_key_algorithm(&attributes, alg);
2180 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2183 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002185 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02002186 const size_t output_size = output_sizes_to_test[i];
2187 psa_status_t expected_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 (output_size >= expected_mac->len ? PSA_SUCCESS :
2189 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002191 mbedtls_test_set_step(output_size);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002192 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002193
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002194 /* Calculate the MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002195 TEST_EQUAL(psa_mac_compute(key, alg,
2196 input->x, input->len,
2197 actual_mac, output_size, &mac_length),
2198 expected_status);
2199 if (expected_status == PSA_SUCCESS) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002200 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002201 actual_mac, mac_length);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002202 }
2203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 if (output_size > 0) {
2205 memset(actual_mac, 0, output_size);
2206 }
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002207
2208 /* Calculate the MAC, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002209 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2210 PSA_ASSERT(psa_mac_update(&operation,
2211 input->x, input->len));
2212 TEST_EQUAL(psa_mac_sign_finish(&operation,
2213 actual_mac, output_size,
2214 &mac_length),
2215 expected_status);
2216 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02002217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218 if (expected_status == PSA_SUCCESS) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002219 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002220 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02002221 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002222 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02002223 actual_mac = NULL;
2224 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002225
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002226exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002227 psa_mac_abort(&operation);
2228 psa_destroy_key(key);
2229 PSA_DONE();
2230 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002231}
2232/* END_CASE */
2233
2234/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002235void mac_verify(int key_type_arg,
2236 data_t *key_data,
2237 int alg_arg,
2238 data_t *input,
2239 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002240{
Ronald Cron5425a212020-08-04 14:58:35 +02002241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002242 psa_key_type_t key_type = key_type_arg;
2243 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002244 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002246 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02002249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002250 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01002251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002252 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
2253 psa_set_key_algorithm(&attributes, alg);
2254 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07002255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2257 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002258
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002259 /* Verify correct MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002260 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
2261 expected_mac->x, expected_mac->len));
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002262
2263 /* Verify correct MAC, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002264 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2265 PSA_ASSERT(psa_mac_update(&operation,
2266 input->x, input->len));
2267 PSA_ASSERT(psa_mac_verify_finish(&operation,
2268 expected_mac->x,
2269 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01002270
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002271 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002272 TEST_EQUAL(psa_mac_verify(key, alg,
2273 input->x, input->len,
2274 expected_mac->x,
2275 expected_mac->len - 1),
2276 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002277
2278 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002279 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2280 PSA_ASSERT(psa_mac_update(&operation,
2281 input->x, input->len));
2282 TEST_EQUAL(psa_mac_verify_finish(&operation,
2283 expected_mac->x,
2284 expected_mac->len - 1),
2285 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002286
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002287 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002288 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002289 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
2290 TEST_EQUAL(psa_mac_verify(key, alg,
2291 input->x, input->len,
2292 perturbed_mac, expected_mac->len + 1),
2293 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002294
2295 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002296 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2297 PSA_ASSERT(psa_mac_update(&operation,
2298 input->x, input->len));
2299 TEST_EQUAL(psa_mac_verify_finish(&operation,
2300 perturbed_mac,
2301 expected_mac->len + 1),
2302 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002303
2304 /* Test changing one byte. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002305 for (size_t i = 0; i < expected_mac->len; i++) {
2306 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002307 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002309 TEST_EQUAL(psa_mac_verify(key, alg,
2310 input->x, input->len,
2311 perturbed_mac, expected_mac->len),
2312 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002314 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2315 PSA_ASSERT(psa_mac_update(&operation,
2316 input->x, input->len));
2317 TEST_EQUAL(psa_mac_verify_finish(&operation,
2318 perturbed_mac,
2319 expected_mac->len),
2320 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002321 perturbed_mac[i] ^= 1;
2322 }
2323
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002325 psa_mac_abort(&operation);
2326 psa_destroy_key(key);
2327 PSA_DONE();
2328 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002329}
2330/* END_CASE */
2331
2332/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002333void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00002334{
Jaeden Ameroab439972019-02-15 14:12:05 +00002335 const uint8_t input[1] = { 0 };
2336 unsigned char output[1] = { 0 };
2337 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002338 /* Test each valid way of initializing the object, except for `= {0}`, as
2339 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2340 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08002341 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00002343 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2344 psa_cipher_operation_t zero;
2345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002346 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00002347
Jaeden Ameroab439972019-02-15 14:12:05 +00002348 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 TEST_EQUAL(psa_cipher_update(&func,
2350 input, sizeof(input),
2351 output, sizeof(output),
2352 &output_length),
2353 PSA_ERROR_BAD_STATE);
2354 TEST_EQUAL(psa_cipher_update(&init,
2355 input, sizeof(input),
2356 output, sizeof(output),
2357 &output_length),
2358 PSA_ERROR_BAD_STATE);
2359 TEST_EQUAL(psa_cipher_update(&zero,
2360 input, sizeof(input),
2361 output, sizeof(output),
2362 &output_length),
2363 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00002364
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002365 /* A default cipher operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002366 PSA_ASSERT(psa_cipher_abort(&func));
2367 PSA_ASSERT(psa_cipher_abort(&init));
2368 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00002369}
2370/* END_CASE */
2371
2372/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002373void cipher_setup(int key_type_arg,
2374 data_t *key,
2375 int alg_arg,
2376 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002377{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002378 psa_key_type_t key_type = key_type_arg;
2379 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002380 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002381 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002382 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002383#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002384 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2385#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002389 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
2390 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002391 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002392 }
2393 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002394
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002395 /* The operation object should be reusable. */
2396#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2398 smoke_test_key_data,
2399 sizeof(smoke_test_key_data),
2400 KNOWN_SUPPORTED_CIPHER_ALG,
2401 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002402 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 }
2404 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002405#endif
2406
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002407exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002408 psa_cipher_abort(&operation);
2409 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002410}
2411/* END_CASE */
2412
Ronald Cronee414c72021-03-18 18:50:08 +01002413/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002414void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00002415{
Ronald Cron5425a212020-08-04 14:58:35 +02002416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002417 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2418 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002420 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002421 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002422 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002423 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002424 0xaa, 0xaa, 0xaa, 0xaa
2425 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002426 const uint8_t text[] = {
2427 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002428 0xbb, 0xbb, 0xbb, 0xbb
2429 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002430 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002431 size_t length = 0;
2432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002433 PSA_ASSERT(psa_crypto_init());
2434 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
2435 psa_set_key_algorithm(&attributes, alg);
2436 psa_set_key_type(&attributes, key_type);
2437 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
2438 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00002439
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002440 /* Call encrypt setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002441 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2442 ASSERT_OPERATION_IS_ACTIVE(operation);
2443 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
2444 PSA_ERROR_BAD_STATE);
2445 ASSERT_OPERATION_IS_INACTIVE(operation);
2446 PSA_ASSERT(psa_cipher_abort(&operation));
2447 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002448
2449 /* Call decrypt setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002450 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
2451 ASSERT_OPERATION_IS_ACTIVE(operation);
2452 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
2453 PSA_ERROR_BAD_STATE);
2454 ASSERT_OPERATION_IS_INACTIVE(operation);
2455 PSA_ASSERT(psa_cipher_abort(&operation));
2456 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002457
Jaeden Ameroab439972019-02-15 14:12:05 +00002458 /* Generate an IV without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 TEST_EQUAL(psa_cipher_generate_iv(&operation,
2460 buffer, sizeof(buffer),
2461 &length),
2462 PSA_ERROR_BAD_STATE);
2463 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002464
2465 /* Generate an IV twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2467 PSA_ASSERT(psa_cipher_generate_iv(&operation,
2468 buffer, sizeof(buffer),
2469 &length));
2470 ASSERT_OPERATION_IS_ACTIVE(operation);
2471 TEST_EQUAL(psa_cipher_generate_iv(&operation,
2472 buffer, sizeof(buffer),
2473 &length),
2474 PSA_ERROR_BAD_STATE);
2475 ASSERT_OPERATION_IS_INACTIVE(operation);
2476 PSA_ASSERT(psa_cipher_abort(&operation));
2477 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002478
2479 /* Generate an IV after it's already set. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002480 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2481 PSA_ASSERT(psa_cipher_set_iv(&operation,
2482 iv, sizeof(iv)));
2483 TEST_EQUAL(psa_cipher_generate_iv(&operation,
2484 buffer, sizeof(buffer),
2485 &length),
2486 PSA_ERROR_BAD_STATE);
2487 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002488
2489 /* Set an IV without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490 TEST_EQUAL(psa_cipher_set_iv(&operation,
2491 iv, sizeof(iv)),
2492 PSA_ERROR_BAD_STATE);
2493 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002494
2495 /* Set an IV after it's already set. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002496 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2497 PSA_ASSERT(psa_cipher_set_iv(&operation,
2498 iv, sizeof(iv)));
2499 ASSERT_OPERATION_IS_ACTIVE(operation);
2500 TEST_EQUAL(psa_cipher_set_iv(&operation,
2501 iv, sizeof(iv)),
2502 PSA_ERROR_BAD_STATE);
2503 ASSERT_OPERATION_IS_INACTIVE(operation);
2504 PSA_ASSERT(psa_cipher_abort(&operation));
2505 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002506
2507 /* Set an IV after it's already generated. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002508 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2509 PSA_ASSERT(psa_cipher_generate_iv(&operation,
2510 buffer, sizeof(buffer),
2511 &length));
2512 TEST_EQUAL(psa_cipher_set_iv(&operation,
2513 iv, sizeof(iv)),
2514 PSA_ERROR_BAD_STATE);
2515 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002516
2517 /* Call update without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002518 TEST_EQUAL(psa_cipher_update(&operation,
2519 text, sizeof(text),
2520 buffer, sizeof(buffer),
2521 &length),
2522 PSA_ERROR_BAD_STATE);
2523 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002524
2525 /* Call update without an IV where an IV is required. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002526 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2527 ASSERT_OPERATION_IS_ACTIVE(operation);
2528 TEST_EQUAL(psa_cipher_update(&operation,
2529 text, sizeof(text),
2530 buffer, sizeof(buffer),
2531 &length),
2532 PSA_ERROR_BAD_STATE);
2533 ASSERT_OPERATION_IS_INACTIVE(operation);
2534 PSA_ASSERT(psa_cipher_abort(&operation));
2535 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002536
2537 /* Call update after finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002538 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2539 PSA_ASSERT(psa_cipher_set_iv(&operation,
2540 iv, sizeof(iv)));
2541 PSA_ASSERT(psa_cipher_finish(&operation,
2542 buffer, sizeof(buffer), &length));
2543 TEST_EQUAL(psa_cipher_update(&operation,
2544 text, sizeof(text),
2545 buffer, sizeof(buffer),
2546 &length),
2547 PSA_ERROR_BAD_STATE);
2548 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002549
2550 /* Call finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002551 TEST_EQUAL(psa_cipher_finish(&operation,
2552 buffer, sizeof(buffer), &length),
2553 PSA_ERROR_BAD_STATE);
2554 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002555
2556 /* Call finish without an IV where an IV is required. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00002558 /* Not calling update means we are encrypting an empty buffer, which is OK
2559 * for cipher modes with padding. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002560 ASSERT_OPERATION_IS_ACTIVE(operation);
2561 TEST_EQUAL(psa_cipher_finish(&operation,
2562 buffer, sizeof(buffer), &length),
2563 PSA_ERROR_BAD_STATE);
2564 ASSERT_OPERATION_IS_INACTIVE(operation);
2565 PSA_ASSERT(psa_cipher_abort(&operation));
2566 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002567
2568 /* Call finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002569 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2570 PSA_ASSERT(psa_cipher_set_iv(&operation,
2571 iv, sizeof(iv)));
2572 PSA_ASSERT(psa_cipher_finish(&operation,
2573 buffer, sizeof(buffer), &length));
2574 TEST_EQUAL(psa_cipher_finish(&operation,
2575 buffer, sizeof(buffer), &length),
2576 PSA_ERROR_BAD_STATE);
2577 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002579 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002580
Jaeden Ameroab439972019-02-15 14:12:05 +00002581exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002582 psa_cipher_abort(&operation);
2583 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002584}
2585/* END_CASE */
2586
2587/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002588void cipher_encrypt_fail(int alg_arg,
2589 int key_type_arg,
2590 data_t *key_data,
2591 data_t *input,
2592 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02002593{
Ronald Cron5425a212020-08-04 14:58:35 +02002594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002595 psa_status_t status;
2596 psa_key_type_t key_type = key_type_arg;
2597 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002598 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002599 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002600 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002601 size_t output_length = 0;
2602 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002604 if (PSA_ERROR_BAD_STATE != expected_status) {
2605 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002607 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2608 psa_set_key_algorithm(&attributes, alg);
2609 psa_set_key_type(&attributes, key_type);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002611 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
2612 input->len);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002613 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002615 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2616 &key));
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002617 }
2618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002619 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
2620 output_buffer_size, &output_length);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002622 TEST_EQUAL(status, expected_status);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002623
2624exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002625 mbedtls_free(output);
2626 psa_destroy_key(key);
2627 PSA_DONE();
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002628}
2629/* END_CASE */
2630
2631/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002632void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
2633 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002634{
2635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2636 psa_key_type_t key_type = key_type_arg;
2637 psa_algorithm_t alg = alg_arg;
Ronald Cron33c69682021-07-15 09:38:11 +02002638 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2639 uint8_t iv[1] = { 0x5a };
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002640 unsigned char *output = NULL;
2641 size_t output_buffer_size = 0;
Gilles Peskine4da5a852022-04-20 17:09:38 +02002642 size_t output_length, length;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002643 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002645 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002646
Gilles Peskine5f504202022-04-20 16:55:03 +02002647 /* Validate size macros */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002648 TEST_LE_U(ciphertext->len,
2649 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
2650 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
2651 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
2652 TEST_LE_U(plaintext->len,
2653 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
2654 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
2655 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine69d98172022-04-20 17:07:52 +02002656
Gilles Peskine5f504202022-04-20 16:55:03 +02002657
2658 /* Set up key and output buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002659 psa_set_key_usage_flags(&attributes,
2660 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
2661 psa_set_key_algorithm(&attributes, alg);
2662 psa_set_key_type(&attributes, key_type);
2663 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2664 &key));
2665 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
2666 plaintext->len);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002667 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002668
Gilles Peskine5f504202022-04-20 16:55:03 +02002669 /* set_iv() is not allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002670 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2671 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
2672 PSA_ERROR_BAD_STATE);
2673 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
2674 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
2675 PSA_ERROR_BAD_STATE);
Gilles Peskine5f504202022-04-20 16:55:03 +02002676
2677 /* generate_iv() is not allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2679 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
2680 &length),
2681 PSA_ERROR_BAD_STATE);
2682 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
2683 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
2684 &length),
2685 PSA_ERROR_BAD_STATE);
Ronald Cron33c69682021-07-15 09:38:11 +02002686
Gilles Peskine4da5a852022-04-20 17:09:38 +02002687 /* Multipart encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002688 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002689 output_length = 0;
2690 length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002691 PSA_ASSERT(psa_cipher_update(&operation,
2692 plaintext->x, plaintext->len,
2693 output, output_buffer_size,
2694 &length));
2695 TEST_LE_U(length, output_buffer_size);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002696 output_length += length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697 PSA_ASSERT(psa_cipher_finish(&operation,
2698 mbedtls_buffer_offset(output, output_length),
2699 output_buffer_size - output_length,
2700 &length));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002701 output_length += length;
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002702 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002703 output, output_length);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002704
2705 /* Multipart encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002706 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002707 output_length = 0;
2708 length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002709 PSA_ASSERT(psa_cipher_update(&operation,
2710 ciphertext->x, ciphertext->len,
2711 output, output_buffer_size,
2712 &length));
2713 TEST_LE_U(length, output_buffer_size);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002714 output_length += length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002715 PSA_ASSERT(psa_cipher_finish(&operation,
2716 mbedtls_buffer_offset(output, output_length),
2717 output_buffer_size - output_length,
2718 &length));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002719 output_length += length;
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002720 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002721 output, output_length);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002722
Gilles Peskine5f504202022-04-20 16:55:03 +02002723 /* One-shot encryption */
Gilles Peskine69d98172022-04-20 17:07:52 +02002724 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002725 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
2726 output, output_buffer_size,
2727 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002728 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002729 output, output_length);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002730
Gilles Peskine69d98172022-04-20 17:07:52 +02002731 /* One-shot decryption */
2732 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002733 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
2734 output, output_buffer_size,
2735 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002736 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002737 output, output_length);
Gilles Peskine5f504202022-04-20 16:55:03 +02002738
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002739exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002740 mbedtls_free(output);
2741 psa_cipher_abort(&operation);
2742 psa_destroy_key(key);
2743 PSA_DONE();
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002744}
2745/* END_CASE */
2746
2747/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotted33ef12021-07-14 12:31:21 +01002749{
2750 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2751 psa_algorithm_t alg = alg_arg;
2752 psa_key_type_t key_type = key_type_arg;
2753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2754 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2755 psa_status_t status;
2756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 PSA_ASSERT(psa_crypto_init());
Paul Elliotted33ef12021-07-14 12:31:21 +01002758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002759 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2760 psa_set_key_algorithm(&attributes, alg);
2761 psa_set_key_type(&attributes, key_type);
Paul Elliotted33ef12021-07-14 12:31:21 +01002762
2763 /* Usage of either of these two size macros would cause divide by zero
2764 * with incorrect key types previously. Input length should be irrelevant
2765 * here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
2767 0);
2768 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotted33ef12021-07-14 12:31:21 +01002769
2770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002771 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2772 &key));
Paul Elliotted33ef12021-07-14 12:31:21 +01002773
2774 /* Should fail due to invalid alg type (to support invalid key type).
2775 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002776 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotted33ef12021-07-14 12:31:21 +01002777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002778 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotted33ef12021-07-14 12:31:21 +01002779
2780exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002781 psa_cipher_abort(&operation);
2782 psa_destroy_key(key);
2783 PSA_DONE();
Paul Elliotted33ef12021-07-14 12:31:21 +01002784}
2785/* END_CASE */
2786
2787/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002788void cipher_encrypt_validation(int alg_arg,
2789 int key_type_arg,
2790 data_t *key_data,
2791 data_t *input)
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002792{
2793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2794 psa_key_type_t key_type = key_type_arg;
2795 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002796 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002797 unsigned char *output1 = NULL;
2798 size_t output1_buffer_size = 0;
2799 size_t output1_length = 0;
2800 unsigned char *output2 = NULL;
2801 size_t output2_buffer_size = 0;
2802 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002803 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002804 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002807 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02002808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002809 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2810 psa_set_key_algorithm(&attributes, alg);
2811 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03002812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002813 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
2814 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
2815 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002816 TEST_CALLOC(output1, output1_buffer_size);
2817 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002819 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2820 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002821
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002822 /* The one-shot cipher encryption uses generated iv so validating
2823 the output is not possible. Validating with multipart encryption. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002824 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
2825 output1_buffer_size, &output1_length));
2826 TEST_LE_U(output1_length,
2827 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
2828 TEST_LE_U(output1_length,
2829 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002831 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2832 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002834 PSA_ASSERT(psa_cipher_update(&operation,
2835 input->x, input->len,
2836 output2, output2_buffer_size,
2837 &function_output_length));
2838 TEST_LE_U(function_output_length,
2839 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
2840 TEST_LE_U(function_output_length,
2841 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002842 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002844 PSA_ASSERT(psa_cipher_finish(&operation,
2845 output2 + output2_length,
2846 output2_buffer_size - output2_length,
2847 &function_output_length));
2848 TEST_LE_U(function_output_length,
2849 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
2850 TEST_LE_U(function_output_length,
2851 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002852 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002854 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002855 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002856 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002857
Gilles Peskine50e586b2018-06-08 14:28:46 +02002858exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002859 psa_cipher_abort(&operation);
2860 mbedtls_free(output1);
2861 mbedtls_free(output2);
2862 psa_destroy_key(key);
2863 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02002864}
2865/* END_CASE */
2866
2867/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002868void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
2869 data_t *key_data, data_t *iv,
2870 data_t *input,
2871 int first_part_size_arg,
2872 int output1_length_arg, int output2_length_arg,
2873 data_t *expected_output,
2874 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875{
Ronald Cron5425a212020-08-04 14:58:35 +02002876 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002877 psa_key_type_t key_type = key_type_arg;
2878 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002879 psa_status_t status;
2880 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002881 size_t first_part_size = first_part_size_arg;
2882 size_t output1_length = output1_length_arg;
2883 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002884 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002885 size_t output_buffer_size = 0;
2886 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002887 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002888 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002889 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002891 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002893 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2894 psa_set_key_algorithm(&attributes, alg);
2895 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03002896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002897 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2898 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002900 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002902 if (iv->len > 0) {
2903 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002904 }
2905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002906 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
2907 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002908 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02002909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002910 TEST_LE_U(first_part_size, input->len);
2911 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
2912 output, output_buffer_size,
2913 &function_output_length));
2914 TEST_ASSERT(function_output_length == output1_length);
2915 TEST_LE_U(function_output_length,
2916 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
2917 TEST_LE_U(function_output_length,
2918 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002919 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002921 if (first_part_size < input->len) {
2922 PSA_ASSERT(psa_cipher_update(&operation,
2923 input->x + first_part_size,
2924 input->len - first_part_size,
2925 (output_buffer_size == 0 ? NULL :
2926 output + total_output_length),
2927 output_buffer_size - total_output_length,
2928 &function_output_length));
2929 TEST_ASSERT(function_output_length == output2_length);
2930 TEST_LE_U(function_output_length,
2931 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
2932 alg,
2933 input->len - first_part_size));
2934 TEST_LE_U(function_output_length,
2935 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002936 total_output_length += function_output_length;
2937 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 status = psa_cipher_finish(&operation,
2940 (output_buffer_size == 0 ? NULL :
2941 output + total_output_length),
2942 output_buffer_size - total_output_length,
2943 &function_output_length);
2944 TEST_LE_U(function_output_length,
2945 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
2946 TEST_LE_U(function_output_length,
2947 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002948 total_output_length += function_output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002949 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02002950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002951 if (expected_status == PSA_SUCCESS) {
2952 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002953
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002954 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01002955 output, total_output_length);
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002956 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002957
2958exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002959 psa_cipher_abort(&operation);
2960 mbedtls_free(output);
2961 psa_destroy_key(key);
2962 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963}
2964/* END_CASE */
2965
2966/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002967void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
2968 data_t *key_data, data_t *iv,
2969 data_t *input,
2970 int first_part_size_arg,
2971 int output1_length_arg, int output2_length_arg,
2972 data_t *expected_output,
2973 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974{
Ronald Cron5425a212020-08-04 14:58:35 +02002975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976 psa_key_type_t key_type = key_type_arg;
2977 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002978 psa_status_t status;
2979 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002980 size_t first_part_size = first_part_size_arg;
2981 size_t output1_length = output1_length_arg;
2982 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002983 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002984 size_t output_buffer_size = 0;
2985 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002986 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002987 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002990 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02002991
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002992 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
2993 psa_set_key_algorithm(&attributes, alg);
2994 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03002995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2997 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002999 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02003000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003001 if (iv->len > 0) {
3002 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003003 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003005 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
3006 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003007 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02003008
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003009 TEST_LE_U(first_part_size, input->len);
3010 PSA_ASSERT(psa_cipher_update(&operation,
3011 input->x, first_part_size,
3012 output, output_buffer_size,
3013 &function_output_length));
3014 TEST_ASSERT(function_output_length == output1_length);
3015 TEST_LE_U(function_output_length,
3016 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
3017 TEST_LE_U(function_output_length,
3018 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003019 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003021 if (first_part_size < input->len) {
3022 PSA_ASSERT(psa_cipher_update(&operation,
3023 input->x + first_part_size,
3024 input->len - first_part_size,
3025 (output_buffer_size == 0 ? NULL :
3026 output + total_output_length),
3027 output_buffer_size - total_output_length,
3028 &function_output_length));
3029 TEST_ASSERT(function_output_length == output2_length);
3030 TEST_LE_U(function_output_length,
3031 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3032 alg,
3033 input->len - first_part_size));
3034 TEST_LE_U(function_output_length,
3035 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02003036 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003037 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003039 status = psa_cipher_finish(&operation,
3040 (output_buffer_size == 0 ? NULL :
3041 output + total_output_length),
3042 output_buffer_size - total_output_length,
3043 &function_output_length);
3044 TEST_LE_U(function_output_length,
3045 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3046 TEST_LE_U(function_output_length,
3047 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003048 total_output_length += function_output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02003050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051 if (expected_status == PSA_SUCCESS) {
3052 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02003053
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003054 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003055 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02003056 }
3057
Gilles Peskine50e586b2018-06-08 14:28:46 +02003058exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059 psa_cipher_abort(&operation);
3060 mbedtls_free(output);
3061 psa_destroy_key(key);
3062 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02003063}
3064/* END_CASE */
3065
Gilles Peskine50e586b2018-06-08 14:28:46 +02003066/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003067void cipher_decrypt_fail(int alg_arg,
3068 int key_type_arg,
3069 data_t *key_data,
3070 data_t *iv,
3071 data_t *input_arg,
3072 int expected_status_arg)
3073{
3074 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3075 psa_status_t status;
3076 psa_key_type_t key_type = key_type_arg;
3077 psa_algorithm_t alg = alg_arg;
3078 psa_status_t expected_status = expected_status_arg;
3079 unsigned char *input = NULL;
3080 size_t input_buffer_size = 0;
3081 unsigned char *output = NULL;
3082 size_t output_buffer_size = 0;
3083 size_t output_length = 0;
3084 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3085
3086 if (PSA_ERROR_BAD_STATE != expected_status) {
3087 PSA_ASSERT(psa_crypto_init());
3088
3089 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3090 psa_set_key_algorithm(&attributes, alg);
3091 psa_set_key_type(&attributes, key_type);
3092
3093 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3094 &key));
3095 }
3096
3097 /* Allocate input buffer and copy the iv and the plaintext */
3098 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
3099 if (input_buffer_size > 0) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003100 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003101 memcpy(input, iv->x, iv->len);
3102 memcpy(input + iv->len, input_arg->x, input_arg->len);
3103 }
3104
3105 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003106 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003107
3108 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
3109 output_buffer_size, &output_length);
3110 TEST_EQUAL(status, expected_status);
3111
3112exit:
3113 mbedtls_free(input);
3114 mbedtls_free(output);
3115 psa_destroy_key(key);
3116 PSA_DONE();
3117}
3118/* END_CASE */
3119
3120/* BEGIN_CASE */
3121void cipher_decrypt(int alg_arg,
3122 int key_type_arg,
3123 data_t *key_data,
3124 data_t *iv,
3125 data_t *input_arg,
3126 data_t *expected_output)
3127{
3128 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3129 psa_key_type_t key_type = key_type_arg;
3130 psa_algorithm_t alg = alg_arg;
3131 unsigned char *input = NULL;
3132 size_t input_buffer_size = 0;
3133 unsigned char *output = NULL;
3134 size_t output_buffer_size = 0;
3135 size_t output_length = 0;
3136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3137
3138 PSA_ASSERT(psa_crypto_init());
3139
3140 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3141 psa_set_key_algorithm(&attributes, alg);
3142 psa_set_key_type(&attributes, key_type);
3143
3144 /* Allocate input buffer and copy the iv and the plaintext */
3145 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
3146 if (input_buffer_size > 0) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003147 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003148 memcpy(input, iv->x, iv->len);
3149 memcpy(input + iv->len, input_arg->x, input_arg->len);
3150 }
3151
3152 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003153 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003154
3155 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3156 &key));
3157
3158 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
3159 output_buffer_size, &output_length));
3160 TEST_LE_U(output_length,
3161 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
3162 TEST_LE_U(output_length,
3163 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
3164
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003165 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003166 output, output_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003167exit:
3168 mbedtls_free(input);
3169 mbedtls_free(output);
3170 psa_destroy_key(key);
3171 PSA_DONE();
3172}
3173/* END_CASE */
3174
3175/* BEGIN_CASE */
3176void cipher_verify_output(int alg_arg,
gabor-mezei-arm43611b02021-06-25 15:49:14 +02003177 int key_type_arg,
3178 data_t *key_data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003179 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02003180{
Ronald Cron5425a212020-08-04 14:58:35 +02003181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003182 psa_key_type_t key_type = key_type_arg;
3183 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003184 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003185 size_t output1_size = 0;
3186 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003187 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003188 size_t output2_size = 0;
3189 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003192 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02003193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003194 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3195 psa_set_key_algorithm(&attributes, alg);
3196 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03003197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003198 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3199 &key));
3200 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003201 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03003202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003203 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
3204 output1, output1_size,
3205 &output1_length));
3206 TEST_LE_U(output1_length,
3207 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
3208 TEST_LE_U(output1_length,
3209 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03003210
3211 output2_size = output1_length;
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003212 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03003213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003214 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
3215 output2, output2_size,
3216 &output2_length));
3217 TEST_LE_U(output2_length,
3218 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
3219 TEST_LE_U(output2_length,
3220 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03003221
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003222 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03003223
3224exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003225 mbedtls_free(output1);
3226 mbedtls_free(output2);
3227 psa_destroy_key(key);
3228 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03003229}
3230/* END_CASE */
3231
3232/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003233void cipher_verify_output_multipart(int alg_arg,
3234 int key_type_arg,
3235 data_t *key_data,
3236 data_t *input,
3237 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03003238{
Ronald Cron5425a212020-08-04 14:58:35 +02003239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003240 psa_key_type_t key_type = key_type_arg;
3241 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003242 size_t first_part_size = first_part_size_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003243 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03003244 size_t iv_size = 16;
3245 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003246 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003247 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003248 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003249 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003250 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003251 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003252 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003253 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3254 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003257 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03003258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003259 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3260 psa_set_key_algorithm(&attributes, alg);
3261 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03003262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003263 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3264 &key));
Moran Pekerded84402018-06-06 16:36:50 +03003265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003266 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
3267 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03003268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003269 if (alg != PSA_ALG_ECB_NO_PADDING) {
3270 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
3271 iv, iv_size,
3272 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003273 }
3274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3276 TEST_LE_U(output1_buffer_size,
3277 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003278 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03003279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003280 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003282 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
3283 output1, output1_buffer_size,
3284 &function_output_length));
3285 TEST_LE_U(function_output_length,
3286 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
3287 TEST_LE_U(function_output_length,
3288 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003289 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003291 PSA_ASSERT(psa_cipher_update(&operation1,
3292 input->x + first_part_size,
3293 input->len - first_part_size,
3294 output1, output1_buffer_size,
3295 &function_output_length));
3296 TEST_LE_U(function_output_length,
3297 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3298 alg,
3299 input->len - first_part_size));
3300 TEST_LE_U(function_output_length,
3301 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003302 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003304 PSA_ASSERT(psa_cipher_finish(&operation1,
3305 output1 + output1_length,
3306 output1_buffer_size - output1_length,
3307 &function_output_length));
3308 TEST_LE_U(function_output_length,
3309 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3310 TEST_LE_U(function_output_length,
3311 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02003312 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003314 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02003315
Gilles Peskine048b7f02018-06-08 14:20:49 +02003316 output2_buffer_size = output1_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003317 TEST_LE_U(output2_buffer_size,
3318 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
3319 TEST_LE_U(output2_buffer_size,
3320 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003321 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02003322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003323 if (iv_length > 0) {
3324 PSA_ASSERT(psa_cipher_set_iv(&operation2,
3325 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003326 }
Moran Pekerded84402018-06-06 16:36:50 +03003327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003328 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
3329 output2, output2_buffer_size,
3330 &function_output_length));
3331 TEST_LE_U(function_output_length,
3332 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
3333 TEST_LE_U(function_output_length,
3334 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003335 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003337 PSA_ASSERT(psa_cipher_update(&operation2,
3338 output1 + first_part_size,
3339 output1_length - first_part_size,
3340 output2, output2_buffer_size,
3341 &function_output_length));
3342 TEST_LE_U(function_output_length,
3343 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3344 alg,
3345 output1_length - first_part_size));
3346 TEST_LE_U(function_output_length,
3347 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003348 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003350 PSA_ASSERT(psa_cipher_finish(&operation2,
3351 output2 + output2_length,
3352 output2_buffer_size - output2_length,
3353 &function_output_length));
3354 TEST_LE_U(function_output_length,
3355 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3356 TEST_LE_U(function_output_length,
3357 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02003358 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003360 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02003361
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003362 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02003363
3364exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003365 psa_cipher_abort(&operation1);
3366 psa_cipher_abort(&operation2);
3367 mbedtls_free(output1);
3368 mbedtls_free(output2);
3369 psa_destroy_key(key);
3370 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02003371}
3372/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003373
Gilles Peskine20035e32018-02-03 22:44:14 +01003374/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003375void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
3376 int alg_arg,
3377 data_t *nonce,
3378 data_t *additional_data,
3379 data_t *input_data,
3380 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003381{
Ronald Cron5425a212020-08-04 14:58:35 +02003382 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383 psa_key_type_t key_type = key_type_arg;
3384 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003385 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003386 unsigned char *output_data = NULL;
3387 size_t output_size = 0;
3388 size_t output_length = 0;
3389 unsigned char *output_data2 = NULL;
3390 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003391 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003392 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003397 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3398 psa_set_key_algorithm(&attributes, alg);
3399 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003401 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3402 &key));
3403 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3404 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003406 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3407 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003408 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3409 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003410 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3411 expected_result != PSA_ERROR_NOT_SUPPORTED) {
3412 TEST_EQUAL(output_size,
3413 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3414 TEST_ASSERT(output_size <=
3415 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01003416 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003417 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003419 status = psa_aead_encrypt(key, alg,
3420 nonce->x, nonce->len,
3421 additional_data->x,
3422 additional_data->len,
3423 input_data->x, input_data->len,
3424 output_data, output_size,
3425 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01003426
3427 /* If the operation is not supported, just skip and not fail in case the
3428 * encryption involves a common limitation of cryptography hardwares and
3429 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003430 if (status == PSA_ERROR_NOT_SUPPORTED) {
3431 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3432 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01003433 }
3434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003437 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003438 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003439
Gilles Peskine003a4a92019-05-14 16:09:40 +02003440 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3441 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003442 TEST_EQUAL(input_data->len,
3443 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02003444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003445 TEST_ASSERT(input_data->len <=
3446 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01003447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003448 TEST_EQUAL(psa_aead_decrypt(key, alg,
3449 nonce->x, nonce->len,
3450 additional_data->x,
3451 additional_data->len,
3452 output_data, output_length,
3453 output_data2, output_length,
3454 &output_length2),
3455 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02003456
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003457 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003458 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003460
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003462 psa_destroy_key(key);
3463 mbedtls_free(output_data);
3464 mbedtls_free(output_data2);
3465 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003466}
3467/* END_CASE */
3468
3469/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003470void aead_encrypt(int key_type_arg, data_t *key_data,
3471 int alg_arg,
3472 data_t *nonce,
3473 data_t *additional_data,
3474 data_t *input_data,
3475 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003476{
Ronald Cron5425a212020-08-04 14:58:35 +02003477 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478 psa_key_type_t key_type = key_type_arg;
3479 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003480 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003481 unsigned char *output_data = NULL;
3482 size_t output_size = 0;
3483 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003485 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003487 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003489 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3490 psa_set_key_algorithm(&attributes, alg);
3491 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003493 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3494 &key));
3495 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3496 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003498 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3499 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003500 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3501 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003502 TEST_EQUAL(output_size,
3503 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3504 TEST_ASSERT(output_size <=
3505 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003506 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003508 status = psa_aead_encrypt(key, alg,
3509 nonce->x, nonce->len,
3510 additional_data->x, additional_data->len,
3511 input_data->x, input_data->len,
3512 output_data, output_size,
3513 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003514
Ronald Cron28a45ed2021-02-09 20:35:42 +01003515 /* If the operation is not supported, just skip and not fail in case the
3516 * encryption involves a common limitation of cryptography hardwares and
3517 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 if (status == PSA_ERROR_NOT_SUPPORTED) {
3519 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3520 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01003521 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003523 PSA_ASSERT(status);
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003524 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003525 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003526
Gilles Peskinea1cac842018-06-11 19:33:02 +02003527exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003528 psa_destroy_key(key);
3529 mbedtls_free(output_data);
3530 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003531}
3532/* END_CASE */
3533
3534/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535void aead_decrypt(int key_type_arg, data_t *key_data,
3536 int alg_arg,
3537 data_t *nonce,
3538 data_t *additional_data,
3539 data_t *input_data,
3540 data_t *expected_data,
3541 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003542{
Ronald Cron5425a212020-08-04 14:58:35 +02003543 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003544 psa_key_type_t key_type = key_type_arg;
3545 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003546 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003547 unsigned char *output_data = NULL;
3548 size_t output_size = 0;
3549 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003551 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003552 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003554 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003556 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3557 psa_set_key_algorithm(&attributes, alg);
3558 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003560 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3561 &key));
3562 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3563 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003565 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3566 alg);
3567 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3568 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01003569 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3570 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003571 TEST_EQUAL(output_size,
3572 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3573 TEST_ASSERT(output_size <=
3574 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01003575 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003576 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003578 status = psa_aead_decrypt(key, alg,
3579 nonce->x, nonce->len,
3580 additional_data->x,
3581 additional_data->len,
3582 input_data->x, input_data->len,
3583 output_data, output_size,
3584 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01003585
Ronald Cron28a45ed2021-02-09 20:35:42 +01003586 /* If the operation is not supported, just skip and not fail in case the
3587 * decryption involves a common limitation of cryptography hardwares and
3588 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003589 if (status == PSA_ERROR_NOT_SUPPORTED) {
3590 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3591 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01003592 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003594 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 if (expected_result == PSA_SUCCESS) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003597 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003598 output_data, output_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003599 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02003600
Gilles Peskinea1cac842018-06-11 19:33:02 +02003601exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003602 psa_destroy_key(key);
3603 mbedtls_free(output_data);
3604 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003605}
3606/* END_CASE */
3607
3608/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003609void signature_size(int type_arg,
3610 int bits,
3611 int alg_arg,
3612 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003613{
3614 psa_key_type_t type = type_arg;
3615 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003616 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01003617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003618 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01003619#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003620 TEST_EQUAL(actual_size,
3621 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg));
Gilles Peskine841b14b2019-11-26 17:37:37 +01003622#endif /* MBEDTLS_TEST_DEPRECATED */
3623
Gilles Peskinee59236f2018-01-27 23:32:46 +01003624exit:
3625 ;
3626}
3627/* END_CASE */
3628
3629/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003630void sign_hash_deterministic(int key_type_arg, data_t *key_data,
3631 int alg_arg, data_t *input_data,
3632 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003633{
Ronald Cron5425a212020-08-04 14:58:35 +02003634 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003635 psa_key_type_t key_type = key_type_arg;
3636 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003637 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003638 unsigned char *signature = NULL;
3639 size_t signature_size;
3640 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003643 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01003644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003645 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3646 psa_set_key_algorithm(&attributes, alg);
3647 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07003648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003649 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3650 &key));
3651 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3652 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01003653
Shaun Case0e7791f2021-12-20 21:14:10 -08003654 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003655 * library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003656 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
3657 key_bits, alg);
3658 TEST_ASSERT(signature_size != 0);
3659 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003660 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003661
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003662 /* Perform the signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003663 PSA_ASSERT(psa_sign_hash(key, alg,
3664 input_data->x, input_data->len,
3665 signature, signature_size,
3666 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003667 /* Verify that the signature is what is expected. */
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003668 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003669 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003670
Gilles Peskine0627f982019-11-26 19:12:16 +01003671#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 memset(signature, 0, signature_size);
Gilles Peskine895242b2019-11-29 12:15:40 +01003673 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 PSA_ASSERT(psa_asymmetric_sign(key, alg,
3675 input_data->x, input_data->len,
3676 signature, signature_size,
3677 &signature_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003678 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003679 signature, signature_length);
Gilles Peskine0627f982019-11-26 19:12:16 +01003680#endif /* MBEDTLS_TEST_DEPRECATED */
3681
Gilles Peskine20035e32018-02-03 22:44:14 +01003682exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003683 /*
3684 * Key attributes may have been returned by psa_get_key_attributes()
3685 * thus reset them as required.
3686 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003687 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003689 psa_destroy_key(key);
3690 mbedtls_free(signature);
3691 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01003692}
3693/* END_CASE */
3694
3695/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003696void sign_hash_fail(int key_type_arg, data_t *key_data,
3697 int alg_arg, data_t *input_data,
3698 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01003699{
Ronald Cron5425a212020-08-04 14:58:35 +02003700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003701 psa_key_type_t key_type = key_type_arg;
3702 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003703 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003704 psa_status_t actual_status;
3705 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003706 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003707 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003708 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003709
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003710 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003712 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01003713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3715 psa_set_key_algorithm(&attributes, alg);
3716 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07003717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003718 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3719 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01003720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721 actual_status = psa_sign_hash(key, alg,
3722 input_data->x, input_data->len,
3723 signature, signature_size,
3724 &signature_length);
3725 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003726 /* The value of *signature_length is unspecified on error, but
3727 * whatever it is, it should be less than signature_size, so that
3728 * if the caller tries to read *signature_length bytes without
3729 * checking the error code then they don't overflow a buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003730 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003731
Gilles Peskine895242b2019-11-29 12:15:40 +01003732#if defined(MBEDTLS_TEST_DEPRECATED)
3733 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734 TEST_EQUAL(psa_asymmetric_sign(key, alg,
3735 input_data->x, input_data->len,
3736 signature, signature_size,
3737 &signature_length),
3738 expected_status);
3739 TEST_LE_U(signature_length, signature_size);
Gilles Peskine895242b2019-11-29 12:15:40 +01003740#endif /* MBEDTLS_TEST_DEPRECATED */
3741
Gilles Peskine20035e32018-02-03 22:44:14 +01003742exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 psa_reset_key_attributes(&attributes);
3744 psa_destroy_key(key);
3745 mbedtls_free(signature);
3746 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01003747}
3748/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003749
3750/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003751void sign_verify_hash(int key_type_arg, data_t *key_data,
3752 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02003753{
Ronald Cron5425a212020-08-04 14:58:35 +02003754 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003755 psa_key_type_t key_type = key_type_arg;
3756 psa_algorithm_t alg = alg_arg;
3757 size_t key_bits;
3758 unsigned char *signature = NULL;
3759 size_t signature_size;
3760 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003763 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02003764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003765 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3766 psa_set_key_algorithm(&attributes, alg);
3767 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02003768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003769 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3770 &key));
3771 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3772 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02003773
Shaun Case0e7791f2021-12-20 21:14:10 -08003774 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02003775 * library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003776 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
3777 key_bits, alg);
3778 TEST_ASSERT(signature_size != 0);
3779 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003780 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02003781
3782 /* Perform the signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003783 PSA_ASSERT(psa_sign_hash(key, alg,
3784 input_data->x, input_data->len,
3785 signature, signature_size,
3786 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003787 /* Check that the signature length looks sensible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003788 TEST_LE_U(signature_length, signature_size);
3789 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02003790
3791 /* Use the library to verify that the signature is correct. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003792 PSA_ASSERT(psa_verify_hash(key, alg,
3793 input_data->x, input_data->len,
3794 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02003797 /* Flip a bit in the input and verify that the signature is now
3798 * detected as invalid. Flip a bit at the beginning, not at the end,
3799 * because ECDSA may ignore the last few bits of the input. */
3800 input_data->x[0] ^= 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003801 TEST_EQUAL(psa_verify_hash(key, alg,
3802 input_data->x, input_data->len,
3803 signature, signature_length),
3804 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02003805 }
3806
3807exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003808 /*
3809 * Key attributes may have been returned by psa_get_key_attributes()
3810 * thus reset them as required.
3811 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003812 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003814 psa_destroy_key(key);
3815 mbedtls_free(signature);
3816 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02003817}
3818/* END_CASE */
3819
3820/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003821void verify_hash(int key_type_arg, data_t *key_data,
3822 int alg_arg, data_t *hash_data,
3823 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03003824{
Ronald Cron5425a212020-08-04 14:58:35 +02003825 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003826 psa_key_type_t key_type = key_type_arg;
3827 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003830 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003832 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03003833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003834 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3835 psa_set_key_algorithm(&attributes, alg);
3836 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03003837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3839 &key));
itayzafrir5c753392018-05-08 11:18:38 +03003840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003841 PSA_ASSERT(psa_verify_hash(key, alg,
3842 hash_data->x, hash_data->len,
3843 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01003844
3845#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003846 PSA_ASSERT(psa_asymmetric_verify(key, alg,
3847 hash_data->x, hash_data->len,
3848 signature_data->x,
3849 signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01003850
3851#endif /* MBEDTLS_TEST_DEPRECATED */
3852
itayzafrir5c753392018-05-08 11:18:38 +03003853exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003854 psa_reset_key_attributes(&attributes);
3855 psa_destroy_key(key);
3856 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03003857}
3858/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003859
3860/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003861void verify_hash_fail(int key_type_arg, data_t *key_data,
3862 int alg_arg, data_t *hash_data,
3863 data_t *signature_data,
3864 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865{
Ronald Cron5425a212020-08-04 14:58:35 +02003866 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003867 psa_key_type_t key_type = key_type_arg;
3868 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003869 psa_status_t actual_status;
3870 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003873 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003875 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3876 psa_set_key_algorithm(&attributes, alg);
3877 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003879 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3880 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 actual_status = psa_verify_hash(key, alg,
3883 hash_data->x, hash_data->len,
3884 signature_data->x, signature_data->len);
3885 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886
Gilles Peskine895242b2019-11-29 12:15:40 +01003887#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003888 TEST_EQUAL(psa_asymmetric_verify(key, alg,
3889 hash_data->x, hash_data->len,
3890 signature_data->x, signature_data->len),
3891 expected_status);
Gilles Peskine895242b2019-11-29 12:15:40 +01003892#endif /* MBEDTLS_TEST_DEPRECATED */
3893
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003894exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003895 psa_reset_key_attributes(&attributes);
3896 psa_destroy_key(key);
3897 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898}
3899/* END_CASE */
3900
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003902void sign_message_deterministic(int key_type_arg,
3903 data_t *key_data,
3904 int alg_arg,
3905 data_t *input_data,
3906 data_t *output_data)
gabor-mezei-armabd72582021-04-15 18:19:50 +02003907{
3908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3909 psa_key_type_t key_type = key_type_arg;
3910 psa_algorithm_t alg = alg_arg;
3911 size_t key_bits;
3912 unsigned char *signature = NULL;
3913 size_t signature_size;
3914 size_t signature_length = 0xdeadbeef;
3915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003917 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armabd72582021-04-15 18:19:50 +02003918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003919 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3920 psa_set_key_algorithm(&attributes, alg);
3921 psa_set_key_type(&attributes, key_type);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003923 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3924 &key));
3925 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3926 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003928 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
3929 TEST_ASSERT(signature_size != 0);
3930 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003931 TEST_CALLOC(signature, signature_size);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003933 PSA_ASSERT(psa_sign_message(key, alg,
3934 input_data->x, input_data->len,
3935 signature, signature_size,
3936 &signature_length));
gabor-mezei-armabd72582021-04-15 18:19:50 +02003937
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003938 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003939 signature, signature_length);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003940
3941exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003942 psa_reset_key_attributes(&attributes);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944 psa_destroy_key(key);
3945 mbedtls_free(signature);
3946 PSA_DONE();
gabor-mezei-armabd72582021-04-15 18:19:50 +02003947
3948}
3949/* END_CASE */
3950
3951/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003952void sign_message_fail(int key_type_arg,
3953 data_t *key_data,
3954 int alg_arg,
3955 data_t *input_data,
3956 int signature_size_arg,
3957 int expected_status_arg)
3958{
3959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3960 psa_key_type_t key_type = key_type_arg;
3961 psa_algorithm_t alg = alg_arg;
3962 size_t signature_size = signature_size_arg;
3963 psa_status_t actual_status;
3964 psa_status_t expected_status = expected_status_arg;
3965 unsigned char *signature = NULL;
3966 size_t signature_length = 0xdeadbeef;
3967 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3968
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003969 TEST_CALLOC(signature, signature_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003970
3971 PSA_ASSERT(psa_crypto_init());
3972
3973 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3974 psa_set_key_algorithm(&attributes, alg);
3975 psa_set_key_type(&attributes, key_type);
3976
3977 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3978 &key));
3979
3980 actual_status = psa_sign_message(key, alg,
3981 input_data->x, input_data->len,
3982 signature, signature_size,
3983 &signature_length);
3984 TEST_EQUAL(actual_status, expected_status);
3985 /* The value of *signature_length is unspecified on error, but
3986 * whatever it is, it should be less than signature_size, so that
3987 * if the caller tries to read *signature_length bytes without
3988 * checking the error code then they don't overflow a buffer. */
3989 TEST_LE_U(signature_length, signature_size);
3990
3991exit:
3992 psa_reset_key_attributes(&attributes);
3993 psa_destroy_key(key);
3994 mbedtls_free(signature);
3995 PSA_DONE();
3996}
3997/* END_CASE */
3998
3999/* BEGIN_CASE */
4000void sign_verify_message(int key_type_arg,
4001 data_t *key_data,
4002 int alg_arg,
4003 data_t *input_data)
4004{
4005 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4006 psa_key_type_t key_type = key_type_arg;
4007 psa_algorithm_t alg = alg_arg;
4008 size_t key_bits;
4009 unsigned char *signature = NULL;
4010 size_t signature_size;
4011 size_t signature_length = 0xdeadbeef;
4012 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4013
4014 PSA_ASSERT(psa_crypto_init());
4015
4016 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4017 PSA_KEY_USAGE_VERIFY_MESSAGE);
4018 psa_set_key_algorithm(&attributes, alg);
4019 psa_set_key_type(&attributes, key_type);
4020
4021 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4022 &key));
4023 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4024 key_bits = psa_get_key_bits(&attributes);
4025
4026 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
4027 TEST_ASSERT(signature_size != 0);
4028 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004029 TEST_CALLOC(signature, signature_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004030
4031 PSA_ASSERT(psa_sign_message(key, alg,
4032 input_data->x, input_data->len,
4033 signature, signature_size,
4034 &signature_length));
4035 TEST_LE_U(signature_length, signature_size);
4036 TEST_ASSERT(signature_length > 0);
4037
4038 PSA_ASSERT(psa_verify_message(key, alg,
4039 input_data->x, input_data->len,
4040 signature, signature_length));
4041
4042 if (input_data->len != 0) {
4043 /* Flip a bit in the input and verify that the signature is now
4044 * detected as invalid. Flip a bit at the beginning, not at the end,
4045 * because ECDSA may ignore the last few bits of the input. */
4046 input_data->x[0] ^= 1;
4047 TEST_EQUAL(psa_verify_message(key, alg,
4048 input_data->x, input_data->len,
4049 signature, signature_length),
4050 PSA_ERROR_INVALID_SIGNATURE);
4051 }
4052
4053exit:
4054 psa_reset_key_attributes(&attributes);
4055
4056 psa_destroy_key(key);
4057 mbedtls_free(signature);
4058 PSA_DONE();
4059}
4060/* END_CASE */
4061
4062/* BEGIN_CASE */
4063void verify_message(int key_type_arg,
4064 data_t *key_data,
4065 int alg_arg,
4066 data_t *input_data,
4067 data_t *signature_data)
4068{
4069 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4070 psa_key_type_t key_type = key_type_arg;
4071 psa_algorithm_t alg = alg_arg;
4072 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4073
4074 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
4075
4076 PSA_ASSERT(psa_crypto_init());
4077
4078 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
4079 psa_set_key_algorithm(&attributes, alg);
4080 psa_set_key_type(&attributes, key_type);
4081
4082 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4083 &key));
4084
4085 PSA_ASSERT(psa_verify_message(key, alg,
4086 input_data->x, input_data->len,
4087 signature_data->x, signature_data->len));
4088
4089exit:
4090 psa_reset_key_attributes(&attributes);
4091 psa_destroy_key(key);
4092 PSA_DONE();
4093}
4094/* END_CASE */
4095
4096/* BEGIN_CASE */
4097void verify_message_fail(int key_type_arg,
4098 data_t *key_data,
4099 int alg_arg,
4100 data_t *hash_data,
4101 data_t *signature_data,
4102 int expected_status_arg)
4103{
4104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4105 psa_key_type_t key_type = key_type_arg;
4106 psa_algorithm_t alg = alg_arg;
4107 psa_status_t actual_status;
4108 psa_status_t expected_status = expected_status_arg;
4109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4110
4111 PSA_ASSERT(psa_crypto_init());
4112
4113 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
4114 psa_set_key_algorithm(&attributes, alg);
4115 psa_set_key_type(&attributes, key_type);
4116
4117 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4118 &key));
4119
4120 actual_status = psa_verify_message(key, alg,
4121 hash_data->x, hash_data->len,
4122 signature_data->x,
4123 signature_data->len);
4124 TEST_EQUAL(actual_status, expected_status);
4125
4126exit:
4127 psa_reset_key_attributes(&attributes);
4128 psa_destroy_key(key);
4129 PSA_DONE();
4130}
4131/* END_CASE */
4132
4133/* BEGIN_CASE */
4134void asymmetric_encrypt(int key_type_arg,
gabor-mezei-armabd72582021-04-15 18:19:50 +02004135 data_t *key_data,
4136 int alg_arg,
4137 data_t *input_data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004138 data_t *label,
4139 int expected_output_length_arg,
4140 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02004141{
Ronald Cron5425a212020-08-04 14:58:35 +02004142 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004143 psa_key_type_t key_type = key_type_arg;
4144 psa_algorithm_t alg = alg_arg;
4145 size_t expected_output_length = expected_output_length_arg;
4146 size_t key_bits;
4147 unsigned char *output = NULL;
4148 size_t output_size;
4149 size_t output_length = ~0;
4150 psa_status_t actual_status;
4151 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004154 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004155
Gilles Peskine656896e2018-06-29 19:12:28 +02004156 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004157 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4158 psa_set_key_algorithm(&attributes, alg);
4159 psa_set_key_type(&attributes, key_type);
4160 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4161 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02004162
4163 /* Determine the maximum output length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004164 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4165 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004167 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4168 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004169 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02004170
4171 /* Encrypt the input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004172 actual_status = psa_asymmetric_encrypt(key, alg,
4173 input_data->x, input_data->len,
4174 label->x, label->len,
4175 output, output_size,
4176 &output_length);
4177 TEST_EQUAL(actual_status, expected_status);
oberon-sk8a23f492023-02-13 13:42:02 +01004178 if (actual_status == PSA_SUCCESS) {
4179 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch6ed14362023-02-22 13:39:21 +01004180 } else {
4181 TEST_LE_U(output_length, output_size);
oberon-sk8a23f492023-02-13 13:42:02 +01004182 }
Gilles Peskine656896e2018-06-29 19:12:28 +02004183
Gilles Peskine68428122018-06-30 18:42:41 +02004184 /* If the label is empty, the test framework puts a non-null pointer
4185 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004186 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004187 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004188 if (output_size != 0) {
4189 memset(output, 0, output_size);
4190 }
4191 actual_status = psa_asymmetric_encrypt(key, alg,
4192 input_data->x, input_data->len,
4193 NULL, label->len,
4194 output, output_size,
4195 &output_length);
4196 TEST_EQUAL(actual_status, expected_status);
oberon-sk8a23f492023-02-13 13:42:02 +01004197 if (actual_status == PSA_SUCCESS) {
4198 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch6ed14362023-02-22 13:39:21 +01004199 } else {
4200 TEST_LE_U(output_length, output_size);
oberon-sk8a23f492023-02-13 13:42:02 +01004201 }
Gilles Peskine68428122018-06-30 18:42:41 +02004202 }
4203
Gilles Peskine656896e2018-06-29 19:12:28 +02004204exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004205 /*
4206 * Key attributes may have been returned by psa_get_key_attributes()
4207 * thus reset them as required.
4208 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004209 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004211 psa_destroy_key(key);
4212 mbedtls_free(output);
4213 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02004214}
4215/* END_CASE */
4216
4217/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004218void asymmetric_encrypt_decrypt(int key_type_arg,
4219 data_t *key_data,
4220 int alg_arg,
4221 data_t *input_data,
4222 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004223{
Ronald Cron5425a212020-08-04 14:58:35 +02004224 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004225 psa_key_type_t key_type = key_type_arg;
4226 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004227 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004228 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004229 size_t output_size;
4230 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004231 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004232 size_t output2_size;
4233 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004236 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004238 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4239 psa_set_key_algorithm(&attributes, alg);
4240 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004242 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4243 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004244
4245 /* Determine the maximum ciphertext length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004246 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4247 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004249 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4250 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004251 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01004252
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004253 output2_size = input_data->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004254 TEST_LE_U(output2_size,
4255 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
4256 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004257 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004258
Gilles Peskineeebd7382018-06-08 18:11:54 +02004259 /* We test encryption by checking that encrypt-then-decrypt gives back
4260 * the original plaintext because of the non-optional random
4261 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004262 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
4263 input_data->x, input_data->len,
4264 label->x, label->len,
4265 output, output_size,
4266 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004267 /* We don't know what ciphertext length to expect, but check that
4268 * it looks sensible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004269 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004271 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4272 output, output_length,
4273 label->x, label->len,
4274 output2, output2_size,
4275 &output2_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004276 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004277 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004278
4279exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004280 /*
4281 * Key attributes may have been returned by psa_get_key_attributes()
4282 * thus reset them as required.
4283 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004284 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 psa_destroy_key(key);
4287 mbedtls_free(output);
4288 mbedtls_free(output2);
4289 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004290}
4291/* END_CASE */
4292
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004293/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004294void asymmetric_decrypt(int key_type_arg,
4295 data_t *key_data,
4296 int alg_arg,
4297 data_t *input_data,
4298 data_t *label,
4299 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004300{
Ronald Cron5425a212020-08-04 14:58:35 +02004301 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004302 psa_key_type_t key_type = key_type_arg;
4303 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004304 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004305 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004306 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004307 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004310 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004312 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4313 psa_set_key_algorithm(&attributes, alg);
4314 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004316 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4317 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004319 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4320 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004321
4322 /* Determine the maximum ciphertext length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004323 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4324 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004325 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01004326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004327 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4328 input_data->x, input_data->len,
4329 label->x, label->len,
4330 output,
4331 output_size,
4332 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004333 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004334 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004335
Gilles Peskine68428122018-06-30 18:42:41 +02004336 /* If the label is empty, the test framework puts a non-null pointer
4337 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004338 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004339 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004340 if (output_size != 0) {
4341 memset(output, 0, output_size);
4342 }
4343 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4344 input_data->x, input_data->len,
4345 NULL, label->len,
4346 output,
4347 output_size,
4348 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004349 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004350 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02004351 }
4352
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004353exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004354 psa_reset_key_attributes(&attributes);
4355 psa_destroy_key(key);
4356 mbedtls_free(output);
4357 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004358}
4359/* END_CASE */
4360
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004361/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004362void asymmetric_decrypt_fail(int key_type_arg,
4363 data_t *key_data,
4364 int alg_arg,
4365 data_t *input_data,
4366 data_t *label,
4367 int output_size_arg,
4368 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004369{
Ronald Cron5425a212020-08-04 14:58:35 +02004370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004371 psa_key_type_t key_type = key_type_arg;
4372 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004373 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004374 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004375 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004376 psa_status_t actual_status;
4377 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004379
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004380 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004384 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4385 psa_set_key_algorithm(&attributes, alg);
4386 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004388 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4389 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004391 actual_status = psa_asymmetric_decrypt(key, alg,
4392 input_data->x, input_data->len,
4393 label->x, label->len,
4394 output, output_size,
4395 &output_length);
4396 TEST_EQUAL(actual_status, expected_status);
4397 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004398
Gilles Peskine68428122018-06-30 18:42:41 +02004399 /* If the label is empty, the test framework puts a non-null pointer
4400 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004401 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004402 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004403 if (output_size != 0) {
4404 memset(output, 0, output_size);
4405 }
4406 actual_status = psa_asymmetric_decrypt(key, alg,
4407 input_data->x, input_data->len,
4408 NULL, label->len,
4409 output, output_size,
4410 &output_length);
4411 TEST_EQUAL(actual_status, expected_status);
4412 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02004413 }
4414
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004415exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004416 psa_reset_key_attributes(&attributes);
4417 psa_destroy_key(key);
4418 mbedtls_free(output);
4419 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004420}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004421/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004422
4423/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004424void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00004425{
4426 /* Test each valid way of initializing the object, except for `= {0}`, as
4427 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4428 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08004429 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004430 size_t capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004431 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004432 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4433 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004435 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00004436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004437 /* A default operation should not be able to report its capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
4439 PSA_ERROR_BAD_STATE);
4440 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
4441 PSA_ERROR_BAD_STATE);
4442 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
4443 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004444
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445 /* A default operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004446 PSA_ASSERT(psa_key_derivation_abort(&func));
4447 PSA_ASSERT(psa_key_derivation_abort(&init));
4448 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00004449}
4450/* END_CASE */
4451
Janos Follath16de4a42019-06-13 16:32:24 +01004452/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004453void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02004454{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004455 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004456 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004457 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004459 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02004460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004461 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
4462 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02004463
4464exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004465 psa_key_derivation_abort(&operation);
4466 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02004467}
4468/* END_CASE */
4469
Janos Follathaf3c2a02019-06-12 12:34:34 +01004470/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004471void derive_set_capacity(int alg_arg, int capacity_arg,
4472 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01004473{
4474 psa_algorithm_t alg = alg_arg;
4475 size_t capacity = capacity_arg;
4476 psa_status_t expected_status = expected_status_arg;
4477 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004479 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01004480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01004482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004483 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
4484 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01004485
4486exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004487 psa_key_derivation_abort(&operation);
4488 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01004489}
4490/* END_CASE */
4491
4492/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004493void derive_input(int alg_arg,
4494 int step_arg1, int key_type_arg1, data_t *input1,
4495 int expected_status_arg1,
4496 int step_arg2, int key_type_arg2, data_t *input2,
4497 int expected_status_arg2,
4498 int step_arg3, int key_type_arg3, data_t *input3,
4499 int expected_status_arg3,
4500 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01004501{
4502 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004503 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
4504 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
4505 psa_status_t expected_statuses[] = { expected_status_arg1,
4506 expected_status_arg2,
4507 expected_status_arg3 };
4508 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02004509 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4510 MBEDTLS_SVC_KEY_ID_INIT,
4511 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004512 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4514 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004515 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004516 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004517 psa_status_t expected_output_status = expected_output_status_arg;
4518 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01004521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4523 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01004524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004525 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01004526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004527 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
4528 mbedtls_test_set_step(i);
4529 if (steps[i] == 0) {
Gilles Peskinef6279312021-05-27 13:21:20 +02004530 /* Skip this step */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004531 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
4532 psa_set_key_type(&attributes, key_types[i]);
4533 PSA_ASSERT(psa_import_key(&attributes,
4534 inputs[i]->x, inputs[i]->len,
4535 &keys[i]));
4536 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
4537 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004538 // When taking a private key as secret input, use key agreement
4539 // to add the shared secret to the derivation
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004540 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
4541 &operation, keys[i]),
4542 expected_statuses[i]);
4543 } else {
4544 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
4545 keys[i]),
4546 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004547 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004548 } else {
4549 TEST_EQUAL(psa_key_derivation_input_bytes(
4550 &operation, steps[i],
4551 inputs[i]->x, inputs[i]->len),
4552 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01004553 }
4554 }
4555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004556 if (output_key_type != PSA_KEY_TYPE_NONE) {
4557 psa_reset_key_attributes(&attributes);
4558 psa_set_key_type(&attributes, output_key_type);
4559 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004560 actual_output_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 psa_key_derivation_output_key(&attributes, &operation,
4562 &output_key);
4563 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004564 uint8_t buffer[1];
4565 actual_output_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004566 psa_key_derivation_output_bytes(&operation,
4567 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004568 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004569 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004570
Janos Follathaf3c2a02019-06-12 12:34:34 +01004571exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004572 psa_key_derivation_abort(&operation);
4573 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
4574 psa_destroy_key(keys[i]);
4575 }
4576 psa_destroy_key(output_key);
4577 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01004578}
4579/* END_CASE */
4580
Janos Follathd958bb72019-07-03 15:02:16 +01004581/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004582void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004583{
Janos Follathd958bb72019-07-03 15:02:16 +01004584 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004585 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004586 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004588 unsigned char input1[] = "Input 1";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004589 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01004590 unsigned char input2[] = "Input 2";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004591 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004592 uint8_t buffer[42];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004593 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004594 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4595 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004596 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004597 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004599 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004601 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4602 psa_set_key_algorithm(&attributes, alg);
4603 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004605 PSA_ASSERT(psa_import_key(&attributes,
4606 key_data, sizeof(key_data),
4607 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004608
4609 /* valid key derivation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004610 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
4611 input1, input1_length,
4612 input2, input2_length,
4613 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01004614 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004615 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004616
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004617 /* state of operation shouldn't allow additional generation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004618 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
4619 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004621 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
4624 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004625
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004626exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004627 psa_key_derivation_abort(&operation);
4628 psa_destroy_key(key);
4629 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004630}
4631/* END_CASE */
4632
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004633/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004634void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004635{
4636 uint8_t output_buffer[16];
4637 size_t buffer_size = 16;
4638 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004641 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
4642 output_buffer, buffer_size)
4643 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004645 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
4646 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004648 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004650 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
4651 output_buffer, buffer_size)
4652 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004654 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
4655 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004656
4657exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004659}
4660/* END_CASE */
4661
4662/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004663void derive_output(int alg_arg,
4664 int step1_arg, data_t *input1,
4665 int step2_arg, data_t *input2,
4666 int step3_arg, data_t *input3,
4667 int requested_capacity_arg,
4668 data_t *expected_output1,
4669 data_t *expected_output2)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004670{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004671 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004672 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg };
4673 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02004674 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4675 MBEDTLS_SVC_KEY_ID_INIT,
4676 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004677 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004678 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004679 uint8_t *expected_outputs[2] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004680 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004681 size_t output_sizes[2] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004682 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004683 size_t output_buffer_size = 0;
4684 uint8_t *output_buffer = NULL;
4685 size_t expected_capacity;
4686 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004687 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004688 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004689 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004691 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
4692 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004693 output_buffer_size = output_sizes[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004694 }
4695 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004696 expected_outputs[i] = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004697 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004698 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004699 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004700 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004702 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4703 psa_set_key_algorithm(&attributes, alg);
4704 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004705
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004706 /* Extraction phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004707 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
4708 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
4709 requested_capacity));
4710 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
4711 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02004712 case 0:
4713 break;
4714 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004715 PSA_ASSERT(psa_import_key(&attributes,
4716 inputs[i]->x, inputs[i]->len,
4717 &keys[i]));
gabor-mezei-armceface22021-01-21 12:26:17 +01004718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004719 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
4720 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes));
4721 TEST_ASSERT(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes)) <=
4722 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
gabor-mezei-armceface22021-01-21 12:26:17 +01004723 }
4724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004725 PSA_ASSERT(psa_key_derivation_input_key(
4726 &operation, steps[i], keys[i]));
Gilles Peskine1468da72019-05-29 17:35:49 +02004727 break;
4728 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004729 PSA_ASSERT(psa_key_derivation_input_bytes(
4730 &operation, steps[i],
4731 inputs[i]->x, inputs[i]->len));
Gilles Peskine1468da72019-05-29 17:35:49 +02004732 break;
4733 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004734 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004736 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4737 &current_capacity));
4738 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004739 expected_capacity = requested_capacity;
4740
4741 /* Expansion phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004742 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004743 /* Read some bytes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004744 status = psa_key_derivation_output_bytes(&operation,
4745 output_buffer, output_sizes[i]);
4746 if (expected_capacity == 0 && output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004747 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004748 TEST_ASSERT(status == PSA_SUCCESS ||
4749 status == PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004750 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751 } else if (expected_capacity == 0 ||
4752 output_sizes[i] > expected_capacity) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004753 /* Capacity exceeded. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004754 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004755 expected_capacity = 0;
4756 continue;
4757 }
4758 /* Success. Check the read data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004759 PSA_ASSERT(status);
4760 if (output_sizes[i] != 0) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004761 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004762 expected_outputs[i], output_sizes[i]);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004763 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004764 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004765 expected_capacity -= output_sizes[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004766 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4767 &current_capacity));
4768 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004769 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004770 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004771
4772exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773 mbedtls_free(output_buffer);
4774 psa_key_derivation_abort(&operation);
4775 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
4776 psa_destroy_key(keys[i]);
4777 }
4778 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004779}
4780/* END_CASE */
4781
4782/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004783void derive_full(int alg_arg,
4784 data_t *key_data,
4785 data_t *input1,
4786 data_t *input2,
4787 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02004788{
Ronald Cron5425a212020-08-04 14:58:35 +02004789 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004790 psa_algorithm_t alg = alg_arg;
4791 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004792 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004793 unsigned char output_buffer[16];
4794 size_t expected_capacity = requested_capacity;
4795 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004798 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02004799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004800 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4801 psa_set_key_algorithm(&attributes, alg);
4802 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02004803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004804 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4805 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02004806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004807 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
4808 input1->x, input1->len,
4809 input2->x, input2->len,
4810 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01004811 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004812 }
Janos Follath47f27ed2019-06-25 13:24:52 +01004813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004814 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4815 &current_capacity));
4816 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02004817
4818 /* Expansion phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004819 while (current_capacity > 0) {
4820 size_t read_size = sizeof(output_buffer);
4821 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02004822 read_size = current_capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004823 }
4824 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
4825 output_buffer,
4826 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02004827 expected_capacity -= read_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004828 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4829 &current_capacity));
4830 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02004831 }
4832
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833 /* Check that the operation refuses to go over capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004834 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
4835 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02004836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004837 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02004838
4839exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004840 psa_key_derivation_abort(&operation);
4841 psa_destroy_key(key);
4842 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02004843}
4844/* END_CASE */
4845
Janos Follathe60c9052019-07-03 13:51:30 +01004846/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004847void derive_key_exercise(int alg_arg,
4848 data_t *key_data,
4849 data_t *input1,
4850 data_t *input2,
4851 int derived_type_arg,
4852 int derived_bits_arg,
4853 int derived_usage_arg,
4854 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02004855{
Ronald Cron5425a212020-08-04 14:58:35 +02004856 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4857 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004858 psa_algorithm_t alg = alg_arg;
4859 psa_key_type_t derived_type = derived_type_arg;
4860 size_t derived_bits = derived_bits_arg;
4861 psa_key_usage_t derived_usage = derived_usage_arg;
4862 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004863 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004866 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004868 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02004869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004870 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4871 psa_set_key_algorithm(&attributes, alg);
4872 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
4873 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4874 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004875
4876 /* Derive a key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004877 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4878 input1->x, input1->len,
4879 input2->x, input2->len,
4880 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01004881 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004882 }
Janos Follathe60c9052019-07-03 13:51:30 +01004883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004884 psa_set_key_usage_flags(&attributes, derived_usage);
4885 psa_set_key_algorithm(&attributes, derived_alg);
4886 psa_set_key_type(&attributes, derived_type);
4887 psa_set_key_bits(&attributes, derived_bits);
4888 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
4889 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004890
4891 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004892 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
4893 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
4894 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004895
4896 /* Exercise the derived key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004897 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02004898 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004899 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02004900
4901exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004902 /*
4903 * Key attributes may have been returned by psa_get_key_attributes()
4904 * thus reset them as required.
4905 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004906 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004908 psa_key_derivation_abort(&operation);
4909 psa_destroy_key(base_key);
4910 psa_destroy_key(derived_key);
4911 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02004912}
4913/* END_CASE */
4914
Janos Follath42fd8882019-07-03 14:17:09 +01004915/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004916void derive_key_export(int alg_arg,
4917 data_t *key_data,
4918 data_t *input1,
4919 data_t *input2,
4920 int bytes1_arg,
4921 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02004922{
Ronald Cron5425a212020-08-04 14:58:35 +02004923 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4924 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004925 psa_algorithm_t alg = alg_arg;
4926 size_t bytes1 = bytes1_arg;
4927 size_t bytes2 = bytes2_arg;
4928 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004929 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004930 uint8_t *output_buffer = NULL;
4931 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004932 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4933 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004934 size_t length;
4935
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004936 TEST_CALLOC(output_buffer, capacity);
4937 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004938 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02004939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004940 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
4941 psa_set_key_algorithm(&base_attributes, alg);
4942 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
4943 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
4944 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004945
4946 /* Derive some material and output it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004947 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4948 input1->x, input1->len,
4949 input2->x, input2->len,
4950 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01004951 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004952 }
Janos Follath42fd8882019-07-03 14:17:09 +01004953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004954 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
4955 output_buffer,
4956 capacity));
4957 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004958
4959 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004960 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4961 input1->x, input1->len,
4962 input2->x, input2->len,
4963 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01004964 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004965 }
Janos Follath42fd8882019-07-03 14:17:09 +01004966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004967 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
4968 psa_set_key_algorithm(&derived_attributes, 0);
4969 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
4970 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
4971 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
4972 &derived_key));
4973 PSA_ASSERT(psa_export_key(derived_key,
4974 export_buffer, bytes1,
4975 &length));
4976 TEST_EQUAL(length, bytes1);
4977 PSA_ASSERT(psa_destroy_key(derived_key));
4978 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
4979 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
4980 &derived_key));
4981 PSA_ASSERT(psa_export_key(derived_key,
4982 export_buffer + bytes1, bytes2,
4983 &length));
4984 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004985
4986 /* Compare the outputs from the two runs. */
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004987 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004988 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004989
4990exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004991 mbedtls_free(output_buffer);
4992 mbedtls_free(export_buffer);
4993 psa_key_derivation_abort(&operation);
4994 psa_destroy_key(base_key);
4995 psa_destroy_key(derived_key);
4996 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02004997}
4998/* END_CASE */
4999
5000/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005001void derive_key(int alg_arg,
5002 data_t *key_data, data_t *input1, data_t *input2,
5003 int type_arg, int bits_arg,
5004 int expected_status_arg,
5005 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02005006{
Ronald Cron5425a212020-08-04 14:58:35 +02005007 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5008 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005009 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005010 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005011 size_t bits = bits_arg;
5012 psa_status_t expected_status = expected_status_arg;
5013 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5014 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5015 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005017 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02005018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
5020 psa_set_key_algorithm(&base_attributes, alg);
5021 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
5022 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
5023 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02005024
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005025 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
5026 input1->x, input1->len,
5027 input2->x, input2->len,
5028 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02005029 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005030 }
Gilles Peskinec744d992019-07-30 17:26:54 +02005031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005032 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
5033 psa_set_key_algorithm(&derived_attributes, 0);
5034 psa_set_key_type(&derived_attributes, type);
5035 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01005036
5037 psa_status_t status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005038 psa_key_derivation_output_key(&derived_attributes,
5039 &operation,
5040 &derived_key);
5041 if (is_large_output > 0) {
5042 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
5043 }
5044 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02005045
5046exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005047 psa_key_derivation_abort(&operation);
5048 psa_destroy_key(base_key);
5049 psa_destroy_key(derived_key);
5050 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02005051}
5052/* END_CASE */
5053
5054/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055void key_agreement_setup(int alg_arg,
5056 int our_key_type_arg, int our_key_alg_arg,
5057 data_t *our_key_data, data_t *peer_key_data,
5058 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005059{
Ronald Cron5425a212020-08-04 14:58:35 +02005060 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005061 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005062 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005063 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005065 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005066 psa_status_t expected_status = expected_status_arg;
5067 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005069 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02005070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005071 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5072 psa_set_key_algorithm(&attributes, our_key_alg);
5073 psa_set_key_type(&attributes, our_key_type);
5074 PSA_ASSERT(psa_import_key(&attributes,
5075 our_key_data->x, our_key_data->len,
5076 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02005077
Gilles Peskine77f40d82019-04-11 21:27:06 +02005078 /* The tests currently include inputs that should fail at either step.
5079 * Test cases that fail at the setup step should be changed to call
5080 * key_derivation_setup instead, and this function should be renamed
5081 * to key_agreement_fail. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005082 status = psa_key_derivation_setup(&operation, alg);
5083 if (status == PSA_SUCCESS) {
5084 TEST_EQUAL(psa_key_derivation_key_agreement(
5085 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5086 our_key,
5087 peer_key_data->x, peer_key_data->len),
5088 expected_status);
5089 } else {
5090 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02005091 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005092
5093exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005094 psa_key_derivation_abort(&operation);
5095 psa_destroy_key(our_key);
5096 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02005097}
5098/* END_CASE */
5099
5100/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005101void raw_key_agreement(int alg_arg,
5102 int our_key_type_arg, data_t *our_key_data,
5103 data_t *peer_key_data,
5104 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02005105{
Ronald Cron5425a212020-08-04 14:58:35 +02005106 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005107 psa_algorithm_t alg = alg_arg;
5108 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005110 unsigned char *output = NULL;
5111 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005112 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005114 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02005115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005116 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5117 psa_set_key_algorithm(&attributes, alg);
5118 psa_set_key_type(&attributes, our_key_type);
5119 PSA_ASSERT(psa_import_key(&attributes,
5120 our_key_data->x, our_key_data->len,
5121 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02005122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005123 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
5124 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01005125
Gilles Peskine7d150292022-04-13 23:25:52 +02005126 /* Validate size macros */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005127 TEST_LE_U(expected_output->len,
5128 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
5129 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
5130 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine7d150292022-04-13 23:25:52 +02005131
5132 /* Good case with exact output size */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005133 TEST_CALLOC(output, expected_output->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005134 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
5135 peer_key_data->x, peer_key_data->len,
5136 output, expected_output->len,
5137 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005138 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005139 expected_output->x, expected_output->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005140 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005141 output = NULL;
5142 output_length = ~0;
5143
5144 /* Larger buffer */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005145 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
5147 peer_key_data->x, peer_key_data->len,
5148 output, expected_output->len + 1,
5149 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005150 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005151 expected_output->x, expected_output->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005152 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005153 output = NULL;
5154 output_length = ~0;
5155
5156 /* Buffer too small */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005157 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005158 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
5159 peer_key_data->x, peer_key_data->len,
5160 output, expected_output->len - 1,
5161 &output_length),
5162 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine7d150292022-04-13 23:25:52 +02005163 /* Not required by the spec, but good robustness */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005164 TEST_LE_U(output_length, expected_output->len - 1);
5165 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005166 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005167
5168exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 mbedtls_free(output);
5170 psa_destroy_key(our_key);
5171 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02005172}
5173/* END_CASE */
5174
5175/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005176void key_agreement_capacity(int alg_arg,
5177 int our_key_type_arg, data_t *our_key_data,
5178 data_t *peer_key_data,
5179 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02005180{
Ronald Cron5425a212020-08-04 14:58:35 +02005181 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005182 psa_algorithm_t alg = alg_arg;
5183 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005184 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005186 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005187 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005189 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02005190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005191 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5192 psa_set_key_algorithm(&attributes, alg);
5193 psa_set_key_type(&attributes, our_key_type);
5194 PSA_ASSERT(psa_import_key(&attributes,
5195 our_key_data->x, our_key_data->len,
5196 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02005197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005198 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
5199 PSA_ASSERT(psa_key_derivation_key_agreement(
5200 &operation,
5201 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5202 peer_key_data->x, peer_key_data->len));
5203 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005204 /* The test data is for info="" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005205 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
5206 PSA_KEY_DERIVATION_INPUT_INFO,
5207 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005208 }
Gilles Peskine59685592018-09-18 12:11:34 +02005209
Shaun Case0e7791f2021-12-20 21:14:10 -08005210 /* Test the advertised capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005211 PSA_ASSERT(psa_key_derivation_get_capacity(
5212 &operation, &actual_capacity));
5213 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02005214
Gilles Peskinebf491972018-10-25 22:36:12 +02005215 /* Test the actual capacity by reading the output. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005216 while (actual_capacity > sizeof(output)) {
5217 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5218 output, sizeof(output)));
5219 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02005220 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005221 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5222 output, actual_capacity));
5223 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
5224 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02005225
Gilles Peskine59685592018-09-18 12:11:34 +02005226exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005227 psa_key_derivation_abort(&operation);
5228 psa_destroy_key(our_key);
5229 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02005230}
5231/* END_CASE */
5232
5233/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005234void key_agreement_output(int alg_arg,
5235 int our_key_type_arg, data_t *our_key_data,
5236 data_t *peer_key_data,
5237 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02005238{
Ronald Cron5425a212020-08-04 14:58:35 +02005239 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005240 psa_algorithm_t alg = alg_arg;
5241 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005244 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005245
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005246 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005247 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02005248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005249 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02005250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005251 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5252 psa_set_key_algorithm(&attributes, alg);
5253 psa_set_key_type(&attributes, our_key_type);
5254 PSA_ASSERT(psa_import_key(&attributes,
5255 our_key_data->x, our_key_data->len,
5256 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02005257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005258 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
5259 PSA_ASSERT(psa_key_derivation_key_agreement(
5260 &operation,
5261 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5262 peer_key_data->x, peer_key_data->len));
5263 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005264 /* The test data is for info="" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005265 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
5266 PSA_KEY_DERIVATION_INPUT_INFO,
5267 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005268 }
Gilles Peskine59685592018-09-18 12:11:34 +02005269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005270 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5271 actual_output,
5272 expected_output1->len));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005273 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005274 expected_output1->x, expected_output1->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005275 if (expected_output2->len != 0) {
5276 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5277 actual_output,
5278 expected_output2->len));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005279 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005280 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005281 }
Gilles Peskine59685592018-09-18 12:11:34 +02005282
5283exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005284 psa_key_derivation_abort(&operation);
5285 psa_destroy_key(our_key);
5286 PSA_DONE();
5287 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02005288}
5289/* END_CASE */
5290
5291/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005292void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02005293{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005294 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005295 unsigned char *output = NULL;
5296 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005297 size_t i;
5298 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005300 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00005301
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005302 TEST_CALLOC(output, bytes);
5303 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02005304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005305 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02005306
Gilles Peskinea50d7392018-06-21 10:22:13 +02005307 /* Run several times, to ensure that every output byte will be
5308 * nonzero at least once with overwhelming probability
5309 * (2^(-8*number_of_runs)). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005310 for (run = 0; run < 10; run++) {
5311 if (bytes != 0) {
5312 memset(output, 0, bytes);
5313 }
5314 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02005315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005316 for (i = 0; i < bytes; i++) {
5317 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02005318 ++changed[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005319 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005320 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005321 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005322
5323 /* Check that every byte was changed to nonzero at least once. This
5324 * validates that psa_generate_random is overwriting every byte of
5325 * the output buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005326 for (i = 0; i < bytes; i++) {
5327 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02005328 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005329
5330exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005331 PSA_DONE();
5332 mbedtls_free(output);
5333 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02005334}
5335/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005336
5337/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005338void generate_key(int type_arg,
5339 int bits_arg,
5340 int usage_arg,
5341 int alg_arg,
5342 int expected_status_arg,
5343 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005344{
Ronald Cron5425a212020-08-04 14:58:35 +02005345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005346 psa_key_type_t type = type_arg;
5347 psa_key_usage_t usage = usage_arg;
5348 size_t bits = bits_arg;
5349 psa_algorithm_t alg = alg_arg;
5350 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005352 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005354 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02005355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005356 psa_set_key_usage_flags(&attributes, usage);
5357 psa_set_key_algorithm(&attributes, alg);
5358 psa_set_key_type(&attributes, type);
5359 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005360
5361 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005362 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01005363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005364 if (is_large_key > 0) {
5365 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
5366 }
5367 TEST_EQUAL(status, expected_status);
5368 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005369 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005370 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005371
5372 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005373 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
5374 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
5375 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005376
Gilles Peskine818ca122018-06-20 18:16:48 +02005377 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005378 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02005379 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005380 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005381
5382exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005383 /*
5384 * Key attributes may have been returned by psa_get_key_attributes()
5385 * thus reset them as required.
5386 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005387 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 psa_destroy_key(key);
5390 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02005391}
5392/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005393
Ronald Cronee414c72021-03-18 18:50:08 +01005394/* 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 +01005395void generate_key_rsa(int bits_arg,
5396 data_t *e_arg,
5397 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02005398{
Ronald Cron5425a212020-08-04 14:58:35 +02005399 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005400 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005401 size_t bits = bits_arg;
5402 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5403 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5404 psa_status_t expected_status = expected_status_arg;
5405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5406 uint8_t *exported = NULL;
5407 size_t exported_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005408 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005409 size_t exported_length = SIZE_MAX;
5410 uint8_t *e_read_buffer = NULL;
5411 int is_default_public_exponent = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005412 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005413 size_t e_read_length = SIZE_MAX;
5414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005415 if (e_arg->len == 0 ||
5416 (e_arg->len == 3 &&
5417 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005418 is_default_public_exponent = 1;
5419 e_read_size = 0;
5420 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005421 TEST_CALLOC(e_read_buffer, e_read_size);
5422 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005424 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02005425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005426 psa_set_key_usage_flags(&attributes, usage);
5427 psa_set_key_algorithm(&attributes, alg);
5428 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
5429 e_arg->x, e_arg->len));
5430 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005431
5432 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005433 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
5434 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005435 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005436 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005437
5438 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005439 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5440 TEST_EQUAL(psa_get_key_type(&attributes), type);
5441 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
5442 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
5443 e_read_buffer, e_read_size,
5444 &e_read_length));
5445 if (is_default_public_exponent) {
5446 TEST_EQUAL(e_read_length, 0);
5447 } else {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005448 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005449 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005450
5451 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005453 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005454 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005455
5456 /* Export the key and check the public exponent. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005457 PSA_ASSERT(psa_export_public_key(key,
5458 exported, exported_size,
5459 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02005460 {
5461 uint8_t *p = exported;
5462 uint8_t *end = exported + exported_length;
5463 size_t len;
5464 /* RSAPublicKey ::= SEQUENCE {
5465 * modulus INTEGER, -- n
5466 * publicExponent INTEGER } -- e
5467 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005468 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
5469 MBEDTLS_ASN1_SEQUENCE |
5470 MBEDTLS_ASN1_CONSTRUCTED));
5471 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
5472 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
5473 MBEDTLS_ASN1_INTEGER));
5474 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005475 ++p;
5476 --len;
5477 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 if (e_arg->len == 0) {
5479 TEST_EQUAL(len, 3);
5480 TEST_EQUAL(p[0], 1);
5481 TEST_EQUAL(p[1], 0);
5482 TEST_EQUAL(p[2], 1);
5483 } else {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005484 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005485 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005486 }
5487
5488exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005489 /*
5490 * Key attributes may have been returned by psa_get_key_attributes() or
5491 * set by psa_set_key_domain_parameters() thus reset them as required.
5492 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005493 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 psa_destroy_key(key);
5496 PSA_DONE();
5497 mbedtls_free(e_read_buffer);
5498 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005499}
5500/* END_CASE */
5501
Darryl Greend49a4992018-06-18 17:27:26 +01005502/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005503void persistent_key_load_key_from_storage(data_t *data,
5504 int type_arg, int bits_arg,
5505 int usage_flags_arg, int alg_arg,
5506 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01005507{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005508 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005509 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5511 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005512 psa_key_type_t type = type_arg;
5513 size_t bits = bits_arg;
5514 psa_key_usage_t usage_flags = usage_flags_arg;
5515 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005516 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005517 unsigned char *first_export = NULL;
5518 unsigned char *second_export = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005519 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01005520 size_t first_exported_length;
5521 size_t second_exported_length;
5522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005523 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005524 TEST_CALLOC(first_export, export_size);
5525 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005526 }
Darryl Greend49a4992018-06-18 17:27:26 +01005527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005528 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01005529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005530 psa_set_key_id(&attributes, key_id);
5531 psa_set_key_usage_flags(&attributes, usage_flags);
5532 psa_set_key_algorithm(&attributes, alg);
5533 psa_set_key_type(&attributes, type);
5534 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01005535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005536 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00005537 case IMPORT_KEY:
5538 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005539 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
5540 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00005541 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005542
Darryl Green0c6575a2018-11-07 16:05:30 +00005543 case GENERATE_KEY:
5544 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005545 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00005546 break;
5547
5548 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005549#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005550 {
5551 /* Create base key */
5552 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
5553 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5554 psa_set_key_usage_flags(&base_attributes,
5555 PSA_KEY_USAGE_DERIVE);
5556 psa_set_key_algorithm(&base_attributes, derive_alg);
5557 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
5558 PSA_ASSERT(psa_import_key(&base_attributes,
5559 data->x, data->len,
5560 &base_key));
5561 /* Derive a key. */
5562 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
5563 PSA_ASSERT(psa_key_derivation_input_key(
5564 &operation,
5565 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
5566 PSA_ASSERT(psa_key_derivation_input_bytes(
5567 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
5568 NULL, 0));
5569 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
5570 &operation,
5571 &key));
5572 PSA_ASSERT(psa_key_derivation_abort(&operation));
5573 PSA_ASSERT(psa_destroy_key(base_key));
5574 base_key = MBEDTLS_SVC_KEY_ID_INIT;
5575 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005576#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005577 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005578#endif
5579 break;
5580
5581 default:
Agathiyan Bragadeesh27e29892023-07-14 17:28:27 +01005582 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005583 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005584 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005585 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01005586
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005587 /* Export the key if permitted by the key policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005588 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
5589 PSA_ASSERT(psa_export_key(key,
5590 first_export, export_size,
5591 &first_exported_length));
5592 if (generation_method == IMPORT_KEY) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005593 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005594 first_export, first_exported_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005595 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005596 }
Darryl Greend49a4992018-06-18 17:27:26 +01005597
5598 /* Shutdown and restart */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005599 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005600 PSA_DONE();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005601 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01005602
Darryl Greend49a4992018-06-18 17:27:26 +01005603 /* Check key slot still contains key data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005604 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5605 TEST_ASSERT(mbedtls_svc_key_id_equal(
5606 psa_get_key_id(&attributes), key_id));
5607 TEST_EQUAL(psa_get_key_lifetime(&attributes),
5608 PSA_KEY_LIFETIME_PERSISTENT);
5609 TEST_EQUAL(psa_get_key_type(&attributes), type);
5610 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
5611 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
5612 mbedtls_test_update_key_usage_flags(usage_flags));
5613 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01005614
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005615 /* Export the key again if permitted by the key policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005616 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
5617 PSA_ASSERT(psa_export_key(key,
5618 second_export, export_size,
5619 &second_exported_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005620 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005621 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00005622 }
5623
5624 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005625 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00005626 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005627 }
Darryl Greend49a4992018-06-18 17:27:26 +01005628
5629exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005630 /*
5631 * Key attributes may have been returned by psa_get_key_attributes()
5632 * thus reset them as required.
5633 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005634 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005636 mbedtls_free(first_export);
5637 mbedtls_free(second_export);
5638 psa_key_derivation_abort(&operation);
5639 psa_destroy_key(base_key);
5640 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005641 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005642}
5643/* END_CASE */