leviathan-crypto 2.1.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (296) hide show
  1. package/CLAUDE.md +86 -443
  2. package/README.md +198 -65
  3. package/dist/aes/aes-cbc.d.ts +40 -0
  4. package/dist/aes/aes-cbc.js +158 -0
  5. package/dist/aes/aes-ctr.d.ts +50 -0
  6. package/dist/aes/aes-ctr.js +141 -0
  7. package/dist/aes/aes-gcm-siv.d.ts +67 -0
  8. package/dist/aes/aes-gcm-siv.js +217 -0
  9. package/dist/aes/aes-gcm.d.ts +61 -0
  10. package/dist/aes/aes-gcm.js +226 -0
  11. package/dist/aes/cipher-suite.d.ts +21 -0
  12. package/dist/aes/cipher-suite.js +179 -0
  13. package/dist/aes/embedded.d.ts +1 -0
  14. package/dist/aes/embedded.js +26 -0
  15. package/dist/aes/generator.d.ts +14 -0
  16. package/dist/aes/generator.js +103 -0
  17. package/dist/aes/index.d.ts +58 -0
  18. package/dist/aes/index.js +125 -0
  19. package/dist/aes/ops.d.ts +60 -0
  20. package/dist/aes/ops.js +164 -0
  21. package/dist/aes/pool-worker.d.ts +1 -0
  22. package/dist/aes/pool-worker.js +92 -0
  23. package/dist/aes/types.d.ts +1 -0
  24. package/dist/aes/types.js +23 -0
  25. package/dist/aes.wasm +0 -0
  26. package/dist/blake3/embedded.d.ts +1 -0
  27. package/dist/blake3/embedded.js +26 -0
  28. package/dist/blake3/index.d.ts +143 -0
  29. package/dist/blake3/index.js +620 -0
  30. package/dist/blake3/types.d.ts +102 -0
  31. package/dist/blake3/types.js +31 -0
  32. package/dist/blake3/validate.d.ts +29 -0
  33. package/dist/blake3/validate.js +80 -0
  34. package/dist/blake3.wasm +0 -0
  35. package/dist/chacha20/cipher-suite.js +47 -25
  36. package/dist/chacha20/generator.d.ts +2 -2
  37. package/dist/chacha20/generator.js +4 -4
  38. package/dist/chacha20/index.d.ts +16 -15
  39. package/dist/chacha20/index.js +52 -46
  40. package/dist/chacha20/ops.d.ts +7 -7
  41. package/dist/chacha20/ops.js +34 -34
  42. package/dist/chacha20/pool-worker.js +5 -3
  43. package/dist/cte-wasm.d.ts +1 -0
  44. package/dist/cte-wasm.js +3 -0
  45. package/dist/curve25519.wasm +0 -0
  46. package/dist/ecdsa/der.d.ts +23 -0
  47. package/dist/ecdsa/der.js +192 -0
  48. package/dist/ecdsa/ecprivatekey-der.d.ts +32 -0
  49. package/dist/ecdsa/ecprivatekey-der.js +230 -0
  50. package/dist/ecdsa/embedded.d.ts +1 -0
  51. package/dist/ecdsa/embedded.js +25 -0
  52. package/dist/ecdsa/index.d.ts +124 -0
  53. package/dist/ecdsa/index.js +366 -0
  54. package/dist/ecdsa/types.d.ts +31 -0
  55. package/dist/ecdsa/types.js +28 -0
  56. package/dist/ecdsa/validate.d.ts +18 -0
  57. package/dist/ecdsa/validate.js +92 -0
  58. package/dist/ed25519/embedded.d.ts +1 -0
  59. package/dist/ed25519/embedded.js +31 -0
  60. package/dist/ed25519/index.d.ts +70 -0
  61. package/dist/ed25519/index.js +308 -0
  62. package/dist/ed25519/types.d.ts +27 -0
  63. package/dist/ed25519/types.js +27 -0
  64. package/dist/ed25519/validate.d.ts +7 -0
  65. package/dist/ed25519/validate.js +77 -0
  66. package/dist/embedded/aes-pool-worker.d.ts +1 -0
  67. package/dist/embedded/aes-pool-worker.js +5 -0
  68. package/dist/embedded/aes.d.ts +1 -0
  69. package/dist/embedded/aes.js +3 -0
  70. package/dist/embedded/blake3.d.ts +1 -0
  71. package/dist/embedded/blake3.js +3 -0
  72. package/dist/embedded/chacha20-pool-worker.d.ts +1 -1
  73. package/dist/embedded/chacha20-pool-worker.js +2 -2
  74. package/dist/embedded/chacha20.d.ts +1 -1
  75. package/dist/embedded/chacha20.js +2 -2
  76. package/dist/embedded/curve25519.d.ts +1 -0
  77. package/dist/embedded/curve25519.js +3 -0
  78. package/dist/embedded/mldsa.d.ts +1 -0
  79. package/dist/embedded/mldsa.js +3 -0
  80. package/dist/embedded/mlkem.d.ts +1 -0
  81. package/dist/embedded/mlkem.js +3 -0
  82. package/dist/embedded/p256.d.ts +1 -0
  83. package/dist/embedded/p256.js +3 -0
  84. package/dist/embedded/serpent-pool-worker.d.ts +1 -1
  85. package/dist/embedded/serpent-pool-worker.js +2 -2
  86. package/dist/embedded/serpent.d.ts +1 -1
  87. package/dist/embedded/serpent.js +2 -2
  88. package/dist/embedded/sha2.d.ts +1 -1
  89. package/dist/embedded/sha2.js +2 -2
  90. package/dist/embedded/sha3.d.ts +1 -1
  91. package/dist/embedded/sha3.js +2 -2
  92. package/dist/embedded/slhdsa.d.ts +1 -0
  93. package/dist/embedded/slhdsa.js +3 -0
  94. package/dist/errors.d.ts +92 -1
  95. package/dist/errors.js +111 -1
  96. package/dist/fortuna.d.ts +5 -5
  97. package/dist/fortuna.js +37 -64
  98. package/dist/index.d.ts +38 -9
  99. package/dist/index.js +63 -19
  100. package/dist/init.d.ts +1 -1
  101. package/dist/init.js +11 -25
  102. package/dist/keccak/embedded.js +1 -1
  103. package/dist/keccak/index.d.ts +2 -0
  104. package/dist/keccak/index.js +4 -2
  105. package/dist/loader.d.ts +1 -24
  106. package/dist/loader.js +13 -16
  107. package/dist/merkle/blake3-tree.d.ts +35 -0
  108. package/dist/merkle/blake3-tree.js +187 -0
  109. package/dist/merkle/checkpoint.d.ts +58 -0
  110. package/dist/merkle/checkpoint.js +217 -0
  111. package/dist/merkle/index.d.ts +19 -0
  112. package/dist/merkle/index.js +37 -0
  113. package/dist/merkle/merkle-log.d.ts +130 -0
  114. package/dist/merkle/merkle-log.js +207 -0
  115. package/dist/merkle/merkle-verifier.d.ts +126 -0
  116. package/dist/merkle/merkle-verifier.js +296 -0
  117. package/dist/merkle/proof.d.ts +70 -0
  118. package/dist/merkle/proof.js +300 -0
  119. package/dist/merkle/sha256-tree.d.ts +33 -0
  120. package/dist/merkle/sha256-tree.js +145 -0
  121. package/dist/merkle/signed-log.d.ts +156 -0
  122. package/dist/merkle/signed-log.js +356 -0
  123. package/dist/merkle/signed-note.d.ts +309 -0
  124. package/dist/merkle/signed-note.js +648 -0
  125. package/dist/merkle/sth.d.ts +31 -0
  126. package/dist/merkle/sth.js +31 -0
  127. package/dist/merkle/storage.d.ts +40 -0
  128. package/dist/merkle/storage.js +71 -0
  129. package/dist/merkle/tree.d.ts +68 -0
  130. package/dist/merkle/tree.js +94 -0
  131. package/dist/mldsa/embedded.d.ts +1 -0
  132. package/dist/{kyber → mldsa}/embedded.js +5 -5
  133. package/dist/mldsa/expand.d.ts +53 -0
  134. package/dist/mldsa/expand.js +188 -0
  135. package/dist/mldsa/format.d.ts +16 -0
  136. package/dist/mldsa/format.js +68 -0
  137. package/dist/mldsa/hashvariant.d.ts +32 -0
  138. package/dist/mldsa/hashvariant.js +248 -0
  139. package/dist/mldsa/index.d.ts +142 -0
  140. package/dist/mldsa/index.js +463 -0
  141. package/dist/mldsa/keygen.d.ts +16 -0
  142. package/dist/mldsa/keygen.js +232 -0
  143. package/dist/mldsa/params.d.ts +21 -0
  144. package/dist/mldsa/params.js +55 -0
  145. package/dist/mldsa/sha3-helpers.d.ts +30 -0
  146. package/dist/mldsa/sha3-helpers.js +124 -0
  147. package/dist/mldsa/sign.d.ts +36 -0
  148. package/dist/mldsa/sign.js +380 -0
  149. package/dist/mldsa/types.d.ts +91 -0
  150. package/dist/mldsa/types.js +25 -0
  151. package/dist/mldsa/validate.d.ts +55 -0
  152. package/dist/mldsa/validate.js +125 -0
  153. package/dist/mldsa/verify.d.ts +29 -0
  154. package/dist/mldsa/verify.js +269 -0
  155. package/dist/mldsa.wasm +0 -0
  156. package/dist/mlkem/embedded.d.ts +1 -0
  157. package/dist/mlkem/embedded.js +27 -0
  158. package/dist/mlkem/indcpa.d.ts +49 -0
  159. package/dist/{kyber → mlkem}/indcpa.js +44 -44
  160. package/dist/mlkem/index.d.ts +37 -0
  161. package/dist/{kyber → mlkem}/index.js +24 -34
  162. package/dist/mlkem/kem.d.ts +21 -0
  163. package/dist/{kyber → mlkem}/kem.js +44 -64
  164. package/dist/{kyber → mlkem}/params.d.ts +4 -4
  165. package/dist/{kyber → mlkem}/params.js +2 -2
  166. package/dist/mlkem/suite.d.ts +12 -0
  167. package/dist/{kyber → mlkem}/suite.js +17 -12
  168. package/dist/{kyber → mlkem}/types.d.ts +3 -3
  169. package/dist/{kyber → mlkem}/types.js +1 -1
  170. package/dist/{kyber → mlkem}/validate.d.ts +7 -7
  171. package/dist/{kyber → mlkem}/validate.js +7 -7
  172. package/dist/{kyber.wasm → mlkem.wasm} +0 -0
  173. package/dist/p256.wasm +0 -0
  174. package/dist/ratchet/index.d.ts +2 -0
  175. package/dist/ratchet/index.js +1 -0
  176. package/dist/ratchet/kdf-chain.js +3 -3
  177. package/dist/ratchet/ratchet-keypair.js +2 -2
  178. package/dist/ratchet/root-kdf.js +7 -7
  179. package/dist/ratchet/skipped-key-store.js +4 -4
  180. package/dist/ratchet/types.d.ts +1 -1
  181. package/dist/serpent/cipher-suite.js +20 -17
  182. package/dist/serpent/generator.d.ts +1 -1
  183. package/dist/serpent/generator.js +2 -2
  184. package/dist/serpent/index.d.ts +8 -7
  185. package/dist/serpent/index.js +18 -27
  186. package/dist/serpent/pool-worker.js +7 -5
  187. package/dist/serpent/serpent-cbc.d.ts +4 -4
  188. package/dist/serpent/serpent-cbc.js +11 -8
  189. package/dist/serpent/shared-ops.d.ts +3 -23
  190. package/dist/serpent/shared-ops.js +50 -85
  191. package/dist/serpent.wasm +0 -0
  192. package/dist/sha2/hkdf.js +5 -5
  193. package/dist/sha2/index.d.ts +21 -1
  194. package/dist/sha2/index.js +65 -10
  195. package/dist/sha2/types.d.ts +41 -2
  196. package/dist/sha2.wasm +0 -0
  197. package/dist/sha3/index.d.ts +72 -3
  198. package/dist/sha3/index.js +240 -14
  199. package/dist/sha3/kmac.d.ts +121 -0
  200. package/dist/sha3/kmac.js +800 -0
  201. package/dist/sha3.wasm +0 -0
  202. package/dist/shared/pkcs7.d.ts +22 -0
  203. package/dist/shared/pkcs7.js +84 -0
  204. package/dist/sign/ctx.d.ts +41 -0
  205. package/dist/sign/ctx.js +102 -0
  206. package/dist/sign/envelope.d.ts +45 -0
  207. package/dist/sign/envelope.js +152 -0
  208. package/dist/sign/hasher.d.ts +9 -0
  209. package/dist/sign/hasher.js +132 -0
  210. package/dist/sign/index.d.ts +11 -0
  211. package/dist/sign/index.js +34 -0
  212. package/dist/sign/sign-stream.d.ts +25 -0
  213. package/dist/sign/sign-stream.js +112 -0
  214. package/dist/sign/suites/ecdsa-p256.d.ts +2 -0
  215. package/dist/sign/suites/ecdsa-p256.js +120 -0
  216. package/dist/sign/suites/ed25519.d.ts +3 -0
  217. package/dist/sign/suites/ed25519.js +165 -0
  218. package/dist/sign/suites/hybrid-classical.d.ts +23 -0
  219. package/dist/sign/suites/hybrid-classical.js +526 -0
  220. package/dist/sign/suites/hybrid-pq.d.ts +4 -0
  221. package/dist/sign/suites/hybrid-pq.js +234 -0
  222. package/dist/sign/suites/mldsa.d.ts +7 -0
  223. package/dist/sign/suites/mldsa.js +161 -0
  224. package/dist/sign/suites/slhdsa.d.ts +7 -0
  225. package/dist/sign/suites/slhdsa.js +176 -0
  226. package/dist/sign/types.d.ts +106 -0
  227. package/dist/sign/types.js +28 -0
  228. package/dist/sign/verify-stream.d.ts +30 -0
  229. package/dist/sign/verify-stream.js +227 -0
  230. package/dist/slhdsa/embedded.d.ts +1 -0
  231. package/dist/slhdsa/embedded.js +26 -0
  232. package/dist/slhdsa/index.d.ts +149 -0
  233. package/dist/slhdsa/index.js +493 -0
  234. package/dist/slhdsa/params.d.ts +26 -0
  235. package/dist/slhdsa/params.js +70 -0
  236. package/dist/slhdsa/prehash.d.ts +68 -0
  237. package/dist/slhdsa/prehash.js +307 -0
  238. package/dist/slhdsa/sign.d.ts +39 -0
  239. package/dist/slhdsa/sign.js +116 -0
  240. package/dist/slhdsa/types.d.ts +129 -0
  241. package/dist/slhdsa/types.js +27 -0
  242. package/dist/slhdsa/validate.d.ts +60 -0
  243. package/dist/slhdsa/validate.js +127 -0
  244. package/dist/slhdsa/verify.d.ts +32 -0
  245. package/dist/slhdsa/verify.js +107 -0
  246. package/dist/slhdsa.wasm +0 -0
  247. package/dist/stream/header.js +3 -3
  248. package/dist/stream/index.d.ts +1 -0
  249. package/dist/stream/index.js +1 -0
  250. package/dist/stream/open-stream.js +31 -10
  251. package/dist/stream/seal-stream-pool.d.ts +1 -0
  252. package/dist/stream/seal-stream-pool.js +63 -26
  253. package/dist/stream/seal-stream.d.ts +1 -1
  254. package/dist/stream/seal-stream.js +20 -9
  255. package/dist/stream/seal.js +6 -6
  256. package/dist/stream/types.d.ts +3 -1
  257. package/dist/stream/types.js +1 -1
  258. package/dist/types.d.ts +1 -1
  259. package/dist/types.js +1 -1
  260. package/dist/utils.d.ts +3 -3
  261. package/dist/utils.js +46 -54
  262. package/dist/wasm-source.d.ts +7 -7
  263. package/dist/wasm-source.js +1 -1
  264. package/dist/x25519/embedded.d.ts +1 -0
  265. package/dist/x25519/embedded.js +31 -0
  266. package/dist/x25519/index.d.ts +43 -0
  267. package/dist/x25519/index.js +159 -0
  268. package/dist/x25519/types.d.ts +25 -0
  269. package/dist/x25519/types.js +27 -0
  270. package/dist/x25519/validate.d.ts +2 -0
  271. package/dist/x25519/validate.js +39 -0
  272. package/package.json +70 -26
  273. package/SECURITY.md +0 -163
  274. package/dist/ct-wasm.d.ts +0 -1
  275. package/dist/ct-wasm.js +0 -3
  276. package/dist/docs/aead.md +0 -363
  277. package/dist/docs/architecture.md +0 -1011
  278. package/dist/docs/argon2id.md +0 -305
  279. package/dist/docs/chacha20.md +0 -781
  280. package/dist/docs/exports.md +0 -277
  281. package/dist/docs/fortuna.md +0 -530
  282. package/dist/docs/init.md +0 -301
  283. package/dist/docs/loader.md +0 -256
  284. package/dist/docs/serpent.md +0 -617
  285. package/dist/docs/sha2.md +0 -671
  286. package/dist/docs/sha3.md +0 -612
  287. package/dist/docs/types.md +0 -416
  288. package/dist/docs/utils.md +0 -457
  289. package/dist/embedded/kyber.d.ts +0 -1
  290. package/dist/embedded/kyber.js +0 -3
  291. package/dist/kyber/embedded.d.ts +0 -1
  292. package/dist/kyber/indcpa.d.ts +0 -49
  293. package/dist/kyber/index.d.ts +0 -38
  294. package/dist/kyber/kem.d.ts +0 -21
  295. package/dist/kyber/suite.d.ts +0 -12
  296. /package/dist/{ct.wasm → cte.wasm} +0 -0
