Hi @kgoldman
Thank you for your interest in Mbed TLS!
I have a raw RSA public key (e, n) that I want to write in PEM format. I found mbedtls_pk_write_pubkey_pem() but not a method to create the mbedtls_pk_context from n and e. Any hints?
This article may have the answer for you, regarding RSA context. Regarding ECC, unfortunately, there is not a simple API for that, however, you can use the following to insert the public point Q(x and y. z is 1 in Mbed TLS) to the ec p context.
mbedtls_ecp_keypair ctx;
mbedtls_ecp_group_load();
mbedtls_ecp_point_read_binary() or mbedtls_ecp_point_read_string(); //depending on your point format
mbedtls_pk_context is described as a public key context, but it seems to be used for private keys as well. Is that correct?
correct. It’s named public key context, because it is for public key cryptography, which involves public and private keys. Note that the private key includes both private and public components, while the public key contains only the public components.
What’s the difference between an mbedtls_rsa_context and an mbedtls_pk_context? Is there a way to convert between them?
mbedtls_rsa_context
\ mbedtls_ecp_keypair
are the algorithm specific context. mbedtls_pk_context
is a wrapper for the algorithm context, to supply a unified interface for users of the public key cryptography. in addition, the wrapper pk context is used for the Mbed TLS key parsing and writing API.
In key_app reference application you can see how to convert from mbedtls_pk_context
to mbedtls_rsa_context
(and to mbedtls_ecp_keypair
a few lines after ). To convert from mbedtls_rsa_context
to mbedtls_pk_context
, there is no designated API, unfortunately, however you can do the following, given rsa_context
:
mbedtls_pk_context pk;
mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA) );
memcpy( mbedtls_pk_rsa( pk ), &rsa_context );
The example wa for RSA, but you can do similar with ECC key type(MBEDTLS_PK_ECKEY
, MBEDTLS_PK_ECKEY_DH
or MBEDTLS_PK_ECDSA
, depending on what context you have, and your functionality needs)
Regards,
Mbed TLS Team member
Ron