@basmilius/apple-common 0.5.3 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/cli.d.ts +0 -2
- package/dist/index.d.ts +3 -4
- package/dist/index.js +103 -866
- package/dist/utils.d.ts +2 -0
- package/package.json +6 -7
- 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/cli.d.ts
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
export { v4 as uuid } from "uuid";
|
|
2
|
-
export { Chacha20, Curve25519, hkdf } from "./crypto";
|
|
3
2
|
export { Discovery } from "./discovery";
|
|
4
|
-
export {
|
|
5
|
-
export { prompt, waitFor, cli } from "./cli";
|
|
3
|
+
export { prompt, waitFor } from "./cli";
|
|
6
4
|
export { Connection, EncryptionAwareConnection, EncryptionState } from "./connection";
|
|
7
5
|
export { Context } from "./context";
|
|
8
6
|
export { AIRPLAY_SERVICE, AIRPLAY_TRANSIENT_PIN, COMPANION_LINK_SERVICE, HTTP_TIMEOUT, RAOP_SERVICE } from "./const";
|
|
@@ -10,5 +8,6 @@ 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";
|
|
12
|
+
export type { AudioSource } from "./audioSource";
|
|
14
13
|
export type { ConnectionState, DiscoveryResult, EventMap } from "./types";
|
package/dist/index.js
CHANGED
|
@@ -1,821 +1,69 @@
|
|
|
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;
|
|
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));
|
|
37
5
|
}
|
|
38
|
-
function
|
|
39
|
-
|
|
40
|
-
writeUint32LE(value / 4294967296 >>> 0, out, offset + 4);
|
|
41
|
-
return out;
|
|
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();
|
|
42
8
|
}
|
|
43
9
|
|
|
44
|
-
// ../../node_modules
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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;
|
|
48
18
|
}
|
|
49
|
-
return
|
|
19
|
+
return rnds8Pool.slice(poolPtr, poolPtr += 16);
|
|
50
20
|
}
|
|
51
21
|
|
|
52
|
-
// ../../node_modules
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
}
|
|
22
|
+
// ../../node_modules/uuid/dist-node/native.js
|
|
23
|
+
import { randomUUID } from "node:crypto";
|
|
24
|
+
var native_default = { randomUUID };
|
|
255
25
|
|
|
256
|
-
// ../../node_modules
|
|
257
|
-
function
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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;
|
|
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");
|
|
270
32
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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;
|
|
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`);
|
|
505
39
|
}
|
|
506
|
-
|
|
507
|
-
|
|
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;
|
|
40
|
+
for (let i = 0;i < 16; ++i) {
|
|
41
|
+
buf[offset + i] = rnds[i];
|
|
558
42
|
}
|
|
559
|
-
|
|
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;
|
|
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);
|
|
47
|
+
function v4(options, buf, offset) {
|
|
48
|
+
if (native_default.randomUUID && !buf && !options) {
|
|
49
|
+
return native_default.randomUUID();
|
|
666
50
|
}
|
|
667
|
-
|
|
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
|
|
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));
|
|
51
|
+
return _v4(options, buf, offset);
|
|
807
52
|
}
|
|
53
|
+
var v4_default = v4;
|
|
808
54
|
// src/discovery.ts
|
|
809
55
|
import mdns from "node-dns-sd";
|
|
810
56
|
|
|
811
57
|
// src/cli.ts
|
|
812
58
|
import { createInterface } from "node:readline";
|
|
813
|
-
var cli = createInterface({
|
|
814
|
-
input: process.stdin,
|
|
815
|
-
output: process.stdout
|
|
816
|
-
});
|
|
817
59
|
async function prompt(message) {
|
|
818
|
-
|
|
60
|
+
const cli = createInterface({
|
|
61
|
+
input: process.stdin,
|
|
62
|
+
output: process.stdout
|
|
63
|
+
});
|
|
64
|
+
const answer = await new Promise((resolve) => cli.question(`${message}: `, resolve));
|
|
65
|
+
cli.close();
|
|
66
|
+
return answer;
|
|
819
67
|
}
|
|
820
68
|
async function waitFor(ms) {
|
|
821
69
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
@@ -914,46 +162,6 @@ function getTxt(result) {
|
|
|
914
162
|
}
|
|
915
163
|
return txt;
|
|
916
164
|
}
|
|
917
|
-
// src/net/getLocalIP.ts
|
|
918
|
-
import { networkInterfaces } from "node:os";
|
|
919
|
-
function getLocalIP_default() {
|
|
920
|
-
const interfaces = networkInterfaces();
|
|
921
|
-
for (const name of Object.keys(interfaces)) {
|
|
922
|
-
const iface = interfaces[name];
|
|
923
|
-
if (!iface) {
|
|
924
|
-
continue;
|
|
925
|
-
}
|
|
926
|
-
for (const net of iface) {
|
|
927
|
-
if (net.internal || net.family !== "IPv4") {
|
|
928
|
-
continue;
|
|
929
|
-
}
|
|
930
|
-
if (net.address && net.address !== "127.0.0.1") {
|
|
931
|
-
return net.address;
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
return null;
|
|
936
|
-
}
|
|
937
|
-
// src/net/getMacAddress.ts
|
|
938
|
-
import { networkInterfaces as networkInterfaces2 } from "node:os";
|
|
939
|
-
function getMacAddress_default() {
|
|
940
|
-
const interfaces = networkInterfaces2();
|
|
941
|
-
for (const name of Object.keys(interfaces)) {
|
|
942
|
-
const iface = interfaces[name];
|
|
943
|
-
if (!iface) {
|
|
944
|
-
continue;
|
|
945
|
-
}
|
|
946
|
-
for (const net of iface) {
|
|
947
|
-
if (net.internal || net.family !== "IPv4") {
|
|
948
|
-
continue;
|
|
949
|
-
}
|
|
950
|
-
if (net.mac && net.mac !== "00:00:00:00:00:00") {
|
|
951
|
-
return net.mac.toUpperCase();
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
return "00:00:00:00:00:00";
|
|
956
|
-
}
|
|
957
165
|
// src/connection.ts
|
|
958
166
|
import { EventEmitter } from "node:events";
|
|
959
167
|
import { Socket } from "node:net";
|
|
@@ -1301,10 +509,8 @@ class Context {
|
|
|
1301
509
|
}
|
|
1302
510
|
// src/pairing.ts
|
|
1303
511
|
import { OPack, TLV8 } from "@basmilius/apple-encoding";
|
|
512
|
+
import { Chacha20, Curve25519, Ed25519, hkdf } from "@basmilius/apple-encryption";
|
|
1304
513
|
import { SRP, SrpClient } from "fast-srp-hap";
|
|
1305
|
-
import { v4 as uuid } from "uuid";
|
|
1306
|
-
import tweetnacl2 from "tweetnacl";
|
|
1307
|
-
|
|
1308
514
|
class BasePairing {
|
|
1309
515
|
get context() {
|
|
1310
516
|
return this.#context;
|
|
@@ -1333,11 +539,11 @@ class AccessoryPair extends BasePairing {
|
|
|
1333
539
|
constructor(context, requestHandler) {
|
|
1334
540
|
super(context);
|
|
1335
541
|
this.#name = "basmilius/apple-protocols";
|
|
1336
|
-
this.#pairingId = Buffer.from(
|
|
542
|
+
this.#pairingId = Buffer.from(v4_default().toUpperCase());
|
|
1337
543
|
this.#requestHandler = requestHandler;
|
|
1338
544
|
}
|
|
1339
545
|
async start() {
|
|
1340
|
-
const keyPair =
|
|
546
|
+
const keyPair = Ed25519.generateKeyPair();
|
|
1341
547
|
this.#publicKey = Buffer.from(keyPair.publicKey);
|
|
1342
548
|
this.#secretKey = Buffer.from(keyPair.secretKey);
|
|
1343
549
|
}
|
|
@@ -1358,14 +564,14 @@ class AccessoryPair extends BasePairing {
|
|
|
1358
564
|
const m2 = await this.m2(m1);
|
|
1359
565
|
const m3 = await this.m3(m2);
|
|
1360
566
|
const m4 = await this.m4(m3);
|
|
1361
|
-
const accessoryToControllerKey =
|
|
567
|
+
const accessoryToControllerKey = hkdf({
|
|
1362
568
|
hash: "sha512",
|
|
1363
569
|
key: m4.sharedSecret,
|
|
1364
570
|
length: 32,
|
|
1365
571
|
salt: Buffer.from("Control-Salt"),
|
|
1366
572
|
info: Buffer.from("Control-Read-Encryption-Key")
|
|
1367
573
|
});
|
|
1368
|
-
const controllerToAccessoryKey =
|
|
574
|
+
const controllerToAccessoryKey = hkdf({
|
|
1369
575
|
hash: "sha512",
|
|
1370
576
|
key: m4.sharedSecret,
|
|
1371
577
|
length: 32,
|
|
@@ -1414,14 +620,14 @@ class AccessoryPair extends BasePairing {
|
|
|
1414
620
|
return { sharedSecret };
|
|
1415
621
|
}
|
|
1416
622
|
async m5(m4) {
|
|
1417
|
-
const iosDeviceX =
|
|
623
|
+
const iosDeviceX = hkdf({
|
|
1418
624
|
hash: "sha512",
|
|
1419
625
|
key: m4.sharedSecret,
|
|
1420
626
|
length: 32,
|
|
1421
627
|
salt: Buffer.from("Pair-Setup-Controller-Sign-Salt", "utf8"),
|
|
1422
628
|
info: Buffer.from("Pair-Setup-Controller-Sign-Info", "utf8")
|
|
1423
629
|
});
|
|
1424
|
-
const sessionKey =
|
|
630
|
+
const sessionKey = hkdf({
|
|
1425
631
|
hash: "sha512",
|
|
1426
632
|
key: m4.sharedSecret,
|
|
1427
633
|
length: 32,
|
|
@@ -1433,7 +639,7 @@ class AccessoryPair extends BasePairing {
|
|
|
1433
639
|
this.#pairingId,
|
|
1434
640
|
this.#publicKey
|
|
1435
641
|
]);
|
|
1436
|
-
const signature =
|
|
642
|
+
const signature = Ed25519.sign(deviceInfo, this.#secretKey);
|
|
1437
643
|
const innerTlv = TLV8.encode([
|
|
1438
644
|
[TLV8.Value.Identifier, this.#pairingId],
|
|
1439
645
|
[TLV8.Value.PublicKey, this.#publicKey],
|
|
@@ -1442,7 +648,7 @@ class AccessoryPair extends BasePairing {
|
|
|
1442
648
|
name: this.#name
|
|
1443
649
|
})]
|
|
1444
650
|
]);
|
|
1445
|
-
const { authTag, ciphertext } =
|
|
651
|
+
const { authTag, ciphertext } = Chacha20.encrypt(sessionKey, Buffer.from("PS-Msg05"), null, innerTlv);
|
|
1446
652
|
const encrypted = Buffer.concat([ciphertext, authTag]);
|
|
1447
653
|
const response = await this.#requestHandler("m5", TLV8.encode([
|
|
1448
654
|
[TLV8.Value.State, TLV8.State.M5],
|
|
@@ -1459,12 +665,12 @@ class AccessoryPair extends BasePairing {
|
|
|
1459
665
|
};
|
|
1460
666
|
}
|
|
1461
667
|
async m6(m4, m5) {
|
|
1462
|
-
const data =
|
|
668
|
+
const data = Chacha20.decrypt(m5.sessionKey, Buffer.from("PS-Msg06"), null, m5.data, m5.authTag);
|
|
1463
669
|
const tlv = TLV8.decode(data);
|
|
1464
670
|
const accessoryIdentifier = tlv.get(TLV8.Value.Identifier);
|
|
1465
671
|
const accessoryLongTermPublicKey = tlv.get(TLV8.Value.PublicKey);
|
|
1466
672
|
const accessorySignature = tlv.get(TLV8.Value.Signature);
|
|
1467
|
-
const accessoryX =
|
|
673
|
+
const accessoryX = hkdf({
|
|
1468
674
|
hash: "sha512",
|
|
1469
675
|
key: m4.sharedSecret,
|
|
1470
676
|
length: 32,
|
|
@@ -1476,7 +682,7 @@ class AccessoryPair extends BasePairing {
|
|
|
1476
682
|
accessoryIdentifier,
|
|
1477
683
|
accessoryLongTermPublicKey
|
|
1478
684
|
]);
|
|
1479
|
-
if (!
|
|
685
|
+
if (!Ed25519.verify(accessoryInfo, accessorySignature, accessoryLongTermPublicKey)) {
|
|
1480
686
|
throw new Error("Invalid accessory signature.");
|
|
1481
687
|
}
|
|
1482
688
|
return {
|
|
@@ -1494,7 +700,7 @@ class AccessoryVerify extends BasePairing {
|
|
|
1494
700
|
#requestHandler;
|
|
1495
701
|
constructor(context, requestHandler) {
|
|
1496
702
|
super(context);
|
|
1497
|
-
this.#ephemeralKeyPair =
|
|
703
|
+
this.#ephemeralKeyPair = Curve25519.generateKeyPair();
|
|
1498
704
|
this.#requestHandler = requestHandler;
|
|
1499
705
|
}
|
|
1500
706
|
async start(credentials) {
|
|
@@ -1517,8 +723,8 @@ class AccessoryVerify extends BasePairing {
|
|
|
1517
723
|
};
|
|
1518
724
|
}
|
|
1519
725
|
async#m2(localAccessoryIdentifier, longTermPublicKey, m1) {
|
|
1520
|
-
const sharedSecret = Buffer.from(
|
|
1521
|
-
const sessionKey =
|
|
726
|
+
const sharedSecret = Buffer.from(Curve25519.generateSharedSecKey(this.#ephemeralKeyPair.secretKey, m1.serverPublicKey));
|
|
727
|
+
const sessionKey = hkdf({
|
|
1522
728
|
hash: "sha512",
|
|
1523
729
|
key: sharedSecret,
|
|
1524
730
|
length: 32,
|
|
@@ -1527,7 +733,7 @@ class AccessoryVerify extends BasePairing {
|
|
|
1527
733
|
});
|
|
1528
734
|
const encryptedData = m1.encryptedData.subarray(0, -16);
|
|
1529
735
|
const encryptedTag = m1.encryptedData.subarray(-16);
|
|
1530
|
-
const data =
|
|
736
|
+
const data = Chacha20.decrypt(sessionKey, Buffer.from("PV-Msg02"), null, encryptedData, encryptedTag);
|
|
1531
737
|
const tlv = TLV8.decode(data);
|
|
1532
738
|
const accessoryIdentifier = tlv.get(TLV8.Value.Identifier);
|
|
1533
739
|
const accessorySignature = tlv.get(TLV8.Value.Signature);
|
|
@@ -1539,7 +745,7 @@ class AccessoryVerify extends BasePairing {
|
|
|
1539
745
|
accessoryIdentifier,
|
|
1540
746
|
this.#ephemeralKeyPair.publicKey
|
|
1541
747
|
]);
|
|
1542
|
-
if (!
|
|
748
|
+
if (!Ed25519.verify(accessoryInfo, accessorySignature, longTermPublicKey)) {
|
|
1543
749
|
throw new Error("Invalid accessory signature.");
|
|
1544
750
|
}
|
|
1545
751
|
return {
|
|
@@ -1554,12 +760,12 @@ class AccessoryVerify extends BasePairing {
|
|
|
1554
760
|
pairingId,
|
|
1555
761
|
m2.serverEphemeralPublicKey
|
|
1556
762
|
]);
|
|
1557
|
-
const iosDeviceSignature = Buffer.from(
|
|
763
|
+
const iosDeviceSignature = Buffer.from(Ed25519.sign(iosDeviceInfo, secretKey));
|
|
1558
764
|
const innerTlv = TLV8.encode([
|
|
1559
765
|
[TLV8.Value.Identifier, pairingId],
|
|
1560
766
|
[TLV8.Value.Signature, iosDeviceSignature]
|
|
1561
767
|
]);
|
|
1562
|
-
const { authTag, ciphertext } =
|
|
768
|
+
const { authTag, ciphertext } = Chacha20.encrypt(m2.sessionKey, Buffer.from("PV-Msg03"), null, innerTlv);
|
|
1563
769
|
const encrypted = Buffer.concat([ciphertext, authTag]);
|
|
1564
770
|
await this.#requestHandler("m3", TLV8.encode([
|
|
1565
771
|
[TLV8.Value.State, TLV8.State.M3],
|
|
@@ -1635,6 +841,41 @@ class TimingServer {
|
|
|
1635
841
|
}
|
|
1636
842
|
// src/utils.ts
|
|
1637
843
|
import { randomBytes } from "node:crypto";
|
|
844
|
+
import { networkInterfaces } from "node:os";
|
|
845
|
+
function getLocalIP() {
|
|
846
|
+
const interfaces = networkInterfaces();
|
|
847
|
+
for (const iface of Object.values(interfaces)) {
|
|
848
|
+
if (!iface) {
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
for (const net2 of iface) {
|
|
852
|
+
if (net2.internal || net2.family !== "IPv4") {
|
|
853
|
+
continue;
|
|
854
|
+
}
|
|
855
|
+
if (net2.address && net2.address !== "127.0.0.1") {
|
|
856
|
+
return net2.address;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
return null;
|
|
861
|
+
}
|
|
862
|
+
function getMacAddress() {
|
|
863
|
+
const interfaces = networkInterfaces();
|
|
864
|
+
for (const iface of Object.values(interfaces)) {
|
|
865
|
+
if (!iface) {
|
|
866
|
+
continue;
|
|
867
|
+
}
|
|
868
|
+
for (const net2 of iface) {
|
|
869
|
+
if (net2.internal || net2.family !== "IPv4") {
|
|
870
|
+
continue;
|
|
871
|
+
}
|
|
872
|
+
if (net2.mac && net2.mac !== "00:00:00:00:00:00") {
|
|
873
|
+
return net2.mac.toUpperCase();
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
return "00:00:00:00:00:00";
|
|
878
|
+
}
|
|
1638
879
|
function randomInt32() {
|
|
1639
880
|
return randomBytes(4).readUInt32BE(0);
|
|
1640
881
|
}
|
|
@@ -1672,17 +913,15 @@ function splitUInt53(number) {
|
|
|
1672
913
|
}
|
|
1673
914
|
export {
|
|
1674
915
|
waitFor,
|
|
1675
|
-
|
|
916
|
+
v4_default as uuid,
|
|
1676
917
|
uint53ToLE,
|
|
1677
918
|
uint16ToBE,
|
|
1678
919
|
reporter,
|
|
1679
920
|
randomInt64,
|
|
1680
921
|
randomInt32,
|
|
1681
922
|
prompt,
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
getLocalIP_default as getLocalIP,
|
|
1685
|
-
cli,
|
|
923
|
+
getMacAddress,
|
|
924
|
+
getLocalIP,
|
|
1686
925
|
TimingServer,
|
|
1687
926
|
RAOP_SERVICE,
|
|
1688
927
|
HTTP_TIMEOUT,
|
|
@@ -1690,10 +929,8 @@ export {
|
|
|
1690
929
|
EncryptionAwareConnection,
|
|
1691
930
|
ENCRYPTION,
|
|
1692
931
|
Discovery,
|
|
1693
|
-
exports_curve25519 as Curve25519,
|
|
1694
932
|
Context,
|
|
1695
933
|
Connection,
|
|
1696
|
-
exports_chacha20 as Chacha20,
|
|
1697
934
|
COMPANION_LINK_SERVICE,
|
|
1698
935
|
AccessoryVerify,
|
|
1699
936
|
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.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": {
|
|
@@ -39,15 +39,14 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@basmilius/apple-encoding": "0.
|
|
43
|
-
"@
|
|
42
|
+
"@basmilius/apple-encoding": "0.6.0",
|
|
43
|
+
"@basmilius/apple-encryption": "0.6.0",
|
|
44
44
|
"fast-srp-hap": "^2.0.4",
|
|
45
|
-
"node-dns-sd": "^1.0.1"
|
|
46
|
-
"tweetnacl": "^1.0.3",
|
|
47
|
-
"uuid": "^13.0.0"
|
|
45
|
+
"node-dns-sd": "^1.0.1"
|
|
48
46
|
},
|
|
49
47
|
"devDependencies": {
|
|
50
48
|
"@basmilius/tools": "^2.23.0",
|
|
51
|
-
"@types/bun": "^1.3.8"
|
|
49
|
+
"@types/bun": "^1.3.8",
|
|
50
|
+
"uuid": "^13.0.0"
|
|
52
51
|
}
|
|
53
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