Mbed forum

EC Private Key context from X, Y, K coordinates

(Siddarth Gandhi) #1

I have my ec private key in the 04 || X || Y || K format. Is it possible to parse it to mbedtls_pk_context or a PEM or DER format?

0 Likes

(Ken Goldman) #2

I’m new to mbedtls, but here’s how I convert a public key X, Y, and the curve to a mbedtls_pk_context. It works, but I don’t claim it’s the right way. Perhaps a similar solution works for a private key.

pkInfo = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
irc = mbedtls_pk_setup(*mbedPubkey, pkInfo);
keypair = mbedtls_pk_ec(**mbedPubkey); /* quick access */
grp = &(keypair->grp);
mbedtls_ecp_group_init(grp);
irc = mbedtls_ecp_group_load(grp, MBEDTLS_ECP_DP_SECP256R1);
pt = &(keypair->Q);
mbedtls_ecp_point_init(pt);
X = &(pt->X);
Y = &(pt->Y);
Z = &(pt->Z);
uint8_t ztmp = 1;
irc = mbedtls_mpi_read_binary(X, buffer, size);

same for Y and Z

0 Likes

(Ron Eldor) #3

Hi @siddarthgandhi
Thank you for your interest in Mbed TLS!

Ken is essentially correct, however you can use mbedtls_ecp_point_read_binary() to read the public component into keypair->Q( The 04 || X || Y part)
The private component K should be read using mbedtls_mpi_read_binary() into keypair->d of the example above.
Regards,
Mbed TLS Team member
Ron

0 Likes