blob: 02b30d22255a01c8d01af5a5b1d0d858d0035f8e [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,
David Horstmann72745902024-02-06 17:03:13 +00003294 output1 + output1_length,
3295 output1_buffer_size - output1_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003296 &function_output_length));
3297 TEST_LE_U(function_output_length,
3298 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3299 alg,
3300 input->len - first_part_size));
3301 TEST_LE_U(function_output_length,
3302 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003303 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003305 PSA_ASSERT(psa_cipher_finish(&operation1,
3306 output1 + output1_length,
3307 output1_buffer_size - output1_length,
3308 &function_output_length));
3309 TEST_LE_U(function_output_length,
3310 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3311 TEST_LE_U(function_output_length,
3312 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02003313 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003315 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02003316
Gilles Peskine048b7f02018-06-08 14:20:49 +02003317 output2_buffer_size = output1_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003318 TEST_LE_U(output2_buffer_size,
3319 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
3320 TEST_LE_U(output2_buffer_size,
3321 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003322 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02003323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003324 if (iv_length > 0) {
3325 PSA_ASSERT(psa_cipher_set_iv(&operation2,
3326 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003327 }
Moran Pekerded84402018-06-06 16:36:50 +03003328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
3330 output2, output2_buffer_size,
3331 &function_output_length));
3332 TEST_LE_U(function_output_length,
3333 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
3334 TEST_LE_U(function_output_length,
3335 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003336 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003338 PSA_ASSERT(psa_cipher_update(&operation2,
3339 output1 + first_part_size,
3340 output1_length - first_part_size,
David Horstmann72745902024-02-06 17:03:13 +00003341 output2 + output2_length,
3342 output2_buffer_size - output2_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 &function_output_length));
3344 TEST_LE_U(function_output_length,
3345 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3346 alg,
3347 output1_length - first_part_size));
3348 TEST_LE_U(function_output_length,
3349 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003350 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003352 PSA_ASSERT(psa_cipher_finish(&operation2,
3353 output2 + output2_length,
3354 output2_buffer_size - output2_length,
3355 &function_output_length));
3356 TEST_LE_U(function_output_length,
3357 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3358 TEST_LE_U(function_output_length,
3359 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02003360 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003362 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02003363
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003364 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02003365
3366exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003367 psa_cipher_abort(&operation1);
3368 psa_cipher_abort(&operation2);
3369 mbedtls_free(output1);
3370 mbedtls_free(output2);
3371 psa_destroy_key(key);
3372 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02003373}
3374/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003375
Gilles Peskine20035e32018-02-03 22:44:14 +01003376/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003377void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
3378 int alg_arg,
3379 data_t *nonce,
3380 data_t *additional_data,
3381 data_t *input_data,
3382 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383{
Ronald Cron5425a212020-08-04 14:58:35 +02003384 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385 psa_key_type_t key_type = key_type_arg;
3386 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003387 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388 unsigned char *output_data = NULL;
3389 size_t output_size = 0;
3390 size_t output_length = 0;
3391 unsigned char *output_data2 = NULL;
3392 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003393 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003394 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003397 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003399 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3400 psa_set_key_algorithm(&attributes, alg);
3401 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003403 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3404 &key));
3405 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3406 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003408 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3409 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003410 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3411 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003412 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3413 expected_result != PSA_ERROR_NOT_SUPPORTED) {
3414 TEST_EQUAL(output_size,
3415 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3416 TEST_ASSERT(output_size <=
3417 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01003418 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003419 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003421 status = psa_aead_encrypt(key, alg,
3422 nonce->x, nonce->len,
3423 additional_data->x,
3424 additional_data->len,
3425 input_data->x, input_data->len,
3426 output_data, output_size,
3427 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01003428
3429 /* If the operation is not supported, just skip and not fail in case the
3430 * encryption involves a common limitation of cryptography hardwares and
3431 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003432 if (status == PSA_ERROR_NOT_SUPPORTED) {
3433 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3434 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01003435 }
3436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003437 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003439 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003440 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003441
Gilles Peskine003a4a92019-05-14 16:09:40 +02003442 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3443 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003444 TEST_EQUAL(input_data->len,
3445 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02003446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003447 TEST_ASSERT(input_data->len <=
3448 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01003449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 TEST_EQUAL(psa_aead_decrypt(key, alg,
3451 nonce->x, nonce->len,
3452 additional_data->x,
3453 additional_data->len,
3454 output_data, output_length,
3455 output_data2, output_length,
3456 &output_length2),
3457 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02003458
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003459 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003460 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003462
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003464 psa_destroy_key(key);
3465 mbedtls_free(output_data);
3466 mbedtls_free(output_data2);
3467 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003468}
3469/* END_CASE */
3470
3471/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003472void aead_encrypt(int key_type_arg, data_t *key_data,
3473 int alg_arg,
3474 data_t *nonce,
3475 data_t *additional_data,
3476 data_t *input_data,
3477 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478{
Ronald Cron5425a212020-08-04 14:58:35 +02003479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003480 psa_key_type_t key_type = key_type_arg;
3481 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003482 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003483 unsigned char *output_data = NULL;
3484 size_t output_size = 0;
3485 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003487 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003489 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003491 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3492 psa_set_key_algorithm(&attributes, alg);
3493 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003495 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3496 &key));
3497 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3498 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3501 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003502 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3503 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003504 TEST_EQUAL(output_size,
3505 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3506 TEST_ASSERT(output_size <=
3507 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003508 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003510 status = psa_aead_encrypt(key, alg,
3511 nonce->x, nonce->len,
3512 additional_data->x, additional_data->len,
3513 input_data->x, input_data->len,
3514 output_data, output_size,
3515 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003516
Ronald Cron28a45ed2021-02-09 20:35:42 +01003517 /* If the operation is not supported, just skip and not fail in case the
3518 * encryption involves a common limitation of cryptography hardwares and
3519 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003520 if (status == PSA_ERROR_NOT_SUPPORTED) {
3521 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3522 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01003523 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003525 PSA_ASSERT(status);
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003526 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003527 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003528
Gilles Peskinea1cac842018-06-11 19:33:02 +02003529exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003530 psa_destroy_key(key);
3531 mbedtls_free(output_data);
3532 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003533}
3534/* END_CASE */
3535
3536/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003537void aead_decrypt(int key_type_arg, data_t *key_data,
3538 int alg_arg,
3539 data_t *nonce,
3540 data_t *additional_data,
3541 data_t *input_data,
3542 data_t *expected_data,
3543 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003544{
Ronald Cron5425a212020-08-04 14:58:35 +02003545 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003546 psa_key_type_t key_type = key_type_arg;
3547 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003548 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003549 unsigned char *output_data = NULL;
3550 size_t output_size = 0;
3551 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003552 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003553 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003554 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003556 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003558 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3559 psa_set_key_algorithm(&attributes, alg);
3560 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003562 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3563 &key));
3564 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3565 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003567 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3568 alg);
3569 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3570 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01003571 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3572 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573 TEST_EQUAL(output_size,
3574 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3575 TEST_ASSERT(output_size <=
3576 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01003577 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003578 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003580 status = psa_aead_decrypt(key, alg,
3581 nonce->x, nonce->len,
3582 additional_data->x,
3583 additional_data->len,
3584 input_data->x, input_data->len,
3585 output_data, output_size,
3586 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01003587
Ronald Cron28a45ed2021-02-09 20:35:42 +01003588 /* If the operation is not supported, just skip and not fail in case the
3589 * decryption involves a common limitation of cryptography hardwares and
3590 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003591 if (status == PSA_ERROR_NOT_SUPPORTED) {
3592 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3593 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01003594 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003598 if (expected_result == PSA_SUCCESS) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003599 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003600 output_data, output_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003601 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02003602
Gilles Peskinea1cac842018-06-11 19:33:02 +02003603exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003604 psa_destroy_key(key);
3605 mbedtls_free(output_data);
3606 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003607}
3608/* END_CASE */
3609
3610/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003611void signature_size(int type_arg,
3612 int bits,
3613 int alg_arg,
3614 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003615{
3616 psa_key_type_t type = type_arg;
3617 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003618 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01003619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003620 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01003621#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003622 TEST_EQUAL(actual_size,
3623 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg));
Gilles Peskine841b14b2019-11-26 17:37:37 +01003624#endif /* MBEDTLS_TEST_DEPRECATED */
3625
Gilles Peskinee59236f2018-01-27 23:32:46 +01003626exit:
3627 ;
3628}
3629/* END_CASE */
3630
3631/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632void sign_hash_deterministic(int key_type_arg, data_t *key_data,
3633 int alg_arg, data_t *input_data,
3634 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003635{
Ronald Cron5425a212020-08-04 14:58:35 +02003636 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003637 psa_key_type_t key_type = key_type_arg;
3638 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003639 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003640 unsigned char *signature = NULL;
3641 size_t signature_size;
3642 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003643 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003645 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01003646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003647 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3648 psa_set_key_algorithm(&attributes, alg);
3649 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07003650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003651 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3652 &key));
3653 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3654 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01003655
Shaun Case0e7791f2021-12-20 21:14:10 -08003656 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003657 * library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003658 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
3659 key_bits, alg);
3660 TEST_ASSERT(signature_size != 0);
3661 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003662 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003663
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003664 /* Perform the signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003665 PSA_ASSERT(psa_sign_hash(key, alg,
3666 input_data->x, input_data->len,
3667 signature, signature_size,
3668 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003669 /* Verify that the signature is what is expected. */
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003670 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003671 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003672
Gilles Peskine0627f982019-11-26 19:12:16 +01003673#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 memset(signature, 0, signature_size);
Gilles Peskine895242b2019-11-29 12:15:40 +01003675 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003676 PSA_ASSERT(psa_asymmetric_sign(key, alg,
3677 input_data->x, input_data->len,
3678 signature, signature_size,
3679 &signature_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003680 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003681 signature, signature_length);
Gilles Peskine0627f982019-11-26 19:12:16 +01003682#endif /* MBEDTLS_TEST_DEPRECATED */
3683
Gilles Peskine20035e32018-02-03 22:44:14 +01003684exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003685 /*
3686 * Key attributes may have been returned by psa_get_key_attributes()
3687 * thus reset them as required.
3688 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003689 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003691 psa_destroy_key(key);
3692 mbedtls_free(signature);
3693 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01003694}
3695/* END_CASE */
3696
3697/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003698void sign_hash_fail(int key_type_arg, data_t *key_data,
3699 int alg_arg, data_t *input_data,
3700 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01003701{
Ronald Cron5425a212020-08-04 14:58:35 +02003702 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003703 psa_key_type_t key_type = key_type_arg;
3704 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003705 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003706 psa_status_t actual_status;
3707 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003708 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003709 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003711
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003712 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01003715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003716 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3717 psa_set_key_algorithm(&attributes, alg);
3718 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07003719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3721 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01003722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003723 actual_status = psa_sign_hash(key, alg,
3724 input_data->x, input_data->len,
3725 signature, signature_size,
3726 &signature_length);
3727 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003728 /* The value of *signature_length is unspecified on error, but
3729 * whatever it is, it should be less than signature_size, so that
3730 * if the caller tries to read *signature_length bytes without
3731 * checking the error code then they don't overflow a buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003732 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003733
Gilles Peskine895242b2019-11-29 12:15:40 +01003734#if defined(MBEDTLS_TEST_DEPRECATED)
3735 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003736 TEST_EQUAL(psa_asymmetric_sign(key, alg,
3737 input_data->x, input_data->len,
3738 signature, signature_size,
3739 &signature_length),
3740 expected_status);
3741 TEST_LE_U(signature_length, signature_size);
Gilles Peskine895242b2019-11-29 12:15:40 +01003742#endif /* MBEDTLS_TEST_DEPRECATED */
3743
Gilles Peskine20035e32018-02-03 22:44:14 +01003744exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003745 psa_reset_key_attributes(&attributes);
3746 psa_destroy_key(key);
3747 mbedtls_free(signature);
3748 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01003749}
3750/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003751
3752/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003753void sign_verify_hash(int key_type_arg, data_t *key_data,
3754 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02003755{
Ronald Cron5425a212020-08-04 14:58:35 +02003756 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003757 psa_key_type_t key_type = key_type_arg;
3758 psa_algorithm_t alg = alg_arg;
3759 size_t key_bits;
3760 unsigned char *signature = NULL;
3761 size_t signature_size;
3762 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003763 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003765 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02003766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3768 psa_set_key_algorithm(&attributes, alg);
3769 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02003770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003771 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3772 &key));
3773 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3774 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02003775
Shaun Case0e7791f2021-12-20 21:14:10 -08003776 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02003777 * library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003778 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
3779 key_bits, alg);
3780 TEST_ASSERT(signature_size != 0);
3781 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003782 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02003783
3784 /* Perform the signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003785 PSA_ASSERT(psa_sign_hash(key, alg,
3786 input_data->x, input_data->len,
3787 signature, signature_size,
3788 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003789 /* Check that the signature length looks sensible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 TEST_LE_U(signature_length, signature_size);
3791 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02003792
3793 /* Use the library to verify that the signature is correct. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003794 PSA_ASSERT(psa_verify_hash(key, alg,
3795 input_data->x, input_data->len,
3796 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003798 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02003799 /* Flip a bit in the input and verify that the signature is now
3800 * detected as invalid. Flip a bit at the beginning, not at the end,
3801 * because ECDSA may ignore the last few bits of the input. */
3802 input_data->x[0] ^= 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003803 TEST_EQUAL(psa_verify_hash(key, alg,
3804 input_data->x, input_data->len,
3805 signature, signature_length),
3806 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02003807 }
3808
3809exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003810 /*
3811 * Key attributes may have been returned by psa_get_key_attributes()
3812 * thus reset them as required.
3813 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003814 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003816 psa_destroy_key(key);
3817 mbedtls_free(signature);
3818 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02003819}
3820/* END_CASE */
3821
3822/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003823void verify_hash(int key_type_arg, data_t *key_data,
3824 int alg_arg, data_t *hash_data,
3825 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03003826{
Ronald Cron5425a212020-08-04 14:58:35 +02003827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003828 psa_key_type_t key_type = key_type_arg;
3829 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003830 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003832 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003834 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03003835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003836 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3837 psa_set_key_algorithm(&attributes, alg);
3838 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03003839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003840 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3841 &key));
itayzafrir5c753392018-05-08 11:18:38 +03003842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 PSA_ASSERT(psa_verify_hash(key, alg,
3844 hash_data->x, hash_data->len,
3845 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01003846
3847#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003848 PSA_ASSERT(psa_asymmetric_verify(key, alg,
3849 hash_data->x, hash_data->len,
3850 signature_data->x,
3851 signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01003852
3853#endif /* MBEDTLS_TEST_DEPRECATED */
3854
itayzafrir5c753392018-05-08 11:18:38 +03003855exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003856 psa_reset_key_attributes(&attributes);
3857 psa_destroy_key(key);
3858 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03003859}
3860/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003861
3862/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003863void verify_hash_fail(int key_type_arg, data_t *key_data,
3864 int alg_arg, data_t *hash_data,
3865 data_t *signature_data,
3866 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003867{
Ronald Cron5425a212020-08-04 14:58:35 +02003868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003869 psa_key_type_t key_type = key_type_arg;
3870 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871 psa_status_t actual_status;
3872 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003875 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3878 psa_set_key_algorithm(&attributes, alg);
3879 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003881 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3882 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884 actual_status = psa_verify_hash(key, alg,
3885 hash_data->x, hash_data->len,
3886 signature_data->x, signature_data->len);
3887 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003888
Gilles Peskine895242b2019-11-29 12:15:40 +01003889#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003890 TEST_EQUAL(psa_asymmetric_verify(key, alg,
3891 hash_data->x, hash_data->len,
3892 signature_data->x, signature_data->len),
3893 expected_status);
Gilles Peskine895242b2019-11-29 12:15:40 +01003894#endif /* MBEDTLS_TEST_DEPRECATED */
3895
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003897 psa_reset_key_attributes(&attributes);
3898 psa_destroy_key(key);
3899 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900}
3901/* END_CASE */
3902
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003904void sign_message_deterministic(int key_type_arg,
3905 data_t *key_data,
3906 int alg_arg,
3907 data_t *input_data,
3908 data_t *output_data)
gabor-mezei-armabd72582021-04-15 18:19:50 +02003909{
3910 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3911 psa_key_type_t key_type = key_type_arg;
3912 psa_algorithm_t alg = alg_arg;
3913 size_t key_bits;
3914 unsigned char *signature = NULL;
3915 size_t signature_size;
3916 size_t signature_length = 0xdeadbeef;
3917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003919 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armabd72582021-04-15 18:19:50 +02003920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003921 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3922 psa_set_key_algorithm(&attributes, alg);
3923 psa_set_key_type(&attributes, key_type);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003925 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3926 &key));
3927 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3928 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003930 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
3931 TEST_ASSERT(signature_size != 0);
3932 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003933 TEST_CALLOC(signature, signature_size);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003935 PSA_ASSERT(psa_sign_message(key, alg,
3936 input_data->x, input_data->len,
3937 signature, signature_size,
3938 &signature_length));
gabor-mezei-armabd72582021-04-15 18:19:50 +02003939
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003940 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01003941 signature, signature_length);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003942
3943exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944 psa_reset_key_attributes(&attributes);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003946 psa_destroy_key(key);
3947 mbedtls_free(signature);
3948 PSA_DONE();
gabor-mezei-armabd72582021-04-15 18:19:50 +02003949
3950}
3951/* END_CASE */
3952
3953/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003954void sign_message_fail(int key_type_arg,
3955 data_t *key_data,
3956 int alg_arg,
3957 data_t *input_data,
3958 int signature_size_arg,
3959 int expected_status_arg)
3960{
3961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3962 psa_key_type_t key_type = key_type_arg;
3963 psa_algorithm_t alg = alg_arg;
3964 size_t signature_size = signature_size_arg;
3965 psa_status_t actual_status;
3966 psa_status_t expected_status = expected_status_arg;
3967 unsigned char *signature = NULL;
3968 size_t signature_length = 0xdeadbeef;
3969 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3970
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003971 TEST_CALLOC(signature, signature_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003972
3973 PSA_ASSERT(psa_crypto_init());
3974
3975 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3976 psa_set_key_algorithm(&attributes, alg);
3977 psa_set_key_type(&attributes, key_type);
3978
3979 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3980 &key));
3981
3982 actual_status = psa_sign_message(key, alg,
3983 input_data->x, input_data->len,
3984 signature, signature_size,
3985 &signature_length);
3986 TEST_EQUAL(actual_status, expected_status);
3987 /* The value of *signature_length is unspecified on error, but
3988 * whatever it is, it should be less than signature_size, so that
3989 * if the caller tries to read *signature_length bytes without
3990 * checking the error code then they don't overflow a buffer. */
3991 TEST_LE_U(signature_length, signature_size);
3992
3993exit:
3994 psa_reset_key_attributes(&attributes);
3995 psa_destroy_key(key);
3996 mbedtls_free(signature);
3997 PSA_DONE();
3998}
3999/* END_CASE */
4000
4001/* BEGIN_CASE */
4002void sign_verify_message(int key_type_arg,
4003 data_t *key_data,
4004 int alg_arg,
4005 data_t *input_data)
4006{
4007 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4008 psa_key_type_t key_type = key_type_arg;
4009 psa_algorithm_t alg = alg_arg;
4010 size_t key_bits;
4011 unsigned char *signature = NULL;
4012 size_t signature_size;
4013 size_t signature_length = 0xdeadbeef;
4014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4015
4016 PSA_ASSERT(psa_crypto_init());
4017
4018 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4019 PSA_KEY_USAGE_VERIFY_MESSAGE);
4020 psa_set_key_algorithm(&attributes, alg);
4021 psa_set_key_type(&attributes, key_type);
4022
4023 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4024 &key));
4025 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4026 key_bits = psa_get_key_bits(&attributes);
4027
4028 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
4029 TEST_ASSERT(signature_size != 0);
4030 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004031 TEST_CALLOC(signature, signature_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004032
4033 PSA_ASSERT(psa_sign_message(key, alg,
4034 input_data->x, input_data->len,
4035 signature, signature_size,
4036 &signature_length));
4037 TEST_LE_U(signature_length, signature_size);
4038 TEST_ASSERT(signature_length > 0);
4039
4040 PSA_ASSERT(psa_verify_message(key, alg,
4041 input_data->x, input_data->len,
4042 signature, signature_length));
4043
4044 if (input_data->len != 0) {
4045 /* Flip a bit in the input and verify that the signature is now
4046 * detected as invalid. Flip a bit at the beginning, not at the end,
4047 * because ECDSA may ignore the last few bits of the input. */
4048 input_data->x[0] ^= 1;
4049 TEST_EQUAL(psa_verify_message(key, alg,
4050 input_data->x, input_data->len,
4051 signature, signature_length),
4052 PSA_ERROR_INVALID_SIGNATURE);
4053 }
4054
4055exit:
4056 psa_reset_key_attributes(&attributes);
4057
4058 psa_destroy_key(key);
4059 mbedtls_free(signature);
4060 PSA_DONE();
4061}
4062/* END_CASE */
4063
4064/* BEGIN_CASE */
4065void verify_message(int key_type_arg,
4066 data_t *key_data,
4067 int alg_arg,
4068 data_t *input_data,
4069 data_t *signature_data)
4070{
4071 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4072 psa_key_type_t key_type = key_type_arg;
4073 psa_algorithm_t alg = alg_arg;
4074 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4075
4076 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
4077
4078 PSA_ASSERT(psa_crypto_init());
4079
4080 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
4081 psa_set_key_algorithm(&attributes, alg);
4082 psa_set_key_type(&attributes, key_type);
4083
4084 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4085 &key));
4086
4087 PSA_ASSERT(psa_verify_message(key, alg,
4088 input_data->x, input_data->len,
4089 signature_data->x, signature_data->len));
4090
4091exit:
4092 psa_reset_key_attributes(&attributes);
4093 psa_destroy_key(key);
4094 PSA_DONE();
4095}
4096/* END_CASE */
4097
4098/* BEGIN_CASE */
4099void verify_message_fail(int key_type_arg,
4100 data_t *key_data,
4101 int alg_arg,
4102 data_t *hash_data,
4103 data_t *signature_data,
4104 int expected_status_arg)
4105{
4106 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4107 psa_key_type_t key_type = key_type_arg;
4108 psa_algorithm_t alg = alg_arg;
4109 psa_status_t actual_status;
4110 psa_status_t expected_status = expected_status_arg;
4111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4112
4113 PSA_ASSERT(psa_crypto_init());
4114
4115 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
4116 psa_set_key_algorithm(&attributes, alg);
4117 psa_set_key_type(&attributes, key_type);
4118
4119 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4120 &key));
4121
4122 actual_status = psa_verify_message(key, alg,
4123 hash_data->x, hash_data->len,
4124 signature_data->x,
4125 signature_data->len);
4126 TEST_EQUAL(actual_status, expected_status);
4127
4128exit:
4129 psa_reset_key_attributes(&attributes);
4130 psa_destroy_key(key);
4131 PSA_DONE();
4132}
4133/* END_CASE */
4134
4135/* BEGIN_CASE */
4136void asymmetric_encrypt(int key_type_arg,
gabor-mezei-armabd72582021-04-15 18:19:50 +02004137 data_t *key_data,
4138 int alg_arg,
4139 data_t *input_data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004140 data_t *label,
4141 int expected_output_length_arg,
4142 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02004143{
Ronald Cron5425a212020-08-04 14:58:35 +02004144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004145 psa_key_type_t key_type = key_type_arg;
4146 psa_algorithm_t alg = alg_arg;
4147 size_t expected_output_length = expected_output_length_arg;
4148 size_t key_bits;
4149 unsigned char *output = NULL;
4150 size_t output_size;
4151 size_t output_length = ~0;
4152 psa_status_t actual_status;
4153 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004154 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004156 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004157
Gilles Peskine656896e2018-06-29 19:12:28 +02004158 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004159 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4160 psa_set_key_algorithm(&attributes, alg);
4161 psa_set_key_type(&attributes, key_type);
4162 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4163 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02004164
4165 /* Determine the maximum output length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004166 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4167 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004169 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4170 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004171 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02004172
4173 /* Encrypt the input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 actual_status = psa_asymmetric_encrypt(key, alg,
4175 input_data->x, input_data->len,
4176 label->x, label->len,
4177 output, output_size,
4178 &output_length);
4179 TEST_EQUAL(actual_status, expected_status);
oberon-sk8a23f492023-02-13 13:42:02 +01004180 if (actual_status == PSA_SUCCESS) {
4181 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch6ed14362023-02-22 13:39:21 +01004182 } else {
4183 TEST_LE_U(output_length, output_size);
oberon-sk8a23f492023-02-13 13:42:02 +01004184 }
Gilles Peskine656896e2018-06-29 19:12:28 +02004185
Gilles Peskine68428122018-06-30 18:42:41 +02004186 /* If the label is empty, the test framework puts a non-null pointer
4187 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004188 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004189 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004190 if (output_size != 0) {
4191 memset(output, 0, output_size);
4192 }
4193 actual_status = psa_asymmetric_encrypt(key, alg,
4194 input_data->x, input_data->len,
4195 NULL, label->len,
4196 output, output_size,
4197 &output_length);
4198 TEST_EQUAL(actual_status, expected_status);
oberon-sk8a23f492023-02-13 13:42:02 +01004199 if (actual_status == PSA_SUCCESS) {
4200 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch6ed14362023-02-22 13:39:21 +01004201 } else {
4202 TEST_LE_U(output_length, output_size);
oberon-sk8a23f492023-02-13 13:42:02 +01004203 }
Gilles Peskine68428122018-06-30 18:42:41 +02004204 }
4205
Gilles Peskine656896e2018-06-29 19:12:28 +02004206exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004207 /*
4208 * Key attributes may have been returned by psa_get_key_attributes()
4209 * thus reset them as required.
4210 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004211 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004213 psa_destroy_key(key);
4214 mbedtls_free(output);
4215 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02004216}
4217/* END_CASE */
4218
4219/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004220void asymmetric_encrypt_decrypt(int key_type_arg,
4221 data_t *key_data,
4222 int alg_arg,
4223 data_t *input_data,
4224 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004225{
Ronald Cron5425a212020-08-04 14:58:35 +02004226 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004227 psa_key_type_t key_type = key_type_arg;
4228 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004229 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004230 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004231 size_t output_size;
4232 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004233 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004234 size_t output2_size;
4235 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004238 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004240 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4241 psa_set_key_algorithm(&attributes, alg);
4242 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004244 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4245 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004246
4247 /* Determine the maximum ciphertext length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004248 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4249 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004251 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4252 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004253 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01004254
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004255 output2_size = input_data->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004256 TEST_LE_U(output2_size,
4257 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
4258 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004259 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004260
Gilles Peskineeebd7382018-06-08 18:11:54 +02004261 /* We test encryption by checking that encrypt-then-decrypt gives back
4262 * the original plaintext because of the non-optional random
4263 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004264 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
4265 input_data->x, input_data->len,
4266 label->x, label->len,
4267 output, output_size,
4268 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004269 /* We don't know what ciphertext length to expect, but check that
4270 * it looks sensible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004271 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004273 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4274 output, output_length,
4275 label->x, label->len,
4276 output2, output2_size,
4277 &output2_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004278 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004279 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004280
4281exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004282 /*
4283 * Key attributes may have been returned by psa_get_key_attributes()
4284 * thus reset them as required.
4285 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004288 psa_destroy_key(key);
4289 mbedtls_free(output);
4290 mbedtls_free(output2);
4291 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004292}
4293/* END_CASE */
4294
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004295/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004296void asymmetric_decrypt(int key_type_arg,
4297 data_t *key_data,
4298 int alg_arg,
4299 data_t *input_data,
4300 data_t *label,
4301 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004302{
Ronald Cron5425a212020-08-04 14:58:35 +02004303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004304 psa_key_type_t key_type = key_type_arg;
4305 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004306 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004307 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004308 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004309 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004310 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004312 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004314 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4315 psa_set_key_algorithm(&attributes, alg);
4316 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4319 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004321 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4322 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004323
4324 /* Determine the maximum ciphertext length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004325 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4326 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004327 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01004328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004329 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4330 input_data->x, input_data->len,
4331 label->x, label->len,
4332 output,
4333 output_size,
4334 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004335 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004336 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004337
Gilles Peskine68428122018-06-30 18:42:41 +02004338 /* If the label is empty, the test framework puts a non-null pointer
4339 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004340 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004341 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004342 if (output_size != 0) {
4343 memset(output, 0, output_size);
4344 }
4345 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4346 input_data->x, input_data->len,
4347 NULL, label->len,
4348 output,
4349 output_size,
4350 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004351 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004352 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02004353 }
4354
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004355exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004356 psa_reset_key_attributes(&attributes);
4357 psa_destroy_key(key);
4358 mbedtls_free(output);
4359 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004360}
4361/* END_CASE */
4362
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004363/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004364void asymmetric_decrypt_fail(int key_type_arg,
4365 data_t *key_data,
4366 int alg_arg,
4367 data_t *input_data,
4368 data_t *label,
4369 int output_size_arg,
4370 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004371{
Ronald Cron5425a212020-08-04 14:58:35 +02004372 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004373 psa_key_type_t key_type = key_type_arg;
4374 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004375 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004376 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004377 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004378 psa_status_t actual_status;
4379 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004380 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004381
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004382 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004384 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004386 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4387 psa_set_key_algorithm(&attributes, alg);
4388 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004390 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4391 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004393 actual_status = psa_asymmetric_decrypt(key, alg,
4394 input_data->x, input_data->len,
4395 label->x, label->len,
4396 output, output_size,
4397 &output_length);
4398 TEST_EQUAL(actual_status, expected_status);
4399 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004400
Gilles Peskine68428122018-06-30 18:42:41 +02004401 /* If the label is empty, the test framework puts a non-null pointer
4402 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004403 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004404 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004405 if (output_size != 0) {
4406 memset(output, 0, output_size);
4407 }
4408 actual_status = psa_asymmetric_decrypt(key, alg,
4409 input_data->x, input_data->len,
4410 NULL, label->len,
4411 output, output_size,
4412 &output_length);
4413 TEST_EQUAL(actual_status, expected_status);
4414 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02004415 }
4416
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004417exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418 psa_reset_key_attributes(&attributes);
4419 psa_destroy_key(key);
4420 mbedtls_free(output);
4421 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004422}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004423/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004424
4425/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004426void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00004427{
4428 /* Test each valid way of initializing the object, except for `= {0}`, as
4429 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4430 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08004431 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004432 size_t capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004433 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004434 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4435 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004437 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00004438
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 /* A default operation should not be able to report its capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004440 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
4441 PSA_ERROR_BAD_STATE);
4442 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
4443 PSA_ERROR_BAD_STATE);
4444 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
4445 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004446
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 /* A default operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004448 PSA_ASSERT(psa_key_derivation_abort(&func));
4449 PSA_ASSERT(psa_key_derivation_abort(&init));
4450 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00004451}
4452/* END_CASE */
4453
Janos Follath16de4a42019-06-13 16:32:24 +01004454/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004455void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02004456{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004457 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004458 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004459 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004461 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02004462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004463 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
4464 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02004465
4466exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 psa_key_derivation_abort(&operation);
4468 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02004469}
4470/* END_CASE */
4471
Janos Follathaf3c2a02019-06-12 12:34:34 +01004472/* BEGIN_CASE */
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304473void derive_set_capacity(int alg_arg, int64_t capacity_arg,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004474 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01004475{
4476 psa_algorithm_t alg = alg_arg;
4477 size_t capacity = capacity_arg;
4478 psa_status_t expected_status = expected_status_arg;
4479 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01004482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004483 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01004484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004485 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
4486 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01004487
4488exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004489 psa_key_derivation_abort(&operation);
4490 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01004491}
4492/* END_CASE */
4493
4494/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004495void derive_input(int alg_arg,
4496 int step_arg1, int key_type_arg1, data_t *input1,
4497 int expected_status_arg1,
4498 int step_arg2, int key_type_arg2, data_t *input2,
4499 int expected_status_arg2,
4500 int step_arg3, int key_type_arg3, data_t *input3,
4501 int expected_status_arg3,
4502 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01004503{
4504 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
4506 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
4507 psa_status_t expected_statuses[] = { expected_status_arg1,
4508 expected_status_arg2,
4509 expected_status_arg3 };
4510 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02004511 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4512 MBEDTLS_SVC_KEY_ID_INIT,
4513 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004514 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4516 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004517 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004518 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004519 psa_status_t expected_output_status = expected_output_status_arg;
4520 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01004523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004524 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4525 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01004526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004527 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01004528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004529 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
4530 mbedtls_test_set_step(i);
4531 if (steps[i] == 0) {
Gilles Peskinef6279312021-05-27 13:21:20 +02004532 /* Skip this step */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004533 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
4534 psa_set_key_type(&attributes, key_types[i]);
4535 PSA_ASSERT(psa_import_key(&attributes,
4536 inputs[i]->x, inputs[i]->len,
4537 &keys[i]));
4538 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
4539 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004540 // When taking a private key as secret input, use key agreement
4541 // to add the shared secret to the derivation
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004542 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
4543 &operation, keys[i]),
4544 expected_statuses[i]);
4545 } else {
4546 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
4547 keys[i]),
4548 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004549 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004550 } else {
4551 TEST_EQUAL(psa_key_derivation_input_bytes(
4552 &operation, steps[i],
4553 inputs[i]->x, inputs[i]->len),
4554 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01004555 }
4556 }
4557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 if (output_key_type != PSA_KEY_TYPE_NONE) {
4559 psa_reset_key_attributes(&attributes);
4560 psa_set_key_type(&attributes, output_key_type);
4561 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004562 actual_output_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004563 psa_key_derivation_output_key(&attributes, &operation,
4564 &output_key);
4565 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004566 uint8_t buffer[1];
4567 actual_output_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004568 psa_key_derivation_output_bytes(&operation,
4569 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004570 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004571 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004572
Janos Follathaf3c2a02019-06-12 12:34:34 +01004573exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004574 psa_key_derivation_abort(&operation);
4575 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
4576 psa_destroy_key(keys[i]);
4577 }
4578 psa_destroy_key(output_key);
4579 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01004580}
4581/* END_CASE */
4582
Janos Follathd958bb72019-07-03 15:02:16 +01004583/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004585{
Janos Follathd958bb72019-07-03 15:02:16 +01004586 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004588 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004589 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004590 unsigned char input1[] = "Input 1";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004591 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01004592 unsigned char input2[] = "Input 2";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004593 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004594 uint8_t buffer[42];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004596 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4597 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004598 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004601 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4604 psa_set_key_algorithm(&attributes, alg);
4605 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004607 PSA_ASSERT(psa_import_key(&attributes,
4608 key_data, sizeof(key_data),
4609 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004610
4611 /* valid key derivation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004612 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
4613 input1, input1_length,
4614 input2, input2_length,
4615 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01004616 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004618
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004619 /* state of operation shouldn't allow additional generation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004620 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
4621 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004625 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
4626 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004627
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004628exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629 psa_key_derivation_abort(&operation);
4630 psa_destroy_key(key);
4631 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004632}
4633/* END_CASE */
4634
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004635/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004636void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004637{
4638 uint8_t output_buffer[16];
4639 size_t buffer_size = 16;
4640 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004641 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004643 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
4644 output_buffer, buffer_size)
4645 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004647 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
4648 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004650 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004652 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
4653 output_buffer, buffer_size)
4654 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004656 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
4657 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004658
4659exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004660 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004661}
4662/* END_CASE */
4663
4664/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004665void derive_output(int alg_arg,
4666 int step1_arg, data_t *input1,
4667 int step2_arg, data_t *input2,
4668 int step3_arg, data_t *input3,
4669 int requested_capacity_arg,
4670 data_t *expected_output1,
4671 data_t *expected_output2)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004672{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004673 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004674 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg };
4675 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02004676 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4677 MBEDTLS_SVC_KEY_ID_INIT,
4678 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004679 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004680 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004681 uint8_t *expected_outputs[2] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004682 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004683 size_t output_sizes[2] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004684 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004685 size_t output_buffer_size = 0;
4686 uint8_t *output_buffer = NULL;
4687 size_t expected_capacity;
4688 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004689 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004690 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004691 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004693 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
4694 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004695 output_buffer_size = output_sizes[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004696 }
4697 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004698 expected_outputs[i] = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004699 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004700 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004701 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004702 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004704 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4705 psa_set_key_algorithm(&attributes, alg);
4706 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004707
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004708 /* Extraction phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
4710 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
4711 requested_capacity));
4712 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
4713 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02004714 case 0:
4715 break;
4716 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004717 PSA_ASSERT(psa_import_key(&attributes,
4718 inputs[i]->x, inputs[i]->len,
4719 &keys[i]));
gabor-mezei-armceface22021-01-21 12:26:17 +01004720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
4722 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes));
4723 TEST_ASSERT(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes)) <=
4724 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
gabor-mezei-armceface22021-01-21 12:26:17 +01004725 }
4726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004727 PSA_ASSERT(psa_key_derivation_input_key(
4728 &operation, steps[i], keys[i]));
Gilles Peskine1468da72019-05-29 17:35:49 +02004729 break;
4730 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004731 PSA_ASSERT(psa_key_derivation_input_bytes(
4732 &operation, steps[i],
4733 inputs[i]->x, inputs[i]->len));
Gilles Peskine1468da72019-05-29 17:35:49 +02004734 break;
4735 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004736 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004738 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4739 &current_capacity));
4740 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004741 expected_capacity = requested_capacity;
4742
4743 /* Expansion phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004744 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004745 /* Read some bytes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004746 status = psa_key_derivation_output_bytes(&operation,
4747 output_buffer, output_sizes[i]);
4748 if (expected_capacity == 0 && output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004749 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004750 TEST_ASSERT(status == PSA_SUCCESS ||
4751 status == PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004752 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004753 } else if (expected_capacity == 0 ||
4754 output_sizes[i] > expected_capacity) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004755 /* Capacity exceeded. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004756 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004757 expected_capacity = 0;
4758 continue;
4759 }
4760 /* Success. Check the read data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004761 PSA_ASSERT(status);
4762 if (output_sizes[i] != 0) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004763 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004764 expected_outputs[i], output_sizes[i]);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004765 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004767 expected_capacity -= output_sizes[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4769 &current_capacity));
4770 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004771 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004773
4774exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004775 mbedtls_free(output_buffer);
4776 psa_key_derivation_abort(&operation);
4777 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
4778 psa_destroy_key(keys[i]);
4779 }
4780 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004781}
4782/* END_CASE */
4783
4784/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004785void derive_full(int alg_arg,
4786 data_t *key_data,
4787 data_t *input1,
4788 data_t *input2,
4789 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02004790{
Ronald Cron5425a212020-08-04 14:58:35 +02004791 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004792 psa_algorithm_t alg = alg_arg;
4793 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004794 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304795 unsigned char output_buffer[32];
Gilles Peskined54931c2018-07-17 21:06:59 +02004796 size_t expected_capacity = requested_capacity;
4797 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004800 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02004801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4803 psa_set_key_algorithm(&attributes, alg);
4804 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02004805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004806 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4807 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02004808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004809 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
4810 input1->x, input1->len,
4811 input2->x, input2->len,
4812 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01004813 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004814 }
Janos Follath47f27ed2019-06-25 13:24:52 +01004815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004816 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4817 &current_capacity));
4818 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02004819
4820 /* Expansion phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004821 while (current_capacity > 0) {
4822 size_t read_size = sizeof(output_buffer);
4823 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02004824 read_size = current_capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004825 }
4826 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
4827 output_buffer,
4828 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02004829 expected_capacity -= read_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004830 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4831 &current_capacity));
4832 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02004833 }
4834
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 /* Check that the operation refuses to go over capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004836 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
4837 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02004838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004839 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02004840
4841exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004842 psa_key_derivation_abort(&operation);
4843 psa_destroy_key(key);
4844 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02004845}
4846/* END_CASE */
4847
Janos Follathe60c9052019-07-03 13:51:30 +01004848/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004849void derive_key_exercise(int alg_arg,
4850 data_t *key_data,
4851 data_t *input1,
4852 data_t *input2,
4853 int derived_type_arg,
4854 int derived_bits_arg,
4855 int derived_usage_arg,
4856 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02004857{
Ronald Cron5425a212020-08-04 14:58:35 +02004858 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4859 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004860 psa_algorithm_t alg = alg_arg;
4861 psa_key_type_t derived_type = derived_type_arg;
4862 size_t derived_bits = derived_bits_arg;
4863 psa_key_usage_t derived_usage = derived_usage_arg;
4864 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004865 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004868 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004870 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02004871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004872 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4873 psa_set_key_algorithm(&attributes, alg);
4874 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
4875 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4876 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004877
4878 /* Derive a key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004879 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4880 input1->x, input1->len,
4881 input2->x, input2->len,
4882 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01004883 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004884 }
Janos Follathe60c9052019-07-03 13:51:30 +01004885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004886 psa_set_key_usage_flags(&attributes, derived_usage);
4887 psa_set_key_algorithm(&attributes, derived_alg);
4888 psa_set_key_type(&attributes, derived_type);
4889 psa_set_key_bits(&attributes, derived_bits);
4890 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
4891 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004892
4893 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004894 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
4895 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
4896 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004897
4898 /* Exercise the derived key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004899 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02004900 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004901 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02004902
4903exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004904 /*
4905 * Key attributes may have been returned by psa_get_key_attributes()
4906 * thus reset them as required.
4907 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004908 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004910 psa_key_derivation_abort(&operation);
4911 psa_destroy_key(base_key);
4912 psa_destroy_key(derived_key);
4913 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02004914}
4915/* END_CASE */
4916
Janos Follath42fd8882019-07-03 14:17:09 +01004917/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004918void derive_key_export(int alg_arg,
4919 data_t *key_data,
4920 data_t *input1,
4921 data_t *input2,
4922 int bytes1_arg,
4923 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02004924{
Ronald Cron5425a212020-08-04 14:58:35 +02004925 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4926 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004927 psa_algorithm_t alg = alg_arg;
4928 size_t bytes1 = bytes1_arg;
4929 size_t bytes2 = bytes2_arg;
4930 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004932 uint8_t *output_buffer = NULL;
4933 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004934 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4935 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004936 size_t length;
4937
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004938 TEST_CALLOC(output_buffer, capacity);
4939 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004940 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02004941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004942 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
4943 psa_set_key_algorithm(&base_attributes, alg);
4944 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
4945 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
4946 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004947
4948 /* Derive some material and output it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004949 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4950 input1->x, input1->len,
4951 input2->x, input2->len,
4952 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01004953 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004954 }
Janos Follath42fd8882019-07-03 14:17:09 +01004955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004956 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
4957 output_buffer,
4958 capacity));
4959 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004960
4961 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004962 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4963 input1->x, input1->len,
4964 input2->x, input2->len,
4965 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01004966 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004967 }
Janos Follath42fd8882019-07-03 14:17:09 +01004968
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004969 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
4970 psa_set_key_algorithm(&derived_attributes, 0);
4971 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
4972 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
4973 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
4974 &derived_key));
4975 PSA_ASSERT(psa_export_key(derived_key,
4976 export_buffer, bytes1,
4977 &length));
4978 TEST_EQUAL(length, bytes1);
4979 PSA_ASSERT(psa_destroy_key(derived_key));
4980 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
4981 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
4982 &derived_key));
4983 PSA_ASSERT(psa_export_key(derived_key,
4984 export_buffer + bytes1, bytes2,
4985 &length));
4986 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004987
4988 /* Compare the outputs from the two runs. */
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004989 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01004990 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004991
4992exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004993 mbedtls_free(output_buffer);
4994 mbedtls_free(export_buffer);
4995 psa_key_derivation_abort(&operation);
4996 psa_destroy_key(base_key);
4997 psa_destroy_key(derived_key);
4998 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02004999}
5000/* END_CASE */
5001
5002/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005003void derive_key(int alg_arg,
5004 data_t *key_data, data_t *input1, data_t *input2,
5005 int type_arg, int bits_arg,
5006 int expected_status_arg,
5007 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02005008{
Ronald Cron5425a212020-08-04 14:58:35 +02005009 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5010 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005011 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005012 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005013 size_t bits = bits_arg;
5014 psa_status_t expected_status = expected_status_arg;
5015 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5016 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5017 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02005020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005021 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
5022 psa_set_key_algorithm(&base_attributes, alg);
5023 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
5024 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
5025 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02005026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005027 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
5028 input1->x, input1->len,
5029 input2->x, input2->len,
5030 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02005031 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005032 }
Gilles Peskinec744d992019-07-30 17:26:54 +02005033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005034 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
5035 psa_set_key_algorithm(&derived_attributes, 0);
5036 psa_set_key_type(&derived_attributes, type);
5037 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01005038
5039 psa_status_t status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005040 psa_key_derivation_output_key(&derived_attributes,
5041 &operation,
5042 &derived_key);
5043 if (is_large_output > 0) {
5044 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
5045 }
5046 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02005047
5048exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005049 psa_key_derivation_abort(&operation);
5050 psa_destroy_key(base_key);
5051 psa_destroy_key(derived_key);
5052 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02005053}
5054/* END_CASE */
5055
5056/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005057void key_agreement_setup(int alg_arg,
5058 int our_key_type_arg, int our_key_alg_arg,
5059 data_t *our_key_data, data_t *peer_key_data,
5060 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005061{
Ronald Cron5425a212020-08-04 14:58:35 +02005062 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005063 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005064 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005065 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005068 psa_status_t expected_status = expected_status_arg;
5069 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005071 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02005072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005073 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5074 psa_set_key_algorithm(&attributes, our_key_alg);
5075 psa_set_key_type(&attributes, our_key_type);
5076 PSA_ASSERT(psa_import_key(&attributes,
5077 our_key_data->x, our_key_data->len,
5078 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02005079
Gilles Peskine77f40d82019-04-11 21:27:06 +02005080 /* The tests currently include inputs that should fail at either step.
5081 * Test cases that fail at the setup step should be changed to call
5082 * key_derivation_setup instead, and this function should be renamed
5083 * to key_agreement_fail. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005084 status = psa_key_derivation_setup(&operation, alg);
5085 if (status == PSA_SUCCESS) {
5086 TEST_EQUAL(psa_key_derivation_key_agreement(
5087 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5088 our_key,
5089 peer_key_data->x, peer_key_data->len),
5090 expected_status);
5091 } else {
5092 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02005093 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005094
5095exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005096 psa_key_derivation_abort(&operation);
5097 psa_destroy_key(our_key);
5098 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02005099}
5100/* END_CASE */
5101
5102/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005103void raw_key_agreement(int alg_arg,
5104 int our_key_type_arg, data_t *our_key_data,
5105 data_t *peer_key_data,
5106 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02005107{
Ronald Cron5425a212020-08-04 14:58:35 +02005108 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005109 psa_algorithm_t alg = alg_arg;
5110 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005112 unsigned char *output = NULL;
5113 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005114 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005116 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02005117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005118 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5119 psa_set_key_algorithm(&attributes, alg);
5120 psa_set_key_type(&attributes, our_key_type);
5121 PSA_ASSERT(psa_import_key(&attributes,
5122 our_key_data->x, our_key_data->len,
5123 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02005124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
5126 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01005127
Gilles Peskine7d150292022-04-13 23:25:52 +02005128 /* Validate size macros */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005129 TEST_LE_U(expected_output->len,
5130 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
5131 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
5132 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine7d150292022-04-13 23:25:52 +02005133
5134 /* Good case with exact output size */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005135 TEST_CALLOC(output, expected_output->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005136 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
5137 peer_key_data->x, peer_key_data->len,
5138 output, expected_output->len,
5139 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005140 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005141 expected_output->x, expected_output->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005142 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005143 output = NULL;
5144 output_length = ~0;
5145
5146 /* Larger buffer */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005147 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005148 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
5149 peer_key_data->x, peer_key_data->len,
5150 output, expected_output->len + 1,
5151 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005152 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005153 expected_output->x, expected_output->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005154 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005155 output = NULL;
5156 output_length = ~0;
5157
5158 /* Buffer too small */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005159 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005160 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
5161 peer_key_data->x, peer_key_data->len,
5162 output, expected_output->len - 1,
5163 &output_length),
5164 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine7d150292022-04-13 23:25:52 +02005165 /* Not required by the spec, but good robustness */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005166 TEST_LE_U(output_length, expected_output->len - 1);
5167 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005168 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005169
5170exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005171 mbedtls_free(output);
5172 psa_destroy_key(our_key);
5173 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02005174}
5175/* END_CASE */
5176
5177/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005178void key_agreement_capacity(int alg_arg,
5179 int our_key_type_arg, data_t *our_key_data,
5180 data_t *peer_key_data,
5181 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02005182{
Ronald Cron5425a212020-08-04 14:58:35 +02005183 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005184 psa_algorithm_t alg = alg_arg;
5185 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005186 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005188 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005189 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005191 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02005192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005193 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5194 psa_set_key_algorithm(&attributes, alg);
5195 psa_set_key_type(&attributes, our_key_type);
5196 PSA_ASSERT(psa_import_key(&attributes,
5197 our_key_data->x, our_key_data->len,
5198 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02005199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005200 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
5201 PSA_ASSERT(psa_key_derivation_key_agreement(
5202 &operation,
5203 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5204 peer_key_data->x, peer_key_data->len));
5205 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005206 /* The test data is for info="" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005207 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
5208 PSA_KEY_DERIVATION_INPUT_INFO,
5209 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005210 }
Gilles Peskine59685592018-09-18 12:11:34 +02005211
Shaun Case0e7791f2021-12-20 21:14:10 -08005212 /* Test the advertised capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005213 PSA_ASSERT(psa_key_derivation_get_capacity(
5214 &operation, &actual_capacity));
5215 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02005216
Gilles Peskinebf491972018-10-25 22:36:12 +02005217 /* Test the actual capacity by reading the output. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005218 while (actual_capacity > sizeof(output)) {
5219 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5220 output, sizeof(output)));
5221 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02005222 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005223 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5224 output, actual_capacity));
5225 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
5226 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02005227
Gilles Peskine59685592018-09-18 12:11:34 +02005228exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005229 psa_key_derivation_abort(&operation);
5230 psa_destroy_key(our_key);
5231 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02005232}
5233/* END_CASE */
5234
5235/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005236void key_agreement_output(int alg_arg,
5237 int our_key_type_arg, data_t *our_key_data,
5238 data_t *peer_key_data,
5239 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02005240{
Ronald Cron5425a212020-08-04 14:58:35 +02005241 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005242 psa_algorithm_t alg = alg_arg;
5243 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005246 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005247
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005248 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005249 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02005250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005251 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02005252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005253 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5254 psa_set_key_algorithm(&attributes, alg);
5255 psa_set_key_type(&attributes, our_key_type);
5256 PSA_ASSERT(psa_import_key(&attributes,
5257 our_key_data->x, our_key_data->len,
5258 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02005259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005260 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
5261 PSA_ASSERT(psa_key_derivation_key_agreement(
5262 &operation,
5263 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5264 peer_key_data->x, peer_key_data->len));
5265 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005266 /* The test data is for info="" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005267 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
5268 PSA_KEY_DERIVATION_INPUT_INFO,
5269 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005270 }
Gilles Peskine59685592018-09-18 12:11:34 +02005271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005272 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5273 actual_output,
5274 expected_output1->len));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005275 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005276 expected_output1->x, expected_output1->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005277 if (expected_output2->len != 0) {
5278 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5279 actual_output,
5280 expected_output2->len));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005281 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005282 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005283 }
Gilles Peskine59685592018-09-18 12:11:34 +02005284
5285exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005286 psa_key_derivation_abort(&operation);
5287 psa_destroy_key(our_key);
5288 PSA_DONE();
5289 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02005290}
5291/* END_CASE */
5292
5293/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005294void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02005295{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005296 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005297 unsigned char *output = NULL;
5298 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005299 size_t i;
5300 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005302 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00005303
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005304 TEST_CALLOC(output, bytes);
5305 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02005306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005307 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02005308
Gilles Peskinea50d7392018-06-21 10:22:13 +02005309 /* Run several times, to ensure that every output byte will be
5310 * nonzero at least once with overwhelming probability
5311 * (2^(-8*number_of_runs)). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005312 for (run = 0; run < 10; run++) {
5313 if (bytes != 0) {
5314 memset(output, 0, bytes);
5315 }
5316 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02005317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005318 for (i = 0; i < bytes; i++) {
5319 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02005320 ++changed[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005321 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005322 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005323 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005324
5325 /* Check that every byte was changed to nonzero at least once. This
5326 * validates that psa_generate_random is overwriting every byte of
5327 * the output buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005328 for (i = 0; i < bytes; i++) {
5329 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02005330 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005331
5332exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005333 PSA_DONE();
5334 mbedtls_free(output);
5335 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02005336}
5337/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005338
5339/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005340void generate_key(int type_arg,
5341 int bits_arg,
5342 int usage_arg,
5343 int alg_arg,
5344 int expected_status_arg,
5345 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005346{
Ronald Cron5425a212020-08-04 14:58:35 +02005347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005348 psa_key_type_t type = type_arg;
5349 psa_key_usage_t usage = usage_arg;
5350 size_t bits = bits_arg;
5351 psa_algorithm_t alg = alg_arg;
5352 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005354 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005356 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02005357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005358 psa_set_key_usage_flags(&attributes, usage);
5359 psa_set_key_algorithm(&attributes, alg);
5360 psa_set_key_type(&attributes, type);
5361 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005362
5363 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005364 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01005365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005366 if (is_large_key > 0) {
5367 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
5368 }
5369 TEST_EQUAL(status, expected_status);
5370 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005371 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005372 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005373
5374 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005375 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
5376 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
5377 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005378
Gilles Peskine818ca122018-06-20 18:16:48 +02005379 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005380 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02005381 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005382 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005383
5384exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005385 /*
5386 * Key attributes may have been returned by psa_get_key_attributes()
5387 * thus reset them as required.
5388 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005391 psa_destroy_key(key);
5392 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02005393}
5394/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005395
Ronald Cronee414c72021-03-18 18:50:08 +01005396/* 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 +01005397void generate_key_rsa(int bits_arg,
5398 data_t *e_arg,
5399 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02005400{
Ronald Cron5425a212020-08-04 14:58:35 +02005401 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005402 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005403 size_t bits = bits_arg;
5404 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5405 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5406 psa_status_t expected_status = expected_status_arg;
5407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5408 uint8_t *exported = NULL;
5409 size_t exported_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005410 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005411 size_t exported_length = SIZE_MAX;
5412 uint8_t *e_read_buffer = NULL;
5413 int is_default_public_exponent = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005414 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005415 size_t e_read_length = SIZE_MAX;
5416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005417 if (e_arg->len == 0 ||
5418 (e_arg->len == 3 &&
5419 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005420 is_default_public_exponent = 1;
5421 e_read_size = 0;
5422 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005423 TEST_CALLOC(e_read_buffer, e_read_size);
5424 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005426 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02005427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005428 psa_set_key_usage_flags(&attributes, usage);
5429 psa_set_key_algorithm(&attributes, alg);
5430 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
5431 e_arg->x, e_arg->len));
5432 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005433
5434 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005435 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
5436 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005437 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005438 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005439
5440 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005441 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5442 TEST_EQUAL(psa_get_key_type(&attributes), type);
5443 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
5444 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
5445 e_read_buffer, e_read_size,
5446 &e_read_length));
5447 if (is_default_public_exponent) {
5448 TEST_EQUAL(e_read_length, 0);
5449 } else {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005450 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005451 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005452
5453 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005454 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005455 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005456 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005457
5458 /* Export the key and check the public exponent. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005459 PSA_ASSERT(psa_export_public_key(key,
5460 exported, exported_size,
5461 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02005462 {
5463 uint8_t *p = exported;
5464 uint8_t *end = exported + exported_length;
5465 size_t len;
5466 /* RSAPublicKey ::= SEQUENCE {
5467 * modulus INTEGER, -- n
5468 * publicExponent INTEGER } -- e
5469 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005470 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
5471 MBEDTLS_ASN1_SEQUENCE |
5472 MBEDTLS_ASN1_CONSTRUCTED));
5473 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
5474 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
5475 MBEDTLS_ASN1_INTEGER));
5476 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005477 ++p;
5478 --len;
5479 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 if (e_arg->len == 0) {
5481 TEST_EQUAL(len, 3);
5482 TEST_EQUAL(p[0], 1);
5483 TEST_EQUAL(p[1], 0);
5484 TEST_EQUAL(p[2], 1);
5485 } else {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005486 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005487 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005488 }
5489
5490exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005491 /*
5492 * Key attributes may have been returned by psa_get_key_attributes() or
5493 * set by psa_set_key_domain_parameters() thus reset them as required.
5494 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005497 psa_destroy_key(key);
5498 PSA_DONE();
5499 mbedtls_free(e_read_buffer);
5500 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005501}
5502/* END_CASE */
5503
Darryl Greend49a4992018-06-18 17:27:26 +01005504/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005505void persistent_key_load_key_from_storage(data_t *data,
5506 int type_arg, int bits_arg,
5507 int usage_flags_arg, int alg_arg,
5508 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01005509{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005510 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5513 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005514 psa_key_type_t type = type_arg;
5515 size_t bits = bits_arg;
5516 psa_key_usage_t usage_flags = usage_flags_arg;
5517 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005518 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005519 unsigned char *first_export = NULL;
5520 unsigned char *second_export = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005521 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01005522 size_t first_exported_length;
5523 size_t second_exported_length;
5524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005525 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005526 TEST_CALLOC(first_export, export_size);
5527 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005528 }
Darryl Greend49a4992018-06-18 17:27:26 +01005529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005530 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01005531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005532 psa_set_key_id(&attributes, key_id);
5533 psa_set_key_usage_flags(&attributes, usage_flags);
5534 psa_set_key_algorithm(&attributes, alg);
5535 psa_set_key_type(&attributes, type);
5536 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01005537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005538 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00005539 case IMPORT_KEY:
5540 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005541 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
5542 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00005543 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005544
Darryl Green0c6575a2018-11-07 16:05:30 +00005545 case GENERATE_KEY:
5546 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00005548 break;
5549
5550 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005551#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005552 {
5553 /* Create base key */
5554 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
5555 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5556 psa_set_key_usage_flags(&base_attributes,
5557 PSA_KEY_USAGE_DERIVE);
5558 psa_set_key_algorithm(&base_attributes, derive_alg);
5559 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
5560 PSA_ASSERT(psa_import_key(&base_attributes,
5561 data->x, data->len,
5562 &base_key));
5563 /* Derive a key. */
5564 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
5565 PSA_ASSERT(psa_key_derivation_input_key(
5566 &operation,
5567 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
5568 PSA_ASSERT(psa_key_derivation_input_bytes(
5569 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
5570 NULL, 0));
5571 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
5572 &operation,
5573 &key));
5574 PSA_ASSERT(psa_key_derivation_abort(&operation));
5575 PSA_ASSERT(psa_destroy_key(base_key));
5576 base_key = MBEDTLS_SVC_KEY_ID_INIT;
5577 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005578#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005579 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005580#endif
5581 break;
5582
5583 default:
Agathiyan Bragadeesh27e29892023-07-14 17:28:27 +01005584 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005585 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005586 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005587 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01005588
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005589 /* Export the key if permitted by the key policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005590 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
5591 PSA_ASSERT(psa_export_key(key,
5592 first_export, export_size,
5593 &first_exported_length));
5594 if (generation_method == IMPORT_KEY) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005595 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005596 first_export, first_exported_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005597 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005598 }
Darryl Greend49a4992018-06-18 17:27:26 +01005599
5600 /* Shutdown and restart */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005601 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005602 PSA_DONE();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005603 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01005604
Darryl Greend49a4992018-06-18 17:27:26 +01005605 /* Check key slot still contains key data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005606 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5607 TEST_ASSERT(mbedtls_svc_key_id_equal(
5608 psa_get_key_id(&attributes), key_id));
5609 TEST_EQUAL(psa_get_key_lifetime(&attributes),
5610 PSA_KEY_LIFETIME_PERSISTENT);
5611 TEST_EQUAL(psa_get_key_type(&attributes), type);
5612 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
5613 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
5614 mbedtls_test_update_key_usage_flags(usage_flags));
5615 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01005616
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005617 /* Export the key again if permitted by the key policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005618 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
5619 PSA_ASSERT(psa_export_key(key,
5620 second_export, export_size,
5621 &second_exported_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005622 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrovea240fe32023-09-04 11:29:39 +01005623 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00005624 }
5625
5626 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005627 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00005628 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005629 }
Darryl Greend49a4992018-06-18 17:27:26 +01005630
5631exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005632 /*
5633 * Key attributes may have been returned by psa_get_key_attributes()
5634 * thus reset them as required.
5635 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005636 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005638 mbedtls_free(first_export);
5639 mbedtls_free(second_export);
5640 psa_key_derivation_abort(&operation);
5641 psa_destroy_key(base_key);
5642 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005643 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005644}
5645/* END_CASE */