Member-only story
Post Quantum Cryptography in a Browser
Like it or not, we need to move away from ECDH (Elliptic Curve Diffie Hellman) for key exchange and move towards using ML-KEM (aka Kyber) for a key encapsulation method (KEM).
Alice will generate a key pair (pkAlice and skAlice) and sends Bob her public key (pkAlice). Bob will then generate a new key (bobKey). He will then encrypt the new key (bobKey) with Alice’s public key (pkAlice), and sends the resultant ciphertext (cipherText) to Alice. Alice then decrypts the ciphertext (cipherText) with her private key (skAlice). She then gets aliceKey, and which will be the same as bobKey (Figure 1):
const [pkAlice, skAlice] = await recipient.generateKeyPair();
const sender = new MlKem512();
const [cipherText, bobKey] = await sender.encap(pkAlice);
const aliceKey = await recipient.decap(cipherText, skAlice);The HTML is [here]:
<html>
<body>
<script type="module">
const toHexString = (bytes) => {
return Array.from(bytes, (byte) => {
return ('0' + (byte & 0xff).toString(16)).slice(-2);
}).join('');
};
import { MlKem512, MlKem768, MlKem1024 } from "https://esm.sh/mlkem"…