blob: 1cd6e2bb98523048a157a326ec7841df4efc4078 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/* BEGIN_HEADER */
Gilles Peskine5255a9e2024-09-15 19:54:22 +02002#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00003#include "mbedtls/pk.h"
4#include "mbedtls/pem.h"
5#include "mbedtls/oid.h"
Valerio Settifa49a8e2023-01-26 10:00:55 +01006#include "mbedtls/ecp.h"
Valerio Settiaed87992023-07-04 19:58:43 +02007#include "mbedtls/psa_util.h"
Valerio Setti77a75682023-05-15 11:18:46 +02008#include "pk_internal.h"
Waleed Elmelegy38202a22023-09-21 15:21:10 +01009
Gilles Peskine157679c2024-02-09 19:29:44 +010010#if defined(MBEDTLS_PSA_CRYPTO_C)
11#include "test/psa_exercise_key.h"
12#endif
13
Waleed Elmelegy38202a22023-09-21 15:21:10 +010014#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
15#define HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der
16#endif
17
Gilles Peskine1d338762024-02-12 14:18:26 +010018#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_FS_IO)
Gilles Peskine157679c2024-02-09 19:29:44 +010019static int test_psa_bridge(const mbedtls_pk_context *ctx,
20 psa_key_usage_t usage_flag)
21{
22 switch (usage_flag) {
23 case PSA_KEY_USAGE_SIGN_HASH:
24 mbedtls_test_set_step(0);
25 break;
26 case PSA_KEY_USAGE_SIGN_MESSAGE:
27 mbedtls_test_set_step(1);
28 break;
29 case PSA_KEY_USAGE_DECRYPT:
30 mbedtls_test_set_step(2);
31 break;
32 case PSA_KEY_USAGE_DERIVE:
33 mbedtls_test_set_step(3);
34 break;
35 case PSA_KEY_USAGE_VERIFY_HASH:
36 mbedtls_test_set_step(4);
37 break;
38 case PSA_KEY_USAGE_VERIFY_MESSAGE:
39 mbedtls_test_set_step(5);
40 break;
41 case PSA_KEY_USAGE_ENCRYPT:
42 mbedtls_test_set_step(6);
43 break;
44 }
45
46 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
47 mbedtls_svc_key_id_t psa_key = MBEDTLS_SVC_KEY_ID_INIT;
48 int ok = 0;
49
50 TEST_EQUAL(mbedtls_pk_get_psa_attributes(ctx, usage_flag, &attributes), 0);
Gilles Peskine6b3a9ee2024-04-22 17:18:13 +020051 int ret = mbedtls_pk_import_into_psa(ctx, &attributes, &psa_key);
52 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_RSA &&
53 mbedtls_pk_get_bitlen(ctx) % 8 != 0 &&
54 ret == MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) {
55 /* There is a historical limitation with support for RSA keys in PSA:
56 * only byte-aligned sizes are supported.
57 * https://github.com/Mbed-TLS/mbedtls/issues/9048
58 * For now, for such keys, treat not-supported from PSA as a success.
59 */
60 ok = 1;
61 goto exit;
62 }
63 TEST_EQUAL(ret, 0);
Gilles Peskine2ec141a2024-02-15 17:22:37 +010064 if (!mbedtls_test_key_consistency_psa_pk(psa_key, ctx)) {
65 goto exit;
66 }
Gilles Peskine34955672024-02-12 14:19:24 +010067
Gilles Peskine157679c2024-02-09 19:29:44 +010068 psa_algorithm_t exercise_usage = psa_get_key_usage_flags(&attributes);
69 psa_algorithm_t exercise_alg = psa_get_key_algorithm(&attributes);
Gilles Peskine34955672024-02-12 14:19:24 +010070 if (mbedtls_test_can_exercise_psa_algorithm(exercise_alg)) {
71 TEST_ASSERT(mbedtls_test_psa_exercise_key(psa_key,
72 exercise_usage,
Ryan Everett0a271fd2024-03-12 16:34:02 +000073 exercise_alg, 0));
Gilles Peskine34955672024-02-12 14:19:24 +010074 }
Gilles Peskine157679c2024-02-09 19:29:44 +010075
76 mbedtls_test_set_step((unsigned long) -1);
77 ok = 1;
78
79exit:
80 psa_destroy_key(psa_key);
81 psa_reset_key_attributes(&attributes);
82 return ok;
83}
84
Elena Uziunaite8dde3b32024-07-05 12:10:21 +010085#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine157679c2024-02-09 19:29:44 +010086/* Whether a pk key can do ECDSA. Opaque keys are not supported since this
87 * test suite does not create opaque keys. */
88static int pk_can_ecdsa(const mbedtls_pk_context *ctx)
89{
90 /* Check whether we have an EC key. Unfortunately this also accepts
91 * keys on Montgomery curves, which can only do ECDH, so we'll have
92 * to dig further. */
93 if (!mbedtls_pk_can_do(ctx, MBEDTLS_PK_ECDSA)) {
94 return 0;
95 }
96#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
97 return ctx->ec_family != PSA_ECC_FAMILY_MONTGOMERY;
98#elif defined(MBEDTLS_ECDSA_C)
99 return mbedtls_ecdsa_can_do(mbedtls_pk_ec_ro(*ctx)->grp.id);
100#else
101 return 0;
102#endif
103}
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100104#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine1d338762024-02-12 14:18:26 +0100105#endif /* MBEDTLS_PSA_CRYPTO_C && && MBEDTLS_FS_IO */
Gilles Peskine157679c2024-02-09 19:29:44 +0100106
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107/* END_HEADER */
108
109/* BEGIN_DEPENDENCIES
Valerio Settic5d85e52023-07-26 18:12:23 +0200110 * depends_on:MBEDTLS_PK_PARSE_C
Paul Bakker1a7550a2013-09-15 13:01:22 +0200111 * END_DEPENDENCIES
112 */
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200118 int res;
119 char *pwd = password;
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200122 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
129 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200130
Gilles Peskine799befd2023-11-15 11:04:08 +0100131 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
136 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100137 TEST_EQUAL(mbedtls_rsa_check_privkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100138
Gilles Peskine92fb6042024-02-01 22:33:06 +0100139 size_t bitlen = mbedtls_rsa_get_bitlen(rsa);
140 TEST_EQUAL(mbedtls_pk_get_bitlen(&ctx), bitlen);
141 TEST_EQUAL(mbedtls_pk_get_len(&ctx), (bitlen + 7) / 8);
142
Gilles Peskined0783862024-02-02 13:13:34 +0100143#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100144 PSA_INIT();
145 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
146 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
147 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DECRYPT));
148 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
149 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
150 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100151#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200152 }
153
Paul Bakkerbd51b262014-07-10 15:26:12 +0200154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100156 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100158
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159/* END_CASE */
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165 int res;
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200168 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171
Gilles Peskine799befd2023-11-15 11:04:08 +0100172 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
177 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100178 TEST_EQUAL(mbedtls_rsa_check_pubkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100179
Gilles Peskine069cec12024-02-12 16:59:17 +0100180 size_t bitlen = mbedtls_rsa_get_bitlen(rsa);
181 TEST_EQUAL(mbedtls_pk_get_bitlen(&ctx), bitlen);
182 TEST_EQUAL(mbedtls_pk_get_len(&ctx), (bitlen + 7) / 8);
183
Gilles Peskined0783862024-02-02 13:13:34 +0100184#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100185 PSA_INIT();
186 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
187 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
188 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100189#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200190 }
191
Paul Bakkerbd51b262014-07-10 15:26:12 +0200192exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100194 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200195}
196/* END_CASE */
197
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100198/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200202 int res;
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800205 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200208
Gilles Peskine799befd2023-11-15 11:04:08 +0100209 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +0200213#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
214 /* No need to check whether the parsed public point is on the curve or
215 * not because this is already done by the internal "pk_get_ecpubkey()"
216 * function */
217#else
218 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +0200219 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100220 TEST_EQUAL(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q), 0);
Valerio Setti483738e2023-05-17 15:37:29 +0200221#endif
Gilles Peskined0783862024-02-02 13:13:34 +0100222
223#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100224 PSA_INIT();
225 if (pk_can_ecdsa(&ctx)) {
226 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
227 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
228 }
Gilles Peskined0783862024-02-02 13:13:34 +0100229#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200230 }
231
Paul Bakkerbd51b262014-07-10 15:26:12 +0200232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100234 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200235}
236/* END_CASE */
237
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100238/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200242 int res;
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800245 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
248 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200249
Gilles Peskine799befd2023-11-15 11:04:08 +0100250 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Gilles Peskined0783862024-02-02 13:13:34 +0100254#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
255 /* PSA keys are already checked on import so nothing to do here. */
256#else
Valerio Setti7237d5f2023-05-18 19:00:22 +0200257 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100258 TEST_EQUAL(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100259#endif
260
261#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100262 PSA_INIT();
263 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DERIVE));
264 if (pk_can_ecdsa(&ctx)) {
265 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
266 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
267 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
268 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
269 }
Valerio Setti7237d5f2023-05-18 19:00:22 +0200270#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200271 }
272
Paul Bakkerbd51b262014-07-10 15:26:12 +0200273exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100275 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200276}
277/* END_CASE */
278
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100279/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200281{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200285 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
288 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200289
Paul Bakkerbd51b262014-07-10 15:26:12 +0200290exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200292 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200293}
294/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200295
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100296/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100297void pk_parse_key_encrypted(data_t *buf, data_t *pass, int result)
298{
299 mbedtls_pk_context pk;
300
301 mbedtls_pk_init(&pk);
302 USE_PSA_INIT();
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100303
Waleed Elmelegy9d4d8eb2023-09-21 08:27:39 +0100304 TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len,
Waleed Elmelegy556a0792023-09-21 09:19:56 +0100305 pass->x, pass->len,
306 mbedtls_test_rnd_std_rand,
307 NULL), result);
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100308exit:
309 mbedtls_pk_free(&pk);
310 USE_PSA_DONE();
311}
312/* END_CASE */
313
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100314/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY:MBEDTLS_PK_WRITE_C */
Valerio Settid476faa2023-07-05 10:33:53 +0200315void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200316{
317 /* Montgomery keys have specific bits set to either 0 or 1 depending on
318 * their position. This is enforced during parsing (please see the implementation
319 * of mbedtls_ecp_read_key() for more details). The scope of this function
320 * is to verify this enforcing by feeding the parse algorithm with a x25519
321 * key which does not have those bits set properly. */
322 mbedtls_pk_context pk;
323 unsigned char *output_key = NULL;
324 size_t output_key_len = 0;
325
326 mbedtls_pk_init(&pk);
327 USE_PSA_INIT();
328
329 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
330 mbedtls_test_rnd_std_rand, NULL), 0);
331
332 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100333 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200334 /* output_key_len is updated with the real amount of data written to
335 * output_key buffer. */
336 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
337 TEST_ASSERT(output_key_len > 0);
338
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100339 TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200340
341exit:
342 if (output_key != NULL) {
343 mbedtls_free(output_key);
344 }
345 mbedtls_pk_free(&pk);
346 USE_PSA_DONE();
347}
348/* END_CASE */