Import EC private key is rejected as key length mis-match #331

Closed
opened 2019-12-06 22:46:39 -06:00 by CharleyChu · 3 comments
CharleyChu commented 2019-12-06 22:46:39 -06:00 (Migrated from github.com)

Call psa_import_key() to import an ASN1 encoding of ECP256R1 private key. It is rejected as following check failed in psa_import_key_into_slot()

if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
return( PSA_ERROR_INVALID_ARGUMENT );

The expect length is 32 for ECP256R1, but the actually key length (data_lenght) is 33 as a leading 00 is added for the DER representation of positive integers

[ ] Question
[ ] Enhancement
[x ] Bug

Call psa_import_key() to import an ASN1 encoding of ECP256R1 private key. It is rejected as following check failed in psa_import_key_into_slot() if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length ) return( PSA_ERROR_INVALID_ARGUMENT ); The expect length is 32 for ECP256R1, but the actually key length (data_lenght) is 33 as a leading 00 is added for the DER representation of positive integers [ ] Question [ ] Enhancement [x ] Bug
ciarmcom commented 2019-12-07 00:47:03 -06:00 (Migrated from github.com)
*Internal Jira reference: https://jira.arm.com/browse/IOTCRYPT-1020*
gilles-peskine-arm commented 2019-12-08 11:52:27 -06:00 (Migrated from github.com)

Hi @CharleyChu,

psa_import_key expects the raw private key, not an ASN.1 encoding. For a P256R1 key, the raw private key is always 32 bytes, no matter how many leading zero bits are present.

If you have the key in an ASN.1 encoding, you'll need to convert it. Mbed Crypto can import keys in the format defined in RFC 5915 with mbedtls_pk_parse_key(), then you can obtain a mbedtls_ecp_keypair* with mbedtls_pk_ec() and use mbedtls_mpi_write_binary(mbedtls_pk_ec(pk)->d, buf, 32) to extract the private key. We intend to provide more convenient parsing functions in a future version of Mbed Crypto.

Hi @CharleyChu, `psa_import_key` expects the raw private key, not an ASN.1 encoding. For a P256R1 key, the raw private key is always 32 bytes, no matter how many leading zero bits are present. If you have the key in an ASN.1 encoding, you'll need to convert it. Mbed Crypto can import keys in the format defined in RFC 5915 with `mbedtls_pk_parse_key()`, then you can obtain a `mbedtls_ecp_keypair*` with `mbedtls_pk_ec()` and use `mbedtls_mpi_write_binary(mbedtls_pk_ec(pk)->d, buf, 32)` to extract the private key. We intend to provide more convenient parsing functions in a future version of Mbed Crypto.
CharleyChu commented 2019-12-09 13:56:57 -06:00 (Migrated from github.com)

Thanks Gilles!

From: Gilles Peskine notifications@github.com
Sent: Sunday, December 8, 2019 9:52 AM
To: ARMmbed/mbed-crypto mbed-crypto@noreply.github.com
Cc: Hao Chuan Chu charley.chu@cypress.com; Mention mention@noreply.github.com
Subject: Re: [ARMmbed/mbed-crypto] Import EC private key is rejected as key length mis-match (#331)

Hi @CharleyChuhttps://github.com/CharleyChu,

psa_import_key expects the raw private key, not an ASN.1 encoding. For a P256R1 key, the raw private key is always 32 bytes, no matter how many leading zero bits are present.

If you have the key in an ASN.1 encoding, you'll need to convert it. Mbed Crypto can import keys in the format defined in RFC 5915 with mbedtls_pk_parse_key(), then you can obtain a mbedtls_ecp_keypair* with mbedtls_pk_ec() and use mbedtls_mpi_write_binary(mbedtls_pk_ec(pk)->d, buf, 32) to extract the private key. We intend to provide more convenient parsing functions in a future version of Mbed Crypto.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/ARMmbed/mbed-crypto/issues/331?email_source=notifications&email_token=AMS3WFIQYGMMKGF372Y7C7DQXUX5XA5CNFSM4JXHUHL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGHE4UQ#issuecomment-562974290, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AMS3WFJXJ7VC7HZCHWHQWMDQXUX5XANCNFSM4JXHUHLQ.

This message and any attachments may contain confidential information from Cypress or its subsidiaries. If it has been received in error, please advise the sender and immediately delete this message.

Thanks Gilles! From: Gilles Peskine <notifications@github.com> Sent: Sunday, December 8, 2019 9:52 AM To: ARMmbed/mbed-crypto <mbed-crypto@noreply.github.com> Cc: Hao Chuan Chu <charley.chu@cypress.com>; Mention <mention@noreply.github.com> Subject: Re: [ARMmbed/mbed-crypto] Import EC private key is rejected as key length mis-match (#331) Hi @CharleyChu<https://github.com/CharleyChu>, psa_import_key expects the raw private key, not an ASN.1 encoding. For a P256R1 key, the raw private key is always 32 bytes, no matter how many leading zero bits are present. If you have the key in an ASN.1 encoding, you'll need to convert it. Mbed Crypto can import keys in the format defined in RFC 5915 with mbedtls_pk_parse_key(), then you can obtain a mbedtls_ecp_keypair* with mbedtls_pk_ec() and use mbedtls_mpi_write_binary(mbedtls_pk_ec(pk)->d, buf, 32) to extract the private key. We intend to provide more convenient parsing functions in a future version of Mbed Crypto. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub<https://github.com/ARMmbed/mbed-crypto/issues/331?email_source=notifications&email_token=AMS3WFIQYGMMKGF372Y7C7DQXUX5XA5CNFSM4JXHUHL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGHE4UQ#issuecomment-562974290>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AMS3WFJXJ7VC7HZCHWHQWMDQXUX5XANCNFSM4JXHUHLQ>. This message and any attachments may contain confidential information from Cypress or its subsidiaries. If it has been received in error, please advise the sender and immediately delete this message.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-ARMmbed/mbed-crypto#331
No description provided.