I am using an embedded chip called an ATECC508A in order to generate a ECDSA SHA-256 public/private key, and signature for a given message.
When I read the public key from the chip, I obtain a value of:
0x4B 0x34 0x89 0xAB 0x1B 0xE2 0x4C 0x84 0xA4 0x74 0xBE 0x85 0xD9 0xCF 0x99 0xF1
0x12 0xF1 0x5E 0x13 0x67 0x17 0xB8 0x8C 0x3D 0xD8 0x54 0xC4 0x70 0xB0 0x11 0x05
0xB1 0x9E 0x4E 0x3D 0xED 0x39 0xFA 0xED 0xF3 0xDB 0x94 0x7A 0xF6 0xCE 0x3A 0x0F
0x6C 0xB1 0x86 0xB1 0x64 0x5D 0x8A 0xB8 0xA2 0x74 0x9F 0xF2 0x42 0x55 0x67 0x62
It does not start with 0x30 like a DER formatted key would have, and I am not sure if it there is any special formatting to the byte string.
To import the public key in the raw format above I do the following in javascript with the WebCrypto API (subtle crypto library):
- First, convert to a new Uint8array:
var pubkeytest = new Uint8Array([0x4B 0x34 0x89 0xAB 0x1B 0xE2 0x4C 0x84 0xA4 0x74 0xBE 0x85 0xD9 0xCF 0x99 0xF1
0x12 0xF1 0x5E 0x13 0x67 0x17 0xB8 0x8C 0x3D 0xD8 0x54 0xC4 0x70 0xB0 0x11 0x05
0xB1 0x9E 0x4E 0x3D 0xED 0x39 0xFA 0xED 0xF3 0xDB 0x94 0x7A 0xF6 0xCE 0x3A 0x0F
0x6C 0xB1 0x86 0xB1 0x64 0x5D 0x8A 0xB8 0xA2 0x74 0x9F 0xF2 0x42 0x55 0x67 0x62]);
- Attempt to import the uint8 array above:
window.crypto.subtle.importKey(
"raw",
pubkeytest.buffer, {
name: "ECDSA",
namedCurve: "P-256",
hash: {
name: 'SHA-256'
}
},
false, //whether the key is extractable (i.e. can be used in exportKey)
["verify"] //"verify" for public key import, "sign" for private key imports
)
.then(function(pubkeytest) {
//returns a publicKey (or privateKey if you are importing a private key)
console.log(pubkeytest);
})
.catch(function(err) {
console.error(err);
});
The output in the console triggers an error on the console.error(err) line:
DOMException: Data provided to an operation does not meet requirements
I can successfully import a random public key using AES by doing the following:
const rawKey = window.crypto.getRandomValues(new Uint8Array(16));
window.crypto.subtle.importKey(
"raw",
rawKey,
"AES-GCM",
true,
["encrypt", "decrypt"]
);
The only difference I can see is the number of bytes in the array (16 vs 64) I am feeding importKey() as well as the algorithm type AES-GCM instead of ECDSA, and it does not declare P-256 or SHA-256.
How can I import the 64byte raw hex public key string above for use into javascript? I am able to perform crypto verification on signature/message using the example from here: https://github.com/mdn/dom-examples/tree/master/web-crypto
Thanks in advance