Home - xero/leviathan-crypto GitHub Wiki

  โ–ˆโ–ˆ     โ–โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ–ˆโ–ˆ     โ–โ–ˆโ–Œ  โ–„โ–ˆโ–Œ   โ–ˆโ–ˆโ–ˆโ–Œ โ–€โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–€โ–„โ–ˆโ–ˆโ–Œ  โ–โ–ˆโ–Œ  โ–ˆโ–ˆโ–ˆโ–Œ    โ–ˆโ–ˆโ–Œ   โ–“โ–“
 โ–โ–ˆโ–Œ     โ–โ–ˆโ–Œ    โ–“โ–ˆ     โ–โ–ˆโ–Œ  โ–“โ–ˆโ–ˆ  โ–โ–ˆโ–Œโ–ˆโ–ˆ    โ–โ–ˆโ–Œ   โ–ˆโ–ˆโ–ˆ   โ–ˆโ–ˆโ–Œ โ–โ–ˆโ–Œโ–ˆโ–ˆ    โ–“โ–ˆโ–ˆ   โ–ˆโ–ˆ
 โ–ˆโ–ˆโ–Œ     โ–‘โ–ˆโ–ˆโ–ˆ   โ–โ–ˆโ–Œ    โ–ˆโ–ˆ   โ–€โ–€   โ–ˆโ–ˆ โ–โ–ˆโ–Œ   โ–ˆโ–ˆ   โ–โ–ˆโ–ˆโ–Œ   โ–ˆโ–“  โ–“โ–ˆ โ–โ–ˆโ–Œ  โ–โ–ˆโ–ˆโ–ˆโ–Œ  โ–ˆโ–“
 โ–ˆโ–ˆ      โ–ˆโ–ˆ     โ–โ–ˆโ–Œ    โ–ˆโ–“  โ–โ–ˆโ–ˆ  โ–โ–ˆโ–Œ  โ–ˆโ–“   โ–ˆโ–ˆ   โ–โ–ˆโ–ˆโ–„โ–„ โ–โ–ˆโ–Œ โ–โ–ˆโ–Œ  โ–ˆโ–ˆ  โ–โ–ˆโ–Œโ–ˆโ–ˆ โ–โ–ˆโ–Œ
โ–โ–ˆโ–Œ     โ–โ–ˆโ–Œ      โ–ˆโ–ˆ   โ–โ–ˆโ–Œ  โ–ˆโ–ˆ   โ–ˆโ–ˆ   โ–ˆโ–ˆ  โ–โ–ˆโ–Œ   โ–ˆโ–ˆโ–€โ–€โ–ˆโ–ˆโ–ˆโ–ˆโ–Œ โ–ˆโ–ˆ   โ–ˆโ–ˆ  โ–ˆโ–ˆ โ–โ–ˆโ–Œโ–โ–ˆโ–Œ
โ–โ–’โ–Œ     โ–โ–’โ–Œ      โ–โ–’โ–Œ  โ–ˆโ–ˆ   โ–’โ–ˆ   โ–ˆโ–ˆโ–€โ–€โ–€โ–ˆโ–ˆโ–Œ โ–โ–’โ–Œ   โ–’โ–ˆ    โ–ˆโ–“โ–‘ โ–’โ–ˆโ–€โ–€โ–€โ–ˆโ–ˆโ–Œ โ–’โ–ˆ  โ–ˆโ–ˆโ–โ–ˆ
โ–ˆโ–“ โ–„โ–„โ–“โ–ˆ โ–ˆโ–“ โ–„โ–„โ–“โ–ˆ   โ–“โ–“ โ–โ–“โ–Œ  โ–โ–“โ–Œ  โ–โ–ˆโ–Œ   โ–โ–’โ–Œ โ–ˆโ–“   โ–โ–“โ–Œ   โ–โ–“โ–ˆ โ–โ–“โ–Œ   โ–โ–’โ–Œโ–โ–“โ–Œ  โ–โ–ˆโ–ˆโ–ˆ
โ–“โ–ˆโ–ˆโ–€โ–€   โ–“โ–ˆโ–ˆโ–€โ–€      โ–“โ–ˆโ–“โ–ˆ   โ–โ–ˆโ–Œ  โ–โ–ˆโ–Œ   โ–โ–“โ–Œ โ–“โ–ˆ   โ–โ–ˆโ–Œ   โ–โ–ˆโ–“ โ–โ–ˆโ–Œ   โ–โ–“โ–Œโ–โ–ˆโ–Œ   โ–ˆโ–ˆโ–“
                    โ–“โ–ˆ                               โ–€โ–€        โ–โ–ˆโ–Œโ–Œโ–Œ