@@ -1,416 +0,0 @@
1
- <img src="https://github.com/xero/leviathan-crypto/raw/main/docs/logo.svg" alt="logo" width="120" align="left" margin="10">
2
-
3
- ### TypeScript Interfaces
4
-
5
- Defines the abstract interfaces all leviathan-crypto cryptographic classes implement. These are type-only exports; they contain no runtime code and generate no JavaScript output.
6
-
7
- > ### Table of Contents
8
- > - [API Reference](#api-reference)
9
- > - [Usage Examples](#usage-examples)
10
- > - [Generator](#generator)
11
- > - [HashFn](#hashfn)
12
- > - [WasmSource](#wasmsource)
13
- > - [CipherSuite](#ciphersuite)
14
- > - [DerivedKeys](#derivedkeys)
15
- > - [SealStreamOpts](#sealstreamopts)
16
- > - [PoolOpts](#poolopts)
17
-
18
- ---
19
-
20
- ## API Reference
21
-
22
- Use these interfaces when you need generic code that works with any hash function, any cipher, or any AEAD scheme without depending on a specific implementation. They are available immediately on import with no `init()` call required.
23
-
24
- ### Hash
25
-
26
- ```typescript
27
- interface Hash {
28
- hash(msg: Uint8Array): Uint8Array;
29
- dispose(): void;
30
- }
31
- ```
32
-
33
- Interface for unkeyed hash functions (e.g., SHA-256, SHA-512, SHA-3).
34
-
35
- | Method | Description |
36
- |---|---|
37
- | `hash(msg)` | Hashes the entire message and returns the digest as a new `Uint8Array`. |
38
- | `dispose()` | Releases WASM resources and wipes internal buffers. Call when done. |
39
-
40
- ---
41
-
42
- ### KeyedHash
43
-
44
- ```typescript
45
- interface KeyedHash {
46
- hash(key: Uint8Array, msg: Uint8Array): Uint8Array;
47
- dispose(): void;
48
- }
49
- ```
50
-
51
- Interface for keyed hash functions / MACs (e.g., HMAC-SHA256, HMAC-SHA512).
52
-
53
- `KeyedHash` does **not** extend `Hash`. Its `hash` method takes a `key` parameter in addition to the message.
54
-
55
- | Method | Description |
56
- |---|---|
57
- | `hash(key, msg)` | Computes the keyed hash / MAC over `msg` using `key`. Returns the tag as a new `Uint8Array`. |
58
- | `dispose()` | Releases WASM resources and wipes internal buffers. Call when done. |
59
-
60
- ---
61
-
62
- ### Blockcipher
63
-
64
- ```typescript
65
- interface Blockcipher {
66
- encrypt(block: Uint8Array): Uint8Array;
67
- decrypt(block: Uint8Array): Uint8Array;
68
- dispose(): void;
69
- }
70
- ```
71
-
72
- Interface for raw block ciphers (e.g., Serpent in ECB mode). Operates on single blocks.
73
-
74
- | Method | Description |
75
- |---|---|
76
- | `encrypt(block)` | Encrypts a single block and returns the ciphertext. |
77
- | `decrypt(block)` | Decrypts a single block and returns the plaintext. |
78
- | `dispose()` | Releases WASM resources and wipes internal buffers (including expanded key schedule). |
79
-
80
- ---
81
-
82
- ### Streamcipher
83
-
84
- ```typescript
85
- interface Streamcipher {
86
- encrypt(msg: Uint8Array): Uint8Array;
87
- decrypt(msg: Uint8Array): Uint8Array;
88
- dispose(): void;
89
- }
90
- ```
91
-
92
- Interface for stream ciphers and block cipher streaming modes (e.g., Serpent-CTR, ChaCha20). Handles arbitrary-length messages.
93
-
94
- | Method | Description |
95
- |---|---|
96
- | `encrypt(msg)` | Encrypts an arbitrary-length message. Returns the ciphertext (same length as input). |
97
- | `decrypt(msg)` | Decrypts an arbitrary-length ciphertext. Returns the plaintext (same length as input). |
98
- | `dispose()` | Releases WASM resources and wipes internal buffers. |
99
-
100
- ---
101
-
102
- ### AEAD
103
-
104
- ```typescript
105
- interface AEAD {
106
- encrypt(msg: Uint8Array, aad?: Uint8Array): Uint8Array;
107
- decrypt(ciphertext: Uint8Array, aad?: Uint8Array): Uint8Array;
108
- dispose(): void;
109
- }
110
- ```
111
-
112
- Interface for authenticated encryption with associated data (e.g., XChaCha20-Poly1305). Provides both confidentiality and integrity.
113
-
114
- | Method | Description |
115
- |---|---|
116
- | `encrypt(msg, aad?)` | Encrypts `msg` and authenticates both `msg` and optional `aad`. Returns ciphertext with appended authentication tag. |
117
- | `decrypt(ciphertext, aad?)` | Decrypts and verifies the authentication tag. Returns plaintext on success. Throws `Error` on authentication failure. Never returns null. |
118
- | `dispose()` | Releases WASM resources and wipes internal buffers. |
119
-
120
- ---
121
-
122
- ## Usage Examples
123
-
124
- ### Type-constraining a function parameter
125
-
126
- ```typescript
127
- import type { Hash } from 'leviathan-crypto'
128
-
129
- function digestAndLog(hasher: Hash, data: Uint8Array): Uint8Array {
130
- const digest = hasher.hash(data)
131
- console.log('digest length:', digest.length)
132
- return digest
133
- }
134
- ```
135
-
136
- This function accepts any `Hash` implementation (`SHA256`, `SHA512`, `SHA3_256`, etc.) without importing any of them directly.
137
-
138
- ---
139
-
140
- ### Accepting any AEAD scheme
141
-
142
- ```typescript
143
- import type { AEAD } from 'leviathan-crypto'
144
-
145
- function sealMessage(aead: AEAD, plaintext: Uint8Array, metadata: Uint8Array): Uint8Array {
146
- return aead.encrypt(plaintext, metadata)
147
- }
148
-
149
- function openMessage(aead: AEAD, ciphertext: Uint8Array, metadata: Uint8Array): Uint8Array {
150
- // decrypt() throws on auth failure, no null check needed
151
- return aead.decrypt(ciphertext, metadata)
152
- }
153
- ```
154
-
155
- ---
156
-
157
- ### Generic keyed-hash wrapper
158
-
159
- ```typescript
160
- import type { KeyedHash } from 'leviathan-crypto'
161
-
162
- function authenticate(mac: KeyedHash, key: Uint8Array, ...parts: Uint8Array[]): Uint8Array {
163
- // Concatenate all message parts, then compute the tag
164
- const total = parts.reduce((sum, p) => sum + p.length, 0)
165
- const msg = new Uint8Array(total)
166
- let offset = 0
167
- for (const part of parts) {
168
- msg.set(part, offset)
169
- offset += part.length
170
- }
171
- return mac.hash(key, msg)
172
- }
173
- ```
174
-
175
- ---
176
-
177
- ### Storing a cipher with its interface type
178
-
179
- ```typescript
180
- import type { Streamcipher, Blockcipher } from 'leviathan-crypto'
181
-
182
- interface EncryptionContext {
183
- cipher: Streamcipher | Blockcipher
184
- mode: 'stream' | 'block'
185
- }
186
-
187
- function cleanup(ctx: EncryptionContext): void {
188
- ctx.cipher.dispose()
189
- }
190
- ```
191
-
192
- ---
193
-
194
- ## Generator
195
-
196
- ```typescript
197
- interface Generator {
198
- readonly keySize: number; // bytes
199
- readonly blockSize: number; // bytes per cipher block
200
- readonly counterSize: number; // bytes
201
- readonly wasmModules: readonly string[];
202
- generate(key: Uint8Array, counter: Uint8Array, n: number): Uint8Array;
203
- }
204
- ```
205
-
206
- Stateless cipher PRF. Used by `Fortuna` as the generator slot.
207
- Implementations are plain const objects; they assert that no stateful
208
- instance owns the underlying WASM module before each call but do not
209
- acquire it themselves.
210
-
211
- | Member | Description |
212
- |---|---|
213
- | `keySize` | Generator key size in bytes. Must equal the paired `HashFn.outputSize` when used with `Fortuna`. |
214
- | `blockSize` | Bytes per cipher block. Used by `Fortuna` to compute counter advancement. |
215
- | `counterSize` | Counter width in bytes. `Fortuna` allocates its `genCnt` of this size. |
216
- | `wasmModules` | List of WASM module names the generator depends on. Used for `init()` preflight in `Fortuna.create()`. |
217
- | `generate(key, counter, n)` | Produces `n` bytes of keystream from `(key, counter)`. Stateless; does not mutate either input. |
218
-
219
- Shipped implementations: `SerpentGenerator` (from `'leviathan-crypto/serpent'`), `ChaCha20Generator` (from `'leviathan-crypto/chacha20'`).
220
-
221
- ---
222
-
223
- ## HashFn
224
-
225
- ```typescript
226
- interface HashFn {
227
- readonly outputSize: number; // bytes
228
- readonly wasmModules: readonly string[];
229
- digest(msg: Uint8Array): Uint8Array;
230
- }
231
- ```
232
-
233
- Stateless hash function. Used by `Fortuna` for the accumulator chain and
234
- the reseed key derivation. Distinct from the existing `Hash` interface
235
- above, which describes class-shaped instances that own scratch state and
236
- require `dispose()`.
237
-
238
- | Member | Description |
239
- |---|---|
240
- | `outputSize` | Digest size in bytes. Must equal the paired `Generator.keySize` when used with `Fortuna`. |
241
- | `wasmModules` | List of WASM module names the hash depends on. |
242
- | `digest(msg)` | Produces a digest of the input. Stateless; safe to call concurrently with itself within a single JavaScript turn. |
243
-
244
- Shipped implementations: `SHA256Hash` (from `'leviathan-crypto/sha2'`), `SHA3_256Hash` (from `'leviathan-crypto/sha3'`).
245
-
246
- ---
247
-
248
- ## WasmSource
249
-
250
- Union type for WASM module sources. Accepted by `init()`, `serpentInit()`, etc.
251
-
252
- `string | URL | ArrayBuffer | Uint8Array | WebAssembly.Module | Response | Promise<Response>`
253
-
254
- ---
255
-
256
- ## CipherSuite
257
-
258
- Cipher-specific logic injected into `SealStream` and `OpenStream`.
259
-
260
- | Field | Type | Description |
261
- |-------|------|-------------|
262
- | `formatEnum` | `number` | Wire format ID encoded in header byte 0 bits 0-5 (max 0x3f): bits 0-3 = cipher nibble (0x1=xchacha20, 0x2=serpent), bits 4-5 = KEM selector (0x00=none, 0x10=ML-KEM-512, 0x20=ML-KEM-768, 0x30=ML-KEM-1024), bit 6 reserved |
263
- | `formatName` | `string` | Human-readable label, e.g. `'xchacha20'`, `'serpent'`, `'mlkem768+xchacha20'` |
264
- | `hkdfInfo` | `string` | HKDF info string for key derivation |
265
- | `keySize` | `number` | Seal/encrypt key size in bytes (encapsulation key bytes for KEM suites) |
266
- | `decKeySize` | `number \| undefined` | Open/decrypt key size in bytes (decapsulation key bytes for KEM suites). Absent → same as `keySize` (symmetric case) |
267
- | `kemCtSize` | `number` | KEM ciphertext byte length appended to the header in the preamble. `0` for symmetric suites |
268
- | `tagSize` | `number` | Authentication tag size in bytes |
269
- | `padded` | `boolean` | Whether ciphertext includes padding (PKCS7 for CBC) |
270
- | `wasmChunkSize` | `number` | WASM buffer capacity for one padded chunk. `SealStreamPool.create()` validates `paddedFull ≤ wasmChunkSize` at startup for padded ciphers and throws `RangeError` if the check fails. `SerpentCipher`: 65552. `XChaCha20Cipher`: 65536. `KyberSuite` forwards from its inner cipher. |
271
- | `wasmModules` | `readonly string[]` | Cipher-specific WASM modules used by pool workers and per-chunk operations (not transitive dependencies such as HKDF-SHA-256 used by `deriveKeys()`) |
272
-
273
- | Method | Signature | Description |
274
- |--------|-----------|-------------|
275
- | `deriveKeys` | `(masterKey, nonce, kemCt?) → DerivedKeys` | HKDF key derivation. `kemCt` is the KEM ciphertext; present only for hybrid suites. |
276
- | `sealChunk` | `(keys, counterNonce, chunk, aad?) → Uint8Array` | Encrypt one chunk |
277
- | `openChunk` | `(keys, counterNonce, chunk, aad?) → Uint8Array` | Decrypt one chunk |
278
- | `wipeKeys` | `(keys) → void` | Zero derived key material |
279
- | `createPoolWorker` | `() → Worker` | Create a Web Worker for pool use. Default spawns a classic worker from a blob URL over a build-time IIFE; override via spread for strict-CSP environments. See [ciphersuite.md](./ciphersuite.md). |
280
-
281
- Implementations: `XChaCha20Cipher`, `SerpentCipher` (plain `const` objects, not classes), and `KyberSuite` (factory function returning a `CipherSuite`). See [ciphersuite.md](./ciphersuite.md).
282
-
283
- > [!IMPORTANT]
284
- > All CipherSuite implementations use HKDF-SHA-256 in `deriveKeys()`. The stream layer requires
285
- > `sha2` to be initialized regardless of which cipher is selected.
286
-
287
- ---
288
-
289
- ## DerivedKeys
290
-
291
- Opaque key material returned by `CipherSuite.deriveKeys()`.
292
-
293
- | Field | Type | Description |
294
- |-------|------|-------------|
295
- | `bytes` | `readonly Uint8Array` | Raw derived key bytes (opaque to the stream layer) |
296
- | `kemCiphertext?` | `readonly Uint8Array \| undefined` | KEM ciphertext produced during encapsulation. Present only for hybrid KEM suites; absent for symmetric suites. |
297
-
298
- ---
299
-
300
- ## SealStreamOpts
301
-
302
- Options for `SealStream` constructor.
303
-
304
- | Field | Type | Default | Description |
305
- |-------|------|---------|-------------|
306
- | `chunkSize` | `number` | `65536` | Chunk size in bytes. Range: [1024, 16777215]. |
307
- | `framed` | `boolean` | `false` | Enable u32be length-prefixed framing. |
308
-
309
- ---
310
-
311
- ## PoolOpts
312
-
313
- Options for `SealStreamPool.create()`.
314
-
315
- | Field | Type | Default | Description |
316
- |-------|------|---------|-------------|
317
- | `wasm` | `WasmSource \| Record<string, WasmSource>` | | WASM module source(s). Single source for single-module ciphers, Record for multi-module. |
318
- | `workers` | `number` | `navigator.hardwareConcurrency ?? 4` | Number of Web Workers. |
319
- | `chunkSize` | `number` | `65536` | Chunk size in bytes. |
320
- | `framed` | `boolean` | `false` | Enable framed mode. |
321
- | `jobTimeout` | `number` | `30000` | Per-job timeout in milliseconds. |
322
-
323
- ---
324
-
325
- ## Ratchet types
326
-
327
- Shared types for the ratchet KDF module. See [ratchet.md](./ratchet.md) for full API.
328
-
329
- ### MlKemLike
330
-
331
- Structural interface satisfied by `MlKem512`, `MlKem768`, and `MlKem1024`. Used as the `kem` parameter type for `kemRatchetEncap`, `kemRatchetDecap`, and `RatchetKeypair`.
332
-
333
- ```typescript
334
- interface MlKemLike {
335
- readonly params: KyberParams
336
- keygen(): { encapsulationKey: Uint8Array; decapsulationKey: Uint8Array }
337
- encapsulate(ek: Uint8Array): { ciphertext: Uint8Array; sharedSecret: Uint8Array }
338
- decapsulate(dk: Uint8Array, ct: Uint8Array): Uint8Array
339
- }
340
- ```
341
-
342
- ### RatchetInitResult
343
-
344
- ```typescript
345
- interface RatchetInitResult {
346
- readonly nextRootKey: Uint8Array // 32 bytes
347
- readonly sendChainKey: Uint8Array // 32 bytes
348
- readonly recvChainKey: Uint8Array // 32 bytes
349
- }
350
- ```
351
-
352
- ### KemEncapResult
353
-
354
- ```typescript
355
- interface KemEncapResult {
356
- readonly nextRootKey: Uint8Array // 32 bytes
357
- readonly sendChainKey: Uint8Array // 32 bytes
358
- readonly recvChainKey: Uint8Array // 32 bytes
359
- readonly kemCt: Uint8Array // ML-KEM ciphertext — transmit in-band
360
- }
361
- ```
362
-
363
- ### KemDecapResult
364
-
365
- ```typescript
366
- interface KemDecapResult {
367
- readonly nextRootKey: Uint8Array // 32 bytes
368
- readonly sendChainKey: Uint8Array // 32 bytes
369
- readonly recvChainKey: Uint8Array // 32 bytes
370
- }
371
- ```
372
-
373
- ### RatchetMessageHeader
374
-
375
- ```typescript
376
- interface RatchetMessageHeader {
377
- readonly epoch: number // sender's epoch at seal time; starts 0, increments on ratchet step
378
- readonly counter: number // KDFChain.n at seal time (post-step value, first message = 1)
379
- readonly pn?: number // previous chain length — present only on the first message of a new epoch
380
- readonly kemCt?: Uint8Array // ML-KEM ciphertext — present only on the first message of a new epoch (encap side)
381
- }
382
- ```
383
-
384
- Canonical header shape for a ratchet-protected message. `pn` and `kemCt` are absent on every message except the first one of a new epoch, where both must be present together.
385
-
386
- ### ResolveHandle
387
-
388
- Return type of `SkippedKeyStore.resolve()`.
389
-
390
- ```typescript
391
- interface ResolveHandle {
392
- readonly key: Uint8Array // 32-byte message key — throws after settlement
393
- commit(): void // wipe key and mark settled — call on successful decrypt
394
- rollback(): void // return key to store and mark settled — call on auth failure
395
- }
396
- ```
397
-
398
- `commit()` and `rollback()` are mutually exclusive; calling either a second time (or calling the other after settling) throws `Error: 'SkippedKeyStore: handle already settled'`. Accessing `.key` after settlement also throws. This enforces the delete-on-use contract: a key is consumed exactly once, either by committing (decrypt succeeded, key wiped) or rolling back (decrypt failed, key returned to the store for a future legitimate delivery at the same counter).
399
-
400
- ---
401
-
402
-
403
- ## Cross-References
404
-
405
- | Document | Description |
406
- | -------- | ----------- |
407
- | [index](./README.md) | Project Documentation index |
408
- | [architecture](./architecture.md) | architecture overview, module relationships, buffer layouts, and build pipeline |
409
- | [utils](./utils.md) | encoding utilities and `constantTimeEqual` for verifying MACs from `KeyedHash` |
410
- | [serpent](./serpent.md) | Serpent classes implement `Blockcipher`, `Streamcipher`, and `AEAD` |
411
- | [chacha20](./chacha20.md) | `XChaCha20Cipher` is a `CipherSuite` for `SealStream`/`OpenStream`/`Seal`; `Seal` provides one-shot AEAD over any `CipherSuite`; `ChaCha20`/`ChaCha20Poly1305`/`XChaCha20Poly1305` are stateless primitives |
412
- | [sha2](./sha2.md) | SHA-2 classes implement `Hash`; HMAC classes implement `KeyedHash` |
413
- | [sha3](./sha3.md) | SHA-3 classes implement `Hash`; SHAKE classes extend with XOF API |
414
- | [ratchet](./ratchet.md) | ratchet KDF primitives; `MlKemLike`, `RatchetInitResult`, `KemEncapResult`, `KemDecapResult`, `RatchetMessageHeader`, `ResolveHandle` |
415
- | [test-suite](./test-suite.md) | test suite structure and vector corpus |
416
-