3

I've already searched for my question in the documentation of mbedtls but there was no explicit answer.

Is there any way to generate public and private ECC keys with mbedTLS? I've already got sha256 properly working, with the help of a tutorial, but it seems that ECC operations are not well documented.

Is there an example, on how to generate a private/public key on the NIST P-256 curve?

Thanks for you help.

Stoogy
  • 1,307
  • 3
  • 16
  • 34
Habebit
  • 957
  • 6
  • 23

1 Answers1

4

Here is a sample code to generate ECC keys (but without any check of the return value):

    mbedtls_pk_context key;
    mbedtls_entropy_context entropy;
    mbedtls_ctr_drbg_context ctr_drbg;

    mbedtls_pk_type_t pk_alg = MBEDTLS_PK_ECKEY;

    mbedtls_pk_init(&key);
    mbedtls_entropy_init( &entropy );
    mbedtls_ctr_drbg_init(&ctr_drbg);


    ret = mbedtls_pk_setup(&key, mbedtls_pk_info_from_type(pk_alg));

    ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
                               &entropy,
                               (const unsigned char *) "ecdsa",
                               strlen(pers)
    );

    ret = mbedtls_ecp_gen_key(<KEY TYPE>,
                              mbedtls_pk_ec(key),
                              mbedtls_ctr_drbg_random,
                              &ctr_drbg
    );

Where <KEY_TYPE> can be found at: https://tls.mbed.org/api/ecp_8h.html#af79e530ea8f8416480f805baa20b1a2d and in your case should be MBEDTLS_ECP_DP_SECP256R1.

Stoogy
  • 1,307
  • 3
  • 16
  • 34