End-to-end encryption for one-to-one messaging, in TypeScript. It implements X3DH key agreement, the Double Ratchet with XChaCha20-Poly1305, and Sesame-style management of several sessions per peer device, on top of the @noble primitives.
brnr-e2ee has not had an independent security audit. It has a byte-exact specification, an independent reference implementation, Wycheproof and known-answer tests, property-based and fuzz tests, and mutation testing, all described below. None of that replaces an audit. Read THREATMODEL.md before you rely on it.
I wrote it for two of my apps, BRNR and lowercase, which both depend on it. It's published so that anyone can read it, check it and use it.
E2eeError with one of 19 codes, inputs are parsed against strict schemas, and a rejected message never changes state (SPEC §16).It holds no storage and makes no network calls. You give it a KeyStore, and it tells you exactly what to persist and when.
The four runtime dependencies are @noble/curves, @noble/ciphers, @noble/hashes and zod, each pinned to an exact version. The build is ESM and CommonJS with type declarations for both, and it runs on Node 22.12 or later, in browsers, and on React Native (Hermes).
These are deferred, not forgotten (SPEC §2):
It also doesn't wipe keys from memory, because JavaScript strings can't be wiped (THREATMODEL item 8).
npm install --save-exact brnr-e2ee
Pin the exact version: a minor release can add a cipher suite, and you'll want to review that before you take it (Versioning).
On React Native, where globalThis.crypto doesn't exist, install a crypto.getRandomValues polyfill before anything imports this library, and pass a secure rng to createE2ee. @noble/curves checks for WebCrypto once, when it loads, and uses it to blind secret scalar multiplications (THREATMODEL item 16).
Alice starts a session with Bob from his published bundle and sends him a message. This exact code runs in CI as test/readme.test.ts.
import {
createE2ee,
E2eeError,
SUITES,
withSession,
type KeyStore,
type MessageV2,
type OneTimePrekeyPair,
type SignedPrekeyPair,
type StoredSession,
} from "brnr-e2ee";
/** A minimal in-memory KeyStore. Real apps use IndexedDB, SQLite or an encrypted file store. */
class MemoryKeyStore implements KeyStore {
sessions = new Map<string, StoredSession>();
spks = new Map<number, SignedPrekeyPair>();
otps = new Map<number, OneTimePrekeyPair>();
replay = new Set<string>();
private locks = new Map<string, Promise<unknown>>();
async getSession(peer: string, conv: string) {
return this.sessions.get(`${peer}|${conv}`) ?? null;
}
async putSession(
peer: string,
conv: string,
serialized: string | null,
expected: number | null,
opts: Parameters<KeyStore["putSession"]>[4] = {},
) {
const key = `${peer}|${conv}`;
const cur = this.sessions.get(key);
if ((cur?.version ?? null) !== expected)
throw new Error("version mismatch");
const version = (cur?.version ?? 0) + 1;
// One "transaction": session, tracker, OTP deletions and replay records together.
this.sessions.set(key, {
serialized: serialized ?? cur?.serialized ?? null,
version,
tracker: opts.tracker ?? cur?.tracker ?? null,
});
for (const id of opts.deleteOtpIds ?? []) this.otps.delete(id);
for (const r of opts.acceptedHandshakes ?? [])
if (this.spks.has(r.spkId)) this.replay.add(`${r.spkId}:${r.hsDigest}`);
return version;
}
async getSpk(id: number) {
return this.spks.get(id);
}
async getOtp(id: number) {
return this.otps.get(id);
}
async deleteOtp(id: number) {
this.otps.delete(id);
}
async hasAcceptedHandshake(spkId: number, digest: string) {
return this.replay.has(`${spkId}:${digest}`);
}
withLock<T>(conv: string, fn: () => Promise<T>): Promise<T> {
const run = (this.locks.get(conv) ?? Promise.resolve()).then(fn, fn);
this.locks.set(
conv,
run.catch(() => undefined),
);
return run;
}
}
const e2ee = createE2ee(); // pass { rng } on React Native / Hermes
const conversationId = "conv-1";
// Bob publishes a bundle (the server stores it and hands out one OTP per fetch).
const bobStore = new MemoryKeyStore();
const bob = e2ee.generateDeviceIdentity("bob-phone");
const spk = e2ee.generateSignedPrekey(bob, 1);
const [otp] = e2ee.generateOneTimePrekeys(1, 1);
bobStore.spks.set(spk.spkId, spk);
bobStore.otps.set(otp!.otpId, otp!);
const bobBundle = {
...e2ee.buildDeviceBundle({ device: bob, spk }),
otp: { otpId: otp!.otpId, pk: otp!.pk },
};
// Alice sends the first message. The state is persisted before the message leaves.
const aliceStore = new MemoryKeyStore();
const alice = e2ee.generateDeviceIdentity("alice-laptop");
// The AD's shape follows the suite: suite v2 also names the recipient device.
const ad = (suite: SuiteId, from: string, to: string): AssociatedData =>
suite === "x25519-xchacha20poly1305-sha256-v2"
? {
conversationId,
senderDeviceId: from,
recipientDeviceId: to,
suite,
v: 2,
}
: { conversationId, senderDeviceId: from, suite, v: 2 };
async function aliceSends(text: string): Promise<MessageV2> {
return withSession(
e2ee,
aliceStore,
bob.deviceId,
conversationId,
async (state, tracker) => {
const s =
state ??
e2ee.initiateSession({
conversationId,
self: alice,
peerBundle: bobBundle,
});
const r = e2ee.encrypt({
state: s,
plaintext: new TextEncoder().encode(text),
ad: ad(e2ee.inspectSession(s).suite, alice.deviceId, bob.deviceId),
});
return { state: r.state, tracker, result: r.message };
},
);
}
// Bob receives (SPEC §8.3). The pinned keys come from a verified bundle, never from the message.
async function bobReceives(m: MessageV2): Promise<string | null> {
return withSession(
e2ee,
bobStore,
alice.deviceId,
conversationId,
async (state, tracker) => {
const accept = async (existing: typeof state) => {
const h = m.handshake!;
const s = await bobStore.getSpk(h.spkId);
const o =
h.otpId === undefined ? undefined : await bobStore.getOtp(h.otpId);
const seen = await bobStore.hasAcceptedHandshake(
h.spkId,
e2ee.handshakeDigest(h),
);
return e2ee.acceptSession({
conversationId,
self: bob,
expectedPeerBoxPk: alice.boxPk,
expectedPeerSignPk: alice.signPk,
message: m,
lookupSpk: () => s,
lookupOtp: () => o,
seenHandshake: () => seen,
...(existing ? { existing } : {}),
});
};
try {
let r;
try {
r = e2ee.decrypt({
state: state ?? (await accept(null)),
message: m,
expectedAd: ad(m.suite, alice.deviceId, bob.deviceId),
});
} catch (err) {
if (
!(err instanceof E2eeError) ||
err.code !== "E2EE_SESSION_CONFLICT" ||
!state
)
throw err;
r = e2ee.decrypt({
state: await accept(state),
message: m,
expectedAd: ad(m.suite, alice.deviceId, bob.deviceId),
});
}
const t = tracker ? e2ee.onDecrypt(tracker, r) : null;
return {
state: r.state,
tracker: t,
result: new TextDecoder().decode(r.plaintext),
};
} catch (err) {
// Recovery triggers update the tracker (SPEC §8.4); other errors drop the message.
const reason =
err instanceof E2eeError && err.code === "E2EE_HANDSHAKE_REQUIRED"
? "handshake-required"
: err instanceof E2eeError &&
err.details?.["reason"] === "evicted-session"
? "evicted-session"
: null;
if (reason)
return {
state: null,
tracker: e2ee.recordRecoverySignal(tracker, reason),
result: null,
};
if (err instanceof E2eeError)
return { state: null, tracker: null, result: null };
throw err;
}
},
);
}
const m = await aliceSends("hello bob");
console.log(await bobReceives(m)); // "hello bob"
A real app also follows the recovery and refresh rules in SPEC §8.4 (recoveryDecision, refreshDecision, and a control message after every re-initiation), and its delete paths flush the pending lists first (§8.5).
Everything starts from createE2ee(), which returns an object with the protocol functions. The full reference, generated from the type declarations, is at ashwinsathian.github.io/brnr-e2ee, and SPEC.md defines what each function does, byte for byte.
| Area | Functions |
|---|---|
| Keys | generateAccountIdentity, generateDeviceIdentity, generateSignedPrekey, generateOneTimePrekeys |
| Bundles and device lists | buildDeviceBundle, verifyDeviceBundle, parseBundle, buildSignedDeviceList, verifySignedDeviceList, parseDeviceList, deviceListDigest, bundleMatchesDeviceList |
| Sessions | initiateSession, acceptSession, encrypt, decrypt, inspectSession, handshakeDigest, parseMessage, serializeState, parseState |
| Recovery | recordRecoverySignal, recoveryDecision, refreshDecision, onDecrypt, serializeTracker, parseTracker |
| Safety numbers | computeSafetyNumber, verifySafetyNumberPayload |
| Device linking | generateLinkEphemeral, generateLinkRequestId, linkTranscript, linkCommitment, verifyLinkCommitment, linkConfirmationCode, sealAccountKey, openAccountKey |
| Reports | buildReportDisclosure, verifyReportDisclosureItem |
| Top-level exports | createE2ee, withSession, E2eeError, isE2eeError, E2EE_ERROR_CODES, SUITES, LIMITS, and the types (KeyStore, MessageV2, StoredSession, …) |
brnr-e2ee/testing exports the deterministic RNG, the fingerprint chain and operation counters that the vectors and benchmarks use. It's outside the SemVer guarantees; don't use it in an app. createE2ee refuses its RNGs unless you pass dangerouslyAllowTestRng: true.
The library can only keep its promises if the app around it does its part. SPEC §19 is the normative list. You MUST:
rng where globalThis.crypto is absent (React Native/Hermes). Also install a globalThis.crypto.getRandomValues polyfill before the library first loads, so @noble/curves can blind secret scalar multiplications (see THREATMODEL.md item 16).encrypt before sending.withSession.expectedPeerBoxPk and expectedPeerSignPk from a pinned, verified bundle or device list, never from the message being processed.pendingOtpDeletes and pendingReplayRecords before deleting any session.fn, follow §8.4, and send the control message after every re-initiation.MAX_WIRE_BYTES, and require canonical payloads (payload === canonicalJson(parseMessage(payload))) where reports are supported.spkId or otpId on a device; take ids from a persisted counter.parseState fails inside withSession, delete the session and tracker through the app's delete path, and run an OTP expiry as §19 item 13 describes.handshakeDigest, never by hand.suite: "x25519-xchacha20poly1305-sha256-v2" to initiateSession. Until then, a first contact can still fall back to v1 and its unknown-key-share weakness (§19 item 17).What was done instead of an audit, so you can judge it for yourself:
scripts/ref/ is one, written from the spec without reading the library, and CI checks that the two agree on every vector (319 checks).vectors/ are regenerated from fixed seeds on every run and must match byte for byte.decrypt and acceptSession with mutated input. npm run test:release runs 10,000 cases of each.test/security/. They're no substitute for an independent reviewer.Four runtime dependencies, pinned exact. scripts/check-deps.mjs fails the build if the list changes or if any package in the production tree has a non-permissive licence.
No install scripts, here or in any runtime dependency, and CI installs with --ignore-scripts.
Releases are built and published by release.yml through npm trusted publishing. There is no npm token to steal, and every version carries a provenance attestation that ties the tarball to the commit and the workflow run. Check it with:
npm audit signatures
Each GitHub release has the same tarball attached, with its SHA-256.
Workflow actions are pinned to commit SHAs and audited with zizmor; gitleaks, semgrep and OSV-Scanner run on every push to main and on every pull request.
The vectors ship in the package, so another implementation can test against them without cloning this repo:
import x3dh from "brnr-e2ee/vectors/x3dh.json" with { type: "json" };
Each file starts with a description. Most hold a list of cases with inputs and expected outputs; the conversation vectors hold parties and a list of events to replay in order. SPEC §15 describes the deterministic RNG the vectors were generated with.
The package follows SemVer, and the wire format is versioned separately (SPEC §18):
x25519-xchacha20poly1305-sha256-v1, and 1.1 adds x25519-xchacha20poly1305-sha256-v2 and prefers it;Changes are listed in CHANGELOG.md.
Bug reports, questions about the spec, and pull requests are welcome; CONTRIBUTING.md explains how to set up and what the checks expect. Please report security issues privately, as SECURITY.md describes, and not in a public issue.
MIT © Ashwin Sathian