Leviathan Crypto Library

bun add leviathan-crypto
# or
npm install leviathan-crypto

No bundler is required. See CDN usage.


AEAD

Seal, SealStream, OpenStream, and SealStreamPool are the primary API for authenticated encryption in leviathan-crypto. They are cipher-agnostic: you pass a CipherSuite object at construction and the implementation handles key derivation, nonce management, and authentication for you.

The classes form a natural progression:

  • Seal handles data that fits in memory (>~66k).
  • SealStream and OpenStream handle data that arrives in chunks or is too large to buffer.
  • SealStreamPool parallelizes the chunked approach across Web Workers.

All four produce and consume the same wire format, so a Seal blob can be opened by OpenStream and vice versa.


Signatures

Sign, SignStream, and VerifyStream are the primary API for digital signatures in leviathan-crypto. They are scheme-agnostic: you pass a SignatureSuite object at construction and the implementation handles context binding, M' construction, and authentication for you.

The classes form a natural progression:

  • Sign handles data that fits in memory.
  • SignStream and VerifyStream handle data that arrives in chunks or is too large to buffer.

All three produce and consume the same wire format, so a Sign blob can be verified by VerifyStream and vice versa.


Session primitives

The ratchet module provides Double-Ratchet KDF primitives with post-quantum KEM steps, for consumers building forward-secret session protocols (secure messengers, streaming key-rotation systems) whose needs outgrow one-shot AEAD.

These are the primitives, not a full session. You compose them with your transport, header format, and epoch orchestration. See the ratchet guide for the construction.


Find the right tool

I want to...
Encrypt data Seal with SerpentCipher, XChaCha20Cipher, or AESGCMSIVCipher
Encrypt a stream or large file SealStream to encrypt, OpenStream to decrypt
Encrypt in parallel SealStreamPool distributes chunks across Web Workers
Add post-quantum security MlKemSuite wraps MlKem512, MlKem768, or MlKem1024 with any cipher suite
Build a forward-secret session ratchetInit, KDFChain, kemRatchetEncap / kemRatchetDecap, SkippedKeyStore
Sign data with a classical signature Ed25519Suite / Ed25519PreHashSuite (ed25519.md) or EcdsaP256Suite (ecdsa-p256.md) via Sign / SignStream / VerifyStream
Sign data with a post-quantum signature MlDsa44/65/87Suite (+ *PreHashSuite) for lattice ML-DSA (mldsa.md) or SlhDsa128f/192f/256fSuite (+ *PreHashSuite) for hash-based SLH-DSA (slhdsa.md). Full catalog in signaturesuite.md
Sign data with a classical+PQ hybrid MlDsa44Ed25519Suite, MlDsa65Ed25519Suite, MlDsa44EcdsaP256Suite, MlDsa65EcdsaP256Suite for draft-ietf-lamps-pq-composite-sigs
Sign data with a PQ-only hybrid MlDsa44SlhDsa128fSuite, MlDsa65SlhDsa192fSuite, MlDsa87SlhDsa256fSuite for ML-DSA + SLH-DSA composites at matching NIST categories
Build a transparency log MerkleLog for append plus inclusion / consistency proofs, MerkleVerifier for clients, SignedLog for custom storage backends
Exchange a key with a peer X25519 for Curve25519 Diffie-Hellman
Hash data SHA256, SHA384, SHA512, SHA3_256, SHA3_512, SHAKE256 ...
Authenticate a message HMAC_SHA256, HMAC_SHA384, HMAC_SHA512, or KMAC256
Derive keys HKDF_SHA256 or HKDF_SHA512
Generate random bytes Fortuna for forward-secret generation, randomBytes for one-off use
Compare secrets safely constantTimeEqual uses a WASM SIMD path to prevent timing attacks
Work with bytes hexToBytes, bytesToHex, wipe, xor, concat ...

