With one of my U2F tokens, NIST P-256 ECDSA signature verification fails on the U2F_REGISTER response.
I'm working according to this protocol description: https://fidoalliance.org/specs/fido-u2f-v1.0-nfc-bt-amendment-20150514/fido-u2f-raw-message-formats.html
U2F_REGISTER request:
b852c99d386769a3289d45708680efcc2350c0a7c152adb93c708b22a55c5d11: challenge
8adb4559abbea4a68754314e64aa6785901b6a236db5a14d040916799865e290: application
U2F_REGISTER response:
05: reserved byte
user_public_key (NIST P-256):
04: key format (uncompressed)
db3ca8b3863f2fed19dada227aa8a51dba9bd0ecafcb5313225c04618c9329df: x
e540eb5c58d24704e899eaa72feef06722ad4669c5a3d5537ab88dc6a712f96d: y
keyhandle
40: length (64 bytes)
96e7d09341237e1c306a71ed9d59eeb16be621dbb34eb346ca999301ad0bee28f62876fced320734b4f139b89b8608bb4b4cef0f864064c2b3af1966167c4278
attestation certificate in X.509 DER format
30: sequence
82: length (130 bytes) ??
01433081ea...
ECDSA signature in X.509 DER format
30: sequence
44: length (68 bytes)
02: integer
20: length (32 bytes)
6d090eefac83a67f9361adcd391395ab3636470e1eb479dc94e1194dc1f25259: r
02: integer
20: length (32 bytes)
660f7d23cd6b1c74e8499503fd21f6662a3270e916a57096037001baad5c7064: s
I compute the SHA-256 hash of 00 (1 byte) + application (32 bytes) + challenge (32 bytes) + keyhandle (64 bytes this time) + user_public_key (1 + 32 + 32 bytes)
.
Then I call ecdsa.Verify(user_public_key, hash, r, s)
.
For one of the U2F tokens I have, ecdsa.Verify returns true (good), but for another one (see the bytes above), it returns false. What am I doing wrong in the example above?
I was using the wrong public key for verifying the signature. I should have been using the public key within the attestation certificate (rather than the public key directly in the response). By doing so the signature verified correctly.
Here is a code example in Python which does the signature verification: https://github.com/concise/lightu2f.py/blob/20540f75ee5f86a4b2ad4bffe34074760978cbf9/lightu2f.py#L100 . It runs correctly on the data in the question.