let randKey = window.crypto.getRandomValues(new Uint8Array(64));
let importedKey = await window.crypto.subtle.importKey("raw", randKey,
{
name: "ECDH",
namedCurve: "P-256",
},
false,
["deriveKey"]
);
Yet this fails with the error: Uncaught DOMException: Cannot create a key using the specified key usages.
How do I import a key to be used in key derivation?
I am basing my code off this snippet from the subtle crypto docs, which works perfectly:
let bobsKeyPair = await window.crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256",
},
false,
["deriveKey"],
);
But I want to do this with a key that has already been generated.
I found another stackoverflow question that said to use []
in the usages section, but all that did was change the error to just be Uncaught Error
.
2
Answers
There is a bug in importKey, where the wrong error is being reported to you.
The real problem is that ECDH keys cannot be exported or imported in
raw
format. You need to instead usepkcs8
as the format.The key usage
deriveKey
is only permitted for private keys. For public keys, an empty list must be used for the key usages.In addition, the raw public key must be passed in compressed or uncompressed format and it must of course be a valid key for the curve in question (not an arbitrary byte sequence).
In the following code, first a valid raw public key in uncompressed format is generated. To do this, a P-256 key pair is generated, whose public key is then exported in raw, uncompressed format. This is then used to demonstrate how such a key can be imported: