1a8e1175bSopenharmony_ci/* BEGIN_HEADER */ 2a8e1175bSopenharmony_ci#include "mbedtls/pk.h" 3a8e1175bSopenharmony_ci#include "mbedtls/pem.h" 4a8e1175bSopenharmony_ci#include "mbedtls/oid.h" 5a8e1175bSopenharmony_ci#include "mbedtls/ecp.h" 6a8e1175bSopenharmony_ci#include "mbedtls/psa_util.h" 7a8e1175bSopenharmony_ci#include "pk_internal.h" 8a8e1175bSopenharmony_ci 9a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C) 10a8e1175bSopenharmony_ci#include "test/psa_exercise_key.h" 11a8e1175bSopenharmony_ci#endif 12a8e1175bSopenharmony_ci 13a8e1175bSopenharmony_ci#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) 14a8e1175bSopenharmony_ci#define HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der 15a8e1175bSopenharmony_ci#endif 16a8e1175bSopenharmony_ci 17a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_FS_IO) 18a8e1175bSopenharmony_cistatic int test_psa_bridge(const mbedtls_pk_context *ctx, 19a8e1175bSopenharmony_ci psa_key_usage_t usage_flag) 20a8e1175bSopenharmony_ci{ 21a8e1175bSopenharmony_ci switch (usage_flag) { 22a8e1175bSopenharmony_ci case PSA_KEY_USAGE_SIGN_HASH: 23a8e1175bSopenharmony_ci mbedtls_test_set_step(0); 24a8e1175bSopenharmony_ci break; 25a8e1175bSopenharmony_ci case PSA_KEY_USAGE_SIGN_MESSAGE: 26a8e1175bSopenharmony_ci mbedtls_test_set_step(1); 27a8e1175bSopenharmony_ci break; 28a8e1175bSopenharmony_ci case PSA_KEY_USAGE_DECRYPT: 29a8e1175bSopenharmony_ci mbedtls_test_set_step(2); 30a8e1175bSopenharmony_ci break; 31a8e1175bSopenharmony_ci case PSA_KEY_USAGE_DERIVE: 32a8e1175bSopenharmony_ci mbedtls_test_set_step(3); 33a8e1175bSopenharmony_ci break; 34a8e1175bSopenharmony_ci case PSA_KEY_USAGE_VERIFY_HASH: 35a8e1175bSopenharmony_ci mbedtls_test_set_step(4); 36a8e1175bSopenharmony_ci break; 37a8e1175bSopenharmony_ci case PSA_KEY_USAGE_VERIFY_MESSAGE: 38a8e1175bSopenharmony_ci mbedtls_test_set_step(5); 39a8e1175bSopenharmony_ci break; 40a8e1175bSopenharmony_ci case PSA_KEY_USAGE_ENCRYPT: 41a8e1175bSopenharmony_ci mbedtls_test_set_step(6); 42a8e1175bSopenharmony_ci break; 43a8e1175bSopenharmony_ci } 44a8e1175bSopenharmony_ci 45a8e1175bSopenharmony_ci psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 46a8e1175bSopenharmony_ci mbedtls_svc_key_id_t psa_key = MBEDTLS_SVC_KEY_ID_INIT; 47a8e1175bSopenharmony_ci int ok = 0; 48a8e1175bSopenharmony_ci 49a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_pk_get_psa_attributes(ctx, usage_flag, &attributes), 0); 50a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_pk_import_into_psa(ctx, &attributes, &psa_key), 0); 51a8e1175bSopenharmony_ci if (!mbedtls_test_key_consistency_psa_pk(psa_key, ctx)) { 52a8e1175bSopenharmony_ci goto exit; 53a8e1175bSopenharmony_ci } 54a8e1175bSopenharmony_ci 55a8e1175bSopenharmony_ci psa_algorithm_t exercise_usage = psa_get_key_usage_flags(&attributes); 56a8e1175bSopenharmony_ci psa_algorithm_t exercise_alg = psa_get_key_algorithm(&attributes); 57a8e1175bSopenharmony_ci if (mbedtls_test_can_exercise_psa_algorithm(exercise_alg)) { 58a8e1175bSopenharmony_ci TEST_ASSERT(mbedtls_test_psa_exercise_key(psa_key, 59a8e1175bSopenharmony_ci exercise_usage, 60a8e1175bSopenharmony_ci exercise_alg, 0)); 61a8e1175bSopenharmony_ci } 62a8e1175bSopenharmony_ci 63a8e1175bSopenharmony_ci mbedtls_test_set_step((unsigned long) -1); 64a8e1175bSopenharmony_ci ok = 1; 65a8e1175bSopenharmony_ci 66a8e1175bSopenharmony_ciexit: 67a8e1175bSopenharmony_ci psa_destroy_key(psa_key); 68a8e1175bSopenharmony_ci psa_reset_key_attributes(&attributes); 69a8e1175bSopenharmony_ci return ok; 70a8e1175bSopenharmony_ci} 71a8e1175bSopenharmony_ci 72a8e1175bSopenharmony_ci#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) 73a8e1175bSopenharmony_ci/* Whether a pk key can do ECDSA. Opaque keys are not supported since this 74a8e1175bSopenharmony_ci * test suite does not create opaque keys. */ 75a8e1175bSopenharmony_cistatic int pk_can_ecdsa(const mbedtls_pk_context *ctx) 76a8e1175bSopenharmony_ci{ 77a8e1175bSopenharmony_ci /* Check whether we have an EC key. Unfortunately this also accepts 78a8e1175bSopenharmony_ci * keys on Montgomery curves, which can only do ECDH, so we'll have 79a8e1175bSopenharmony_ci * to dig further. */ 80a8e1175bSopenharmony_ci if (!mbedtls_pk_can_do(ctx, MBEDTLS_PK_ECDSA)) { 81a8e1175bSopenharmony_ci return 0; 82a8e1175bSopenharmony_ci } 83a8e1175bSopenharmony_ci#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) 84a8e1175bSopenharmony_ci return ctx->ec_family != PSA_ECC_FAMILY_MONTGOMERY; 85a8e1175bSopenharmony_ci#elif defined(MBEDTLS_ECDSA_C) 86a8e1175bSopenharmony_ci return mbedtls_ecdsa_can_do(mbedtls_pk_ec_ro(*ctx)->grp.id); 87a8e1175bSopenharmony_ci#else 88a8e1175bSopenharmony_ci return 0; 89a8e1175bSopenharmony_ci#endif 90a8e1175bSopenharmony_ci} 91a8e1175bSopenharmony_ci#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ 92a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_CRYPTO_C && && MBEDTLS_FS_IO */ 93a8e1175bSopenharmony_ci 94a8e1175bSopenharmony_ci/* END_HEADER */ 95a8e1175bSopenharmony_ci 96a8e1175bSopenharmony_ci/* BEGIN_DEPENDENCIES 97a8e1175bSopenharmony_ci * depends_on:MBEDTLS_PK_PARSE_C 98a8e1175bSopenharmony_ci * END_DEPENDENCIES 99a8e1175bSopenharmony_ci */ 100a8e1175bSopenharmony_ci 101a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */ 102a8e1175bSopenharmony_civoid pk_parse_keyfile_rsa(char *key_file, char *password, int result) 103a8e1175bSopenharmony_ci{ 104a8e1175bSopenharmony_ci mbedtls_pk_context ctx; 105a8e1175bSopenharmony_ci int res; 106a8e1175bSopenharmony_ci char *pwd = password; 107a8e1175bSopenharmony_ci 108a8e1175bSopenharmony_ci mbedtls_pk_init(&ctx); 109a8e1175bSopenharmony_ci MD_PSA_INIT(); 110a8e1175bSopenharmony_ci 111a8e1175bSopenharmony_ci if (strcmp(pwd, "NULL") == 0) { 112a8e1175bSopenharmony_ci pwd = NULL; 113a8e1175bSopenharmony_ci } 114a8e1175bSopenharmony_ci 115a8e1175bSopenharmony_ci res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd, 116a8e1175bSopenharmony_ci mbedtls_test_rnd_std_rand, NULL); 117a8e1175bSopenharmony_ci 118a8e1175bSopenharmony_ci TEST_EQUAL(res, result); 119a8e1175bSopenharmony_ci 120a8e1175bSopenharmony_ci if (res == 0) { 121a8e1175bSopenharmony_ci mbedtls_rsa_context *rsa; 122a8e1175bSopenharmony_ci TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA)); 123a8e1175bSopenharmony_ci rsa = mbedtls_pk_rsa(ctx); 124a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_rsa_check_privkey(rsa), 0); 125a8e1175bSopenharmony_ci 126a8e1175bSopenharmony_ci size_t bitlen = mbedtls_rsa_get_bitlen(rsa); 127a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_pk_get_bitlen(&ctx), bitlen); 128a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_pk_get_len(&ctx), (bitlen + 7) / 8); 129a8e1175bSopenharmony_ci 130a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C) 131a8e1175bSopenharmony_ci PSA_INIT(); 132a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH)); 133a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE)); 134a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DECRYPT)); 135a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH)); 136a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE)); 137a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT)); 138a8e1175bSopenharmony_ci#endif 139a8e1175bSopenharmony_ci } 140a8e1175bSopenharmony_ci 141a8e1175bSopenharmony_ciexit: 142a8e1175bSopenharmony_ci mbedtls_pk_free(&ctx); 143a8e1175bSopenharmony_ci PSA_DONE(); 144a8e1175bSopenharmony_ci} 145a8e1175bSopenharmony_ci 146a8e1175bSopenharmony_ci/* END_CASE */ 147a8e1175bSopenharmony_ci 148a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */ 149a8e1175bSopenharmony_civoid pk_parse_public_keyfile_rsa(char *key_file, int result) 150a8e1175bSopenharmony_ci{ 151a8e1175bSopenharmony_ci mbedtls_pk_context ctx; 152a8e1175bSopenharmony_ci int res; 153a8e1175bSopenharmony_ci 154a8e1175bSopenharmony_ci mbedtls_pk_init(&ctx); 155a8e1175bSopenharmony_ci MD_PSA_INIT(); 156a8e1175bSopenharmony_ci 157a8e1175bSopenharmony_ci res = mbedtls_pk_parse_public_keyfile(&ctx, key_file); 158a8e1175bSopenharmony_ci 159a8e1175bSopenharmony_ci TEST_EQUAL(res, result); 160a8e1175bSopenharmony_ci 161a8e1175bSopenharmony_ci if (res == 0) { 162a8e1175bSopenharmony_ci mbedtls_rsa_context *rsa; 163a8e1175bSopenharmony_ci TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA)); 164a8e1175bSopenharmony_ci rsa = mbedtls_pk_rsa(ctx); 165a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_rsa_check_pubkey(rsa), 0); 166a8e1175bSopenharmony_ci 167a8e1175bSopenharmony_ci size_t bitlen = mbedtls_rsa_get_bitlen(rsa); 168a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_pk_get_bitlen(&ctx), bitlen); 169a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_pk_get_len(&ctx), (bitlen + 7) / 8); 170a8e1175bSopenharmony_ci 171a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C) 172a8e1175bSopenharmony_ci PSA_INIT(); 173a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH)); 174a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE)); 175a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT)); 176a8e1175bSopenharmony_ci#endif 177a8e1175bSopenharmony_ci } 178a8e1175bSopenharmony_ci 179a8e1175bSopenharmony_ciexit: 180a8e1175bSopenharmony_ci mbedtls_pk_free(&ctx); 181a8e1175bSopenharmony_ci PSA_DONE(); 182a8e1175bSopenharmony_ci} 183a8e1175bSopenharmony_ci/* END_CASE */ 184a8e1175bSopenharmony_ci 185a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */ 186a8e1175bSopenharmony_civoid pk_parse_public_keyfile_ec(char *key_file, int result) 187a8e1175bSopenharmony_ci{ 188a8e1175bSopenharmony_ci mbedtls_pk_context ctx; 189a8e1175bSopenharmony_ci int res; 190a8e1175bSopenharmony_ci 191a8e1175bSopenharmony_ci mbedtls_pk_init(&ctx); 192a8e1175bSopenharmony_ci MD_OR_USE_PSA_INIT(); 193a8e1175bSopenharmony_ci 194a8e1175bSopenharmony_ci res = mbedtls_pk_parse_public_keyfile(&ctx, key_file); 195a8e1175bSopenharmony_ci 196a8e1175bSopenharmony_ci TEST_EQUAL(res, result); 197a8e1175bSopenharmony_ci 198a8e1175bSopenharmony_ci if (res == 0) { 199a8e1175bSopenharmony_ci TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY)); 200a8e1175bSopenharmony_ci#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) 201a8e1175bSopenharmony_ci /* No need to check whether the parsed public point is on the curve or 202a8e1175bSopenharmony_ci * not because this is already done by the internal "pk_get_ecpubkey()" 203a8e1175bSopenharmony_ci * function */ 204a8e1175bSopenharmony_ci#else 205a8e1175bSopenharmony_ci const mbedtls_ecp_keypair *eckey; 206a8e1175bSopenharmony_ci eckey = mbedtls_pk_ec_ro(ctx); 207a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q), 0); 208a8e1175bSopenharmony_ci#endif 209a8e1175bSopenharmony_ci 210a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C) 211a8e1175bSopenharmony_ci PSA_INIT(); 212a8e1175bSopenharmony_ci if (pk_can_ecdsa(&ctx)) { 213a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH)); 214a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE)); 215a8e1175bSopenharmony_ci } 216a8e1175bSopenharmony_ci#endif 217a8e1175bSopenharmony_ci } 218a8e1175bSopenharmony_ci 219a8e1175bSopenharmony_ciexit: 220a8e1175bSopenharmony_ci mbedtls_pk_free(&ctx); 221a8e1175bSopenharmony_ci PSA_DONE(); 222a8e1175bSopenharmony_ci} 223a8e1175bSopenharmony_ci/* END_CASE */ 224a8e1175bSopenharmony_ci 225a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */ 226a8e1175bSopenharmony_civoid pk_parse_keyfile_ec(char *key_file, char *password, int result) 227a8e1175bSopenharmony_ci{ 228a8e1175bSopenharmony_ci mbedtls_pk_context ctx; 229a8e1175bSopenharmony_ci int res; 230a8e1175bSopenharmony_ci 231a8e1175bSopenharmony_ci mbedtls_pk_init(&ctx); 232a8e1175bSopenharmony_ci MD_OR_USE_PSA_INIT(); 233a8e1175bSopenharmony_ci 234a8e1175bSopenharmony_ci res = mbedtls_pk_parse_keyfile(&ctx, key_file, password, 235a8e1175bSopenharmony_ci mbedtls_test_rnd_std_rand, NULL); 236a8e1175bSopenharmony_ci 237a8e1175bSopenharmony_ci TEST_EQUAL(res, result); 238a8e1175bSopenharmony_ci 239a8e1175bSopenharmony_ci if (res == 0) { 240a8e1175bSopenharmony_ci TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY)); 241a8e1175bSopenharmony_ci#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) 242a8e1175bSopenharmony_ci /* PSA keys are already checked on import so nothing to do here. */ 243a8e1175bSopenharmony_ci#else 244a8e1175bSopenharmony_ci const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx); 245a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d), 0); 246a8e1175bSopenharmony_ci#endif 247a8e1175bSopenharmony_ci 248a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C) 249a8e1175bSopenharmony_ci PSA_INIT(); 250a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DERIVE)); 251a8e1175bSopenharmony_ci if (pk_can_ecdsa(&ctx)) { 252a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH)); 253a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE)); 254a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH)); 255a8e1175bSopenharmony_ci TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE)); 256a8e1175bSopenharmony_ci } 257a8e1175bSopenharmony_ci#endif 258a8e1175bSopenharmony_ci } 259a8e1175bSopenharmony_ci 260a8e1175bSopenharmony_ciexit: 261a8e1175bSopenharmony_ci mbedtls_pk_free(&ctx); 262a8e1175bSopenharmony_ci PSA_DONE(); 263a8e1175bSopenharmony_ci} 264a8e1175bSopenharmony_ci/* END_CASE */ 265a8e1175bSopenharmony_ci 266a8e1175bSopenharmony_ci/* BEGIN_CASE */ 267a8e1175bSopenharmony_civoid pk_parse_key(data_t *buf, int result) 268a8e1175bSopenharmony_ci{ 269a8e1175bSopenharmony_ci mbedtls_pk_context pk; 270a8e1175bSopenharmony_ci 271a8e1175bSopenharmony_ci mbedtls_pk_init(&pk); 272a8e1175bSopenharmony_ci USE_PSA_INIT(); 273a8e1175bSopenharmony_ci 274a8e1175bSopenharmony_ci TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0, 275a8e1175bSopenharmony_ci mbedtls_test_rnd_std_rand, NULL) == result); 276a8e1175bSopenharmony_ci 277a8e1175bSopenharmony_ciexit: 278a8e1175bSopenharmony_ci mbedtls_pk_free(&pk); 279a8e1175bSopenharmony_ci USE_PSA_DONE(); 280a8e1175bSopenharmony_ci} 281a8e1175bSopenharmony_ci/* END_CASE */ 282a8e1175bSopenharmony_ci 283a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */ 284a8e1175bSopenharmony_civoid pk_parse_key_encrypted(data_t *buf, data_t *pass, int result) 285a8e1175bSopenharmony_ci{ 286a8e1175bSopenharmony_ci mbedtls_pk_context pk; 287a8e1175bSopenharmony_ci 288a8e1175bSopenharmony_ci mbedtls_pk_init(&pk); 289a8e1175bSopenharmony_ci USE_PSA_INIT(); 290a8e1175bSopenharmony_ci 291a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len, 292a8e1175bSopenharmony_ci pass->x, pass->len, 293a8e1175bSopenharmony_ci mbedtls_test_rnd_std_rand, 294a8e1175bSopenharmony_ci NULL), result); 295a8e1175bSopenharmony_ciexit: 296a8e1175bSopenharmony_ci mbedtls_pk_free(&pk); 297a8e1175bSopenharmony_ci USE_PSA_DONE(); 298a8e1175bSopenharmony_ci} 299a8e1175bSopenharmony_ci/* END_CASE */ 300a8e1175bSopenharmony_ci 301a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */ 302a8e1175bSopenharmony_civoid pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output) 303a8e1175bSopenharmony_ci{ 304a8e1175bSopenharmony_ci /* Montgomery keys have specific bits set to either 0 or 1 depending on 305a8e1175bSopenharmony_ci * their position. This is enforced during parsing (please see the implementation 306a8e1175bSopenharmony_ci * of mbedtls_ecp_read_key() for more details). The scope of this function 307a8e1175bSopenharmony_ci * is to verify this enforcing by feeding the parse algorithm with a x25519 308a8e1175bSopenharmony_ci * key which does not have those bits set properly. */ 309a8e1175bSopenharmony_ci mbedtls_pk_context pk; 310a8e1175bSopenharmony_ci unsigned char *output_key = NULL; 311a8e1175bSopenharmony_ci size_t output_key_len = 0; 312a8e1175bSopenharmony_ci 313a8e1175bSopenharmony_ci mbedtls_pk_init(&pk); 314a8e1175bSopenharmony_ci USE_PSA_INIT(); 315a8e1175bSopenharmony_ci 316a8e1175bSopenharmony_ci TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0, 317a8e1175bSopenharmony_ci mbedtls_test_rnd_std_rand, NULL), 0); 318a8e1175bSopenharmony_ci 319a8e1175bSopenharmony_ci output_key_len = input_key->len; 320a8e1175bSopenharmony_ci TEST_CALLOC(output_key, output_key_len); 321a8e1175bSopenharmony_ci /* output_key_len is updated with the real amount of data written to 322a8e1175bSopenharmony_ci * output_key buffer. */ 323a8e1175bSopenharmony_ci output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len); 324a8e1175bSopenharmony_ci TEST_ASSERT(output_key_len > 0); 325a8e1175bSopenharmony_ci 326a8e1175bSopenharmony_ci TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len); 327a8e1175bSopenharmony_ci 328a8e1175bSopenharmony_ciexit: 329a8e1175bSopenharmony_ci if (output_key != NULL) { 330a8e1175bSopenharmony_ci mbedtls_free(output_key); 331a8e1175bSopenharmony_ci } 332a8e1175bSopenharmony_ci mbedtls_pk_free(&pk); 333a8e1175bSopenharmony_ci USE_PSA_DONE(); 334a8e1175bSopenharmony_ci} 335a8e1175bSopenharmony_ci/* END_CASE */ 336