Importing ECC private key from PEM to PSA and error using mbedTLS helper functions

Hi,

I have an ECC private key (in PEM) that is parsed by mbedtls_pk_parse_key(). Now, I want to import this private key into PSA (using psa_import_key()). 
I have followed the recommendations here https://github.com/Mbed-TLS/mbedtls/blob/development/docs/psa-transition.md, see section - "Importing a PK key by export-import for an ECC private key".I have written a conversion routine as suggested here i.e. as below:

unsigned char buf[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
size_t length = PSA_BITS_TO_BYTES(mbedtls_pk_bitlen(&pk));
mbedtls_ecp_keypair *ec = mbedtls_pk_ec(&pk);
psa_ecc_curve_t curve;
{
    mbedtls_ecp_group grp;
    mbedtls_ecp_group_init(&grp);
    mbedtls_ecp_point Q;
    mbedtls_ecp_point_init(&Q);
    mbedtls_mpi d;
    mbedtls_mpi_init(&d);
    mbedtls_ecp_export(ec, &grp, &d, &Q);
    size_t bits;
    curve = mbedtls_ecc_group_to_psa(grp.id, &bits);
    mbedtls_ecp_group_free(&grp);
    mbedtls_ecp_point_free(&Q);
    mbedtls_mpi_free(&d);
}
mbedtls_ecp_write_key(ec, buf, length);

But when building, i get an error "undefined reference to `mbedtls_ecp_export'".
I tried configuing 
CONFIG_MBEDTLS_ECP_C=y and  CONFIG_MBEDTLS_ECP_DP_SECP256R1_ENABLED=y and CONFIG_MBEDTLS_ECP_ALL_ENABLED=y. But, i still get this error when building.
I can see that the function is defined in ecp.c in the nrf SDK.

Is there any particular kconfig option in the nrf SDK that is controlling the compiling of this function mbedtls_ecp_export()?
If not, is there an alternative way to import this ECC private key(returned by into PSA (for ECDSA NIST-P256-SECP-R1)?

Thanks,

Mathi.
#PSA #mbedTLS #ECC #PrivateKey#Import#PEM#Crypto#ECDSA#NISTP256#PSATRANSITION

Parents
  • Hello Mathi,

    Can you please try to add CONFIG_MBEDTLS_CMAC_C in the kconfig and build again?

    Thanks.

    BR

    kazi

  • Hi Kazi,

    Thanks for the reply.
    Meanwhile, after some further playing around, i was able to get that error fixed after enabling the kconfig option CONFIG_MBEDTLS_LEGACY_CRYPTO_C=y. Actually, I didn't need any of the other CONFIG_MBEDTLS_ECP** related kconfig options.

    While that helped, i am yet to figure out whether enabling the CONFIG_MBEDTLS_LEGACY_CRYPTO_C will affect my ongoing effort to make our product strictly use only PSA APIs (for crypto algorithms and keystorage). The only exception(i hope) to that strategy would be about allowing usage of mbedTLS "utilities" for key parsing, such as the pseudo code i have explained in this ticket.

    Kind Regards,
    Mathi.

  • Hi Kazi,

    Just as an update. I did not manage to go any further by enabling CONFIG_MBEDTLS_LEGACY_CRYPTO_C=y. 
    Enabling this is causing PSA key import to fail with error code -134.  Enabling this flag seems to be a show-stopper for us.


    I should now probably rephrase my original question to as below: 
    "Are there any helper routines in the SDK to convert an ECC private(and public key) key in PEM format and then import it into PSA?"

    Thanks, Mathi.

Reply
  • Hi Kazi,

    Just as an update. I did not manage to go any further by enabling CONFIG_MBEDTLS_LEGACY_CRYPTO_C=y. 
    Enabling this is causing PSA key import to fail with error code -134.  Enabling this flag seems to be a show-stopper for us.


    I should now probably rephrase my original question to as below: 
    "Are there any helper routines in the SDK to convert an ECC private(and public key) key in PEM format and then import it into PSA?"

    Thanks, Mathi.

Children
  • Hello Mathi,

    we unfortunately don't fully (not for all the use cases) support PSA APIs with legacy APIs at the same time.

    BR

    kazi

  • Thanks for the official confirmation.
    Meanwhile, i have written a parser myself for converting from PEM to DER and extracting the key material.

    I heard that mbedTLS 3.6 is soon to released which will contain some PSA routines that handle this use case. I hope the SDK is upgraded to absorb the latest mbedTLS release.
    https://lists.trustedfirmware.org/archives/list/[email protected]/thread/76ZSKG3E6O2U5KLDXOM3NOCG5ZBVNJF2/


    Mathi.

  • Hi Mathi. Could you help me with one thing? I try to verify JWT token but I have problem with importing public key. I'm using to convert this tool: https://lapo.it/asn1js/

    Convert my test public key:

    I copied raw key manualy to my table and using like below.

     

    	static const char public_key[65]={0x04, 0x39, 0xE4, 0x43, 0xEC, 0xD2, 0xA4, 0x2D, 0x56, 0xAD, 0xBB, 0xA4, 0xC8, 0x0D, 0xA1, 0x09, 
    									  0x5C, 0xF7, 0x63, 0x7F, 0x00, 0x1C, 0xF5, 0x2A, 0x20, 0x7B, 0x96, 0x3F, 0xC8, 0xCF, 0x89, 0x99, 
    									  0xB7, 0x9E, 0xB3, 0x90, 0xD6, 0xFC, 0x3B, 0x30, 0xF7, 0xC1, 0x46, 0x36, 0x62, 0xC0, 0x2D, 0xE8, 
    									  0xA9, 0x90, 0xA1, 0xC0, 0x1C, 0x3A, 0x73, 0xC4, 0x9A, 0x69, 0xBA, 0x74, 0xF6, 0x09, 0x39, 0x4F, 
    									  0xCE};
    
    
    	psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
    	psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_VERIFY_HASH);
    	psa_set_key_lifetime(&key_attributes, PSA_KEY_LIFETIME_VOLATILE);
    	psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
    	psa_set_key_type(&key_attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1));
    	psa_set_key_bits(&key_attributes, 256);
    
    	LOG_HEXDUMP_INF(public_key,65,"PUBLIC KEY:");
    
    	status = psa_import_key(&key_attributes, public_key, sizeof(public_key), &pub_key_handle);
    	if (status != PSA_SUCCESS) 
    	{
    		#ifdef LOG_CRYPTO
    			LOG_INF("psa_import_key failed! (Error: %d)", status);
    		#endif	
    	}
    
    	status = psa_verify_hash(pub_key_handle, PSA_ALG_ECDSA(PSA_ALG_SHA_256), sha_digest, sizeof(sha_digest), signature, sizeof(signature));
    	if (status != PSA_SUCCESS) 
    	{
    		LOG_INF("PSA_verify_hash failed! (Error: %d)", status);
    	}
    	else
    	{
    		LOG_INF("Verify OK");
    	}

    sha_digest (hader+payload) is correct, I checked on online tool. Verification is always false. Probably I use wrong data key format. The question is: how to properly copy data from online tool to my table?

    Best regards

    PW

Related