@basmilius/apple-common 0.5.2 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/index.d.ts +1 -3
- package/dist/index.js +97 -863
- package/dist/utils.d.ts +2 -0
- package/package.json +8 -8
- package/dist/crypto/chacha20.d.ts +0 -9
- package/dist/crypto/curve25519.d.ts +0 -7
- package/dist/crypto/hkdf.d.ts +0 -8
- package/dist/crypto/index.d.ts +0 -3
- package/dist/net/getLocalIP.d.ts +0 -1
- package/dist/net/getMacAddress.d.ts +0 -1
- package/dist/net/index.d.ts +0 -2
package/LICENSE
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
export { v4 as uuid } from "uuid";
|
|
2
|
-
export { Chacha20, Curve25519, hkdf } from "./crypto";
|
|
3
2
|
export { Discovery } from "./discovery";
|
|
4
|
-
export { getLocalIP, getMacAddress } from "./net";
|
|
5
3
|
export { prompt, waitFor, cli } from "./cli";
|
|
6
4
|
export { Connection, EncryptionAwareConnection, EncryptionState } from "./connection";
|
|
7
5
|
export { Context } from "./context";
|
|
@@ -10,5 +8,5 @@ export { type AccessoryCredentials, type AccessoryKeys, AccessoryPair, Accessory
|
|
|
10
8
|
export { type Logger, type Reporter, reporter } from "./reporter";
|
|
11
9
|
export { ENCRYPTION } from "./symbols";
|
|
12
10
|
export { TimingServer } from "./timing";
|
|
13
|
-
export { randomInt32, randomInt64, uint16ToBE, uint53ToLE } from "./utils";
|
|
11
|
+
export { getLocalIP, getMacAddress, randomInt32, randomInt64, uint16ToBE, uint53ToLE } from "./utils";
|
|
14
12
|
export type { ConnectionState, DiscoveryResult, EventMap } from "./types";
|
package/dist/index.js
CHANGED
|
@@ -1,810 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
get: all[name],
|
|
6
|
-
enumerable: true,
|
|
7
|
-
configurable: true,
|
|
8
|
-
set: (newValue) => all[name] = () => newValue
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
// src/index.ts
|
|
13
|
-
import {
|
|
14
|
-
v4
|
|
15
|
-
} from "uuid";
|
|
16
|
-
|
|
17
|
-
// src/crypto/chacha20.ts
|
|
18
|
-
var exports_chacha20 = {};
|
|
19
|
-
__export(exports_chacha20, {
|
|
20
|
-
padNonce: () => padNonce,
|
|
21
|
-
encrypt: () => encrypt,
|
|
22
|
-
decrypt: () => decrypt,
|
|
23
|
-
CHACHA20_NONCE_LENGTH: () => CHACHA20_NONCE_LENGTH,
|
|
24
|
-
CHACHA20_AUTH_TAG_LENGTH: () => CHACHA20_AUTH_TAG_LENGTH
|
|
25
|
-
});
|
|
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;
|
|
1
|
+
// ../../node_modules/uuid/dist-node/stringify.js
|
|
2
|
+
var byteToHex = [];
|
|
3
|
+
for (let i = 0;i < 256; ++i) {
|
|
4
|
+
byteToHex.push((i + 256).toString(16).slice(1));
|
|
238
5
|
}
|
|
239
|
-
function
|
|
240
|
-
|
|
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
|
-
}
|
|
6
|
+
function unsafeStringify(arr, offset = 0) {
|
|
7
|
+
return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
|
|
254
8
|
}
|
|
255
9
|
|
|
256
|
-
// ../../node_modules
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
10
|
+
// ../../node_modules/uuid/dist-node/rng.js
|
|
11
|
+
import { randomFillSync } from "node:crypto";
|
|
12
|
+
var rnds8Pool = new Uint8Array(256);
|
|
13
|
+
var poolPtr = rnds8Pool.length;
|
|
14
|
+
function rng() {
|
|
15
|
+
if (poolPtr > rnds8Pool.length - 16) {
|
|
16
|
+
randomFillSync(rnds8Pool);
|
|
17
|
+
poolPtr = 0;
|
|
264
18
|
}
|
|
265
|
-
return
|
|
266
|
-
}
|
|
267
|
-
function equal(a, b) {
|
|
268
|
-
if (a.length === 0 || b.length === 0) {
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
271
|
-
return compare(a, b) !== 0;
|
|
19
|
+
return rnds8Pool.slice(poolPtr, poolPtr += 16);
|
|
272
20
|
}
|
|
273
21
|
|
|
274
|
-
// ../../node_modules
|
|
275
|
-
|
|
22
|
+
// ../../node_modules/uuid/dist-node/native.js
|
|
23
|
+
import { randomUUID } from "node:crypto";
|
|
24
|
+
var native_default = { randomUUID };
|
|
276
25
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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;
|
|
26
|
+
// ../../node_modules/uuid/dist-node/v4.js
|
|
27
|
+
function _v4(options, buf, offset) {
|
|
28
|
+
options = options || {};
|
|
29
|
+
const rnds = options.random ?? options.rng?.() ?? rng();
|
|
30
|
+
if (rnds.length < 16) {
|
|
31
|
+
throw new Error("Random bytes length must be >= 16");
|
|
595
32
|
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
if (
|
|
601
|
-
|
|
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;
|
|
33
|
+
rnds[6] = rnds[6] & 15 | 64;
|
|
34
|
+
rnds[8] = rnds[8] & 63 | 128;
|
|
35
|
+
if (buf) {
|
|
36
|
+
offset = offset || 0;
|
|
37
|
+
if (offset < 0 || offset + 16 > buf.length) {
|
|
38
|
+
throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
|
|
616
39
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
this._blocks(m, mpos, want);
|
|
620
|
-
mpos += want;
|
|
621
|
-
bytes -= want;
|
|
40
|
+
for (let i = 0;i < 16; ++i) {
|
|
41
|
+
buf[offset + i] = rnds[i];
|
|
622
42
|
}
|
|
623
|
-
|
|
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;
|
|
43
|
+
return buf;
|
|
648
44
|
}
|
|
45
|
+
return unsafeStringify(rnds);
|
|
649
46
|
}
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
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);
|
|
47
|
+
function v4(options, buf, offset) {
|
|
48
|
+
if (native_default.randomUUID && !buf && !options) {
|
|
49
|
+
return native_default.randomUUID();
|
|
750
50
|
}
|
|
51
|
+
return _v4(options, buf, offset);
|
|
751
52
|
}
|
|
752
|
-
|
|
753
|
-
// src/crypto/chacha20.ts
|
|
754
|
-
var CHACHA20_AUTH_TAG_LENGTH = 16;
|
|
755
|
-
var CHACHA20_NONCE_LENGTH = 12;
|
|
756
|
-
function decrypt(key, nonce, aad, ciphertext, authTag) {
|
|
757
|
-
nonce = padNonce(nonce);
|
|
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);
|
|
765
|
-
}
|
|
766
|
-
function encrypt(key, nonce, aad, plaintext) {
|
|
767
|
-
nonce = padNonce(nonce);
|
|
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));
|
|
772
|
-
return {
|
|
773
|
-
ciphertext,
|
|
774
|
-
authTag
|
|
775
|
-
};
|
|
776
|
-
}
|
|
777
|
-
function padNonce(nonce) {
|
|
778
|
-
if (nonce.length >= CHACHA20_NONCE_LENGTH) {
|
|
779
|
-
return nonce;
|
|
780
|
-
}
|
|
781
|
-
return Buffer.concat([
|
|
782
|
-
Buffer.alloc(CHACHA20_NONCE_LENGTH - nonce.length, 0),
|
|
783
|
-
nonce
|
|
784
|
-
]);
|
|
785
|
-
}
|
|
786
|
-
// src/crypto/curve25519.ts
|
|
787
|
-
var exports_curve25519 = {};
|
|
788
|
-
__export(exports_curve25519, {
|
|
789
|
-
generateSharedSecKey: () => generateSharedSecKey,
|
|
790
|
-
generateKeyPair: () => generateKeyPair
|
|
791
|
-
});
|
|
792
|
-
import tweetnacl from "tweetnacl";
|
|
793
|
-
function generateKeyPair() {
|
|
794
|
-
const keyPair = tweetnacl.box.keyPair();
|
|
795
|
-
return {
|
|
796
|
-
publicKey: keyPair.publicKey,
|
|
797
|
-
secretKey: keyPair.secretKey
|
|
798
|
-
};
|
|
799
|
-
}
|
|
800
|
-
function generateSharedSecKey(priKey, pubKey) {
|
|
801
|
-
return tweetnacl.scalarMult(priKey, pubKey);
|
|
802
|
-
}
|
|
803
|
-
// src/crypto/hkdf.ts
|
|
804
|
-
import { hkdfSync } from "node:crypto";
|
|
805
|
-
function hkdf_default(options) {
|
|
806
|
-
return Buffer.from(hkdfSync(options.hash, options.key, options.salt, options.info, options.length));
|
|
807
|
-
}
|
|
53
|
+
var v4_default = v4;
|
|
808
54
|
// src/discovery.ts
|
|
809
55
|
import mdns from "node-dns-sd";
|
|
810
56
|
|
|
@@ -857,11 +103,9 @@ class Discovery {
|
|
|
857
103
|
console.log(devices.map((d) => ` ● ${d.id}`).join(`
|
|
858
104
|
`));
|
|
859
105
|
tries--;
|
|
860
|
-
if (tries === 0) {
|
|
861
|
-
throw new Error("Device not found after serveral tries, aborting.");
|
|
862
|
-
}
|
|
863
106
|
await waitFor(timeout);
|
|
864
107
|
}
|
|
108
|
+
throw new Error("Device not found after serveral tries, aborting.");
|
|
865
109
|
}
|
|
866
110
|
static airplay() {
|
|
867
111
|
return new Discovery(AIRPLAY_SERVICE);
|
|
@@ -916,46 +160,6 @@ function getTxt(result) {
|
|
|
916
160
|
}
|
|
917
161
|
return txt;
|
|
918
162
|
}
|
|
919
|
-
// src/net/getLocalIP.ts
|
|
920
|
-
import { networkInterfaces } from "node:os";
|
|
921
|
-
function getLocalIP_default() {
|
|
922
|
-
const interfaces = networkInterfaces();
|
|
923
|
-
for (const name of Object.keys(interfaces)) {
|
|
924
|
-
const iface = interfaces[name];
|
|
925
|
-
if (!iface) {
|
|
926
|
-
continue;
|
|
927
|
-
}
|
|
928
|
-
for (const net of iface) {
|
|
929
|
-
if (net.internal || net.family !== "IPv4") {
|
|
930
|
-
continue;
|
|
931
|
-
}
|
|
932
|
-
if (net.address && net.address !== "127.0.0.1") {
|
|
933
|
-
return net.address;
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
return null;
|
|
938
|
-
}
|
|
939
|
-
// src/net/getMacAddress.ts
|
|
940
|
-
import { networkInterfaces as networkInterfaces2 } from "node:os";
|
|
941
|
-
function getMacAddress_default() {
|
|
942
|
-
const interfaces = networkInterfaces2();
|
|
943
|
-
for (const name of Object.keys(interfaces)) {
|
|
944
|
-
const iface = interfaces[name];
|
|
945
|
-
if (!iface) {
|
|
946
|
-
continue;
|
|
947
|
-
}
|
|
948
|
-
for (const net of iface) {
|
|
949
|
-
if (net.internal || net.family !== "IPv4") {
|
|
950
|
-
continue;
|
|
951
|
-
}
|
|
952
|
-
if (net.mac && net.mac !== "00:00:00:00:00:00") {
|
|
953
|
-
return net.mac.toUpperCase();
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
return "00:00:00:00:00:00";
|
|
958
|
-
}
|
|
959
163
|
// src/connection.ts
|
|
960
164
|
import { EventEmitter } from "node:events";
|
|
961
165
|
import { Socket } from "node:net";
|
|
@@ -1303,10 +507,8 @@ class Context {
|
|
|
1303
507
|
}
|
|
1304
508
|
// src/pairing.ts
|
|
1305
509
|
import { OPack, TLV8 } from "@basmilius/apple-encoding";
|
|
510
|
+
import { Chacha20, Curve25519, Ed25519, hkdf } from "@basmilius/apple-encryption";
|
|
1306
511
|
import { SRP, SrpClient } from "fast-srp-hap";
|
|
1307
|
-
import { v4 as uuid } from "uuid";
|
|
1308
|
-
import tweetnacl2 from "tweetnacl";
|
|
1309
|
-
|
|
1310
512
|
class BasePairing {
|
|
1311
513
|
get context() {
|
|
1312
514
|
return this.#context;
|
|
@@ -1335,11 +537,11 @@ class AccessoryPair extends BasePairing {
|
|
|
1335
537
|
constructor(context, requestHandler) {
|
|
1336
538
|
super(context);
|
|
1337
539
|
this.#name = "basmilius/apple-protocols";
|
|
1338
|
-
this.#pairingId = Buffer.from(
|
|
540
|
+
this.#pairingId = Buffer.from(v4_default().toUpperCase());
|
|
1339
541
|
this.#requestHandler = requestHandler;
|
|
1340
542
|
}
|
|
1341
543
|
async start() {
|
|
1342
|
-
const keyPair =
|
|
544
|
+
const keyPair = Ed25519.generateKeyPair();
|
|
1343
545
|
this.#publicKey = Buffer.from(keyPair.publicKey);
|
|
1344
546
|
this.#secretKey = Buffer.from(keyPair.secretKey);
|
|
1345
547
|
}
|
|
@@ -1360,14 +562,14 @@ class AccessoryPair extends BasePairing {
|
|
|
1360
562
|
const m2 = await this.m2(m1);
|
|
1361
563
|
const m3 = await this.m3(m2);
|
|
1362
564
|
const m4 = await this.m4(m3);
|
|
1363
|
-
const accessoryToControllerKey =
|
|
565
|
+
const accessoryToControllerKey = hkdf({
|
|
1364
566
|
hash: "sha512",
|
|
1365
567
|
key: m4.sharedSecret,
|
|
1366
568
|
length: 32,
|
|
1367
569
|
salt: Buffer.from("Control-Salt"),
|
|
1368
570
|
info: Buffer.from("Control-Read-Encryption-Key")
|
|
1369
571
|
});
|
|
1370
|
-
const controllerToAccessoryKey =
|
|
572
|
+
const controllerToAccessoryKey = hkdf({
|
|
1371
573
|
hash: "sha512",
|
|
1372
574
|
key: m4.sharedSecret,
|
|
1373
575
|
length: 32,
|
|
@@ -1416,14 +618,14 @@ class AccessoryPair extends BasePairing {
|
|
|
1416
618
|
return { sharedSecret };
|
|
1417
619
|
}
|
|
1418
620
|
async m5(m4) {
|
|
1419
|
-
const iosDeviceX =
|
|
621
|
+
const iosDeviceX = hkdf({
|
|
1420
622
|
hash: "sha512",
|
|
1421
623
|
key: m4.sharedSecret,
|
|
1422
624
|
length: 32,
|
|
1423
625
|
salt: Buffer.from("Pair-Setup-Controller-Sign-Salt", "utf8"),
|
|
1424
626
|
info: Buffer.from("Pair-Setup-Controller-Sign-Info", "utf8")
|
|
1425
627
|
});
|
|
1426
|
-
const sessionKey =
|
|
628
|
+
const sessionKey = hkdf({
|
|
1427
629
|
hash: "sha512",
|
|
1428
630
|
key: m4.sharedSecret,
|
|
1429
631
|
length: 32,
|
|
@@ -1435,7 +637,7 @@ class AccessoryPair extends BasePairing {
|
|
|
1435
637
|
this.#pairingId,
|
|
1436
638
|
this.#publicKey
|
|
1437
639
|
]);
|
|
1438
|
-
const signature =
|
|
640
|
+
const signature = Ed25519.sign(deviceInfo, this.#secretKey);
|
|
1439
641
|
const innerTlv = TLV8.encode([
|
|
1440
642
|
[TLV8.Value.Identifier, this.#pairingId],
|
|
1441
643
|
[TLV8.Value.PublicKey, this.#publicKey],
|
|
@@ -1444,7 +646,7 @@ class AccessoryPair extends BasePairing {
|
|
|
1444
646
|
name: this.#name
|
|
1445
647
|
})]
|
|
1446
648
|
]);
|
|
1447
|
-
const { authTag, ciphertext } =
|
|
649
|
+
const { authTag, ciphertext } = Chacha20.encrypt(sessionKey, Buffer.from("PS-Msg05"), null, innerTlv);
|
|
1448
650
|
const encrypted = Buffer.concat([ciphertext, authTag]);
|
|
1449
651
|
const response = await this.#requestHandler("m5", TLV8.encode([
|
|
1450
652
|
[TLV8.Value.State, TLV8.State.M5],
|
|
@@ -1461,12 +663,12 @@ class AccessoryPair extends BasePairing {
|
|
|
1461
663
|
};
|
|
1462
664
|
}
|
|
1463
665
|
async m6(m4, m5) {
|
|
1464
|
-
const data =
|
|
666
|
+
const data = Chacha20.decrypt(m5.sessionKey, Buffer.from("PS-Msg06"), null, m5.data, m5.authTag);
|
|
1465
667
|
const tlv = TLV8.decode(data);
|
|
1466
668
|
const accessoryIdentifier = tlv.get(TLV8.Value.Identifier);
|
|
1467
669
|
const accessoryLongTermPublicKey = tlv.get(TLV8.Value.PublicKey);
|
|
1468
670
|
const accessorySignature = tlv.get(TLV8.Value.Signature);
|
|
1469
|
-
const accessoryX =
|
|
671
|
+
const accessoryX = hkdf({
|
|
1470
672
|
hash: "sha512",
|
|
1471
673
|
key: m4.sharedSecret,
|
|
1472
674
|
length: 32,
|
|
@@ -1478,7 +680,7 @@ class AccessoryPair extends BasePairing {
|
|
|
1478
680
|
accessoryIdentifier,
|
|
1479
681
|
accessoryLongTermPublicKey
|
|
1480
682
|
]);
|
|
1481
|
-
if (!
|
|
683
|
+
if (!Ed25519.verify(accessoryInfo, accessorySignature, accessoryLongTermPublicKey)) {
|
|
1482
684
|
throw new Error("Invalid accessory signature.");
|
|
1483
685
|
}
|
|
1484
686
|
return {
|
|
@@ -1496,7 +698,7 @@ class AccessoryVerify extends BasePairing {
|
|
|
1496
698
|
#requestHandler;
|
|
1497
699
|
constructor(context, requestHandler) {
|
|
1498
700
|
super(context);
|
|
1499
|
-
this.#ephemeralKeyPair =
|
|
701
|
+
this.#ephemeralKeyPair = Curve25519.generateKeyPair();
|
|
1500
702
|
this.#requestHandler = requestHandler;
|
|
1501
703
|
}
|
|
1502
704
|
async start(credentials) {
|
|
@@ -1519,8 +721,8 @@ class AccessoryVerify extends BasePairing {
|
|
|
1519
721
|
};
|
|
1520
722
|
}
|
|
1521
723
|
async#m2(localAccessoryIdentifier, longTermPublicKey, m1) {
|
|
1522
|
-
const sharedSecret = Buffer.from(
|
|
1523
|
-
const sessionKey =
|
|
724
|
+
const sharedSecret = Buffer.from(Curve25519.generateSharedSecKey(this.#ephemeralKeyPair.secretKey, m1.serverPublicKey));
|
|
725
|
+
const sessionKey = hkdf({
|
|
1524
726
|
hash: "sha512",
|
|
1525
727
|
key: sharedSecret,
|
|
1526
728
|
length: 32,
|
|
@@ -1529,7 +731,7 @@ class AccessoryVerify extends BasePairing {
|
|
|
1529
731
|
});
|
|
1530
732
|
const encryptedData = m1.encryptedData.subarray(0, -16);
|
|
1531
733
|
const encryptedTag = m1.encryptedData.subarray(-16);
|
|
1532
|
-
const data =
|
|
734
|
+
const data = Chacha20.decrypt(sessionKey, Buffer.from("PV-Msg02"), null, encryptedData, encryptedTag);
|
|
1533
735
|
const tlv = TLV8.decode(data);
|
|
1534
736
|
const accessoryIdentifier = tlv.get(TLV8.Value.Identifier);
|
|
1535
737
|
const accessorySignature = tlv.get(TLV8.Value.Signature);
|
|
@@ -1541,7 +743,7 @@ class AccessoryVerify extends BasePairing {
|
|
|
1541
743
|
accessoryIdentifier,
|
|
1542
744
|
this.#ephemeralKeyPair.publicKey
|
|
1543
745
|
]);
|
|
1544
|
-
if (!
|
|
746
|
+
if (!Ed25519.verify(accessoryInfo, accessorySignature, longTermPublicKey)) {
|
|
1545
747
|
throw new Error("Invalid accessory signature.");
|
|
1546
748
|
}
|
|
1547
749
|
return {
|
|
@@ -1556,12 +758,12 @@ class AccessoryVerify extends BasePairing {
|
|
|
1556
758
|
pairingId,
|
|
1557
759
|
m2.serverEphemeralPublicKey
|
|
1558
760
|
]);
|
|
1559
|
-
const iosDeviceSignature = Buffer.from(
|
|
761
|
+
const iosDeviceSignature = Buffer.from(Ed25519.sign(iosDeviceInfo, secretKey));
|
|
1560
762
|
const innerTlv = TLV8.encode([
|
|
1561
763
|
[TLV8.Value.Identifier, pairingId],
|
|
1562
764
|
[TLV8.Value.Signature, iosDeviceSignature]
|
|
1563
765
|
]);
|
|
1564
|
-
const { authTag, ciphertext } =
|
|
766
|
+
const { authTag, ciphertext } = Chacha20.encrypt(m2.sessionKey, Buffer.from("PV-Msg03"), null, innerTlv);
|
|
1565
767
|
const encrypted = Buffer.concat([ciphertext, authTag]);
|
|
1566
768
|
await this.#requestHandler("m3", TLV8.encode([
|
|
1567
769
|
[TLV8.Value.State, TLV8.State.M3],
|
|
@@ -1637,6 +839,41 @@ class TimingServer {
|
|
|
1637
839
|
}
|
|
1638
840
|
// src/utils.ts
|
|
1639
841
|
import { randomBytes } from "node:crypto";
|
|
842
|
+
import { networkInterfaces } from "node:os";
|
|
843
|
+
function getLocalIP() {
|
|
844
|
+
const interfaces = networkInterfaces();
|
|
845
|
+
for (const iface of Object.values(interfaces)) {
|
|
846
|
+
if (!iface) {
|
|
847
|
+
continue;
|
|
848
|
+
}
|
|
849
|
+
for (const net2 of iface) {
|
|
850
|
+
if (net2.internal || net2.family !== "IPv4") {
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
853
|
+
if (net2.address && net2.address !== "127.0.0.1") {
|
|
854
|
+
return net2.address;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
return null;
|
|
859
|
+
}
|
|
860
|
+
function getMacAddress() {
|
|
861
|
+
const interfaces = networkInterfaces();
|
|
862
|
+
for (const iface of Object.values(interfaces)) {
|
|
863
|
+
if (!iface) {
|
|
864
|
+
continue;
|
|
865
|
+
}
|
|
866
|
+
for (const net2 of iface) {
|
|
867
|
+
if (net2.internal || net2.family !== "IPv4") {
|
|
868
|
+
continue;
|
|
869
|
+
}
|
|
870
|
+
if (net2.mac && net2.mac !== "00:00:00:00:00:00") {
|
|
871
|
+
return net2.mac.toUpperCase();
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
return "00:00:00:00:00:00";
|
|
876
|
+
}
|
|
1640
877
|
function randomInt32() {
|
|
1641
878
|
return randomBytes(4).readUInt32BE(0);
|
|
1642
879
|
}
|
|
@@ -1674,16 +911,15 @@ function splitUInt53(number) {
|
|
|
1674
911
|
}
|
|
1675
912
|
export {
|
|
1676
913
|
waitFor,
|
|
1677
|
-
|
|
914
|
+
v4_default as uuid,
|
|
1678
915
|
uint53ToLE,
|
|
1679
916
|
uint16ToBE,
|
|
1680
917
|
reporter,
|
|
1681
918
|
randomInt64,
|
|
1682
919
|
randomInt32,
|
|
1683
920
|
prompt,
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
getLocalIP_default as getLocalIP,
|
|
921
|
+
getMacAddress,
|
|
922
|
+
getLocalIP,
|
|
1687
923
|
cli,
|
|
1688
924
|
TimingServer,
|
|
1689
925
|
RAOP_SERVICE,
|
|
@@ -1692,10 +928,8 @@ export {
|
|
|
1692
928
|
EncryptionAwareConnection,
|
|
1693
929
|
ENCRYPTION,
|
|
1694
930
|
Discovery,
|
|
1695
|
-
exports_curve25519 as Curve25519,
|
|
1696
931
|
Context,
|
|
1697
932
|
Connection,
|
|
1698
|
-
exports_chacha20 as Chacha20,
|
|
1699
933
|
COMPANION_LINK_SERVICE,
|
|
1700
934
|
AccessoryVerify,
|
|
1701
935
|
AccessoryPair,
|
package/dist/utils.d.ts
CHANGED
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.5.
|
|
4
|
+
"version": "0.5.4",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": {
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"provenance": true
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
|
-
"build": "tsc && bun -b build.ts"
|
|
28
|
+
"build": "tsc && bun -b build.ts",
|
|
29
|
+
"test:discovery": "bun --watch test-discovery.ts"
|
|
29
30
|
},
|
|
30
31
|
"main": "./dist/index.js",
|
|
31
32
|
"types": "./dist/index.d.ts",
|
|
@@ -38,15 +39,14 @@
|
|
|
38
39
|
}
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
|
-
"@basmilius/apple-encoding": "0.5.
|
|
42
|
-
"@
|
|
42
|
+
"@basmilius/apple-encoding": "0.5.4",
|
|
43
|
+
"@basmilius/apple-encryption": "0.5.4",
|
|
43
44
|
"fast-srp-hap": "^2.0.4",
|
|
44
|
-
"node-dns-sd": "^1.0.1"
|
|
45
|
-
"tweetnacl": "^1.0.3",
|
|
46
|
-
"uuid": "^13.0.0"
|
|
45
|
+
"node-dns-sd": "^1.0.1"
|
|
47
46
|
},
|
|
48
47
|
"devDependencies": {
|
|
49
48
|
"@basmilius/tools": "^2.23.0",
|
|
50
|
-
"@types/bun": "^1.3.8"
|
|
49
|
+
"@types/bun": "^1.3.8",
|
|
50
|
+
"uuid": "^13.0.0"
|
|
51
51
|
}
|
|
52
52
|
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export declare const CHACHA20_AUTH_TAG_LENGTH = 16;
|
|
2
|
-
export declare const CHACHA20_NONCE_LENGTH = 12;
|
|
3
|
-
export declare function decrypt(key: Buffer, nonce: Buffer, aad: Buffer | null, ciphertext: Buffer, authTag: Buffer): Buffer;
|
|
4
|
-
export declare function encrypt(key: Buffer, nonce: Buffer, aad: Buffer | null, plaintext: Buffer): EncryptedData;
|
|
5
|
-
export declare function padNonce(nonce: Buffer): Buffer;
|
|
6
|
-
export type EncryptedData = {
|
|
7
|
-
readonly ciphertext: Buffer;
|
|
8
|
-
readonly authTag: Buffer;
|
|
9
|
-
};
|
package/dist/crypto/hkdf.d.ts
DELETED
package/dist/crypto/index.d.ts
DELETED
package/dist/net/getLocalIP.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function(): string;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function(): string;
|
package/dist/net/index.d.ts
DELETED