@decryption/ciphers 0.2.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.
@@ -0,0 +1,166 @@
1
+ /**
2
+ * WebCrypto-based AES gcm/ctr/cbc, `managedNonce` and `randomBytes`.
3
+ * We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
4
+ * @module
5
+ */
6
+ import { abytes, anumber } from './utils';
7
+ function getWebcryptoSubtle() {
8
+ const cr = typeof globalThis !== 'undefined' && globalThis.crypto;
9
+ if (cr && typeof cr.subtle === 'object' && cr.subtle != null)
10
+ return cr.subtle;
11
+ throw new Error('crypto.subtle must be defined');
12
+ }
13
+ /**
14
+ * Internal webcrypto utils. Can be overridden if crypto.subtle is not present,
15
+ * for example in React Native.
16
+ * Raw keys are re-imported on every call; this wrapper intentionally does not
17
+ * cache `CryptoKey` objects between operations.
18
+ */
19
+ export const utils = {
20
+ async encrypt(key, keyParams, cryptParams, plaintext) {
21
+ const cr = getWebcryptoSubtle();
22
+ const iKey = await cr.importKey('raw', key, keyParams, true, ['encrypt']);
23
+ const ciphertext = await cr.encrypt(cryptParams, iKey, plaintext);
24
+ return new Uint8Array(ciphertext);
25
+ },
26
+ async decrypt(key, keyParams, cryptParams, ciphertext) {
27
+ const cr = getWebcryptoSubtle();
28
+ const iKey = await cr.importKey('raw', key, keyParams, true, ['decrypt']);
29
+ const plaintext = await cr.decrypt(cryptParams, iKey, ciphertext);
30
+ return new Uint8Array(plaintext);
31
+ },
32
+ };
33
+ const mode = {
34
+ CBC: 'AES-CBC',
35
+ CTR: 'AES-CTR',
36
+ GCM: 'AES-GCM',
37
+ };
38
+ function getCryptParams(algo, nonce, AAD) {
39
+ if (algo === mode.CBC)
40
+ return { name: mode.CBC, iv: nonce };
41
+ // WebCrypto allows 1..128 counter bits; use the full block to match sync ctr() / Node CTR wrap.
42
+ if (algo === mode.CTR)
43
+ return { name: mode.CTR, counter: nonce, length: 128 };
44
+ if (algo === mode.GCM) {
45
+ // Rely on the backend default tag length (128 bits) instead of setting it explicitly.
46
+ if (AAD)
47
+ return { name: mode.GCM, iv: nonce, additionalData: AAD };
48
+ else
49
+ return { name: mode.GCM, iv: nonce };
50
+ }
51
+ throw new Error('unknown aes block mode');
52
+ }
53
+ function generate(algo, nonceLength) {
54
+ anumber(nonceLength);
55
+ const res = (key, nonce, AAD) => {
56
+ abytes(key);
57
+ abytes(nonce);
58
+ // Reject falsy non-byte AAD locally; otherwise false/0/''/null silently become "no AAD".
59
+ if (AAD !== undefined)
60
+ abytes(AAD, undefined, 'AAD');
61
+ // Exact nonce-length enforcement and WebCrypto-specific AAD normalization are
62
+ // delegated to the backend; locally we only require byte-array inputs here.
63
+ // Keep caller key/nonce/AAD by reference; mutating them after
64
+ // construction changes later operations.
65
+ const keyParams = { name: algo, length: key.length * 8 };
66
+ const cryptParams = getCryptParams(algo, nonce, AAD);
67
+ let consumed = false;
68
+ return {
69
+ // keyLength,
70
+ encrypt(plaintext) {
71
+ abytes(plaintext);
72
+ if (consumed)
73
+ throw new Error('Cannot encrypt() twice with same key / nonce');
74
+ consumed = true;
75
+ return utils.encrypt(key, keyParams, cryptParams, plaintext);
76
+ },
77
+ decrypt(ciphertext) {
78
+ abytes(ciphertext);
79
+ return utils.decrypt(key, keyParams, cryptParams, ciphertext);
80
+ },
81
+ };
82
+ };
83
+ res.nonceLength = nonceLength;
84
+ res.blockSize = 16; // always for AES
85
+ return res;
86
+ }
87
+ /**
88
+ * AES-CBC implemented with WebCrypto.
89
+ * Uses WebCrypto's built-in PKCS padding behavior; exact IV-length checks are
90
+ * delegated to the backend instead of local `abytes(..., 16)` validation.
91
+ * @param key - AES key bytes.
92
+ * @param iv - 16-byte initialization vector.
93
+ * @returns Async cipher instance.
94
+ * @example
95
+ * Encrypts a block with the browser or Node WebCrypto backend.
96
+ *
97
+ * ```ts
98
+ * import { cbc } from './webcrypto';
99
+ * import { randomBytes } from './utils';
100
+ * const key = randomBytes(16);
101
+ * const iv = randomBytes(16);
102
+ * const cipher = cbc(key, iv);
103
+ * await cipher.encrypt(new Uint8Array(16));
104
+ * ```
105
+ */
106
+ export const cbc = /* @__PURE__ */ (() => generate(mode.CBC, 16))();
107
+ /**
108
+ * AES-CTR implemented with WebCrypto.
109
+ * Uses WebCrypto's full 128-bit counter-length setting so the whole
110
+ * 16-byte counter block is incremented, matching sync `aes.ts:ctr`.
111
+ * @param key - AES key bytes.
112
+ * @param nonce - 16-byte counter block incremented as a full big-endian AES counter block.
113
+ * @returns Async cipher instance.
114
+ * @example
115
+ * Encrypts a short payload with WebCrypto AES-CTR.
116
+ *
117
+ * ```ts
118
+ * import { ctr } from './webcrypto';
119
+ * import { randomBytes } from './utils';
120
+ * const key = randomBytes(16);
121
+ * const nonce = randomBytes(16);
122
+ * const cipher = ctr(key, nonce);
123
+ * await cipher.encrypt(new Uint8Array([1, 2, 3]));
124
+ * ```
125
+ */
126
+ export const ctr = /* @__PURE__ */ (() => generate(mode.CTR, 16))();
127
+ /**
128
+ * AES-GCM implemented with WebCrypto.
129
+ * AAD type normalization and nonce-shape enforcement beyond raw bytes are left
130
+ * to the backend WebCrypto implementation.
131
+ * @param key - AES key bytes.
132
+ * @param nonce - Nonce bytes.
133
+ * @param AAD - Additional authenticated data.
134
+ * @returns Async cipher instance.
135
+ * @example
136
+ * Encrypts and authenticates plaintext with WebCrypto AES-GCM.
137
+ *
138
+ * ```ts
139
+ * import { gcm } from './webcrypto';
140
+ * import { randomBytes } from './utils';
141
+ * const key = randomBytes(16);
142
+ * const nonce = randomBytes(12);
143
+ * const cipher = gcm(key, nonce);
144
+ * await cipher.encrypt(new Uint8Array([1, 2, 3]));
145
+ * ```
146
+ */
147
+ export const gcm = /* @__PURE__ */ (() => generate(mode.GCM, 12))();
148
+ // // Type tests
149
+ // import { siv, gcm, ctr, ecb, cbc } from '../aes';
150
+ // import { xsalsa20poly1305 } from '../salsa';
151
+ // import { chacha20poly1305, xchacha20poly1305 } from '../chacha';
152
+ // const wsiv = managedNonce(siv);
153
+ // const wgcm = managedNonce(gcm);
154
+ // const wctr = managedNonce(ctr);
155
+ // const wcbc = managedNonce(cbc);
156
+ // const wsalsapoly = managedNonce(xsalsa20poly1305);
157
+ // const wchacha = managedNonce(chacha20poly1305);
158
+ // const wxchacha = managedNonce(xchacha20poly1305);
159
+ // // should fail
160
+ // const wcbc2 = managedNonce(managedNonce(cbc));
161
+ // const wctr = managedNonce(ctr);
162
+ // import { gcm as gcmSync } from '../aes';
163
+ // const x1 = managedNonce(gcmSync);
164
+ // // const x1: (key: Uint8Array, AAD?: Uint8Array | undefined) => Cipher
165
+ // const x2 = managedNonce(gcm);
166
+ // // const x2: (key: Uint8Array, AAD?: Uint8Array | undefined) => AsyncCipher
package/ff1.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ import { type Cipher, type TArg, type TRet } from './utils';
2
+ /**
3
+ * FPE-FF1 format-preserving encryption.
4
+ * @param radix - Alphabet size for each input digit.
5
+ * @param key - AES key bytes.
6
+ * @param tweak - Optional tweak bytes.
7
+ * @returns Encrypt/decrypt helpers over digit arrays.
8
+ * @example
9
+ * Encrypts decimal digits without changing their format, using a fresh AES key.
10
+ *
11
+ * ```ts
12
+ * import { FF1 } from './ff1';
13
+ * import { randomBytes } from './utils';
14
+ * const key = randomBytes(16);
15
+ * const ff1 = FF1(10, key);
16
+ * ff1.encrypt([1, 2, 3]);
17
+ * ```
18
+ */
19
+ export declare function FF1(radix: number, key: TArg<Uint8Array>, tweak?: TArg<Uint8Array>): {
20
+ encrypt(x: number[]): number[];
21
+ decrypt(x: number[]): number[];
22
+ };
23
+ /**
24
+ * Binary FPE-FF1 wrapper over byte arrays.
25
+ * @param key - AES key bytes.
26
+ * @param tweak - Optional tweak bytes.
27
+ * @returns Encrypt/decrypt helpers over byte arrays.
28
+ * @example
29
+ * Encrypts raw bytes through FF1's binary alphabet wrapper with a fresh AES key.
30
+ *
31
+ * ```ts
32
+ * import { BinaryFF1 } from './ff1';
33
+ * import { randomBytes } from './utils';
34
+ * const key = randomBytes(16);
35
+ * const ff1 = BinaryFF1(key);
36
+ * ff1.encrypt(new Uint8Array([1, 2, 3]));
37
+ * ```
38
+ */
39
+ export declare function BinaryFF1(key: TArg<Uint8Array>, tweak?: TArg<Uint8Array>): TRet<Cipher>;
package/ff1.js ADDED
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FF1 = FF1;
4
+ exports.BinaryFF1 = BinaryFF1;
5
+ /**
6
+ * FPE-FF1 (Format-preserving encryption algorithm) specified in
7
+ * {@link https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38G.pdf | NIST 800-38G}.
8
+ * @module
9
+ */
10
+ const aes_1 = require("./aes");
11
+ const utils_1 = require("./utils");
12
+ // NIST SP 800-38G §4.3 / §5.1 Algorithm 7: FF1's designated CIPH_K here is AES, so this file
13
+ // reuses the reviewed AES key schedule and single-block encryption helpers.
14
+ // NOTE: no point in inlining encrypt instead of encryptBlock, since BigInt stuff will be slow
15
+ const { expandKeyLE, encryptBlock } = aes_1.unsafe;
16
+ // Format-preserving encryption algorithm (FPE-FF1) specified in
17
+ // {@link https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38G.pdf | NIST Special Publication 800-38G}.
18
+ const BLOCK_LEN = 16;
19
+ function mod(a, b) {
20
+ const result = a % b;
21
+ return result >= 0 ? result : b + result;
22
+ }
23
+ function NUMradix(radix, data) {
24
+ let res = BigInt(0);
25
+ for (let i of data)
26
+ res = res * BigInt(radix) + BigInt(i);
27
+ return res;
28
+ }
29
+ function getRound(radix, key, tweak, x) {
30
+ if (radix > 2 ** 16 - 1)
31
+ throw new Error('invalid radix ' + radix);
32
+ // radix**minlen ≥ 100
33
+ const minLen = Math.ceil(Math.log(100) / Math.log(radix));
34
+ const maxLen = 2 ** 32 - 1;
35
+ // 2 ≤ minlen ≤ maxlen < 2**32
36
+ if (2 > minLen || minLen > maxLen || maxLen >= 2 ** 32)
37
+ throw new Error('Invalid radix: 2 ≤ minlen ≤ maxlen < 2**32');
38
+ if (!Array.isArray(x))
39
+ throw new Error('invalid X');
40
+ if (x.length < minLen || x.length > maxLen)
41
+ throw new Error('X is outside minLen..maxLen bounds');
42
+ // SP 800-38G defines FF1 over numeral strings in base `radix`; out-of-range digits must fail
43
+ // before NUMradix(...) or round splitting can reinterpret them as a different numeral string.
44
+ for (const i of x) {
45
+ if (!Number.isSafeInteger(i) || i < 0 || i >= radix)
46
+ throw new Error('invalid X: digit outside radix');
47
+ }
48
+ const u = Math.floor(x.length / 2);
49
+ const v = x.length - u;
50
+ const b = Math.ceil(Math.ceil(v * Math.log2(radix)) / 8);
51
+ const d = 4 * Math.ceil(b / 4) + 4;
52
+ const padding = mod(-tweak.length - b - 1, 16);
53
+ // P = [1]1 || [2]1 || [1]1 || [radix]3 || [10]1 || [u mod 256]1 || [n]4 || [t]4.
54
+ const P = Uint8Array.from([1, 2, 1, 0, 0, 0, 10, u, 0, 0, 0, 0, 0, 0, 0, 0]);
55
+ const view = new DataView(P.buffer);
56
+ // NIST SP 800-38G §5.1 bounds radix <= 2^16, so the 24-bit [radix]3 field is encoded here as
57
+ // 0x00 || uint16_be(radix).
58
+ view.setUint16(4, radix, false);
59
+ view.setUint32(8, x.length, false);
60
+ view.setUint32(12, tweak.length, false);
61
+ // Q = T || [0](−t−b−1) mod 16 || [i]1 || [NUMradix(B)]b.
62
+ const PQ = new Uint8Array(P.length + tweak.length + padding + 1 + b);
63
+ PQ.set(P);
64
+ (0, utils_1.clean)(P);
65
+ PQ.set(tweak, P.length);
66
+ const xk = expandKeyLE(key);
67
+ const round = (A, B, i, decrypt = false) => {
68
+ // Q = ... || [i]1 || [NUMradix(B)]b.
69
+ PQ[PQ.length - b - 1] = i;
70
+ if (b)
71
+ PQ.set((0, utils_1.numberToBytesBE)(NUMradix(radix, B), b), PQ.length - b);
72
+ // NIST SP 800-38G Algorithm 6 PRF: Y_j = CIPH_K(Y_(j-1) xor X_j) starting from Y_0 = 0^128.
73
+ let r = new Uint8Array(16);
74
+ for (let j = 0; j < PQ.length / BLOCK_LEN; j++) {
75
+ for (let i = 0; i < BLOCK_LEN; i++)
76
+ r[i] ^= PQ[j * BLOCK_LEN + i];
77
+ encryptBlock(xk, r);
78
+ }
79
+ // Let S be the first d bytes of the following string of ⎡d/16⎤ blocks:
80
+ // R || CIPHK(R ⊕[1]16) || CIPHK(R ⊕[2]16) ...CIPHK(R ⊕[⎡d / 16⎤ – 1]16).
81
+ let s = Array.from(r);
82
+ for (let j = 1; s.length < d; j++) {
83
+ const block = (0, utils_1.numberToBytesBE)(BigInt(j), 16);
84
+ for (let k = 0; k < BLOCK_LEN; k++)
85
+ block[k] ^= r[k];
86
+ s.push(...Array.from(encryptBlock(xk, block)));
87
+ }
88
+ let y = (0, utils_1.bytesToNumberBE)(Uint8Array.from(s.slice(0, d)));
89
+ s.fill(0);
90
+ if (decrypt)
91
+ y = -y;
92
+ const m = i % 2 === 0 ? u : v;
93
+ let c = mod(NUMradix(radix, A) + y, BigInt(radix) ** BigInt(m));
94
+ // STR(radix, m, c)
95
+ const C = Array(m).fill(0);
96
+ for (let i = 0; i < m; i++, c /= BigInt(radix))
97
+ C[m - 1 - i] = Number(c % BigInt(radix));
98
+ A.fill(0);
99
+ A = B;
100
+ B = C;
101
+ return [A, B];
102
+ };
103
+ const destroy = () => {
104
+ (0, utils_1.clean)(xk, PQ);
105
+ };
106
+ return { u, round, destroy };
107
+ }
108
+ const EMPTY_BUF = /* @__PURE__ */ Uint8Array.of();
109
+ /**
110
+ * FPE-FF1 format-preserving encryption.
111
+ * @param radix - Alphabet size for each input digit.
112
+ * @param key - AES key bytes.
113
+ * @param tweak - Optional tweak bytes.
114
+ * @returns Encrypt/decrypt helpers over digit arrays.
115
+ * @example
116
+ * Encrypts decimal digits without changing their format, using a fresh AES key.
117
+ *
118
+ * ```ts
119
+ * import { FF1 } from './ff1';
120
+ * import { randomBytes } from './utils';
121
+ * const key = randomBytes(16);
122
+ * const ff1 = FF1(10, key);
123
+ * ff1.encrypt([1, 2, 3]);
124
+ * ```
125
+ */
126
+ function FF1(radix, key, tweak = EMPTY_BUF) {
127
+ (0, utils_1.anumber)(radix);
128
+ (0, utils_1.abytes)(key);
129
+ (0, utils_1.abytes)(tweak);
130
+ // This borrows caller key/tweak buffers by reference through the bound closure; mutating them
131
+ // after construction changes later encrypt/decrypt outputs.
132
+ const PQ = getRound.bind(null, radix, key, tweak);
133
+ return {
134
+ encrypt(x) {
135
+ const { u, round, destroy } = PQ(x);
136
+ let [A, B] = [x.slice(0, u), x.slice(u)];
137
+ for (let i = 0; i < 10; i++)
138
+ [A, B] = round(A, B, i);
139
+ destroy();
140
+ const res = A.concat(B);
141
+ A.fill(0);
142
+ B.fill(0);
143
+ return res;
144
+ },
145
+ decrypt(x) {
146
+ const { u, round, destroy } = PQ(x);
147
+ // The FF1.Decrypt algorithm is similar to the FF1.Encrypt algorithm;
148
+ // the differences are in Step 6, where:
149
+ // 1) the order of the indices is reversed,
150
+ // 2) the roles of A and B are swapped
151
+ // 3) modular addition is replaced by modular subtraction, in Step 6vi.
152
+ let [B, A] = [x.slice(0, u), x.slice(u)];
153
+ for (let i = 9; i >= 0; i--)
154
+ [A, B] = round(A, B, i, true);
155
+ destroy();
156
+ const res = B.concat(A);
157
+ A.fill(0);
158
+ B.fill(0);
159
+ return res;
160
+ },
161
+ };
162
+ }
163
+ // Binary wrapper uses little-endian bit order within each byte so bit 0 stays
164
+ // in the first numeral slot for this library-defined byte-array surface.
165
+ const binLE = {
166
+ encode(bytes) {
167
+ const x = [];
168
+ for (let i = 0; i < bytes.length; i++) {
169
+ for (let j = 0, tmp = bytes[i]; j < 8; j++, tmp >>= 1)
170
+ x.push(tmp & 1);
171
+ }
172
+ return x;
173
+ },
174
+ decode(b) {
175
+ if (!Array.isArray(b) || b.length % 8)
176
+ throw new Error('Invalid binary string');
177
+ const res = new Uint8Array(b.length / 8);
178
+ for (let i = 0, j = 0; i < res.length; i++) {
179
+ res[i] = b[j++] | (b[j++] << 1) | (b[j++] << 2) | (b[j++] << 3);
180
+ res[i] |= (b[j++] << 4) | (b[j++] << 5) | (b[j++] << 6) | (b[j++] << 7);
181
+ }
182
+ return res;
183
+ },
184
+ };
185
+ /**
186
+ * Binary FPE-FF1 wrapper over byte arrays.
187
+ * @param key - AES key bytes.
188
+ * @param tweak - Optional tweak bytes.
189
+ * @returns Encrypt/decrypt helpers over byte arrays.
190
+ * @example
191
+ * Encrypts raw bytes through FF1's binary alphabet wrapper with a fresh AES key.
192
+ *
193
+ * ```ts
194
+ * import { BinaryFF1 } from './ff1';
195
+ * import { randomBytes } from './utils';
196
+ * const key = randomBytes(16);
197
+ * const ff1 = BinaryFF1(key);
198
+ * ff1.encrypt(new Uint8Array([1, 2, 3]));
199
+ * ```
200
+ */
201
+ function BinaryFF1(key, tweak = EMPTY_BUF) {
202
+ const ff1 = FF1(2, key, tweak);
203
+ return {
204
+ encrypt: (x) => binLE.decode(ff1.encrypt(binLE.encode(x))),
205
+ decrypt: (x) => binLE.decode(ff1.decrypt(binLE.encode(x))),
206
+ };
207
+ }
package/index.d.ts ADDED
File without changes
package/index.js ADDED
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /**
3
+ * Audited & minimal JS implementation of Salsa20, ChaCha and AES. Check out individual modules.
4
+ * @example
5
+ ```js
6
+ import { gcm, aessiv } from './aes';
7
+ import { xsalsa20poly1305 } from './salsa';
8
+ import { secretbox } from './salsa'; // == xsalsa20poly1305
9
+ import { chacha20poly1305, xchacha20poly1305 } from './chacha';
10
+
11
+ // Unauthenticated encryption: make sure to use HMAC or similar
12
+ import { ctr, cfb, cbc, ecb } from './aes';
13
+ import { salsa20, xsalsa20 } from './salsa';
14
+ import { chacha20, xchacha20, chacha8, chacha12 } from './chacha';
15
+
16
+ // KW
17
+ import { aeskw, aeskwp } from './aes';
18
+
19
+ // Utilities
20
+ import { managedNonce, randomBytes, bytesToHex, hexToBytes } from './utils';
21
+ import { poly1305 } from './_poly1305';
22
+ import { ghash, polyval } from './_polyval';
23
+ ```
24
+ * @module
25
+ */
26
+ throw new Error('root module cannot be imported: import submodules instead. Check out README');
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@decryption/ciphers",
3
+ "version": "0.2.0",
4
+ "description": "Audited & minimal 0-dependency JS implementation of AES, ChaCha20-Poly1305 and XChaCha20-Poly1305. Dual CJS+ESM fork of @noble/ciphers.",
5
+ "author": "Constructive <developers@constructive.io>",
6
+ "main": "index.js",
7
+ "module": "esm/index.js",
8
+ "types": "index.d.ts",
9
+ "homepage": "https://github.com/constructive-io/decryption",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/constructive-io/decryption"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/constructive-io/decryption/issues"
21
+ },
22
+ "scripts": {
23
+ "copy": "makage assets",
24
+ "clean": "makage clean",
25
+ "prepublishOnly": "npm run build",
26
+ "build": "makage build && cp LICENSE dist/LICENSE",
27
+ "lint": "echo \"vendored fork — linted upstream\" && exit 0",
28
+ "test": "jest",
29
+ "test:watch": "jest --watch"
30
+ },
31
+ "keywords": [
32
+ "aes",
33
+ "gcm",
34
+ "chacha",
35
+ "xchacha",
36
+ "poly1305",
37
+ "aead",
38
+ "cipher",
39
+ "encryption"
40
+ ],
41
+ "devDependencies": {
42
+ "makage": "0.3.0"
43
+ },
44
+ "gitHead": "8d85fbfdbb1d57da93e99904a03e6e8d8531d281"
45
+ }
package/salsa.d.ts ADDED
@@ -0,0 +1,114 @@
1
+ import { type ARXCipher, type TArg, type TRet, type XorStream } from './utils';
2
+ /**
3
+ * hsalsa hashes key and nonce-prefix words into the 32-byte subkey used by XSalsa20.
4
+ * Identical to `hsalsa_small`.
5
+ * Need to find a way to merge it with `salsaCore` without 25% performance hit.
6
+ * @param s - Sigma constants as 32-bit words.
7
+ * @param k - Key words.
8
+ * @param i - Nonce-prefix words.
9
+ * @param out - Output buffer for the derived subkey.
10
+ * @example
11
+ * Derives the XSalsa20 subkey from sigma, key, and nonce-prefix words.
12
+ *
13
+ * ```ts
14
+ * const sigma = new Uint32Array(4);
15
+ * const key = new Uint32Array(8);
16
+ * const nonce = new Uint32Array(4);
17
+ * const out = new Uint32Array(8);
18
+ * hsalsa(sigma, key, nonce, out);
19
+ * ```
20
+ */
21
+ export declare function hsalsa(s: TArg<Uint32Array>, k: TArg<Uint32Array>, i: TArg<Uint32Array>, out: TArg<Uint32Array>): void;
22
+ /**
23
+ * Salsa20 from original paper. 8-byte nonce.
24
+ * With smaller nonce, it's not safe to make it random (CSPRNG), due to collision chance.
25
+ * @param key - 16-byte or 32-byte key.
26
+ * @param nonce - 8-byte nonce.
27
+ * @param data - Input bytes to xor with the keystream.
28
+ * @param output - Optional destination buffer.
29
+ * @param counter - Initial block counter.
30
+ * Only the low 32 bits of Salsa20's 64-bit counter state are exposed here;
31
+ * the high word stays zero and the implementation still caps the public
32
+ * value to 32 bits.
33
+ * @returns Encrypted or decrypted bytes.
34
+ * @example
35
+ * Encrypts bytes with the original 8-byte-nonce Salsa20 stream cipher.
36
+ *
37
+ * ```ts
38
+ * import { salsa20 } from './salsa';
39
+ * import { randomBytes } from './utils';
40
+ * const key = randomBytes(32);
41
+ * const nonce = randomBytes(8);
42
+ * salsa20(key, nonce, new Uint8Array([1, 2, 3, 4]));
43
+ * ```
44
+ */
45
+ export declare const salsa20: TRet<XorStream>;
46
+ /**
47
+ * XSalsa20 extended-nonce salsa.
48
+ * With 24-byte nonce, it's safe to make it random (CSPRNG).
49
+ * @param key - 32-byte key.
50
+ * This XSalsa20 wrapper does not enable Salsa20's 16-byte legacy key mode.
51
+ * @param nonce - 24-byte nonce.
52
+ * @param data - Input bytes to xor with the keystream.
53
+ * @param output - Optional destination buffer.
54
+ * @param counter - Initial block counter.
55
+ * @returns Encrypted or decrypted bytes.
56
+ * @example
57
+ * Encrypts bytes with XSalsa20 and a random 24-byte nonce.
58
+ *
59
+ * ```ts
60
+ * import { xsalsa20 } from './salsa';
61
+ * import { randomBytes } from './utils';
62
+ * const key = randomBytes(32);
63
+ * const nonce = randomBytes(24);
64
+ * xsalsa20(key, nonce, new Uint8Array([1, 2, 3, 4]));
65
+ * ```
66
+ */
67
+ export declare const xsalsa20: TRet<XorStream>;
68
+ /**
69
+ * xsalsa20-poly1305 eXtended-nonce (24 bytes) salsa.
70
+ * With 24-byte nonce, it's safe to make it random (CSPRNG).
71
+ * Also known as `secretbox` from libsodium / nacl.
72
+ * No AAD input is supported here. Caller-provided `output` buffers for
73
+ * `encrypt()` / `decrypt()` must be `input.length + 32` bytes because the
74
+ * implementation uses a 32-byte leading scratch area before returning `+16`.
75
+ * @param key - 32-byte key.
76
+ * @param nonce - 24-byte nonce.
77
+ * @param AAD - Must be omitted; XSalsa20-Poly1305 secretbox does not support associated data.
78
+ * @returns AEAD cipher instance.
79
+ * @example
80
+ * Encrypts and authenticates plaintext with XSalsa20-Poly1305.
81
+ *
82
+ * ```ts
83
+ * import { xsalsa20poly1305 } from './salsa';
84
+ * import { randomBytes } from './utils';
85
+ * const key = randomBytes(32);
86
+ * const nonce = randomBytes(24);
87
+ * const cipher = xsalsa20poly1305(key, nonce);
88
+ * cipher.encrypt(new Uint8Array([1, 2, 3]));
89
+ * ```
90
+ */
91
+ export declare const xsalsa20poly1305: TRet<ARXCipher>;
92
+ /**
93
+ * Alias to `xsalsa20poly1305`, for compatibility with libsodium / nacl.
94
+ * Check out {@link https://github.com/serenity-kit/noble-sodium | noble-sodium}
95
+ * for `crypto_box`.
96
+ * @param key - 32-byte key.
97
+ * @param nonce - 24-byte nonce.
98
+ * @returns Wrapper with `seal()` and `open()` helpers.
99
+ * @example
100
+ * Uses the libsodium-style `seal()` and `open()` wrapper.
101
+ *
102
+ * ```ts
103
+ * import { secretbox } from './salsa';
104
+ * import { randomBytes } from './utils';
105
+ * const key = randomBytes(32);
106
+ * const nonce = randomBytes(24);
107
+ * const box = secretbox(key, nonce);
108
+ * box.seal(new Uint8Array([1, 2, 3]));
109
+ * ```
110
+ */
111
+ export declare function secretbox(key: TArg<Uint8Array>, nonce: TArg<Uint8Array>): TRet<{
112
+ seal: (plaintext: TArg<Uint8Array>, output?: TArg<Uint8Array>) => TRet<Uint8Array>;
113
+ open: (ciphertext: TArg<Uint8Array>, output?: TArg<Uint8Array>) => TRet<Uint8Array>;
114
+ }>;