Parsing EC Public Key using points coordinates

Hi,

I have to parse a public ECDSA 256 public key using its X and Y points (see below [3]). The points are represented as base64url encoding of the octet string representation of the coordinate, as defined in Section 2.3.5 [1] of SEC1 [2].

I can’t find the MBEDTLS API for transforming these coordinates to a mbedtls_pk_context structure. Could you help me out?

[1] (RFC 7518: JSON Web Algorithms (JWA))
[2] (RFC 7518: JSON Web Algorithms (JWA)).

[3]
/ kty / 1: 2 / EC2 /,
/ kid / 2: ”39Gqlw”,
/ crv / -1: 1 / P-256 /,
/ x / -2: 24 h’bac5b11cad8f99f9c72b05cf4b9e26d244dc189f745228255a219a86d6a09eff’,
/ y / -3: 26 h’20138bf82dc1b6d562be0fa54ab7804a3a64b6d72ccfed6b6fb6ed28bbfc117e’

Thanks,
Doru

Hi @doru91
Do you mean section 2.3.5 or section 2.3.4?

Have you considered using mbedtls_ecp_point_read_string()?
If X and Y are in binary form, you can use mbedtls_ecp_point_read_binary() , but you will need to have the first byte state the compression, and concatenate X and Y to the input binary buffer.

I suggest you call after that mbedtls_ecp_check_pubkey() with the group and point Q for checking the conversion was done correct.
Once you have the point Q, you can assign it to a mbedtls_ecp_keypair.
You should then call mbedtls_pk_setup with the relevant mbedtls_pk_info_t ( received by calling mbedtls_pk_info_from_type() )
Once you have the mbedtls_pk_context, and you know for sure it is en ECP key pair, you can simply assign the public key Q to the context:

mbedtls_pk_ec( pk_context )->Q = Q;
mbedtls_pk_ec( pk_context )->grp = MBEDTLS_ECP_DP_SECP256R1;

Personally, I prefer not assigning directly to the struct’s members, but there isn’t a valid API.

Regards,
Mbed TLS Team member
Ron

Hi, Ron

Sorry for the extra delayed answer (I had to switch to another project).

The x and y points are encoded as OCTET STRING (see [1] and [2]) so I believe I meant section 2.3.4 of SEC1.
In this case, mbedtls_ecp_point_read_string() should work, right?

[1] RFC 5480: Elliptic Curve Cryptography Subject Public Key Information
[2] RFC 8152: CBOR Object Signing and Encryption (COSE)

Thanks,
Doru

Hi Doru,

In this case, mbedtls_ecp_point_read_string() should work, right?

Yes, in this case, it should work.

Regards,
Ron

Just wanted to let you know that mbedtls_ecp_point_read_binary worked.

Thanks!