Arm Mbed OS support forum

Using mbedtls for ECDH Key Exchange with smart card

Hello guys,

i’m quite new to this topic, but i’m currently working on a project which requires a ECDH key exchange between my terminal (Proxmark 3 ARM microcontroller) and a smart card.
I will use mbedtls, but I have a few questions about this.
First, I try to orientate on the “curve25519” example. My solution needs to implement multiple curves, but I think the steps should be 99% the same.
These are the current steps: (The “fake_rand” is my own random function because I have no std libs available, therefore I try to implement this with the integrated mbedtls buffer function without calloc and free)

mbedtls_ecdh_context ec_context;
mbedtls_ecdh_init(&ec_context);
mbedtls_ecp_group_load(&ec_context.grp, MBEDTLS_ECP_DP_BP256R1);
mbedtls_ecdh_gen_public(&ec_context.grp, &ec_context.d, &ec_context.Q, fake_rand, NULL);

After this, I need to send the public key to the smard card in hexadecimal presentation, but I can’t find any function in order to export the key. Is it correct to use “mbedtls_mpi_write_binary” and then convert it to hex?

Next I receive X and Y coordinates in Hex from the card. How can I use these values to fill “ec_context.Qp”? Is it possible to import hex values?

Quite a lot of questions, but thanks in advance.
Kind regards, Robin