tlse icon indicating copy to clipboard operation
tlse copied to clipboard

How to export private key from context->ecc_dhe->k

Open naushadck opened this issue 10 months ago • 2 comments

How can i export private key/public key from context->ecc_dhe->k and make a new ecc_key variable.

I can create an ecc_key by using ecc_ansi_x963_import_ex() from
a buffer. Is it possible to do the similar for the private key ?.

naushadck avatar Feb 14 '25 06:02 naushadck

It should be possible. You should check the tomcrypt API reference, TLSe is just using the tomcrypt crypto funcitons.

Just keep in mind that the private key when using ECDHE (Elliptic Curve Diffie-Hellman Ephemeral) is useless, because the common secret is derived as: remote_public_key (+) local_private_key == local_public_key (+) remote_private_key.

Knowing keysize, you could just save key->k to an unsigned char *buf of keysize (you should check if its in bits or bytes). That should be enough. You also need the curve parameters, but this is set in the named curve definition. In other others, you should add an id for the curve in order to identify it.

Check mp_unsigned_bin_size(a) and mp_to_unsigned_bin(a, buffer) where a is key->k and buffer is your saved buffer. Then you can use mp_read_unsigned_bin to read it back from the buffer.

Hope it helps.

eduardsui avatar Feb 14 '25 08:02 eduardsui

Thank you.

My understanding was (rpk * lpk = lpk * rpk ) remote_public_key * local_private_key == local_public_key * remote_private_key.

naushadck avatar Feb 20 '25 02:02 naushadck