@basmilius/apple-common 0.0.81 → 0.0.83
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/dist/crypto/chacha20.d.ts +1 -1
- package/dist/index.js +741 -13
- package/package.json +3 -3
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const CHACHA20_AUTH_TAG_LENGTH = 16;
|
|
2
2
|
export declare const CHACHA20_NONCE_LENGTH = 12;
|
|
3
|
-
export declare function decrypt(key: Buffer, nonce: Buffer,
|
|
3
|
+
export declare function decrypt(key: Buffer, nonce: Buffer, aad: Buffer | null, ciphertext: Buffer, authTag: Buffer): Buffer;
|
|
4
4
|
export declare function encrypt(key: Buffer, nonce: Buffer, aad: Buffer | null, plaintext: Buffer): EncryptedData;
|
|
5
5
|
export declare function padNonce(nonce: Buffer): Buffer;
|
|
6
6
|
export type EncryptedData = {
|
package/dist/index.js
CHANGED
|
@@ -23,25 +23,752 @@ __export(exports_chacha20, {
|
|
|
23
23
|
CHACHA20_NONCE_LENGTH: () => CHACHA20_NONCE_LENGTH,
|
|
24
24
|
CHACHA20_AUTH_TAG_LENGTH: () => CHACHA20_AUTH_TAG_LENGTH
|
|
25
25
|
});
|
|
26
|
-
|
|
26
|
+
|
|
27
|
+
// ../../node_modules/@stablelib/int/lib/int.js
|
|
28
|
+
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
|
|
29
|
+
|
|
30
|
+
// ../../node_modules/@stablelib/binary/lib/binary.js
|
|
31
|
+
function writeUint32LE(value, out = new Uint8Array(4), offset = 0) {
|
|
32
|
+
out[offset + 0] = value >>> 0;
|
|
33
|
+
out[offset + 1] = value >>> 8;
|
|
34
|
+
out[offset + 2] = value >>> 16;
|
|
35
|
+
out[offset + 3] = value >>> 24;
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
function writeUint64LE(value, out = new Uint8Array(8), offset = 0) {
|
|
39
|
+
writeUint32LE(value >>> 0, out, offset);
|
|
40
|
+
writeUint32LE(value / 4294967296 >>> 0, out, offset + 4);
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ../../node_modules/@stablelib/wipe/lib/wipe.js
|
|
45
|
+
function wipe(array) {
|
|
46
|
+
for (let i = 0;i < array.length; i++) {
|
|
47
|
+
array[i] = 0;
|
|
48
|
+
}
|
|
49
|
+
return array;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ../../node_modules/@stablelib/chacha/lib/chacha.js
|
|
53
|
+
var ROUNDS = 20;
|
|
54
|
+
function core(out, input, key) {
|
|
55
|
+
let j0 = 1634760805;
|
|
56
|
+
let j1 = 857760878;
|
|
57
|
+
let j2 = 2036477234;
|
|
58
|
+
let j3 = 1797285236;
|
|
59
|
+
let j4 = key[3] << 24 | key[2] << 16 | key[1] << 8 | key[0];
|
|
60
|
+
let j5 = key[7] << 24 | key[6] << 16 | key[5] << 8 | key[4];
|
|
61
|
+
let j6 = key[11] << 24 | key[10] << 16 | key[9] << 8 | key[8];
|
|
62
|
+
let j7 = key[15] << 24 | key[14] << 16 | key[13] << 8 | key[12];
|
|
63
|
+
let j8 = key[19] << 24 | key[18] << 16 | key[17] << 8 | key[16];
|
|
64
|
+
let j9 = key[23] << 24 | key[22] << 16 | key[21] << 8 | key[20];
|
|
65
|
+
let j10 = key[27] << 24 | key[26] << 16 | key[25] << 8 | key[24];
|
|
66
|
+
let j11 = key[31] << 24 | key[30] << 16 | key[29] << 8 | key[28];
|
|
67
|
+
let j12 = input[3] << 24 | input[2] << 16 | input[1] << 8 | input[0];
|
|
68
|
+
let j13 = input[7] << 24 | input[6] << 16 | input[5] << 8 | input[4];
|
|
69
|
+
let j14 = input[11] << 24 | input[10] << 16 | input[9] << 8 | input[8];
|
|
70
|
+
let j15 = input[15] << 24 | input[14] << 16 | input[13] << 8 | input[12];
|
|
71
|
+
let x0 = j0;
|
|
72
|
+
let x1 = j1;
|
|
73
|
+
let x2 = j2;
|
|
74
|
+
let x3 = j3;
|
|
75
|
+
let x4 = j4;
|
|
76
|
+
let x5 = j5;
|
|
77
|
+
let x6 = j6;
|
|
78
|
+
let x7 = j7;
|
|
79
|
+
let x8 = j8;
|
|
80
|
+
let x9 = j9;
|
|
81
|
+
let x10 = j10;
|
|
82
|
+
let x11 = j11;
|
|
83
|
+
let x12 = j12;
|
|
84
|
+
let x13 = j13;
|
|
85
|
+
let x14 = j14;
|
|
86
|
+
let x15 = j15;
|
|
87
|
+
for (let i = 0;i < ROUNDS; i += 2) {
|
|
88
|
+
x0 = x0 + x4 | 0;
|
|
89
|
+
x12 ^= x0;
|
|
90
|
+
x12 = x12 >>> 32 - 16 | x12 << 16;
|
|
91
|
+
x8 = x8 + x12 | 0;
|
|
92
|
+
x4 ^= x8;
|
|
93
|
+
x4 = x4 >>> 32 - 12 | x4 << 12;
|
|
94
|
+
x1 = x1 + x5 | 0;
|
|
95
|
+
x13 ^= x1;
|
|
96
|
+
x13 = x13 >>> 32 - 16 | x13 << 16;
|
|
97
|
+
x9 = x9 + x13 | 0;
|
|
98
|
+
x5 ^= x9;
|
|
99
|
+
x5 = x5 >>> 32 - 12 | x5 << 12;
|
|
100
|
+
x2 = x2 + x6 | 0;
|
|
101
|
+
x14 ^= x2;
|
|
102
|
+
x14 = x14 >>> 32 - 16 | x14 << 16;
|
|
103
|
+
x10 = x10 + x14 | 0;
|
|
104
|
+
x6 ^= x10;
|
|
105
|
+
x6 = x6 >>> 32 - 12 | x6 << 12;
|
|
106
|
+
x3 = x3 + x7 | 0;
|
|
107
|
+
x15 ^= x3;
|
|
108
|
+
x15 = x15 >>> 32 - 16 | x15 << 16;
|
|
109
|
+
x11 = x11 + x15 | 0;
|
|
110
|
+
x7 ^= x11;
|
|
111
|
+
x7 = x7 >>> 32 - 12 | x7 << 12;
|
|
112
|
+
x2 = x2 + x6 | 0;
|
|
113
|
+
x14 ^= x2;
|
|
114
|
+
x14 = x14 >>> 32 - 8 | x14 << 8;
|
|
115
|
+
x10 = x10 + x14 | 0;
|
|
116
|
+
x6 ^= x10;
|
|
117
|
+
x6 = x6 >>> 32 - 7 | x6 << 7;
|
|
118
|
+
x3 = x3 + x7 | 0;
|
|
119
|
+
x15 ^= x3;
|
|
120
|
+
x15 = x15 >>> 32 - 8 | x15 << 8;
|
|
121
|
+
x11 = x11 + x15 | 0;
|
|
122
|
+
x7 ^= x11;
|
|
123
|
+
x7 = x7 >>> 32 - 7 | x7 << 7;
|
|
124
|
+
x1 = x1 + x5 | 0;
|
|
125
|
+
x13 ^= x1;
|
|
126
|
+
x13 = x13 >>> 32 - 8 | x13 << 8;
|
|
127
|
+
x9 = x9 + x13 | 0;
|
|
128
|
+
x5 ^= x9;
|
|
129
|
+
x5 = x5 >>> 32 - 7 | x5 << 7;
|
|
130
|
+
x0 = x0 + x4 | 0;
|
|
131
|
+
x12 ^= x0;
|
|
132
|
+
x12 = x12 >>> 32 - 8 | x12 << 8;
|
|
133
|
+
x8 = x8 + x12 | 0;
|
|
134
|
+
x4 ^= x8;
|
|
135
|
+
x4 = x4 >>> 32 - 7 | x4 << 7;
|
|
136
|
+
x0 = x0 + x5 | 0;
|
|
137
|
+
x15 ^= x0;
|
|
138
|
+
x15 = x15 >>> 32 - 16 | x15 << 16;
|
|
139
|
+
x10 = x10 + x15 | 0;
|
|
140
|
+
x5 ^= x10;
|
|
141
|
+
x5 = x5 >>> 32 - 12 | x5 << 12;
|
|
142
|
+
x1 = x1 + x6 | 0;
|
|
143
|
+
x12 ^= x1;
|
|
144
|
+
x12 = x12 >>> 32 - 16 | x12 << 16;
|
|
145
|
+
x11 = x11 + x12 | 0;
|
|
146
|
+
x6 ^= x11;
|
|
147
|
+
x6 = x6 >>> 32 - 12 | x6 << 12;
|
|
148
|
+
x2 = x2 + x7 | 0;
|
|
149
|
+
x13 ^= x2;
|
|
150
|
+
x13 = x13 >>> 32 - 16 | x13 << 16;
|
|
151
|
+
x8 = x8 + x13 | 0;
|
|
152
|
+
x7 ^= x8;
|
|
153
|
+
x7 = x7 >>> 32 - 12 | x7 << 12;
|
|
154
|
+
x3 = x3 + x4 | 0;
|
|
155
|
+
x14 ^= x3;
|
|
156
|
+
x14 = x14 >>> 32 - 16 | x14 << 16;
|
|
157
|
+
x9 = x9 + x14 | 0;
|
|
158
|
+
x4 ^= x9;
|
|
159
|
+
x4 = x4 >>> 32 - 12 | x4 << 12;
|
|
160
|
+
x2 = x2 + x7 | 0;
|
|
161
|
+
x13 ^= x2;
|
|
162
|
+
x13 = x13 >>> 32 - 8 | x13 << 8;
|
|
163
|
+
x8 = x8 + x13 | 0;
|
|
164
|
+
x7 ^= x8;
|
|
165
|
+
x7 = x7 >>> 32 - 7 | x7 << 7;
|
|
166
|
+
x3 = x3 + x4 | 0;
|
|
167
|
+
x14 ^= x3;
|
|
168
|
+
x14 = x14 >>> 32 - 8 | x14 << 8;
|
|
169
|
+
x9 = x9 + x14 | 0;
|
|
170
|
+
x4 ^= x9;
|
|
171
|
+
x4 = x4 >>> 32 - 7 | x4 << 7;
|
|
172
|
+
x1 = x1 + x6 | 0;
|
|
173
|
+
x12 ^= x1;
|
|
174
|
+
x12 = x12 >>> 32 - 8 | x12 << 8;
|
|
175
|
+
x11 = x11 + x12 | 0;
|
|
176
|
+
x6 ^= x11;
|
|
177
|
+
x6 = x6 >>> 32 - 7 | x6 << 7;
|
|
178
|
+
x0 = x0 + x5 | 0;
|
|
179
|
+
x15 ^= x0;
|
|
180
|
+
x15 = x15 >>> 32 - 8 | x15 << 8;
|
|
181
|
+
x10 = x10 + x15 | 0;
|
|
182
|
+
x5 ^= x10;
|
|
183
|
+
x5 = x5 >>> 32 - 7 | x5 << 7;
|
|
184
|
+
}
|
|
185
|
+
writeUint32LE(x0 + j0 | 0, out, 0);
|
|
186
|
+
writeUint32LE(x1 + j1 | 0, out, 4);
|
|
187
|
+
writeUint32LE(x2 + j2 | 0, out, 8);
|
|
188
|
+
writeUint32LE(x3 + j3 | 0, out, 12);
|
|
189
|
+
writeUint32LE(x4 + j4 | 0, out, 16);
|
|
190
|
+
writeUint32LE(x5 + j5 | 0, out, 20);
|
|
191
|
+
writeUint32LE(x6 + j6 | 0, out, 24);
|
|
192
|
+
writeUint32LE(x7 + j7 | 0, out, 28);
|
|
193
|
+
writeUint32LE(x8 + j8 | 0, out, 32);
|
|
194
|
+
writeUint32LE(x9 + j9 | 0, out, 36);
|
|
195
|
+
writeUint32LE(x10 + j10 | 0, out, 40);
|
|
196
|
+
writeUint32LE(x11 + j11 | 0, out, 44);
|
|
197
|
+
writeUint32LE(x12 + j12 | 0, out, 48);
|
|
198
|
+
writeUint32LE(x13 + j13 | 0, out, 52);
|
|
199
|
+
writeUint32LE(x14 + j14 | 0, out, 56);
|
|
200
|
+
writeUint32LE(x15 + j15 | 0, out, 60);
|
|
201
|
+
}
|
|
202
|
+
function streamXOR(key, nonce, src, dst, nonceInplaceCounterLength = 0) {
|
|
203
|
+
if (key.length !== 32) {
|
|
204
|
+
throw new Error("ChaCha: key size must be 32 bytes");
|
|
205
|
+
}
|
|
206
|
+
if (dst.length < src.length) {
|
|
207
|
+
throw new Error("ChaCha: destination is shorter than source");
|
|
208
|
+
}
|
|
209
|
+
let nc;
|
|
210
|
+
let counterLength;
|
|
211
|
+
if (nonceInplaceCounterLength === 0) {
|
|
212
|
+
if (nonce.length !== 8 && nonce.length !== 12) {
|
|
213
|
+
throw new Error("ChaCha nonce must be 8 or 12 bytes");
|
|
214
|
+
}
|
|
215
|
+
nc = new Uint8Array(16);
|
|
216
|
+
counterLength = nc.length - nonce.length;
|
|
217
|
+
nc.set(nonce, counterLength);
|
|
218
|
+
} else {
|
|
219
|
+
if (nonce.length !== 16) {
|
|
220
|
+
throw new Error("ChaCha nonce with counter must be 16 bytes");
|
|
221
|
+
}
|
|
222
|
+
nc = nonce;
|
|
223
|
+
counterLength = nonceInplaceCounterLength;
|
|
224
|
+
}
|
|
225
|
+
const block = new Uint8Array(64);
|
|
226
|
+
for (let i = 0;i < src.length; i += 64) {
|
|
227
|
+
core(block, nc, key);
|
|
228
|
+
for (let j = i;j < i + 64 && j < src.length; j++) {
|
|
229
|
+
dst[j] = src[j] ^ block[j - i];
|
|
230
|
+
}
|
|
231
|
+
incrementCounter(nc, 0, counterLength);
|
|
232
|
+
}
|
|
233
|
+
wipe(block);
|
|
234
|
+
if (nonceInplaceCounterLength === 0) {
|
|
235
|
+
wipe(nc);
|
|
236
|
+
}
|
|
237
|
+
return dst;
|
|
238
|
+
}
|
|
239
|
+
function stream(key, nonce, dst, nonceInplaceCounterLength = 0) {
|
|
240
|
+
wipe(dst);
|
|
241
|
+
return streamXOR(key, nonce, dst, dst, nonceInplaceCounterLength);
|
|
242
|
+
}
|
|
243
|
+
function incrementCounter(counter, pos, len) {
|
|
244
|
+
let carry = 1;
|
|
245
|
+
while (len--) {
|
|
246
|
+
carry = carry + (counter[pos] & 255) | 0;
|
|
247
|
+
counter[pos] = carry & 255;
|
|
248
|
+
carry >>>= 8;
|
|
249
|
+
pos++;
|
|
250
|
+
}
|
|
251
|
+
if (carry > 0) {
|
|
252
|
+
throw new Error("ChaCha: counter overflow");
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// ../../node_modules/@stablelib/constant-time/lib/constant-time.js
|
|
257
|
+
function compare(a, b) {
|
|
258
|
+
if (a.length !== b.length) {
|
|
259
|
+
return 0;
|
|
260
|
+
}
|
|
261
|
+
let result = 0;
|
|
262
|
+
for (let i = 0;i < a.length; i++) {
|
|
263
|
+
result |= a[i] ^ b[i];
|
|
264
|
+
}
|
|
265
|
+
return 1 & result - 1 >>> 8;
|
|
266
|
+
}
|
|
267
|
+
function equal(a, b) {
|
|
268
|
+
if (a.length === 0 || b.length === 0) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
return compare(a, b) !== 0;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ../../node_modules/@stablelib/poly1305/lib/poly1305.js
|
|
275
|
+
var DIGEST_LENGTH = 16;
|
|
276
|
+
|
|
277
|
+
class Poly1305 {
|
|
278
|
+
digestLength = DIGEST_LENGTH;
|
|
279
|
+
_buffer = new Uint8Array(16);
|
|
280
|
+
_r = new Uint16Array(10);
|
|
281
|
+
_h = new Uint16Array(10);
|
|
282
|
+
_pad = new Uint16Array(8);
|
|
283
|
+
_leftover = 0;
|
|
284
|
+
_fin = 0;
|
|
285
|
+
_finished = false;
|
|
286
|
+
constructor(key) {
|
|
287
|
+
let t0 = key[0] | key[1] << 8;
|
|
288
|
+
this._r[0] = t0 & 8191;
|
|
289
|
+
let t1 = key[2] | key[3] << 8;
|
|
290
|
+
this._r[1] = (t0 >>> 13 | t1 << 3) & 8191;
|
|
291
|
+
let t2 = key[4] | key[5] << 8;
|
|
292
|
+
this._r[2] = (t1 >>> 10 | t2 << 6) & 7939;
|
|
293
|
+
let t3 = key[6] | key[7] << 8;
|
|
294
|
+
this._r[3] = (t2 >>> 7 | t3 << 9) & 8191;
|
|
295
|
+
let t4 = key[8] | key[9] << 8;
|
|
296
|
+
this._r[4] = (t3 >>> 4 | t4 << 12) & 255;
|
|
297
|
+
this._r[5] = t4 >>> 1 & 8190;
|
|
298
|
+
let t5 = key[10] | key[11] << 8;
|
|
299
|
+
this._r[6] = (t4 >>> 14 | t5 << 2) & 8191;
|
|
300
|
+
let t6 = key[12] | key[13] << 8;
|
|
301
|
+
this._r[7] = (t5 >>> 11 | t6 << 5) & 8065;
|
|
302
|
+
let t7 = key[14] | key[15] << 8;
|
|
303
|
+
this._r[8] = (t6 >>> 8 | t7 << 8) & 8191;
|
|
304
|
+
this._r[9] = t7 >>> 5 & 127;
|
|
305
|
+
this._pad[0] = key[16] | key[17] << 8;
|
|
306
|
+
this._pad[1] = key[18] | key[19] << 8;
|
|
307
|
+
this._pad[2] = key[20] | key[21] << 8;
|
|
308
|
+
this._pad[3] = key[22] | key[23] << 8;
|
|
309
|
+
this._pad[4] = key[24] | key[25] << 8;
|
|
310
|
+
this._pad[5] = key[26] | key[27] << 8;
|
|
311
|
+
this._pad[6] = key[28] | key[29] << 8;
|
|
312
|
+
this._pad[7] = key[30] | key[31] << 8;
|
|
313
|
+
}
|
|
314
|
+
_blocks(m, mpos, bytes) {
|
|
315
|
+
let hibit = this._fin ? 0 : 1 << 11;
|
|
316
|
+
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];
|
|
317
|
+
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];
|
|
318
|
+
while (bytes >= 16) {
|
|
319
|
+
let t0 = m[mpos + 0] | m[mpos + 1] << 8;
|
|
320
|
+
h0 += t0 & 8191;
|
|
321
|
+
let t1 = m[mpos + 2] | m[mpos + 3] << 8;
|
|
322
|
+
h1 += (t0 >>> 13 | t1 << 3) & 8191;
|
|
323
|
+
let t2 = m[mpos + 4] | m[mpos + 5] << 8;
|
|
324
|
+
h2 += (t1 >>> 10 | t2 << 6) & 8191;
|
|
325
|
+
let t3 = m[mpos + 6] | m[mpos + 7] << 8;
|
|
326
|
+
h3 += (t2 >>> 7 | t3 << 9) & 8191;
|
|
327
|
+
let t4 = m[mpos + 8] | m[mpos + 9] << 8;
|
|
328
|
+
h4 += (t3 >>> 4 | t4 << 12) & 8191;
|
|
329
|
+
h5 += t4 >>> 1 & 8191;
|
|
330
|
+
let t5 = m[mpos + 10] | m[mpos + 11] << 8;
|
|
331
|
+
h6 += (t4 >>> 14 | t5 << 2) & 8191;
|
|
332
|
+
let t6 = m[mpos + 12] | m[mpos + 13] << 8;
|
|
333
|
+
h7 += (t5 >>> 11 | t6 << 5) & 8191;
|
|
334
|
+
let t7 = m[mpos + 14] | m[mpos + 15] << 8;
|
|
335
|
+
h8 += (t6 >>> 8 | t7 << 8) & 8191;
|
|
336
|
+
h9 += t7 >>> 5 | hibit;
|
|
337
|
+
let c = 0;
|
|
338
|
+
let d0 = c;
|
|
339
|
+
d0 += h0 * r0;
|
|
340
|
+
d0 += h1 * (5 * r9);
|
|
341
|
+
d0 += h2 * (5 * r8);
|
|
342
|
+
d0 += h3 * (5 * r7);
|
|
343
|
+
d0 += h4 * (5 * r6);
|
|
344
|
+
c = d0 >>> 13;
|
|
345
|
+
d0 &= 8191;
|
|
346
|
+
d0 += h5 * (5 * r5);
|
|
347
|
+
d0 += h6 * (5 * r4);
|
|
348
|
+
d0 += h7 * (5 * r3);
|
|
349
|
+
d0 += h8 * (5 * r2);
|
|
350
|
+
d0 += h9 * (5 * r1);
|
|
351
|
+
c += d0 >>> 13;
|
|
352
|
+
d0 &= 8191;
|
|
353
|
+
let d1 = c;
|
|
354
|
+
d1 += h0 * r1;
|
|
355
|
+
d1 += h1 * r0;
|
|
356
|
+
d1 += h2 * (5 * r9);
|
|
357
|
+
d1 += h3 * (5 * r8);
|
|
358
|
+
d1 += h4 * (5 * r7);
|
|
359
|
+
c = d1 >>> 13;
|
|
360
|
+
d1 &= 8191;
|
|
361
|
+
d1 += h5 * (5 * r6);
|
|
362
|
+
d1 += h6 * (5 * r5);
|
|
363
|
+
d1 += h7 * (5 * r4);
|
|
364
|
+
d1 += h8 * (5 * r3);
|
|
365
|
+
d1 += h9 * (5 * r2);
|
|
366
|
+
c += d1 >>> 13;
|
|
367
|
+
d1 &= 8191;
|
|
368
|
+
let d2 = c;
|
|
369
|
+
d2 += h0 * r2;
|
|
370
|
+
d2 += h1 * r1;
|
|
371
|
+
d2 += h2 * r0;
|
|
372
|
+
d2 += h3 * (5 * r9);
|
|
373
|
+
d2 += h4 * (5 * r8);
|
|
374
|
+
c = d2 >>> 13;
|
|
375
|
+
d2 &= 8191;
|
|
376
|
+
d2 += h5 * (5 * r7);
|
|
377
|
+
d2 += h6 * (5 * r6);
|
|
378
|
+
d2 += h7 * (5 * r5);
|
|
379
|
+
d2 += h8 * (5 * r4);
|
|
380
|
+
d2 += h9 * (5 * r3);
|
|
381
|
+
c += d2 >>> 13;
|
|
382
|
+
d2 &= 8191;
|
|
383
|
+
let d3 = c;
|
|
384
|
+
d3 += h0 * r3;
|
|
385
|
+
d3 += h1 * r2;
|
|
386
|
+
d3 += h2 * r1;
|
|
387
|
+
d3 += h3 * r0;
|
|
388
|
+
d3 += h4 * (5 * r9);
|
|
389
|
+
c = d3 >>> 13;
|
|
390
|
+
d3 &= 8191;
|
|
391
|
+
d3 += h5 * (5 * r8);
|
|
392
|
+
d3 += h6 * (5 * r7);
|
|
393
|
+
d3 += h7 * (5 * r6);
|
|
394
|
+
d3 += h8 * (5 * r5);
|
|
395
|
+
d3 += h9 * (5 * r4);
|
|
396
|
+
c += d3 >>> 13;
|
|
397
|
+
d3 &= 8191;
|
|
398
|
+
let d4 = c;
|
|
399
|
+
d4 += h0 * r4;
|
|
400
|
+
d4 += h1 * r3;
|
|
401
|
+
d4 += h2 * r2;
|
|
402
|
+
d4 += h3 * r1;
|
|
403
|
+
d4 += h4 * r0;
|
|
404
|
+
c = d4 >>> 13;
|
|
405
|
+
d4 &= 8191;
|
|
406
|
+
d4 += h5 * (5 * r9);
|
|
407
|
+
d4 += h6 * (5 * r8);
|
|
408
|
+
d4 += h7 * (5 * r7);
|
|
409
|
+
d4 += h8 * (5 * r6);
|
|
410
|
+
d4 += h9 * (5 * r5);
|
|
411
|
+
c += d4 >>> 13;
|
|
412
|
+
d4 &= 8191;
|
|
413
|
+
let d5 = c;
|
|
414
|
+
d5 += h0 * r5;
|
|
415
|
+
d5 += h1 * r4;
|
|
416
|
+
d5 += h2 * r3;
|
|
417
|
+
d5 += h3 * r2;
|
|
418
|
+
d5 += h4 * r1;
|
|
419
|
+
c = d5 >>> 13;
|
|
420
|
+
d5 &= 8191;
|
|
421
|
+
d5 += h5 * r0;
|
|
422
|
+
d5 += h6 * (5 * r9);
|
|
423
|
+
d5 += h7 * (5 * r8);
|
|
424
|
+
d5 += h8 * (5 * r7);
|
|
425
|
+
d5 += h9 * (5 * r6);
|
|
426
|
+
c += d5 >>> 13;
|
|
427
|
+
d5 &= 8191;
|
|
428
|
+
let d6 = c;
|
|
429
|
+
d6 += h0 * r6;
|
|
430
|
+
d6 += h1 * r5;
|
|
431
|
+
d6 += h2 * r4;
|
|
432
|
+
d6 += h3 * r3;
|
|
433
|
+
d6 += h4 * r2;
|
|
434
|
+
c = d6 >>> 13;
|
|
435
|
+
d6 &= 8191;
|
|
436
|
+
d6 += h5 * r1;
|
|
437
|
+
d6 += h6 * r0;
|
|
438
|
+
d6 += h7 * (5 * r9);
|
|
439
|
+
d6 += h8 * (5 * r8);
|
|
440
|
+
d6 += h9 * (5 * r7);
|
|
441
|
+
c += d6 >>> 13;
|
|
442
|
+
d6 &= 8191;
|
|
443
|
+
let d7 = c;
|
|
444
|
+
d7 += h0 * r7;
|
|
445
|
+
d7 += h1 * r6;
|
|
446
|
+
d7 += h2 * r5;
|
|
447
|
+
d7 += h3 * r4;
|
|
448
|
+
d7 += h4 * r3;
|
|
449
|
+
c = d7 >>> 13;
|
|
450
|
+
d7 &= 8191;
|
|
451
|
+
d7 += h5 * r2;
|
|
452
|
+
d7 += h6 * r1;
|
|
453
|
+
d7 += h7 * r0;
|
|
454
|
+
d7 += h8 * (5 * r9);
|
|
455
|
+
d7 += h9 * (5 * r8);
|
|
456
|
+
c += d7 >>> 13;
|
|
457
|
+
d7 &= 8191;
|
|
458
|
+
let d8 = c;
|
|
459
|
+
d8 += h0 * r8;
|
|
460
|
+
d8 += h1 * r7;
|
|
461
|
+
d8 += h2 * r6;
|
|
462
|
+
d8 += h3 * r5;
|
|
463
|
+
d8 += h4 * r4;
|
|
464
|
+
c = d8 >>> 13;
|
|
465
|
+
d8 &= 8191;
|
|
466
|
+
d8 += h5 * r3;
|
|
467
|
+
d8 += h6 * r2;
|
|
468
|
+
d8 += h7 * r1;
|
|
469
|
+
d8 += h8 * r0;
|
|
470
|
+
d8 += h9 * (5 * r9);
|
|
471
|
+
c += d8 >>> 13;
|
|
472
|
+
d8 &= 8191;
|
|
473
|
+
let d9 = c;
|
|
474
|
+
d9 += h0 * r9;
|
|
475
|
+
d9 += h1 * r8;
|
|
476
|
+
d9 += h2 * r7;
|
|
477
|
+
d9 += h3 * r6;
|
|
478
|
+
d9 += h4 * r5;
|
|
479
|
+
c = d9 >>> 13;
|
|
480
|
+
d9 &= 8191;
|
|
481
|
+
d9 += h5 * r4;
|
|
482
|
+
d9 += h6 * r3;
|
|
483
|
+
d9 += h7 * r2;
|
|
484
|
+
d9 += h8 * r1;
|
|
485
|
+
d9 += h9 * r0;
|
|
486
|
+
c += d9 >>> 13;
|
|
487
|
+
d9 &= 8191;
|
|
488
|
+
c = (c << 2) + c | 0;
|
|
489
|
+
c = c + d0 | 0;
|
|
490
|
+
d0 = c & 8191;
|
|
491
|
+
c = c >>> 13;
|
|
492
|
+
d1 += c;
|
|
493
|
+
h0 = d0;
|
|
494
|
+
h1 = d1;
|
|
495
|
+
h2 = d2;
|
|
496
|
+
h3 = d3;
|
|
497
|
+
h4 = d4;
|
|
498
|
+
h5 = d5;
|
|
499
|
+
h6 = d6;
|
|
500
|
+
h7 = d7;
|
|
501
|
+
h8 = d8;
|
|
502
|
+
h9 = d9;
|
|
503
|
+
mpos += 16;
|
|
504
|
+
bytes -= 16;
|
|
505
|
+
}
|
|
506
|
+
this._h[0] = h0;
|
|
507
|
+
this._h[1] = h1;
|
|
508
|
+
this._h[2] = h2;
|
|
509
|
+
this._h[3] = h3;
|
|
510
|
+
this._h[4] = h4;
|
|
511
|
+
this._h[5] = h5;
|
|
512
|
+
this._h[6] = h6;
|
|
513
|
+
this._h[7] = h7;
|
|
514
|
+
this._h[8] = h8;
|
|
515
|
+
this._h[9] = h9;
|
|
516
|
+
}
|
|
517
|
+
finish(mac, macpos = 0) {
|
|
518
|
+
const g = new Uint16Array(10);
|
|
519
|
+
let c;
|
|
520
|
+
let mask;
|
|
521
|
+
let f;
|
|
522
|
+
let i;
|
|
523
|
+
if (this._leftover) {
|
|
524
|
+
i = this._leftover;
|
|
525
|
+
this._buffer[i++] = 1;
|
|
526
|
+
for (;i < 16; i++) {
|
|
527
|
+
this._buffer[i] = 0;
|
|
528
|
+
}
|
|
529
|
+
this._fin = 1;
|
|
530
|
+
this._blocks(this._buffer, 0, 16);
|
|
531
|
+
}
|
|
532
|
+
c = this._h[1] >>> 13;
|
|
533
|
+
this._h[1] &= 8191;
|
|
534
|
+
for (i = 2;i < 10; i++) {
|
|
535
|
+
this._h[i] += c;
|
|
536
|
+
c = this._h[i] >>> 13;
|
|
537
|
+
this._h[i] &= 8191;
|
|
538
|
+
}
|
|
539
|
+
this._h[0] += c * 5;
|
|
540
|
+
c = this._h[0] >>> 13;
|
|
541
|
+
this._h[0] &= 8191;
|
|
542
|
+
this._h[1] += c;
|
|
543
|
+
c = this._h[1] >>> 13;
|
|
544
|
+
this._h[1] &= 8191;
|
|
545
|
+
this._h[2] += c;
|
|
546
|
+
g[0] = this._h[0] + 5;
|
|
547
|
+
c = g[0] >>> 13;
|
|
548
|
+
g[0] &= 8191;
|
|
549
|
+
for (i = 1;i < 10; i++) {
|
|
550
|
+
g[i] = this._h[i] + c;
|
|
551
|
+
c = g[i] >>> 13;
|
|
552
|
+
g[i] &= 8191;
|
|
553
|
+
}
|
|
554
|
+
g[9] -= 1 << 13;
|
|
555
|
+
mask = (c ^ 1) - 1;
|
|
556
|
+
for (i = 0;i < 10; i++) {
|
|
557
|
+
g[i] &= mask;
|
|
558
|
+
}
|
|
559
|
+
mask = ~mask;
|
|
560
|
+
for (i = 0;i < 10; i++) {
|
|
561
|
+
this._h[i] = this._h[i] & mask | g[i];
|
|
562
|
+
}
|
|
563
|
+
this._h[0] = (this._h[0] | this._h[1] << 13) & 65535;
|
|
564
|
+
this._h[1] = (this._h[1] >>> 3 | this._h[2] << 10) & 65535;
|
|
565
|
+
this._h[2] = (this._h[2] >>> 6 | this._h[3] << 7) & 65535;
|
|
566
|
+
this._h[3] = (this._h[3] >>> 9 | this._h[4] << 4) & 65535;
|
|
567
|
+
this._h[4] = (this._h[4] >>> 12 | this._h[5] << 1 | this._h[6] << 14) & 65535;
|
|
568
|
+
this._h[5] = (this._h[6] >>> 2 | this._h[7] << 11) & 65535;
|
|
569
|
+
this._h[6] = (this._h[7] >>> 5 | this._h[8] << 8) & 65535;
|
|
570
|
+
this._h[7] = (this._h[8] >>> 8 | this._h[9] << 5) & 65535;
|
|
571
|
+
f = this._h[0] + this._pad[0];
|
|
572
|
+
this._h[0] = f & 65535;
|
|
573
|
+
for (i = 1;i < 8; i++) {
|
|
574
|
+
f = (this._h[i] + this._pad[i] | 0) + (f >>> 16) | 0;
|
|
575
|
+
this._h[i] = f & 65535;
|
|
576
|
+
}
|
|
577
|
+
mac[macpos + 0] = this._h[0] >>> 0;
|
|
578
|
+
mac[macpos + 1] = this._h[0] >>> 8;
|
|
579
|
+
mac[macpos + 2] = this._h[1] >>> 0;
|
|
580
|
+
mac[macpos + 3] = this._h[1] >>> 8;
|
|
581
|
+
mac[macpos + 4] = this._h[2] >>> 0;
|
|
582
|
+
mac[macpos + 5] = this._h[2] >>> 8;
|
|
583
|
+
mac[macpos + 6] = this._h[3] >>> 0;
|
|
584
|
+
mac[macpos + 7] = this._h[3] >>> 8;
|
|
585
|
+
mac[macpos + 8] = this._h[4] >>> 0;
|
|
586
|
+
mac[macpos + 9] = this._h[4] >>> 8;
|
|
587
|
+
mac[macpos + 10] = this._h[5] >>> 0;
|
|
588
|
+
mac[macpos + 11] = this._h[5] >>> 8;
|
|
589
|
+
mac[macpos + 12] = this._h[6] >>> 0;
|
|
590
|
+
mac[macpos + 13] = this._h[6] >>> 8;
|
|
591
|
+
mac[macpos + 14] = this._h[7] >>> 0;
|
|
592
|
+
mac[macpos + 15] = this._h[7] >>> 8;
|
|
593
|
+
this._finished = true;
|
|
594
|
+
return this;
|
|
595
|
+
}
|
|
596
|
+
update(m) {
|
|
597
|
+
let mpos = 0;
|
|
598
|
+
let bytes = m.length;
|
|
599
|
+
let want;
|
|
600
|
+
if (this._leftover) {
|
|
601
|
+
want = 16 - this._leftover;
|
|
602
|
+
if (want > bytes) {
|
|
603
|
+
want = bytes;
|
|
604
|
+
}
|
|
605
|
+
for (let i = 0;i < want; i++) {
|
|
606
|
+
this._buffer[this._leftover + i] = m[mpos + i];
|
|
607
|
+
}
|
|
608
|
+
bytes -= want;
|
|
609
|
+
mpos += want;
|
|
610
|
+
this._leftover += want;
|
|
611
|
+
if (this._leftover < 16) {
|
|
612
|
+
return this;
|
|
613
|
+
}
|
|
614
|
+
this._blocks(this._buffer, 0, 16);
|
|
615
|
+
this._leftover = 0;
|
|
616
|
+
}
|
|
617
|
+
if (bytes >= 16) {
|
|
618
|
+
want = bytes - bytes % 16;
|
|
619
|
+
this._blocks(m, mpos, want);
|
|
620
|
+
mpos += want;
|
|
621
|
+
bytes -= want;
|
|
622
|
+
}
|
|
623
|
+
if (bytes) {
|
|
624
|
+
for (let i = 0;i < bytes; i++) {
|
|
625
|
+
this._buffer[this._leftover + i] = m[mpos + i];
|
|
626
|
+
}
|
|
627
|
+
this._leftover += bytes;
|
|
628
|
+
}
|
|
629
|
+
return this;
|
|
630
|
+
}
|
|
631
|
+
digest() {
|
|
632
|
+
if (this._finished) {
|
|
633
|
+
throw new Error("Poly1305 was finished");
|
|
634
|
+
}
|
|
635
|
+
let mac = new Uint8Array(16);
|
|
636
|
+
this.finish(mac);
|
|
637
|
+
return mac;
|
|
638
|
+
}
|
|
639
|
+
clean() {
|
|
640
|
+
wipe(this._buffer);
|
|
641
|
+
wipe(this._r);
|
|
642
|
+
wipe(this._h);
|
|
643
|
+
wipe(this._pad);
|
|
644
|
+
this._leftover = 0;
|
|
645
|
+
this._fin = 0;
|
|
646
|
+
this._finished = true;
|
|
647
|
+
return this;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
// ../../node_modules/@stablelib/chacha20poly1305/lib/chacha20poly1305.js
|
|
652
|
+
var KEY_LENGTH = 32;
|
|
653
|
+
var NONCE_LENGTH = 12;
|
|
654
|
+
var TAG_LENGTH = 16;
|
|
655
|
+
var ZEROS = new Uint8Array(16);
|
|
656
|
+
|
|
657
|
+
class ChaCha20Poly1305 {
|
|
658
|
+
nonceLength = NONCE_LENGTH;
|
|
659
|
+
tagLength = TAG_LENGTH;
|
|
660
|
+
_key;
|
|
661
|
+
constructor(key) {
|
|
662
|
+
if (key.length !== KEY_LENGTH) {
|
|
663
|
+
throw new Error("ChaCha20Poly1305 needs 32-byte key");
|
|
664
|
+
}
|
|
665
|
+
this._key = new Uint8Array(key);
|
|
666
|
+
}
|
|
667
|
+
seal(nonce, plaintext, associatedData, dst) {
|
|
668
|
+
if (nonce.length > 16) {
|
|
669
|
+
throw new Error("ChaCha20Poly1305: incorrect nonce length");
|
|
670
|
+
}
|
|
671
|
+
const counter = new Uint8Array(16);
|
|
672
|
+
counter.set(nonce, counter.length - nonce.length);
|
|
673
|
+
const authKey = new Uint8Array(32);
|
|
674
|
+
stream(this._key, counter, authKey, 4);
|
|
675
|
+
const resultLength = plaintext.length + this.tagLength;
|
|
676
|
+
let result;
|
|
677
|
+
if (dst) {
|
|
678
|
+
if (dst.length !== resultLength) {
|
|
679
|
+
throw new Error("ChaCha20Poly1305: incorrect destination length");
|
|
680
|
+
}
|
|
681
|
+
result = dst;
|
|
682
|
+
} else {
|
|
683
|
+
result = new Uint8Array(resultLength);
|
|
684
|
+
}
|
|
685
|
+
streamXOR(this._key, counter, plaintext, result, 4);
|
|
686
|
+
this._authenticate(result.subarray(result.length - this.tagLength, result.length), authKey, result.subarray(0, result.length - this.tagLength), associatedData);
|
|
687
|
+
wipe(counter);
|
|
688
|
+
return result;
|
|
689
|
+
}
|
|
690
|
+
open(nonce, sealed, associatedData, dst) {
|
|
691
|
+
if (nonce.length > 16) {
|
|
692
|
+
throw new Error("ChaCha20Poly1305: incorrect nonce length");
|
|
693
|
+
}
|
|
694
|
+
if (sealed.length < this.tagLength) {
|
|
695
|
+
return null;
|
|
696
|
+
}
|
|
697
|
+
const counter = new Uint8Array(16);
|
|
698
|
+
counter.set(nonce, counter.length - nonce.length);
|
|
699
|
+
const authKey = new Uint8Array(32);
|
|
700
|
+
stream(this._key, counter, authKey, 4);
|
|
701
|
+
const calculatedTag = new Uint8Array(this.tagLength);
|
|
702
|
+
this._authenticate(calculatedTag, authKey, sealed.subarray(0, sealed.length - this.tagLength), associatedData);
|
|
703
|
+
if (!equal(calculatedTag, sealed.subarray(sealed.length - this.tagLength, sealed.length))) {
|
|
704
|
+
return null;
|
|
705
|
+
}
|
|
706
|
+
const resultLength = sealed.length - this.tagLength;
|
|
707
|
+
let result;
|
|
708
|
+
if (dst) {
|
|
709
|
+
if (dst.length !== resultLength) {
|
|
710
|
+
throw new Error("ChaCha20Poly1305: incorrect destination length");
|
|
711
|
+
}
|
|
712
|
+
result = dst;
|
|
713
|
+
} else {
|
|
714
|
+
result = new Uint8Array(resultLength);
|
|
715
|
+
}
|
|
716
|
+
streamXOR(this._key, counter, sealed.subarray(0, sealed.length - this.tagLength), result, 4);
|
|
717
|
+
wipe(counter);
|
|
718
|
+
return result;
|
|
719
|
+
}
|
|
720
|
+
clean() {
|
|
721
|
+
wipe(this._key);
|
|
722
|
+
return this;
|
|
723
|
+
}
|
|
724
|
+
_authenticate(tagOut, authKey, ciphertext, associatedData) {
|
|
725
|
+
const h = new Poly1305(authKey);
|
|
726
|
+
if (associatedData) {
|
|
727
|
+
h.update(associatedData);
|
|
728
|
+
if (associatedData.length % 16 > 0) {
|
|
729
|
+
h.update(ZEROS.subarray(associatedData.length % 16));
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
h.update(ciphertext);
|
|
733
|
+
if (ciphertext.length % 16 > 0) {
|
|
734
|
+
h.update(ZEROS.subarray(ciphertext.length % 16));
|
|
735
|
+
}
|
|
736
|
+
const length = new Uint8Array(8);
|
|
737
|
+
if (associatedData) {
|
|
738
|
+
writeUint64LE(associatedData.length, length);
|
|
739
|
+
}
|
|
740
|
+
h.update(length);
|
|
741
|
+
writeUint64LE(ciphertext.length, length);
|
|
742
|
+
h.update(length);
|
|
743
|
+
const tag = h.digest();
|
|
744
|
+
for (let i = 0;i < tag.length; i++) {
|
|
745
|
+
tagOut[i] = tag[i];
|
|
746
|
+
}
|
|
747
|
+
h.clean();
|
|
748
|
+
wipe(tag);
|
|
749
|
+
wipe(length);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
// src/crypto/chacha20.ts
|
|
27
754
|
var CHACHA20_AUTH_TAG_LENGTH = 16;
|
|
28
755
|
var CHACHA20_NONCE_LENGTH = 12;
|
|
29
|
-
function decrypt(key, nonce,
|
|
756
|
+
function decrypt(key, nonce, aad, ciphertext, authTag) {
|
|
30
757
|
nonce = padNonce(nonce);
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
758
|
+
const chacha = new ChaCha20Poly1305(key);
|
|
759
|
+
const sealed = Buffer.concat([ciphertext, authTag]);
|
|
760
|
+
const plaintext = chacha.open(nonce, sealed, aad ?? undefined);
|
|
761
|
+
if (!plaintext) {
|
|
762
|
+
throw new Error("Decryption failed: authentication tag mismatch");
|
|
763
|
+
}
|
|
764
|
+
return Buffer.from(plaintext);
|
|
37
765
|
}
|
|
38
766
|
function encrypt(key, nonce, aad, plaintext) {
|
|
39
767
|
nonce = padNonce(nonce);
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
const ciphertext =
|
|
43
|
-
|
|
44
|
-
const authTag = cipher.getAuthTag();
|
|
768
|
+
const chacha = new ChaCha20Poly1305(key);
|
|
769
|
+
const sealed = chacha.seal(nonce, plaintext, aad ?? undefined);
|
|
770
|
+
const ciphertext = Buffer.from(sealed.subarray(0, sealed.length - CHACHA20_AUTH_TAG_LENGTH));
|
|
771
|
+
const authTag = Buffer.from(sealed.subarray(sealed.length - CHACHA20_AUTH_TAG_LENGTH));
|
|
45
772
|
return {
|
|
46
773
|
ciphertext,
|
|
47
774
|
authTag
|
|
@@ -417,6 +1144,7 @@ class Connection extends EventEmitter {
|
|
|
417
1144
|
const wasConnected = this.#state === "connected";
|
|
418
1145
|
if (this.#state !== "closing") {
|
|
419
1146
|
this.#state = "disconnected";
|
|
1147
|
+
reporter.net(`Connection closed (${hadError ? "with error" : "normally"}).`);
|
|
420
1148
|
}
|
|
421
1149
|
this.emit("close", hadError);
|
|
422
1150
|
if (wasConnected && this.#retryEnabled && hadError) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basmilius/apple-common",
|
|
3
3
|
"description": "Common features shared across various apple protocol packages.",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.83",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": {
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@basmilius/apple-encoding": "0.0.
|
|
42
|
-
"
|
|
41
|
+
"@basmilius/apple-encoding": "0.0.83",
|
|
42
|
+
"@stablelib/chacha20poly1305": "^2.0.1",
|
|
43
43
|
"fast-srp-hap": "^2.0.4",
|
|
44
44
|
"node-dns-sd": "^1.0.1",
|
|
45
45
|
"tweetnacl": "^1.0.3",
|