blob: 8a20bc4eacac4af92ccbc335ce13058b93f0afaa [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
Gilles Peskine8e94efe2021-02-13 00:25:53 +010016#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010017#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010018#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010019
Gilles Peskinef6279312021-05-27 13:21:20 +020020/* If this comes up, it's a bug in the test code or in the test data. */
21#define UNUSED 0xdeadbeef
22
Dave Rodgman34b147d2021-06-23 12:49:59 +010023/* Assert that an operation is (not) active.
24 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010025#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
26#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Dave Rodgman34b147d2021-06-23 12:49:59 +010027
Jaeden Amerof24c7f82018-06-27 17:20:43 +010028/** An invalid export length that will never be set by psa_export_key(). */
29static const size_t INVALID_EXPORT_LENGTH = ~0U;
30
Gilles Peskinea7aa4422018-08-14 15:17:54 +020031/** Test if a buffer contains a constant byte value.
32 *
33 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034 *
35 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037 * \param size Size of the buffer in bytes.
38 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 * \return 1 if the buffer is all-bits-zero.
40 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020041 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043{
44 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 for (i = 0; i < size; i++) {
46 if (((unsigned char *) buffer)[i] != c) {
47 return 0;
48 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020049 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020051}
Andrzej Kureke0015962022-01-17 15:29:38 +010052#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020053/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054static int asn1_write_10x(unsigned char **p,
55 unsigned char *start,
56 size_t bits,
57 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058{
59 int ret;
60 int len = bits / 8 + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061 if (bits == 0) {
62 return MBEDTLS_ERR_ASN1_INVALID_DATA;
63 }
64 if (bits <= 8 && x >= 1 << (bits - 1)) {
65 return MBEDTLS_ERR_ASN1_INVALID_DATA;
66 }
67 if (*p < start || *p - start < (ptrdiff_t) len) {
68 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
69 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +020070 *p -= len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 (*p)[len-1] = x;
72 if (bits % 8 == 0) {
73 (*p)[1] |= 1;
74 } else {
75 (*p)[0] |= 1 << (bits % 8);
76 }
77 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
78 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
79 MBEDTLS_ASN1_INTEGER));
80 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +020081}
82
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083static int construct_fake_rsa_key(unsigned char *buffer,
84 size_t buffer_size,
85 unsigned char **p,
86 size_t bits,
87 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020088{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +020090 int ret;
91 int len = 0;
92 /* Construct something that looks like a DER encoding of
93 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
94 * RSAPrivateKey ::= SEQUENCE {
95 * version Version,
96 * modulus INTEGER, -- n
97 * publicExponent INTEGER, -- e
98 * privateExponent INTEGER, -- d
99 * prime1 INTEGER, -- p
100 * prime2 INTEGER, -- q
101 * exponent1 INTEGER, -- d mod (p-1)
102 * exponent2 INTEGER, -- d mod (q-1)
103 * coefficient INTEGER, -- (inverse of q) mod p
104 * otherPrimeInfos OtherPrimeInfos OPTIONAL
105 * }
106 * Or, for a public key, the same structure with only
107 * version, modulus and publicExponent.
108 */
109 *p = buffer + buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 if (keypair) {
111 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
112 asn1_write_10x(p, buffer, half_bits, 1));
113 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
114 asn1_write_10x(p, buffer, half_bits, 1));
115 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
116 asn1_write_10x(p, buffer, half_bits, 1));
117 MBEDTLS_ASN1_CHK_ADD(len, /* q */
118 asn1_write_10x(p, buffer, half_bits, 1));
119 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
120 asn1_write_10x(p, buffer, half_bits, 3));
121 MBEDTLS_ASN1_CHK_ADD(len, /* d */
122 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200123 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
125 asn1_write_10x(p, buffer, 17, 1));
126 MBEDTLS_ASN1_CHK_ADD(len, /* n */
127 asn1_write_10x(p, buffer, bits, 1));
128 if (keypair) {
129 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
130 mbedtls_asn1_write_int(p, buffer, 0));
131 }
132 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200133 {
134 const unsigned char tag =
135 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200137 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200139}
Andrzej Kureke0015962022-01-17 15:29:38 +0100140#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142int exercise_mac_setup(psa_key_type_t key_type,
143 const unsigned char *key_bytes,
144 size_t key_length,
145 psa_algorithm_t alg,
146 psa_mac_operation_t *operation,
147 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100148{
Ronald Cron5425a212020-08-04 14:58:35 +0200149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
153 psa_set_key_algorithm(&attributes, alg);
154 psa_set_key_type(&attributes, key_type);
155 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100158 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100160 /* If setup failed, reproduce the failure, so that the caller can
161 * test the resulting state of the operation object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 if (*status != PSA_SUCCESS) {
163 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 }
165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 psa_destroy_key(key);
167 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100168
169exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 psa_destroy_key(key);
171 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100172}
173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174int exercise_cipher_setup(psa_key_type_t key_type,
175 const unsigned char *key_bytes,
176 size_t key_length,
177 psa_algorithm_t alg,
178 psa_cipher_operation_t *operation,
179 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100180{
Ronald Cron5425a212020-08-04 14:58:35 +0200181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
185 psa_set_key_algorithm(&attributes, alg);
186 psa_set_key_type(&attributes, key_type);
187 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100190 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* If setup failed, reproduce the failure, so that the caller can
193 * test the resulting state of the operation object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 if (*status != PSA_SUCCESS) {
195 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
196 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 }
198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 psa_destroy_key(key);
200 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201
202exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 psa_destroy_key(key);
204 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100205}
206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200208{
209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211 uint8_t buffer[1];
212 size_t length;
213 int ok = 0;
214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 psa_set_key_id(&attributes, key_id);
216 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
217 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
218 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
219 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
220 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200221 TEST_EQUAL(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200223 TEST_EQUAL(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
225 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
226 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
227 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
228 TEST_EQUAL(psa_get_key_type(&attributes), 0);
229 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
232 PSA_ERROR_INVALID_HANDLE);
233 TEST_EQUAL(psa_export_public_key(key,
234 buffer, sizeof(buffer), &length),
235 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200236
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 ok = 1;
238
239exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100240 /*
241 * Key attributes may have been returned by psa_get_key_attributes()
242 * thus reset them as required.
243 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247}
248
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200249/* Assert that a key isn't reported as having a slot number. */
250#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252 do \
253 { \
254 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 TEST_EQUAL(psa_get_key_slot_number( \
256 attributes, \
257 &ASSERT_NO_SLOT_NUMBER_slot_number), \
258 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200259 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200261#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262#define ASSERT_NO_SLOT_NUMBER(attributes) \
263 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200264#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
265
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100266/* An overapproximation of the amount of storage needed for a key of the
267 * given type and with the given content. The API doesn't make it easy
268 * to find a good value for the size. The current implementation doesn't
269 * care about the value anyway. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270#define KEY_BITS_FROM_DATA(type, data) \
271 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100272
Darryl Green0c6575a2018-11-07 16:05:30 +0000273typedef enum {
274 IMPORT_KEY = 0,
275 GENERATE_KEY = 1,
276 DERIVE_KEY = 2
277} generate_method;
278
Gilles Peskinee59236f2018-01-27 23:32:46 +0100279/* END_HEADER */
280
281/* BEGIN_DEPENDENCIES
282 * depends_on:MBEDTLS_PSA_CRYPTO_C
283 * END_DEPENDENCIES
284 */
285
286/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200288{
289 size_t max_truncated_mac_size =
290 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
291
292 /* Check that the length for a truncated MAC always fits in the algorithm
293 * encoding. The shifted mask is the maximum truncated value. The
294 * untruncated algorithm may be one byte larger. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskine841b14b2019-11-26 17:37:37 +0100296
297#if defined(MBEDTLS_TEST_DEPRECATED)
298 /* Check deprecated constants. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 TEST_EQUAL(PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR);
300 TEST_EQUAL(PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS);
301 TEST_EQUAL(PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST);
302 TEST_EQUAL(PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA);
303 TEST_EQUAL(PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED);
304 TEST_EQUAL(PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH);
305 TEST_EQUAL(PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH);
306 TEST_EQUAL(PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskineb87b7192019-12-04 16:24:10 +0100307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 TEST_EQUAL(PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1);
309 TEST_EQUAL(PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1);
310 TEST_EQUAL(PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1);
311 TEST_EQUAL(PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1);
312 TEST_EQUAL(PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1);
313 TEST_EQUAL(PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1);
314 TEST_EQUAL(PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1);
315 TEST_EQUAL(PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1);
316 TEST_EQUAL(PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1);
317 TEST_EQUAL(PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1);
318 TEST_EQUAL(PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2);
319 TEST_EQUAL(PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1);
320 TEST_EQUAL(PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1);
321 TEST_EQUAL(PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1);
322 TEST_EQUAL(PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1);
323 TEST_EQUAL(PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1);
324 TEST_EQUAL(PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1);
325 TEST_EQUAL(PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1);
326 TEST_EQUAL(PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1);
327 TEST_EQUAL(PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1);
328 TEST_EQUAL(PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1);
329 TEST_EQUAL(PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1);
330 TEST_EQUAL(PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1);
331 TEST_EQUAL(PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2);
332 TEST_EQUAL(PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2);
333 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
334 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
335 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
336 TEST_EQUAL(PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY);
337 TEST_EQUAL(PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY);
Paul Elliott8ff510a2020-06-02 17:19:28 +0100338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 TEST_EQUAL(PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1);
340 TEST_EQUAL(PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1);
341 TEST_EQUAL(PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2);
342 TEST_EQUAL(PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1);
343 TEST_EQUAL(PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1);
344 TEST_EQUAL(PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2);
345 TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1);
346 TEST_EQUAL(PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY);
Gilles Peskineb87b7192019-12-04 16:24:10 +0100347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 TEST_EQUAL(PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919);
349 TEST_EQUAL(PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919);
350 TEST_EQUAL(PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919);
351 TEST_EQUAL(PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919);
352 TEST_EQUAL(PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919);
Paul Elliott75e27032020-06-03 15:17:39 +0100353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 TEST_EQUAL(PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919);
355 TEST_EQUAL(PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM);
Gilles Peskineb87b7192019-12-04 16:24:10 +0100356#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200357}
358/* END_CASE */
359
360/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361void import_with_policy(int type_arg,
362 int usage_arg, int alg_arg,
363 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +0200364{
365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
366 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200367 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200368 psa_key_type_t type = type_arg;
369 psa_key_usage_t usage = usage_arg;
370 psa_algorithm_t alg = alg_arg;
371 psa_status_t expected_status = expected_status_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +0200373 psa_status_t status;
374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +0200376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 psa_set_key_type(&attributes, type);
378 psa_set_key_usage_flags(&attributes, usage);
379 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381 status = psa_import_key(&attributes,
382 key_material, sizeof(key_material),
383 &key);
384 TEST_EQUAL(status, expected_status);
385 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +0200386 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 }
Gilles Peskine6edfa292019-07-31 15:53:45 +0200388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
390 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
391 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
392 mbedtls_test_update_key_usage_flags(usage));
393 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
394 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 PSA_ASSERT(psa_destroy_key(key));
397 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200398
399exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100400 /*
401 * Key attributes may have been returned by psa_get_key_attributes()
402 * thus reset them as required.
403 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 psa_destroy_key(key);
407 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +0200408}
409/* END_CASE */
410
411/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412void import_with_data(data_t *data, int type_arg,
413 int attr_bits_arg,
414 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200415{
416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
417 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200418 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200419 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200420 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200421 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100422 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 psa_set_key_type(&attributes, type);
427 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +0200428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 status = psa_import_key(&attributes, data->x, data->len, &key);
430 TEST_EQUAL(status, expected_status);
431 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200432 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
436 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
437 if (attr_bits != 0) {
438 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
439 }
440 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 PSA_ASSERT(psa_destroy_key(key));
443 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100444
445exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100446 /*
447 * Key attributes may have been returned by psa_get_key_attributes()
448 * thus reset them as required.
449 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 psa_destroy_key(key);
453 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100454}
455/* END_CASE */
456
457/* BEGIN_CASE */
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100458/* Construct and attempt to import a large unstructured key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459void import_large_key(int type_arg, int byte_size_arg,
460 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +0200461{
462 psa_key_type_t type = type_arg;
463 size_t byte_size = byte_size_arg;
464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
465 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200466 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200467 psa_status_t status;
468 uint8_t *buffer = NULL;
469 size_t buffer_size = byte_size + 1;
470 size_t n;
471
Steven Cooreman69967ce2021-01-18 18:01:08 +0100472 /* Skip the test case if the target running the test cannot
Shaun Case0e7791f2021-12-20 21:14:10 -0800473 * accommodate large keys due to heap size constraints */
Tom Cosgrove20e27de2023-09-04 11:09:08 +0100474 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +0200476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +0200478
479 /* Try importing the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
481 psa_set_key_type(&attributes, type);
482 status = psa_import_key(&attributes, buffer, byte_size, &key);
483 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
484 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +0200485
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 if (status == PSA_SUCCESS) {
487 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
488 TEST_EQUAL(psa_get_key_type(&attributes), type);
489 TEST_EQUAL(psa_get_key_bits(&attributes),
490 PSA_BYTES_TO_BITS(byte_size));
491 ASSERT_NO_SLOT_NUMBER(&attributes);
492 memset(buffer, 0, byte_size + 1);
493 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
494 for (n = 0; n < byte_size; n++) {
495 TEST_EQUAL(buffer[n], 'K');
496 }
497 for (n = byte_size; n < buffer_size; n++) {
498 TEST_EQUAL(buffer[n], 0);
499 }
Gilles Peskinec744d992019-07-30 17:26:54 +0200500 }
501
502exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100503 /*
504 * Key attributes may have been returned by psa_get_key_attributes()
505 * thus reset them as required.
506 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 psa_destroy_key(key);
510 PSA_DONE();
511 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +0200512}
513/* END_CASE */
514
Andrzej Kureke0015962022-01-17 15:29:38 +0100515/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100516/* Import an RSA key with a valid structure (but not valid numbers
517 * inside, beyond having sensible size and parity). This is expected to
518 * fail for large keys. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200520{
Ronald Cron5425a212020-08-04 14:58:35 +0200521 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200522 size_t bits = bits_arg;
523 psa_status_t expected_status = expected_status_arg;
524 psa_status_t status;
525 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200526 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200527 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200529 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200530 unsigned char *p;
531 int ret;
532 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100536 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
539 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200540 length = ret;
541
542 /* Try importing the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543 psa_set_key_type(&attributes, type);
544 status = psa_import_key(&attributes, p, length, &key);
545 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +0200546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547 if (status == PSA_SUCCESS) {
548 PSA_ASSERT(psa_destroy_key(key));
549 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200550
551exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 mbedtls_free(buffer);
553 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200554}
555/* END_CASE */
556
557/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558void import_export(data_t *data,
559 int type_arg,
560 int usage_arg, int alg_arg,
561 int expected_bits,
562 int export_size_delta,
563 int expected_export_status_arg,
564 /*whether reexport must give the original input exactly*/
565 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566{
Ronald Cron5425a212020-08-04 14:58:35 +0200567 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200569 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200570 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572 unsigned char *exported = NULL;
573 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100574 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100575 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100576 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200577 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200578 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579
Moran Pekercb088e72018-07-17 17:36:59 +0300580 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100581 TEST_CALLOC(exported, export_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 if (!canonical_input) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100583 TEST_CALLOC(reexported, export_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 }
585 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587 psa_set_key_usage_flags(&attributes, usage_arg);
588 psa_set_key_algorithm(&attributes, alg);
589 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -0700590
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100592 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593
594 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
596 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
597 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
598 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100599
600 /* Export the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601 status = psa_export_key(key, exported, export_size, &exported_length);
602 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100603
604 /* The exported length must be set by psa_export_key() to a value between 0
605 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100606 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
607 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
608 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
611 export_size - exported_length));
612 if (status != PSA_SUCCESS) {
613 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200615 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100616
Gilles Peskineea38a922021-02-13 00:05:16 +0100617 /* Run sanity checks on the exported key. For non-canonical inputs,
618 * this validates the canonical representations. For canonical inputs,
619 * this doesn't directly validate the implementation, but it still helps
620 * by cross-validating the test data with the sanity check code. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Gilles Peskine8f609232018-08-11 01:24:55 +0200622 goto exit;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624
625 if (canonical_input) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100626 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100627 } else {
628 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
629 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
630 &key2));
631 PSA_ASSERT(psa_export_key(key2,
632 reexported,
633 export_size,
634 &reexported_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100635 TEST_MEMORY_COMPARE(exported, exported_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 reexported, reexported_length);
637 PSA_ASSERT(psa_destroy_key(key2));
638 }
639 TEST_ASSERT(exported_length <=
640 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
641 psa_get_key_bits(&got_attributes)));
642 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643
644destroy:
645 /* Destroy the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100646 PSA_ASSERT(psa_destroy_key(key));
647 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648
649exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100650 /*
651 * Key attributes may have been returned by psa_get_key_attributes()
652 * thus reset them as required.
653 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656 mbedtls_free(exported);
657 mbedtls_free(reexported);
658 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659}
660/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100661
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663void import_export_public_key(data_t *data,
664 int type_arg, // key pair or public key
665 int alg_arg,
666 int export_size_delta,
667 int expected_export_status_arg,
668 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +0300669{
Ronald Cron5425a212020-08-04 14:58:35 +0200670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300671 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200672 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200673 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300674 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300675 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100676 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100677 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100680 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +0300681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100682 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
683 psa_set_key_algorithm(&attributes, alg);
684 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +0300685
686 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100687 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +0300688
Gilles Peskine49c25912018-10-29 15:15:31 +0100689 /* Export the public key */
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100690 TEST_CALLOC(exported, export_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691 status = psa_export_public_key(key,
692 exported, export_size,
693 &exported_length);
694 TEST_EQUAL(status, expected_export_status);
695 if (status == PSA_SUCCESS) {
696 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100697 size_t bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100698 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
699 bits = psa_get_key_bits(&attributes);
700 TEST_LE_U(expected_public_key->len,
701 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
702 TEST_LE_U(expected_public_key->len,
703 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
704 TEST_LE_U(expected_public_key->len,
705 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100706 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100707 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100708 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300709
710exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100711 /*
712 * Key attributes may have been returned by psa_get_key_attributes()
713 * thus reset them as required.
714 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 mbedtls_free(exported);
718 psa_destroy_key(key);
719 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +0300720}
721/* END_CASE */
722
Gilles Peskine20035e32018-02-03 22:44:14 +0100723/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100724void import_and_exercise_key(data_t *data,
725 int type_arg,
726 int bits_arg,
727 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200728{
Ronald Cron5425a212020-08-04 14:58:35 +0200729 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200730 psa_key_type_t type = type_arg;
731 size_t bits = bits_arg;
732 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100733 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +0200734 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200735 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100737 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100739 psa_set_key_usage_flags(&attributes, usage);
740 psa_set_key_algorithm(&attributes, alg);
741 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200742
743 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200745
746 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
748 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
749 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200750
751 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100752 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +0200753 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 PSA_ASSERT(psa_destroy_key(key));
757 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200758
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200759exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100760 /*
761 * Key attributes may have been returned by psa_get_key_attributes()
762 * thus reset them as required.
763 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100764 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100766 psa_reset_key_attributes(&attributes);
767 psa_destroy_key(key);
768 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200769}
770/* END_CASE */
771
772/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100773void effective_key_attributes(int type_arg, int expected_type_arg,
774 int bits_arg, int expected_bits_arg,
775 int usage_arg, int expected_usage_arg,
776 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +0200777{
Ronald Cron5425a212020-08-04 14:58:35 +0200778 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100779 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100780 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100781 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100782 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200783 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100784 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200785 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100786 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100789 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +0200790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 psa_set_key_usage_flags(&attributes, usage);
792 psa_set_key_algorithm(&attributes, alg);
793 psa_set_key_type(&attributes, key_type);
794 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +0200795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100796 PSA_ASSERT(psa_generate_key(&attributes, &key));
797 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +0200798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100799 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
800 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
801 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
802 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
803 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +0200804
805exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100806 /*
807 * Key attributes may have been returned by psa_get_key_attributes()
808 * thus reset them as required.
809 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100810 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100812 psa_destroy_key(key);
813 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +0200814}
815/* END_CASE */
816
817/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100818void check_key_policy(int type_arg, int bits_arg,
819 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +0100820{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
822 usage_arg,
823 mbedtls_test_update_key_usage_flags(usage_arg),
824 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +0100825 goto exit;
826}
827/* END_CASE */
828
829/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100830void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +0000831{
832 /* Test each valid way of initializing the object, except for `= {0}`, as
833 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
834 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -0800835 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100836 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200837 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
838 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100840 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +0000841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100842 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
843 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
844 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100846 TEST_EQUAL(psa_get_key_type(&func), 0);
847 TEST_EQUAL(psa_get_key_type(&init), 0);
848 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850 TEST_EQUAL(psa_get_key_bits(&func), 0);
851 TEST_EQUAL(psa_get_key_bits(&init), 0);
852 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100854 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
855 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
856 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
859 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
860 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +0000861}
862/* END_CASE */
863
864/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100865void mac_key_policy(int policy_usage_arg,
866 int policy_alg_arg,
867 int key_type_arg,
868 data_t *key_data,
869 int exercise_alg_arg,
870 int expected_status_sign_arg,
871 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +0200872{
Ronald Cron5425a212020-08-04 14:58:35 +0200873 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200874 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000875 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200876 psa_key_type_t key_type = key_type_arg;
877 psa_algorithm_t policy_alg = policy_alg_arg;
878 psa_algorithm_t exercise_alg = exercise_alg_arg;
879 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880 psa_status_t status;
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200881 psa_status_t expected_status_sign = expected_status_sign_arg;
882 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200883 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100885 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +0200886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100887 psa_set_key_usage_flags(&attributes, policy_usage);
888 psa_set_key_algorithm(&attributes, policy_alg);
889 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +0200890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100891 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
892 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +0200893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
895 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100897 status = psa_mac_sign_setup(&operation, key, exercise_alg);
898 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100899
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200900 /* Calculate the MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100901 uint8_t input[128] = { 0 };
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200902 size_t mac_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100903 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
904 input, 128,
905 mac, PSA_MAC_MAX_SIZE, &mac_len),
906 expected_status_sign);
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200907
908 /* Verify correct MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100909 status = psa_mac_verify(key, exercise_alg, input, 128,
910 mac, mac_len);
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100912 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
913 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
914 } else {
915 TEST_EQUAL(status, expected_status_verify);
916 }
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +0200919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920 memset(mac, 0, sizeof(mac));
921 status = psa_mac_verify_setup(&operation, key, exercise_alg);
922 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200923
924exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925 psa_mac_abort(&operation);
926 psa_destroy_key(key);
927 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928}
929/* END_CASE */
930
931/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100932void cipher_key_policy(int policy_usage_arg,
933 int policy_alg,
934 int key_type,
935 data_t *key_data,
936 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937{
Ronald Cron5425a212020-08-04 14:58:35 +0200938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000940 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200941 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200942 psa_status_t status;
943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100944 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100946 psa_set_key_usage_flags(&attributes, policy_usage);
947 psa_set_key_algorithm(&attributes, policy_alg);
948 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
951 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200952
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200953 /* Check if no key usage flag implication is done */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 TEST_EQUAL(policy_usage,
955 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
958 if (policy_alg == exercise_alg &&
959 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
960 PSA_ASSERT(status);
961 } else {
962 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
963 }
964 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
967 if (policy_alg == exercise_alg &&
968 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
969 PSA_ASSERT(status);
970 } else {
971 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
972 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973
974exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100975 psa_cipher_abort(&operation);
976 psa_destroy_key(key);
977 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200978}
979/* END_CASE */
980
981/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100982void aead_key_policy(int policy_usage_arg,
983 int policy_alg,
984 int key_type,
985 data_t *key_data,
986 int nonce_length_arg,
987 int tag_length_arg,
988 int exercise_alg,
989 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200990{
Ronald Cron5425a212020-08-04 14:58:35 +0200991 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200993 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200994 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100995 psa_status_t expected_status = expected_status_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100996 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997 size_t nonce_length = nonce_length_arg;
998 unsigned char tag[16];
999 size_t tag_length = tag_length_arg;
1000 size_t output_length;
1001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 TEST_LE_U(nonce_length, sizeof(nonce));
1003 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001005 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001007 psa_set_key_usage_flags(&attributes, policy_usage);
1008 psa_set_key_algorithm(&attributes, policy_alg);
1009 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001011 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1012 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001013
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001014 /* Check if no key usage implication is done */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001015 TEST_EQUAL(policy_usage,
1016 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001017
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001018 status = psa_aead_encrypt(key, exercise_alg,
1019 nonce, nonce_length,
1020 NULL, 0,
1021 NULL, 0,
1022 tag, tag_length,
1023 &output_length);
1024 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1025 TEST_EQUAL(status, expected_status);
1026 } else {
1027 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1028 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 memset(tag, 0, sizeof(tag));
1031 status = psa_aead_decrypt(key, exercise_alg,
1032 nonce, nonce_length,
1033 NULL, 0,
1034 tag, tag_length,
1035 NULL, 0,
1036 &output_length);
1037 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
1038 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1039 } else if (expected_status == PSA_SUCCESS) {
1040 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1041 } else {
1042 TEST_EQUAL(status, expected_status);
1043 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001044
1045exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001046 psa_destroy_key(key);
1047 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001048}
1049/* END_CASE */
1050
1051/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052void asymmetric_encryption_key_policy(int policy_usage_arg,
1053 int policy_alg,
1054 int key_type,
1055 data_t *key_data,
1056 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001057{
Ronald Cron5425a212020-08-04 14:58:35 +02001058 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001060 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001061 psa_status_t status;
1062 size_t key_bits;
1063 size_t buffer_length;
1064 unsigned char *buffer = NULL;
1065 size_t output_length;
1066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001067 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001069 psa_set_key_usage_flags(&attributes, policy_usage);
1070 psa_set_key_algorithm(&attributes, policy_alg);
1071 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001073 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1074 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001075
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001076 /* Check if no key usage implication is done */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001077 TEST_EQUAL(policy_usage,
1078 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1081 key_bits = psa_get_key_bits(&attributes);
1082 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
1083 exercise_alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01001084 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 status = psa_asymmetric_encrypt(key, exercise_alg,
1087 NULL, 0,
1088 NULL, 0,
1089 buffer, buffer_length,
1090 &output_length);
1091 if (policy_alg == exercise_alg &&
1092 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1093 PSA_ASSERT(status);
1094 } else {
1095 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1096 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001098 if (buffer_length != 0) {
1099 memset(buffer, 0, buffer_length);
1100 }
1101 status = psa_asymmetric_decrypt(key, exercise_alg,
1102 buffer, buffer_length,
1103 NULL, 0,
1104 buffer, buffer_length,
1105 &output_length);
1106 if (policy_alg == exercise_alg &&
1107 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1108 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
1109 } else {
1110 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1111 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001112
1113exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001114 /*
1115 * Key attributes may have been returned by psa_get_key_attributes()
1116 * thus reset them as required.
1117 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001120 psa_destroy_key(key);
1121 PSA_DONE();
1122 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001123}
1124/* END_CASE */
1125
1126/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001127void asymmetric_signature_key_policy(int policy_usage_arg,
1128 int policy_alg,
1129 int key_type,
1130 data_t *key_data,
1131 int exercise_alg,
1132 int payload_length_arg,
1133 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134{
Ronald Cron5425a212020-08-04 14:58:35 +02001135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001137 psa_key_usage_t policy_usage = policy_usage_arg;
1138 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001140 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001141 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1142 * compatible with the policy and `payload_length_arg` is supposed to be
1143 * a valid input length to sign. If `payload_length_arg <= 0`,
1144 * `exercise_alg` is supposed to be forbidden by the policy. */
1145 int compatible_alg = payload_length_arg > 0;
1146 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001147 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001148 size_t signature_length;
1149
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001150 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001151 in the expected usage flags. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001152 TEST_EQUAL(expected_usage,
1153 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001157 psa_set_key_usage_flags(&attributes, policy_usage);
1158 psa_set_key_algorithm(&attributes, policy_alg);
1159 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001161 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1162 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 status = psa_sign_hash(key, exercise_alg,
1167 payload, payload_length,
1168 signature, sizeof(signature),
1169 &signature_length);
1170 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
1171 PSA_ASSERT(status);
1172 } else {
1173 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1174 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001176 memset(signature, 0, sizeof(signature));
1177 status = psa_verify_hash(key, exercise_alg,
1178 payload, payload_length,
1179 signature, sizeof(signature));
1180 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
1181 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1182 } else {
1183 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1184 }
Gilles Peskined5b33222018-06-18 22:20:03 +02001185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001186 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
1187 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
1188 status = psa_sign_message(key, exercise_alg,
1189 payload, payload_length,
1190 signature, sizeof(signature),
1191 &signature_length);
1192 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
1193 PSA_ASSERT(status);
1194 } else {
1195 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1196 }
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001198 memset(signature, 0, sizeof(signature));
1199 status = psa_verify_message(key, exercise_alg,
1200 payload, payload_length,
1201 signature, sizeof(signature));
1202 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
1203 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1204 } else {
1205 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1206 }
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001207 }
1208
Gilles Peskined5b33222018-06-18 22:20:03 +02001209exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001210 psa_destroy_key(key);
1211 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001212}
1213/* END_CASE */
1214
Janos Follathba3fab92019-06-11 14:50:16 +01001215/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001216void derive_key_policy(int policy_usage,
1217 int policy_alg,
1218 int key_type,
1219 data_t *key_data,
1220 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02001221{
Ronald Cron5425a212020-08-04 14:58:35 +02001222 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001224 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001225 psa_status_t status;
1226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001227 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02001228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001229 psa_set_key_usage_flags(&attributes, policy_usage);
1230 psa_set_key_algorithm(&attributes, policy_alg);
1231 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02001232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1234 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02001235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001236 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01001237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001238 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
1239 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
1240 PSA_ASSERT(psa_key_derivation_input_bytes(
1241 &operation,
1242 PSA_KEY_DERIVATION_INPUT_SEED,
1243 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01001244 }
Janos Follathba3fab92019-06-11 14:50:16 +01001245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001246 status = psa_key_derivation_input_key(&operation,
1247 PSA_KEY_DERIVATION_INPUT_SECRET,
1248 key);
Janos Follathba3fab92019-06-11 14:50:16 +01001249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001250 if (policy_alg == exercise_alg &&
1251 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
1252 PSA_ASSERT(status);
1253 } else {
1254 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1255 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02001256
1257exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001258 psa_key_derivation_abort(&operation);
1259 psa_destroy_key(key);
1260 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02001261}
1262/* END_CASE */
1263
1264/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001265void agreement_key_policy(int policy_usage,
1266 int policy_alg,
1267 int key_type_arg,
1268 data_t *key_data,
1269 int exercise_alg,
1270 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02001271{
Ronald Cron5425a212020-08-04 14:58:35 +02001272 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001274 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001275 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001276 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001277 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001279 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02001280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 psa_set_key_usage_flags(&attributes, policy_usage);
1282 psa_set_key_algorithm(&attributes, policy_alg);
1283 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02001284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1286 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02001287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
1289 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02001290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001291 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02001292
1293exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 psa_key_derivation_abort(&operation);
1295 psa_destroy_key(key);
1296 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02001297}
1298/* END_CASE */
1299
1300/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001301void key_policy_alg2(int key_type_arg, data_t *key_data,
1302 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001303{
Ronald Cron5425a212020-08-04 14:58:35 +02001304 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001305 psa_key_type_t key_type = key_type_arg;
1306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1307 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1308 psa_key_usage_t usage = usage_arg;
1309 psa_algorithm_t alg = alg_arg;
1310 psa_algorithm_t alg2 = alg2_arg;
1311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001312 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 psa_set_key_usage_flags(&attributes, usage);
1315 psa_set_key_algorithm(&attributes, alg);
1316 psa_set_key_enrollment_algorithm(&attributes, alg2);
1317 psa_set_key_type(&attributes, key_type);
1318 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1319 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001320
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001321 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001322 usage = mbedtls_test_update_key_usage_flags(usage);
1323 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1324 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
1325 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1326 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001328 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001329 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001330 }
1331 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001332 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001334
1335exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001336 /*
1337 * Key attributes may have been returned by psa_get_key_attributes()
1338 * thus reset them as required.
1339 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001340 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001342 psa_destroy_key(key);
1343 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001344}
1345/* END_CASE */
1346
1347/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001348void raw_agreement_key_policy(int policy_usage,
1349 int policy_alg,
1350 int key_type_arg,
1351 data_t *key_data,
1352 int exercise_alg,
1353 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001354{
Ronald Cron5425a212020-08-04 14:58:35 +02001355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001357 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001358 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001359 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001360 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001362 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001364 psa_set_key_usage_flags(&attributes, policy_usage);
1365 psa_set_key_algorithm(&attributes, policy_alg);
1366 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1369 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001371 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001374
1375exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001376 psa_key_derivation_abort(&operation);
1377 psa_destroy_key(key);
1378 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001379}
1380/* END_CASE */
1381
1382/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383void copy_success(int source_usage_arg,
1384 int source_alg_arg, int source_alg2_arg,
1385 int type_arg, data_t *material,
1386 int copy_attributes,
1387 int target_usage_arg,
1388 int target_alg_arg, int target_alg2_arg,
1389 int expected_usage_arg,
1390 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01001391{
Gilles Peskineca25db92019-04-19 11:43:08 +02001392 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1393 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001394 psa_key_usage_t expected_usage = expected_usage_arg;
1395 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001396 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001397 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1398 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001399 uint8_t *export_buffer = NULL;
1400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001401 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01001402
Gilles Peskineca25db92019-04-19 11:43:08 +02001403 /* Prepare the source key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001404 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
1405 psa_set_key_algorithm(&source_attributes, source_alg_arg);
1406 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
1407 psa_set_key_type(&source_attributes, type_arg);
1408 PSA_ASSERT(psa_import_key(&source_attributes,
1409 material->x, material->len,
1410 &source_key));
1411 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001412
Gilles Peskineca25db92019-04-19 11:43:08 +02001413 /* Prepare the target attributes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001414 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02001415 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001416 /* Set volatile lifetime to reset the key identifier to 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001417 psa_set_key_lifetime(&target_attributes, PSA_KEY_LIFETIME_VOLATILE);
Ronald Cron65f38a32020-10-23 17:11:13 +02001418 }
1419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001420 if (target_usage_arg != -1) {
1421 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
1422 }
1423 if (target_alg_arg != -1) {
1424 psa_set_key_algorithm(&target_attributes, target_alg_arg);
1425 }
1426 if (target_alg2_arg != -1) {
1427 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
1428 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001429
1430 /* Copy the key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431 PSA_ASSERT(psa_copy_key(source_key,
1432 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001433
1434 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001435 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001436
1437 /* Test that the target slot has the expected content and policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001438 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
1439 TEST_EQUAL(psa_get_key_type(&source_attributes),
1440 psa_get_key_type(&target_attributes));
1441 TEST_EQUAL(psa_get_key_bits(&source_attributes),
1442 psa_get_key_bits(&target_attributes));
1443 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
1444 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
1445 TEST_EQUAL(expected_alg2,
1446 psa_get_key_enrollment_algorithm(&target_attributes));
1447 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01001448 size_t length;
Tom Cosgrove30ceb232023-09-04 11:20:19 +01001449 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 PSA_ASSERT(psa_export_key(target_key, export_buffer,
1451 material->len, &length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01001452 TEST_MEMORY_COMPARE(material->x, material->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001453 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01001454 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01001457 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001458 }
1459 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001460 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001461 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01001464
1465exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001466 /*
1467 * Source and target key attributes may have been returned by
1468 * psa_get_key_attributes() thus reset them as required.
1469 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001470 psa_reset_key_attributes(&source_attributes);
1471 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001473 PSA_DONE();
1474 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01001475}
1476/* END_CASE */
1477
1478/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001479void copy_fail(int source_usage_arg,
1480 int source_alg_arg, int source_alg2_arg,
1481 int type_arg, data_t *material,
1482 int target_type_arg, int target_bits_arg,
1483 int target_usage_arg,
1484 int target_alg_arg, int target_alg2_arg,
1485 int target_id_arg, int target_lifetime_arg,
1486 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02001487{
1488 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1489 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001490 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1491 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001492 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02001493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02001495
1496 /* Prepare the source key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
1498 psa_set_key_algorithm(&source_attributes, source_alg_arg);
1499 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
1500 psa_set_key_type(&source_attributes, type_arg);
1501 PSA_ASSERT(psa_import_key(&source_attributes,
1502 material->x, material->len,
1503 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02001504
1505 /* Prepare the target attributes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001506 psa_set_key_id(&target_attributes, key_id);
1507 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
1508 psa_set_key_type(&target_attributes, target_type_arg);
1509 psa_set_key_bits(&target_attributes, target_bits_arg);
1510 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
1511 psa_set_key_algorithm(&target_attributes, target_alg_arg);
1512 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02001513
1514 /* Try to copy the key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001515 TEST_EQUAL(psa_copy_key(source_key,
1516 &target_attributes, &target_key),
1517 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001519 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02001520
Gilles Peskine4a644642019-05-03 17:14:08 +02001521exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 psa_reset_key_attributes(&source_attributes);
1523 psa_reset_key_attributes(&target_attributes);
1524 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02001525}
1526/* END_CASE */
1527
1528/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001529void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00001530{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001531 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001532 /* Test each valid way of initializing the object, except for `= {0}`, as
1533 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1534 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08001535 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00001537 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1538 psa_hash_operation_t zero;
1539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001540 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00001541
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001542 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
1544 PSA_ERROR_BAD_STATE);
1545 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
1546 PSA_ERROR_BAD_STATE);
1547 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
1548 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001549
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001550 /* A default hash operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001551 PSA_ASSERT(psa_hash_abort(&func));
1552 PSA_ASSERT(psa_hash_abort(&init));
1553 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00001554}
1555/* END_CASE */
1556
1557/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001558void hash_setup(int alg_arg,
1559 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001560{
1561 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001562 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001563 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001564 psa_status_t status;
1565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001566 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001568 status = psa_hash_setup(&operation, alg);
1569 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001570
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001571 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001572 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001573
1574 /* If setup failed, reproduce the failure, so as to
1575 * test the resulting state of the operation object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001576 if (status != PSA_SUCCESS) {
1577 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
1578 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001579
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001580 /* Now the operation object should be reusable. */
1581#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001582 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
1583 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001584#endif
1585
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001586exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001588}
1589/* END_CASE */
1590
1591/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592void hash_compute_fail(int alg_arg, data_t *input,
1593 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01001594{
1595 psa_algorithm_t alg = alg_arg;
1596 uint8_t *output = NULL;
1597 size_t output_size = output_size_arg;
1598 size_t output_length = INVALID_EXPORT_LENGTH;
1599 psa_status_t expected_status = expected_status_arg;
1600 psa_status_t status;
1601
Tom Cosgrove30ceb232023-09-04 11:20:19 +01001602 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001604 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01001605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001606 status = psa_hash_compute(alg, input->x, input->len,
1607 output, output_size, &output_length);
1608 TEST_EQUAL(status, expected_status);
1609 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001610
1611exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001612 mbedtls_free(output);
1613 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01001614}
1615/* END_CASE */
1616
1617/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001618void hash_compare_fail(int alg_arg, data_t *input,
1619 data_t *reference_hash,
1620 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01001621{
1622 psa_algorithm_t alg = alg_arg;
1623 psa_status_t expected_status = expected_status_arg;
1624 psa_status_t status;
1625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001626 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01001627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001628 status = psa_hash_compare(alg, input->x, input->len,
1629 reference_hash->x, reference_hash->len);
1630 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01001631
1632exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001633 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01001634}
1635/* END_CASE */
1636
1637/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001638void hash_compute_compare(int alg_arg, data_t *input,
1639 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01001640{
1641 psa_algorithm_t alg = alg_arg;
1642 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1643 size_t output_length = INVALID_EXPORT_LENGTH;
1644 size_t i;
1645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001646 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01001647
1648 /* Compute with tight buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001649 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
1650 output, PSA_HASH_LENGTH(alg),
1651 &output_length));
1652 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01001653 TEST_MEMORY_COMPARE(output, output_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001654 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001655
1656 /* Compute with larger buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001657 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
1658 output, sizeof(output),
1659 &output_length));
1660 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01001661 TEST_MEMORY_COMPARE(output, output_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001662 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001663
1664 /* Compare with correct hash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
1666 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01001667
1668 /* Compare with trailing garbage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001669 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
1670 output, output_length + 1),
1671 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001672
1673 /* Compare with truncated hash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001674 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
1675 output, output_length - 1),
1676 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001677
1678 /* Compare with corrupted value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001679 for (i = 0; i < output_length; i++) {
1680 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001681 output[i] ^= 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001682 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
1683 output, output_length),
1684 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01001685 output[i] ^= 1;
1686 }
1687
1688exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001689 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01001690}
1691/* END_CASE */
1692
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001693/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001694void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02001695{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001696 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001697 unsigned char input[] = "";
1698 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001699 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001700 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1701 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001702 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
1703 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001704 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001705 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001706 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001708 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02001709
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001710 /* Call setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001711 PSA_ASSERT(psa_hash_setup(&operation, alg));
1712 ASSERT_OPERATION_IS_ACTIVE(operation);
1713 TEST_EQUAL(psa_hash_setup(&operation, alg),
1714 PSA_ERROR_BAD_STATE);
1715 ASSERT_OPERATION_IS_INACTIVE(operation);
1716 PSA_ASSERT(psa_hash_abort(&operation));
1717 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001718
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001719 /* Call update without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001720 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
1721 PSA_ERROR_BAD_STATE);
1722 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02001723
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001724 /* Check that update calls abort on error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001725 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman54f73512021-06-24 18:14:52 +01001726 operation.id = UINT_MAX;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001727 ASSERT_OPERATION_IS_ACTIVE(operation);
1728 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
1729 PSA_ERROR_BAD_STATE);
1730 ASSERT_OPERATION_IS_INACTIVE(operation);
1731 PSA_ASSERT(psa_hash_abort(&operation));
1732 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001733
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001734 /* Call update after finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001735 PSA_ASSERT(psa_hash_setup(&operation, alg));
1736 PSA_ASSERT(psa_hash_finish(&operation,
1737 hash, sizeof(hash), &hash_len));
1738 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
1739 PSA_ERROR_BAD_STATE);
1740 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02001741
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001742 /* Call verify without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001743 TEST_EQUAL(psa_hash_verify(&operation,
1744 valid_hash, sizeof(valid_hash)),
1745 PSA_ERROR_BAD_STATE);
1746 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001747
1748 /* Call verify after finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749 PSA_ASSERT(psa_hash_setup(&operation, alg));
1750 PSA_ASSERT(psa_hash_finish(&operation,
1751 hash, sizeof(hash), &hash_len));
1752 TEST_EQUAL(psa_hash_verify(&operation,
1753 valid_hash, sizeof(valid_hash)),
1754 PSA_ERROR_BAD_STATE);
1755 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001756
1757 /* Call verify twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001758 PSA_ASSERT(psa_hash_setup(&operation, alg));
1759 ASSERT_OPERATION_IS_ACTIVE(operation);
1760 PSA_ASSERT(psa_hash_verify(&operation,
1761 valid_hash, sizeof(valid_hash)));
1762 ASSERT_OPERATION_IS_INACTIVE(operation);
1763 TEST_EQUAL(psa_hash_verify(&operation,
1764 valid_hash, sizeof(valid_hash)),
1765 PSA_ERROR_BAD_STATE);
1766 ASSERT_OPERATION_IS_INACTIVE(operation);
1767 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001768
1769 /* Call finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001770 TEST_EQUAL(psa_hash_finish(&operation,
1771 hash, sizeof(hash), &hash_len),
1772 PSA_ERROR_BAD_STATE);
1773 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001774
1775 /* Call finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001776 PSA_ASSERT(psa_hash_setup(&operation, alg));
1777 PSA_ASSERT(psa_hash_finish(&operation,
1778 hash, sizeof(hash), &hash_len));
1779 TEST_EQUAL(psa_hash_finish(&operation,
1780 hash, sizeof(hash), &hash_len),
1781 PSA_ERROR_BAD_STATE);
1782 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001783
1784 /* Call finish after calling verify. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001785 PSA_ASSERT(psa_hash_setup(&operation, alg));
1786 PSA_ASSERT(psa_hash_verify(&operation,
1787 valid_hash, sizeof(valid_hash)));
1788 TEST_EQUAL(psa_hash_finish(&operation,
1789 hash, sizeof(hash), &hash_len),
1790 PSA_ERROR_BAD_STATE);
1791 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02001792
1793exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001794 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02001795}
1796/* END_CASE */
1797
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001798/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001799void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03001800{
1801 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001802 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1803 * appended to it */
1804 unsigned char hash[] = {
1805 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1806 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001807 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
1808 };
1809 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00001810 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001812 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03001813
itayzafrir27e69452018-11-01 14:26:34 +02001814 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001815 PSA_ASSERT(psa_hash_setup(&operation, alg));
1816 ASSERT_OPERATION_IS_ACTIVE(operation);
1817 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
1818 PSA_ERROR_INVALID_SIGNATURE);
1819 ASSERT_OPERATION_IS_INACTIVE(operation);
1820 PSA_ASSERT(psa_hash_abort(&operation));
1821 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03001822
itayzafrir27e69452018-11-01 14:26:34 +02001823 /* psa_hash_verify with a non-matching hash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001824 PSA_ASSERT(psa_hash_setup(&operation, alg));
1825 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
1826 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03001827
itayzafrir27e69452018-11-01 14:26:34 +02001828 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001829 PSA_ASSERT(psa_hash_setup(&operation, alg));
1830 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
1831 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03001832
itayzafrirec93d302018-10-18 18:01:10 +03001833exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001834 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03001835}
1836/* END_CASE */
1837
Ronald Cronee414c72021-03-18 18:50:08 +01001838/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001839void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03001840{
1841 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001842 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001843 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00001844 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001845 size_t hash_len;
1846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001847 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03001848
itayzafrir58028322018-10-25 10:22:01 +03001849 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001850 PSA_ASSERT(psa_hash_setup(&operation, alg));
1851 TEST_EQUAL(psa_hash_finish(&operation,
1852 hash, expected_size - 1, &hash_len),
1853 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03001854
1855exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001856 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03001857}
1858/* END_CASE */
1859
Ronald Cronee414c72021-03-18 18:50:08 +01001860/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001861void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001862{
1863 psa_algorithm_t alg = PSA_ALG_SHA_256;
1864 unsigned char hash[PSA_HASH_MAX_SIZE];
1865 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1866 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1867 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1868 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1869 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1870 size_t hash_len;
1871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001872 PSA_ASSERT(psa_crypto_init());
1873 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001875 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
1876 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
1877 PSA_ASSERT(psa_hash_finish(&op_finished,
1878 hash, sizeof(hash), &hash_len));
1879 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
1880 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001882 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
1883 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001885 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
1886 PSA_ASSERT(psa_hash_finish(&op_init,
1887 hash, sizeof(hash), &hash_len));
1888 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
1889 PSA_ASSERT(psa_hash_finish(&op_finished,
1890 hash, sizeof(hash), &hash_len));
1891 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
1892 PSA_ASSERT(psa_hash_finish(&op_aborted,
1893 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001894
1895exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001896 psa_hash_abort(&op_source);
1897 psa_hash_abort(&op_init);
1898 psa_hash_abort(&op_setup);
1899 psa_hash_abort(&op_finished);
1900 psa_hash_abort(&op_aborted);
1901 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001902}
1903/* END_CASE */
1904
Ronald Cronee414c72021-03-18 18:50:08 +01001905/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001906void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001907{
1908 psa_algorithm_t alg = PSA_ALG_SHA_256;
1909 unsigned char hash[PSA_HASH_MAX_SIZE];
1910 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1911 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1912 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1913 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1914 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1915 size_t hash_len;
1916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001917 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
1920 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
1921 PSA_ASSERT(psa_hash_finish(&op_finished,
1922 hash, sizeof(hash), &hash_len));
1923 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
1924 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001926 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
1927 PSA_ASSERT(psa_hash_finish(&op_target,
1928 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001930 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
1931 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
1932 PSA_ERROR_BAD_STATE);
1933 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
1934 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001935
1936exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001937 psa_hash_abort(&op_target);
1938 psa_hash_abort(&op_init);
1939 psa_hash_abort(&op_setup);
1940 psa_hash_abort(&op_finished);
1941 psa_hash_abort(&op_aborted);
1942 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001943}
1944/* END_CASE */
1945
itayzafrir58028322018-10-25 10:22:01 +03001946/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001947void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00001948{
Jaeden Amero252ef282019-02-15 14:05:35 +00001949 const uint8_t input[1] = { 0 };
1950
Jaeden Amero769ce272019-01-04 11:48:03 +00001951 /* Test each valid way of initializing the object, except for `= {0}`, as
1952 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1953 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08001954 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001955 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00001956 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1957 psa_mac_operation_t zero;
1958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001959 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00001960
Jaeden Amero252ef282019-02-15 14:05:35 +00001961 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001962 TEST_EQUAL(psa_mac_update(&func,
1963 input, sizeof(input)),
1964 PSA_ERROR_BAD_STATE);
1965 TEST_EQUAL(psa_mac_update(&init,
1966 input, sizeof(input)),
1967 PSA_ERROR_BAD_STATE);
1968 TEST_EQUAL(psa_mac_update(&zero,
1969 input, sizeof(input)),
1970 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00001971
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001972 /* A default MAC operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001973 PSA_ASSERT(psa_mac_abort(&func));
1974 PSA_ASSERT(psa_mac_abort(&init));
1975 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00001976}
1977/* END_CASE */
1978
1979/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001980void mac_setup(int key_type_arg,
1981 data_t *key,
1982 int alg_arg,
1983 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001984{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001985 psa_key_type_t key_type = key_type_arg;
1986 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001987 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001988 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001989 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1990#if defined(KNOWN_SUPPORTED_MAC_ALG)
1991 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1992#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001994 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001996 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
1997 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001998 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001999 }
2000 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002001
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002002 /* The operation object should be reusable. */
2003#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
2005 smoke_test_key_data,
2006 sizeof(smoke_test_key_data),
2007 KNOWN_SUPPORTED_MAC_ALG,
2008 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002009 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002010 }
2011 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002012#endif
2013
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002014exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002015 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002016}
2017/* END_CASE */
2018
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002019/* 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 +01002020void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00002021{
Ronald Cron5425a212020-08-04 14:58:35 +02002022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002023 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2024 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002025 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002026 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2027 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
2029 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002030 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002031 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2032 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2033 size_t sign_mac_length = 0;
2034 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2035 const uint8_t verify_mac[] = {
2036 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2037 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002038 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
2039 };
Jaeden Amero252ef282019-02-15 14:05:35 +00002040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002041 PSA_ASSERT(psa_crypto_init());
2042 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
2043 psa_set_key_algorithm(&attributes, alg);
2044 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
2047 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002048
Jaeden Amero252ef282019-02-15 14:05:35 +00002049 /* Call update without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002050 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
2051 PSA_ERROR_BAD_STATE);
2052 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002053
2054 /* Call sign finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002055 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
2056 &sign_mac_length),
2057 PSA_ERROR_BAD_STATE);
2058 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002059
2060 /* Call verify finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002061 TEST_EQUAL(psa_mac_verify_finish(&operation,
2062 verify_mac, sizeof(verify_mac)),
2063 PSA_ERROR_BAD_STATE);
2064 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002065
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002066 /* Call setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2068 ASSERT_OPERATION_IS_ACTIVE(operation);
2069 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
2070 PSA_ERROR_BAD_STATE);
2071 ASSERT_OPERATION_IS_INACTIVE(operation);
2072 PSA_ASSERT(psa_mac_abort(&operation));
2073 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002074
Jaeden Amero252ef282019-02-15 14:05:35 +00002075 /* Call update after sign finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002076 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2077 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2078 PSA_ASSERT(psa_mac_sign_finish(&operation,
2079 sign_mac, sizeof(sign_mac),
2080 &sign_mac_length));
2081 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
2082 PSA_ERROR_BAD_STATE);
2083 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002084
2085 /* Call update after verify finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002086 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2087 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2088 PSA_ASSERT(psa_mac_verify_finish(&operation,
2089 verify_mac, sizeof(verify_mac)));
2090 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
2091 PSA_ERROR_BAD_STATE);
2092 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002093
2094 /* Call sign finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002095 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2096 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2097 PSA_ASSERT(psa_mac_sign_finish(&operation,
2098 sign_mac, sizeof(sign_mac),
2099 &sign_mac_length));
2100 TEST_EQUAL(psa_mac_sign_finish(&operation,
2101 sign_mac, sizeof(sign_mac),
2102 &sign_mac_length),
2103 PSA_ERROR_BAD_STATE);
2104 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002105
2106 /* Call verify finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002107 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2108 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2109 PSA_ASSERT(psa_mac_verify_finish(&operation,
2110 verify_mac, sizeof(verify_mac)));
2111 TEST_EQUAL(psa_mac_verify_finish(&operation,
2112 verify_mac, sizeof(verify_mac)),
2113 PSA_ERROR_BAD_STATE);
2114 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00002115
2116 /* Setup sign but try verify. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002117 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2118 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2119 ASSERT_OPERATION_IS_ACTIVE(operation);
2120 TEST_EQUAL(psa_mac_verify_finish(&operation,
2121 verify_mac, sizeof(verify_mac)),
2122 PSA_ERROR_BAD_STATE);
2123 ASSERT_OPERATION_IS_INACTIVE(operation);
2124 PSA_ASSERT(psa_mac_abort(&operation));
2125 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00002126
2127 /* Setup verify but try sign. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002128 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2129 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
2130 ASSERT_OPERATION_IS_ACTIVE(operation);
2131 TEST_EQUAL(psa_mac_sign_finish(&operation,
2132 sign_mac, sizeof(sign_mac),
2133 &sign_mac_length),
2134 PSA_ERROR_BAD_STATE);
2135 ASSERT_OPERATION_IS_INACTIVE(operation);
2136 PSA_ASSERT(psa_mac_abort(&operation));
2137 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002140
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002141exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002142 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002143}
2144/* END_CASE */
2145
2146/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002147void mac_sign(int key_type_arg,
2148 data_t *key_data,
2149 int alg_arg,
2150 data_t *input,
2151 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002152{
Ronald Cron5425a212020-08-04 14:58:35 +02002153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002154 psa_key_type_t key_type = key_type_arg;
2155 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002156 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002158 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002159 size_t mac_buffer_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002160 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002161 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002162 const size_t output_sizes_to_test[] = {
2163 0,
2164 1,
2165 expected_mac->len - 1,
2166 expected_mac->len,
2167 expected_mac->len + 1,
2168 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002170 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002171 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002172 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002174 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002176 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
2177 psa_set_key_algorithm(&attributes, alg);
2178 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002180 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2181 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002183 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02002184 const size_t output_size = output_sizes_to_test[i];
2185 psa_status_t expected_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002186 (output_size >= expected_mac->len ? PSA_SUCCESS :
2187 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002189 mbedtls_test_set_step(output_size);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002190 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002191
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002192 /* Calculate the MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002193 TEST_EQUAL(psa_mac_compute(key, alg,
2194 input->x, input->len,
2195 actual_mac, output_size, &mac_length),
2196 expected_status);
2197 if (expected_status == PSA_SUCCESS) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002198 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002199 actual_mac, mac_length);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002200 }
2201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002202 if (output_size > 0) {
2203 memset(actual_mac, 0, output_size);
2204 }
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002205
2206 /* Calculate the MAC, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002207 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
2208 PSA_ASSERT(psa_mac_update(&operation,
2209 input->x, input->len));
2210 TEST_EQUAL(psa_mac_sign_finish(&operation,
2211 actual_mac, output_size,
2212 &mac_length),
2213 expected_status);
2214 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02002215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 if (expected_status == PSA_SUCCESS) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002217 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02002219 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002220 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02002221 actual_mac = NULL;
2222 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002223
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002224exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002225 psa_mac_abort(&operation);
2226 psa_destroy_key(key);
2227 PSA_DONE();
2228 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002229}
2230/* END_CASE */
2231
2232/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233void mac_verify(int key_type_arg,
2234 data_t *key_data,
2235 int alg_arg,
2236 data_t *input,
2237 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002238{
Ronald Cron5425a212020-08-04 14:58:35 +02002239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002240 psa_key_type_t key_type = key_type_arg;
2241 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002242 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002244 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002246 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02002247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01002249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002250 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
2251 psa_set_key_algorithm(&attributes, alg);
2252 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07002253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2255 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002256
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002257 /* Verify correct MAC, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
2259 expected_mac->x, expected_mac->len));
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002260
2261 /* Verify correct MAC, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2263 PSA_ASSERT(psa_mac_update(&operation,
2264 input->x, input->len));
2265 PSA_ASSERT(psa_mac_verify_finish(&operation,
2266 expected_mac->x,
2267 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01002268
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002269 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002270 TEST_EQUAL(psa_mac_verify(key, alg,
2271 input->x, input->len,
2272 expected_mac->x,
2273 expected_mac->len - 1),
2274 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002275
2276 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2278 PSA_ASSERT(psa_mac_update(&operation,
2279 input->x, input->len));
2280 TEST_EQUAL(psa_mac_verify_finish(&operation,
2281 expected_mac->x,
2282 expected_mac->len - 1),
2283 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002284
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002285 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002286 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002287 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
2288 TEST_EQUAL(psa_mac_verify(key, alg,
2289 input->x, input->len,
2290 perturbed_mac, expected_mac->len + 1),
2291 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002292
2293 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002294 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2295 PSA_ASSERT(psa_mac_update(&operation,
2296 input->x, input->len));
2297 TEST_EQUAL(psa_mac_verify_finish(&operation,
2298 perturbed_mac,
2299 expected_mac->len + 1),
2300 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002301
2302 /* Test changing one byte. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002303 for (size_t i = 0; i < expected_mac->len; i++) {
2304 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002305 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002307 TEST_EQUAL(psa_mac_verify(key, alg,
2308 input->x, input->len,
2309 perturbed_mac, expected_mac->len),
2310 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002312 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
2313 PSA_ASSERT(psa_mac_update(&operation,
2314 input->x, input->len));
2315 TEST_EQUAL(psa_mac_verify_finish(&operation,
2316 perturbed_mac,
2317 expected_mac->len),
2318 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002319 perturbed_mac[i] ^= 1;
2320 }
2321
Gilles Peskine8c9def32018-02-08 10:02:12 +01002322exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002323 psa_mac_abort(&operation);
2324 psa_destroy_key(key);
2325 PSA_DONE();
2326 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002327}
2328/* END_CASE */
2329
2330/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002331void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00002332{
Jaeden Ameroab439972019-02-15 14:12:05 +00002333 const uint8_t input[1] = { 0 };
2334 unsigned char output[1] = { 0 };
2335 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002336 /* Test each valid way of initializing the object, except for `= {0}`, as
2337 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2338 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08002339 * to suppress the Clang warning for the test. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002340 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00002341 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2342 psa_cipher_operation_t zero;
2343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002344 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00002345
Jaeden Ameroab439972019-02-15 14:12:05 +00002346 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002347 TEST_EQUAL(psa_cipher_update(&func,
2348 input, sizeof(input),
2349 output, sizeof(output),
2350 &output_length),
2351 PSA_ERROR_BAD_STATE);
2352 TEST_EQUAL(psa_cipher_update(&init,
2353 input, sizeof(input),
2354 output, sizeof(output),
2355 &output_length),
2356 PSA_ERROR_BAD_STATE);
2357 TEST_EQUAL(psa_cipher_update(&zero,
2358 input, sizeof(input),
2359 output, sizeof(output),
2360 &output_length),
2361 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00002362
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002363 /* A default cipher operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 PSA_ASSERT(psa_cipher_abort(&func));
2365 PSA_ASSERT(psa_cipher_abort(&init));
2366 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00002367}
2368/* END_CASE */
2369
2370/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002371void cipher_setup(int key_type_arg,
2372 data_t *key,
2373 int alg_arg,
2374 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002375{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002376 psa_key_type_t key_type = key_type_arg;
2377 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002378 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002379 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002380 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002381#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002382 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2383#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002385 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
2388 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002389 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002390 }
2391 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002392
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002393 /* The operation object should be reusable. */
2394#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002395 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2396 smoke_test_key_data,
2397 sizeof(smoke_test_key_data),
2398 KNOWN_SUPPORTED_CIPHER_ALG,
2399 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002400 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002401 }
2402 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002403#endif
2404
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002405exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002406 psa_cipher_abort(&operation);
2407 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002408}
2409/* END_CASE */
2410
Ronald Cronee414c72021-03-18 18:50:08 +01002411/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002412void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00002413{
Ronald Cron5425a212020-08-04 14:58:35 +02002414 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002415 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2416 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002418 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002419 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002420 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002422 0xaa, 0xaa, 0xaa, 0xaa
2423 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 const uint8_t text[] = {
2425 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002426 0xbb, 0xbb, 0xbb, 0xbb
2427 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002428 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002429 size_t length = 0;
2430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002431 PSA_ASSERT(psa_crypto_init());
2432 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
2433 psa_set_key_algorithm(&attributes, alg);
2434 psa_set_key_type(&attributes, key_type);
2435 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
2436 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00002437
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002438 /* Call encrypt setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002439 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2440 ASSERT_OPERATION_IS_ACTIVE(operation);
2441 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
2442 PSA_ERROR_BAD_STATE);
2443 ASSERT_OPERATION_IS_INACTIVE(operation);
2444 PSA_ASSERT(psa_cipher_abort(&operation));
2445 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002446
2447 /* Call decrypt setup twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002448 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
2449 ASSERT_OPERATION_IS_ACTIVE(operation);
2450 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
2451 PSA_ERROR_BAD_STATE);
2452 ASSERT_OPERATION_IS_INACTIVE(operation);
2453 PSA_ASSERT(psa_cipher_abort(&operation));
2454 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002455
Jaeden Ameroab439972019-02-15 14:12:05 +00002456 /* Generate an IV without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002457 TEST_EQUAL(psa_cipher_generate_iv(&operation,
2458 buffer, sizeof(buffer),
2459 &length),
2460 PSA_ERROR_BAD_STATE);
2461 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002462
2463 /* Generate an IV twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002464 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2465 PSA_ASSERT(psa_cipher_generate_iv(&operation,
2466 buffer, sizeof(buffer),
2467 &length));
2468 ASSERT_OPERATION_IS_ACTIVE(operation);
2469 TEST_EQUAL(psa_cipher_generate_iv(&operation,
2470 buffer, sizeof(buffer),
2471 &length),
2472 PSA_ERROR_BAD_STATE);
2473 ASSERT_OPERATION_IS_INACTIVE(operation);
2474 PSA_ASSERT(psa_cipher_abort(&operation));
2475 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002476
2477 /* Generate an IV after it's already set. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002478 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2479 PSA_ASSERT(psa_cipher_set_iv(&operation,
2480 iv, sizeof(iv)));
2481 TEST_EQUAL(psa_cipher_generate_iv(&operation,
2482 buffer, sizeof(buffer),
2483 &length),
2484 PSA_ERROR_BAD_STATE);
2485 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002486
2487 /* Set an IV without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002488 TEST_EQUAL(psa_cipher_set_iv(&operation,
2489 iv, sizeof(iv)),
2490 PSA_ERROR_BAD_STATE);
2491 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002492
2493 /* Set an IV after it's already set. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2495 PSA_ASSERT(psa_cipher_set_iv(&operation,
2496 iv, sizeof(iv)));
2497 ASSERT_OPERATION_IS_ACTIVE(operation);
2498 TEST_EQUAL(psa_cipher_set_iv(&operation,
2499 iv, sizeof(iv)),
2500 PSA_ERROR_BAD_STATE);
2501 ASSERT_OPERATION_IS_INACTIVE(operation);
2502 PSA_ASSERT(psa_cipher_abort(&operation));
2503 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002504
2505 /* Set an IV after it's already generated. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002506 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2507 PSA_ASSERT(psa_cipher_generate_iv(&operation,
2508 buffer, sizeof(buffer),
2509 &length));
2510 TEST_EQUAL(psa_cipher_set_iv(&operation,
2511 iv, sizeof(iv)),
2512 PSA_ERROR_BAD_STATE);
2513 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002514
2515 /* Call update without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002516 TEST_EQUAL(psa_cipher_update(&operation,
2517 text, sizeof(text),
2518 buffer, sizeof(buffer),
2519 &length),
2520 PSA_ERROR_BAD_STATE);
2521 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002522
2523 /* Call update without an IV where an IV is required. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002524 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2525 ASSERT_OPERATION_IS_ACTIVE(operation);
2526 TEST_EQUAL(psa_cipher_update(&operation,
2527 text, sizeof(text),
2528 buffer, sizeof(buffer),
2529 &length),
2530 PSA_ERROR_BAD_STATE);
2531 ASSERT_OPERATION_IS_INACTIVE(operation);
2532 PSA_ASSERT(psa_cipher_abort(&operation));
2533 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002534
2535 /* Call update after finish. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002536 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2537 PSA_ASSERT(psa_cipher_set_iv(&operation,
2538 iv, sizeof(iv)));
2539 PSA_ASSERT(psa_cipher_finish(&operation,
2540 buffer, sizeof(buffer), &length));
2541 TEST_EQUAL(psa_cipher_update(&operation,
2542 text, sizeof(text),
2543 buffer, sizeof(buffer),
2544 &length),
2545 PSA_ERROR_BAD_STATE);
2546 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002547
2548 /* Call finish without calling setup beforehand. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002549 TEST_EQUAL(psa_cipher_finish(&operation,
2550 buffer, sizeof(buffer), &length),
2551 PSA_ERROR_BAD_STATE);
2552 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002553
2554 /* Call finish without an IV where an IV is required. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002555 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00002556 /* Not calling update means we are encrypting an empty buffer, which is OK
2557 * for cipher modes with padding. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002558 ASSERT_OPERATION_IS_ACTIVE(operation);
2559 TEST_EQUAL(psa_cipher_finish(&operation,
2560 buffer, sizeof(buffer), &length),
2561 PSA_ERROR_BAD_STATE);
2562 ASSERT_OPERATION_IS_INACTIVE(operation);
2563 PSA_ASSERT(psa_cipher_abort(&operation));
2564 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00002565
2566 /* Call finish twice in a row. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002567 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2568 PSA_ASSERT(psa_cipher_set_iv(&operation,
2569 iv, sizeof(iv)));
2570 PSA_ASSERT(psa_cipher_finish(&operation,
2571 buffer, sizeof(buffer), &length));
2572 TEST_EQUAL(psa_cipher_finish(&operation,
2573 buffer, sizeof(buffer), &length),
2574 PSA_ERROR_BAD_STATE);
2575 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00002576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002578
Jaeden Ameroab439972019-02-15 14:12:05 +00002579exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580 psa_cipher_abort(&operation);
2581 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002582}
2583/* END_CASE */
2584
2585/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002586void cipher_encrypt_fail(int alg_arg,
2587 int key_type_arg,
2588 data_t *key_data,
2589 data_t *input,
2590 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02002591{
Ronald Cron5425a212020-08-04 14:58:35 +02002592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002593 psa_status_t status;
2594 psa_key_type_t key_type = key_type_arg;
2595 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002596 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002597 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002598 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002599 size_t output_length = 0;
2600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002602 if (PSA_ERROR_BAD_STATE != expected_status) {
2603 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002605 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2606 psa_set_key_algorithm(&attributes, alg);
2607 psa_set_key_type(&attributes, key_type);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002609 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
2610 input->len);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002611 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2614 &key));
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002615 }
2616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002617 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
2618 output_buffer_size, &output_length);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002620 TEST_EQUAL(status, expected_status);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002621
2622exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002623 mbedtls_free(output);
2624 psa_destroy_key(key);
2625 PSA_DONE();
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002626}
2627/* END_CASE */
2628
2629/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002630void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
2631 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002632{
2633 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2634 psa_key_type_t key_type = key_type_arg;
2635 psa_algorithm_t alg = alg_arg;
Ronald Cron33c69682021-07-15 09:38:11 +02002636 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2637 uint8_t iv[1] = { 0x5a };
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002638 unsigned char *output = NULL;
2639 size_t output_buffer_size = 0;
Gilles Peskine4da5a852022-04-20 17:09:38 +02002640 size_t output_length, length;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002643 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002644
Gilles Peskine5f504202022-04-20 16:55:03 +02002645 /* Validate size macros */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646 TEST_LE_U(ciphertext->len,
2647 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
2648 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
2649 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
2650 TEST_LE_U(plaintext->len,
2651 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
2652 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
2653 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine69d98172022-04-20 17:07:52 +02002654
Gilles Peskine5f504202022-04-20 16:55:03 +02002655
2656 /* Set up key and output buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002657 psa_set_key_usage_flags(&attributes,
2658 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
2659 psa_set_key_algorithm(&attributes, alg);
2660 psa_set_key_type(&attributes, key_type);
2661 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2662 &key));
2663 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
2664 plaintext->len);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002665 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002666
Gilles Peskine5f504202022-04-20 16:55:03 +02002667 /* set_iv() is not allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002668 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2669 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
2670 PSA_ERROR_BAD_STATE);
2671 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
2672 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
2673 PSA_ERROR_BAD_STATE);
Gilles Peskine5f504202022-04-20 16:55:03 +02002674
2675 /* generate_iv() is not allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002676 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2677 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
2678 &length),
2679 PSA_ERROR_BAD_STATE);
2680 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
2681 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
2682 &length),
2683 PSA_ERROR_BAD_STATE);
Ronald Cron33c69682021-07-15 09:38:11 +02002684
Gilles Peskine4da5a852022-04-20 17:09:38 +02002685 /* Multipart encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002686 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002687 output_length = 0;
2688 length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002689 PSA_ASSERT(psa_cipher_update(&operation,
2690 plaintext->x, plaintext->len,
2691 output, output_buffer_size,
2692 &length));
2693 TEST_LE_U(length, output_buffer_size);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002694 output_length += length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002695 PSA_ASSERT(psa_cipher_finish(&operation,
2696 mbedtls_buffer_offset(output, output_length),
2697 output_buffer_size - output_length,
2698 &length));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002699 output_length += length;
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002700 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002701 output, output_length);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002702
2703 /* Multipart encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002704 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002705 output_length = 0;
2706 length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002707 PSA_ASSERT(psa_cipher_update(&operation,
2708 ciphertext->x, ciphertext->len,
2709 output, output_buffer_size,
2710 &length));
2711 TEST_LE_U(length, output_buffer_size);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002712 output_length += length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002713 PSA_ASSERT(psa_cipher_finish(&operation,
2714 mbedtls_buffer_offset(output, output_length),
2715 output_buffer_size - output_length,
2716 &length));
Gilles Peskine4da5a852022-04-20 17:09:38 +02002717 output_length += length;
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002718 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002719 output, output_length);
Gilles Peskine4da5a852022-04-20 17:09:38 +02002720
Gilles Peskine5f504202022-04-20 16:55:03 +02002721 /* One-shot encryption */
Gilles Peskine69d98172022-04-20 17:07:52 +02002722 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002723 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
2724 output, output_buffer_size,
2725 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002726 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002727 output, output_length);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002728
Gilles Peskine69d98172022-04-20 17:07:52 +02002729 /* One-shot decryption */
2730 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002731 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
2732 output, output_buffer_size,
2733 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002734 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002735 output, output_length);
Gilles Peskine5f504202022-04-20 16:55:03 +02002736
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002737exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002738 mbedtls_free(output);
2739 psa_cipher_abort(&operation);
2740 psa_destroy_key(key);
2741 PSA_DONE();
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002742}
2743/* END_CASE */
2744
2745/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002746void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotted33ef12021-07-14 12:31:21 +01002747{
2748 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2749 psa_algorithm_t alg = alg_arg;
2750 psa_key_type_t key_type = key_type_arg;
2751 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2752 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2753 psa_status_t status;
2754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002755 PSA_ASSERT(psa_crypto_init());
Paul Elliotted33ef12021-07-14 12:31:21 +01002756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2758 psa_set_key_algorithm(&attributes, alg);
2759 psa_set_key_type(&attributes, key_type);
Paul Elliotted33ef12021-07-14 12:31:21 +01002760
2761 /* Usage of either of these two size macros would cause divide by zero
2762 * with incorrect key types previously. Input length should be irrelevant
2763 * here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002764 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
2765 0);
2766 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotted33ef12021-07-14 12:31:21 +01002767
2768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002769 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2770 &key));
Paul Elliotted33ef12021-07-14 12:31:21 +01002771
2772 /* Should fail due to invalid alg type (to support invalid key type).
2773 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002774 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotted33ef12021-07-14 12:31:21 +01002775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002776 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotted33ef12021-07-14 12:31:21 +01002777
2778exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002779 psa_cipher_abort(&operation);
2780 psa_destroy_key(key);
2781 PSA_DONE();
Paul Elliotted33ef12021-07-14 12:31:21 +01002782}
2783/* END_CASE */
2784
2785/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002786void cipher_encrypt_validation(int alg_arg,
2787 int key_type_arg,
2788 data_t *key_data,
2789 data_t *input)
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002790{
2791 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2792 psa_key_type_t key_type = key_type_arg;
2793 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002794 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002795 unsigned char *output1 = NULL;
2796 size_t output1_buffer_size = 0;
2797 size_t output1_length = 0;
2798 unsigned char *output2 = NULL;
2799 size_t output2_buffer_size = 0;
2800 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002801 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002802 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002803 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02002806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002807 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2808 psa_set_key_algorithm(&attributes, alg);
2809 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03002810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002811 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
2812 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
2813 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002814 TEST_CALLOC(output1, output1_buffer_size);
2815 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002817 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2818 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002819
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002820 /* The one-shot cipher encryption uses generated iv so validating
2821 the output is not possible. Validating with multipart encryption. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
2823 output1_buffer_size, &output1_length));
2824 TEST_LE_U(output1_length,
2825 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
2826 TEST_LE_U(output1_length,
2827 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002829 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
2830 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002832 PSA_ASSERT(psa_cipher_update(&operation,
2833 input->x, input->len,
2834 output2, output2_buffer_size,
2835 &function_output_length));
2836 TEST_LE_U(function_output_length,
2837 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
2838 TEST_LE_U(function_output_length,
2839 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002840 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002842 PSA_ASSERT(psa_cipher_finish(&operation,
2843 output2 + output2_length,
2844 output2_buffer_size - output2_length,
2845 &function_output_length));
2846 TEST_LE_U(function_output_length,
2847 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
2848 TEST_LE_U(function_output_length,
2849 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002850 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002852 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002853 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002854 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002855
Gilles Peskine50e586b2018-06-08 14:28:46 +02002856exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002857 psa_cipher_abort(&operation);
2858 mbedtls_free(output1);
2859 mbedtls_free(output2);
2860 psa_destroy_key(key);
2861 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02002862}
2863/* END_CASE */
2864
2865/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002866void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
2867 data_t *key_data, data_t *iv,
2868 data_t *input,
2869 int first_part_size_arg,
2870 int output1_length_arg, int output2_length_arg,
2871 data_t *expected_output,
2872 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02002873{
Ronald Cron5425a212020-08-04 14:58:35 +02002874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875 psa_key_type_t key_type = key_type_arg;
2876 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002877 psa_status_t status;
2878 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002879 size_t first_part_size = first_part_size_arg;
2880 size_t output1_length = output1_length_arg;
2881 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002882 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883 size_t output_buffer_size = 0;
2884 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002885 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002886 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02002890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002891 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
2892 psa_set_key_algorithm(&attributes, alg);
2893 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03002894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002895 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2896 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002898 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002900 if (iv->len > 0) {
2901 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002902 }
2903
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002904 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
2905 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01002906 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02002907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002908 TEST_LE_U(first_part_size, input->len);
2909 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
2910 output, output_buffer_size,
2911 &function_output_length));
2912 TEST_ASSERT(function_output_length == output1_length);
2913 TEST_LE_U(function_output_length,
2914 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
2915 TEST_LE_U(function_output_length,
2916 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002917 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002919 if (first_part_size < input->len) {
2920 PSA_ASSERT(psa_cipher_update(&operation,
2921 input->x + first_part_size,
2922 input->len - first_part_size,
2923 (output_buffer_size == 0 ? NULL :
2924 output + total_output_length),
2925 output_buffer_size - total_output_length,
2926 &function_output_length));
2927 TEST_ASSERT(function_output_length == output2_length);
2928 TEST_LE_U(function_output_length,
2929 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
2930 alg,
2931 input->len - first_part_size));
2932 TEST_LE_U(function_output_length,
2933 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002934 total_output_length += function_output_length;
2935 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002937 status = psa_cipher_finish(&operation,
2938 (output_buffer_size == 0 ? NULL :
2939 output + total_output_length),
2940 output_buffer_size - total_output_length,
2941 &function_output_length);
2942 TEST_LE_U(function_output_length,
2943 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
2944 TEST_LE_U(function_output_length,
2945 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002946 total_output_length += function_output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002949 if (expected_status == PSA_SUCCESS) {
2950 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002951
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01002952 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002953 output, total_output_length);
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002954 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002955
2956exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002957 psa_cipher_abort(&operation);
2958 mbedtls_free(output);
2959 psa_destroy_key(key);
2960 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961}
2962/* END_CASE */
2963
2964/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002965void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
2966 data_t *key_data, data_t *iv,
2967 data_t *input,
2968 int first_part_size_arg,
2969 int output1_length_arg, int output2_length_arg,
2970 data_t *expected_output,
2971 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972{
Ronald Cron5425a212020-08-04 14:58:35 +02002973 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974 psa_key_type_t key_type = key_type_arg;
2975 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002976 psa_status_t status;
2977 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002978 size_t first_part_size = first_part_size_arg;
2979 size_t output1_length = output1_length_arg;
2980 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002981 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002982 size_t output_buffer_size = 0;
2983 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002984 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002985 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02002989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002990 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
2991 psa_set_key_algorithm(&attributes, alg);
2992 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03002993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002994 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2995 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002997 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02002998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002999 if (iv->len > 0) {
3000 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003001 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003003 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
3004 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003005 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02003006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003007 TEST_LE_U(first_part_size, input->len);
3008 PSA_ASSERT(psa_cipher_update(&operation,
3009 input->x, first_part_size,
3010 output, output_buffer_size,
3011 &function_output_length));
3012 TEST_ASSERT(function_output_length == output1_length);
3013 TEST_LE_U(function_output_length,
3014 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
3015 TEST_LE_U(function_output_length,
3016 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003017 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003019 if (first_part_size < input->len) {
3020 PSA_ASSERT(psa_cipher_update(&operation,
3021 input->x + first_part_size,
3022 input->len - first_part_size,
3023 (output_buffer_size == 0 ? NULL :
3024 output + total_output_length),
3025 output_buffer_size - total_output_length,
3026 &function_output_length));
3027 TEST_ASSERT(function_output_length == output2_length);
3028 TEST_LE_U(function_output_length,
3029 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3030 alg,
3031 input->len - first_part_size));
3032 TEST_LE_U(function_output_length,
3033 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02003034 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003035 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003037 status = psa_cipher_finish(&operation,
3038 (output_buffer_size == 0 ? NULL :
3039 output + total_output_length),
3040 output_buffer_size - total_output_length,
3041 &function_output_length);
3042 TEST_LE_U(function_output_length,
3043 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3044 TEST_LE_U(function_output_length,
3045 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003046 total_output_length += function_output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003047 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02003048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049 if (expected_status == PSA_SUCCESS) {
3050 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02003051
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003052 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003053 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02003054 }
3055
Gilles Peskine50e586b2018-06-08 14:28:46 +02003056exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003057 psa_cipher_abort(&operation);
3058 mbedtls_free(output);
3059 psa_destroy_key(key);
3060 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02003061}
3062/* END_CASE */
3063
Gilles Peskine50e586b2018-06-08 14:28:46 +02003064/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003065void cipher_decrypt_fail(int alg_arg,
3066 int key_type_arg,
3067 data_t *key_data,
3068 data_t *iv,
3069 data_t *input_arg,
3070 int expected_status_arg)
3071{
3072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3073 psa_status_t status;
3074 psa_key_type_t key_type = key_type_arg;
3075 psa_algorithm_t alg = alg_arg;
3076 psa_status_t expected_status = expected_status_arg;
3077 unsigned char *input = NULL;
3078 size_t input_buffer_size = 0;
3079 unsigned char *output = NULL;
3080 size_t output_buffer_size = 0;
3081 size_t output_length = 0;
3082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3083
3084 if (PSA_ERROR_BAD_STATE != expected_status) {
3085 PSA_ASSERT(psa_crypto_init());
3086
3087 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3088 psa_set_key_algorithm(&attributes, alg);
3089 psa_set_key_type(&attributes, key_type);
3090
3091 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3092 &key));
3093 }
3094
3095 /* Allocate input buffer and copy the iv and the plaintext */
3096 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
3097 if (input_buffer_size > 0) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003098 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003099 memcpy(input, iv->x, iv->len);
3100 memcpy(input + iv->len, input_arg->x, input_arg->len);
3101 }
3102
3103 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003104 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003105
3106 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
3107 output_buffer_size, &output_length);
3108 TEST_EQUAL(status, expected_status);
3109
3110exit:
3111 mbedtls_free(input);
3112 mbedtls_free(output);
3113 psa_destroy_key(key);
3114 PSA_DONE();
3115}
3116/* END_CASE */
3117
3118/* BEGIN_CASE */
3119void cipher_decrypt(int alg_arg,
3120 int key_type_arg,
3121 data_t *key_data,
3122 data_t *iv,
3123 data_t *input_arg,
3124 data_t *expected_output)
3125{
3126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3127 psa_key_type_t key_type = key_type_arg;
3128 psa_algorithm_t alg = alg_arg;
3129 unsigned char *input = NULL;
3130 size_t input_buffer_size = 0;
3131 unsigned char *output = NULL;
3132 size_t output_buffer_size = 0;
3133 size_t output_length = 0;
3134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3135
3136 PSA_ASSERT(psa_crypto_init());
3137
3138 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3139 psa_set_key_algorithm(&attributes, alg);
3140 psa_set_key_type(&attributes, key_type);
3141
3142 /* Allocate input buffer and copy the iv and the plaintext */
3143 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
3144 if (input_buffer_size > 0) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003145 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003146 memcpy(input, iv->x, iv->len);
3147 memcpy(input + iv->len, input_arg->x, input_arg->len);
3148 }
3149
3150 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003151 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003152
3153 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3154 &key));
3155
3156 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
3157 output_buffer_size, &output_length));
3158 TEST_LE_U(output_length,
3159 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
3160 TEST_LE_U(output_length,
3161 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
3162
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003163 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003164 output, output_length);
3165exit:
3166 mbedtls_free(input);
3167 mbedtls_free(output);
3168 psa_destroy_key(key);
3169 PSA_DONE();
3170}
3171/* END_CASE */
3172
3173/* BEGIN_CASE */
3174void cipher_verify_output(int alg_arg,
gabor-mezei-arm43611b02021-06-25 15:49:14 +02003175 int key_type_arg,
3176 data_t *key_data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003177 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02003178{
Ronald Cron5425a212020-08-04 14:58:35 +02003179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003180 psa_key_type_t key_type = key_type_arg;
3181 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003182 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003183 size_t output1_size = 0;
3184 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003185 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003186 size_t output2_size = 0;
3187 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003190 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02003191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003192 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3193 psa_set_key_algorithm(&attributes, alg);
3194 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03003195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003196 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3197 &key));
3198 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003199 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03003200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003201 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
3202 output1, output1_size,
3203 &output1_length));
3204 TEST_LE_U(output1_length,
3205 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
3206 TEST_LE_U(output1_length,
3207 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03003208
3209 output2_size = output1_length;
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003210 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03003211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003212 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
3213 output2, output2_size,
3214 &output2_length));
3215 TEST_LE_U(output2_length,
3216 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
3217 TEST_LE_U(output2_length,
3218 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03003219
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003220 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03003221
3222exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003223 mbedtls_free(output1);
3224 mbedtls_free(output2);
3225 psa_destroy_key(key);
3226 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03003227}
3228/* END_CASE */
3229
3230/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003231void cipher_verify_output_multipart(int alg_arg,
3232 int key_type_arg,
3233 data_t *key_data,
3234 data_t *input,
3235 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03003236{
Ronald Cron5425a212020-08-04 14:58:35 +02003237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003238 psa_key_type_t key_type = key_type_arg;
3239 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003240 size_t first_part_size = first_part_size_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003241 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03003242 size_t iv_size = 16;
3243 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003244 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003245 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003246 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003247 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003248 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003249 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003250 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003251 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3252 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003255 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03003256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003257 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3258 psa_set_key_algorithm(&attributes, alg);
3259 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03003260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003261 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3262 &key));
Moran Pekerded84402018-06-06 16:36:50 +03003263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003264 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
3265 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03003266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003267 if (alg != PSA_ALG_ECB_NO_PADDING) {
3268 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
3269 iv, iv_size,
3270 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003271 }
3272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003273 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3274 TEST_LE_U(output1_buffer_size,
3275 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003276 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03003277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003278 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003280 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
3281 output1, output1_buffer_size,
3282 &function_output_length));
3283 TEST_LE_U(function_output_length,
3284 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
3285 TEST_LE_U(function_output_length,
3286 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003287 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003289 PSA_ASSERT(psa_cipher_update(&operation1,
3290 input->x + first_part_size,
3291 input->len - first_part_size,
3292 output1, output1_buffer_size,
3293 &function_output_length));
3294 TEST_LE_U(function_output_length,
3295 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3296 alg,
3297 input->len - first_part_size));
3298 TEST_LE_U(function_output_length,
3299 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003300 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003302 PSA_ASSERT(psa_cipher_finish(&operation1,
3303 output1 + output1_length,
3304 output1_buffer_size - output1_length,
3305 &function_output_length));
3306 TEST_LE_U(function_output_length,
3307 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3308 TEST_LE_U(function_output_length,
3309 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02003310 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003312 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02003313
Gilles Peskine048b7f02018-06-08 14:20:49 +02003314 output2_buffer_size = output1_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003315 TEST_LE_U(output2_buffer_size,
3316 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
3317 TEST_LE_U(output2_buffer_size,
3318 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003319 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02003320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003321 if (iv_length > 0) {
3322 PSA_ASSERT(psa_cipher_set_iv(&operation2,
3323 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003324 }
Moran Pekerded84402018-06-06 16:36:50 +03003325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003326 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
3327 output2, output2_buffer_size,
3328 &function_output_length));
3329 TEST_LE_U(function_output_length,
3330 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
3331 TEST_LE_U(function_output_length,
3332 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003333 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003335 PSA_ASSERT(psa_cipher_update(&operation2,
3336 output1 + first_part_size,
3337 output1_length - first_part_size,
3338 output2, output2_buffer_size,
3339 &function_output_length));
3340 TEST_LE_U(function_output_length,
3341 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
3342 alg,
3343 output1_length - first_part_size));
3344 TEST_LE_U(function_output_length,
3345 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02003346 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003348 PSA_ASSERT(psa_cipher_finish(&operation2,
3349 output2 + output2_length,
3350 output2_buffer_size - output2_length,
3351 &function_output_length));
3352 TEST_LE_U(function_output_length,
3353 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
3354 TEST_LE_U(function_output_length,
3355 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02003356 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003358 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02003359
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003360 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02003361
3362exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003363 psa_cipher_abort(&operation1);
3364 psa_cipher_abort(&operation2);
3365 mbedtls_free(output1);
3366 mbedtls_free(output2);
3367 psa_destroy_key(key);
3368 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02003369}
3370/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003371
Gilles Peskine20035e32018-02-03 22:44:14 +01003372/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003373void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
3374 int alg_arg,
3375 data_t *nonce,
3376 data_t *additional_data,
3377 data_t *input_data,
3378 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379{
Ronald Cron5425a212020-08-04 14:58:35 +02003380 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003381 psa_key_type_t key_type = key_type_arg;
3382 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003383 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003384 unsigned char *output_data = NULL;
3385 size_t output_size = 0;
3386 size_t output_length = 0;
3387 unsigned char *output_data2 = NULL;
3388 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003389 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003390 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003391 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003393 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3396 psa_set_key_algorithm(&attributes, alg);
3397 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003399 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3400 &key));
3401 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3402 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003404 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3405 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003406 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3407 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003408 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3409 expected_result != PSA_ERROR_NOT_SUPPORTED) {
3410 TEST_EQUAL(output_size,
3411 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3412 TEST_ASSERT(output_size <=
3413 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01003414 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003415 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003417 status = psa_aead_encrypt(key, alg,
3418 nonce->x, nonce->len,
3419 additional_data->x,
3420 additional_data->len,
3421 input_data->x, input_data->len,
3422 output_data, output_size,
3423 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01003424
3425 /* If the operation is not supported, just skip and not fail in case the
3426 * encryption involves a common limitation of cryptography hardwares and
3427 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003428 if (status == PSA_ERROR_NOT_SUPPORTED) {
3429 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3430 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01003431 }
3432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003433 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003436 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437
Gilles Peskine003a4a92019-05-14 16:09:40 +02003438 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3439 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003440 TEST_EQUAL(input_data->len,
3441 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02003442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003443 TEST_ASSERT(input_data->len <=
3444 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01003445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 TEST_EQUAL(psa_aead_decrypt(key, alg,
3447 nonce->x, nonce->len,
3448 additional_data->x,
3449 additional_data->len,
3450 output_data, output_length,
3451 output_data2, output_length,
3452 &output_length2),
3453 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02003454
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003455 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003456 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003457 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003458
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 psa_destroy_key(key);
3461 mbedtls_free(output_data);
3462 mbedtls_free(output_data2);
3463 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003464}
3465/* END_CASE */
3466
3467/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003468void aead_encrypt(int key_type_arg, data_t *key_data,
3469 int alg_arg,
3470 data_t *nonce,
3471 data_t *additional_data,
3472 data_t *input_data,
3473 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474{
Ronald Cron5425a212020-08-04 14:58:35 +02003475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003476 psa_key_type_t key_type = key_type_arg;
3477 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003478 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479 unsigned char *output_data = NULL;
3480 size_t output_size = 0;
3481 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003483 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003485 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003487 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3488 psa_set_key_algorithm(&attributes, alg);
3489 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003491 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3492 &key));
3493 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3494 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003496 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3497 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003498 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3499 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500 TEST_EQUAL(output_size,
3501 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3502 TEST_ASSERT(output_size <=
3503 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003504 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003506 status = psa_aead_encrypt(key, alg,
3507 nonce->x, nonce->len,
3508 additional_data->x, additional_data->len,
3509 input_data->x, input_data->len,
3510 output_data, output_size,
3511 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003512
Ronald Cron28a45ed2021-02-09 20:35:42 +01003513 /* If the operation is not supported, just skip and not fail in case the
3514 * encryption involves a common limitation of cryptography hardwares and
3515 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003516 if (status == PSA_ERROR_NOT_SUPPORTED) {
3517 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3518 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01003519 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 PSA_ASSERT(status);
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003522 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003523 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003524
Gilles Peskinea1cac842018-06-11 19:33:02 +02003525exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 psa_destroy_key(key);
3527 mbedtls_free(output_data);
3528 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003529}
3530/* END_CASE */
3531
3532/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003533void aead_decrypt(int key_type_arg, data_t *key_data,
3534 int alg_arg,
3535 data_t *nonce,
3536 data_t *additional_data,
3537 data_t *input_data,
3538 data_t *expected_data,
3539 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02003540{
Ronald Cron5425a212020-08-04 14:58:35 +02003541 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003542 psa_key_type_t key_type = key_type_arg;
3543 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003544 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003545 unsigned char *output_data = NULL;
3546 size_t output_size = 0;
3547 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003548 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003549 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003550 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003552 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02003553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003554 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
3555 psa_set_key_algorithm(&attributes, alg);
3556 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003558 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3559 &key));
3560 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3561 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01003562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003563 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
3564 alg);
3565 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3566 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01003567 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3568 * should be exact. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003569 TEST_EQUAL(output_size,
3570 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
3571 TEST_ASSERT(output_size <=
3572 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01003573 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003574 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003576 status = psa_aead_decrypt(key, alg,
3577 nonce->x, nonce->len,
3578 additional_data->x,
3579 additional_data->len,
3580 input_data->x, input_data->len,
3581 output_data, output_size,
3582 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01003583
Ronald Cron28a45ed2021-02-09 20:35:42 +01003584 /* If the operation is not supported, just skip and not fail in case the
3585 * decryption involves a common limitation of cryptography hardwares and
3586 * an alternative implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 if (status == PSA_ERROR_NOT_SUPPORTED) {
3588 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
3589 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01003590 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003592 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02003593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003594 if (expected_result == PSA_SUCCESS) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003595 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 output_data, output_length);
3597 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02003598
Gilles Peskinea1cac842018-06-11 19:33:02 +02003599exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003600 psa_destroy_key(key);
3601 mbedtls_free(output_data);
3602 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02003603}
3604/* END_CASE */
3605
3606/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003607void signature_size(int type_arg,
3608 int bits,
3609 int alg_arg,
3610 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003611{
3612 psa_key_type_t type = type_arg;
3613 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003614 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01003615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003616 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01003617#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003618 TEST_EQUAL(actual_size,
3619 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg));
Gilles Peskine841b14b2019-11-26 17:37:37 +01003620#endif /* MBEDTLS_TEST_DEPRECATED */
3621
Gilles Peskinee59236f2018-01-27 23:32:46 +01003622exit:
3623 ;
3624}
3625/* END_CASE */
3626
3627/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003628void sign_hash_deterministic(int key_type_arg, data_t *key_data,
3629 int alg_arg, data_t *input_data,
3630 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003631{
Ronald Cron5425a212020-08-04 14:58:35 +02003632 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003633 psa_key_type_t key_type = key_type_arg;
3634 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003635 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003636 unsigned char *signature = NULL;
3637 size_t signature_size;
3638 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003639 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003641 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01003642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003643 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3644 psa_set_key_algorithm(&attributes, alg);
3645 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07003646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003647 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3648 &key));
3649 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3650 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01003651
Shaun Case0e7791f2021-12-20 21:14:10 -08003652 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003653 * library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003654 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
3655 key_bits, alg);
3656 TEST_ASSERT(signature_size != 0);
3657 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003658 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003659
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003660 /* Perform the signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 PSA_ASSERT(psa_sign_hash(key, alg,
3662 input_data->x, input_data->len,
3663 signature, signature_size,
3664 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003665 /* Verify that the signature is what is expected. */
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003666 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003667 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003668
Gilles Peskine0627f982019-11-26 19:12:16 +01003669#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 memset(signature, 0, signature_size);
Gilles Peskine895242b2019-11-29 12:15:40 +01003671 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 PSA_ASSERT(psa_asymmetric_sign(key, alg,
3673 input_data->x, input_data->len,
3674 signature, signature_size,
3675 &signature_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003676 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003677 signature, signature_length);
Gilles Peskine0627f982019-11-26 19:12:16 +01003678#endif /* MBEDTLS_TEST_DEPRECATED */
3679
Gilles Peskine20035e32018-02-03 22:44:14 +01003680exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003681 /*
3682 * Key attributes may have been returned by psa_get_key_attributes()
3683 * thus reset them as required.
3684 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003685 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003687 psa_destroy_key(key);
3688 mbedtls_free(signature);
3689 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01003690}
3691/* END_CASE */
3692
3693/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003694void sign_hash_fail(int key_type_arg, data_t *key_data,
3695 int alg_arg, data_t *input_data,
3696 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01003697{
Ronald Cron5425a212020-08-04 14:58:35 +02003698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003699 psa_key_type_t key_type = key_type_arg;
3700 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003701 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003702 psa_status_t actual_status;
3703 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003704 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003705 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003706 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003707
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003708 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01003711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003712 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3713 psa_set_key_algorithm(&attributes, alg);
3714 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07003715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003716 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3717 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01003718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719 actual_status = psa_sign_hash(key, alg,
3720 input_data->x, input_data->len,
3721 signature, signature_size,
3722 &signature_length);
3723 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003724 /* The value of *signature_length is unspecified on error, but
3725 * whatever it is, it should be less than signature_size, so that
3726 * if the caller tries to read *signature_length bytes without
3727 * checking the error code then they don't overflow a buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003728 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01003729
Gilles Peskine895242b2019-11-29 12:15:40 +01003730#if defined(MBEDTLS_TEST_DEPRECATED)
3731 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003732 TEST_EQUAL(psa_asymmetric_sign(key, alg,
3733 input_data->x, input_data->len,
3734 signature, signature_size,
3735 &signature_length),
3736 expected_status);
3737 TEST_LE_U(signature_length, signature_size);
Gilles Peskine895242b2019-11-29 12:15:40 +01003738#endif /* MBEDTLS_TEST_DEPRECATED */
3739
Gilles Peskine20035e32018-02-03 22:44:14 +01003740exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003741 psa_reset_key_attributes(&attributes);
3742 psa_destroy_key(key);
3743 mbedtls_free(signature);
3744 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01003745}
3746/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003747
3748/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003749void sign_verify_hash(int key_type_arg, data_t *key_data,
3750 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02003751{
Ronald Cron5425a212020-08-04 14:58:35 +02003752 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003753 psa_key_type_t key_type = key_type_arg;
3754 psa_algorithm_t alg = alg_arg;
3755 size_t key_bits;
3756 unsigned char *signature = NULL;
3757 size_t signature_size;
3758 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003761 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02003762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003763 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3764 psa_set_key_algorithm(&attributes, alg);
3765 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02003766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3768 &key));
3769 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3770 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02003771
Shaun Case0e7791f2021-12-20 21:14:10 -08003772 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02003773 * library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
3775 key_bits, alg);
3776 TEST_ASSERT(signature_size != 0);
3777 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003778 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02003779
3780 /* Perform the signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003781 PSA_ASSERT(psa_sign_hash(key, alg,
3782 input_data->x, input_data->len,
3783 signature, signature_size,
3784 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003785 /* Check that the signature length looks sensible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003786 TEST_LE_U(signature_length, signature_size);
3787 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02003788
3789 /* Use the library to verify that the signature is correct. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 PSA_ASSERT(psa_verify_hash(key, alg,
3791 input_data->x, input_data->len,
3792 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02003793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003794 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02003795 /* Flip a bit in the input and verify that the signature is now
3796 * detected as invalid. Flip a bit at the beginning, not at the end,
3797 * because ECDSA may ignore the last few bits of the input. */
3798 input_data->x[0] ^= 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003799 TEST_EQUAL(psa_verify_hash(key, alg,
3800 input_data->x, input_data->len,
3801 signature, signature_length),
3802 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02003803 }
3804
3805exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003806 /*
3807 * Key attributes may have been returned by psa_get_key_attributes()
3808 * thus reset them as required.
3809 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003812 psa_destroy_key(key);
3813 mbedtls_free(signature);
3814 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02003815}
3816/* END_CASE */
3817
3818/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819void verify_hash(int key_type_arg, data_t *key_data,
3820 int alg_arg, data_t *hash_data,
3821 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03003822{
Ronald Cron5425a212020-08-04 14:58:35 +02003823 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003824 psa_key_type_t key_type = key_type_arg;
3825 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003826 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003830 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03003831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003832 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3833 psa_set_key_algorithm(&attributes, alg);
3834 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03003835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003836 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3837 &key));
itayzafrir5c753392018-05-08 11:18:38 +03003838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003839 PSA_ASSERT(psa_verify_hash(key, alg,
3840 hash_data->x, hash_data->len,
3841 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01003842
3843#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003844 PSA_ASSERT(psa_asymmetric_verify(key, alg,
3845 hash_data->x, hash_data->len,
3846 signature_data->x,
3847 signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01003848
3849#endif /* MBEDTLS_TEST_DEPRECATED */
3850
itayzafrir5c753392018-05-08 11:18:38 +03003851exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003852 psa_reset_key_attributes(&attributes);
3853 psa_destroy_key(key);
3854 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03003855}
3856/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003857
3858/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003859void verify_hash_fail(int key_type_arg, data_t *key_data,
3860 int alg_arg, data_t *hash_data,
3861 data_t *signature_data,
3862 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003863{
Ronald Cron5425a212020-08-04 14:58:35 +02003864 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865 psa_key_type_t key_type = key_type_arg;
3866 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003867 psa_status_t actual_status;
3868 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003869 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003871 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003873 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3874 psa_set_key_algorithm(&attributes, alg);
3875 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3878 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 actual_status = psa_verify_hash(key, alg,
3881 hash_data->x, hash_data->len,
3882 signature_data->x, signature_data->len);
3883 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884
Gilles Peskine895242b2019-11-29 12:15:40 +01003885#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003886 TEST_EQUAL(psa_asymmetric_verify(key, alg,
3887 hash_data->x, hash_data->len,
3888 signature_data->x, signature_data->len),
3889 expected_status);
Gilles Peskine895242b2019-11-29 12:15:40 +01003890#endif /* MBEDTLS_TEST_DEPRECATED */
3891
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003892exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003893 psa_reset_key_attributes(&attributes);
3894 psa_destroy_key(key);
3895 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896}
3897/* END_CASE */
3898
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003900void sign_message_deterministic(int key_type_arg,
3901 data_t *key_data,
3902 int alg_arg,
3903 data_t *input_data,
3904 data_t *output_data)
gabor-mezei-armabd72582021-04-15 18:19:50 +02003905{
3906 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3907 psa_key_type_t key_type = key_type_arg;
3908 psa_algorithm_t alg = alg_arg;
3909 size_t key_bits;
3910 unsigned char *signature = NULL;
3911 size_t signature_size;
3912 size_t signature_length = 0xdeadbeef;
3913 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armabd72582021-04-15 18:19:50 +02003916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003917 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3918 psa_set_key_algorithm(&attributes, alg);
3919 psa_set_key_type(&attributes, key_type);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003921 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3922 &key));
3923 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
3924 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003926 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
3927 TEST_ASSERT(signature_size != 0);
3928 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003929 TEST_CALLOC(signature, signature_size);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003931 PSA_ASSERT(psa_sign_message(key, alg,
3932 input_data->x, input_data->len,
3933 signature, signature_size,
3934 &signature_length));
gabor-mezei-armabd72582021-04-15 18:19:50 +02003935
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01003936 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003937 signature, signature_length);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003938
3939exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003940 psa_reset_key_attributes(&attributes);
gabor-mezei-armabd72582021-04-15 18:19:50 +02003941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003942 psa_destroy_key(key);
3943 mbedtls_free(signature);
3944 PSA_DONE();
gabor-mezei-armabd72582021-04-15 18:19:50 +02003945
3946}
3947/* END_CASE */
3948
3949/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950void sign_message_fail(int key_type_arg,
3951 data_t *key_data,
3952 int alg_arg,
3953 data_t *input_data,
3954 int signature_size_arg,
3955 int expected_status_arg)
3956{
3957 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3958 psa_key_type_t key_type = key_type_arg;
3959 psa_algorithm_t alg = alg_arg;
3960 size_t signature_size = signature_size_arg;
3961 psa_status_t actual_status;
3962 psa_status_t expected_status = expected_status_arg;
3963 unsigned char *signature = NULL;
3964 size_t signature_length = 0xdeadbeef;
3965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3966
Tom Cosgrove30ceb232023-09-04 11:20:19 +01003967 TEST_CALLOC(signature, signature_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003968
3969 PSA_ASSERT(psa_crypto_init());
3970
3971 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
3972 psa_set_key_algorithm(&attributes, alg);
3973 psa_set_key_type(&attributes, key_type);
3974
3975 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3976 &key));
3977
3978 actual_status = psa_sign_message(key, alg,
3979 input_data->x, input_data->len,
3980 signature, signature_size,
3981 &signature_length);
3982 TEST_EQUAL(actual_status, expected_status);
3983 /* The value of *signature_length is unspecified on error, but
3984 * whatever it is, it should be less than signature_size, so that
3985 * if the caller tries to read *signature_length bytes without
3986 * checking the error code then they don't overflow a buffer. */
3987 TEST_LE_U(signature_length, signature_size);
3988
3989exit:
3990 psa_reset_key_attributes(&attributes);
3991 psa_destroy_key(key);
3992 mbedtls_free(signature);
3993 PSA_DONE();
3994}
3995/* END_CASE */
3996
3997/* BEGIN_CASE */
3998void sign_verify_message(int key_type_arg,
3999 data_t *key_data,
4000 int alg_arg,
4001 data_t *input_data)
4002{
4003 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4004 psa_key_type_t key_type = key_type_arg;
4005 psa_algorithm_t alg = alg_arg;
4006 size_t key_bits;
4007 unsigned char *signature = NULL;
4008 size_t signature_size;
4009 size_t signature_length = 0xdeadbeef;
4010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4011
4012 PSA_ASSERT(psa_crypto_init());
4013
4014 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4015 PSA_KEY_USAGE_VERIFY_MESSAGE);
4016 psa_set_key_algorithm(&attributes, alg);
4017 psa_set_key_type(&attributes, key_type);
4018
4019 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4020 &key));
4021 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4022 key_bits = psa_get_key_bits(&attributes);
4023
4024 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
4025 TEST_ASSERT(signature_size != 0);
4026 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004027 TEST_CALLOC(signature, signature_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004028
4029 PSA_ASSERT(psa_sign_message(key, alg,
4030 input_data->x, input_data->len,
4031 signature, signature_size,
4032 &signature_length));
4033 TEST_LE_U(signature_length, signature_size);
4034 TEST_ASSERT(signature_length > 0);
4035
4036 PSA_ASSERT(psa_verify_message(key, alg,
4037 input_data->x, input_data->len,
4038 signature, signature_length));
4039
4040 if (input_data->len != 0) {
4041 /* Flip a bit in the input and verify that the signature is now
4042 * detected as invalid. Flip a bit at the beginning, not at the end,
4043 * because ECDSA may ignore the last few bits of the input. */
4044 input_data->x[0] ^= 1;
4045 TEST_EQUAL(psa_verify_message(key, alg,
4046 input_data->x, input_data->len,
4047 signature, signature_length),
4048 PSA_ERROR_INVALID_SIGNATURE);
4049 }
4050
4051exit:
4052 psa_reset_key_attributes(&attributes);
4053
4054 psa_destroy_key(key);
4055 mbedtls_free(signature);
4056 PSA_DONE();
4057}
4058/* END_CASE */
4059
4060/* BEGIN_CASE */
4061void verify_message(int key_type_arg,
4062 data_t *key_data,
4063 int alg_arg,
4064 data_t *input_data,
4065 data_t *signature_data)
4066{
4067 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4068 psa_key_type_t key_type = key_type_arg;
4069 psa_algorithm_t alg = alg_arg;
4070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4071
4072 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
4073
4074 PSA_ASSERT(psa_crypto_init());
4075
4076 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
4077 psa_set_key_algorithm(&attributes, alg);
4078 psa_set_key_type(&attributes, key_type);
4079
4080 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4081 &key));
4082
4083 PSA_ASSERT(psa_verify_message(key, alg,
4084 input_data->x, input_data->len,
4085 signature_data->x, signature_data->len));
4086
4087exit:
4088 psa_reset_key_attributes(&attributes);
4089 psa_destroy_key(key);
4090 PSA_DONE();
4091}
4092/* END_CASE */
4093
4094/* BEGIN_CASE */
4095void verify_message_fail(int key_type_arg,
4096 data_t *key_data,
4097 int alg_arg,
4098 data_t *hash_data,
4099 data_t *signature_data,
4100 int expected_status_arg)
4101{
4102 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4103 psa_key_type_t key_type = key_type_arg;
4104 psa_algorithm_t alg = alg_arg;
4105 psa_status_t actual_status;
4106 psa_status_t expected_status = expected_status_arg;
4107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4108
4109 PSA_ASSERT(psa_crypto_init());
4110
4111 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
4112 psa_set_key_algorithm(&attributes, alg);
4113 psa_set_key_type(&attributes, key_type);
4114
4115 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4116 &key));
4117
4118 actual_status = psa_verify_message(key, alg,
4119 hash_data->x, hash_data->len,
4120 signature_data->x,
4121 signature_data->len);
4122 TEST_EQUAL(actual_status, expected_status);
4123
4124exit:
4125 psa_reset_key_attributes(&attributes);
4126 psa_destroy_key(key);
4127 PSA_DONE();
4128}
4129/* END_CASE */
4130
4131/* BEGIN_CASE */
4132void asymmetric_encrypt(int key_type_arg,
gabor-mezei-armabd72582021-04-15 18:19:50 +02004133 data_t *key_data,
4134 int alg_arg,
4135 data_t *input_data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004136 data_t *label,
4137 int expected_output_length_arg,
4138 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02004139{
Ronald Cron5425a212020-08-04 14:58:35 +02004140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004141 psa_key_type_t key_type = key_type_arg;
4142 psa_algorithm_t alg = alg_arg;
4143 size_t expected_output_length = expected_output_length_arg;
4144 size_t key_bits;
4145 unsigned char *output = NULL;
4146 size_t output_size;
4147 size_t output_length = ~0;
4148 psa_status_t actual_status;
4149 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004152 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004153
Gilles Peskine656896e2018-06-29 19:12:28 +02004154 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004155 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4156 psa_set_key_algorithm(&attributes, alg);
4157 psa_set_key_type(&attributes, key_type);
4158 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4159 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02004160
4161 /* Determine the maximum output length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004162 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4163 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004165 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4166 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004167 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02004168
4169 /* Encrypt the input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004170 actual_status = psa_asymmetric_encrypt(key, alg,
4171 input_data->x, input_data->len,
4172 label->x, label->len,
4173 output, output_size,
4174 &output_length);
4175 TEST_EQUAL(actual_status, expected_status);
oberon-sk8a23f492023-02-13 13:42:02 +01004176 if (actual_status == PSA_SUCCESS) {
4177 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch6ed14362023-02-22 13:39:21 +01004178 } else {
4179 TEST_LE_U(output_length, output_size);
oberon-sk8a23f492023-02-13 13:42:02 +01004180 }
Gilles Peskine656896e2018-06-29 19:12:28 +02004181
Gilles Peskine68428122018-06-30 18:42:41 +02004182 /* If the label is empty, the test framework puts a non-null pointer
4183 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004184 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004185 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004186 if (output_size != 0) {
4187 memset(output, 0, output_size);
4188 }
4189 actual_status = psa_asymmetric_encrypt(key, alg,
4190 input_data->x, input_data->len,
4191 NULL, label->len,
4192 output, output_size,
4193 &output_length);
4194 TEST_EQUAL(actual_status, expected_status);
oberon-sk8a23f492023-02-13 13:42:02 +01004195 if (actual_status == PSA_SUCCESS) {
4196 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch6ed14362023-02-22 13:39:21 +01004197 } else {
4198 TEST_LE_U(output_length, output_size);
oberon-sk8a23f492023-02-13 13:42:02 +01004199 }
Gilles Peskine68428122018-06-30 18:42:41 +02004200 }
4201
Gilles Peskine656896e2018-06-29 19:12:28 +02004202exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004203 /*
4204 * Key attributes may have been returned by psa_get_key_attributes()
4205 * thus reset them as required.
4206 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004207 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004209 psa_destroy_key(key);
4210 mbedtls_free(output);
4211 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02004212}
4213/* END_CASE */
4214
4215/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004216void asymmetric_encrypt_decrypt(int key_type_arg,
4217 data_t *key_data,
4218 int alg_arg,
4219 data_t *input_data,
4220 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004221{
Ronald Cron5425a212020-08-04 14:58:35 +02004222 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004223 psa_key_type_t key_type = key_type_arg;
4224 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004225 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004226 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004227 size_t output_size;
4228 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004229 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004230 size_t output2_size;
4231 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004234 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004236 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4237 psa_set_key_algorithm(&attributes, alg);
4238 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004240 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4241 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004242
4243 /* Determine the maximum ciphertext length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004244 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4245 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004247 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4248 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004249 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01004250
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004251 output2_size = input_data->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004252 TEST_LE_U(output2_size,
4253 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
4254 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004255 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004256
Gilles Peskineeebd7382018-06-08 18:11:54 +02004257 /* We test encryption by checking that encrypt-then-decrypt gives back
4258 * the original plaintext because of the non-optional random
4259 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004260 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
4261 input_data->x, input_data->len,
4262 label->x, label->len,
4263 output, output_size,
4264 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004265 /* We don't know what ciphertext length to expect, but check that
4266 * it looks sensible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004267 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004269 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4270 output, output_length,
4271 label->x, label->len,
4272 output2, output2_size,
4273 &output2_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004274 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004275 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004276
4277exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004278 /*
4279 * Key attributes may have been returned by psa_get_key_attributes()
4280 * thus reset them as required.
4281 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004282 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004284 psa_destroy_key(key);
4285 mbedtls_free(output);
4286 mbedtls_free(output2);
4287 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004288}
4289/* END_CASE */
4290
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004291/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004292void asymmetric_decrypt(int key_type_arg,
4293 data_t *key_data,
4294 int alg_arg,
4295 data_t *input_data,
4296 data_t *label,
4297 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004298{
Ronald Cron5425a212020-08-04 14:58:35 +02004299 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004300 psa_key_type_t key_type = key_type_arg;
4301 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004302 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004303 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004304 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004305 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004308 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004310 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4311 psa_set_key_algorithm(&attributes, alg);
4312 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004314 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4315 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004317 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4318 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01004319
4320 /* Determine the maximum ciphertext length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004321 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
4322 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004323 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01004324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004325 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4326 input_data->x, input_data->len,
4327 label->x, label->len,
4328 output,
4329 output_size,
4330 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004331 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004332 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004333
Gilles Peskine68428122018-06-30 18:42:41 +02004334 /* If the label is empty, the test framework puts a non-null pointer
4335 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004336 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004337 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004338 if (output_size != 0) {
4339 memset(output, 0, output_size);
4340 }
4341 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
4342 input_data->x, input_data->len,
4343 NULL, label->len,
4344 output,
4345 output_size,
4346 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004347 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004348 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02004349 }
4350
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004351exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004352 psa_reset_key_attributes(&attributes);
4353 psa_destroy_key(key);
4354 mbedtls_free(output);
4355 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004356}
4357/* END_CASE */
4358
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004359/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004360void asymmetric_decrypt_fail(int key_type_arg,
4361 data_t *key_data,
4362 int alg_arg,
4363 data_t *input_data,
4364 data_t *label,
4365 int output_size_arg,
4366 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004367{
Ronald Cron5425a212020-08-04 14:58:35 +02004368 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004369 psa_key_type_t key_type = key_type_arg;
4370 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004371 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004372 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004373 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004374 psa_status_t actual_status;
4375 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004377
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004378 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004380 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4383 psa_set_key_algorithm(&attributes, alg);
4384 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004386 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4387 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004389 actual_status = psa_asymmetric_decrypt(key, alg,
4390 input_data->x, input_data->len,
4391 label->x, label->len,
4392 output, output_size,
4393 &output_length);
4394 TEST_EQUAL(actual_status, expected_status);
4395 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004396
Gilles Peskine68428122018-06-30 18:42:41 +02004397 /* If the label is empty, the test framework puts a non-null pointer
4398 * in label->x. Test that a null pointer works as well. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004399 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02004400 output_length = ~0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004401 if (output_size != 0) {
4402 memset(output, 0, output_size);
4403 }
4404 actual_status = psa_asymmetric_decrypt(key, alg,
4405 input_data->x, input_data->len,
4406 NULL, label->len,
4407 output, output_size,
4408 &output_length);
4409 TEST_EQUAL(actual_status, expected_status);
4410 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02004411 }
4412
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004413exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004414 psa_reset_key_attributes(&attributes);
4415 psa_destroy_key(key);
4416 mbedtls_free(output);
4417 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004418}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004419/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004420
4421/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004422void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00004423{
4424 /* Test each valid way of initializing the object, except for `= {0}`, as
4425 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4426 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08004427 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004428 size_t capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004429 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004430 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4431 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004433 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00004434
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435 /* A default operation should not be able to report its capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004436 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
4437 PSA_ERROR_BAD_STATE);
4438 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
4439 PSA_ERROR_BAD_STATE);
4440 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
4441 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004442
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 /* A default operation should be abortable without error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004444 PSA_ASSERT(psa_key_derivation_abort(&func));
4445 PSA_ASSERT(psa_key_derivation_abort(&init));
4446 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00004447}
4448/* END_CASE */
4449
Janos Follath16de4a42019-06-13 16:32:24 +01004450/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004451void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02004452{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004453 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004454 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004457 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02004458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004459 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
4460 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02004461
4462exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004463 psa_key_derivation_abort(&operation);
4464 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02004465}
4466/* END_CASE */
4467
Janos Follathaf3c2a02019-06-12 12:34:34 +01004468/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004469void derive_set_capacity(int alg_arg, int capacity_arg,
4470 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01004471{
4472 psa_algorithm_t alg = alg_arg;
4473 size_t capacity = capacity_arg;
4474 psa_status_t expected_status = expected_status_arg;
4475 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004477 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01004478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004479 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01004480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
4482 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01004483
4484exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004485 psa_key_derivation_abort(&operation);
4486 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01004487}
4488/* END_CASE */
4489
4490/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004491void derive_input(int alg_arg,
4492 int step_arg1, int key_type_arg1, data_t *input1,
4493 int expected_status_arg1,
4494 int step_arg2, int key_type_arg2, data_t *input2,
4495 int expected_status_arg2,
4496 int step_arg3, int key_type_arg3, data_t *input3,
4497 int expected_status_arg3,
4498 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01004499{
4500 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004501 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
4502 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
4503 psa_status_t expected_statuses[] = { expected_status_arg1,
4504 expected_status_arg2,
4505 expected_status_arg3 };
4506 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02004507 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4508 MBEDTLS_SVC_KEY_ID_INIT,
4509 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004510 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4512 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004513 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004514 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004515 psa_status_t expected_output_status = expected_output_status_arg;
4516 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004518 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01004519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4521 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01004522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01004524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004525 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
4526 mbedtls_test_set_step(i);
4527 if (steps[i] == 0) {
Gilles Peskinef6279312021-05-27 13:21:20 +02004528 /* Skip this step */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004529 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
4530 psa_set_key_type(&attributes, key_types[i]);
4531 PSA_ASSERT(psa_import_key(&attributes,
4532 inputs[i]->x, inputs[i]->len,
4533 &keys[i]));
4534 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
4535 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004536 // When taking a private key as secret input, use key agreement
4537 // to add the shared secret to the derivation
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004538 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
4539 &operation, keys[i]),
4540 expected_statuses[i]);
4541 } else {
4542 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
4543 keys[i]),
4544 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004545 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004546 } else {
4547 TEST_EQUAL(psa_key_derivation_input_bytes(
4548 &operation, steps[i],
4549 inputs[i]->x, inputs[i]->len),
4550 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01004551 }
4552 }
4553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004554 if (output_key_type != PSA_KEY_TYPE_NONE) {
4555 psa_reset_key_attributes(&attributes);
4556 psa_set_key_type(&attributes, output_key_type);
4557 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004558 actual_output_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004559 psa_key_derivation_output_key(&attributes, &operation,
4560 &output_key);
4561 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004562 uint8_t buffer[1];
4563 actual_output_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004564 psa_key_derivation_output_bytes(&operation,
4565 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004566 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004567 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004568
Janos Follathaf3c2a02019-06-12 12:34:34 +01004569exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570 psa_key_derivation_abort(&operation);
4571 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
4572 psa_destroy_key(keys[i]);
4573 }
4574 psa_destroy_key(output_key);
4575 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01004576}
4577/* END_CASE */
4578
Janos Follathd958bb72019-07-03 15:02:16 +01004579/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004581{
Janos Follathd958bb72019-07-03 15:02:16 +01004582 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004583 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004584 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004585 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004586 unsigned char input1[] = "Input 1";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004587 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01004588 unsigned char input2[] = "Input 2";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004589 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004590 uint8_t buffer[42];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004591 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004592 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4593 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004594 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004595 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004597 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004599 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4600 psa_set_key_algorithm(&attributes, alg);
4601 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 PSA_ASSERT(psa_import_key(&attributes,
4604 key_data, sizeof(key_data),
4605 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004606
4607 /* valid key derivation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004608 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
4609 input1, input1_length,
4610 input2, input2_length,
4611 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01004612 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004613 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004614
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004615 /* state of operation shouldn't allow additional generation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004616 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
4617 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004619 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004621 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
4622 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004623
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004624exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004625 psa_key_derivation_abort(&operation);
4626 psa_destroy_key(key);
4627 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004628}
4629/* END_CASE */
4630
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004631/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004632void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004633{
4634 uint8_t output_buffer[16];
4635 size_t buffer_size = 16;
4636 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004637 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004639 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
4640 output_buffer, buffer_size)
4641 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004643 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
4644 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004646 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004648 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
4649 output_buffer, buffer_size)
4650 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004652 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
4653 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004654
4655exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004656 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004657}
4658/* END_CASE */
4659
4660/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004661void derive_output(int alg_arg,
4662 int step1_arg, data_t *input1,
4663 int step2_arg, data_t *input2,
4664 int step3_arg, data_t *input3,
4665 int requested_capacity_arg,
4666 data_t *expected_output1,
4667 data_t *expected_output2)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004668{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004669 psa_algorithm_t alg = alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004670 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg };
4671 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02004672 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4673 MBEDTLS_SVC_KEY_ID_INIT,
4674 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004675 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004676 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004677 uint8_t *expected_outputs[2] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004678 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004679 size_t output_sizes[2] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004680 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004681 size_t output_buffer_size = 0;
4682 uint8_t *output_buffer = NULL;
4683 size_t expected_capacity;
4684 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004686 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004687 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004689 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
4690 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004691 output_buffer_size = output_sizes[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004692 }
4693 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004694 expected_outputs[i] = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004695 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004696 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004697 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004698 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004700 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4701 psa_set_key_algorithm(&attributes, alg);
4702 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004703
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004704 /* Extraction phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004705 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
4706 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
4707 requested_capacity));
4708 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
4709 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02004710 case 0:
4711 break;
4712 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004713 PSA_ASSERT(psa_import_key(&attributes,
4714 inputs[i]->x, inputs[i]->len,
4715 &keys[i]));
gabor-mezei-armceface22021-01-21 12:26:17 +01004716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004717 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
4718 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes));
4719 TEST_ASSERT(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes)) <=
4720 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
gabor-mezei-armceface22021-01-21 12:26:17 +01004721 }
4722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004723 PSA_ASSERT(psa_key_derivation_input_key(
4724 &operation, steps[i], keys[i]));
Gilles Peskine1468da72019-05-29 17:35:49 +02004725 break;
4726 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004727 PSA_ASSERT(psa_key_derivation_input_bytes(
4728 &operation, steps[i],
4729 inputs[i]->x, inputs[i]->len));
Gilles Peskine1468da72019-05-29 17:35:49 +02004730 break;
4731 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004732 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004734 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4735 &current_capacity));
4736 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004737 expected_capacity = requested_capacity;
4738
4739 /* Expansion phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004740 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004741 /* Read some bytes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004742 status = psa_key_derivation_output_bytes(&operation,
4743 output_buffer, output_sizes[i]);
4744 if (expected_capacity == 0 && output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004745 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004746 TEST_ASSERT(status == PSA_SUCCESS ||
4747 status == PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004748 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004749 } else if (expected_capacity == 0 ||
4750 output_sizes[i] > expected_capacity) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004751 /* Capacity exceeded. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004752 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004753 expected_capacity = 0;
4754 continue;
4755 }
4756 /* Success. Check the read data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004757 PSA_ASSERT(status);
4758 if (output_sizes[i] != 0) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004759 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004760 expected_outputs[i], output_sizes[i]);
4761 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004762 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004763 expected_capacity -= output_sizes[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004764 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4765 &current_capacity));
4766 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004767 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004769
4770exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004771 mbedtls_free(output_buffer);
4772 psa_key_derivation_abort(&operation);
4773 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
4774 psa_destroy_key(keys[i]);
4775 }
4776 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004777}
4778/* END_CASE */
4779
4780/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004781void derive_full(int alg_arg,
4782 data_t *key_data,
4783 data_t *input1,
4784 data_t *input2,
4785 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02004786{
Ronald Cron5425a212020-08-04 14:58:35 +02004787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004788 psa_algorithm_t alg = alg_arg;
4789 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004790 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004791 unsigned char output_buffer[16];
4792 size_t expected_capacity = requested_capacity;
4793 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004794 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004796 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02004797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004798 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4799 psa_set_key_algorithm(&attributes, alg);
4800 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02004801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4803 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02004804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004805 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
4806 input1->x, input1->len,
4807 input2->x, input2->len,
4808 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01004809 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004810 }
Janos Follath47f27ed2019-06-25 13:24:52 +01004811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004812 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4813 &current_capacity));
4814 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02004815
4816 /* Expansion phase. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004817 while (current_capacity > 0) {
4818 size_t read_size = sizeof(output_buffer);
4819 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02004820 read_size = current_capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004821 }
4822 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
4823 output_buffer,
4824 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02004825 expected_capacity -= read_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004826 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
4827 &current_capacity));
4828 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02004829 }
4830
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 /* Check that the operation refuses to go over capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004832 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
4833 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02004834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004835 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02004836
4837exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004838 psa_key_derivation_abort(&operation);
4839 psa_destroy_key(key);
4840 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02004841}
4842/* END_CASE */
4843
Janos Follathe60c9052019-07-03 13:51:30 +01004844/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004845void derive_key_exercise(int alg_arg,
4846 data_t *key_data,
4847 data_t *input1,
4848 data_t *input2,
4849 int derived_type_arg,
4850 int derived_bits_arg,
4851 int derived_usage_arg,
4852 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02004853{
Ronald Cron5425a212020-08-04 14:58:35 +02004854 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4855 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004856 psa_algorithm_t alg = alg_arg;
4857 psa_key_type_t derived_type = derived_type_arg;
4858 size_t derived_bits = derived_bits_arg;
4859 psa_key_usage_t derived_usage = derived_usage_arg;
4860 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004861 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004863 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004864 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004866 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02004867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004868 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4869 psa_set_key_algorithm(&attributes, alg);
4870 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
4871 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4872 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004873
4874 /* Derive a key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004875 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4876 input1->x, input1->len,
4877 input2->x, input2->len,
4878 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01004879 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004880 }
Janos Follathe60c9052019-07-03 13:51:30 +01004881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004882 psa_set_key_usage_flags(&attributes, derived_usage);
4883 psa_set_key_algorithm(&attributes, derived_alg);
4884 psa_set_key_type(&attributes, derived_type);
4885 psa_set_key_bits(&attributes, derived_bits);
4886 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
4887 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004888
4889 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004890 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
4891 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
4892 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004893
4894 /* Exercise the derived key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004895 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02004896 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004897 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02004898
4899exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004900 /*
4901 * Key attributes may have been returned by psa_get_key_attributes()
4902 * thus reset them as required.
4903 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004904 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004906 psa_key_derivation_abort(&operation);
4907 psa_destroy_key(base_key);
4908 psa_destroy_key(derived_key);
4909 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02004910}
4911/* END_CASE */
4912
Janos Follath42fd8882019-07-03 14:17:09 +01004913/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004914void derive_key_export(int alg_arg,
4915 data_t *key_data,
4916 data_t *input1,
4917 data_t *input2,
4918 int bytes1_arg,
4919 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02004920{
Ronald Cron5425a212020-08-04 14:58:35 +02004921 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4922 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004923 psa_algorithm_t alg = alg_arg;
4924 size_t bytes1 = bytes1_arg;
4925 size_t bytes2 = bytes2_arg;
4926 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004928 uint8_t *output_buffer = NULL;
4929 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004930 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4931 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004932 size_t length;
4933
Tom Cosgrove30ceb232023-09-04 11:20:19 +01004934 TEST_CALLOC(output_buffer, capacity);
4935 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004936 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02004937
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004938 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
4939 psa_set_key_algorithm(&base_attributes, alg);
4940 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
4941 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
4942 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004943
4944 /* Derive some material and output it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004945 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4946 input1->x, input1->len,
4947 input2->x, input2->len,
4948 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01004949 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004950 }
Janos Follath42fd8882019-07-03 14:17:09 +01004951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004952 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
4953 output_buffer,
4954 capacity));
4955 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02004956
4957 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004958 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
4959 input1->x, input1->len,
4960 input2->x, input2->len,
4961 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01004962 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004963 }
Janos Follath42fd8882019-07-03 14:17:09 +01004964
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004965 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
4966 psa_set_key_algorithm(&derived_attributes, 0);
4967 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
4968 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
4969 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
4970 &derived_key));
4971 PSA_ASSERT(psa_export_key(derived_key,
4972 export_buffer, bytes1,
4973 &length));
4974 TEST_EQUAL(length, bytes1);
4975 PSA_ASSERT(psa_destroy_key(derived_key));
4976 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
4977 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
4978 &derived_key));
4979 PSA_ASSERT(psa_export_key(derived_key,
4980 export_buffer + bytes1, bytes2,
4981 &length));
4982 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004983
4984 /* Compare the outputs from the two runs. */
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01004985 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004986 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02004987
4988exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004989 mbedtls_free(output_buffer);
4990 mbedtls_free(export_buffer);
4991 psa_key_derivation_abort(&operation);
4992 psa_destroy_key(base_key);
4993 psa_destroy_key(derived_key);
4994 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02004995}
4996/* END_CASE */
4997
4998/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004999void derive_key(int alg_arg,
5000 data_t *key_data, data_t *input1, data_t *input2,
5001 int type_arg, int bits_arg,
5002 int expected_status_arg,
5003 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02005004{
Ronald Cron5425a212020-08-04 14:58:35 +02005005 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5006 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005007 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005008 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005009 size_t bits = bits_arg;
5010 psa_status_t expected_status = expected_status_arg;
5011 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5012 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5013 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005015 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02005016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005017 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
5018 psa_set_key_algorithm(&base_attributes, alg);
5019 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
5020 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
5021 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02005022
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005023 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
5024 input1->x, input1->len,
5025 input2->x, input2->len,
5026 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02005027 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005028 }
Gilles Peskinec744d992019-07-30 17:26:54 +02005029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005030 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
5031 psa_set_key_algorithm(&derived_attributes, 0);
5032 psa_set_key_type(&derived_attributes, type);
5033 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01005034
5035 psa_status_t status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005036 psa_key_derivation_output_key(&derived_attributes,
5037 &operation,
5038 &derived_key);
5039 if (is_large_output > 0) {
5040 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
5041 }
5042 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02005043
5044exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005045 psa_key_derivation_abort(&operation);
5046 psa_destroy_key(base_key);
5047 psa_destroy_key(derived_key);
5048 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02005049}
5050/* END_CASE */
5051
5052/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053void key_agreement_setup(int alg_arg,
5054 int our_key_type_arg, int our_key_alg_arg,
5055 data_t *our_key_data, data_t *peer_key_data,
5056 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005057{
Ronald Cron5425a212020-08-04 14:58:35 +02005058 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005059 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005060 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005061 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005063 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005064 psa_status_t expected_status = expected_status_arg;
5065 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02005068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005069 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5070 psa_set_key_algorithm(&attributes, our_key_alg);
5071 psa_set_key_type(&attributes, our_key_type);
5072 PSA_ASSERT(psa_import_key(&attributes,
5073 our_key_data->x, our_key_data->len,
5074 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02005075
Gilles Peskine77f40d82019-04-11 21:27:06 +02005076 /* The tests currently include inputs that should fail at either step.
5077 * Test cases that fail at the setup step should be changed to call
5078 * key_derivation_setup instead, and this function should be renamed
5079 * to key_agreement_fail. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005080 status = psa_key_derivation_setup(&operation, alg);
5081 if (status == PSA_SUCCESS) {
5082 TEST_EQUAL(psa_key_derivation_key_agreement(
5083 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5084 our_key,
5085 peer_key_data->x, peer_key_data->len),
5086 expected_status);
5087 } else {
5088 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02005089 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005090
5091exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005092 psa_key_derivation_abort(&operation);
5093 psa_destroy_key(our_key);
5094 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02005095}
5096/* END_CASE */
5097
5098/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005099void raw_key_agreement(int alg_arg,
5100 int our_key_type_arg, data_t *our_key_data,
5101 data_t *peer_key_data,
5102 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02005103{
Ronald Cron5425a212020-08-04 14:58:35 +02005104 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005105 psa_algorithm_t alg = alg_arg;
5106 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005108 unsigned char *output = NULL;
5109 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005110 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005112 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02005113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005114 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5115 psa_set_key_algorithm(&attributes, alg);
5116 psa_set_key_type(&attributes, our_key_type);
5117 PSA_ASSERT(psa_import_key(&attributes,
5118 our_key_data->x, our_key_data->len,
5119 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02005120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005121 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
5122 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01005123
Gilles Peskine7d150292022-04-13 23:25:52 +02005124 /* Validate size macros */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 TEST_LE_U(expected_output->len,
5126 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
5127 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
5128 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine7d150292022-04-13 23:25:52 +02005129
5130 /* Good case with exact output size */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005131 TEST_CALLOC(output, expected_output->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005132 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
5133 peer_key_data->x, peer_key_data->len,
5134 output, expected_output->len,
5135 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005136 TEST_MEMORY_COMPARE(output, output_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137 expected_output->x, expected_output->len);
5138 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005139 output = NULL;
5140 output_length = ~0;
5141
5142 /* Larger buffer */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005143 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005144 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
5145 peer_key_data->x, peer_key_data->len,
5146 output, expected_output->len + 1,
5147 &output_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005148 TEST_MEMORY_COMPARE(output, output_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005149 expected_output->x, expected_output->len);
5150 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005151 output = NULL;
5152 output_length = ~0;
5153
5154 /* Buffer too small */
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005155 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005156 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
5157 peer_key_data->x, peer_key_data->len,
5158 output, expected_output->len - 1,
5159 &output_length),
5160 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine7d150292022-04-13 23:25:52 +02005161 /* Not required by the spec, but good robustness */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005162 TEST_LE_U(output_length, expected_output->len - 1);
5163 mbedtls_free(output);
Gilles Peskine7d150292022-04-13 23:25:52 +02005164 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005165
5166exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005167 mbedtls_free(output);
5168 psa_destroy_key(our_key);
5169 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02005170}
5171/* END_CASE */
5172
5173/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005174void key_agreement_capacity(int alg_arg,
5175 int our_key_type_arg, data_t *our_key_data,
5176 data_t *peer_key_data,
5177 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02005178{
Ronald Cron5425a212020-08-04 14:58:35 +02005179 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005180 psa_algorithm_t alg = alg_arg;
5181 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005184 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005185 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005187 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02005188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005189 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5190 psa_set_key_algorithm(&attributes, alg);
5191 psa_set_key_type(&attributes, our_key_type);
5192 PSA_ASSERT(psa_import_key(&attributes,
5193 our_key_data->x, our_key_data->len,
5194 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02005195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005196 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
5197 PSA_ASSERT(psa_key_derivation_key_agreement(
5198 &operation,
5199 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5200 peer_key_data->x, peer_key_data->len));
5201 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005202 /* The test data is for info="" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005203 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
5204 PSA_KEY_DERIVATION_INPUT_INFO,
5205 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005206 }
Gilles Peskine59685592018-09-18 12:11:34 +02005207
Shaun Case0e7791f2021-12-20 21:14:10 -08005208 /* Test the advertised capacity. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005209 PSA_ASSERT(psa_key_derivation_get_capacity(
5210 &operation, &actual_capacity));
5211 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02005212
Gilles Peskinebf491972018-10-25 22:36:12 +02005213 /* Test the actual capacity by reading the output. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005214 while (actual_capacity > sizeof(output)) {
5215 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5216 output, sizeof(output)));
5217 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02005218 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005219 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5220 output, actual_capacity));
5221 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
5222 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02005223
Gilles Peskine59685592018-09-18 12:11:34 +02005224exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005225 psa_key_derivation_abort(&operation);
5226 psa_destroy_key(our_key);
5227 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02005228}
5229/* END_CASE */
5230
5231/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005232void key_agreement_output(int alg_arg,
5233 int our_key_type_arg, data_t *our_key_data,
5234 data_t *peer_key_data,
5235 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02005236{
Ronald Cron5425a212020-08-04 14:58:35 +02005237 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005238 psa_algorithm_t alg = alg_arg;
5239 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005240 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005242 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005243
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005244 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005245 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02005246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005247 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02005248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005249 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5250 psa_set_key_algorithm(&attributes, alg);
5251 psa_set_key_type(&attributes, our_key_type);
5252 PSA_ASSERT(psa_import_key(&attributes,
5253 our_key_data->x, our_key_data->len,
5254 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02005255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005256 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
5257 PSA_ASSERT(psa_key_derivation_key_agreement(
5258 &operation,
5259 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5260 peer_key_data->x, peer_key_data->len));
5261 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005262 /* The test data is for info="" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005263 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
5264 PSA_KEY_DERIVATION_INPUT_INFO,
5265 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005266 }
Gilles Peskine59685592018-09-18 12:11:34 +02005267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005268 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5269 actual_output,
5270 expected_output1->len));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005271 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005272 expected_output1->x, expected_output1->len);
5273 if (expected_output2->len != 0) {
5274 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
5275 actual_output,
5276 expected_output2->len));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005277 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005278 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005279 }
Gilles Peskine59685592018-09-18 12:11:34 +02005280
5281exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005282 psa_key_derivation_abort(&operation);
5283 psa_destroy_key(our_key);
5284 PSA_DONE();
5285 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02005286}
5287/* END_CASE */
5288
5289/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005290void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02005291{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005292 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005293 unsigned char *output = NULL;
5294 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005295 size_t i;
5296 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005298 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00005299
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005300 TEST_CALLOC(output, bytes);
5301 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02005302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005303 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02005304
Gilles Peskinea50d7392018-06-21 10:22:13 +02005305 /* Run several times, to ensure that every output byte will be
5306 * nonzero at least once with overwhelming probability
5307 * (2^(-8*number_of_runs)). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005308 for (run = 0; run < 10; run++) {
5309 if (bytes != 0) {
5310 memset(output, 0, bytes);
5311 }
5312 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02005313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005314 for (i = 0; i < bytes; i++) {
5315 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02005316 ++changed[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005318 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005319 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005320
5321 /* Check that every byte was changed to nonzero at least once. This
5322 * validates that psa_generate_random is overwriting every byte of
5323 * the output buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005324 for (i = 0; i < bytes; i++) {
5325 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02005326 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005327
5328exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005329 PSA_DONE();
5330 mbedtls_free(output);
5331 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02005332}
5333/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005334
5335/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005336void generate_key(int type_arg,
5337 int bits_arg,
5338 int usage_arg,
5339 int alg_arg,
5340 int expected_status_arg,
5341 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005342{
Ronald Cron5425a212020-08-04 14:58:35 +02005343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005344 psa_key_type_t type = type_arg;
5345 psa_key_usage_t usage = usage_arg;
5346 size_t bits = bits_arg;
5347 psa_algorithm_t alg = alg_arg;
5348 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005350 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005352 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02005353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005354 psa_set_key_usage_flags(&attributes, usage);
5355 psa_set_key_algorithm(&attributes, alg);
5356 psa_set_key_type(&attributes, type);
5357 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005358
5359 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005360 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01005361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005362 if (is_large_key > 0) {
5363 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
5364 }
5365 TEST_EQUAL(status, expected_status);
5366 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005367 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005368 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005369
5370 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005371 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
5372 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
5373 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005374
Gilles Peskine818ca122018-06-20 18:16:48 +02005375 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005376 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02005377 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005378 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005379
5380exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005381 /*
5382 * Key attributes may have been returned by psa_get_key_attributes()
5383 * thus reset them as required.
5384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005385 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005387 psa_destroy_key(key);
5388 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02005389}
5390/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005391
Ronald Cronee414c72021-03-18 18:50:08 +01005392/* 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 +01005393void generate_key_rsa(int bits_arg,
5394 data_t *e_arg,
5395 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02005396{
Ronald Cron5425a212020-08-04 14:58:35 +02005397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005398 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005399 size_t bits = bits_arg;
5400 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5401 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5402 psa_status_t expected_status = expected_status_arg;
5403 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5404 uint8_t *exported = NULL;
5405 size_t exported_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005406 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005407 size_t exported_length = SIZE_MAX;
5408 uint8_t *e_read_buffer = NULL;
5409 int is_default_public_exponent = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005410 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005411 size_t e_read_length = SIZE_MAX;
5412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005413 if (e_arg->len == 0 ||
5414 (e_arg->len == 3 &&
5415 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005416 is_default_public_exponent = 1;
5417 e_read_size = 0;
5418 }
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005419 TEST_CALLOC(e_read_buffer, e_read_size);
5420 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005422 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02005423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005424 psa_set_key_usage_flags(&attributes, usage);
5425 psa_set_key_algorithm(&attributes, alg);
5426 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
5427 e_arg->x, e_arg->len));
5428 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005429
5430 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005431 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
5432 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005433 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005434 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005435
5436 /* Test the key information */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005437 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5438 TEST_EQUAL(psa_get_key_type(&attributes), type);
5439 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
5440 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
5441 e_read_buffer, e_read_size,
5442 &e_read_length));
5443 if (is_default_public_exponent) {
5444 TEST_EQUAL(e_read_length, 0);
5445 } else {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005446 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005447 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005448
5449 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005451 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005453
5454 /* Export the key and check the public exponent. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005455 PSA_ASSERT(psa_export_public_key(key,
5456 exported, exported_size,
5457 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02005458 {
5459 uint8_t *p = exported;
5460 uint8_t *end = exported + exported_length;
5461 size_t len;
5462 /* RSAPublicKey ::= SEQUENCE {
5463 * modulus INTEGER, -- n
5464 * publicExponent INTEGER } -- e
5465 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005466 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
5467 MBEDTLS_ASN1_SEQUENCE |
5468 MBEDTLS_ASN1_CONSTRUCTED));
5469 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
5470 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
5471 MBEDTLS_ASN1_INTEGER));
5472 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02005473 ++p;
5474 --len;
5475 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005476 if (e_arg->len == 0) {
5477 TEST_EQUAL(len, 3);
5478 TEST_EQUAL(p[0], 1);
5479 TEST_EQUAL(p[1], 0);
5480 TEST_EQUAL(p[2], 1);
5481 } else {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005482 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005483 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02005484 }
5485
5486exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005487 /*
5488 * Key attributes may have been returned by psa_get_key_attributes() or
5489 * set by psa_set_key_domain_parameters() thus reset them as required.
5490 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005491 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005493 psa_destroy_key(key);
5494 PSA_DONE();
5495 mbedtls_free(e_read_buffer);
5496 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02005497}
5498/* END_CASE */
5499
Darryl Greend49a4992018-06-18 17:27:26 +01005500/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005501void persistent_key_load_key_from_storage(data_t *data,
5502 int type_arg, int bits_arg,
5503 int usage_flags_arg, int alg_arg,
5504 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01005505{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005506 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5509 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005510 psa_key_type_t type = type_arg;
5511 size_t bits = bits_arg;
5512 psa_key_usage_t usage_flags = usage_flags_arg;
5513 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005514 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005515 unsigned char *first_export = NULL;
5516 unsigned char *second_export = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005517 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01005518 size_t first_exported_length;
5519 size_t second_exported_length;
5520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005521 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove30ceb232023-09-04 11:20:19 +01005522 TEST_CALLOC(first_export, export_size);
5523 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005524 }
Darryl Greend49a4992018-06-18 17:27:26 +01005525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005526 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01005527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005528 psa_set_key_id(&attributes, key_id);
5529 psa_set_key_usage_flags(&attributes, usage_flags);
5530 psa_set_key_algorithm(&attributes, alg);
5531 psa_set_key_type(&attributes, type);
5532 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01005533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005534 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00005535 case IMPORT_KEY:
5536 /* Import the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005537 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
5538 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00005539 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005540
Darryl Green0c6575a2018-11-07 16:05:30 +00005541 case GENERATE_KEY:
5542 /* Generate a key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005543 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00005544 break;
5545
5546 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005547#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005548 {
5549 /* Create base key */
5550 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
5551 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5552 psa_set_key_usage_flags(&base_attributes,
5553 PSA_KEY_USAGE_DERIVE);
5554 psa_set_key_algorithm(&base_attributes, derive_alg);
5555 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
5556 PSA_ASSERT(psa_import_key(&base_attributes,
5557 data->x, data->len,
5558 &base_key));
5559 /* Derive a key. */
5560 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
5561 PSA_ASSERT(psa_key_derivation_input_key(
5562 &operation,
5563 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
5564 PSA_ASSERT(psa_key_derivation_input_bytes(
5565 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
5566 NULL, 0));
5567 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
5568 &operation,
5569 &key));
5570 PSA_ASSERT(psa_key_derivation_abort(&operation));
5571 PSA_ASSERT(psa_destroy_key(base_key));
5572 base_key = MBEDTLS_SVC_KEY_ID_INIT;
5573 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005574#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005575 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005576#endif
5577 break;
5578
5579 default:
Agathiyan Bragadeesh27e29892023-07-14 17:28:27 +01005580 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005581 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005582 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005583 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01005584
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005585 /* Export the key if permitted by the key policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005586 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
5587 PSA_ASSERT(psa_export_key(key,
5588 first_export, export_size,
5589 &first_exported_length));
5590 if (generation_method == IMPORT_KEY) {
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005591 TEST_MEMORY_COMPARE(data->x, data->len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005592 first_export, first_exported_length);
5593 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005594 }
Darryl Greend49a4992018-06-18 17:27:26 +01005595
5596 /* Shutdown and restart */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005597 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005598 PSA_DONE();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005599 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01005600
Darryl Greend49a4992018-06-18 17:27:26 +01005601 /* Check key slot still contains key data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005602 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5603 TEST_ASSERT(mbedtls_svc_key_id_equal(
5604 psa_get_key_id(&attributes), key_id));
5605 TEST_EQUAL(psa_get_key_lifetime(&attributes),
5606 PSA_KEY_LIFETIME_PERSISTENT);
5607 TEST_EQUAL(psa_get_key_type(&attributes), type);
5608 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
5609 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
5610 mbedtls_test_update_key_usage_flags(usage_flags));
5611 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01005612
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005613 /* Export the key again if permitted by the key policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005614 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
5615 PSA_ASSERT(psa_export_key(key,
5616 second_export, export_size,
5617 &second_exported_length));
Tom Cosgroveba3b14d2023-09-04 11:23:02 +01005618 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005619 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00005620 }
5621
5622 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005623 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00005624 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005625 }
Darryl Greend49a4992018-06-18 17:27:26 +01005626
5627exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005628 /*
5629 * Key attributes may have been returned by psa_get_key_attributes()
5630 * thus reset them as required.
5631 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005632 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005634 mbedtls_free(first_export);
5635 mbedtls_free(second_export);
5636 psa_key_derivation_abort(&operation);
5637 psa_destroy_key(base_key);
5638 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005639 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005640}
5641/* END_CASE */