blob: 7dc8413c85008e3e7341f83e81fcb7f9f9a139e2 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/pk.h"
3#include "mbedtls/pem.h"
4#include "mbedtls/oid.h"
Valerio Settifa49a8e2023-01-26 10:00:55 +01005#include "mbedtls/ecp.h"
Valerio Settiaed87992023-07-04 19:58:43 +02006#include "mbedtls/psa_util.h"
Valerio Setti77a75682023-05-15 11:18:46 +02007#include "pk_internal.h"
Waleed Elmelegy38202a22023-09-21 15:21:10 +01008
Gilles Peskine157679c2024-02-09 19:29:44 +01009#if defined(MBEDTLS_PSA_CRYPTO_C)
10#include "test/psa_exercise_key.h"
11#endif
12
Waleed Elmelegy38202a22023-09-21 15:21:10 +010013#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
14#define HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der
15#endif
16
Gilles Peskine1d338762024-02-12 14:18:26 +010017#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_FS_IO)
Gilles Peskine157679c2024-02-09 19:29:44 +010018static int test_psa_bridge(const mbedtls_pk_context *ctx,
19 psa_key_usage_t usage_flag)
20{
21 switch (usage_flag) {
22 case PSA_KEY_USAGE_SIGN_HASH:
23 mbedtls_test_set_step(0);
24 break;
25 case PSA_KEY_USAGE_SIGN_MESSAGE:
26 mbedtls_test_set_step(1);
27 break;
28 case PSA_KEY_USAGE_DECRYPT:
29 mbedtls_test_set_step(2);
30 break;
31 case PSA_KEY_USAGE_DERIVE:
32 mbedtls_test_set_step(3);
33 break;
34 case PSA_KEY_USAGE_VERIFY_HASH:
35 mbedtls_test_set_step(4);
36 break;
37 case PSA_KEY_USAGE_VERIFY_MESSAGE:
38 mbedtls_test_set_step(5);
39 break;
40 case PSA_KEY_USAGE_ENCRYPT:
41 mbedtls_test_set_step(6);
42 break;
43 }
44
45 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
46 mbedtls_svc_key_id_t psa_key = MBEDTLS_SVC_KEY_ID_INIT;
47 int ok = 0;
48
49 TEST_EQUAL(mbedtls_pk_get_psa_attributes(ctx, usage_flag, &attributes), 0);
50 TEST_EQUAL(mbedtls_pk_import_into_psa(ctx, &attributes, &psa_key), 0);
Gilles Peskine2ec141a2024-02-15 17:22:37 +010051 if (!mbedtls_test_key_consistency_psa_pk(psa_key, ctx)) {
52 goto exit;
53 }
Gilles Peskine34955672024-02-12 14:19:24 +010054
Gilles Peskine157679c2024-02-09 19:29:44 +010055 psa_algorithm_t exercise_usage = psa_get_key_usage_flags(&attributes);
56 psa_algorithm_t exercise_alg = psa_get_key_algorithm(&attributes);
Gilles Peskine34955672024-02-12 14:19:24 +010057 if (mbedtls_test_can_exercise_psa_algorithm(exercise_alg)) {
58 TEST_ASSERT(mbedtls_test_psa_exercise_key(psa_key,
59 exercise_usage,
60 exercise_alg));
61 }
Gilles Peskine157679c2024-02-09 19:29:44 +010062
63 mbedtls_test_set_step((unsigned long) -1);
64 ok = 1;
65
66exit:
67 psa_destroy_key(psa_key);
68 psa_reset_key_attributes(&attributes);
69 return ok;
70}
71
Gilles Peskine1d338762024-02-12 14:18:26 +010072#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine157679c2024-02-09 19:29:44 +010073/* Whether a pk key can do ECDSA. Opaque keys are not supported since this
74 * test suite does not create opaque keys. */
75static int pk_can_ecdsa(const mbedtls_pk_context *ctx)
76{
77 /* Check whether we have an EC key. Unfortunately this also accepts
78 * keys on Montgomery curves, which can only do ECDH, so we'll have
79 * to dig further. */
80 if (!mbedtls_pk_can_do(ctx, MBEDTLS_PK_ECDSA)) {
81 return 0;
82 }
83#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
84 return ctx->ec_family != PSA_ECC_FAMILY_MONTGOMERY;
85#elif defined(MBEDTLS_ECDSA_C)
86 return mbedtls_ecdsa_can_do(mbedtls_pk_ec_ro(*ctx)->grp.id);
87#else
88 return 0;
89#endif
90}
Gilles Peskine1d338762024-02-12 14:18:26 +010091#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
92#endif /* MBEDTLS_PSA_CRYPTO_C && && MBEDTLS_FS_IO */
Gilles Peskine157679c2024-02-09 19:29:44 +010093
Paul Bakker1a7550a2013-09-15 13:01:22 +020094/* END_HEADER */
95
96/* BEGIN_DEPENDENCIES
Valerio Settic5d85e52023-07-26 18:12:23 +020097 * depends_on:MBEDTLS_PK_PARSE_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020098 * END_DEPENDENCIES
99 */
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 int res;
106 char *pwd = password;
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200109 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200112 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
116 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117
Gilles Peskine799befd2023-11-15 11:04:08 +0100118 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
123 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100124 TEST_EQUAL(mbedtls_rsa_check_privkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100125
Gilles Peskine92fb6042024-02-01 22:33:06 +0100126 size_t bitlen = mbedtls_rsa_get_bitlen(rsa);
127 TEST_EQUAL(mbedtls_pk_get_bitlen(&ctx), bitlen);
128 TEST_EQUAL(mbedtls_pk_get_len(&ctx), (bitlen + 7) / 8);
129
Gilles Peskined0783862024-02-02 13:13:34 +0100130#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100131 PSA_INIT();
132 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
133 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
134 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DECRYPT));
135 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
136 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
137 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100138#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139 }
140
Paul Bakkerbd51b262014-07-10 15:26:12 +0200141exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100143 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100145
Paul Bakker1a7550a2013-09-15 13:01:22 +0200146/* END_CASE */
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200152 int res;
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200155 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200158
Gilles Peskine799befd2023-11-15 11:04:08 +0100159 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
164 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100165 TEST_EQUAL(mbedtls_rsa_check_pubkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100166
Gilles Peskine069cec12024-02-12 16:59:17 +0100167 size_t bitlen = mbedtls_rsa_get_bitlen(rsa);
168 TEST_EQUAL(mbedtls_pk_get_bitlen(&ctx), bitlen);
169 TEST_EQUAL(mbedtls_pk_get_len(&ctx), (bitlen + 7) / 8);
170
Gilles Peskined0783862024-02-02 13:13:34 +0100171#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100172 PSA_INIT();
173 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
174 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
175 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100176#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177 }
178
Paul Bakkerbd51b262014-07-10 15:26:12 +0200179exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100181 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200182}
183/* END_CASE */
184
Valerio Setti545a0d62023-06-14 14:56:48 +0200185/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200189 int res;
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800192 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200195
Gilles Peskine799befd2023-11-15 11:04:08 +0100196 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +0200200#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
201 /* No need to check whether the parsed public point is on the curve or
202 * not because this is already done by the internal "pk_get_ecpubkey()"
203 * function */
204#else
205 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +0200206 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100207 TEST_EQUAL(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q), 0);
Valerio Setti483738e2023-05-17 15:37:29 +0200208#endif
Gilles Peskined0783862024-02-02 13:13:34 +0100209
210#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100211 PSA_INIT();
212 if (pk_can_ecdsa(&ctx)) {
213 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
214 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
215 }
Gilles Peskined0783862024-02-02 13:13:34 +0100216#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200217 }
218
Paul Bakkerbd51b262014-07-10 15:26:12 +0200219exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100221 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200222}
223/* END_CASE */
224
Valerio Setti545a0d62023-06-14 14:56:48 +0200225/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200229 int res;
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800232 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
235 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200236
Gilles Peskine799befd2023-11-15 11:04:08 +0100237 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Gilles Peskined0783862024-02-02 13:13:34 +0100241#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
242 /* PSA keys are already checked on import so nothing to do here. */
243#else
Valerio Setti7237d5f2023-05-18 19:00:22 +0200244 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100245 TEST_EQUAL(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100246#endif
247
248#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100249 PSA_INIT();
250 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DERIVE));
251 if (pk_can_ecdsa(&ctx)) {
252 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
253 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
254 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
255 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
256 }
Valerio Setti7237d5f2023-05-18 19:00:22 +0200257#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200258 }
259
Paul Bakkerbd51b262014-07-10 15:26:12 +0200260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100262 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200263}
264/* END_CASE */
265
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100267void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200268{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200272 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
275 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200276
Paul Bakkerbd51b262014-07-10 15:26:12 +0200277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200279 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200280}
281/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200282
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100283/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100284void pk_parse_key_encrypted(data_t *buf, data_t *pass, int result)
285{
286 mbedtls_pk_context pk;
287
288 mbedtls_pk_init(&pk);
289 USE_PSA_INIT();
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100290
Waleed Elmelegy9d4d8eb2023-09-21 08:27:39 +0100291 TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len,
Waleed Elmelegy556a0792023-09-21 09:19:56 +0100292 pass->x, pass->len,
293 mbedtls_test_rnd_std_rand,
294 NULL), result);
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100295exit:
296 mbedtls_pk_free(&pk);
297 USE_PSA_DONE();
298}
299/* END_CASE */
300
Valerio Settid476faa2023-07-05 10:33:53 +0200301/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */
302void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200303{
304 /* Montgomery keys have specific bits set to either 0 or 1 depending on
305 * their position. This is enforced during parsing (please see the implementation
306 * of mbedtls_ecp_read_key() for more details). The scope of this function
307 * is to verify this enforcing by feeding the parse algorithm with a x25519
308 * key which does not have those bits set properly. */
309 mbedtls_pk_context pk;
310 unsigned char *output_key = NULL;
311 size_t output_key_len = 0;
312
313 mbedtls_pk_init(&pk);
314 USE_PSA_INIT();
315
316 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
317 mbedtls_test_rnd_std_rand, NULL), 0);
318
319 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100320 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200321 /* output_key_len is updated with the real amount of data written to
322 * output_key buffer. */
323 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
324 TEST_ASSERT(output_key_len > 0);
325
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100326 TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200327
328exit:
329 if (output_key != NULL) {
330 mbedtls_free(output_key);
331 }
332 mbedtls_pk_free(&pk);
333 USE_PSA_DONE();
334}
335/* END_CASE */