I want to take a secret and generate a ECDH public/private key from it.
In the browser, a usual way would be to use PBKDF2 (or other deterministic bytes) to generate an ECDH public/private key pair in WebCrypto.
The following sample code should do this, but it throws a DOM Exception in Chrome:
// Generate a random KDF key.
const priv = new Uint8Array(24)
crypto.getRandomValues(priv)
const kdfKey = await crypto.subtle.importKey(
'raw', priv, { name: 'PBKDF2' }, false, ['deriveKey'])
// Derive the ECDH key.
const salt = new Uint8Array(16)
const iterations = 2000
const hash = { name: 'SHA-512' }
const curve = { name: 'ECDH', namedCurve: 'P-384' }
const usages = ['deriveKey']
crypto.getRandomValues(salt)
const ecdhKey = await crypto.subtle.deriveKey({
name: 'PBKDF2', salt, iterations, hash
}, kdfKey, curve, true, usages) // throws.
The above works when the algorithm is AES-GCM
(i.e. when curve is replaced with e.g. { name: 'AES-GCM', length: 256 }
), but other algorithms throw exceptions as well, so I suspect I'm missing something ... subtle.
My hope was/is that WebCrypto would be suited to accepting random bits and generating the ECDH public/private key pair. It looks like this might not be the case.
The alternative would be to use PBKDF2
to deriveBits
that can be used to manually create the ECDH key pair. If this is indeed the only option, what is the usual algorithm for turning random bits into a public/private key (i.e. references & public implementations)? If I have to develop something, I'll likely post it here interest & review.
from Using WebCrypto to generate ECDH key from PBKDF2
No comments:
Post a Comment