@pathscale/secure-local-storage-chacha20-poly1305 1.0.0 → 1.0.1
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/CHANGELOG.md +11 -0
- package/README.md +16 -32
- package/dist/SecureLocalStorage.d.ts +0 -1
- package/dist/SecureLocalStorage.d.ts.map +1 -1
- package/dist/encryption.d.ts +4 -2
- package/dist/encryption.d.ts.map +1 -1
- package/dist/index.js +1371 -149
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1370 -148
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -6
package/dist/index.mjs
CHANGED
|
@@ -1,178 +1,1220 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// Copyright (C) 2016 Dmitry Chestnykh
|
|
2
|
+
// MIT License. See LICENSE file for details.
|
|
3
|
+
/**
|
|
4
|
+
* Package binary provides functions for encoding and decoding numbers in byte arrays.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Writes 4-byte little-endian representation of 32-bit unsigned
|
|
8
|
+
* value to array starting at offset.
|
|
9
|
+
*
|
|
10
|
+
* If byte array is not given, creates a new 4-byte one.
|
|
11
|
+
*
|
|
12
|
+
* Returns the output byte array.
|
|
13
|
+
*/
|
|
14
|
+
function writeUint32LE(value, out = new Uint8Array(4), offset = 0) {
|
|
15
|
+
out[offset + 0] = value >>> 0;
|
|
16
|
+
out[offset + 1] = value >>> 8;
|
|
17
|
+
out[offset + 2] = value >>> 16;
|
|
18
|
+
out[offset + 3] = value >>> 24;
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Writes 8-byte little-endian representation of 64-bit unsigned
|
|
23
|
+
* value to byte array starting at offset.
|
|
24
|
+
*
|
|
25
|
+
* Due to JavaScript limitation, supports values up to 2^53-1.
|
|
26
|
+
*
|
|
27
|
+
* If byte array is not given, creates a new 8-byte one.
|
|
28
|
+
*
|
|
29
|
+
* Returns the output byte array.
|
|
30
|
+
*/
|
|
31
|
+
function writeUint64LE(value, out = new Uint8Array(8), offset = 0) {
|
|
32
|
+
writeUint32LE(value >>> 0, out, offset);
|
|
33
|
+
writeUint32LE(value / 0x100000000 >>> 0, out, offset + 4);
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
5
36
|
|
|
37
|
+
// Copyright (C) 2016 Dmitry Chestnykh
|
|
38
|
+
// MIT License. See LICENSE file for details.
|
|
6
39
|
/**
|
|
7
|
-
*
|
|
40
|
+
* Sets all values in the given array to zero and returns it.
|
|
41
|
+
*
|
|
42
|
+
* The fact that it sets bytes to zero can be relied on.
|
|
43
|
+
*
|
|
44
|
+
* There is no guarantee that this function makes data disappear from memory,
|
|
45
|
+
* as runtime implementation can, for example, have copying garbage collector
|
|
46
|
+
* that will make copies of sensitive data before we wipe it. Or that an
|
|
47
|
+
* operating system will write our data to swap or sleep image. Another thing
|
|
48
|
+
* is that an optimizing compiler can remove calls to this function or make it
|
|
49
|
+
* no-op. There's nothing we can do with it, so we just do our best and hope
|
|
50
|
+
* that everything will be okay and good will triumph over evil.
|
|
8
51
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
52
|
+
function wipe(array) {
|
|
53
|
+
// Right now it's similar to array.fill(0). If it turns
|
|
54
|
+
// out that runtimes optimize this call away, maybe
|
|
55
|
+
// we can try something else.
|
|
56
|
+
for (let i = 0; i < array.length; i++) {
|
|
57
|
+
array[i] = 0;
|
|
12
58
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
59
|
+
return array;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Copyright (C) 2016 Dmitry Chestnykh
|
|
63
|
+
// MIT License. See LICENSE file for details.
|
|
64
|
+
/**
|
|
65
|
+
* Package chacha implements ChaCha stream cipher.
|
|
66
|
+
*/
|
|
67
|
+
// Number of ChaCha rounds (ChaCha20).
|
|
68
|
+
const ROUNDS$1 = 20;
|
|
69
|
+
// Applies the ChaCha core function to 16-byte input,
|
|
70
|
+
// 32-byte key key, and puts the result into 64-byte array out.
|
|
71
|
+
function core(out, input, key) {
|
|
72
|
+
let j0 = 0x61707865; // "expa" -- ChaCha's "sigma" constant
|
|
73
|
+
let j1 = 0x3320646E; // "nd 3" for 32-byte keys
|
|
74
|
+
let j2 = 0x79622D32; // "2-by"
|
|
75
|
+
let j3 = 0x6B206574; // "te k"
|
|
76
|
+
let j4 = (key[3] << 24) | (key[2] << 16) | (key[1] << 8) | key[0];
|
|
77
|
+
let j5 = (key[7] << 24) | (key[6] << 16) | (key[5] << 8) | key[4];
|
|
78
|
+
let j6 = (key[11] << 24) | (key[10] << 16) | (key[9] << 8) | key[8];
|
|
79
|
+
let j7 = (key[15] << 24) | (key[14] << 16) | (key[13] << 8) | key[12];
|
|
80
|
+
let j8 = (key[19] << 24) | (key[18] << 16) | (key[17] << 8) | key[16];
|
|
81
|
+
let j9 = (key[23] << 24) | (key[22] << 16) | (key[21] << 8) | key[20];
|
|
82
|
+
let j10 = (key[27] << 24) | (key[26] << 16) | (key[25] << 8) | key[24];
|
|
83
|
+
let j11 = (key[31] << 24) | (key[30] << 16) | (key[29] << 8) | key[28];
|
|
84
|
+
let j12 = (input[3] << 24) | (input[2] << 16) | (input[1] << 8) | input[0];
|
|
85
|
+
let j13 = (input[7] << 24) | (input[6] << 16) | (input[5] << 8) | input[4];
|
|
86
|
+
let j14 = (input[11] << 24) | (input[10] << 16) | (input[9] << 8) | input[8];
|
|
87
|
+
let j15 = (input[15] << 24) | (input[14] << 16) | (input[13] << 8) | input[12];
|
|
88
|
+
let x0 = j0;
|
|
89
|
+
let x1 = j1;
|
|
90
|
+
let x2 = j2;
|
|
91
|
+
let x3 = j3;
|
|
92
|
+
let x4 = j4;
|
|
93
|
+
let x5 = j5;
|
|
94
|
+
let x6 = j6;
|
|
95
|
+
let x7 = j7;
|
|
96
|
+
let x8 = j8;
|
|
97
|
+
let x9 = j9;
|
|
98
|
+
let x10 = j10;
|
|
99
|
+
let x11 = j11;
|
|
100
|
+
let x12 = j12;
|
|
101
|
+
let x13 = j13;
|
|
102
|
+
let x14 = j14;
|
|
103
|
+
let x15 = j15;
|
|
104
|
+
for (let i = 0; i < ROUNDS$1; i += 2) {
|
|
105
|
+
x0 = x0 + x4 | 0;
|
|
106
|
+
x12 ^= x0;
|
|
107
|
+
x12 = x12 >>> (32 - 16) | x12 << 16;
|
|
108
|
+
x8 = x8 + x12 | 0;
|
|
109
|
+
x4 ^= x8;
|
|
110
|
+
x4 = x4 >>> (32 - 12) | x4 << 12;
|
|
111
|
+
x1 = x1 + x5 | 0;
|
|
112
|
+
x13 ^= x1;
|
|
113
|
+
x13 = x13 >>> (32 - 16) | x13 << 16;
|
|
114
|
+
x9 = x9 + x13 | 0;
|
|
115
|
+
x5 ^= x9;
|
|
116
|
+
x5 = x5 >>> (32 - 12) | x5 << 12;
|
|
117
|
+
x2 = x2 + x6 | 0;
|
|
118
|
+
x14 ^= x2;
|
|
119
|
+
x14 = x14 >>> (32 - 16) | x14 << 16;
|
|
120
|
+
x10 = x10 + x14 | 0;
|
|
121
|
+
x6 ^= x10;
|
|
122
|
+
x6 = x6 >>> (32 - 12) | x6 << 12;
|
|
123
|
+
x3 = x3 + x7 | 0;
|
|
124
|
+
x15 ^= x3;
|
|
125
|
+
x15 = x15 >>> (32 - 16) | x15 << 16;
|
|
126
|
+
x11 = x11 + x15 | 0;
|
|
127
|
+
x7 ^= x11;
|
|
128
|
+
x7 = x7 >>> (32 - 12) | x7 << 12;
|
|
129
|
+
x2 = x2 + x6 | 0;
|
|
130
|
+
x14 ^= x2;
|
|
131
|
+
x14 = x14 >>> (32 - 8) | x14 << 8;
|
|
132
|
+
x10 = x10 + x14 | 0;
|
|
133
|
+
x6 ^= x10;
|
|
134
|
+
x6 = x6 >>> (32 - 7) | x6 << 7;
|
|
135
|
+
x3 = x3 + x7 | 0;
|
|
136
|
+
x15 ^= x3;
|
|
137
|
+
x15 = x15 >>> (32 - 8) | x15 << 8;
|
|
138
|
+
x11 = x11 + x15 | 0;
|
|
139
|
+
x7 ^= x11;
|
|
140
|
+
x7 = x7 >>> (32 - 7) | x7 << 7;
|
|
141
|
+
x1 = x1 + x5 | 0;
|
|
142
|
+
x13 ^= x1;
|
|
143
|
+
x13 = x13 >>> (32 - 8) | x13 << 8;
|
|
144
|
+
x9 = x9 + x13 | 0;
|
|
145
|
+
x5 ^= x9;
|
|
146
|
+
x5 = x5 >>> (32 - 7) | x5 << 7;
|
|
147
|
+
x0 = x0 + x4 | 0;
|
|
148
|
+
x12 ^= x0;
|
|
149
|
+
x12 = x12 >>> (32 - 8) | x12 << 8;
|
|
150
|
+
x8 = x8 + x12 | 0;
|
|
151
|
+
x4 ^= x8;
|
|
152
|
+
x4 = x4 >>> (32 - 7) | x4 << 7;
|
|
153
|
+
x0 = x0 + x5 | 0;
|
|
154
|
+
x15 ^= x0;
|
|
155
|
+
x15 = x15 >>> (32 - 16) | x15 << 16;
|
|
156
|
+
x10 = x10 + x15 | 0;
|
|
157
|
+
x5 ^= x10;
|
|
158
|
+
x5 = x5 >>> (32 - 12) | x5 << 12;
|
|
159
|
+
x1 = x1 + x6 | 0;
|
|
160
|
+
x12 ^= x1;
|
|
161
|
+
x12 = x12 >>> (32 - 16) | x12 << 16;
|
|
162
|
+
x11 = x11 + x12 | 0;
|
|
163
|
+
x6 ^= x11;
|
|
164
|
+
x6 = x6 >>> (32 - 12) | x6 << 12;
|
|
165
|
+
x2 = x2 + x7 | 0;
|
|
166
|
+
x13 ^= x2;
|
|
167
|
+
x13 = x13 >>> (32 - 16) | x13 << 16;
|
|
168
|
+
x8 = x8 + x13 | 0;
|
|
169
|
+
x7 ^= x8;
|
|
170
|
+
x7 = x7 >>> (32 - 12) | x7 << 12;
|
|
171
|
+
x3 = x3 + x4 | 0;
|
|
172
|
+
x14 ^= x3;
|
|
173
|
+
x14 = x14 >>> (32 - 16) | x14 << 16;
|
|
174
|
+
x9 = x9 + x14 | 0;
|
|
175
|
+
x4 ^= x9;
|
|
176
|
+
x4 = x4 >>> (32 - 12) | x4 << 12;
|
|
177
|
+
x2 = x2 + x7 | 0;
|
|
178
|
+
x13 ^= x2;
|
|
179
|
+
x13 = x13 >>> (32 - 8) | x13 << 8;
|
|
180
|
+
x8 = x8 + x13 | 0;
|
|
181
|
+
x7 ^= x8;
|
|
182
|
+
x7 = x7 >>> (32 - 7) | x7 << 7;
|
|
183
|
+
x3 = x3 + x4 | 0;
|
|
184
|
+
x14 ^= x3;
|
|
185
|
+
x14 = x14 >>> (32 - 8) | x14 << 8;
|
|
186
|
+
x9 = x9 + x14 | 0;
|
|
187
|
+
x4 ^= x9;
|
|
188
|
+
x4 = x4 >>> (32 - 7) | x4 << 7;
|
|
189
|
+
x1 = x1 + x6 | 0;
|
|
190
|
+
x12 ^= x1;
|
|
191
|
+
x12 = x12 >>> (32 - 8) | x12 << 8;
|
|
192
|
+
x11 = x11 + x12 | 0;
|
|
193
|
+
x6 ^= x11;
|
|
194
|
+
x6 = x6 >>> (32 - 7) | x6 << 7;
|
|
195
|
+
x0 = x0 + x5 | 0;
|
|
196
|
+
x15 ^= x0;
|
|
197
|
+
x15 = x15 >>> (32 - 8) | x15 << 8;
|
|
198
|
+
x10 = x10 + x15 | 0;
|
|
199
|
+
x5 ^= x10;
|
|
200
|
+
x5 = x5 >>> (32 - 7) | x5 << 7;
|
|
31
201
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
202
|
+
writeUint32LE(x0 + j0 | 0, out, 0);
|
|
203
|
+
writeUint32LE(x1 + j1 | 0, out, 4);
|
|
204
|
+
writeUint32LE(x2 + j2 | 0, out, 8);
|
|
205
|
+
writeUint32LE(x3 + j3 | 0, out, 12);
|
|
206
|
+
writeUint32LE(x4 + j4 | 0, out, 16);
|
|
207
|
+
writeUint32LE(x5 + j5 | 0, out, 20);
|
|
208
|
+
writeUint32LE(x6 + j6 | 0, out, 24);
|
|
209
|
+
writeUint32LE(x7 + j7 | 0, out, 28);
|
|
210
|
+
writeUint32LE(x8 + j8 | 0, out, 32);
|
|
211
|
+
writeUint32LE(x9 + j9 | 0, out, 36);
|
|
212
|
+
writeUint32LE(x10 + j10 | 0, out, 40);
|
|
213
|
+
writeUint32LE(x11 + j11 | 0, out, 44);
|
|
214
|
+
writeUint32LE(x12 + j12 | 0, out, 48);
|
|
215
|
+
writeUint32LE(x13 + j13 | 0, out, 52);
|
|
216
|
+
writeUint32LE(x14 + j14 | 0, out, 56);
|
|
217
|
+
writeUint32LE(x15 + j15 | 0, out, 60);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Encrypt src with ChaCha20 stream generated for the given 32-byte key and
|
|
221
|
+
* 8-byte (as in original implementation) or 12-byte (as in RFC7539) nonce and
|
|
222
|
+
* write the result into dst and return it.
|
|
223
|
+
*
|
|
224
|
+
* dst and src may be the same, but otherwise must not overlap.
|
|
225
|
+
*
|
|
226
|
+
* If nonce is 12 bytes, users should not encrypt more than 256 GiB with the
|
|
227
|
+
* same key and nonce, otherwise the stream will repeat. The function will
|
|
228
|
+
* throw error if counter overflows to prevent this.
|
|
229
|
+
*
|
|
230
|
+
* If nonce is 8 bytes, the output is practically unlimited (2^70 bytes, which
|
|
231
|
+
* is more than a million petabytes). However, it is not recommended to
|
|
232
|
+
* generate 8-byte nonces randomly, as the chance of collision is high.
|
|
233
|
+
*
|
|
234
|
+
* Never use the same key and nonce to encrypt more than one message.
|
|
235
|
+
*
|
|
236
|
+
* If nonceInplaceCounterLength is not 0, the nonce is assumed to be a 16-byte
|
|
237
|
+
* array with stream counter in first nonceInplaceCounterLength bytes and nonce
|
|
238
|
+
* in the last remaining bytes. The counter will be incremented inplace for
|
|
239
|
+
* each ChaCha block. This is useful if you need to encrypt one stream of data
|
|
240
|
+
* in chunks.
|
|
241
|
+
*/
|
|
242
|
+
function streamXOR(key, nonce, src, dst, nonceInplaceCounterLength = 0) {
|
|
243
|
+
// We only support 256-bit keys.
|
|
244
|
+
if (key.length !== 32) {
|
|
245
|
+
throw new Error("ChaCha: key size must be 32 bytes");
|
|
39
246
|
}
|
|
40
|
-
|
|
41
|
-
|
|
247
|
+
if (dst.length < src.length) {
|
|
248
|
+
throw new Error("ChaCha: destination is shorter than source");
|
|
42
249
|
}
|
|
43
|
-
|
|
44
|
-
|
|
250
|
+
let nc;
|
|
251
|
+
let counterLength;
|
|
252
|
+
if (nonceInplaceCounterLength === 0) {
|
|
253
|
+
if (nonce.length !== 8 && nonce.length !== 12) {
|
|
254
|
+
throw new Error("ChaCha nonce must be 8 or 12 bytes");
|
|
255
|
+
}
|
|
256
|
+
nc = new Uint8Array(16);
|
|
257
|
+
// First counterLength bytes of nc are counter, starting with zero.
|
|
258
|
+
counterLength = nc.length - nonce.length;
|
|
259
|
+
// Last bytes of nc after counterLength are nonce, set them.
|
|
260
|
+
nc.set(nonce, counterLength);
|
|
45
261
|
}
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
262
|
+
else {
|
|
263
|
+
if (nonce.length !== 16) {
|
|
264
|
+
throw new Error("ChaCha nonce with counter must be 16 bytes");
|
|
265
|
+
}
|
|
266
|
+
// This will update passed nonce with counter inplace.
|
|
267
|
+
nc = nonce;
|
|
268
|
+
counterLength = nonceInplaceCounterLength;
|
|
50
269
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
270
|
+
// Allocate temporary space for ChaCha block.
|
|
271
|
+
const block = new Uint8Array(64);
|
|
272
|
+
for (let i = 0; i < src.length; i += 64) {
|
|
273
|
+
// Generate a block.
|
|
274
|
+
core(block, nc, key);
|
|
275
|
+
// XOR block bytes with src into dst.
|
|
276
|
+
for (let j = i; j < i + 64 && j < src.length; j++) {
|
|
277
|
+
dst[j] = src[j] ^ block[j - i];
|
|
278
|
+
}
|
|
279
|
+
// Increment counter.
|
|
280
|
+
incrementCounter(nc, 0, counterLength);
|
|
59
281
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
282
|
+
// Cleanup temporary space.
|
|
283
|
+
wipe(block);
|
|
284
|
+
if (nonceInplaceCounterLength === 0) {
|
|
285
|
+
// Cleanup counter.
|
|
286
|
+
wipe(nc);
|
|
287
|
+
}
|
|
288
|
+
return dst;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Generate ChaCha20 stream for the given 32-byte key and 8-byte or 12-byte
|
|
292
|
+
* nonce and write it into dst and return it.
|
|
293
|
+
*
|
|
294
|
+
* Never use the same key and nonce to generate more than one stream.
|
|
295
|
+
*
|
|
296
|
+
* If nonceInplaceCounterLength is not 0, it behaves the same with respect to
|
|
297
|
+
* the nonce as described in the streamXOR documentation.
|
|
298
|
+
*
|
|
299
|
+
* stream is like streamXOR with all-zero src.
|
|
300
|
+
*/
|
|
301
|
+
function stream(key, nonce, dst, nonceInplaceCounterLength = 0) {
|
|
302
|
+
wipe(dst);
|
|
303
|
+
return streamXOR(key, nonce, dst, dst, nonceInplaceCounterLength);
|
|
304
|
+
}
|
|
305
|
+
function incrementCounter(counter, pos, len) {
|
|
306
|
+
let carry = 1;
|
|
307
|
+
while (len--) {
|
|
308
|
+
carry = carry + (counter[pos] & 0xff) | 0;
|
|
309
|
+
counter[pos] = carry & 0xff;
|
|
310
|
+
carry >>>= 8;
|
|
311
|
+
pos++;
|
|
312
|
+
}
|
|
313
|
+
if (carry > 0) {
|
|
314
|
+
throw new Error("ChaCha: counter overflow");
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Copyright (C) 2019 Kyle Den Hartog
|
|
319
|
+
// MIT License. See LICENSE file for details.
|
|
320
|
+
/**
|
|
321
|
+
* Package xchacha20 implements XChaCha20 stream cipher.
|
|
322
|
+
*/
|
|
323
|
+
// Number of ChaCha rounds (ChaCha20).
|
|
324
|
+
const ROUNDS = 20;
|
|
325
|
+
/**
|
|
326
|
+
* HChaCha is a one-way function used in XChaCha to extend nonce.
|
|
327
|
+
*
|
|
328
|
+
* It takes 32-byte key and 16-byte src and writes 32-byte result
|
|
329
|
+
* into dst and returns it.
|
|
330
|
+
*/
|
|
331
|
+
function hchacha(key, src, dst) {
|
|
332
|
+
let j0 = 0x61707865; // "expa" -- ChaCha's "sigma" constant
|
|
333
|
+
let j1 = 0x3320646e; // "nd 3" for 32-byte keys
|
|
334
|
+
let j2 = 0x79622d32; // "2-by"
|
|
335
|
+
let j3 = 0x6b206574; // "te k"
|
|
336
|
+
let j4 = (key[3] << 24) | (key[2] << 16) | (key[1] << 8) | key[0];
|
|
337
|
+
let j5 = (key[7] << 24) | (key[6] << 16) | (key[5] << 8) | key[4];
|
|
338
|
+
let j6 = (key[11] << 24) | (key[10] << 16) | (key[9] << 8) | key[8];
|
|
339
|
+
let j7 = (key[15] << 24) | (key[14] << 16) | (key[13] << 8) | key[12];
|
|
340
|
+
let j8 = (key[19] << 24) | (key[18] << 16) | (key[17] << 8) | key[16];
|
|
341
|
+
let j9 = (key[23] << 24) | (key[22] << 16) | (key[21] << 8) | key[20];
|
|
342
|
+
let j10 = (key[27] << 24) | (key[26] << 16) | (key[25] << 8) | key[24];
|
|
343
|
+
let j11 = (key[31] << 24) | (key[30] << 16) | (key[29] << 8) | key[28];
|
|
344
|
+
let j12 = (src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0];
|
|
345
|
+
let j13 = (src[7] << 24) | (src[6] << 16) | (src[5] << 8) | src[4];
|
|
346
|
+
let j14 = (src[11] << 24) | (src[10] << 16) | (src[9] << 8) | src[8];
|
|
347
|
+
let j15 = (src[15] << 24) | (src[14] << 16) | (src[13] << 8) | src[12];
|
|
348
|
+
let x0 = j0;
|
|
349
|
+
let x1 = j1;
|
|
350
|
+
let x2 = j2;
|
|
351
|
+
let x3 = j3;
|
|
352
|
+
let x4 = j4;
|
|
353
|
+
let x5 = j5;
|
|
354
|
+
let x6 = j6;
|
|
355
|
+
let x7 = j7;
|
|
356
|
+
let x8 = j8;
|
|
357
|
+
let x9 = j9;
|
|
358
|
+
let x10 = j10;
|
|
359
|
+
let x11 = j11;
|
|
360
|
+
let x12 = j12;
|
|
361
|
+
let x13 = j13;
|
|
362
|
+
let x14 = j14;
|
|
363
|
+
let x15 = j15;
|
|
364
|
+
for (let i = 0; i < ROUNDS; i += 2) {
|
|
365
|
+
x0 = (x0 + x4) | 0;
|
|
366
|
+
x12 ^= x0;
|
|
367
|
+
x12 = (x12 >>> (32 - 16)) | (x12 << 16);
|
|
368
|
+
x8 = (x8 + x12) | 0;
|
|
369
|
+
x4 ^= x8;
|
|
370
|
+
x4 = (x4 >>> (32 - 12)) | (x4 << 12);
|
|
371
|
+
x1 = (x1 + x5) | 0;
|
|
372
|
+
x13 ^= x1;
|
|
373
|
+
x13 = (x13 >>> (32 - 16)) | (x13 << 16);
|
|
374
|
+
x9 = (x9 + x13) | 0;
|
|
375
|
+
x5 ^= x9;
|
|
376
|
+
x5 = (x5 >>> (32 - 12)) | (x5 << 12);
|
|
377
|
+
x2 = (x2 + x6) | 0;
|
|
378
|
+
x14 ^= x2;
|
|
379
|
+
x14 = (x14 >>> (32 - 16)) | (x14 << 16);
|
|
380
|
+
x10 = (x10 + x14) | 0;
|
|
381
|
+
x6 ^= x10;
|
|
382
|
+
x6 = (x6 >>> (32 - 12)) | (x6 << 12);
|
|
383
|
+
x3 = (x3 + x7) | 0;
|
|
384
|
+
x15 ^= x3;
|
|
385
|
+
x15 = (x15 >>> (32 - 16)) | (x15 << 16);
|
|
386
|
+
x11 = (x11 + x15) | 0;
|
|
387
|
+
x7 ^= x11;
|
|
388
|
+
x7 = (x7 >>> (32 - 12)) | (x7 << 12);
|
|
389
|
+
x2 = (x2 + x6) | 0;
|
|
390
|
+
x14 ^= x2;
|
|
391
|
+
x14 = (x14 >>> (32 - 8)) | (x14 << 8);
|
|
392
|
+
x10 = (x10 + x14) | 0;
|
|
393
|
+
x6 ^= x10;
|
|
394
|
+
x6 = (x6 >>> (32 - 7)) | (x6 << 7);
|
|
395
|
+
x3 = (x3 + x7) | 0;
|
|
396
|
+
x15 ^= x3;
|
|
397
|
+
x15 = (x15 >>> (32 - 8)) | (x15 << 8);
|
|
398
|
+
x11 = (x11 + x15) | 0;
|
|
399
|
+
x7 ^= x11;
|
|
400
|
+
x7 = (x7 >>> (32 - 7)) | (x7 << 7);
|
|
401
|
+
x1 = (x1 + x5) | 0;
|
|
402
|
+
x13 ^= x1;
|
|
403
|
+
x13 = (x13 >>> (32 - 8)) | (x13 << 8);
|
|
404
|
+
x9 = (x9 + x13) | 0;
|
|
405
|
+
x5 ^= x9;
|
|
406
|
+
x5 = (x5 >>> (32 - 7)) | (x5 << 7);
|
|
407
|
+
x0 = (x0 + x4) | 0;
|
|
408
|
+
x12 ^= x0;
|
|
409
|
+
x12 = (x12 >>> (32 - 8)) | (x12 << 8);
|
|
410
|
+
x8 = (x8 + x12) | 0;
|
|
411
|
+
x4 ^= x8;
|
|
412
|
+
x4 = (x4 >>> (32 - 7)) | (x4 << 7);
|
|
413
|
+
x0 = (x0 + x5) | 0;
|
|
414
|
+
x15 ^= x0;
|
|
415
|
+
x15 = (x15 >>> (32 - 16)) | (x15 << 16);
|
|
416
|
+
x10 = (x10 + x15) | 0;
|
|
417
|
+
x5 ^= x10;
|
|
418
|
+
x5 = (x5 >>> (32 - 12)) | (x5 << 12);
|
|
419
|
+
x1 = (x1 + x6) | 0;
|
|
420
|
+
x12 ^= x1;
|
|
421
|
+
x12 = (x12 >>> (32 - 16)) | (x12 << 16);
|
|
422
|
+
x11 = (x11 + x12) | 0;
|
|
423
|
+
x6 ^= x11;
|
|
424
|
+
x6 = (x6 >>> (32 - 12)) | (x6 << 12);
|
|
425
|
+
x2 = (x2 + x7) | 0;
|
|
426
|
+
x13 ^= x2;
|
|
427
|
+
x13 = (x13 >>> (32 - 16)) | (x13 << 16);
|
|
428
|
+
x8 = (x8 + x13) | 0;
|
|
429
|
+
x7 ^= x8;
|
|
430
|
+
x7 = (x7 >>> (32 - 12)) | (x7 << 12);
|
|
431
|
+
x3 = (x3 + x4) | 0;
|
|
432
|
+
x14 ^= x3;
|
|
433
|
+
x14 = (x14 >>> (32 - 16)) | (x14 << 16);
|
|
434
|
+
x9 = (x9 + x14) | 0;
|
|
435
|
+
x4 ^= x9;
|
|
436
|
+
x4 = (x4 >>> (32 - 12)) | (x4 << 12);
|
|
437
|
+
x2 = (x2 + x7) | 0;
|
|
438
|
+
x13 ^= x2;
|
|
439
|
+
x13 = (x13 >>> (32 - 8)) | (x13 << 8);
|
|
440
|
+
x8 = (x8 + x13) | 0;
|
|
441
|
+
x7 ^= x8;
|
|
442
|
+
x7 = (x7 >>> (32 - 7)) | (x7 << 7);
|
|
443
|
+
x3 = (x3 + x4) | 0;
|
|
444
|
+
x14 ^= x3;
|
|
445
|
+
x14 = (x14 >>> (32 - 8)) | (x14 << 8);
|
|
446
|
+
x9 = (x9 + x14) | 0;
|
|
447
|
+
x4 ^= x9;
|
|
448
|
+
x4 = (x4 >>> (32 - 7)) | (x4 << 7);
|
|
449
|
+
x1 = (x1 + x6) | 0;
|
|
450
|
+
x12 ^= x1;
|
|
451
|
+
x12 = (x12 >>> (32 - 8)) | (x12 << 8);
|
|
452
|
+
x11 = (x11 + x12) | 0;
|
|
453
|
+
x6 ^= x11;
|
|
454
|
+
x6 = (x6 >>> (32 - 7)) | (x6 << 7);
|
|
455
|
+
x0 = (x0 + x5) | 0;
|
|
456
|
+
x15 ^= x0;
|
|
457
|
+
x15 = (x15 >>> (32 - 8)) | (x15 << 8);
|
|
458
|
+
x10 = (x10 + x15) | 0;
|
|
459
|
+
x5 ^= x10;
|
|
460
|
+
x5 = (x5 >>> (32 - 7)) | (x5 << 7);
|
|
461
|
+
}
|
|
462
|
+
writeUint32LE(x0, dst, 0);
|
|
463
|
+
writeUint32LE(x1, dst, 4);
|
|
464
|
+
writeUint32LE(x2, dst, 8);
|
|
465
|
+
writeUint32LE(x3, dst, 12);
|
|
466
|
+
writeUint32LE(x12, dst, 16);
|
|
467
|
+
writeUint32LE(x13, dst, 20);
|
|
468
|
+
writeUint32LE(x14, dst, 24);
|
|
469
|
+
writeUint32LE(x15, dst, 28);
|
|
470
|
+
return dst;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Copyright (C) 2016 Dmitry Chestnykh
|
|
474
|
+
// MIT License. See LICENSE file for details.
|
|
475
|
+
/**
|
|
476
|
+
* Package constant-time provides functions for performing algorithmically constant-time operations.
|
|
477
|
+
*/
|
|
478
|
+
/**
|
|
479
|
+
* NOTE! Due to the inability to guarantee real constant time evaluation of
|
|
480
|
+
* anything in JavaScript VM, this is module is the best effort.
|
|
481
|
+
*/
|
|
482
|
+
/**
|
|
483
|
+
* Returns resultIfOne if subject is 1, or resultIfZero if subject is 0.
|
|
484
|
+
*
|
|
485
|
+
* Supports only 32-bit integers, so resultIfOne or resultIfZero are not
|
|
486
|
+
* integers, they'll be converted to them with bitwise operations.
|
|
487
|
+
*/
|
|
488
|
+
/**
|
|
489
|
+
* Returns 1 if a and b are of equal length and their contents
|
|
490
|
+
* are equal, or 0 otherwise.
|
|
491
|
+
*
|
|
492
|
+
* Note that unlike in equal(), zero-length inputs are considered
|
|
493
|
+
* the same, so this function will return 1.
|
|
494
|
+
*/
|
|
495
|
+
function compare(a, b) {
|
|
496
|
+
if (a.length !== b.length) {
|
|
497
|
+
return 0;
|
|
498
|
+
}
|
|
499
|
+
let result = 0;
|
|
500
|
+
for (let i = 0; i < a.length; i++) {
|
|
501
|
+
result |= a[i] ^ b[i];
|
|
502
|
+
}
|
|
503
|
+
return (1 & ((result - 1) >>> 8));
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Returns true if a and b are of equal non-zero length,
|
|
507
|
+
* and their contents are equal, or false otherwise.
|
|
508
|
+
*
|
|
509
|
+
* Note that unlike in compare() zero-length inputs are considered
|
|
510
|
+
* _not_ equal, so this function will return false.
|
|
511
|
+
*/
|
|
512
|
+
function equal(a, b) {
|
|
513
|
+
if (a.length === 0 || b.length === 0) {
|
|
514
|
+
return false;
|
|
515
|
+
}
|
|
516
|
+
return compare(a, b) !== 0;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// Copyright (C) 2016 Dmitry Chestnykh
|
|
520
|
+
// MIT License. See LICENSE file for details.
|
|
521
|
+
/**
|
|
522
|
+
* Package poly1305 implements Poly1305 one-time message authentication algorithm.
|
|
523
|
+
*/
|
|
524
|
+
const DIGEST_LENGTH = 16;
|
|
525
|
+
// Port of Andrew Moon's Poly1305-donna-16. Public domain.
|
|
526
|
+
// https://github.com/floodyberry/poly1305-donna
|
|
527
|
+
/**
|
|
528
|
+
* Poly1305 computes 16-byte authenticator of message using
|
|
529
|
+
* a one-time 32-byte key.
|
|
530
|
+
*
|
|
531
|
+
* Important: key should be used for only one message,
|
|
532
|
+
* it should never repeat.
|
|
533
|
+
*/
|
|
534
|
+
class Poly1305 {
|
|
535
|
+
digestLength = DIGEST_LENGTH;
|
|
536
|
+
_buffer = new Uint8Array(16);
|
|
537
|
+
_r = new Uint16Array(10);
|
|
538
|
+
_h = new Uint16Array(10);
|
|
539
|
+
_pad = new Uint16Array(8);
|
|
540
|
+
_leftover = 0;
|
|
541
|
+
_fin = 0;
|
|
542
|
+
_finished = false;
|
|
543
|
+
constructor(key) {
|
|
544
|
+
let t0 = key[0] | key[1] << 8;
|
|
545
|
+
this._r[0] = (t0) & 0x1fff;
|
|
546
|
+
let t1 = key[2] | key[3] << 8;
|
|
547
|
+
this._r[1] = ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
|
|
548
|
+
let t2 = key[4] | key[5] << 8;
|
|
549
|
+
this._r[2] = ((t1 >>> 10) | (t2 << 6)) & 0x1f03;
|
|
550
|
+
let t3 = key[6] | key[7] << 8;
|
|
551
|
+
this._r[3] = ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
|
|
552
|
+
let t4 = key[8] | key[9] << 8;
|
|
553
|
+
this._r[4] = ((t3 >>> 4) | (t4 << 12)) & 0x00ff;
|
|
554
|
+
this._r[5] = ((t4 >>> 1)) & 0x1ffe;
|
|
555
|
+
let t5 = key[10] | key[11] << 8;
|
|
556
|
+
this._r[6] = ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
|
|
557
|
+
let t6 = key[12] | key[13] << 8;
|
|
558
|
+
this._r[7] = ((t5 >>> 11) | (t6 << 5)) & 0x1f81;
|
|
559
|
+
let t7 = key[14] | key[15] << 8;
|
|
560
|
+
this._r[8] = ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
|
|
561
|
+
this._r[9] = ((t7 >>> 5)) & 0x007f;
|
|
562
|
+
this._pad[0] = key[16] | key[17] << 8;
|
|
563
|
+
this._pad[1] = key[18] | key[19] << 8;
|
|
564
|
+
this._pad[2] = key[20] | key[21] << 8;
|
|
565
|
+
this._pad[3] = key[22] | key[23] << 8;
|
|
566
|
+
this._pad[4] = key[24] | key[25] << 8;
|
|
567
|
+
this._pad[5] = key[26] | key[27] << 8;
|
|
568
|
+
this._pad[6] = key[28] | key[29] << 8;
|
|
569
|
+
this._pad[7] = key[30] | key[31] << 8;
|
|
570
|
+
}
|
|
571
|
+
_blocks(m, mpos, bytes) {
|
|
572
|
+
let hibit = this._fin ? 0 : 1 << 11;
|
|
573
|
+
let h0 = this._h[0], h1 = this._h[1], h2 = this._h[2], h3 = this._h[3], h4 = this._h[4], h5 = this._h[5], h6 = this._h[6], h7 = this._h[7], h8 = this._h[8], h9 = this._h[9];
|
|
574
|
+
let r0 = this._r[0], r1 = this._r[1], r2 = this._r[2], r3 = this._r[3], r4 = this._r[4], r5 = this._r[5], r6 = this._r[6], r7 = this._r[7], r8 = this._r[8], r9 = this._r[9];
|
|
575
|
+
while (bytes >= 16) {
|
|
576
|
+
let t0 = m[mpos + 0] | m[mpos + 1] << 8;
|
|
577
|
+
h0 += (t0) & 0x1fff;
|
|
578
|
+
let t1 = m[mpos + 2] | m[mpos + 3] << 8;
|
|
579
|
+
h1 += ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
|
|
580
|
+
let t2 = m[mpos + 4] | m[mpos + 5] << 8;
|
|
581
|
+
h2 += ((t1 >>> 10) | (t2 << 6)) & 0x1fff;
|
|
582
|
+
let t3 = m[mpos + 6] | m[mpos + 7] << 8;
|
|
583
|
+
h3 += ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
|
|
584
|
+
let t4 = m[mpos + 8] | m[mpos + 9] << 8;
|
|
585
|
+
h4 += ((t3 >>> 4) | (t4 << 12)) & 0x1fff;
|
|
586
|
+
h5 += ((t4 >>> 1)) & 0x1fff;
|
|
587
|
+
let t5 = m[mpos + 10] | m[mpos + 11] << 8;
|
|
588
|
+
h6 += ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
|
|
589
|
+
let t6 = m[mpos + 12] | m[mpos + 13] << 8;
|
|
590
|
+
h7 += ((t5 >>> 11) | (t6 << 5)) & 0x1fff;
|
|
591
|
+
let t7 = m[mpos + 14] | m[mpos + 15] << 8;
|
|
592
|
+
h8 += ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
|
|
593
|
+
h9 += ((t7 >>> 5)) | hibit;
|
|
594
|
+
let c = 0;
|
|
595
|
+
let d0 = c;
|
|
596
|
+
d0 += h0 * r0;
|
|
597
|
+
d0 += h1 * (5 * r9);
|
|
598
|
+
d0 += h2 * (5 * r8);
|
|
599
|
+
d0 += h3 * (5 * r7);
|
|
600
|
+
d0 += h4 * (5 * r6);
|
|
601
|
+
c = (d0 >>> 13);
|
|
602
|
+
d0 &= 0x1fff;
|
|
603
|
+
d0 += h5 * (5 * r5);
|
|
604
|
+
d0 += h6 * (5 * r4);
|
|
605
|
+
d0 += h7 * (5 * r3);
|
|
606
|
+
d0 += h8 * (5 * r2);
|
|
607
|
+
d0 += h9 * (5 * r1);
|
|
608
|
+
c += (d0 >>> 13);
|
|
609
|
+
d0 &= 0x1fff;
|
|
610
|
+
let d1 = c;
|
|
611
|
+
d1 += h0 * r1;
|
|
612
|
+
d1 += h1 * r0;
|
|
613
|
+
d1 += h2 * (5 * r9);
|
|
614
|
+
d1 += h3 * (5 * r8);
|
|
615
|
+
d1 += h4 * (5 * r7);
|
|
616
|
+
c = (d1 >>> 13);
|
|
617
|
+
d1 &= 0x1fff;
|
|
618
|
+
d1 += h5 * (5 * r6);
|
|
619
|
+
d1 += h6 * (5 * r5);
|
|
620
|
+
d1 += h7 * (5 * r4);
|
|
621
|
+
d1 += h8 * (5 * r3);
|
|
622
|
+
d1 += h9 * (5 * r2);
|
|
623
|
+
c += (d1 >>> 13);
|
|
624
|
+
d1 &= 0x1fff;
|
|
625
|
+
let d2 = c;
|
|
626
|
+
d2 += h0 * r2;
|
|
627
|
+
d2 += h1 * r1;
|
|
628
|
+
d2 += h2 * r0;
|
|
629
|
+
d2 += h3 * (5 * r9);
|
|
630
|
+
d2 += h4 * (5 * r8);
|
|
631
|
+
c = (d2 >>> 13);
|
|
632
|
+
d2 &= 0x1fff;
|
|
633
|
+
d2 += h5 * (5 * r7);
|
|
634
|
+
d2 += h6 * (5 * r6);
|
|
635
|
+
d2 += h7 * (5 * r5);
|
|
636
|
+
d2 += h8 * (5 * r4);
|
|
637
|
+
d2 += h9 * (5 * r3);
|
|
638
|
+
c += (d2 >>> 13);
|
|
639
|
+
d2 &= 0x1fff;
|
|
640
|
+
let d3 = c;
|
|
641
|
+
d3 += h0 * r3;
|
|
642
|
+
d3 += h1 * r2;
|
|
643
|
+
d3 += h2 * r1;
|
|
644
|
+
d3 += h3 * r0;
|
|
645
|
+
d3 += h4 * (5 * r9);
|
|
646
|
+
c = (d3 >>> 13);
|
|
647
|
+
d3 &= 0x1fff;
|
|
648
|
+
d3 += h5 * (5 * r8);
|
|
649
|
+
d3 += h6 * (5 * r7);
|
|
650
|
+
d3 += h7 * (5 * r6);
|
|
651
|
+
d3 += h8 * (5 * r5);
|
|
652
|
+
d3 += h9 * (5 * r4);
|
|
653
|
+
c += (d3 >>> 13);
|
|
654
|
+
d3 &= 0x1fff;
|
|
655
|
+
let d4 = c;
|
|
656
|
+
d4 += h0 * r4;
|
|
657
|
+
d4 += h1 * r3;
|
|
658
|
+
d4 += h2 * r2;
|
|
659
|
+
d4 += h3 * r1;
|
|
660
|
+
d4 += h4 * r0;
|
|
661
|
+
c = (d4 >>> 13);
|
|
662
|
+
d4 &= 0x1fff;
|
|
663
|
+
d4 += h5 * (5 * r9);
|
|
664
|
+
d4 += h6 * (5 * r8);
|
|
665
|
+
d4 += h7 * (5 * r7);
|
|
666
|
+
d4 += h8 * (5 * r6);
|
|
667
|
+
d4 += h9 * (5 * r5);
|
|
668
|
+
c += (d4 >>> 13);
|
|
669
|
+
d4 &= 0x1fff;
|
|
670
|
+
let d5 = c;
|
|
671
|
+
d5 += h0 * r5;
|
|
672
|
+
d5 += h1 * r4;
|
|
673
|
+
d5 += h2 * r3;
|
|
674
|
+
d5 += h3 * r2;
|
|
675
|
+
d5 += h4 * r1;
|
|
676
|
+
c = (d5 >>> 13);
|
|
677
|
+
d5 &= 0x1fff;
|
|
678
|
+
d5 += h5 * r0;
|
|
679
|
+
d5 += h6 * (5 * r9);
|
|
680
|
+
d5 += h7 * (5 * r8);
|
|
681
|
+
d5 += h8 * (5 * r7);
|
|
682
|
+
d5 += h9 * (5 * r6);
|
|
683
|
+
c += (d5 >>> 13);
|
|
684
|
+
d5 &= 0x1fff;
|
|
685
|
+
let d6 = c;
|
|
686
|
+
d6 += h0 * r6;
|
|
687
|
+
d6 += h1 * r5;
|
|
688
|
+
d6 += h2 * r4;
|
|
689
|
+
d6 += h3 * r3;
|
|
690
|
+
d6 += h4 * r2;
|
|
691
|
+
c = (d6 >>> 13);
|
|
692
|
+
d6 &= 0x1fff;
|
|
693
|
+
d6 += h5 * r1;
|
|
694
|
+
d6 += h6 * r0;
|
|
695
|
+
d6 += h7 * (5 * r9);
|
|
696
|
+
d6 += h8 * (5 * r8);
|
|
697
|
+
d6 += h9 * (5 * r7);
|
|
698
|
+
c += (d6 >>> 13);
|
|
699
|
+
d6 &= 0x1fff;
|
|
700
|
+
let d7 = c;
|
|
701
|
+
d7 += h0 * r7;
|
|
702
|
+
d7 += h1 * r6;
|
|
703
|
+
d7 += h2 * r5;
|
|
704
|
+
d7 += h3 * r4;
|
|
705
|
+
d7 += h4 * r3;
|
|
706
|
+
c = (d7 >>> 13);
|
|
707
|
+
d7 &= 0x1fff;
|
|
708
|
+
d7 += h5 * r2;
|
|
709
|
+
d7 += h6 * r1;
|
|
710
|
+
d7 += h7 * r0;
|
|
711
|
+
d7 += h8 * (5 * r9);
|
|
712
|
+
d7 += h9 * (5 * r8);
|
|
713
|
+
c += (d7 >>> 13);
|
|
714
|
+
d7 &= 0x1fff;
|
|
715
|
+
let d8 = c;
|
|
716
|
+
d8 += h0 * r8;
|
|
717
|
+
d8 += h1 * r7;
|
|
718
|
+
d8 += h2 * r6;
|
|
719
|
+
d8 += h3 * r5;
|
|
720
|
+
d8 += h4 * r4;
|
|
721
|
+
c = (d8 >>> 13);
|
|
722
|
+
d8 &= 0x1fff;
|
|
723
|
+
d8 += h5 * r3;
|
|
724
|
+
d8 += h6 * r2;
|
|
725
|
+
d8 += h7 * r1;
|
|
726
|
+
d8 += h8 * r0;
|
|
727
|
+
d8 += h9 * (5 * r9);
|
|
728
|
+
c += (d8 >>> 13);
|
|
729
|
+
d8 &= 0x1fff;
|
|
730
|
+
let d9 = c;
|
|
731
|
+
d9 += h0 * r9;
|
|
732
|
+
d9 += h1 * r8;
|
|
733
|
+
d9 += h2 * r7;
|
|
734
|
+
d9 += h3 * r6;
|
|
735
|
+
d9 += h4 * r5;
|
|
736
|
+
c = (d9 >>> 13);
|
|
737
|
+
d9 &= 0x1fff;
|
|
738
|
+
d9 += h5 * r4;
|
|
739
|
+
d9 += h6 * r3;
|
|
740
|
+
d9 += h7 * r2;
|
|
741
|
+
d9 += h8 * r1;
|
|
742
|
+
d9 += h9 * r0;
|
|
743
|
+
c += (d9 >>> 13);
|
|
744
|
+
d9 &= 0x1fff;
|
|
745
|
+
c = (((c << 2) + c)) | 0;
|
|
746
|
+
c = (c + d0) | 0;
|
|
747
|
+
d0 = c & 0x1fff;
|
|
748
|
+
c = (c >>> 13);
|
|
749
|
+
d1 += c;
|
|
750
|
+
h0 = d0;
|
|
751
|
+
h1 = d1;
|
|
752
|
+
h2 = d2;
|
|
753
|
+
h3 = d3;
|
|
754
|
+
h4 = d4;
|
|
755
|
+
h5 = d5;
|
|
756
|
+
h6 = d6;
|
|
757
|
+
h7 = d7;
|
|
758
|
+
h8 = d8;
|
|
759
|
+
h9 = d9;
|
|
760
|
+
mpos += 16;
|
|
761
|
+
bytes -= 16;
|
|
762
|
+
}
|
|
763
|
+
this._h[0] = h0;
|
|
764
|
+
this._h[1] = h1;
|
|
765
|
+
this._h[2] = h2;
|
|
766
|
+
this._h[3] = h3;
|
|
767
|
+
this._h[4] = h4;
|
|
768
|
+
this._h[5] = h5;
|
|
769
|
+
this._h[6] = h6;
|
|
770
|
+
this._h[7] = h7;
|
|
771
|
+
this._h[8] = h8;
|
|
772
|
+
this._h[9] = h9;
|
|
773
|
+
}
|
|
774
|
+
finish(mac, macpos = 0) {
|
|
775
|
+
const g = new Uint16Array(10);
|
|
776
|
+
let c;
|
|
777
|
+
let mask;
|
|
778
|
+
let f;
|
|
779
|
+
let i;
|
|
780
|
+
if (this._leftover) {
|
|
781
|
+
i = this._leftover;
|
|
782
|
+
this._buffer[i++] = 1;
|
|
783
|
+
for (; i < 16; i++) {
|
|
784
|
+
this._buffer[i] = 0;
|
|
81
785
|
}
|
|
82
|
-
|
|
83
|
-
|
|
786
|
+
this._fin = 1;
|
|
787
|
+
this._blocks(this._buffer, 0, 16);
|
|
788
|
+
}
|
|
789
|
+
c = this._h[1] >>> 13;
|
|
790
|
+
this._h[1] &= 0x1fff;
|
|
791
|
+
for (i = 2; i < 10; i++) {
|
|
792
|
+
this._h[i] += c;
|
|
793
|
+
c = this._h[i] >>> 13;
|
|
794
|
+
this._h[i] &= 0x1fff;
|
|
795
|
+
}
|
|
796
|
+
this._h[0] += (c * 5);
|
|
797
|
+
c = this._h[0] >>> 13;
|
|
798
|
+
this._h[0] &= 0x1fff;
|
|
799
|
+
this._h[1] += c;
|
|
800
|
+
c = this._h[1] >>> 13;
|
|
801
|
+
this._h[1] &= 0x1fff;
|
|
802
|
+
this._h[2] += c;
|
|
803
|
+
g[0] = this._h[0] + 5;
|
|
804
|
+
c = g[0] >>> 13;
|
|
805
|
+
g[0] &= 0x1fff;
|
|
806
|
+
for (i = 1; i < 10; i++) {
|
|
807
|
+
g[i] = this._h[i] + c;
|
|
808
|
+
c = g[i] >>> 13;
|
|
809
|
+
g[i] &= 0x1fff;
|
|
810
|
+
}
|
|
811
|
+
g[9] -= (1 << 13);
|
|
812
|
+
mask = (c ^ 1) - 1;
|
|
813
|
+
for (i = 0; i < 10; i++) {
|
|
814
|
+
g[i] &= mask;
|
|
815
|
+
}
|
|
816
|
+
mask = ~mask;
|
|
817
|
+
for (i = 0; i < 10; i++) {
|
|
818
|
+
this._h[i] = (this._h[i] & mask) | g[i];
|
|
819
|
+
}
|
|
820
|
+
this._h[0] = ((this._h[0]) | (this._h[1] << 13)) & 0xffff;
|
|
821
|
+
this._h[1] = ((this._h[1] >>> 3) | (this._h[2] << 10)) & 0xffff;
|
|
822
|
+
this._h[2] = ((this._h[2] >>> 6) | (this._h[3] << 7)) & 0xffff;
|
|
823
|
+
this._h[3] = ((this._h[3] >>> 9) | (this._h[4] << 4)) & 0xffff;
|
|
824
|
+
this._h[4] = ((this._h[4] >>> 12) | (this._h[5] << 1) | (this._h[6] << 14)) & 0xffff;
|
|
825
|
+
this._h[5] = ((this._h[6] >>> 2) | (this._h[7] << 11)) & 0xffff;
|
|
826
|
+
this._h[6] = ((this._h[7] >>> 5) | (this._h[8] << 8)) & 0xffff;
|
|
827
|
+
this._h[7] = ((this._h[8] >>> 8) | (this._h[9] << 5)) & 0xffff;
|
|
828
|
+
f = this._h[0] + this._pad[0];
|
|
829
|
+
this._h[0] = f & 0xffff;
|
|
830
|
+
for (i = 1; i < 8; i++) {
|
|
831
|
+
f = (((this._h[i] + this._pad[i]) | 0) + (f >>> 16)) | 0;
|
|
832
|
+
this._h[i] = f & 0xffff;
|
|
833
|
+
}
|
|
834
|
+
mac[macpos + 0] = this._h[0] >>> 0;
|
|
835
|
+
mac[macpos + 1] = this._h[0] >>> 8;
|
|
836
|
+
mac[macpos + 2] = this._h[1] >>> 0;
|
|
837
|
+
mac[macpos + 3] = this._h[1] >>> 8;
|
|
838
|
+
mac[macpos + 4] = this._h[2] >>> 0;
|
|
839
|
+
mac[macpos + 5] = this._h[2] >>> 8;
|
|
840
|
+
mac[macpos + 6] = this._h[3] >>> 0;
|
|
841
|
+
mac[macpos + 7] = this._h[3] >>> 8;
|
|
842
|
+
mac[macpos + 8] = this._h[4] >>> 0;
|
|
843
|
+
mac[macpos + 9] = this._h[4] >>> 8;
|
|
844
|
+
mac[macpos + 10] = this._h[5] >>> 0;
|
|
845
|
+
mac[macpos + 11] = this._h[5] >>> 8;
|
|
846
|
+
mac[macpos + 12] = this._h[6] >>> 0;
|
|
847
|
+
mac[macpos + 13] = this._h[6] >>> 8;
|
|
848
|
+
mac[macpos + 14] = this._h[7] >>> 0;
|
|
849
|
+
mac[macpos + 15] = this._h[7] >>> 8;
|
|
850
|
+
this._finished = true;
|
|
851
|
+
return this;
|
|
84
852
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
853
|
+
update(m) {
|
|
854
|
+
let mpos = 0;
|
|
855
|
+
let bytes = m.length;
|
|
856
|
+
let want;
|
|
857
|
+
if (this._leftover) {
|
|
858
|
+
want = (16 - this._leftover);
|
|
859
|
+
if (want > bytes) {
|
|
860
|
+
want = bytes;
|
|
861
|
+
}
|
|
862
|
+
for (let i = 0; i < want; i++) {
|
|
863
|
+
this._buffer[this._leftover + i] = m[mpos + i];
|
|
864
|
+
}
|
|
865
|
+
bytes -= want;
|
|
866
|
+
mpos += want;
|
|
867
|
+
this._leftover += want;
|
|
868
|
+
if (this._leftover < 16) {
|
|
869
|
+
return this;
|
|
870
|
+
}
|
|
871
|
+
this._blocks(this._buffer, 0, 16);
|
|
872
|
+
this._leftover = 0;
|
|
88
873
|
}
|
|
89
|
-
|
|
90
|
-
|
|
874
|
+
if (bytes >= 16) {
|
|
875
|
+
want = bytes - (bytes % 16);
|
|
876
|
+
this._blocks(m, mpos, want);
|
|
877
|
+
mpos += want;
|
|
878
|
+
bytes -= want;
|
|
91
879
|
}
|
|
880
|
+
if (bytes) {
|
|
881
|
+
for (let i = 0; i < bytes; i++) {
|
|
882
|
+
this._buffer[this._leftover + i] = m[mpos + i];
|
|
883
|
+
}
|
|
884
|
+
this._leftover += bytes;
|
|
885
|
+
}
|
|
886
|
+
return this;
|
|
92
887
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
888
|
+
digest() {
|
|
889
|
+
// TODO(dchest): it behaves differently than other hashes/HMAC,
|
|
890
|
+
// because it throws when finished — others just return saved result.
|
|
891
|
+
if (this._finished) {
|
|
892
|
+
throw new Error("Poly1305 was finished");
|
|
96
893
|
}
|
|
97
|
-
|
|
98
|
-
|
|
894
|
+
let mac = new Uint8Array(16);
|
|
895
|
+
this.finish(mac);
|
|
896
|
+
return mac;
|
|
897
|
+
}
|
|
898
|
+
clean() {
|
|
899
|
+
wipe(this._buffer);
|
|
900
|
+
wipe(this._r);
|
|
901
|
+
wipe(this._h);
|
|
902
|
+
wipe(this._pad);
|
|
903
|
+
this._leftover = 0;
|
|
904
|
+
this._fin = 0;
|
|
905
|
+
this._finished = true; // mark as finished even if not
|
|
906
|
+
return this;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// Copyright (C) 2016 Dmitry Chestnykh
|
|
911
|
+
// MIT License. See LICENSE file for details.
|
|
912
|
+
const KEY_LENGTH$1 = 32;
|
|
913
|
+
const NONCE_LENGTH$1 = 12;
|
|
914
|
+
const TAG_LENGTH$1 = 16;
|
|
915
|
+
const ZEROS = new Uint8Array(16);
|
|
916
|
+
/**
|
|
917
|
+
* ChaCha20-Poly1305 Authenticated Encryption with Associated Data.
|
|
918
|
+
*
|
|
919
|
+
* Defined in RFC7539.
|
|
920
|
+
*/
|
|
921
|
+
class ChaCha20Poly1305 {
|
|
922
|
+
nonceLength = NONCE_LENGTH$1;
|
|
923
|
+
tagLength = TAG_LENGTH$1;
|
|
924
|
+
_key;
|
|
925
|
+
/**
|
|
926
|
+
* Creates a new instance with the given 32-byte key.
|
|
927
|
+
*/
|
|
928
|
+
constructor(key) {
|
|
929
|
+
if (key.length !== KEY_LENGTH$1) {
|
|
930
|
+
throw new Error("ChaCha20Poly1305 needs 32-byte key");
|
|
99
931
|
}
|
|
932
|
+
// Copy key.
|
|
933
|
+
this._key = new Uint8Array(key);
|
|
100
934
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
935
|
+
/**
|
|
936
|
+
* Encrypts and authenticates plaintext, authenticates associated data,
|
|
937
|
+
* and returns sealed ciphertext, which includes authentication tag.
|
|
938
|
+
*
|
|
939
|
+
* RFC7539 specifies 12 bytes for nonce. It may be this 12-byte nonce
|
|
940
|
+
* ("IV"), or full 16-byte counter (called "32-bit fixed-common part")
|
|
941
|
+
* and nonce.
|
|
942
|
+
*
|
|
943
|
+
* If dst is given (it must be the size of plaintext + the size of tag
|
|
944
|
+
* length) the result will be put into it. Dst and plaintext must not
|
|
945
|
+
* overlap.
|
|
946
|
+
*/
|
|
947
|
+
seal(nonce, plaintext, associatedData, dst) {
|
|
948
|
+
if (nonce.length > 16) {
|
|
949
|
+
throw new Error("ChaCha20Poly1305: incorrect nonce length");
|
|
104
950
|
}
|
|
105
|
-
|
|
106
|
-
|
|
951
|
+
// Allocate space for counter, and set nonce as last bytes of it.
|
|
952
|
+
const counter = new Uint8Array(16);
|
|
953
|
+
counter.set(nonce, counter.length - nonce.length);
|
|
954
|
+
// Generate authentication key by taking first 32-bytes of stream.
|
|
955
|
+
// We pass full counter, which has 12-byte nonce and 4-byte block counter,
|
|
956
|
+
// and it will get incremented after generating the block, which is
|
|
957
|
+
// exactly what we need: we only use the first 32 bytes of 64-byte
|
|
958
|
+
// ChaCha block and discard the next 32 bytes.
|
|
959
|
+
const authKey = new Uint8Array(32);
|
|
960
|
+
stream(this._key, counter, authKey, 4);
|
|
961
|
+
// Allocate space for sealed ciphertext.
|
|
962
|
+
const resultLength = plaintext.length + this.tagLength;
|
|
963
|
+
let result;
|
|
964
|
+
if (dst) {
|
|
965
|
+
if (dst.length !== resultLength) {
|
|
966
|
+
throw new Error("ChaCha20Poly1305: incorrect destination length");
|
|
967
|
+
}
|
|
968
|
+
result = dst;
|
|
969
|
+
}
|
|
970
|
+
else {
|
|
971
|
+
result = new Uint8Array(resultLength);
|
|
107
972
|
}
|
|
973
|
+
// Encrypt plaintext.
|
|
974
|
+
streamXOR(this._key, counter, plaintext, result, 4);
|
|
975
|
+
// Authenticate.
|
|
976
|
+
// XXX: can "simplify" here: pass full result (which is already padded
|
|
977
|
+
// due to zeroes prepared for tag), and ciphertext length instead of
|
|
978
|
+
// subarray of result.
|
|
979
|
+
this._authenticate(result.subarray(result.length - this.tagLength, result.length), authKey, result.subarray(0, result.length - this.tagLength), associatedData);
|
|
980
|
+
// Cleanup.
|
|
981
|
+
wipe(counter);
|
|
982
|
+
return result;
|
|
108
983
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
984
|
+
/**
|
|
985
|
+
* Authenticates sealed ciphertext (which includes authentication tag) and
|
|
986
|
+
* associated data, decrypts ciphertext and returns decrypted plaintext.
|
|
987
|
+
*
|
|
988
|
+
* RFC7539 specifies 12 bytes for nonce. It may be this 12-byte nonce
|
|
989
|
+
* ("IV"), or full 16-byte counter (called "32-bit fixed-common part")
|
|
990
|
+
* and nonce.
|
|
991
|
+
*
|
|
992
|
+
* If authentication fails, it returns null.
|
|
993
|
+
*
|
|
994
|
+
* If dst is given (it must be of ciphertext length minus tag length),
|
|
995
|
+
* the result will be put into it. Dst and plaintext must not overlap.
|
|
996
|
+
*/
|
|
997
|
+
open(nonce, sealed, associatedData, dst) {
|
|
998
|
+
if (nonce.length > 16) {
|
|
999
|
+
throw new Error("ChaCha20Poly1305: incorrect nonce length");
|
|
1000
|
+
}
|
|
1001
|
+
// Sealed ciphertext should at least contain tag.
|
|
1002
|
+
if (sealed.length < this.tagLength) {
|
|
1003
|
+
// TODO(dchest): should we throw here instead?
|
|
1004
|
+
return null;
|
|
1005
|
+
}
|
|
1006
|
+
// Allocate space for counter, and set nonce as last bytes of it.
|
|
1007
|
+
const counter = new Uint8Array(16);
|
|
1008
|
+
counter.set(nonce, counter.length - nonce.length);
|
|
1009
|
+
// Generate authentication key by taking first 32-bytes of stream.
|
|
1010
|
+
const authKey = new Uint8Array(32);
|
|
1011
|
+
stream(this._key, counter, authKey, 4);
|
|
1012
|
+
// Authenticate.
|
|
1013
|
+
// XXX: can simplify and avoid allocation: since authenticate()
|
|
1014
|
+
// already allocates tag (from Poly1305.digest(), it can return)
|
|
1015
|
+
// it instead of copying to calculatedTag. But then in seal()
|
|
1016
|
+
// we'll need to copy it.
|
|
1017
|
+
const calculatedTag = new Uint8Array(this.tagLength);
|
|
1018
|
+
this._authenticate(calculatedTag, authKey, sealed.subarray(0, sealed.length - this.tagLength), associatedData);
|
|
1019
|
+
// Constant-time compare tags and return null if they differ.
|
|
1020
|
+
if (!equal(calculatedTag, sealed.subarray(sealed.length - this.tagLength, sealed.length))) {
|
|
1021
|
+
return null;
|
|
1022
|
+
}
|
|
1023
|
+
// Allocate space for decrypted plaintext.
|
|
1024
|
+
const resultLength = sealed.length - this.tagLength;
|
|
1025
|
+
let result;
|
|
1026
|
+
if (dst) {
|
|
1027
|
+
if (dst.length !== resultLength) {
|
|
1028
|
+
throw new Error("ChaCha20Poly1305: incorrect destination length");
|
|
1029
|
+
}
|
|
1030
|
+
result = dst;
|
|
1031
|
+
}
|
|
1032
|
+
else {
|
|
1033
|
+
result = new Uint8Array(resultLength);
|
|
1034
|
+
}
|
|
1035
|
+
// Decrypt.
|
|
1036
|
+
streamXOR(this._key, counter, sealed.subarray(0, sealed.length - this.tagLength), result, 4);
|
|
1037
|
+
// Cleanup.
|
|
1038
|
+
wipe(counter);
|
|
1039
|
+
return result;
|
|
113
1040
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const extendedNavigator = navigator;
|
|
118
|
-
return extendedNavigator.systemLanguage || navigator.language || '';
|
|
1041
|
+
clean() {
|
|
1042
|
+
wipe(this._key);
|
|
1043
|
+
return this;
|
|
119
1044
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
1045
|
+
_authenticate(tagOut, authKey, ciphertext, associatedData) {
|
|
1046
|
+
// Initialize Poly1305 with authKey.
|
|
1047
|
+
const h = new Poly1305(authKey);
|
|
1048
|
+
// Authenticate padded associated data.
|
|
1049
|
+
if (associatedData) {
|
|
1050
|
+
h.update(associatedData);
|
|
1051
|
+
if (associatedData.length % 16 > 0) {
|
|
1052
|
+
h.update(ZEROS.subarray(associatedData.length % 16));
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
// Authenticate padded ciphertext.
|
|
1056
|
+
h.update(ciphertext);
|
|
1057
|
+
if (ciphertext.length % 16 > 0) {
|
|
1058
|
+
h.update(ZEROS.subarray(ciphertext.length % 16));
|
|
1059
|
+
}
|
|
1060
|
+
// Authenticate length of associated data.
|
|
1061
|
+
// XXX: can avoid allocation here?
|
|
1062
|
+
const length = new Uint8Array(8);
|
|
1063
|
+
if (associatedData) {
|
|
1064
|
+
writeUint64LE(associatedData.length, length);
|
|
1065
|
+
}
|
|
1066
|
+
h.update(length);
|
|
1067
|
+
// Authenticate length of ciphertext.
|
|
1068
|
+
writeUint64LE(ciphertext.length, length);
|
|
1069
|
+
h.update(length);
|
|
1070
|
+
// Get tag and copy it into tagOut.
|
|
1071
|
+
const tag = h.digest();
|
|
1072
|
+
for (let i = 0; i < tag.length; i++) {
|
|
1073
|
+
tagOut[i] = tag[i];
|
|
1074
|
+
}
|
|
1075
|
+
// Cleanup.
|
|
1076
|
+
h.clean();
|
|
1077
|
+
wipe(tag);
|
|
1078
|
+
wipe(length);
|
|
124
1079
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
// Copyright (C) 2019 Kyle Den Hartog
|
|
1083
|
+
// MIT License. See LICENSE file for details.
|
|
1084
|
+
const KEY_LENGTH = 32;
|
|
1085
|
+
const NONCE_LENGTH = 24;
|
|
1086
|
+
const TAG_LENGTH = 16;
|
|
1087
|
+
/**
|
|
1088
|
+
* XChaCha20-Poly1305 Authenticated Encryption with Associated Data.
|
|
1089
|
+
*
|
|
1090
|
+
* Defined in draft-irtf-cfrg-xchacha-01.
|
|
1091
|
+
* See https://tools.ietf.org/html/draft-irtf-cfrg-xchacha-01
|
|
1092
|
+
*/
|
|
1093
|
+
class XChaCha20Poly1305 {
|
|
1094
|
+
nonceLength = NONCE_LENGTH;
|
|
1095
|
+
tagLength = TAG_LENGTH;
|
|
1096
|
+
_key;
|
|
1097
|
+
/**
|
|
1098
|
+
* Creates a new instance with the given 32-byte key.
|
|
1099
|
+
*/
|
|
1100
|
+
constructor(key) {
|
|
1101
|
+
if (key.length !== KEY_LENGTH) {
|
|
1102
|
+
throw new Error("ChaCha20Poly1305 needs 32-byte key");
|
|
143
1103
|
}
|
|
144
|
-
|
|
145
|
-
|
|
1104
|
+
// Copy key.
|
|
1105
|
+
this._key = new Uint8Array(key);
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* Encrypts and authenticates plaintext, authenticates associated data,
|
|
1109
|
+
* and returns sealed ciphertext, which includes authentication tag.
|
|
1110
|
+
*
|
|
1111
|
+
* draft-irtf-cfrg-xchacha-01 defines a 24 byte nonce (192 bits) which
|
|
1112
|
+
* uses the first 16 bytes of the nonce and the secret key with
|
|
1113
|
+
* HChaCha to generate an initial subkey. The last 8 bytes of the nonce
|
|
1114
|
+
* are then prefixed with 4 zero bytes and then provided with the subkey
|
|
1115
|
+
* to the ChaCha20Poly1305 implementation.
|
|
1116
|
+
*
|
|
1117
|
+
* If dst is given (it must be the size of plaintext + the size of tag
|
|
1118
|
+
* length) the result will be put into it. Dst and plaintext must not
|
|
1119
|
+
* overlap.
|
|
1120
|
+
*/
|
|
1121
|
+
seal(nonce, plaintext, associatedData, dst) {
|
|
1122
|
+
if (nonce.length !== 24) {
|
|
1123
|
+
throw new Error("XChaCha20Poly1305: incorrect nonce length");
|
|
146
1124
|
}
|
|
1125
|
+
// Use HSalsa one-way function to transform first 16 bytes of
|
|
1126
|
+
// 24-byte extended nonce and key into a new key for Salsa
|
|
1127
|
+
// stream -- "subkey".
|
|
1128
|
+
const subKey = hchacha(this._key, nonce.subarray(0, 16), new Uint8Array(32));
|
|
1129
|
+
// Use last 8 bytes of 24-byte extended nonce as an actual nonce prefixed by 4 zero bytes,
|
|
1130
|
+
// and a subkey derived in the previous step as key to encrypt.
|
|
1131
|
+
const modifiedNonce = new Uint8Array(12);
|
|
1132
|
+
modifiedNonce.set(nonce.subarray(16), 4);
|
|
1133
|
+
const chaChaPoly = new ChaCha20Poly1305(subKey);
|
|
1134
|
+
const result = chaChaPoly.seal(modifiedNonce, plaintext, associatedData, dst);
|
|
1135
|
+
wipe(subKey);
|
|
1136
|
+
wipe(modifiedNonce);
|
|
1137
|
+
chaChaPoly.clean();
|
|
1138
|
+
return result;
|
|
147
1139
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
1140
|
+
/**
|
|
1141
|
+
* Authenticates sealed ciphertext (which includes authentication tag) and
|
|
1142
|
+
* associated data, decrypts ciphertext and returns decrypted plaintext.
|
|
1143
|
+
*
|
|
1144
|
+
* draft-irtf-cfrg-xchacha-01 defines a 24 byte nonce (192 bits) which
|
|
1145
|
+
* then uses the first 16 bytes of the nonce and the secret key with
|
|
1146
|
+
* Hchacha to generate an initial subkey. The last 8 bytes of the nonce
|
|
1147
|
+
* are then prefixed with 4 zero bytes and then provided with the subkey
|
|
1148
|
+
* to the chacha20poly1305 implementation.
|
|
1149
|
+
*
|
|
1150
|
+
* If authentication fails, it returns null.
|
|
1151
|
+
*
|
|
1152
|
+
* If dst is given (it must be the size of plaintext + the size of tag
|
|
1153
|
+
* length) the result will be put into it. Dst and plaintext must not
|
|
1154
|
+
* overlap.
|
|
1155
|
+
*/
|
|
1156
|
+
open(nonce, sealed, associatedData, dst) {
|
|
1157
|
+
if (nonce.length !== 24) {
|
|
1158
|
+
throw new Error("XChaCha20Poly1305: incorrect nonce length");
|
|
1159
|
+
}
|
|
1160
|
+
// Sealed ciphertext should at least contain tag.
|
|
1161
|
+
if (sealed.length < this.tagLength) {
|
|
1162
|
+
// TODO(dchest): should we throw here instead?
|
|
1163
|
+
return null;
|
|
1164
|
+
}
|
|
1165
|
+
/**
|
|
1166
|
+
* Generate subKey by using HChaCha20 function as defined
|
|
1167
|
+
* in section 2 step 1 of draft-irtf-cfrg-xchacha-01
|
|
1168
|
+
*/
|
|
1169
|
+
const subKey = hchacha(this._key, nonce.subarray(0, 16), new Uint8Array(32));
|
|
1170
|
+
/**
|
|
1171
|
+
* Generate Nonce as defined - remaining 8 bytes of the nonce prefixed with
|
|
1172
|
+
* 4 zero bytes
|
|
1173
|
+
*/
|
|
1174
|
+
const modifiedNonce = new Uint8Array(12);
|
|
1175
|
+
modifiedNonce.set(nonce.subarray(16), 4);
|
|
1176
|
+
/**
|
|
1177
|
+
* Authenticate and decrypt by calling into chacha20poly1305.
|
|
1178
|
+
*/
|
|
1179
|
+
const chaChaPoly = new ChaCha20Poly1305(subKey);
|
|
1180
|
+
const result = chaChaPoly.open(modifiedNonce, sealed, associatedData, dst);
|
|
1181
|
+
wipe(subKey);
|
|
1182
|
+
wipe(modifiedNonce);
|
|
1183
|
+
chaChaPoly.clean();
|
|
1184
|
+
return result;
|
|
1185
|
+
}
|
|
1186
|
+
clean() {
|
|
1187
|
+
wipe(this._key);
|
|
1188
|
+
return this;
|
|
152
1189
|
}
|
|
153
1190
|
}
|
|
154
1191
|
|
|
155
|
-
const ALGORITHM = '
|
|
1192
|
+
const ALGORITHM = 'XChaCha20-Poly1305';
|
|
156
1193
|
const KEY_BYTES = 32;
|
|
157
|
-
const NONCE_BYTES =
|
|
158
|
-
const
|
|
159
|
-
const
|
|
1194
|
+
const NONCE_BYTES = 24;
|
|
1195
|
+
const HEX_KEY_PATTERN = /^[0-9a-f]{64}$/i;
|
|
1196
|
+
const DEFAULT_SECRET_KEY = new Uint8Array([
|
|
1197
|
+
0x70, 0x61, 0x74, 0x68, 0x73, 0x63, 0x61, 0x6c,
|
|
1198
|
+
0x65, 0x2d, 0x73, 0x6c, 0x73, 0x2d, 0x63, 0x68,
|
|
1199
|
+
0x61, 0x63, 0x68, 0x61, 0x32, 0x30, 0x70, 0x6f,
|
|
1200
|
+
0x6c, 0x79, 0x31, 0x33, 0x30, 0x35, 0x6b, 0x31,
|
|
1201
|
+
]);
|
|
160
1202
|
/**
|
|
161
1203
|
* Encryption utility for secure data storage
|
|
162
1204
|
*/
|
|
163
1205
|
class EncryptionManager {
|
|
164
|
-
constructor(secretKey) {
|
|
1206
|
+
constructor(secretKey = '') {
|
|
165
1207
|
this.version = '2.0.0';
|
|
166
1208
|
this.textEncoder = new TextEncoder();
|
|
167
1209
|
this.textDecoder = new TextDecoder();
|
|
168
|
-
this.secretKey = this.
|
|
1210
|
+
this.secretKey = this.loadSecretKey(secretKey);
|
|
169
1211
|
}
|
|
170
1212
|
/**
|
|
171
1213
|
* Update the secret key
|
|
172
1214
|
*/
|
|
173
1215
|
updateSecretKey(newSecretKey) {
|
|
174
1216
|
this.secretKey.fill(0);
|
|
175
|
-
this.secretKey = this.
|
|
1217
|
+
this.secretKey = this.loadSecretKey(newSecretKey);
|
|
176
1218
|
}
|
|
177
1219
|
/**
|
|
178
1220
|
* Encrypt data with type preservation
|
|
@@ -185,9 +1227,11 @@ class EncryptionManager {
|
|
|
185
1227
|
version: this.version,
|
|
186
1228
|
};
|
|
187
1229
|
const serialized = JSON.stringify(item);
|
|
188
|
-
const nonce =
|
|
1230
|
+
const nonce = this.getRandomBytes(NONCE_BYTES);
|
|
189
1231
|
const plaintext = this.textEncoder.encode(serialized);
|
|
190
|
-
const
|
|
1232
|
+
const cipher = new XChaCha20Poly1305(this.secretKey);
|
|
1233
|
+
const ciphertext = cipher.seal(nonce, plaintext);
|
|
1234
|
+
cipher.clean();
|
|
191
1235
|
const envelope = {
|
|
192
1236
|
algorithm: ALGORITHM,
|
|
193
1237
|
nonce: this.encodeBase64(nonce),
|
|
@@ -207,7 +1251,12 @@ class EncryptionManager {
|
|
|
207
1251
|
}
|
|
208
1252
|
const nonce = this.decodeBase64(envelope.nonce);
|
|
209
1253
|
const ciphertext = this.decodeBase64(envelope.ciphertext);
|
|
210
|
-
const
|
|
1254
|
+
const cipher = new XChaCha20Poly1305(this.secretKey);
|
|
1255
|
+
const plaintext = cipher.open(nonce, ciphertext);
|
|
1256
|
+
cipher.clean();
|
|
1257
|
+
if (!plaintext) {
|
|
1258
|
+
throw new Error('Failed to authenticate encrypted data');
|
|
1259
|
+
}
|
|
211
1260
|
const decryptedText = this.textDecoder.decode(plaintext);
|
|
212
1261
|
const item = JSON.parse(decryptedText);
|
|
213
1262
|
return this.deserializeData(item.data, item.type);
|
|
@@ -225,7 +1274,11 @@ class EncryptionManager {
|
|
|
225
1274
|
const envelope = JSON.parse(encryptedData);
|
|
226
1275
|
if (!this.isChachaEnvelope(envelope))
|
|
227
1276
|
return false;
|
|
228
|
-
const
|
|
1277
|
+
const cipher = new XChaCha20Poly1305(this.secretKey);
|
|
1278
|
+
const plaintext = cipher.open(this.decodeBase64(envelope.nonce), this.decodeBase64(envelope.ciphertext));
|
|
1279
|
+
cipher.clean();
|
|
1280
|
+
if (!plaintext)
|
|
1281
|
+
return false;
|
|
229
1282
|
const item = JSON.parse(this.textDecoder.decode(plaintext));
|
|
230
1283
|
return item && typeof item.data !== 'undefined' && typeof item.type === 'string';
|
|
231
1284
|
}
|
|
@@ -233,11 +1286,29 @@ class EncryptionManager {
|
|
|
233
1286
|
return false;
|
|
234
1287
|
}
|
|
235
1288
|
}
|
|
236
|
-
|
|
237
|
-
return
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
1289
|
+
loadSecretKey(key) {
|
|
1290
|
+
return this.tryParseRawKey(key) || DEFAULT_SECRET_KEY.slice();
|
|
1291
|
+
}
|
|
1292
|
+
tryParseRawKey(key) {
|
|
1293
|
+
if (!key)
|
|
1294
|
+
return null;
|
|
1295
|
+
if (HEX_KEY_PATTERN.test(key)) {
|
|
1296
|
+
const bytes = new Uint8Array(KEY_BYTES);
|
|
1297
|
+
for (let index = 0; index < KEY_BYTES; index += 1) {
|
|
1298
|
+
bytes[index] = Number.parseInt(key.slice(index * 2, index * 2 + 2), 16);
|
|
1299
|
+
}
|
|
1300
|
+
return bytes;
|
|
1301
|
+
}
|
|
1302
|
+
try {
|
|
1303
|
+
const decoded = this.decodeBase64(key);
|
|
1304
|
+
if (decoded.length === KEY_BYTES)
|
|
1305
|
+
return decoded;
|
|
1306
|
+
}
|
|
1307
|
+
catch {
|
|
1308
|
+
// Not a base64 key; try raw UTF-8 below.
|
|
1309
|
+
}
|
|
1310
|
+
const raw = this.textEncoder.encode(key);
|
|
1311
|
+
return raw.length === KEY_BYTES ? raw : null;
|
|
241
1312
|
}
|
|
242
1313
|
serializeData(data) {
|
|
243
1314
|
if (data === null || data === undefined) {
|
|
@@ -317,6 +1388,15 @@ class EncryptionManager {
|
|
|
317
1388
|
getGlobalBuffer() {
|
|
318
1389
|
return globalThis.Buffer;
|
|
319
1390
|
}
|
|
1391
|
+
getRandomBytes(length) {
|
|
1392
|
+
const crypto = globalThis.crypto;
|
|
1393
|
+
if (!crypto?.getRandomValues) {
|
|
1394
|
+
throw new Error('No cryptographically secure random source available');
|
|
1395
|
+
}
|
|
1396
|
+
const bytes = new Uint8Array(length);
|
|
1397
|
+
crypto.getRandomValues(bytes);
|
|
1398
|
+
return bytes;
|
|
1399
|
+
}
|
|
320
1400
|
}
|
|
321
1401
|
|
|
322
1402
|
/**
|
|
@@ -402,7 +1482,6 @@ class SecureLocalStorage {
|
|
|
402
1482
|
disabledKeys: config.disabledKeys || envConfig.disabledKeys || [],
|
|
403
1483
|
debug: config.debug || envConfig.debug || false,
|
|
404
1484
|
};
|
|
405
|
-
this.fingerprinting = new BrowserFingerprinting(this.config.disabledKeys);
|
|
406
1485
|
this.storageEngine = this.getStorageEngine();
|
|
407
1486
|
this.prefix = this.config.prefix || 'sls_';
|
|
408
1487
|
this.memoryCache = new Map();
|
|
@@ -560,10 +1639,6 @@ class SecureLocalStorage {
|
|
|
560
1639
|
const newSecretKey = this.generateSecretKey();
|
|
561
1640
|
this.encryption.updateSecretKey(newSecretKey);
|
|
562
1641
|
}
|
|
563
|
-
// Update fingerprinting if disabled keys changed
|
|
564
|
-
if (oldConfig.disabledKeys !== this.config.disabledKeys) {
|
|
565
|
-
this.fingerprinting = new BrowserFingerprinting(this.config.disabledKeys);
|
|
566
|
-
}
|
|
567
1642
|
if (this.config.debug) {
|
|
568
1643
|
console.log('Configuration updated:', this.config);
|
|
569
1644
|
}
|
|
@@ -582,9 +1657,7 @@ class SecureLocalStorage {
|
|
|
582
1657
|
return 'secure-local-storage-default-key';
|
|
583
1658
|
}
|
|
584
1659
|
generateSecretKey() {
|
|
585
|
-
|
|
586
|
-
const fingerprintString = this.fingerprinting.fingerprintToString(fingerprint);
|
|
587
|
-
return `${this.config.hashKey}|${fingerprintString}`;
|
|
1660
|
+
return this.config.hashKey || this.generateDefaultHashKey();
|
|
588
1661
|
}
|
|
589
1662
|
initializeFromStorage() {
|
|
590
1663
|
try {
|
|
@@ -638,6 +1711,155 @@ class MemoryStorage {
|
|
|
638
1711
|
}
|
|
639
1712
|
}
|
|
640
1713
|
|
|
1714
|
+
/**
|
|
1715
|
+
* Browser fingerprinting utility for generating unique browser identifiers
|
|
1716
|
+
*/
|
|
1717
|
+
class BrowserFingerprinting {
|
|
1718
|
+
constructor(disabledKeys = []) {
|
|
1719
|
+
this.disabledKeys = new Set(disabledKeys);
|
|
1720
|
+
}
|
|
1721
|
+
/**
|
|
1722
|
+
* Generate a comprehensive browser fingerprint
|
|
1723
|
+
*/
|
|
1724
|
+
generateFingerprint() {
|
|
1725
|
+
return {
|
|
1726
|
+
userAgent: this.isEnabled('UserAgent') ? this.getUserAgent() : '',
|
|
1727
|
+
screenPrint: this.isEnabled('ScreenPrint') ? this.getScreenPrint() : '',
|
|
1728
|
+
plugins: this.isEnabled('Plugins') ? this.getPlugins() : '',
|
|
1729
|
+
fonts: this.isEnabled('Fonts') ? this.getFonts() : '',
|
|
1730
|
+
localStorage: this.isEnabled('LocalStorage') ? this.hasLocalStorage() : false,
|
|
1731
|
+
sessionStorage: this.isEnabled('SessionStorage') ? this.hasSessionStorage() : false,
|
|
1732
|
+
timeZone: this.isEnabled('TimeZone') ? this.getTimeZone() : '',
|
|
1733
|
+
language: this.isEnabled('Language') ? this.getLanguage() : '',
|
|
1734
|
+
systemLanguage: this.isEnabled('SystemLanguage') ? this.getSystemLanguage() : '',
|
|
1735
|
+
cookie: this.isEnabled('Cookie') ? this.hasCookieSupport() : false,
|
|
1736
|
+
canvas: this.isEnabled('Canvas') ? this.getCanvasFingerprint() : '',
|
|
1737
|
+
hostname: this.isEnabled('Hostname') ? this.getHostname() : '',
|
|
1738
|
+
};
|
|
1739
|
+
}
|
|
1740
|
+
/**
|
|
1741
|
+
* Convert fingerprint to a hash string
|
|
1742
|
+
*/
|
|
1743
|
+
fingerprintToString(fingerprint) {
|
|
1744
|
+
return Object.values(fingerprint)
|
|
1745
|
+
.map(value => String(value))
|
|
1746
|
+
.join('|');
|
|
1747
|
+
}
|
|
1748
|
+
isEnabled(property) {
|
|
1749
|
+
return !this.disabledKeys.has(property);
|
|
1750
|
+
}
|
|
1751
|
+
getUserAgent() {
|
|
1752
|
+
return typeof navigator !== 'undefined' ? navigator.userAgent : '';
|
|
1753
|
+
}
|
|
1754
|
+
getScreenPrint() {
|
|
1755
|
+
if (typeof screen === 'undefined')
|
|
1756
|
+
return '';
|
|
1757
|
+
return `${screen.width}x${screen.height}x${screen.colorDepth}`;
|
|
1758
|
+
}
|
|
1759
|
+
getPlugins() {
|
|
1760
|
+
if (typeof navigator === 'undefined' || !navigator.plugins)
|
|
1761
|
+
return '';
|
|
1762
|
+
const plugins = Array.from(navigator.plugins)
|
|
1763
|
+
.map(plugin => plugin.name)
|
|
1764
|
+
.sort()
|
|
1765
|
+
.join(',');
|
|
1766
|
+
return plugins;
|
|
1767
|
+
}
|
|
1768
|
+
getFonts() {
|
|
1769
|
+
// Basic font detection using canvas
|
|
1770
|
+
if (typeof document === 'undefined')
|
|
1771
|
+
return '';
|
|
1772
|
+
const fonts = [
|
|
1773
|
+
'Arial', 'Helvetica', 'Times New Roman', 'Times', 'Courier New', 'Courier',
|
|
1774
|
+
'Verdana', 'Georgia', 'Palatino', 'Garamond', 'Bookman', 'Comic Sans MS',
|
|
1775
|
+
'Trebuchet MS', 'Arial Black', 'Impact', 'Sans-serif', 'Serif', 'Monospace'
|
|
1776
|
+
];
|
|
1777
|
+
const availableFonts = [];
|
|
1778
|
+
const canvas = document.createElement('canvas');
|
|
1779
|
+
const context = canvas.getContext('2d');
|
|
1780
|
+
if (!context)
|
|
1781
|
+
return '';
|
|
1782
|
+
fonts.forEach(font => {
|
|
1783
|
+
context.font = `12px ${font}, monospace`;
|
|
1784
|
+
const width1 = context.measureText('mmmmmmmmmmlli').width;
|
|
1785
|
+
context.font = `12px ${font}, sans-serif`;
|
|
1786
|
+
const width2 = context.measureText('mmmmmmmmmmlli').width;
|
|
1787
|
+
if (width1 !== width2) {
|
|
1788
|
+
availableFonts.push(font);
|
|
1789
|
+
}
|
|
1790
|
+
});
|
|
1791
|
+
return availableFonts.join(',');
|
|
1792
|
+
}
|
|
1793
|
+
hasLocalStorage() {
|
|
1794
|
+
try {
|
|
1795
|
+
return typeof localStorage !== 'undefined';
|
|
1796
|
+
}
|
|
1797
|
+
catch {
|
|
1798
|
+
return false;
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
hasSessionStorage() {
|
|
1802
|
+
try {
|
|
1803
|
+
return typeof sessionStorage !== 'undefined';
|
|
1804
|
+
}
|
|
1805
|
+
catch {
|
|
1806
|
+
return false;
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
getTimeZone() {
|
|
1810
|
+
try {
|
|
1811
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
1812
|
+
}
|
|
1813
|
+
catch {
|
|
1814
|
+
return new Date().getTimezoneOffset().toString();
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
getLanguage() {
|
|
1818
|
+
if (typeof navigator === 'undefined')
|
|
1819
|
+
return '';
|
|
1820
|
+
return navigator.language || '';
|
|
1821
|
+
}
|
|
1822
|
+
getSystemLanguage() {
|
|
1823
|
+
if (typeof navigator === 'undefined')
|
|
1824
|
+
return '';
|
|
1825
|
+
const extendedNavigator = navigator;
|
|
1826
|
+
return extendedNavigator.systemLanguage || navigator.language || '';
|
|
1827
|
+
}
|
|
1828
|
+
hasCookieSupport() {
|
|
1829
|
+
if (typeof document === 'undefined')
|
|
1830
|
+
return false;
|
|
1831
|
+
return navigator.cookieEnabled;
|
|
1832
|
+
}
|
|
1833
|
+
getCanvasFingerprint() {
|
|
1834
|
+
if (typeof document === 'undefined')
|
|
1835
|
+
return '';
|
|
1836
|
+
try {
|
|
1837
|
+
const canvas = document.createElement('canvas');
|
|
1838
|
+
const ctx = canvas.getContext('2d');
|
|
1839
|
+
if (!ctx)
|
|
1840
|
+
return '';
|
|
1841
|
+
// Draw some text and shapes to create a unique canvas fingerprint
|
|
1842
|
+
ctx.textBaseline = 'top';
|
|
1843
|
+
ctx.font = '14px Arial';
|
|
1844
|
+
ctx.fillStyle = '#f60';
|
|
1845
|
+
ctx.fillRect(125, 1, 62, 20);
|
|
1846
|
+
ctx.fillStyle = '#069';
|
|
1847
|
+
ctx.fillText('SecureStorage 🔒', 2, 15);
|
|
1848
|
+
ctx.fillStyle = 'rgba(102, 204, 0, 0.7)';
|
|
1849
|
+
ctx.fillText('SecureStorage 🔒', 4, 17);
|
|
1850
|
+
return canvas.toDataURL();
|
|
1851
|
+
}
|
|
1852
|
+
catch {
|
|
1853
|
+
return '';
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
1856
|
+
getHostname() {
|
|
1857
|
+
if (typeof location === 'undefined')
|
|
1858
|
+
return '';
|
|
1859
|
+
return location.hostname;
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
|
|
641
1863
|
/**
|
|
642
1864
|
* Default secure local storage instance
|
|
643
1865
|
*/
|