For raw primitives, low-level cipher access, and ASM internals see the full API reference.

[!TIP] New to crypto? We have a lot of technical jargon. Checkout the lexicon if you need a glossary of cryptographic terminology.


Demos

We maintain a number of demo applications for the library

cli [ npm ยท source ยท readme ]

lvthn command-line file encryption tool supporting Serpent-256-CBC+HMAC-SHA256, XChaCha20-Poly1305, and AES-256-GCM-SIV, selectable via the --cipher flag. A single keyfile is compatible with all three ciphers; the header byte determines decryption automatically. Encryption and decryption distribute 64KB chunks across a worker pool sized to hardwareConcurrency. Each worker owns an isolated WASM instance with no shared memory between workers. The tool can export its own interactive completions for a variety of shells.

bun add -g lvthn # or npm i -g lvthn
lvthn keygen --armor -o my.key
cat secret.txt | lvthn encrypt -k my.key --armor > secret.enc

COVCOM [ demo ยท source ยท readme ]

Covert communications app suite for private group conversations. Invite, talk, close the client, and the chat vanishes. Every message is encrypted with XChaCha20 and signed with Ed25519. A BLAKE3 fingerprint on each key allows peers to verify one another. SPQR's manual and epoch ratchets add forward secrecy, while post-quantum ML-KEM-768 encapsulation keeps recorded communications unreadable and secure against future cryptanalysis.

web [ demo ยท source ยท readme ]

A self-contained browser encryption tool in a single HTML file. Encrypt text or files with Serpent-256-CBC and scrypt key derivation, then share the armored output. No server, no install, no network connection after initial load. The code is written to be read. The Encrypt-then-MAC construction, HMAC input, and scrypt parameters are all intentional examples worth studying.

tamper [ demo ยท source ยท readme ]

A crypto attack-resilience demo. It runs a real two-party encrypted channel, then lets you attack it: forge a replay and the sequence check rejects it, tamper with a frame and the Poly1305 tag fails. Key exchange uses X25519 with HKDF-SHA256, message encryption uses XChaCha20-Poly1305, and the relay server is a dumb WebSocket pipe that never sees plaintext. The demo deconstructs the protocol step by step with visual feedback for injection and replay attacks. For a real, production-ready secure messenger built on the same library, see COVCOM.

kyber [ demo ยท source ยท readme ]

Post-quantum cryptography demo simulating a complete ML-KEM key encapsulation ceremony between two browser-side clients. A live wire at the top of the page logs every value that crosses the channel; importantly, the shared secret never appears in the wire. After the ceremony completes, both sides independently derive a symmetric key using HKDF-SHA256 and exchange messages encrypted with XChaCha20-Poly1305. Each wire frame is expandable, revealing the raw nonce, ciphertext, Poly1305 tag, and AAD.

jwt [ demo ยท source ยท readme ]

Classical and post-quantum JSON Web Token signing demo in a single self-contained HTML file. It signs the same claims across eleven algorithms: EdDSA and ES256, the post-quantum ML-DSA and SLH-DSA families, and the leviathan hybrid composites. Every algorithm runs through one uniform path on the Sign suite API, with no per-algorithm branching. The token renders with its three segments color-coded and a live byte readout, so the cost of quantum resistance is visible: the same token grows from about 220 bytes under Ed25519 to past 66 kilobytes under SLH-DSA-SHAKE-256f. Tamper with the payload and verification rejects it, because the signature covers the original bytes.