chromaLTS
chromaLTS4d ago

Using std/crypto with x25519 crashes - How do i use node experimental modules instead?

Please help ❤️ my brain is beginning to melt 🫠 I'm trying to implement ECIES (Elliptic Curve Integrated Encryption Scheme) capabilities in my app, but the standard crypto library doesn't support it. This is because the standard implementation is based on either a faulty Chrome implementation or a faulty Node.js implementation. However, Node.js does have a fix, but it's hidden behind the --experimental-modules flag. In deno is there a way to enable this when importing modules from Node.js? Or am I forced to switch back to Node.js?
6 Replies
bartlomieju
bartlomieju4d ago
@divy please take a look
divy
divy4d ago
@chromaLTS What exactly is the crash/error message?
chromaLTS
chromaLTS4d ago
2 sec @divy error: Uncaught (in promise) OperationError: Invalid key return await crypto.subtle.deriveBits( ^ at deriveBits (ext:deno_crypto/00_crypto.js:4514:15) at SubtleCrypto.deriveBits (ext:deno_crypto/00_crypto.js:1151:26) at deriveSharedSecret (redacted) at Object.redacted (redacted) at async redacted this is the code im running import { crypto } from "@std/crypto/crypto"; async function deriveSharedSecret(privateKey: any, publicKey: any) { return await crypto.subtle.deriveBits( { name: "X25519", public: publicKey, }, privateKey, 128, ); }

async function genKeys() { const alicesKeyPair = await crypto.subtle.generateKey( { name: "X25519", }, false, ["deriveBits"], ) const bobsKeyPair = await crypto.subtle.generateKey( { name: "X25519", }, false, ["deriveBits"], ) console.log("Keys changed"); return {alicesKeyPair, bobsKeyPair} } async function runExample(){ // Generate 2 X25519 key pairs: one for Alice and one for Bob // In more normal usage, they would generate their key pairs // separately and exchange public keys securely // Alice then generates a secret using her private key and Bob's public key. // Bob could generate the same secret using his private key and Alice's public key. const {alicesKeyPair, bobsKeyPair} = await genKeys() const sharedSecretAlice = await deriveSharedSecret( //@ts-ignore alicesKeyPair.privateKey, //@ts-ignore bobsKeyPair.publicKey, ); let buffer = new Uint8Array(sharedSecretAlice, 0, 10); console.log(${buffer}…[${sharedSecretAlice.byteLength} bytes total] (Alice secret)); const sharedSecretBob = await deriveSharedSecret( //@ts-ignore bobsKeyPair.privateKey, //@ts-ignore alicesKeyPair.publicKey, ); buffer = new Uint8Array(sharedSecretBob, 0, 10); console.log(${buffer}…[${sharedSecretAlice.byteLength} bytes total] (Bob secret)); }
divy
divy4d ago
Ah thanks that's a bug, can you open an github issue?
chromaLTS
chromaLTS4d ago
sure @divy Sorry to bother you but i dont want to be more intrusive then nessesary – so im opting to ask now instead of making mistakes later. Where do i open it?, std? - theres existing issues similar but seems outdated and stale. -https://github.com/denoland/std/issues/4224 and https://github.com/denoland/deno/issues/16145 but also super old. both still open. also can i sidestep the bug rn by importing nodes fixed version somehow – even tho its hidden behind the experimental-modules flag? @divy Thanks for the quick response in general! ❤️ I understand not spending time on helping to format issues, so i just went ahead and opened this the best i could - https://github.com/denoland/std/issues/6085 if theres anything else i can help with just let me know 😉
divy
divy4d ago
thanks that works!