@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/salsa.js
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.xsalsa20poly1305 = exports.xsalsa20 = exports.salsa20 = void 0;
|
|
4
|
+
exports.hsalsa = hsalsa;
|
|
5
|
+
exports.secretbox = secretbox;
|
|
6
|
+
/**
|
|
7
|
+
* Salsa20 stream cipher, released in 2005.
|
|
8
|
+
* Salsa's goal was to implement AES replacement that does not rely on S-Boxes,
|
|
9
|
+
* which are hard to implement in a constant-time manner.
|
|
10
|
+
* Salsa20 is usually faster than AES, a big deal on slow, budget mobile phones.
|
|
11
|
+
*
|
|
12
|
+
* - {@link https://cr.yp.to/snuffle/xsalsa-20110204.pdf | XSalsa20},
|
|
13
|
+
* extended-nonce
|
|
14
|
+
* variant was released in 2008. It extends Salsa20's 64-bit nonce to 192 bits,
|
|
15
|
+
* and became safe to be picked at random.
|
|
16
|
+
* - Nacl / Libsodium popularized term "secretbox", - which is just xsalsa20poly1305.
|
|
17
|
+
* We provide the alias and corresponding seal / open methods.
|
|
18
|
+
* "crypto_box" and "sealedbox" are available in package
|
|
19
|
+
* {@link https://github.com/serenity-kit/noble-sodium | noble-sodium}.
|
|
20
|
+
* - Check out
|
|
21
|
+
* {@link https://cr.yp.to/snuffle/salsafamily-20071225.pdf | PDF}
|
|
22
|
+
* and {@link https://cr.yp.to/snuffle.html | website}.
|
|
23
|
+
* @module
|
|
24
|
+
*/
|
|
25
|
+
const _arx_1 = require("./_arx");
|
|
26
|
+
const _poly1305_1 = require("./_poly1305");
|
|
27
|
+
const utils_1 = require("./utils");
|
|
28
|
+
/**
|
|
29
|
+
* Salsa20 core function. It is implemented twice:
|
|
30
|
+
* 1. Simple loop (salsaCore_small, hsalsa_small)
|
|
31
|
+
* 2. Unrolled loop (salsaCore, hsalsa) - 4x faster, but larger & harder to read
|
|
32
|
+
* The specific implementation is selected in `createCipher` below.
|
|
33
|
+
* Performance numbers for 1MB inputs:
|
|
34
|
+
* * default x 779 ops/sec @ 1ms/op
|
|
35
|
+
* * if salsa+hsalsa are merged x 459 ops/sec @ 2ms/op
|
|
36
|
+
* * small x 132 ops/sec @ 7ms/op
|
|
37
|
+
*/
|
|
38
|
+
/** RFC 7914 §3 Salsa20/8 core quarter-round on words a, b, c, d. */
|
|
39
|
+
function salsaQR(x, a, b, c, d) {
|
|
40
|
+
x[b] ^= (0, _arx_1.rotl)((x[a] + x[d]) | 0, 7);
|
|
41
|
+
x[c] ^= (0, _arx_1.rotl)((x[b] + x[a]) | 0, 9);
|
|
42
|
+
x[d] ^= (0, _arx_1.rotl)((x[c] + x[b]) | 0, 13);
|
|
43
|
+
x[a] ^= (0, _arx_1.rotl)((x[d] + x[c]) | 0, 18);
|
|
44
|
+
}
|
|
45
|
+
/** RFC 7914 §3 double-round schedule: four column rounds, then four row rounds. */
|
|
46
|
+
function salsaRound(x, rounds = 20) {
|
|
47
|
+
for (let r = 0; r < rounds; r += 2) {
|
|
48
|
+
salsaQR(x, 0, 4, 8, 12);
|
|
49
|
+
salsaQR(x, 5, 9, 13, 1);
|
|
50
|
+
salsaQR(x, 10, 14, 2, 6);
|
|
51
|
+
salsaQR(x, 15, 3, 7, 11);
|
|
52
|
+
salsaQR(x, 0, 1, 2, 3);
|
|
53
|
+
salsaQR(x, 5, 6, 7, 4);
|
|
54
|
+
salsaQR(x, 10, 11, 8, 9);
|
|
55
|
+
salsaQR(x, 15, 12, 13, 14);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// Shared scratch for the unused auditability helper below; it would be
|
|
59
|
+
// non-reentrant under overlapping/nested calls, but current code doesn't invoke it.
|
|
60
|
+
const stmp = /* @__PURE__ */ new Uint32Array(16);
|
|
61
|
+
/** Small version of salsa without loop unrolling. Unused, provided for auditability. */
|
|
62
|
+
// prettier-ignore
|
|
63
|
+
function salsa(s, k, i, out, isHSalsa = true, rounds = 20) {
|
|
64
|
+
// Create initial array using common pattern
|
|
65
|
+
const y = Uint32Array.from([
|
|
66
|
+
s[0], k[0], k[1], k[2], // "expa" Key Key Key
|
|
67
|
+
k[3], s[1], i[0], i[1], // Key "nd 3" Nonce Nonce
|
|
68
|
+
i[2], i[3], s[2], k[4], // Pos. Pos. "2-by" Key
|
|
69
|
+
k[5], k[6], k[7], s[3], // Key Key Key "te k"
|
|
70
|
+
]);
|
|
71
|
+
const x = stmp;
|
|
72
|
+
x.set(y);
|
|
73
|
+
// const x = y.slice();
|
|
74
|
+
salsaRound(x, rounds);
|
|
75
|
+
// hsalsa extracts 8 specific words for the 32-byte subkey; salsa adds the original state.
|
|
76
|
+
if (isHSalsa) {
|
|
77
|
+
const xindexes = [0, 5, 10, 15, 6, 7, 8, 9];
|
|
78
|
+
for (let i = 0; i < 8; i++)
|
|
79
|
+
out[i] = x[xindexes[i]];
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
for (let i = 0; i < 16; i++)
|
|
83
|
+
out[i] = (y[i] + x[i]) | 0;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Identical to `salsaCore`. Unused. */
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
const salsaCore_small = (s, k, n, out, cnt, rounds) => salsa(s, k, Uint32Array.from([n[0], n[1], cnt, 0]), out, false, rounds);
|
|
89
|
+
/** Identical to `hsalsa`. Unused. */
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
const hsalsa_small = salsa;
|
|
92
|
+
/** Identical to `salsaCore_small`. Uses only the low 32 bits of Salsa20's 64-bit counter state. */
|
|
93
|
+
// prettier-ignore
|
|
94
|
+
function salsaCore(s, k, n, out, cnt, rounds = 20) {
|
|
95
|
+
// Public wrappers expose only the low 32 bits of Salsa20's 64-bit counter; y09 stays zero.
|
|
96
|
+
// Based on {@link https://cr.yp.to/salsa20.html | the Salsa20 reference page}.
|
|
97
|
+
let y00 = s[0], y01 = k[0], y02 = k[1], y03 = k[2], // "expa" Key Key Key
|
|
98
|
+
y04 = k[3], y05 = s[1], y06 = n[0], y07 = n[1], // Key "nd 3" Nonce Nonce
|
|
99
|
+
y08 = cnt, y09 = 0, y10 = s[2], y11 = k[4], // Pos. Pos. "2-by" Key
|
|
100
|
+
y12 = k[5], y13 = k[6], y14 = k[7], y15 = s[3]; // Key Key Key "te k"
|
|
101
|
+
// Save state to temporary variables
|
|
102
|
+
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;
|
|
103
|
+
for (let r = 0; r < rounds; r += 2) {
|
|
104
|
+
x04 ^= (0, _arx_1.rotl)(x00 + x12 | 0, 7);
|
|
105
|
+
x08 ^= (0, _arx_1.rotl)(x04 + x00 | 0, 9);
|
|
106
|
+
x12 ^= (0, _arx_1.rotl)(x08 + x04 | 0, 13);
|
|
107
|
+
x00 ^= (0, _arx_1.rotl)(x12 + x08 | 0, 18);
|
|
108
|
+
x09 ^= (0, _arx_1.rotl)(x05 + x01 | 0, 7);
|
|
109
|
+
x13 ^= (0, _arx_1.rotl)(x09 + x05 | 0, 9);
|
|
110
|
+
x01 ^= (0, _arx_1.rotl)(x13 + x09 | 0, 13);
|
|
111
|
+
x05 ^= (0, _arx_1.rotl)(x01 + x13 | 0, 18);
|
|
112
|
+
x14 ^= (0, _arx_1.rotl)(x10 + x06 | 0, 7);
|
|
113
|
+
x02 ^= (0, _arx_1.rotl)(x14 + x10 | 0, 9);
|
|
114
|
+
x06 ^= (0, _arx_1.rotl)(x02 + x14 | 0, 13);
|
|
115
|
+
x10 ^= (0, _arx_1.rotl)(x06 + x02 | 0, 18);
|
|
116
|
+
x03 ^= (0, _arx_1.rotl)(x15 + x11 | 0, 7);
|
|
117
|
+
x07 ^= (0, _arx_1.rotl)(x03 + x15 | 0, 9);
|
|
118
|
+
x11 ^= (0, _arx_1.rotl)(x07 + x03 | 0, 13);
|
|
119
|
+
x15 ^= (0, _arx_1.rotl)(x11 + x07 | 0, 18);
|
|
120
|
+
x01 ^= (0, _arx_1.rotl)(x00 + x03 | 0, 7);
|
|
121
|
+
x02 ^= (0, _arx_1.rotl)(x01 + x00 | 0, 9);
|
|
122
|
+
x03 ^= (0, _arx_1.rotl)(x02 + x01 | 0, 13);
|
|
123
|
+
x00 ^= (0, _arx_1.rotl)(x03 + x02 | 0, 18);
|
|
124
|
+
x06 ^= (0, _arx_1.rotl)(x05 + x04 | 0, 7);
|
|
125
|
+
x07 ^= (0, _arx_1.rotl)(x06 + x05 | 0, 9);
|
|
126
|
+
x04 ^= (0, _arx_1.rotl)(x07 + x06 | 0, 13);
|
|
127
|
+
x05 ^= (0, _arx_1.rotl)(x04 + x07 | 0, 18);
|
|
128
|
+
x11 ^= (0, _arx_1.rotl)(x10 + x09 | 0, 7);
|
|
129
|
+
x08 ^= (0, _arx_1.rotl)(x11 + x10 | 0, 9);
|
|
130
|
+
x09 ^= (0, _arx_1.rotl)(x08 + x11 | 0, 13);
|
|
131
|
+
x10 ^= (0, _arx_1.rotl)(x09 + x08 | 0, 18);
|
|
132
|
+
x12 ^= (0, _arx_1.rotl)(x15 + x14 | 0, 7);
|
|
133
|
+
x13 ^= (0, _arx_1.rotl)(x12 + x15 | 0, 9);
|
|
134
|
+
x14 ^= (0, _arx_1.rotl)(x13 + x12 | 0, 13);
|
|
135
|
+
x15 ^= (0, _arx_1.rotl)(x14 + x13 | 0, 18);
|
|
136
|
+
}
|
|
137
|
+
// Write output
|
|
138
|
+
let oi = 0;
|
|
139
|
+
out[oi++] = (y00 + x00) | 0;
|
|
140
|
+
out[oi++] = (y01 + x01) | 0;
|
|
141
|
+
out[oi++] = (y02 + x02) | 0;
|
|
142
|
+
out[oi++] = (y03 + x03) | 0;
|
|
143
|
+
out[oi++] = (y04 + x04) | 0;
|
|
144
|
+
out[oi++] = (y05 + x05) | 0;
|
|
145
|
+
out[oi++] = (y06 + x06) | 0;
|
|
146
|
+
out[oi++] = (y07 + x07) | 0;
|
|
147
|
+
out[oi++] = (y08 + x08) | 0;
|
|
148
|
+
out[oi++] = (y09 + x09) | 0;
|
|
149
|
+
out[oi++] = (y10 + x10) | 0;
|
|
150
|
+
out[oi++] = (y11 + x11) | 0;
|
|
151
|
+
out[oi++] = (y12 + x12) | 0;
|
|
152
|
+
out[oi++] = (y13 + x13) | 0;
|
|
153
|
+
out[oi++] = (y14 + x14) | 0;
|
|
154
|
+
out[oi++] = (y15 + x15) | 0;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* hsalsa hashes key and nonce-prefix words into the 32-byte subkey used by XSalsa20.
|
|
158
|
+
* Identical to `hsalsa_small`.
|
|
159
|
+
* Need to find a way to merge it with `salsaCore` without 25% performance hit.
|
|
160
|
+
* @param s - Sigma constants as 32-bit words.
|
|
161
|
+
* @param k - Key words.
|
|
162
|
+
* @param i - Nonce-prefix words.
|
|
163
|
+
* @param out - Output buffer for the derived subkey.
|
|
164
|
+
* @example
|
|
165
|
+
* Derives the XSalsa20 subkey from sigma, key, and nonce-prefix words.
|
|
166
|
+
*
|
|
167
|
+
* ```ts
|
|
168
|
+
* const sigma = new Uint32Array(4);
|
|
169
|
+
* const key = new Uint32Array(8);
|
|
170
|
+
* const nonce = new Uint32Array(4);
|
|
171
|
+
* const out = new Uint32Array(8);
|
|
172
|
+
* hsalsa(sigma, key, nonce, out);
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
// prettier-ignore
|
|
176
|
+
function hsalsa(s, k, i, out) {
|
|
177
|
+
let x00 = (0, utils_1.swap8IfBE)(s[0]), x01 = (0, utils_1.swap8IfBE)(k[0]), x02 = (0, utils_1.swap8IfBE)(k[1]), x03 = (0, utils_1.swap8IfBE)(k[2]), x04 = (0, utils_1.swap8IfBE)(k[3]), x05 = (0, utils_1.swap8IfBE)(s[1]), x06 = (0, utils_1.swap8IfBE)(i[0]), x07 = (0, utils_1.swap8IfBE)(i[1]), x08 = (0, utils_1.swap8IfBE)(i[2]), x09 = (0, utils_1.swap8IfBE)(i[3]), x10 = (0, utils_1.swap8IfBE)(s[2]), x11 = (0, utils_1.swap8IfBE)(k[4]), x12 = (0, utils_1.swap8IfBE)(k[5]), x13 = (0, utils_1.swap8IfBE)(k[6]), x14 = (0, utils_1.swap8IfBE)(k[7]), x15 = (0, utils_1.swap8IfBE)(s[3]);
|
|
178
|
+
for (let r = 0; r < 20; r += 2) {
|
|
179
|
+
x04 ^= (0, _arx_1.rotl)(x00 + x12 | 0, 7);
|
|
180
|
+
x08 ^= (0, _arx_1.rotl)(x04 + x00 | 0, 9);
|
|
181
|
+
x12 ^= (0, _arx_1.rotl)(x08 + x04 | 0, 13);
|
|
182
|
+
x00 ^= (0, _arx_1.rotl)(x12 + x08 | 0, 18);
|
|
183
|
+
x09 ^= (0, _arx_1.rotl)(x05 + x01 | 0, 7);
|
|
184
|
+
x13 ^= (0, _arx_1.rotl)(x09 + x05 | 0, 9);
|
|
185
|
+
x01 ^= (0, _arx_1.rotl)(x13 + x09 | 0, 13);
|
|
186
|
+
x05 ^= (0, _arx_1.rotl)(x01 + x13 | 0, 18);
|
|
187
|
+
x14 ^= (0, _arx_1.rotl)(x10 + x06 | 0, 7);
|
|
188
|
+
x02 ^= (0, _arx_1.rotl)(x14 + x10 | 0, 9);
|
|
189
|
+
x06 ^= (0, _arx_1.rotl)(x02 + x14 | 0, 13);
|
|
190
|
+
x10 ^= (0, _arx_1.rotl)(x06 + x02 | 0, 18);
|
|
191
|
+
x03 ^= (0, _arx_1.rotl)(x15 + x11 | 0, 7);
|
|
192
|
+
x07 ^= (0, _arx_1.rotl)(x03 + x15 | 0, 9);
|
|
193
|
+
x11 ^= (0, _arx_1.rotl)(x07 + x03 | 0, 13);
|
|
194
|
+
x15 ^= (0, _arx_1.rotl)(x11 + x07 | 0, 18);
|
|
195
|
+
x01 ^= (0, _arx_1.rotl)(x00 + x03 | 0, 7);
|
|
196
|
+
x02 ^= (0, _arx_1.rotl)(x01 + x00 | 0, 9);
|
|
197
|
+
x03 ^= (0, _arx_1.rotl)(x02 + x01 | 0, 13);
|
|
198
|
+
x00 ^= (0, _arx_1.rotl)(x03 + x02 | 0, 18);
|
|
199
|
+
x06 ^= (0, _arx_1.rotl)(x05 + x04 | 0, 7);
|
|
200
|
+
x07 ^= (0, _arx_1.rotl)(x06 + x05 | 0, 9);
|
|
201
|
+
x04 ^= (0, _arx_1.rotl)(x07 + x06 | 0, 13);
|
|
202
|
+
x05 ^= (0, _arx_1.rotl)(x04 + x07 | 0, 18);
|
|
203
|
+
x11 ^= (0, _arx_1.rotl)(x10 + x09 | 0, 7);
|
|
204
|
+
x08 ^= (0, _arx_1.rotl)(x11 + x10 | 0, 9);
|
|
205
|
+
x09 ^= (0, _arx_1.rotl)(x08 + x11 | 0, 13);
|
|
206
|
+
x10 ^= (0, _arx_1.rotl)(x09 + x08 | 0, 18);
|
|
207
|
+
x12 ^= (0, _arx_1.rotl)(x15 + x14 | 0, 7);
|
|
208
|
+
x13 ^= (0, _arx_1.rotl)(x12 + x15 | 0, 9);
|
|
209
|
+
x14 ^= (0, _arx_1.rotl)(x13 + x12 | 0, 13);
|
|
210
|
+
x15 ^= (0, _arx_1.rotl)(x14 + x13 | 0, 18);
|
|
211
|
+
}
|
|
212
|
+
let oi = 0;
|
|
213
|
+
// XSalsa20 takes words 0,5,10,15 and 6,7,8,9 as the 32-byte subkey material.
|
|
214
|
+
out[oi++] = x00;
|
|
215
|
+
out[oi++] = x05;
|
|
216
|
+
out[oi++] = x10;
|
|
217
|
+
out[oi++] = x15;
|
|
218
|
+
out[oi++] = x06;
|
|
219
|
+
out[oi++] = x07;
|
|
220
|
+
out[oi++] = x08;
|
|
221
|
+
out[oi++] = x09;
|
|
222
|
+
(0, utils_1.swap32IfBE)(out);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Salsa20 from original paper. 8-byte nonce.
|
|
226
|
+
* With smaller nonce, it's not safe to make it random (CSPRNG), due to collision chance.
|
|
227
|
+
* @param key - 16-byte or 32-byte key.
|
|
228
|
+
* @param nonce - 8-byte nonce.
|
|
229
|
+
* @param data - Input bytes to xor with the keystream.
|
|
230
|
+
* @param output - Optional destination buffer.
|
|
231
|
+
* @param counter - Initial block counter.
|
|
232
|
+
* Only the low 32 bits of Salsa20's 64-bit counter state are exposed here;
|
|
233
|
+
* the high word stays zero and the implementation still caps the public
|
|
234
|
+
* value to 32 bits.
|
|
235
|
+
* @returns Encrypted or decrypted bytes.
|
|
236
|
+
* @example
|
|
237
|
+
* Encrypts bytes with the original 8-byte-nonce Salsa20 stream cipher.
|
|
238
|
+
*
|
|
239
|
+
* ```ts
|
|
240
|
+
* import { salsa20 } from './salsa';
|
|
241
|
+
* import { randomBytes } from './utils';
|
|
242
|
+
* const key = randomBytes(32);
|
|
243
|
+
* const nonce = randomBytes(8);
|
|
244
|
+
* salsa20(key, nonce, new Uint8Array([1, 2, 3, 4]));
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
exports.salsa20 = (0, _arx_1.createCipher)(salsaCore, {
|
|
248
|
+
allowShortKeys: true,
|
|
249
|
+
counterRight: true,
|
|
250
|
+
});
|
|
251
|
+
/**
|
|
252
|
+
* XSalsa20 extended-nonce salsa.
|
|
253
|
+
* With 24-byte nonce, it's safe to make it random (CSPRNG).
|
|
254
|
+
* @param key - 32-byte key.
|
|
255
|
+
* This XSalsa20 wrapper does not enable Salsa20's 16-byte legacy key mode.
|
|
256
|
+
* @param nonce - 24-byte nonce.
|
|
257
|
+
* @param data - Input bytes to xor with the keystream.
|
|
258
|
+
* @param output - Optional destination buffer.
|
|
259
|
+
* @param counter - Initial block counter.
|
|
260
|
+
* @returns Encrypted or decrypted bytes.
|
|
261
|
+
* @example
|
|
262
|
+
* Encrypts bytes with XSalsa20 and a random 24-byte nonce.
|
|
263
|
+
*
|
|
264
|
+
* ```ts
|
|
265
|
+
* import { xsalsa20 } from './salsa';
|
|
266
|
+
* import { randomBytes } from './utils';
|
|
267
|
+
* const key = randomBytes(32);
|
|
268
|
+
* const nonce = randomBytes(24);
|
|
269
|
+
* xsalsa20(key, nonce, new Uint8Array([1, 2, 3, 4]));
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
exports.xsalsa20 = (0, _arx_1.createCipher)(salsaCore, {
|
|
273
|
+
counterRight: true,
|
|
274
|
+
extendNonceFn: hsalsa,
|
|
275
|
+
});
|
|
276
|
+
/**
|
|
277
|
+
* xsalsa20-poly1305 eXtended-nonce (24 bytes) salsa.
|
|
278
|
+
* With 24-byte nonce, it's safe to make it random (CSPRNG).
|
|
279
|
+
* Also known as `secretbox` from libsodium / nacl.
|
|
280
|
+
* No AAD input is supported here. Caller-provided `output` buffers for
|
|
281
|
+
* `encrypt()` / `decrypt()` must be `input.length + 32` bytes because the
|
|
282
|
+
* implementation uses a 32-byte leading scratch area before returning `+16`.
|
|
283
|
+
* @param key - 32-byte key.
|
|
284
|
+
* @param nonce - 24-byte nonce.
|
|
285
|
+
* @param AAD - Must be omitted; XSalsa20-Poly1305 secretbox does not support associated data.
|
|
286
|
+
* @returns AEAD cipher instance.
|
|
287
|
+
* @example
|
|
288
|
+
* Encrypts and authenticates plaintext with XSalsa20-Poly1305.
|
|
289
|
+
*
|
|
290
|
+
* ```ts
|
|
291
|
+
* import { xsalsa20poly1305 } from './salsa';
|
|
292
|
+
* import { randomBytes } from './utils';
|
|
293
|
+
* const key = randomBytes(32);
|
|
294
|
+
* const nonce = randomBytes(24);
|
|
295
|
+
* const cipher = xsalsa20poly1305(key, nonce);
|
|
296
|
+
* cipher.encrypt(new Uint8Array([1, 2, 3]));
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
exports.xsalsa20poly1305 = (0, utils_1.wrapCipher)({ blockSize: 64, nonceLength: 24, tagLength: 16 }, (key, nonce) => {
|
|
300
|
+
// This borrows caller key/nonce buffers by reference; mutating them after construction changes
|
|
301
|
+
// later encrypt/decrypt outputs.
|
|
302
|
+
return {
|
|
303
|
+
encrypt(plaintext, output) {
|
|
304
|
+
// xsalsa20poly1305 optimizes by calculating auth key during the same call as encryption.
|
|
305
|
+
// Unfortunately, makes it hard to separate tag calculation & encryption itself,
|
|
306
|
+
// because 32 bytes is half-block of 64-byte salsa.
|
|
307
|
+
// Need 32 extra bytes up front for the auth-key scratch area described above.
|
|
308
|
+
output = (0, utils_1.getOutput)(plaintext.length + 32, output, false);
|
|
309
|
+
// output[0..32] = Poly1305 auth key, output[32..] = plaintext then ciphertext.
|
|
310
|
+
const authKey = output.subarray(0, 32);
|
|
311
|
+
const ciphPlaintext = output.subarray(32);
|
|
312
|
+
output.set(plaintext, 32);
|
|
313
|
+
// authKey is produced by xoring the first 32 bytes with zeros.
|
|
314
|
+
(0, utils_1.clean)(authKey);
|
|
315
|
+
// output = stream ^ output; authKey = stream ^ zeros(32)
|
|
316
|
+
(0, exports.xsalsa20)(key, nonce, output, output);
|
|
317
|
+
const tag = (0, _poly1305_1.poly1305)(ciphPlaintext, authKey);
|
|
318
|
+
output.set(tag, 16);
|
|
319
|
+
// Clean up auth-key remnants and the temporary tag copy.
|
|
320
|
+
(0, utils_1.clean)(output.subarray(0, 16), tag);
|
|
321
|
+
// Return output[16..].
|
|
322
|
+
return output.subarray(16);
|
|
323
|
+
},
|
|
324
|
+
decrypt(ciphertext, output) {
|
|
325
|
+
// tmp part passed tag ciphertext
|
|
326
|
+
// [0..32] [32..48] [48..]
|
|
327
|
+
// Authenticate the ciphertext before decrypting it; on tag failure the scratch/output
|
|
328
|
+
// buffer may already contain copied ciphertext and derived auth-key material.
|
|
329
|
+
(0, utils_1.abytes)(ciphertext);
|
|
330
|
+
output = (0, utils_1.getOutput)(ciphertext.length + 32, output, false);
|
|
331
|
+
// output[0..32] is auth-key scratch, output[32..48] is passed tag,
|
|
332
|
+
// output[48..] is ciphertext then plaintext.
|
|
333
|
+
const tmp = output.subarray(0, 32);
|
|
334
|
+
const passedTag = output.subarray(32, 48);
|
|
335
|
+
const ciphPlaintext = output.subarray(48);
|
|
336
|
+
output.set(ciphertext, 32);
|
|
337
|
+
// authKey is produced by xoring the scratch area with zeros.
|
|
338
|
+
(0, utils_1.clean)(tmp);
|
|
339
|
+
const authKey = (0, exports.xsalsa20)(key, nonce, tmp, tmp);
|
|
340
|
+
const tag = (0, _poly1305_1.poly1305)(ciphPlaintext, authKey);
|
|
341
|
+
if (!(0, utils_1.equalBytes)(passedTag, tag)) {
|
|
342
|
+
(0, utils_1.clean)(output);
|
|
343
|
+
throw new Error('invalid tag');
|
|
344
|
+
}
|
|
345
|
+
// output = stream ^ output[16..]
|
|
346
|
+
(0, exports.xsalsa20)(key, nonce, output.subarray(16), output.subarray(16));
|
|
347
|
+
(0, utils_1.clean)(tmp, passedTag, tag);
|
|
348
|
+
// Return output[48..], skipping zeroized output[0..48].
|
|
349
|
+
return ciphPlaintext;
|
|
350
|
+
},
|
|
351
|
+
};
|
|
352
|
+
});
|
|
353
|
+
/**
|
|
354
|
+
* Alias to `xsalsa20poly1305`, for compatibility with libsodium / nacl.
|
|
355
|
+
* Check out {@link https://github.com/serenity-kit/noble-sodium | noble-sodium}
|
|
356
|
+
* for `crypto_box`.
|
|
357
|
+
* @param key - 32-byte key.
|
|
358
|
+
* @param nonce - 24-byte nonce.
|
|
359
|
+
* @returns Wrapper with `seal()` and `open()` helpers.
|
|
360
|
+
* @example
|
|
361
|
+
* Uses the libsodium-style `seal()` and `open()` wrapper.
|
|
362
|
+
*
|
|
363
|
+
* ```ts
|
|
364
|
+
* import { secretbox } from './salsa';
|
|
365
|
+
* import { randomBytes } from './utils';
|
|
366
|
+
* const key = randomBytes(32);
|
|
367
|
+
* const nonce = randomBytes(24);
|
|
368
|
+
* const box = secretbox(key, nonce);
|
|
369
|
+
* box.seal(new Uint8Array([1, 2, 3]));
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
372
|
+
function secretbox(key, nonce) {
|
|
373
|
+
const xs = (0, exports.xsalsa20poly1305)(key, nonce);
|
|
374
|
+
return { seal: xs.encrypt, open: xs.decrypt };
|
|
375
|
+
}
|