@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.
- package/LICENSE +22 -0
- package/README.md +75 -0
- package/_arx.d.ts +144 -0
- package/_arx.js +375 -0
- package/_poly1305.d.ts +72 -0
- package/_poly1305.js +367 -0
- package/_polyval.d.ts +120 -0
- package/_polyval.js +335 -0
- package/aes.d.ts +461 -0
- package/aes.js +1768 -0
- package/chacha.d.ts +248 -0
- package/chacha.js +579 -0
- package/esm/_arx.js +368 -0
- package/esm/_poly1305.js +363 -0
- package/esm/_polyval.js +329 -0
- package/esm/aes.js +1764 -0
- package/esm/chacha.js +574 -0
- package/esm/ff1.js +203 -0
- package/esm/index.js +26 -0
- package/esm/salsa.js +370 -0
- package/esm/utils.js +806 -0
- package/esm/webcrypto.js +166 -0
- package/ff1.d.ts +39 -0
- package/ff1.js +207 -0
- package/index.d.ts +0 -0
- package/index.js +26 -0
- package/package.json +45 -0
- package/salsa.d.ts +114 -0
- package/salsa.js +375 -0
- package/utils.d.ts +716 -0
- package/utils.js +841 -0
- package/webcrypto.d.ts +88 -0
- package/webcrypto.js +169 -0
package/esm/chacha.js
ADDED
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChaCha stream cipher, released
|
|
3
|
+
* in 2008. Developed after Salsa20, ChaCha aims to increase diffusion per round.
|
|
4
|
+
* It was standardized in
|
|
5
|
+
* {@link https://www.rfc-editor.org/rfc/rfc8439 | RFC 8439} and
|
|
6
|
+
* is now used in TLS 1.3.
|
|
7
|
+
*
|
|
8
|
+
* {@link https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha | XChaCha20}
|
|
9
|
+
* extended-nonce variant is also provided. Similar to XSalsa, it's safe to use with
|
|
10
|
+
* randomly-generated nonces.
|
|
11
|
+
*
|
|
12
|
+
* Check out
|
|
13
|
+
* {@link http://cr.yp.to/chacha/chacha-20080128.pdf | PDF},
|
|
14
|
+
* {@link https://en.wikipedia.org/wiki/Salsa20 | wiki}, and
|
|
15
|
+
* {@link https://cr.yp.to/chacha.html | website}.
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
import { createCipher, createPRG, rotl } from './_arx';
|
|
20
|
+
import { poly1305 } from './_poly1305';
|
|
21
|
+
import { abytes, clean, equalBytes, getOutput, swap8IfBE, swap32IfBE, u64Lengths, wrapCipher, } from './utils';
|
|
22
|
+
/**
|
|
23
|
+
* ChaCha core function. It is implemented twice:
|
|
24
|
+
* 1. Simple loop (chachaCore_small, hchacha_small)
|
|
25
|
+
* 2. Unrolled loop (chachaCore, hchacha) - 4x faster, but larger & harder to read
|
|
26
|
+
* The specific implementation is selected in `createCipher` below.
|
|
27
|
+
*/
|
|
28
|
+
/** RFC 8439 §2.1 quarter round on words a, b, c, d. */
|
|
29
|
+
// prettier-ignore
|
|
30
|
+
function chachaQR(x, a, b, c, d) {
|
|
31
|
+
x[a] = (x[a] + x[b]) | 0;
|
|
32
|
+
x[d] = rotl(x[d] ^ x[a], 16);
|
|
33
|
+
x[c] = (x[c] + x[d]) | 0;
|
|
34
|
+
x[b] = rotl(x[b] ^ x[c], 12);
|
|
35
|
+
x[a] = (x[a] + x[b]) | 0;
|
|
36
|
+
x[d] = rotl(x[d] ^ x[a], 8);
|
|
37
|
+
x[c] = (x[c] + x[d]) | 0;
|
|
38
|
+
x[b] = rotl(x[b] ^ x[c], 7);
|
|
39
|
+
}
|
|
40
|
+
/** Repeated ChaCha double rounds; callers are expected to pass an even round count. */
|
|
41
|
+
function chachaRound(x, rounds = 20) {
|
|
42
|
+
for (let r = 0; r < rounds; r += 2) {
|
|
43
|
+
// RFC 8439 §2.3 / §2.3.1 inner_block: four column rounds, then four diagonal rounds.
|
|
44
|
+
chachaQR(x, 0, 4, 8, 12);
|
|
45
|
+
chachaQR(x, 1, 5, 9, 13);
|
|
46
|
+
chachaQR(x, 2, 6, 10, 14);
|
|
47
|
+
chachaQR(x, 3, 7, 11, 15);
|
|
48
|
+
chachaQR(x, 0, 5, 10, 15);
|
|
49
|
+
chachaQR(x, 1, 6, 11, 12);
|
|
50
|
+
chachaQR(x, 2, 7, 8, 13);
|
|
51
|
+
chachaQR(x, 3, 4, 9, 14);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Shared scratch for the auditability-only helper below; only the test-only
|
|
55
|
+
// __TESTS.chachaCore_small hook reaches it, so production exports stay reentrant.
|
|
56
|
+
const ctmp = /* @__PURE__ */ new Uint32Array(16);
|
|
57
|
+
/** Small version of chacha without loop unrolling. Unused, provided for auditability. */
|
|
58
|
+
// prettier-ignore
|
|
59
|
+
function chacha(s, k, i, out, isHChacha = true, rounds = 20) {
|
|
60
|
+
// `i` is either `[counter, nonce0, nonce1, nonce2]` for the ChaCha block
|
|
61
|
+
// function or the full 128-bit nonce prefix for the HChaCha subkey path.
|
|
62
|
+
// Create initial array using common pattern
|
|
63
|
+
const y = Uint32Array.from([
|
|
64
|
+
s[0], s[1], s[2], s[3], // "expa" "nd 3" "2-by" "te k"
|
|
65
|
+
k[0], k[1], k[2], k[3], // Key Key Key Key
|
|
66
|
+
k[4], k[5], k[6], k[7], // Key Key Key Key
|
|
67
|
+
i[0], i[1], i[2], i[3], // Counter Counter Nonce Nonce
|
|
68
|
+
]);
|
|
69
|
+
const x = ctmp;
|
|
70
|
+
x.set(y);
|
|
71
|
+
chachaRound(x, rounds);
|
|
72
|
+
// HChaCha writes words 0..3 and 12..15 after the rounds; the ChaCha
|
|
73
|
+
// block path adds the original state word-by-word.
|
|
74
|
+
if (isHChacha) {
|
|
75
|
+
const xindexes = [0, 1, 2, 3, 12, 13, 14, 15];
|
|
76
|
+
for (let i = 0; i < 8; i++)
|
|
77
|
+
out[i] = x[xindexes[i]];
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
for (let i = 0; i < 16; i++)
|
|
81
|
+
out[i] = (y[i] + x[i]) | 0;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Identical to `chachaCore`. Reached only through the test-only `__TESTS` export. */
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
const chachaCore_small = (s, k, n, out, cnt, rounds) =>
|
|
87
|
+
// Keep the reference wrapper on the same [counter, nonce0, nonce1, nonce2] layout as chacha().
|
|
88
|
+
chacha(s, k, Uint32Array.from([cnt, n[0], n[1], n[2]]), out, false, rounds);
|
|
89
|
+
/** Identical to `hchacha`. Unused. */
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
const hchacha_small = chacha;
|
|
92
|
+
/** RFC 8439 §2.3 block core for `state = constants | key | counter | nonce`. */
|
|
93
|
+
// prettier-ignore
|
|
94
|
+
function chachaCore(s, k, n, out, cnt, rounds = 20) {
|
|
95
|
+
let y00 = s[0], y01 = s[1], y02 = s[2], y03 = s[3], // "expa" "nd 3" "2-by" "te k"
|
|
96
|
+
y04 = k[0], y05 = k[1], y06 = k[2], y07 = k[3], // Key Key Key Key
|
|
97
|
+
y08 = k[4], y09 = k[5], y10 = k[6], y11 = k[7], // Key Key Key Key
|
|
98
|
+
y12 = cnt, y13 = n[0], y14 = n[1], y15 = n[2]; // Counter Nonce Nonce Nonce
|
|
99
|
+
// Save state to temporary variables
|
|
100
|
+
let x00 = y00, x01 = y01, x02 = y02, x03 = y03, x04 = y04, x05 = y05, x06 = y06, x07 = y07, x08 = y08, x09 = y09, x10 = y10, x11 = y11, x12 = y12, x13 = y13, x14 = y14, x15 = y15;
|
|
101
|
+
for (let r = 0; r < rounds; r += 2) {
|
|
102
|
+
x00 = (x00 + x04) | 0;
|
|
103
|
+
x12 = rotl(x12 ^ x00, 16);
|
|
104
|
+
x08 = (x08 + x12) | 0;
|
|
105
|
+
x04 = rotl(x04 ^ x08, 12);
|
|
106
|
+
x00 = (x00 + x04) | 0;
|
|
107
|
+
x12 = rotl(x12 ^ x00, 8);
|
|
108
|
+
x08 = (x08 + x12) | 0;
|
|
109
|
+
x04 = rotl(x04 ^ x08, 7);
|
|
110
|
+
x01 = (x01 + x05) | 0;
|
|
111
|
+
x13 = rotl(x13 ^ x01, 16);
|
|
112
|
+
x09 = (x09 + x13) | 0;
|
|
113
|
+
x05 = rotl(x05 ^ x09, 12);
|
|
114
|
+
x01 = (x01 + x05) | 0;
|
|
115
|
+
x13 = rotl(x13 ^ x01, 8);
|
|
116
|
+
x09 = (x09 + x13) | 0;
|
|
117
|
+
x05 = rotl(x05 ^ x09, 7);
|
|
118
|
+
x02 = (x02 + x06) | 0;
|
|
119
|
+
x14 = rotl(x14 ^ x02, 16);
|
|
120
|
+
x10 = (x10 + x14) | 0;
|
|
121
|
+
x06 = rotl(x06 ^ x10, 12);
|
|
122
|
+
x02 = (x02 + x06) | 0;
|
|
123
|
+
x14 = rotl(x14 ^ x02, 8);
|
|
124
|
+
x10 = (x10 + x14) | 0;
|
|
125
|
+
x06 = rotl(x06 ^ x10, 7);
|
|
126
|
+
x03 = (x03 + x07) | 0;
|
|
127
|
+
x15 = rotl(x15 ^ x03, 16);
|
|
128
|
+
x11 = (x11 + x15) | 0;
|
|
129
|
+
x07 = rotl(x07 ^ x11, 12);
|
|
130
|
+
x03 = (x03 + x07) | 0;
|
|
131
|
+
x15 = rotl(x15 ^ x03, 8);
|
|
132
|
+
x11 = (x11 + x15) | 0;
|
|
133
|
+
x07 = rotl(x07 ^ x11, 7);
|
|
134
|
+
x00 = (x00 + x05) | 0;
|
|
135
|
+
x15 = rotl(x15 ^ x00, 16);
|
|
136
|
+
x10 = (x10 + x15) | 0;
|
|
137
|
+
x05 = rotl(x05 ^ x10, 12);
|
|
138
|
+
x00 = (x00 + x05) | 0;
|
|
139
|
+
x15 = rotl(x15 ^ x00, 8);
|
|
140
|
+
x10 = (x10 + x15) | 0;
|
|
141
|
+
x05 = rotl(x05 ^ x10, 7);
|
|
142
|
+
x01 = (x01 + x06) | 0;
|
|
143
|
+
x12 = rotl(x12 ^ x01, 16);
|
|
144
|
+
x11 = (x11 + x12) | 0;
|
|
145
|
+
x06 = rotl(x06 ^ x11, 12);
|
|
146
|
+
x01 = (x01 + x06) | 0;
|
|
147
|
+
x12 = rotl(x12 ^ x01, 8);
|
|
148
|
+
x11 = (x11 + x12) | 0;
|
|
149
|
+
x06 = rotl(x06 ^ x11, 7);
|
|
150
|
+
x02 = (x02 + x07) | 0;
|
|
151
|
+
x13 = rotl(x13 ^ x02, 16);
|
|
152
|
+
x08 = (x08 + x13) | 0;
|
|
153
|
+
x07 = rotl(x07 ^ x08, 12);
|
|
154
|
+
x02 = (x02 + x07) | 0;
|
|
155
|
+
x13 = rotl(x13 ^ x02, 8);
|
|
156
|
+
x08 = (x08 + x13) | 0;
|
|
157
|
+
x07 = rotl(x07 ^ x08, 7);
|
|
158
|
+
x03 = (x03 + x04) | 0;
|
|
159
|
+
x14 = rotl(x14 ^ x03, 16);
|
|
160
|
+
x09 = (x09 + x14) | 0;
|
|
161
|
+
x04 = rotl(x04 ^ x09, 12);
|
|
162
|
+
x03 = (x03 + x04) | 0;
|
|
163
|
+
x14 = rotl(x14 ^ x03, 8);
|
|
164
|
+
x09 = (x09 + x14) | 0;
|
|
165
|
+
x04 = rotl(x04 ^ x09, 7);
|
|
166
|
+
}
|
|
167
|
+
// RFC 8439 §2.3 / §2.3.1: add the original state words back in state order.
|
|
168
|
+
let oi = 0;
|
|
169
|
+
out[oi++] = (y00 + x00) | 0;
|
|
170
|
+
out[oi++] = (y01 + x01) | 0;
|
|
171
|
+
out[oi++] = (y02 + x02) | 0;
|
|
172
|
+
out[oi++] = (y03 + x03) | 0;
|
|
173
|
+
out[oi++] = (y04 + x04) | 0;
|
|
174
|
+
out[oi++] = (y05 + x05) | 0;
|
|
175
|
+
out[oi++] = (y06 + x06) | 0;
|
|
176
|
+
out[oi++] = (y07 + x07) | 0;
|
|
177
|
+
out[oi++] = (y08 + x08) | 0;
|
|
178
|
+
out[oi++] = (y09 + x09) | 0;
|
|
179
|
+
out[oi++] = (y10 + x10) | 0;
|
|
180
|
+
out[oi++] = (y11 + x11) | 0;
|
|
181
|
+
out[oi++] = (y12 + x12) | 0;
|
|
182
|
+
out[oi++] = (y13 + x13) | 0;
|
|
183
|
+
out[oi++] = (y14 + x14) | 0;
|
|
184
|
+
out[oi++] = (y15 + x15) | 0;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* hchacha hashes key and nonce into key' and nonce' for xchacha20.
|
|
188
|
+
* Algorithmically identical to `hchacha_small`, but this exported path
|
|
189
|
+
* normalizes word order on big-endian hosts.
|
|
190
|
+
* Need to find a way to merge it with `chachaCore` without 25% performance hit.
|
|
191
|
+
* @param s - Sigma constants as 32-bit words.
|
|
192
|
+
* @param k - Key words.
|
|
193
|
+
* @param i - Nonce-prefix words.
|
|
194
|
+
* @param out - Output buffer for the derived subkey.
|
|
195
|
+
* @example
|
|
196
|
+
* Derives the XChaCha subkey from sigma, key, and nonce-prefix words.
|
|
197
|
+
*
|
|
198
|
+
* ```ts
|
|
199
|
+
* const sigma = new Uint32Array(4);
|
|
200
|
+
* const key = new Uint32Array(8);
|
|
201
|
+
* const nonce = new Uint32Array(4);
|
|
202
|
+
* const out = new Uint32Array(8);
|
|
203
|
+
* hchacha(sigma, key, nonce, out);
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
// prettier-ignore
|
|
207
|
+
export function hchacha(s, k, i, out) {
|
|
208
|
+
let x00 = swap8IfBE(s[0]), x01 = swap8IfBE(s[1]), x02 = swap8IfBE(s[2]), x03 = swap8IfBE(s[3]), x04 = swap8IfBE(k[0]), x05 = swap8IfBE(k[1]), x06 = swap8IfBE(k[2]), x07 = swap8IfBE(k[3]), x08 = swap8IfBE(k[4]), x09 = swap8IfBE(k[5]), x10 = swap8IfBE(k[6]), x11 = swap8IfBE(k[7]), x12 = swap8IfBE(i[0]), x13 = swap8IfBE(i[1]), x14 = swap8IfBE(i[2]), x15 = swap8IfBE(i[3]);
|
|
209
|
+
for (let r = 0; r < 20; r += 2) {
|
|
210
|
+
x00 = (x00 + x04) | 0;
|
|
211
|
+
x12 = rotl(x12 ^ x00, 16);
|
|
212
|
+
x08 = (x08 + x12) | 0;
|
|
213
|
+
x04 = rotl(x04 ^ x08, 12);
|
|
214
|
+
x00 = (x00 + x04) | 0;
|
|
215
|
+
x12 = rotl(x12 ^ x00, 8);
|
|
216
|
+
x08 = (x08 + x12) | 0;
|
|
217
|
+
x04 = rotl(x04 ^ x08, 7);
|
|
218
|
+
x01 = (x01 + x05) | 0;
|
|
219
|
+
x13 = rotl(x13 ^ x01, 16);
|
|
220
|
+
x09 = (x09 + x13) | 0;
|
|
221
|
+
x05 = rotl(x05 ^ x09, 12);
|
|
222
|
+
x01 = (x01 + x05) | 0;
|
|
223
|
+
x13 = rotl(x13 ^ x01, 8);
|
|
224
|
+
x09 = (x09 + x13) | 0;
|
|
225
|
+
x05 = rotl(x05 ^ x09, 7);
|
|
226
|
+
x02 = (x02 + x06) | 0;
|
|
227
|
+
x14 = rotl(x14 ^ x02, 16);
|
|
228
|
+
x10 = (x10 + x14) | 0;
|
|
229
|
+
x06 = rotl(x06 ^ x10, 12);
|
|
230
|
+
x02 = (x02 + x06) | 0;
|
|
231
|
+
x14 = rotl(x14 ^ x02, 8);
|
|
232
|
+
x10 = (x10 + x14) | 0;
|
|
233
|
+
x06 = rotl(x06 ^ x10, 7);
|
|
234
|
+
x03 = (x03 + x07) | 0;
|
|
235
|
+
x15 = rotl(x15 ^ x03, 16);
|
|
236
|
+
x11 = (x11 + x15) | 0;
|
|
237
|
+
x07 = rotl(x07 ^ x11, 12);
|
|
238
|
+
x03 = (x03 + x07) | 0;
|
|
239
|
+
x15 = rotl(x15 ^ x03, 8);
|
|
240
|
+
x11 = (x11 + x15) | 0;
|
|
241
|
+
x07 = rotl(x07 ^ x11, 7);
|
|
242
|
+
x00 = (x00 + x05) | 0;
|
|
243
|
+
x15 = rotl(x15 ^ x00, 16);
|
|
244
|
+
x10 = (x10 + x15) | 0;
|
|
245
|
+
x05 = rotl(x05 ^ x10, 12);
|
|
246
|
+
x00 = (x00 + x05) | 0;
|
|
247
|
+
x15 = rotl(x15 ^ x00, 8);
|
|
248
|
+
x10 = (x10 + x15) | 0;
|
|
249
|
+
x05 = rotl(x05 ^ x10, 7);
|
|
250
|
+
x01 = (x01 + x06) | 0;
|
|
251
|
+
x12 = rotl(x12 ^ x01, 16);
|
|
252
|
+
x11 = (x11 + x12) | 0;
|
|
253
|
+
x06 = rotl(x06 ^ x11, 12);
|
|
254
|
+
x01 = (x01 + x06) | 0;
|
|
255
|
+
x12 = rotl(x12 ^ x01, 8);
|
|
256
|
+
x11 = (x11 + x12) | 0;
|
|
257
|
+
x06 = rotl(x06 ^ x11, 7);
|
|
258
|
+
x02 = (x02 + x07) | 0;
|
|
259
|
+
x13 = rotl(x13 ^ x02, 16);
|
|
260
|
+
x08 = (x08 + x13) | 0;
|
|
261
|
+
x07 = rotl(x07 ^ x08, 12);
|
|
262
|
+
x02 = (x02 + x07) | 0;
|
|
263
|
+
x13 = rotl(x13 ^ x02, 8);
|
|
264
|
+
x08 = (x08 + x13) | 0;
|
|
265
|
+
x07 = rotl(x07 ^ x08, 7);
|
|
266
|
+
x03 = (x03 + x04) | 0;
|
|
267
|
+
x14 = rotl(x14 ^ x03, 16);
|
|
268
|
+
x09 = (x09 + x14) | 0;
|
|
269
|
+
x04 = rotl(x04 ^ x09, 12);
|
|
270
|
+
x03 = (x03 + x04) | 0;
|
|
271
|
+
x14 = rotl(x14 ^ x03, 8);
|
|
272
|
+
x09 = (x09 + x14) | 0;
|
|
273
|
+
x04 = rotl(x04 ^ x09, 7);
|
|
274
|
+
}
|
|
275
|
+
// HChaCha derives the subkey from state words 0..3 and 12..15 after 20 rounds.
|
|
276
|
+
let oi = 0;
|
|
277
|
+
out[oi++] = x00;
|
|
278
|
+
out[oi++] = x01;
|
|
279
|
+
out[oi++] = x02;
|
|
280
|
+
out[oi++] = x03;
|
|
281
|
+
out[oi++] = x12;
|
|
282
|
+
out[oi++] = x13;
|
|
283
|
+
out[oi++] = x14;
|
|
284
|
+
out[oi++] = x15;
|
|
285
|
+
swap32IfBE(out);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Original, non-RFC chacha20 from DJB. 8-byte nonce, 8-byte counter.
|
|
289
|
+
* The nonce/counter layout still reserves 8 counter bytes internally, but the shared public
|
|
290
|
+
* `counter` argument follows noble's strict non-wrapping 32-bit policy. See `src/_arx.ts`
|
|
291
|
+
* near `MAX_COUNTER` for the full counter-policy rationale.
|
|
292
|
+
* @param key - 16-byte or 32-byte key.
|
|
293
|
+
* @param nonce - 8-byte nonce.
|
|
294
|
+
* @param data - Input bytes to xor with the keystream.
|
|
295
|
+
* @param output - Optional destination buffer.
|
|
296
|
+
* @param counter - Initial block counter.
|
|
297
|
+
* @returns Encrypted or decrypted bytes.
|
|
298
|
+
* @example
|
|
299
|
+
* Encrypts bytes with the original 8-byte-nonce ChaCha variant and a fresh key/nonce.
|
|
300
|
+
*
|
|
301
|
+
* ```ts
|
|
302
|
+
* import { chacha20orig } from './chacha';
|
|
303
|
+
* import { randomBytes } from './utils';
|
|
304
|
+
* const key = randomBytes(32);
|
|
305
|
+
* const nonce = randomBytes(8);
|
|
306
|
+
* chacha20orig(key, nonce, new Uint8Array(4));
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
export const chacha20orig = /* @__PURE__ */ createCipher(chachaCore, {
|
|
310
|
+
counterRight: false,
|
|
311
|
+
counterLength: 8,
|
|
312
|
+
allowShortKeys: true,
|
|
313
|
+
});
|
|
314
|
+
/**
|
|
315
|
+
* ChaCha stream cipher. Conforms to RFC 8439 (IETF, TLS). 12-byte nonce, 4-byte counter.
|
|
316
|
+
* With smaller nonce, it's not safe to make it random (CSPRNG), due to collision chance.
|
|
317
|
+
* @param key - 32-byte key.
|
|
318
|
+
* @param nonce - 12-byte nonce.
|
|
319
|
+
* @param data - Input bytes to xor with the keystream.
|
|
320
|
+
* @param output - Optional destination buffer.
|
|
321
|
+
* @param counter - Initial block counter.
|
|
322
|
+
* @returns Encrypted or decrypted bytes.
|
|
323
|
+
* @example
|
|
324
|
+
* Encrypts bytes with the RFC 8439 ChaCha20 stream cipher and a fresh key/nonce.
|
|
325
|
+
*
|
|
326
|
+
* ```ts
|
|
327
|
+
* import { chacha20 } from './chacha';
|
|
328
|
+
* import { randomBytes } from './utils';
|
|
329
|
+
* const key = randomBytes(32);
|
|
330
|
+
* const nonce = randomBytes(12);
|
|
331
|
+
* chacha20(key, nonce, new Uint8Array(4));
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
export const chacha20 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
335
|
+
counterRight: false,
|
|
336
|
+
counterLength: 4,
|
|
337
|
+
allowShortKeys: false,
|
|
338
|
+
});
|
|
339
|
+
/**
|
|
340
|
+
* XChaCha eXtended-nonce ChaCha. With 24-byte nonce, it's safe to make it random (CSPRNG).
|
|
341
|
+
* See {@link https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha | the IRTF draft}.
|
|
342
|
+
* The nonce/counter layout still reserves 8 counter bytes internally, but the shared public
|
|
343
|
+
* `counter` argument follows noble's strict non-wrapping 32-bit policy. See `src/_arx.ts`
|
|
344
|
+
* near `MAX_COUNTER` for the full counter-policy rationale.
|
|
345
|
+
* @param key - 32-byte key.
|
|
346
|
+
* @param nonce - 24-byte extended nonce.
|
|
347
|
+
* @param data - Input bytes to xor with the keystream.
|
|
348
|
+
* @param output - Optional destination buffer.
|
|
349
|
+
* @param counter - Initial block counter.
|
|
350
|
+
* @returns Encrypted or decrypted bytes.
|
|
351
|
+
* @example
|
|
352
|
+
* Encrypts bytes with XChaCha20 using a fresh key and random 24-byte nonce.
|
|
353
|
+
*
|
|
354
|
+
* ```ts
|
|
355
|
+
* import { xchacha20 } from './chacha';
|
|
356
|
+
* import { randomBytes } from './utils';
|
|
357
|
+
* const key = randomBytes(32);
|
|
358
|
+
* const nonce = randomBytes(24);
|
|
359
|
+
* xchacha20(key, nonce, new Uint8Array(4));
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
export const xchacha20 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
363
|
+
counterRight: false,
|
|
364
|
+
counterLength: 8,
|
|
365
|
+
extendNonceFn: hchacha,
|
|
366
|
+
allowShortKeys: false,
|
|
367
|
+
});
|
|
368
|
+
/**
|
|
369
|
+
* Reduced 8-round chacha, described in original paper.
|
|
370
|
+
* @param key - 32-byte key.
|
|
371
|
+
* @param nonce - 12-byte nonce.
|
|
372
|
+
* @param data - Input bytes to xor with the keystream.
|
|
373
|
+
* @param output - Optional destination buffer.
|
|
374
|
+
* @param counter - Initial block counter.
|
|
375
|
+
* @returns Encrypted or decrypted bytes.
|
|
376
|
+
* @example
|
|
377
|
+
* Uses the reduced 8-round variant for non-critical workloads with a fresh key/nonce.
|
|
378
|
+
*
|
|
379
|
+
* ```ts
|
|
380
|
+
* import { chacha8 } from './chacha';
|
|
381
|
+
* import { randomBytes } from './utils';
|
|
382
|
+
* const key = randomBytes(32);
|
|
383
|
+
* const nonce = randomBytes(12);
|
|
384
|
+
* chacha8(key, nonce, new Uint8Array(4));
|
|
385
|
+
* ```
|
|
386
|
+
*/
|
|
387
|
+
export const chacha8 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
388
|
+
counterRight: false,
|
|
389
|
+
counterLength: 4,
|
|
390
|
+
rounds: 8,
|
|
391
|
+
});
|
|
392
|
+
/**
|
|
393
|
+
* Reduced 12-round chacha, described in original paper.
|
|
394
|
+
* @param key - 32-byte key.
|
|
395
|
+
* @param nonce - 12-byte nonce.
|
|
396
|
+
* @param data - Input bytes to xor with the keystream.
|
|
397
|
+
* @param output - Optional destination buffer.
|
|
398
|
+
* @param counter - Initial block counter.
|
|
399
|
+
* @returns Encrypted or decrypted bytes.
|
|
400
|
+
* @example
|
|
401
|
+
* Uses the reduced 12-round variant for non-critical workloads with a fresh key/nonce.
|
|
402
|
+
*
|
|
403
|
+
* ```ts
|
|
404
|
+
* import { chacha12 } from './chacha';
|
|
405
|
+
* import { randomBytes } from './utils';
|
|
406
|
+
* const key = randomBytes(32);
|
|
407
|
+
* const nonce = randomBytes(12);
|
|
408
|
+
* chacha12(key, nonce, new Uint8Array(4));
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
411
|
+
export const chacha12 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
412
|
+
counterRight: false,
|
|
413
|
+
counterLength: 4,
|
|
414
|
+
rounds: 12,
|
|
415
|
+
});
|
|
416
|
+
// Test-only hooks for keeping the simple/reference core aligned with the unrolled production core.
|
|
417
|
+
export const __TESTS = /* @__PURE__ */ Object.freeze({ chachaCore_small, chachaCore });
|
|
418
|
+
// RFC 8439 §2.8.1 pad16(x): shared zero block for AAD/ciphertext padding.
|
|
419
|
+
const ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
|
|
420
|
+
// RFC 8439 §2.8 / §2.8.1: aligned inputs add nothing, otherwise append 16-(len%16) zero bytes.
|
|
421
|
+
const updatePadded = (h, msg) => {
|
|
422
|
+
h.update(msg);
|
|
423
|
+
const leftover = msg.length % 16;
|
|
424
|
+
if (leftover)
|
|
425
|
+
h.update(ZEROS16.subarray(leftover));
|
|
426
|
+
};
|
|
427
|
+
// RFC 8439 §2.6.1 poly1305_key_gen returns `block[0..31]`, so AEAD key
|
|
428
|
+
// generation only needs 32 zero bytes.
|
|
429
|
+
const ZEROS32 = /* @__PURE__ */ new Uint8Array(32);
|
|
430
|
+
function computeTag(fn, key, nonce, ciphertext, AAD) {
|
|
431
|
+
if (AAD !== undefined)
|
|
432
|
+
abytes(AAD, undefined, 'AAD');
|
|
433
|
+
// RFC 8439 §2.6 / §2.8: derive the Poly1305 one-time key from counter 0,
|
|
434
|
+
// then MAC AAD || pad16(AAD) || ciphertext || pad16(ciphertext) || len(AAD) || len(ciphertext).
|
|
435
|
+
const authKey = fn(key, nonce, ZEROS32);
|
|
436
|
+
const lengths = u64Lengths(ciphertext.length, AAD ? AAD.length : 0, true);
|
|
437
|
+
// Methods below can be replaced with
|
|
438
|
+
// return poly1305_computeTag_small(authKey, lengths, ciphertext, AAD)
|
|
439
|
+
const h = poly1305.create(authKey);
|
|
440
|
+
if (AAD)
|
|
441
|
+
updatePadded(h, AAD);
|
|
442
|
+
updatePadded(h, ciphertext);
|
|
443
|
+
h.update(lengths);
|
|
444
|
+
const res = h.digest();
|
|
445
|
+
clean(authKey, lengths);
|
|
446
|
+
return res;
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* AEAD algorithm from RFC 8439.
|
|
450
|
+
* Salsa20 and chacha (RFC 8439) use poly1305 differently.
|
|
451
|
+
* We could have composed them, but it's hard because of authKey:
|
|
452
|
+
* In salsa20, authKey changes position in salsa stream.
|
|
453
|
+
* In chacha, authKey can't be computed inside computeTag, it modifies the counter.
|
|
454
|
+
*/
|
|
455
|
+
export const _poly1305_aead = (xorStream) => (key, nonce, AAD) => {
|
|
456
|
+
// This borrows caller key/nonce/AAD buffers by reference; mutating them after construction
|
|
457
|
+
// changes future encrypt/decrypt results.
|
|
458
|
+
const tagLength = 16;
|
|
459
|
+
return {
|
|
460
|
+
encrypt(plaintext, output) {
|
|
461
|
+
const plength = plaintext.length;
|
|
462
|
+
output = getOutput(plength + tagLength, output, false);
|
|
463
|
+
output.set(plaintext);
|
|
464
|
+
const oPlain = output.subarray(0, -tagLength);
|
|
465
|
+
// RFC 8439 §2.8: payload encryption starts at counter 1 because counter 0 produced the OTK.
|
|
466
|
+
xorStream(key, nonce, oPlain, oPlain, 1);
|
|
467
|
+
const tag = computeTag(xorStream, key, nonce, oPlain, AAD);
|
|
468
|
+
output.set(tag, plength); // append tag
|
|
469
|
+
clean(tag);
|
|
470
|
+
return output;
|
|
471
|
+
},
|
|
472
|
+
decrypt(ciphertext, output) {
|
|
473
|
+
output = getOutput(ciphertext.length - tagLength, output, false);
|
|
474
|
+
const data = ciphertext.subarray(0, -tagLength);
|
|
475
|
+
const passedTag = ciphertext.subarray(-tagLength);
|
|
476
|
+
const tag = computeTag(xorStream, key, nonce, data, AAD);
|
|
477
|
+
// RFC 8439 §2.8 / §4: authenticate ciphertext before decrypting it, and compare tags with
|
|
478
|
+
// the constant-time equalBytes() helper rather than decrypting speculative plaintext first.
|
|
479
|
+
if (!equalBytes(passedTag, tag)) {
|
|
480
|
+
clean(tag);
|
|
481
|
+
throw new Error('invalid tag');
|
|
482
|
+
}
|
|
483
|
+
output.set(ciphertext.subarray(0, -tagLength));
|
|
484
|
+
// Actual decryption
|
|
485
|
+
xorStream(key, nonce, output, output, 1); // start stream with i=1
|
|
486
|
+
clean(tag);
|
|
487
|
+
return output;
|
|
488
|
+
},
|
|
489
|
+
};
|
|
490
|
+
};
|
|
491
|
+
/**
|
|
492
|
+
* ChaCha20-Poly1305 from RFC 8439.
|
|
493
|
+
*
|
|
494
|
+
* Unsafe to use random nonces under the same key, due to collision chance.
|
|
495
|
+
* Prefer XChaCha instead.
|
|
496
|
+
* @param key - 32-byte key.
|
|
497
|
+
* @param nonce - 12-byte nonce.
|
|
498
|
+
* @param AAD - Additional authenticated data.
|
|
499
|
+
* @returns AEAD cipher instance.
|
|
500
|
+
* @example
|
|
501
|
+
* Encrypts and authenticates plaintext with a fresh key and nonce.
|
|
502
|
+
*
|
|
503
|
+
* ```ts
|
|
504
|
+
* import { chacha20poly1305 } from './chacha';
|
|
505
|
+
* import { randomBytes } from './utils';
|
|
506
|
+
* const key = randomBytes(32);
|
|
507
|
+
* const nonce = randomBytes(12);
|
|
508
|
+
* const cipher = chacha20poly1305(key, nonce);
|
|
509
|
+
* cipher.encrypt(new Uint8Array([1, 2, 3]));
|
|
510
|
+
* ```
|
|
511
|
+
*/
|
|
512
|
+
export const chacha20poly1305 = /* @__PURE__ */ wrapCipher({ blockSize: 64, nonceLength: 12, tagLength: 16 },
|
|
513
|
+
/* @__PURE__ */ _poly1305_aead(chacha20));
|
|
514
|
+
/**
|
|
515
|
+
* XChaCha20-Poly1305 extended-nonce chacha.
|
|
516
|
+
*
|
|
517
|
+
* Can be safely used with random nonces (CSPRNG).
|
|
518
|
+
* See {@link https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha | the IRTF draft}.
|
|
519
|
+
* @param key - 32-byte key.
|
|
520
|
+
* @param nonce - 24-byte nonce.
|
|
521
|
+
* @param AAD - Additional authenticated data.
|
|
522
|
+
* @returns AEAD cipher instance.
|
|
523
|
+
* @example
|
|
524
|
+
* Encrypts and authenticates plaintext with a fresh key and random 24-byte nonce.
|
|
525
|
+
*
|
|
526
|
+
* ```ts
|
|
527
|
+
* import { xchacha20poly1305 } from './chacha';
|
|
528
|
+
* import { randomBytes } from './utils';
|
|
529
|
+
* const key = randomBytes(32);
|
|
530
|
+
* const nonce = randomBytes(24);
|
|
531
|
+
* const cipher = xchacha20poly1305(key, nonce);
|
|
532
|
+
* cipher.encrypt(new Uint8Array([1, 2, 3]));
|
|
533
|
+
* ```
|
|
534
|
+
*/
|
|
535
|
+
export const xchacha20poly1305 = /* @__PURE__ */ wrapCipher({ blockSize: 64, nonceLength: 24, tagLength: 16 },
|
|
536
|
+
/* @__PURE__ */ _poly1305_aead(xchacha20));
|
|
537
|
+
/**
|
|
538
|
+
* Chacha20 CSPRNG (cryptographically secure pseudorandom number generator).
|
|
539
|
+
* It's best to limit usage to non-production, non-critical cases: for example, test-only.
|
|
540
|
+
* Compatible with libtomcrypt. It does not have a specification, so unclear how secure it is.
|
|
541
|
+
* @param seed - Optional seed bytes mixed into the internal `key || nonce` state. When omitted,
|
|
542
|
+
* only 32 random bytes are mixed into the 40-byte state.
|
|
543
|
+
* @returns Seeded concrete `_XorStreamPRG` instance, including `clone()`.
|
|
544
|
+
* @example
|
|
545
|
+
* Seeds the test-only ChaCha20 DRBG from fresh entropy.
|
|
546
|
+
*
|
|
547
|
+
* ```ts
|
|
548
|
+
* import { rngChacha20 } from './chacha';
|
|
549
|
+
* import { randomBytes } from './utils';
|
|
550
|
+
* const seed = randomBytes(32);
|
|
551
|
+
* const prg = rngChacha20(seed);
|
|
552
|
+
* prg.randomBytes(8);
|
|
553
|
+
* ```
|
|
554
|
+
*/
|
|
555
|
+
export const rngChacha20 = /* @__PURE__ */ createPRG(chacha20orig, 64, 32, 8);
|
|
556
|
+
/**
|
|
557
|
+
* Chacha20/8 CSPRNG (cryptographically secure pseudorandom number generator).
|
|
558
|
+
* It's best to limit usage to non-production, non-critical cases: for example, test-only.
|
|
559
|
+
* Faster than `rngChacha20`.
|
|
560
|
+
* @param seed - Optional seed bytes mixed into the internal `key || nonce` state. When omitted,
|
|
561
|
+
* only 32 random bytes are mixed into the 44-byte state.
|
|
562
|
+
* @returns Seeded concrete `_XorStreamPRG` instance, including `clone()`.
|
|
563
|
+
* @example
|
|
564
|
+
* Seeds the faster test-only ChaCha8 DRBG from fresh entropy.
|
|
565
|
+
*
|
|
566
|
+
* ```ts
|
|
567
|
+
* import { rngChacha8 } from './chacha';
|
|
568
|
+
* import { randomBytes } from './utils';
|
|
569
|
+
* const seed = randomBytes(32);
|
|
570
|
+
* const prg = rngChacha8(seed);
|
|
571
|
+
* prg.randomBytes(8);
|
|
572
|
+
* ```
|
|
573
|
+
*/
|
|
574
|
+
export const rngChacha8 = /* @__PURE__ */ createPRG(chacha8, 64, 32, 12);
|