Import EC private key is rejected as key length mis-match #331
Labels
No labels
api-spec
bug
CLA requested
CLA valid
compliance
DO NOT MERGE
duplicate
enhancement
feature branch
good first issue
HEADER FORMATTING ISSUE
help wanted
Information
invalid
Jira status: AWAITING REVIEW
Jira status: CLOSED
Jira status: DONE
Jira status: IN PROGRESS
Jira status: IN REVIEW
Jira status: OPEN
Jira status: REOPENED
Jira status: RESOLVED
mirrored
mirror internally
needs: backports
needs: ci
needs: design review
needs: preceding PR
needs: review
needs: work
notabug
open for community contribution
Product Backlog
question
ready for merge
security
stale
type: bug
type: enhancement
type: question
web pages
wontfix
writing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-ARMmbed/mbed-crypto#331
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
Internal Jira reference: https://jira.arm.com/browse/IOTCRYPT-1020
Hi @CharleyChu,
psa_import_keyexpects 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 ambedtls_ecp_keypair*withmbedtls_pk_ec()and usembedtls_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.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.