@corewire/core 0.0.0 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -1
- package/dist/chunk-wE0WeVzl.mjs +22 -0
- package/dist/crypto-CXSIS6si.mjs +3093 -0
- package/dist/crypto.d.mts +111 -0
- package/dist/crypto.mjs +3 -0
- package/dist/discovery.d.mts +110 -0
- package/dist/discovery.mjs +763 -0
- package/dist/encoding-BaLbwFQ0.mjs +1663 -0
- package/dist/encoding.d.mts +411 -0
- package/dist/encoding.mjs +3 -0
- package/dist/errors-DVv1n4fL.mjs +125 -0
- package/dist/index-BJKNwUmq.d.mts +125 -0
- package/dist/index.d.mts +82 -0
- package/dist/index.mjs +5 -0
- package/dist/pairing.d.mts +169 -0
- package/dist/pairing.mjs +2487 -0
- package/dist/reporter-DfM6vPT0.mjs +223 -0
- package/dist/reporter-Dn7n2TXN.d.mts +112 -0
- package/dist/transport-C4BMdD2J.mjs +270 -0
- package/dist/transport.d.mts +2 -0
- package/dist/transport.mjs +3 -0
- package/dist/types-qzabRJJ9.d.mts +5 -0
- package/dist/utils-DY5OKUuy.mjs +8 -0
- package/package.json +84 -18
package/dist/pairing.mjs
ADDED
|
@@ -0,0 +1,2487 @@
|
|
|
1
|
+
import { r as __require, t as __commonJSMin } from "./chunk-wE0WeVzl.mjs";
|
|
2
|
+
import { f as PairingError, n as AuthenticationError } from "./errors-DVv1n4fL.mjs";
|
|
3
|
+
import { a as generateKeyPair, f as decrypt$1, g as encrypt, h as decrypt, l as generateKeyPair$1, n as deriveEncryptionKeys, o as sign, p as encrypt$1, s as verify, t as derive, u as generateSharedSecKey } from "./crypto-CXSIS6si.mjs";
|
|
4
|
+
import { a as Value, c as encode, d as encode$1, i as State, n as Flags, o as bail, r as Method, s as decode } from "./encoding-BaLbwFQ0.mjs";
|
|
5
|
+
import { a as DEFAULT_CLIENT_NAME } from "./transport-C4BMdD2J.mjs";
|
|
6
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
7
|
+
|
|
8
|
+
//#region src/pairing/cipher.ts
|
|
9
|
+
/** HKDF-SHA512 with string salt and info, as every HAP pairing key uses. */
|
|
10
|
+
function deriveKey(key, salt, info, length = 32) {
|
|
11
|
+
return derive({
|
|
12
|
+
hash: "sha512",
|
|
13
|
+
key,
|
|
14
|
+
length,
|
|
15
|
+
salt: Buffer.from(salt),
|
|
16
|
+
info: Buffer.from(info)
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* ChaCha20-Poly1305 with the session key and an 8-byte message nonce ("PS-Msg05"), the auth tag
|
|
21
|
+
* appended. Legacy devices use AES-128-CTR instead, keyed from the shared secret; the nonce then
|
|
22
|
+
* plays no part.
|
|
23
|
+
*/
|
|
24
|
+
function pairingCipher(phase, sharedSecret, sessionKey, useAes) {
|
|
25
|
+
if (useAes) {
|
|
26
|
+
const key = deriveKey(sharedSecret, "", `${phase}-AES-Key`, 16);
|
|
27
|
+
const iv = deriveKey(sharedSecret, "", `${phase}-AES-IV`, 16);
|
|
28
|
+
return {
|
|
29
|
+
seal: (_, plaintext) => encrypt(key, iv, plaintext),
|
|
30
|
+
open: (_, sealed) => decrypt(key, iv, sealed)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
seal: (nonce, plaintext) => {
|
|
35
|
+
const { authTag, ciphertext } = encrypt$1(sessionKey, Buffer.from(nonce), null, plaintext);
|
|
36
|
+
return Buffer.concat([ciphertext, authTag]);
|
|
37
|
+
},
|
|
38
|
+
open: (nonce, sealed) => decrypt$1(sessionKey, Buffer.from(nonce), null, sealed.subarray(0, -16), sealed.subarray(-16))
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** The HAP channel keys, named from the controller's side: we decrypt with `Control-Read-Encryption-Key`. */
|
|
42
|
+
function deriveControlKeys(sharedSecret) {
|
|
43
|
+
const { readKey, writeKey } = deriveEncryptionKeys(sharedSecret, "Control-Salt", "Control-Read-Encryption-Key", "Control-Write-Encryption-Key");
|
|
44
|
+
return {
|
|
45
|
+
accessoryToControllerKey: readKey,
|
|
46
|
+
controllerToAccessoryKey: writeKey
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Decodes a TLV8 message and throws the accessory's error when it carries one. */
|
|
50
|
+
function decodeTlv(context, buffer) {
|
|
51
|
+
const data = decode(buffer);
|
|
52
|
+
if (data.has(Value.Error)) bail(data);
|
|
53
|
+
context.logger.raw("Decoded TLV", data);
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region ../../node_modules/.bun/fast-srp-hap@2.0.4/node_modules/fast-srp-hap/jsbn/jsbn.js
|
|
59
|
+
var require_jsbn = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
60
|
+
var crypt = __require("crypto");
|
|
61
|
+
var dbits;
|
|
62
|
+
function BigInteger(a, b) {
|
|
63
|
+
if (a != null) if ("number" == typeof a) this.fromNumber(a, b);
|
|
64
|
+
else if (Buffer.isBuffer(a)) this.fromBuffer(a);
|
|
65
|
+
else if (b == null && "string" != typeof a) this.fromByteArray(a);
|
|
66
|
+
else this.fromString(a, b);
|
|
67
|
+
}
|
|
68
|
+
function nbi() {
|
|
69
|
+
return new BigInteger(null);
|
|
70
|
+
}
|
|
71
|
+
function am3(i, x, w, j, c, n) {
|
|
72
|
+
var xl = x & 16383, xh = x >> 14;
|
|
73
|
+
while (--n >= 0) {
|
|
74
|
+
var l = this[i] & 16383;
|
|
75
|
+
var h = this[i++] >> 14;
|
|
76
|
+
var m = xh * l + h * xl;
|
|
77
|
+
l = xl * l + ((m & 16383) << 14) + w[j] + c;
|
|
78
|
+
c = (l >> 28) + (m >> 14) + xh * h;
|
|
79
|
+
w[j++] = l & 268435455;
|
|
80
|
+
}
|
|
81
|
+
return c;
|
|
82
|
+
}
|
|
83
|
+
BigInteger.prototype.am = am3;
|
|
84
|
+
dbits = 28;
|
|
85
|
+
BigInteger.prototype.DB = dbits;
|
|
86
|
+
BigInteger.prototype.DM = (1 << dbits) - 1;
|
|
87
|
+
BigInteger.prototype.DV = 1 << dbits;
|
|
88
|
+
var BI_FP = 52;
|
|
89
|
+
BigInteger.prototype.FV = Math.pow(2, BI_FP);
|
|
90
|
+
BigInteger.prototype.F1 = BI_FP - dbits;
|
|
91
|
+
BigInteger.prototype.F2 = 2 * dbits - BI_FP;
|
|
92
|
+
var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
93
|
+
var BI_RC = new Array();
|
|
94
|
+
var rr = "0".charCodeAt(0), vv;
|
|
95
|
+
for (vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
|
|
96
|
+
rr = "a".charCodeAt(0);
|
|
97
|
+
for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
|
|
98
|
+
rr = "A".charCodeAt(0);
|
|
99
|
+
for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
|
|
100
|
+
function int2char(n) {
|
|
101
|
+
return BI_RM.charAt(n);
|
|
102
|
+
}
|
|
103
|
+
function intAt(s, i) {
|
|
104
|
+
var c = BI_RC[s.charCodeAt(i)];
|
|
105
|
+
return c == null ? -1 : c;
|
|
106
|
+
}
|
|
107
|
+
function bnpCopyTo(r) {
|
|
108
|
+
for (var i = this.t - 1; i >= 0; --i) r[i] = this[i];
|
|
109
|
+
r.t = this.t;
|
|
110
|
+
r.s = this.s;
|
|
111
|
+
}
|
|
112
|
+
function bnpFromInt(x) {
|
|
113
|
+
this.t = 1;
|
|
114
|
+
this.s = x < 0 ? -1 : 0;
|
|
115
|
+
if (x > 0) this[0] = x;
|
|
116
|
+
else if (x < -1) this[0] = x + DV;
|
|
117
|
+
else this.t = 0;
|
|
118
|
+
}
|
|
119
|
+
function nbv(i) {
|
|
120
|
+
var r = nbi();
|
|
121
|
+
r.fromInt(i);
|
|
122
|
+
return r;
|
|
123
|
+
}
|
|
124
|
+
function bnpFromString(data, radix, unsigned) {
|
|
125
|
+
var k;
|
|
126
|
+
switch (radix) {
|
|
127
|
+
case 2:
|
|
128
|
+
k = 1;
|
|
129
|
+
break;
|
|
130
|
+
case 4:
|
|
131
|
+
k = 2;
|
|
132
|
+
break;
|
|
133
|
+
case 8:
|
|
134
|
+
k = 3;
|
|
135
|
+
break;
|
|
136
|
+
case 16:
|
|
137
|
+
k = 4;
|
|
138
|
+
break;
|
|
139
|
+
case 32:
|
|
140
|
+
k = 5;
|
|
141
|
+
break;
|
|
142
|
+
case 256:
|
|
143
|
+
k = 8;
|
|
144
|
+
break;
|
|
145
|
+
default:
|
|
146
|
+
this.fromRadix(data, radix);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
this.t = 0;
|
|
150
|
+
this.s = 0;
|
|
151
|
+
var i = data.length;
|
|
152
|
+
var mi = false;
|
|
153
|
+
var sh = 0;
|
|
154
|
+
while (--i >= 0) {
|
|
155
|
+
var x = k == 8 ? data[i] & 255 : intAt(data, i);
|
|
156
|
+
if (x < 0) {
|
|
157
|
+
if (data.charAt(i) == "-") mi = true;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
mi = false;
|
|
161
|
+
if (sh === 0) this[this.t++] = x;
|
|
162
|
+
else if (sh + k > this.DB) {
|
|
163
|
+
this[this.t - 1] |= (x & (1 << this.DB - sh) - 1) << sh;
|
|
164
|
+
this[this.t++] = x >> this.DB - sh;
|
|
165
|
+
} else this[this.t - 1] |= x << sh;
|
|
166
|
+
sh += k;
|
|
167
|
+
if (sh >= this.DB) sh -= this.DB;
|
|
168
|
+
}
|
|
169
|
+
if (!unsigned && k == 8 && (data[0] & 128) != 0) {
|
|
170
|
+
this.s = -1;
|
|
171
|
+
if (sh > 0) this[this.t - 1] |= (1 << this.DB - sh) - 1 << sh;
|
|
172
|
+
}
|
|
173
|
+
this.clamp();
|
|
174
|
+
if (mi) BigInteger.ZERO.subTo(this, this);
|
|
175
|
+
}
|
|
176
|
+
function bnpFromByteArray(a, unsigned) {
|
|
177
|
+
this.fromString(a, 256, unsigned);
|
|
178
|
+
}
|
|
179
|
+
function bnpFromBuffer(a) {
|
|
180
|
+
this.fromString(a, 256, true);
|
|
181
|
+
}
|
|
182
|
+
function bnpClamp() {
|
|
183
|
+
var c = this.s & this.DM;
|
|
184
|
+
while (this.t > 0 && this[this.t - 1] == c) --this.t;
|
|
185
|
+
}
|
|
186
|
+
function bnToString(b) {
|
|
187
|
+
if (this.s < 0) return "-" + this.negate().toString(b);
|
|
188
|
+
var k;
|
|
189
|
+
if (b == 16) k = 4;
|
|
190
|
+
else if (b == 8) k = 3;
|
|
191
|
+
else if (b == 2) k = 1;
|
|
192
|
+
else if (b == 32) k = 5;
|
|
193
|
+
else if (b == 4) k = 2;
|
|
194
|
+
else return this.toRadix(b);
|
|
195
|
+
var km = (1 << k) - 1, d, m = false, r = "", i = this.t;
|
|
196
|
+
var p = this.DB - i * this.DB % k;
|
|
197
|
+
if (i-- > 0) {
|
|
198
|
+
if (p < this.DB && (d = this[i] >> p) > 0) {
|
|
199
|
+
m = true;
|
|
200
|
+
r = int2char(d);
|
|
201
|
+
}
|
|
202
|
+
while (i >= 0) {
|
|
203
|
+
if (p < k) {
|
|
204
|
+
d = (this[i] & (1 << p) - 1) << k - p;
|
|
205
|
+
d |= this[--i] >> (p += this.DB - k);
|
|
206
|
+
} else {
|
|
207
|
+
d = this[i] >> (p -= k) & km;
|
|
208
|
+
if (p <= 0) {
|
|
209
|
+
p += this.DB;
|
|
210
|
+
--i;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (d > 0) m = true;
|
|
214
|
+
if (m) r += int2char(d);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
//! Fix to be compatible with node >0.12.7 Buffer.js
|
|
218
|
+
if (b == 16 && r.length % 2 != 0) r = "0" + r;
|
|
219
|
+
return m ? r : "0";
|
|
220
|
+
}
|
|
221
|
+
function bnNegate() {
|
|
222
|
+
var r = nbi();
|
|
223
|
+
BigInteger.ZERO.subTo(this, r);
|
|
224
|
+
return r;
|
|
225
|
+
}
|
|
226
|
+
function bnAbs() {
|
|
227
|
+
return this.s < 0 ? this.negate() : this;
|
|
228
|
+
}
|
|
229
|
+
function bnCompareTo(a) {
|
|
230
|
+
var r = this.s - a.s;
|
|
231
|
+
if (r != 0) return r;
|
|
232
|
+
var i = this.t;
|
|
233
|
+
r = i - a.t;
|
|
234
|
+
if (r != 0) return this.s < 0 ? -r : r;
|
|
235
|
+
while (--i >= 0) if ((r = this[i] - a[i]) != 0) return r;
|
|
236
|
+
return 0;
|
|
237
|
+
}
|
|
238
|
+
function bnEqual(a) {
|
|
239
|
+
console.log(this.compareTo(a));
|
|
240
|
+
return this.compareTo(a) == 0 ? true : false;
|
|
241
|
+
}
|
|
242
|
+
function bnGreater(a) {
|
|
243
|
+
return this.compareTo(a) > 0 ? true : false;
|
|
244
|
+
}
|
|
245
|
+
function bnGreaterOrEqual(a) {
|
|
246
|
+
return this.compareTo(a) >= 0 ? true : false;
|
|
247
|
+
}
|
|
248
|
+
function bnLesser(a) {
|
|
249
|
+
return this.compareTo(a) < 0 ? true : false;
|
|
250
|
+
}
|
|
251
|
+
function bnLesserOrEqual(a) {
|
|
252
|
+
return this.compareTo(a) <= 0 ? true : false;
|
|
253
|
+
}
|
|
254
|
+
function nbits(x) {
|
|
255
|
+
var r = 1, t;
|
|
256
|
+
if ((t = x >>> 16) != 0) {
|
|
257
|
+
x = t;
|
|
258
|
+
r += 16;
|
|
259
|
+
}
|
|
260
|
+
if ((t = x >> 8) != 0) {
|
|
261
|
+
x = t;
|
|
262
|
+
r += 8;
|
|
263
|
+
}
|
|
264
|
+
if ((t = x >> 4) != 0) {
|
|
265
|
+
x = t;
|
|
266
|
+
r += 4;
|
|
267
|
+
}
|
|
268
|
+
if ((t = x >> 2) != 0) {
|
|
269
|
+
x = t;
|
|
270
|
+
r += 2;
|
|
271
|
+
}
|
|
272
|
+
if ((t = x >> 1) != 0) {
|
|
273
|
+
x = t;
|
|
274
|
+
r += 1;
|
|
275
|
+
}
|
|
276
|
+
return r;
|
|
277
|
+
}
|
|
278
|
+
function bnBitLength() {
|
|
279
|
+
if (this.t <= 0) return 0;
|
|
280
|
+
return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ this.s & this.DM);
|
|
281
|
+
}
|
|
282
|
+
function bnpDLShiftTo(n, r) {
|
|
283
|
+
var i;
|
|
284
|
+
for (i = this.t - 1; i >= 0; --i) r[i + n] = this[i];
|
|
285
|
+
for (i = n - 1; i >= 0; --i) r[i] = 0;
|
|
286
|
+
r.t = this.t + n;
|
|
287
|
+
r.s = this.s;
|
|
288
|
+
}
|
|
289
|
+
function bnpDRShiftTo(n, r) {
|
|
290
|
+
for (var i = n; i < this.t; ++i) r[i - n] = this[i];
|
|
291
|
+
r.t = Math.max(this.t - n, 0);
|
|
292
|
+
r.s = this.s;
|
|
293
|
+
}
|
|
294
|
+
function bnpLShiftTo(n, r) {
|
|
295
|
+
var bs = n % this.DB;
|
|
296
|
+
var cbs = this.DB - bs;
|
|
297
|
+
var bm = (1 << cbs) - 1;
|
|
298
|
+
var ds = Math.floor(n / this.DB), c = this.s << bs & this.DM, i;
|
|
299
|
+
for (i = this.t - 1; i >= 0; --i) {
|
|
300
|
+
r[i + ds + 1] = this[i] >> cbs | c;
|
|
301
|
+
c = (this[i] & bm) << bs;
|
|
302
|
+
}
|
|
303
|
+
for (i = ds - 1; i >= 0; --i) r[i] = 0;
|
|
304
|
+
r[ds] = c;
|
|
305
|
+
r.t = this.t + ds + 1;
|
|
306
|
+
r.s = this.s;
|
|
307
|
+
r.clamp();
|
|
308
|
+
}
|
|
309
|
+
function bnpRShiftTo(n, r) {
|
|
310
|
+
r.s = this.s;
|
|
311
|
+
var ds = Math.floor(n / this.DB);
|
|
312
|
+
if (ds >= this.t) {
|
|
313
|
+
r.t = 0;
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
var bs = n % this.DB;
|
|
317
|
+
var cbs = this.DB - bs;
|
|
318
|
+
var bm = (1 << bs) - 1;
|
|
319
|
+
r[0] = this[ds] >> bs;
|
|
320
|
+
for (var i = ds + 1; i < this.t; ++i) {
|
|
321
|
+
r[i - ds - 1] |= (this[i] & bm) << cbs;
|
|
322
|
+
r[i - ds] = this[i] >> bs;
|
|
323
|
+
}
|
|
324
|
+
if (bs > 0) r[this.t - ds - 1] |= (this.s & bm) << cbs;
|
|
325
|
+
r.t = this.t - ds;
|
|
326
|
+
r.clamp();
|
|
327
|
+
}
|
|
328
|
+
function bnpSubTo(a, r) {
|
|
329
|
+
var i = 0, c = 0, m = Math.min(a.t, this.t);
|
|
330
|
+
while (i < m) {
|
|
331
|
+
c += this[i] - a[i];
|
|
332
|
+
r[i++] = c & this.DM;
|
|
333
|
+
c >>= this.DB;
|
|
334
|
+
}
|
|
335
|
+
if (a.t < this.t) {
|
|
336
|
+
c -= a.s;
|
|
337
|
+
while (i < this.t) {
|
|
338
|
+
c += this[i];
|
|
339
|
+
r[i++] = c & this.DM;
|
|
340
|
+
c >>= this.DB;
|
|
341
|
+
}
|
|
342
|
+
c += this.s;
|
|
343
|
+
} else {
|
|
344
|
+
c += this.s;
|
|
345
|
+
while (i < a.t) {
|
|
346
|
+
c -= a[i];
|
|
347
|
+
r[i++] = c & this.DM;
|
|
348
|
+
c >>= this.DB;
|
|
349
|
+
}
|
|
350
|
+
c -= a.s;
|
|
351
|
+
}
|
|
352
|
+
r.s = c < 0 ? -1 : 0;
|
|
353
|
+
if (c < -1) r[i++] = this.DV + c;
|
|
354
|
+
else if (c > 0) r[i++] = c;
|
|
355
|
+
r.t = i;
|
|
356
|
+
r.clamp();
|
|
357
|
+
}
|
|
358
|
+
function bnpMultiplyTo(a, r) {
|
|
359
|
+
var x = this.abs(), y = a.abs();
|
|
360
|
+
var i = x.t;
|
|
361
|
+
r.t = i + y.t;
|
|
362
|
+
while (--i >= 0) r[i] = 0;
|
|
363
|
+
for (i = 0; i < y.t; ++i) r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
|
|
364
|
+
r.s = 0;
|
|
365
|
+
r.clamp();
|
|
366
|
+
if (this.s != a.s) BigInteger.ZERO.subTo(r, r);
|
|
367
|
+
}
|
|
368
|
+
function bnpSquareTo(r) {
|
|
369
|
+
var x = this.abs();
|
|
370
|
+
var i = r.t = 2 * x.t;
|
|
371
|
+
while (--i >= 0) r[i] = 0;
|
|
372
|
+
for (i = 0; i < x.t - 1; ++i) {
|
|
373
|
+
var c = x.am(i, x[i], r, 2 * i, 0, 1);
|
|
374
|
+
if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
|
|
375
|
+
r[i + x.t] -= x.DV;
|
|
376
|
+
r[i + x.t + 1] = 1;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (r.t > 0) r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
|
|
380
|
+
r.s = 0;
|
|
381
|
+
r.clamp();
|
|
382
|
+
}
|
|
383
|
+
function bnpDivRemTo(m, q, r) {
|
|
384
|
+
var pm = m.abs();
|
|
385
|
+
if (pm.t <= 0) return;
|
|
386
|
+
var pt = this.abs();
|
|
387
|
+
if (pt.t < pm.t) {
|
|
388
|
+
if (q != null) q.fromInt(0);
|
|
389
|
+
if (r != null) this.copyTo(r);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (r == null) r = nbi();
|
|
393
|
+
var y = nbi(), ts = this.s, ms = m.s;
|
|
394
|
+
var nsh = this.DB - nbits(pm[pm.t - 1]);
|
|
395
|
+
if (nsh > 0) {
|
|
396
|
+
pm.lShiftTo(nsh, y);
|
|
397
|
+
pt.lShiftTo(nsh, r);
|
|
398
|
+
} else {
|
|
399
|
+
pm.copyTo(y);
|
|
400
|
+
pt.copyTo(r);
|
|
401
|
+
}
|
|
402
|
+
var ys = y.t;
|
|
403
|
+
var y0 = y[ys - 1];
|
|
404
|
+
if (y0 === 0) return;
|
|
405
|
+
var yt = y0 * (1 << this.F1) + (ys > 1 ? y[ys - 2] >> this.F2 : 0);
|
|
406
|
+
var d1 = this.FV / yt, d2 = (1 << this.F1) / yt, e = 1 << this.F2;
|
|
407
|
+
var i = r.t, j = i - ys, t = q == null ? nbi() : q;
|
|
408
|
+
y.dlShiftTo(j, t);
|
|
409
|
+
if (r.compareTo(t) >= 0) {
|
|
410
|
+
r[r.t++] = 1;
|
|
411
|
+
r.subTo(t, r);
|
|
412
|
+
}
|
|
413
|
+
BigInteger.ONE.dlShiftTo(ys, t);
|
|
414
|
+
t.subTo(y, y);
|
|
415
|
+
while (y.t < ys) y[y.t++] = 0;
|
|
416
|
+
while (--j >= 0) {
|
|
417
|
+
var qd = r[--i] == y0 ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
|
|
418
|
+
if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) {
|
|
419
|
+
y.dlShiftTo(j, t);
|
|
420
|
+
r.subTo(t, r);
|
|
421
|
+
while (r[i] < --qd) r.subTo(t, r);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
if (q != null) {
|
|
425
|
+
r.drShiftTo(ys, q);
|
|
426
|
+
if (ts != ms) BigInteger.ZERO.subTo(q, q);
|
|
427
|
+
}
|
|
428
|
+
r.t = ys;
|
|
429
|
+
r.clamp();
|
|
430
|
+
if (nsh > 0) r.rShiftTo(nsh, r);
|
|
431
|
+
if (ts < 0) BigInteger.ZERO.subTo(r, r);
|
|
432
|
+
}
|
|
433
|
+
function bnMod(a) {
|
|
434
|
+
var r = nbi();
|
|
435
|
+
this.abs().divRemTo(a, null, r);
|
|
436
|
+
if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r, r);
|
|
437
|
+
return r;
|
|
438
|
+
}
|
|
439
|
+
function Classic(m) {
|
|
440
|
+
this.m = m;
|
|
441
|
+
}
|
|
442
|
+
function cConvert(x) {
|
|
443
|
+
if (x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
|
|
444
|
+
else return x;
|
|
445
|
+
}
|
|
446
|
+
function cRevert(x) {
|
|
447
|
+
return x;
|
|
448
|
+
}
|
|
449
|
+
function cReduce(x) {
|
|
450
|
+
x.divRemTo(this.m, null, x);
|
|
451
|
+
}
|
|
452
|
+
function cMulTo(x, y, r) {
|
|
453
|
+
x.multiplyTo(y, r);
|
|
454
|
+
this.reduce(r);
|
|
455
|
+
}
|
|
456
|
+
function cSqrTo(x, r) {
|
|
457
|
+
x.squareTo(r);
|
|
458
|
+
this.reduce(r);
|
|
459
|
+
}
|
|
460
|
+
Classic.prototype.convert = cConvert;
|
|
461
|
+
Classic.prototype.revert = cRevert;
|
|
462
|
+
Classic.prototype.reduce = cReduce;
|
|
463
|
+
Classic.prototype.mulTo = cMulTo;
|
|
464
|
+
Classic.prototype.sqrTo = cSqrTo;
|
|
465
|
+
function bnpInvDigit() {
|
|
466
|
+
if (this.t < 1) return 0;
|
|
467
|
+
var x = this[0];
|
|
468
|
+
if ((x & 1) === 0) return 0;
|
|
469
|
+
var y = x & 3;
|
|
470
|
+
y = y * (2 - (x & 15) * y) & 15;
|
|
471
|
+
y = y * (2 - (x & 255) * y) & 255;
|
|
472
|
+
y = y * (2 - ((x & 65535) * y & 65535)) & 65535;
|
|
473
|
+
y = y * (2 - x * y % this.DV) % this.DV;
|
|
474
|
+
return y > 0 ? this.DV - y : -y;
|
|
475
|
+
}
|
|
476
|
+
function Montgomery(m) {
|
|
477
|
+
this.m = m;
|
|
478
|
+
this.mp = m.invDigit();
|
|
479
|
+
this.mpl = this.mp & 32767;
|
|
480
|
+
this.mph = this.mp >> 15;
|
|
481
|
+
this.um = (1 << m.DB - 15) - 1;
|
|
482
|
+
this.mt2 = 2 * m.t;
|
|
483
|
+
}
|
|
484
|
+
function montConvert(x) {
|
|
485
|
+
var r = nbi();
|
|
486
|
+
x.abs().dlShiftTo(this.m.t, r);
|
|
487
|
+
r.divRemTo(this.m, null, r);
|
|
488
|
+
if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r, r);
|
|
489
|
+
return r;
|
|
490
|
+
}
|
|
491
|
+
function montRevert(x) {
|
|
492
|
+
var r = nbi();
|
|
493
|
+
x.copyTo(r);
|
|
494
|
+
this.reduce(r);
|
|
495
|
+
return r;
|
|
496
|
+
}
|
|
497
|
+
function montReduce(x) {
|
|
498
|
+
while (x.t <= this.mt2) x[x.t++] = 0;
|
|
499
|
+
for (var i = 0; i < this.m.t; ++i) {
|
|
500
|
+
var j = x[i] & 32767;
|
|
501
|
+
var u0 = j * this.mpl + ((j * this.mph + (x[i] >> 15) * this.mpl & this.um) << 15) & x.DM;
|
|
502
|
+
j = i + this.m.t;
|
|
503
|
+
x[j] += this.m.am(0, u0, x, i, 0, this.m.t);
|
|
504
|
+
while (x[j] >= x.DV) {
|
|
505
|
+
x[j] -= x.DV;
|
|
506
|
+
x[++j]++;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
x.clamp();
|
|
510
|
+
x.drShiftTo(this.m.t, x);
|
|
511
|
+
if (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
|
|
512
|
+
}
|
|
513
|
+
function montSqrTo(x, r) {
|
|
514
|
+
x.squareTo(r);
|
|
515
|
+
this.reduce(r);
|
|
516
|
+
}
|
|
517
|
+
function montMulTo(x, y, r) {
|
|
518
|
+
x.multiplyTo(y, r);
|
|
519
|
+
this.reduce(r);
|
|
520
|
+
}
|
|
521
|
+
Montgomery.prototype.convert = montConvert;
|
|
522
|
+
Montgomery.prototype.revert = montRevert;
|
|
523
|
+
Montgomery.prototype.reduce = montReduce;
|
|
524
|
+
Montgomery.prototype.mulTo = montMulTo;
|
|
525
|
+
Montgomery.prototype.sqrTo = montSqrTo;
|
|
526
|
+
function bnpIsEven() {
|
|
527
|
+
return (this.t > 0 ? this[0] & 1 : this.s) === 0;
|
|
528
|
+
}
|
|
529
|
+
function bnpExp(e, z) {
|
|
530
|
+
if (e > 4294967295 || e < 1) return BigInteger.ONE;
|
|
531
|
+
var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e) - 1;
|
|
532
|
+
g.copyTo(r);
|
|
533
|
+
while (--i >= 0) {
|
|
534
|
+
z.sqrTo(r, r2);
|
|
535
|
+
if ((e & 1 << i) > 0) z.mulTo(r2, g, r);
|
|
536
|
+
else {
|
|
537
|
+
var t = r;
|
|
538
|
+
r = r2;
|
|
539
|
+
r2 = t;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return z.revert(r);
|
|
543
|
+
}
|
|
544
|
+
function bnModPowInt(e, m) {
|
|
545
|
+
var z;
|
|
546
|
+
if (e < 256 || m.isEven()) z = new Classic(m);
|
|
547
|
+
else z = new Montgomery(m);
|
|
548
|
+
return this.exp(e, z);
|
|
549
|
+
}
|
|
550
|
+
function bnClone() {
|
|
551
|
+
var r = nbi();
|
|
552
|
+
this.copyTo(r);
|
|
553
|
+
return r;
|
|
554
|
+
}
|
|
555
|
+
function bnIntValue() {
|
|
556
|
+
if (this.s < 0) {
|
|
557
|
+
if (this.t == 1) return this[0] - this.DV;
|
|
558
|
+
else if (this.t === 0) return -1;
|
|
559
|
+
} else if (this.t == 1) return this[0];
|
|
560
|
+
else if (this.t === 0) return 0;
|
|
561
|
+
return (this[1] & (1 << 32 - this.DB) - 1) << this.DB | this[0];
|
|
562
|
+
}
|
|
563
|
+
function bnByteValue() {
|
|
564
|
+
return this.t == 0 ? this.s : this[0] << 24 >> 24;
|
|
565
|
+
}
|
|
566
|
+
function bnShortValue() {
|
|
567
|
+
return this.t == 0 ? this.s : this[0] << 16 >> 16;
|
|
568
|
+
}
|
|
569
|
+
function bnpChunkSize(r) {
|
|
570
|
+
return Math.floor(Math.LN2 * this.DB / Math.log(r));
|
|
571
|
+
}
|
|
572
|
+
function bnSigNum() {
|
|
573
|
+
if (this.s < 0) return -1;
|
|
574
|
+
else if (this.t <= 0 || this.t == 1 && this[0] <= 0) return 0;
|
|
575
|
+
else return 1;
|
|
576
|
+
}
|
|
577
|
+
function bnpToRadix(b) {
|
|
578
|
+
if (b == null) b = 10;
|
|
579
|
+
if (this.signum() === 0 || b < 2 || b > 36) return "0";
|
|
580
|
+
var cs = this.chunkSize(b);
|
|
581
|
+
var a = Math.pow(b, cs);
|
|
582
|
+
var d = nbv(a), y = nbi(), z = nbi(), r = "";
|
|
583
|
+
this.divRemTo(d, y, z);
|
|
584
|
+
while (y.signum() > 0) {
|
|
585
|
+
r = (a + z.intValue()).toString(b).substr(1) + r;
|
|
586
|
+
y.divRemTo(d, y, z);
|
|
587
|
+
}
|
|
588
|
+
return z.intValue().toString(b) + r;
|
|
589
|
+
}
|
|
590
|
+
function bnpFromRadix(s, b) {
|
|
591
|
+
this.fromInt(0);
|
|
592
|
+
if (b == null) b = 10;
|
|
593
|
+
var cs = this.chunkSize(b);
|
|
594
|
+
var d = Math.pow(b, cs), mi = false, j = 0, w = 0;
|
|
595
|
+
for (var i = 0; i < s.length; ++i) {
|
|
596
|
+
var x = intAt(s, i);
|
|
597
|
+
if (x < 0) {
|
|
598
|
+
if (s.charAt(i) == "-" && this.signum() === 0) mi = true;
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
w = b * w + x;
|
|
602
|
+
if (++j >= cs) {
|
|
603
|
+
this.dMultiply(d);
|
|
604
|
+
this.dAddOffset(w, 0);
|
|
605
|
+
j = 0;
|
|
606
|
+
w = 0;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
if (j > 0) {
|
|
610
|
+
this.dMultiply(Math.pow(b, j));
|
|
611
|
+
this.dAddOffset(w, 0);
|
|
612
|
+
}
|
|
613
|
+
if (mi) BigInteger.ZERO.subTo(this, this);
|
|
614
|
+
}
|
|
615
|
+
function bnpFromNumber(a, b) {
|
|
616
|
+
if ("number" == typeof b) if (a < 2) this.fromInt(1);
|
|
617
|
+
else {
|
|
618
|
+
this.fromNumber(a);
|
|
619
|
+
if (!this.testBit(a - 1)) this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
|
|
620
|
+
if (this.isEven()) this.dAddOffset(1, 0);
|
|
621
|
+
while (!this.isProbablePrime(b)) {
|
|
622
|
+
this.dAddOffset(2, 0);
|
|
623
|
+
if (this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
var x = crypt.randomBytes((a >> 3) + 1);
|
|
628
|
+
var t = a & 7;
|
|
629
|
+
if (t > 0) x[0] &= (1 << t) - 1;
|
|
630
|
+
else x[0] = 0;
|
|
631
|
+
this.fromByteArray(x);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
function bnToByteArray() {
|
|
635
|
+
var i = this.t, r = new Array();
|
|
636
|
+
r[0] = this.s;
|
|
637
|
+
var p = this.DB - i * this.DB % 8, d, k = 0;
|
|
638
|
+
if (i-- > 0) {
|
|
639
|
+
if (p < this.DB && (d = this[i] >> p) != (this.s & this.DM) >> p) r[k++] = d | this.s << this.DB - p;
|
|
640
|
+
while (i >= 0) {
|
|
641
|
+
if (p < 8) {
|
|
642
|
+
d = (this[i] & (1 << p) - 1) << 8 - p;
|
|
643
|
+
d |= this[--i] >> (p += this.DB - 8);
|
|
644
|
+
} else {
|
|
645
|
+
d = this[i] >> (p -= 8) & 255;
|
|
646
|
+
if (p <= 0) {
|
|
647
|
+
p += this.DB;
|
|
648
|
+
--i;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
if ((d & 128) != 0) d |= -256;
|
|
652
|
+
if (k === 0 && (this.s & 128) != (d & 128)) ++k;
|
|
653
|
+
if (k > 0 || d != this.s) r[k++] = d;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
return r;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* return Buffer object
|
|
660
|
+
* @param trim {boolean} slice buffer if first element == 0
|
|
661
|
+
* @returns {Buffer}
|
|
662
|
+
*/
|
|
663
|
+
function bnToBuffer(trimOrSize) {
|
|
664
|
+
var res = Buffer.from(this.toByteArray());
|
|
665
|
+
if (trimOrSize === true && res[0] === 0) res = res.slice(1);
|
|
666
|
+
else if (typeof trimOrSize == "number") {
|
|
667
|
+
if (res.length > trimOrSize) {
|
|
668
|
+
for (var i = 0; i < res.length - trimOrSize; i++) if (res[i] !== 0) return null;
|
|
669
|
+
return res.slice(res.length - trimOrSize);
|
|
670
|
+
} else if (res.length < trimOrSize) {
|
|
671
|
+
var padded = Buffer.alloc(trimOrSize);
|
|
672
|
+
padded.fill(0, 0, trimOrSize - res.length);
|
|
673
|
+
res.copy(padded, trimOrSize - res.length);
|
|
674
|
+
return padded;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
return res;
|
|
678
|
+
}
|
|
679
|
+
function bnEquals(a) {
|
|
680
|
+
return this.compareTo(a) == 0;
|
|
681
|
+
}
|
|
682
|
+
function bnMin(a) {
|
|
683
|
+
return this.compareTo(a) < 0 ? this : a;
|
|
684
|
+
}
|
|
685
|
+
function bnMax(a) {
|
|
686
|
+
return this.compareTo(a) > 0 ? this : a;
|
|
687
|
+
}
|
|
688
|
+
function bnpBitwiseTo(a, op, r) {
|
|
689
|
+
var i, f, m = Math.min(a.t, this.t);
|
|
690
|
+
for (i = 0; i < m; ++i) r[i] = op(this[i], a[i]);
|
|
691
|
+
if (a.t < this.t) {
|
|
692
|
+
f = a.s & this.DM;
|
|
693
|
+
for (i = m; i < this.t; ++i) r[i] = op(this[i], f);
|
|
694
|
+
r.t = this.t;
|
|
695
|
+
} else {
|
|
696
|
+
f = this.s & this.DM;
|
|
697
|
+
for (i = m; i < a.t; ++i) r[i] = op(f, a[i]);
|
|
698
|
+
r.t = a.t;
|
|
699
|
+
}
|
|
700
|
+
r.s = op(this.s, a.s);
|
|
701
|
+
r.clamp();
|
|
702
|
+
}
|
|
703
|
+
function op_and(x, y) {
|
|
704
|
+
return x & y;
|
|
705
|
+
}
|
|
706
|
+
function bnAnd(a) {
|
|
707
|
+
var r = nbi();
|
|
708
|
+
this.bitwiseTo(a, op_and, r);
|
|
709
|
+
return r;
|
|
710
|
+
}
|
|
711
|
+
function op_or(x, y) {
|
|
712
|
+
return x | y;
|
|
713
|
+
}
|
|
714
|
+
function bnOr(a) {
|
|
715
|
+
var r = nbi();
|
|
716
|
+
this.bitwiseTo(a, op_or, r);
|
|
717
|
+
return r;
|
|
718
|
+
}
|
|
719
|
+
function op_xor(x, y) {
|
|
720
|
+
return x ^ y;
|
|
721
|
+
}
|
|
722
|
+
function bnXor(a) {
|
|
723
|
+
var r = nbi();
|
|
724
|
+
this.bitwiseTo(a, op_xor, r);
|
|
725
|
+
return r;
|
|
726
|
+
}
|
|
727
|
+
function op_andnot(x, y) {
|
|
728
|
+
return x & ~y;
|
|
729
|
+
}
|
|
730
|
+
function bnAndNot(a) {
|
|
731
|
+
var r = nbi();
|
|
732
|
+
this.bitwiseTo(a, op_andnot, r);
|
|
733
|
+
return r;
|
|
734
|
+
}
|
|
735
|
+
function bnNot() {
|
|
736
|
+
var r = nbi();
|
|
737
|
+
for (var i = 0; i < this.t; ++i) r[i] = this.DM & ~this[i];
|
|
738
|
+
r.t = this.t;
|
|
739
|
+
r.s = ~this.s;
|
|
740
|
+
return r;
|
|
741
|
+
}
|
|
742
|
+
function bnShiftLeft(n) {
|
|
743
|
+
var r = nbi();
|
|
744
|
+
if (n < 0) this.rShiftTo(-n, r);
|
|
745
|
+
else this.lShiftTo(n, r);
|
|
746
|
+
return r;
|
|
747
|
+
}
|
|
748
|
+
function bnShiftRight(n) {
|
|
749
|
+
var r = nbi();
|
|
750
|
+
if (n < 0) this.lShiftTo(-n, r);
|
|
751
|
+
else this.rShiftTo(n, r);
|
|
752
|
+
return r;
|
|
753
|
+
}
|
|
754
|
+
function lbit(x) {
|
|
755
|
+
if (x === 0) return -1;
|
|
756
|
+
var r = 0;
|
|
757
|
+
if ((x & 65535) === 0) {
|
|
758
|
+
x >>= 16;
|
|
759
|
+
r += 16;
|
|
760
|
+
}
|
|
761
|
+
if ((x & 255) === 0) {
|
|
762
|
+
x >>= 8;
|
|
763
|
+
r += 8;
|
|
764
|
+
}
|
|
765
|
+
if ((x & 15) === 0) {
|
|
766
|
+
x >>= 4;
|
|
767
|
+
r += 4;
|
|
768
|
+
}
|
|
769
|
+
if ((x & 3) === 0) {
|
|
770
|
+
x >>= 2;
|
|
771
|
+
r += 2;
|
|
772
|
+
}
|
|
773
|
+
if ((x & 1) === 0) ++r;
|
|
774
|
+
return r;
|
|
775
|
+
}
|
|
776
|
+
function bnGetLowestSetBit() {
|
|
777
|
+
for (var i = 0; i < this.t; ++i) if (this[i] != 0) return i * this.DB + lbit(this[i]);
|
|
778
|
+
if (this.s < 0) return this.t * this.DB;
|
|
779
|
+
return -1;
|
|
780
|
+
}
|
|
781
|
+
function cbit(x) {
|
|
782
|
+
var r = 0;
|
|
783
|
+
while (x != 0) {
|
|
784
|
+
x &= x - 1;
|
|
785
|
+
++r;
|
|
786
|
+
}
|
|
787
|
+
return r;
|
|
788
|
+
}
|
|
789
|
+
function bnBitCount() {
|
|
790
|
+
var r = 0, x = this.s & this.DM;
|
|
791
|
+
for (var i = 0; i < this.t; ++i) r += cbit(this[i] ^ x);
|
|
792
|
+
return r;
|
|
793
|
+
}
|
|
794
|
+
function bnTestBit(n) {
|
|
795
|
+
var j = Math.floor(n / this.DB);
|
|
796
|
+
if (j >= this.t) return this.s != 0;
|
|
797
|
+
return (this[j] & 1 << n % this.DB) != 0;
|
|
798
|
+
}
|
|
799
|
+
function bnpChangeBit(n, op) {
|
|
800
|
+
var r = BigInteger.ONE.shiftLeft(n);
|
|
801
|
+
this.bitwiseTo(r, op, r);
|
|
802
|
+
return r;
|
|
803
|
+
}
|
|
804
|
+
function bnSetBit(n) {
|
|
805
|
+
return this.changeBit(n, op_or);
|
|
806
|
+
}
|
|
807
|
+
function bnClearBit(n) {
|
|
808
|
+
return this.changeBit(n, op_andnot);
|
|
809
|
+
}
|
|
810
|
+
function bnFlipBit(n) {
|
|
811
|
+
return this.changeBit(n, op_xor);
|
|
812
|
+
}
|
|
813
|
+
function bnpAddTo(a, r) {
|
|
814
|
+
var i = 0, c = 0, m = Math.min(a.t, this.t);
|
|
815
|
+
while (i < m) {
|
|
816
|
+
c += this[i] + a[i];
|
|
817
|
+
r[i++] = c & this.DM;
|
|
818
|
+
c >>= this.DB;
|
|
819
|
+
}
|
|
820
|
+
if (a.t < this.t) {
|
|
821
|
+
c += a.s;
|
|
822
|
+
while (i < this.t) {
|
|
823
|
+
c += this[i];
|
|
824
|
+
r[i++] = c & this.DM;
|
|
825
|
+
c >>= this.DB;
|
|
826
|
+
}
|
|
827
|
+
c += this.s;
|
|
828
|
+
} else {
|
|
829
|
+
c += this.s;
|
|
830
|
+
while (i < a.t) {
|
|
831
|
+
c += a[i];
|
|
832
|
+
r[i++] = c & this.DM;
|
|
833
|
+
c >>= this.DB;
|
|
834
|
+
}
|
|
835
|
+
c += a.s;
|
|
836
|
+
}
|
|
837
|
+
r.s = c < 0 ? -1 : 0;
|
|
838
|
+
if (c > 0) r[i++] = c;
|
|
839
|
+
else if (c < -1) r[i++] = this.DV + c;
|
|
840
|
+
r.t = i;
|
|
841
|
+
r.clamp();
|
|
842
|
+
}
|
|
843
|
+
function bnAdd(a) {
|
|
844
|
+
var r = nbi();
|
|
845
|
+
this.addTo(a, r);
|
|
846
|
+
return r;
|
|
847
|
+
}
|
|
848
|
+
function bnSubtract(a) {
|
|
849
|
+
var r = nbi();
|
|
850
|
+
this.subTo(a, r);
|
|
851
|
+
return r;
|
|
852
|
+
}
|
|
853
|
+
function bnMultiply(a) {
|
|
854
|
+
var r = nbi();
|
|
855
|
+
this.multiplyTo(a, r);
|
|
856
|
+
return r;
|
|
857
|
+
}
|
|
858
|
+
function bnSquare() {
|
|
859
|
+
var r = nbi();
|
|
860
|
+
this.squareTo(r);
|
|
861
|
+
return r;
|
|
862
|
+
}
|
|
863
|
+
function bnDivide(a) {
|
|
864
|
+
var r = nbi();
|
|
865
|
+
this.divRemTo(a, r, null);
|
|
866
|
+
return r;
|
|
867
|
+
}
|
|
868
|
+
function bnRemainder(a) {
|
|
869
|
+
var r = nbi();
|
|
870
|
+
this.divRemTo(a, null, r);
|
|
871
|
+
return r;
|
|
872
|
+
}
|
|
873
|
+
function bnDivideAndRemainder(a) {
|
|
874
|
+
var q = nbi(), r = nbi();
|
|
875
|
+
this.divRemTo(a, q, r);
|
|
876
|
+
return new Array(q, r);
|
|
877
|
+
}
|
|
878
|
+
function bnpDMultiply(n) {
|
|
879
|
+
this[this.t] = this.am(0, n - 1, this, 0, 0, this.t);
|
|
880
|
+
++this.t;
|
|
881
|
+
this.clamp();
|
|
882
|
+
}
|
|
883
|
+
function bnpDAddOffset(n, w) {
|
|
884
|
+
if (n === 0) return;
|
|
885
|
+
while (this.t <= w) this[this.t++] = 0;
|
|
886
|
+
this[w] += n;
|
|
887
|
+
while (this[w] >= this.DV) {
|
|
888
|
+
this[w] -= this.DV;
|
|
889
|
+
if (++w >= this.t) this[this.t++] = 0;
|
|
890
|
+
++this[w];
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
function NullExp() {}
|
|
894
|
+
function nNop(x) {
|
|
895
|
+
return x;
|
|
896
|
+
}
|
|
897
|
+
function nMulTo(x, y, r) {
|
|
898
|
+
x.multiplyTo(y, r);
|
|
899
|
+
}
|
|
900
|
+
function nSqrTo(x, r) {
|
|
901
|
+
x.squareTo(r);
|
|
902
|
+
}
|
|
903
|
+
NullExp.prototype.convert = nNop;
|
|
904
|
+
NullExp.prototype.revert = nNop;
|
|
905
|
+
NullExp.prototype.mulTo = nMulTo;
|
|
906
|
+
NullExp.prototype.sqrTo = nSqrTo;
|
|
907
|
+
function bnPow(e) {
|
|
908
|
+
return this.exp(e, new NullExp());
|
|
909
|
+
}
|
|
910
|
+
function bnpMultiplyLowerTo(a, n, r) {
|
|
911
|
+
var i = Math.min(this.t + a.t, n);
|
|
912
|
+
r.s = 0;
|
|
913
|
+
r.t = i;
|
|
914
|
+
while (i > 0) r[--i] = 0;
|
|
915
|
+
var j;
|
|
916
|
+
for (j = r.t - this.t; i < j; ++i) r[i + this.t] = this.am(0, a[i], r, i, 0, this.t);
|
|
917
|
+
for (j = Math.min(a.t, n); i < j; ++i) this.am(0, a[i], r, i, 0, n - i);
|
|
918
|
+
r.clamp();
|
|
919
|
+
}
|
|
920
|
+
function bnpMultiplyUpperTo(a, n, r) {
|
|
921
|
+
--n;
|
|
922
|
+
var i = r.t = this.t + a.t - n;
|
|
923
|
+
r.s = 0;
|
|
924
|
+
while (--i >= 0) r[i] = 0;
|
|
925
|
+
for (i = Math.max(n - this.t, 0); i < a.t; ++i) r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n);
|
|
926
|
+
r.clamp();
|
|
927
|
+
r.drShiftTo(1, r);
|
|
928
|
+
}
|
|
929
|
+
function Barrett(m) {
|
|
930
|
+
this.r2 = nbi();
|
|
931
|
+
this.q3 = nbi();
|
|
932
|
+
BigInteger.ONE.dlShiftTo(2 * m.t, this.r2);
|
|
933
|
+
this.mu = this.r2.divide(m);
|
|
934
|
+
this.m = m;
|
|
935
|
+
}
|
|
936
|
+
function barrettConvert(x) {
|
|
937
|
+
if (x.s < 0 || x.t > 2 * this.m.t) return x.mod(this.m);
|
|
938
|
+
else if (x.compareTo(this.m) < 0) return x;
|
|
939
|
+
else {
|
|
940
|
+
var r = nbi();
|
|
941
|
+
x.copyTo(r);
|
|
942
|
+
this.reduce(r);
|
|
943
|
+
return r;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
function barrettRevert(x) {
|
|
947
|
+
return x;
|
|
948
|
+
}
|
|
949
|
+
function barrettReduce(x) {
|
|
950
|
+
x.drShiftTo(this.m.t - 1, this.r2);
|
|
951
|
+
if (x.t > this.m.t + 1) {
|
|
952
|
+
x.t = this.m.t + 1;
|
|
953
|
+
x.clamp();
|
|
954
|
+
}
|
|
955
|
+
this.mu.multiplyUpperTo(this.r2, this.m.t + 1, this.q3);
|
|
956
|
+
this.m.multiplyLowerTo(this.q3, this.m.t + 1, this.r2);
|
|
957
|
+
while (x.compareTo(this.r2) < 0) x.dAddOffset(1, this.m.t + 1);
|
|
958
|
+
x.subTo(this.r2, x);
|
|
959
|
+
while (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
|
|
960
|
+
}
|
|
961
|
+
function barrettSqrTo(x, r) {
|
|
962
|
+
x.squareTo(r);
|
|
963
|
+
this.reduce(r);
|
|
964
|
+
}
|
|
965
|
+
function barrettMulTo(x, y, r) {
|
|
966
|
+
x.multiplyTo(y, r);
|
|
967
|
+
this.reduce(r);
|
|
968
|
+
}
|
|
969
|
+
Barrett.prototype.convert = barrettConvert;
|
|
970
|
+
Barrett.prototype.revert = barrettRevert;
|
|
971
|
+
Barrett.prototype.reduce = barrettReduce;
|
|
972
|
+
Barrett.prototype.mulTo = barrettMulTo;
|
|
973
|
+
Barrett.prototype.sqrTo = barrettSqrTo;
|
|
974
|
+
function bnModPow(e, m) {
|
|
975
|
+
var i = e.bitLength(), k, r = nbv(1), z;
|
|
976
|
+
if (i <= 0) return r;
|
|
977
|
+
else if (i < 18) k = 1;
|
|
978
|
+
else if (i < 48) k = 3;
|
|
979
|
+
else if (i < 144) k = 4;
|
|
980
|
+
else if (i < 768) k = 5;
|
|
981
|
+
else k = 6;
|
|
982
|
+
if (i < 8) z = new Classic(m);
|
|
983
|
+
else if (m.isEven()) z = new Barrett(m);
|
|
984
|
+
else z = new Montgomery(m);
|
|
985
|
+
var g = new Array(), n = 3, k1 = k - 1, km = (1 << k) - 1;
|
|
986
|
+
g[1] = z.convert(this);
|
|
987
|
+
if (k > 1) {
|
|
988
|
+
var g2 = nbi();
|
|
989
|
+
z.sqrTo(g[1], g2);
|
|
990
|
+
while (n <= km) {
|
|
991
|
+
g[n] = nbi();
|
|
992
|
+
z.mulTo(g2, g[n - 2], g[n]);
|
|
993
|
+
n += 2;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
var j = e.t - 1, w, is1 = true, r2 = nbi(), t;
|
|
997
|
+
i = nbits(e[j]) - 1;
|
|
998
|
+
while (j >= 0) {
|
|
999
|
+
if (i >= k1) w = e[j] >> i - k1 & km;
|
|
1000
|
+
else {
|
|
1001
|
+
w = (e[j] & (1 << i + 1) - 1) << k1 - i;
|
|
1002
|
+
if (j > 0) w |= e[j - 1] >> this.DB + i - k1;
|
|
1003
|
+
}
|
|
1004
|
+
n = k;
|
|
1005
|
+
while ((w & 1) === 0) {
|
|
1006
|
+
w >>= 1;
|
|
1007
|
+
--n;
|
|
1008
|
+
}
|
|
1009
|
+
if ((i -= n) < 0) {
|
|
1010
|
+
i += this.DB;
|
|
1011
|
+
--j;
|
|
1012
|
+
}
|
|
1013
|
+
if (is1) {
|
|
1014
|
+
g[w].copyTo(r);
|
|
1015
|
+
is1 = false;
|
|
1016
|
+
} else {
|
|
1017
|
+
while (n > 1) {
|
|
1018
|
+
z.sqrTo(r, r2);
|
|
1019
|
+
z.sqrTo(r2, r);
|
|
1020
|
+
n -= 2;
|
|
1021
|
+
}
|
|
1022
|
+
if (n > 0) z.sqrTo(r, r2);
|
|
1023
|
+
else {
|
|
1024
|
+
t = r;
|
|
1025
|
+
r = r2;
|
|
1026
|
+
r2 = t;
|
|
1027
|
+
}
|
|
1028
|
+
z.mulTo(r2, g[w], r);
|
|
1029
|
+
}
|
|
1030
|
+
while (j >= 0 && (e[j] & 1 << i) === 0) {
|
|
1031
|
+
z.sqrTo(r, r2);
|
|
1032
|
+
t = r;
|
|
1033
|
+
r = r2;
|
|
1034
|
+
r2 = t;
|
|
1035
|
+
if (--i < 0) {
|
|
1036
|
+
i = this.DB - 1;
|
|
1037
|
+
--j;
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
return z.revert(r);
|
|
1042
|
+
}
|
|
1043
|
+
function bnGCD(a) {
|
|
1044
|
+
var x = this.s < 0 ? this.negate() : this.clone();
|
|
1045
|
+
var y = a.s < 0 ? a.negate() : a.clone();
|
|
1046
|
+
if (x.compareTo(y) < 0) {
|
|
1047
|
+
var t = x;
|
|
1048
|
+
x = y;
|
|
1049
|
+
y = t;
|
|
1050
|
+
}
|
|
1051
|
+
var i = x.getLowestSetBit(), g = y.getLowestSetBit();
|
|
1052
|
+
if (g < 0) return x;
|
|
1053
|
+
if (i < g) g = i;
|
|
1054
|
+
if (g > 0) {
|
|
1055
|
+
x.rShiftTo(g, x);
|
|
1056
|
+
y.rShiftTo(g, y);
|
|
1057
|
+
}
|
|
1058
|
+
while (x.signum() > 0) {
|
|
1059
|
+
if ((i = x.getLowestSetBit()) > 0) x.rShiftTo(i, x);
|
|
1060
|
+
if ((i = y.getLowestSetBit()) > 0) y.rShiftTo(i, y);
|
|
1061
|
+
if (x.compareTo(y) >= 0) {
|
|
1062
|
+
x.subTo(y, x);
|
|
1063
|
+
x.rShiftTo(1, x);
|
|
1064
|
+
} else {
|
|
1065
|
+
y.subTo(x, y);
|
|
1066
|
+
y.rShiftTo(1, y);
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
if (g > 0) y.lShiftTo(g, y);
|
|
1070
|
+
return y;
|
|
1071
|
+
}
|
|
1072
|
+
function bnpModInt(n) {
|
|
1073
|
+
if (n <= 0) return 0;
|
|
1074
|
+
var d = this.DV % n, r = this.s < 0 ? n - 1 : 0;
|
|
1075
|
+
if (this.t > 0) if (d === 0) r = this[0] % n;
|
|
1076
|
+
else for (var i = this.t - 1; i >= 0; --i) r = (d * r + this[i]) % n;
|
|
1077
|
+
return r;
|
|
1078
|
+
}
|
|
1079
|
+
function bnModInverse(m) {
|
|
1080
|
+
var ac = m.isEven();
|
|
1081
|
+
if (this.isEven() && ac || m.signum() === 0) return BigInteger.ZERO;
|
|
1082
|
+
var u = m.clone(), v = this.clone();
|
|
1083
|
+
var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
|
|
1084
|
+
while (u.signum() != 0) {
|
|
1085
|
+
while (u.isEven()) {
|
|
1086
|
+
u.rShiftTo(1, u);
|
|
1087
|
+
if (ac) {
|
|
1088
|
+
if (!a.isEven() || !b.isEven()) {
|
|
1089
|
+
a.addTo(this, a);
|
|
1090
|
+
b.subTo(m, b);
|
|
1091
|
+
}
|
|
1092
|
+
a.rShiftTo(1, a);
|
|
1093
|
+
} else if (!b.isEven()) b.subTo(m, b);
|
|
1094
|
+
b.rShiftTo(1, b);
|
|
1095
|
+
}
|
|
1096
|
+
while (v.isEven()) {
|
|
1097
|
+
v.rShiftTo(1, v);
|
|
1098
|
+
if (ac) {
|
|
1099
|
+
if (!c.isEven() || !d.isEven()) {
|
|
1100
|
+
c.addTo(this, c);
|
|
1101
|
+
d.subTo(m, d);
|
|
1102
|
+
}
|
|
1103
|
+
c.rShiftTo(1, c);
|
|
1104
|
+
} else if (!d.isEven()) d.subTo(m, d);
|
|
1105
|
+
d.rShiftTo(1, d);
|
|
1106
|
+
}
|
|
1107
|
+
if (u.compareTo(v) >= 0) {
|
|
1108
|
+
u.subTo(v, u);
|
|
1109
|
+
if (ac) a.subTo(c, a);
|
|
1110
|
+
b.subTo(d, b);
|
|
1111
|
+
} else {
|
|
1112
|
+
v.subTo(u, v);
|
|
1113
|
+
if (ac) c.subTo(a, c);
|
|
1114
|
+
d.subTo(b, d);
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
if (v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
|
|
1118
|
+
if (d.compareTo(m) >= 0) return d.subtract(m);
|
|
1119
|
+
if (d.signum() < 0) d.addTo(m, d);
|
|
1120
|
+
else return d;
|
|
1121
|
+
if (d.signum() < 0) return d.add(m);
|
|
1122
|
+
else return d;
|
|
1123
|
+
}
|
|
1124
|
+
var lowprimes = [
|
|
1125
|
+
2,
|
|
1126
|
+
3,
|
|
1127
|
+
5,
|
|
1128
|
+
7,
|
|
1129
|
+
11,
|
|
1130
|
+
13,
|
|
1131
|
+
17,
|
|
1132
|
+
19,
|
|
1133
|
+
23,
|
|
1134
|
+
29,
|
|
1135
|
+
31,
|
|
1136
|
+
37,
|
|
1137
|
+
41,
|
|
1138
|
+
43,
|
|
1139
|
+
47,
|
|
1140
|
+
53,
|
|
1141
|
+
59,
|
|
1142
|
+
61,
|
|
1143
|
+
67,
|
|
1144
|
+
71,
|
|
1145
|
+
73,
|
|
1146
|
+
79,
|
|
1147
|
+
83,
|
|
1148
|
+
89,
|
|
1149
|
+
97,
|
|
1150
|
+
101,
|
|
1151
|
+
103,
|
|
1152
|
+
107,
|
|
1153
|
+
109,
|
|
1154
|
+
113,
|
|
1155
|
+
127,
|
|
1156
|
+
131,
|
|
1157
|
+
137,
|
|
1158
|
+
139,
|
|
1159
|
+
149,
|
|
1160
|
+
151,
|
|
1161
|
+
157,
|
|
1162
|
+
163,
|
|
1163
|
+
167,
|
|
1164
|
+
173,
|
|
1165
|
+
179,
|
|
1166
|
+
181,
|
|
1167
|
+
191,
|
|
1168
|
+
193,
|
|
1169
|
+
197,
|
|
1170
|
+
199,
|
|
1171
|
+
211,
|
|
1172
|
+
223,
|
|
1173
|
+
227,
|
|
1174
|
+
229,
|
|
1175
|
+
233,
|
|
1176
|
+
239,
|
|
1177
|
+
241,
|
|
1178
|
+
251,
|
|
1179
|
+
257,
|
|
1180
|
+
263,
|
|
1181
|
+
269,
|
|
1182
|
+
271,
|
|
1183
|
+
277,
|
|
1184
|
+
281,
|
|
1185
|
+
283,
|
|
1186
|
+
293,
|
|
1187
|
+
307,
|
|
1188
|
+
311,
|
|
1189
|
+
313,
|
|
1190
|
+
317,
|
|
1191
|
+
331,
|
|
1192
|
+
337,
|
|
1193
|
+
347,
|
|
1194
|
+
349,
|
|
1195
|
+
353,
|
|
1196
|
+
359,
|
|
1197
|
+
367,
|
|
1198
|
+
373,
|
|
1199
|
+
379,
|
|
1200
|
+
383,
|
|
1201
|
+
389,
|
|
1202
|
+
397,
|
|
1203
|
+
401,
|
|
1204
|
+
409,
|
|
1205
|
+
419,
|
|
1206
|
+
421,
|
|
1207
|
+
431,
|
|
1208
|
+
433,
|
|
1209
|
+
439,
|
|
1210
|
+
443,
|
|
1211
|
+
449,
|
|
1212
|
+
457,
|
|
1213
|
+
461,
|
|
1214
|
+
463,
|
|
1215
|
+
467,
|
|
1216
|
+
479,
|
|
1217
|
+
487,
|
|
1218
|
+
491,
|
|
1219
|
+
499,
|
|
1220
|
+
503,
|
|
1221
|
+
509,
|
|
1222
|
+
521,
|
|
1223
|
+
523,
|
|
1224
|
+
541,
|
|
1225
|
+
547,
|
|
1226
|
+
557,
|
|
1227
|
+
563,
|
|
1228
|
+
569,
|
|
1229
|
+
571,
|
|
1230
|
+
577,
|
|
1231
|
+
587,
|
|
1232
|
+
593,
|
|
1233
|
+
599,
|
|
1234
|
+
601,
|
|
1235
|
+
607,
|
|
1236
|
+
613,
|
|
1237
|
+
617,
|
|
1238
|
+
619,
|
|
1239
|
+
631,
|
|
1240
|
+
641,
|
|
1241
|
+
643,
|
|
1242
|
+
647,
|
|
1243
|
+
653,
|
|
1244
|
+
659,
|
|
1245
|
+
661,
|
|
1246
|
+
673,
|
|
1247
|
+
677,
|
|
1248
|
+
683,
|
|
1249
|
+
691,
|
|
1250
|
+
701,
|
|
1251
|
+
709,
|
|
1252
|
+
719,
|
|
1253
|
+
727,
|
|
1254
|
+
733,
|
|
1255
|
+
739,
|
|
1256
|
+
743,
|
|
1257
|
+
751,
|
|
1258
|
+
757,
|
|
1259
|
+
761,
|
|
1260
|
+
769,
|
|
1261
|
+
773,
|
|
1262
|
+
787,
|
|
1263
|
+
797,
|
|
1264
|
+
809,
|
|
1265
|
+
811,
|
|
1266
|
+
821,
|
|
1267
|
+
823,
|
|
1268
|
+
827,
|
|
1269
|
+
829,
|
|
1270
|
+
839,
|
|
1271
|
+
853,
|
|
1272
|
+
857,
|
|
1273
|
+
859,
|
|
1274
|
+
863,
|
|
1275
|
+
877,
|
|
1276
|
+
881,
|
|
1277
|
+
883,
|
|
1278
|
+
887,
|
|
1279
|
+
907,
|
|
1280
|
+
911,
|
|
1281
|
+
919,
|
|
1282
|
+
929,
|
|
1283
|
+
937,
|
|
1284
|
+
941,
|
|
1285
|
+
947,
|
|
1286
|
+
953,
|
|
1287
|
+
967,
|
|
1288
|
+
971,
|
|
1289
|
+
977,
|
|
1290
|
+
983,
|
|
1291
|
+
991,
|
|
1292
|
+
997
|
|
1293
|
+
];
|
|
1294
|
+
var lplim = (1 << 26) / lowprimes[lowprimes.length - 1];
|
|
1295
|
+
function bnIsProbablePrime(t) {
|
|
1296
|
+
var i, x = this.abs();
|
|
1297
|
+
if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) {
|
|
1298
|
+
for (i = 0; i < lowprimes.length; ++i) if (x[0] == lowprimes[i]) return true;
|
|
1299
|
+
return false;
|
|
1300
|
+
}
|
|
1301
|
+
if (x.isEven()) return false;
|
|
1302
|
+
i = 1;
|
|
1303
|
+
while (i < lowprimes.length) {
|
|
1304
|
+
var m = lowprimes[i], j = i + 1;
|
|
1305
|
+
while (j < lowprimes.length && m < lplim) m *= lowprimes[j++];
|
|
1306
|
+
m = x.modInt(m);
|
|
1307
|
+
while (i < j) if (m % lowprimes[i++] === 0) return false;
|
|
1308
|
+
}
|
|
1309
|
+
return x.millerRabin(t);
|
|
1310
|
+
}
|
|
1311
|
+
function bnpMillerRabin(t) {
|
|
1312
|
+
var n1 = this.subtract(BigInteger.ONE);
|
|
1313
|
+
var k = n1.getLowestSetBit();
|
|
1314
|
+
if (k <= 0) return false;
|
|
1315
|
+
var r = n1.shiftRight(k);
|
|
1316
|
+
t = t + 1 >> 1;
|
|
1317
|
+
if (t > lowprimes.length) t = lowprimes.length;
|
|
1318
|
+
var a = nbi();
|
|
1319
|
+
for (var i = 0; i < t; ++i) {
|
|
1320
|
+
a.fromInt(lowprimes[Math.floor(Math.random() * lowprimes.length)]);
|
|
1321
|
+
var y = a.modPow(r, this);
|
|
1322
|
+
if (y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
|
|
1323
|
+
var j = 1;
|
|
1324
|
+
while (j++ < k && y.compareTo(n1) != 0) {
|
|
1325
|
+
y = y.modPowInt(2, this);
|
|
1326
|
+
if (y.compareTo(BigInteger.ONE) === 0) return false;
|
|
1327
|
+
}
|
|
1328
|
+
if (y.compareTo(n1) != 0) return false;
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
return true;
|
|
1332
|
+
}
|
|
1333
|
+
BigInteger.prototype.copyTo = bnpCopyTo;
|
|
1334
|
+
BigInteger.prototype.fromInt = bnpFromInt;
|
|
1335
|
+
BigInteger.prototype.fromString = bnpFromString;
|
|
1336
|
+
BigInteger.prototype.fromByteArray = bnpFromByteArray;
|
|
1337
|
+
BigInteger.prototype.fromBuffer = bnpFromBuffer;
|
|
1338
|
+
BigInteger.prototype.clamp = bnpClamp;
|
|
1339
|
+
BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
|
|
1340
|
+
BigInteger.prototype.drShiftTo = bnpDRShiftTo;
|
|
1341
|
+
BigInteger.prototype.lShiftTo = bnpLShiftTo;
|
|
1342
|
+
BigInteger.prototype.rShiftTo = bnpRShiftTo;
|
|
1343
|
+
BigInteger.prototype.subTo = bnpSubTo;
|
|
1344
|
+
BigInteger.prototype.multiplyTo = bnpMultiplyTo;
|
|
1345
|
+
BigInteger.prototype.squareTo = bnpSquareTo;
|
|
1346
|
+
BigInteger.prototype.divRemTo = bnpDivRemTo;
|
|
1347
|
+
BigInteger.prototype.invDigit = bnpInvDigit;
|
|
1348
|
+
BigInteger.prototype.isEven = bnpIsEven;
|
|
1349
|
+
BigInteger.prototype.exp = bnpExp;
|
|
1350
|
+
BigInteger.prototype.chunkSize = bnpChunkSize;
|
|
1351
|
+
BigInteger.prototype.toRadix = bnpToRadix;
|
|
1352
|
+
BigInteger.prototype.fromRadix = bnpFromRadix;
|
|
1353
|
+
BigInteger.prototype.fromNumber = bnpFromNumber;
|
|
1354
|
+
BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
|
|
1355
|
+
BigInteger.prototype.changeBit = bnpChangeBit;
|
|
1356
|
+
BigInteger.prototype.addTo = bnpAddTo;
|
|
1357
|
+
BigInteger.prototype.dMultiply = bnpDMultiply;
|
|
1358
|
+
BigInteger.prototype.dAddOffset = bnpDAddOffset;
|
|
1359
|
+
BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
|
|
1360
|
+
BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
|
|
1361
|
+
BigInteger.prototype.modInt = bnpModInt;
|
|
1362
|
+
BigInteger.prototype.millerRabin = bnpMillerRabin;
|
|
1363
|
+
BigInteger.prototype.toString = bnToString;
|
|
1364
|
+
BigInteger.prototype.negate = bnNegate;
|
|
1365
|
+
BigInteger.prototype.abs = bnAbs;
|
|
1366
|
+
BigInteger.prototype.compareTo = bnCompareTo;
|
|
1367
|
+
BigInteger.prototype.bitLength = bnBitLength;
|
|
1368
|
+
BigInteger.prototype.mod = bnMod;
|
|
1369
|
+
BigInteger.prototype.modPowInt = bnModPowInt;
|
|
1370
|
+
BigInteger.prototype.clone = bnClone;
|
|
1371
|
+
BigInteger.prototype.intValue = bnIntValue;
|
|
1372
|
+
BigInteger.prototype.byteValue = bnByteValue;
|
|
1373
|
+
BigInteger.prototype.shortValue = bnShortValue;
|
|
1374
|
+
BigInteger.prototype.signum = bnSigNum;
|
|
1375
|
+
BigInteger.prototype.toByteArray = bnToByteArray;
|
|
1376
|
+
BigInteger.prototype.toBuffer = bnToBuffer;
|
|
1377
|
+
BigInteger.prototype.equals = bnEquals;
|
|
1378
|
+
BigInteger.prototype.eq = bnEqual;
|
|
1379
|
+
BigInteger.prototype.gt = bnGreater;
|
|
1380
|
+
BigInteger.prototype.gte = bnGreaterOrEqual;
|
|
1381
|
+
BigInteger.prototype.lt = bnLesser;
|
|
1382
|
+
BigInteger.prototype.lte = bnLesserOrEqual;
|
|
1383
|
+
BigInteger.prototype.min = bnMin;
|
|
1384
|
+
BigInteger.prototype.max = bnMax;
|
|
1385
|
+
BigInteger.prototype.and = bnAnd;
|
|
1386
|
+
BigInteger.prototype.or = bnOr;
|
|
1387
|
+
BigInteger.prototype.xor = bnXor;
|
|
1388
|
+
BigInteger.prototype.andNot = bnAndNot;
|
|
1389
|
+
BigInteger.prototype.not = bnNot;
|
|
1390
|
+
BigInteger.prototype.shiftLeft = bnShiftLeft;
|
|
1391
|
+
BigInteger.prototype.shiftRight = bnShiftRight;
|
|
1392
|
+
BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
|
|
1393
|
+
BigInteger.prototype.bitCount = bnBitCount;
|
|
1394
|
+
BigInteger.prototype.testBit = bnTestBit;
|
|
1395
|
+
BigInteger.prototype.setBit = bnSetBit;
|
|
1396
|
+
BigInteger.prototype.clearBit = bnClearBit;
|
|
1397
|
+
BigInteger.prototype.flipBit = bnFlipBit;
|
|
1398
|
+
BigInteger.prototype.add = bnAdd;
|
|
1399
|
+
BigInteger.prototype.subtract = bnSubtract;
|
|
1400
|
+
BigInteger.prototype.multiply = bnMultiply;
|
|
1401
|
+
BigInteger.prototype.divide = bnDivide;
|
|
1402
|
+
BigInteger.prototype.remainder = bnRemainder;
|
|
1403
|
+
BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
|
|
1404
|
+
BigInteger.prototype.modPow = bnModPow;
|
|
1405
|
+
BigInteger.prototype.modInverse = bnModInverse;
|
|
1406
|
+
BigInteger.prototype.pow = bnPow;
|
|
1407
|
+
BigInteger.prototype.gcd = bnGCD;
|
|
1408
|
+
BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
|
|
1409
|
+
BigInteger.int2char = int2char;
|
|
1410
|
+
BigInteger.ZERO = nbv(0);
|
|
1411
|
+
BigInteger.ONE = nbv(1);
|
|
1412
|
+
BigInteger.prototype.square = bnSquare;
|
|
1413
|
+
module.exports = BigInteger;
|
|
1414
|
+
}));
|
|
1415
|
+
|
|
1416
|
+
//#endregion
|
|
1417
|
+
//#region ../../node_modules/.bun/fast-srp-hap@2.0.4/node_modules/fast-srp-hap/lib/params.js
|
|
1418
|
+
var require_params = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
1419
|
+
/**
|
|
1420
|
+
* SRP Group Parameters
|
|
1421
|
+
* http://tools.ietf.org/html/rfc5054#appendix-A
|
|
1422
|
+
*
|
|
1423
|
+
* The 1024-, 1536-, and 2048-bit groups are taken from software
|
|
1424
|
+
* developed by Tom Wu and Eugene Jhong for the Stanford SRP
|
|
1425
|
+
* distribution, and subsequently proven to be prime. The larger primes
|
|
1426
|
+
* are taken from [MODP], but generators have been calculated that are
|
|
1427
|
+
* primitive roots of N, unlike the generators in [MODP].
|
|
1428
|
+
*
|
|
1429
|
+
* The 1024-bit and 1536-bit groups MUST be supported.
|
|
1430
|
+
*/
|
|
1431
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1432
|
+
exports.params = exports.hex = void 0;
|
|
1433
|
+
const BigInteger = require_jsbn();
|
|
1434
|
+
function hex(s) {
|
|
1435
|
+
return new BigInteger(s.split(/\s|\n/).join(""), 16);
|
|
1436
|
+
}
|
|
1437
|
+
exports.hex = hex;
|
|
1438
|
+
exports.params = {
|
|
1439
|
+
1024: {
|
|
1440
|
+
N_length_bits: 1024,
|
|
1441
|
+
N: hex(`
|
|
1442
|
+
EEAF0AB9 ADB38DD6 9C33F80A FA8FC5E8 60726187 75FF3C0B 9EA2314C
|
|
1443
|
+
9C256576 D674DF74 96EA81D3 383B4813 D692C6E0 E0D5D8E2 50B98BE4
|
|
1444
|
+
8E495C1D 6089DAD1 5DC7D7B4 6154D6B6 CE8EF4AD 69B15D49 82559B29
|
|
1445
|
+
7BCF1885 C529F566 660E57EC 68EDBC3C 05726CC0 2FD4CBF4 976EAA9A
|
|
1446
|
+
FD5138FE 8376435B 9FC61D2F C0EB06E3
|
|
1447
|
+
`),
|
|
1448
|
+
g: hex("02"),
|
|
1449
|
+
hash: "sha1"
|
|
1450
|
+
},
|
|
1451
|
+
1536: {
|
|
1452
|
+
N_length_bits: 1536,
|
|
1453
|
+
N: hex(`
|
|
1454
|
+
9DEF3CAF B939277A B1F12A86 17A47BBB DBA51DF4 99AC4C80 BEEEA961
|
|
1455
|
+
4B19CC4D 5F4F5F55 6E27CBDE 51C6A94B E4607A29 1558903B A0D0F843
|
|
1456
|
+
80B655BB 9A22E8DC DF028A7C EC67F0D0 8134B1C8 B9798914 9B609E0B
|
|
1457
|
+
E3BAB63D 47548381 DBC5B1FC 764E3F4B 53DD9DA1 158BFD3E 2B9C8CF5
|
|
1458
|
+
6EDF0195 39349627 DB2FD53D 24B7C486 65772E43 7D6C7F8C E442734A
|
|
1459
|
+
F7CCB7AE 837C264A E3A9BEB8 7F8A2FE9 B8B5292E 5A021FFF 5E91479E
|
|
1460
|
+
8CE7A28C 2442C6F3 15180F93 499A234D CF76E3FE D135F9BB
|
|
1461
|
+
`),
|
|
1462
|
+
g: hex("02"),
|
|
1463
|
+
hash: "sha1"
|
|
1464
|
+
},
|
|
1465
|
+
2048: {
|
|
1466
|
+
N_length_bits: 2048,
|
|
1467
|
+
N: hex(`
|
|
1468
|
+
AC6BDB41 324A9A9B F166DE5E 1389582F AF72B665 1987EE07 FC319294
|
|
1469
|
+
3DB56050 A37329CB B4A099ED 8193E075 7767A13D D52312AB 4B03310D
|
|
1470
|
+
CD7F48A9 DA04FD50 E8083969 EDB767B0 CF609517 9A163AB3 661A05FB
|
|
1471
|
+
D5FAAAE8 2918A996 2F0B93B8 55F97993 EC975EEA A80D740A DBF4FF74
|
|
1472
|
+
7359D041 D5C33EA7 1D281E44 6B14773B CA97B43A 23FB8016 76BD207A
|
|
1473
|
+
436C6481 F1D2B907 8717461A 5B9D32E6 88F87748 544523B5 24B0D57D
|
|
1474
|
+
5EA77A27 75D2ECFA 032CFBDB F52FB378 61602790 04E57AE6 AF874E73
|
|
1475
|
+
03CE5329 9CCC041C 7BC308D8 2A5698F3 A8D0C382 71AE35F8 E9DBFBB6
|
|
1476
|
+
94B5C803 D89F7AE4 35DE236D 525F5475 9B65E372 FCD68EF2 0FA7111F
|
|
1477
|
+
9E4AFF73
|
|
1478
|
+
`),
|
|
1479
|
+
g: hex("02"),
|
|
1480
|
+
hash: "sha256"
|
|
1481
|
+
},
|
|
1482
|
+
3072: {
|
|
1483
|
+
N_length_bits: 3072,
|
|
1484
|
+
N: hex(`
|
|
1485
|
+
FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
|
|
1486
|
+
8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
|
|
1487
|
+
302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
|
|
1488
|
+
A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
|
|
1489
|
+
49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
|
|
1490
|
+
FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
|
|
1491
|
+
670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
|
|
1492
|
+
180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
|
|
1493
|
+
3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
|
|
1494
|
+
04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
|
|
1495
|
+
B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
|
|
1496
|
+
1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
|
|
1497
|
+
BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
|
|
1498
|
+
E0FD108E 4B82D120 A93AD2CA FFFFFFFF FFFFFFFF
|
|
1499
|
+
`),
|
|
1500
|
+
g: hex("05"),
|
|
1501
|
+
hash: "sha256"
|
|
1502
|
+
},
|
|
1503
|
+
hap: {
|
|
1504
|
+
N_length_bits: 3072,
|
|
1505
|
+
N: hex(`
|
|
1506
|
+
FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
|
|
1507
|
+
8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
|
|
1508
|
+
302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
|
|
1509
|
+
A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
|
|
1510
|
+
49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
|
|
1511
|
+
FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
|
|
1512
|
+
670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
|
|
1513
|
+
180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
|
|
1514
|
+
3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
|
|
1515
|
+
04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
|
|
1516
|
+
B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
|
|
1517
|
+
1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
|
|
1518
|
+
BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
|
|
1519
|
+
E0FD108E 4B82D120 A93AD2CA FFFFFFFF FFFFFFFF
|
|
1520
|
+
`),
|
|
1521
|
+
g: hex("05"),
|
|
1522
|
+
hash: "sha512"
|
|
1523
|
+
},
|
|
1524
|
+
4096: {
|
|
1525
|
+
N_length_bits: 4096,
|
|
1526
|
+
N: hex(`
|
|
1527
|
+
FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
|
|
1528
|
+
8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
|
|
1529
|
+
302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
|
|
1530
|
+
A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
|
|
1531
|
+
49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
|
|
1532
|
+
FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
|
|
1533
|
+
670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
|
|
1534
|
+
180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
|
|
1535
|
+
3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
|
|
1536
|
+
04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
|
|
1537
|
+
B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
|
|
1538
|
+
1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
|
|
1539
|
+
BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
|
|
1540
|
+
E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7 88719A10 BDBA5B26
|
|
1541
|
+
99C32718 6AF4E23C 1A946834 B6150BDA 2583E9CA 2AD44CE8 DBBBC2DB
|
|
1542
|
+
04DE8EF9 2E8EFC14 1FBECAA6 287C5947 4E6BC05D 99B2964F A090C3A2
|
|
1543
|
+
233BA186 515BE7ED 1F612970 CEE2D7AF B81BDD76 2170481C D0069127
|
|
1544
|
+
D5B05AA9 93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34063199
|
|
1545
|
+
FFFFFFFF FFFFFFFF
|
|
1546
|
+
`),
|
|
1547
|
+
g: hex("05"),
|
|
1548
|
+
hash: "sha256"
|
|
1549
|
+
},
|
|
1550
|
+
6244: {
|
|
1551
|
+
N_length_bits: 6244,
|
|
1552
|
+
N: hex(`
|
|
1553
|
+
FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
|
|
1554
|
+
8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
|
|
1555
|
+
302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
|
|
1556
|
+
A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
|
|
1557
|
+
49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
|
|
1558
|
+
FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
|
|
1559
|
+
670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
|
|
1560
|
+
180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
|
|
1561
|
+
3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
|
|
1562
|
+
04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
|
|
1563
|
+
B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
|
|
1564
|
+
1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
|
|
1565
|
+
BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
|
|
1566
|
+
E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7 88719A10 BDBA5B26
|
|
1567
|
+
99C32718 6AF4E23C 1A946834 B6150BDA 2583E9CA 2AD44CE8 DBBBC2DB
|
|
1568
|
+
04DE8EF9 2E8EFC14 1FBECAA6 287C5947 4E6BC05D 99B2964F A090C3A2
|
|
1569
|
+
233BA186 515BE7ED 1F612970 CEE2D7AF B81BDD76 2170481C D0069127
|
|
1570
|
+
D5B05AA9 93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34028492
|
|
1571
|
+
36C3FAB4 D27C7026 C1D4DCB2 602646DE C9751E76 3DBA37BD F8FF9406
|
|
1572
|
+
AD9E530E E5DB382F 413001AE B06A53ED 9027D831 179727B0 865A8918
|
|
1573
|
+
DA3EDBEB CF9B14ED 44CE6CBA CED4BB1B DB7F1447 E6CC254B 33205151
|
|
1574
|
+
2BD7AF42 6FB8F401 378CD2BF 5983CA01 C64B92EC F032EA15 D1721D03
|
|
1575
|
+
F482D7CE 6E74FEF6 D55E702F 46980C82 B5A84031 900B1C9E 59E7C97F
|
|
1576
|
+
BEC7E8F3 23A97A7E 36CC88BE 0F1D45B7 FF585AC5 4BD407B2 2B4154AA
|
|
1577
|
+
CC8F6D7E BF48E1D8 14CC5ED2 0F8037E0 A79715EE F29BE328 06A1D58B
|
|
1578
|
+
B7C5DA76 F550AA3D 8A1FBFF0 EB19CCB1 A313D55C DA56C9EC 2EF29632
|
|
1579
|
+
387FE8D7 6E3C0468 043E8F66 3F4860EE 12BF2D5B 0B7474D6 E694F91E
|
|
1580
|
+
6DCC4024 FFFFFFFF FFFFFFFF
|
|
1581
|
+
`),
|
|
1582
|
+
g: hex("05"),
|
|
1583
|
+
hash: "sha256"
|
|
1584
|
+
},
|
|
1585
|
+
8192: {
|
|
1586
|
+
N_length_bits: 8192,
|
|
1587
|
+
N: hex(`
|
|
1588
|
+
FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
|
|
1589
|
+
8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
|
|
1590
|
+
302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
|
|
1591
|
+
A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
|
|
1592
|
+
49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
|
|
1593
|
+
FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
|
|
1594
|
+
670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
|
|
1595
|
+
180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
|
|
1596
|
+
3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
|
|
1597
|
+
04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
|
|
1598
|
+
B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
|
|
1599
|
+
1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
|
|
1600
|
+
BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
|
|
1601
|
+
E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7 88719A10 BDBA5B26
|
|
1602
|
+
99C32718 6AF4E23C 1A946834 B6150BDA 2583E9CA 2AD44CE8 DBBBC2DB
|
|
1603
|
+
04DE8EF9 2E8EFC14 1FBECAA6 287C5947 4E6BC05D 99B2964F A090C3A2
|
|
1604
|
+
233BA186 515BE7ED 1F612970 CEE2D7AF B81BDD76 2170481C D0069127
|
|
1605
|
+
D5B05AA9 93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34028492
|
|
1606
|
+
36C3FAB4 D27C7026 C1D4DCB2 602646DE C9751E76 3DBA37BD F8FF9406
|
|
1607
|
+
AD9E530E E5DB382F 413001AE B06A53ED 9027D831 179727B0 865A8918
|
|
1608
|
+
DA3EDBEB CF9B14ED 44CE6CBA CED4BB1B DB7F1447 E6CC254B 33205151
|
|
1609
|
+
2BD7AF42 6FB8F401 378CD2BF 5983CA01 C64B92EC F032EA15 D1721D03
|
|
1610
|
+
F482D7CE 6E74FEF6 D55E702F 46980C82 B5A84031 900B1C9E 59E7C97F
|
|
1611
|
+
BEC7E8F3 23A97A7E 36CC88BE 0F1D45B7 FF585AC5 4BD407B2 2B4154AA
|
|
1612
|
+
CC8F6D7E BF48E1D8 14CC5ED2 0F8037E0 A79715EE F29BE328 06A1D58B
|
|
1613
|
+
B7C5DA76 F550AA3D 8A1FBFF0 EB19CCB1 A313D55C DA56C9EC 2EF29632
|
|
1614
|
+
387FE8D7 6E3C0468 043E8F66 3F4860EE 12BF2D5B 0B7474D6 E694F91E
|
|
1615
|
+
6DBE1159 74A3926F 12FEE5E4 38777CB6 A932DF8C D8BEC4D0 73B931BA
|
|
1616
|
+
3BC832B6 8D9DD300 741FA7BF 8AFC47ED 2576F693 6BA42466 3AAB639C
|
|
1617
|
+
5AE4F568 3423B474 2BF1C978 238F16CB E39D652D E3FDB8BE FC848AD9
|
|
1618
|
+
22222E04 A4037C07 13EB57A8 1A23F0C7 3473FC64 6CEA306B 4BCBC886
|
|
1619
|
+
2F8385DD FA9D4B7F A2C087E8 79683303 ED5BDD3A 062B3CF5 B3A278A6
|
|
1620
|
+
6D2A13F8 3F44F82D DF310EE0 74AB6A36 4597E899 A0255DC1 64F31CC5
|
|
1621
|
+
0846851D F9AB4819 5DED7EA1 B1D510BD 7EE74D73 FAF36BC3 1ECFA268
|
|
1622
|
+
359046F4 EB879F92 4009438B 481C6CD7 889A002E D5EE382B C9190DA6
|
|
1623
|
+
FC026E47 9558E447 5677E9AA 9E3050E2 765694DF C81F56E8 80B96E71
|
|
1624
|
+
60C980DD 98EDD3DF FFFFFFFF FFFFFFFF
|
|
1625
|
+
`),
|
|
1626
|
+
g: hex("13"),
|
|
1627
|
+
hash: "sha256"
|
|
1628
|
+
}
|
|
1629
|
+
};
|
|
1630
|
+
exports.default = exports.params;
|
|
1631
|
+
}));
|
|
1632
|
+
|
|
1633
|
+
//#endregion
|
|
1634
|
+
//#region ../../node_modules/.bun/fast-srp-hap@2.0.4/node_modules/fast-srp-hap/lib/srp.js
|
|
1635
|
+
var require_srp = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
1636
|
+
var __importDefault = exports && exports.__importDefault || function(mod) {
|
|
1637
|
+
return mod && mod.__esModule ? mod : { "default": mod };
|
|
1638
|
+
};
|
|
1639
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1640
|
+
exports.SrpServer = exports.SrpClient = exports.SRP = void 0;
|
|
1641
|
+
const crypto_1 = __importDefault(__require("crypto"));
|
|
1642
|
+
const assert_1 = __importDefault(__require("assert"));
|
|
1643
|
+
const BigInteger = require_jsbn();
|
|
1644
|
+
const params_1 = require_params();
|
|
1645
|
+
const zero = new BigInteger(0, 10);
|
|
1646
|
+
function assert_(val, msg) {
|
|
1647
|
+
if (!val) throw new Error(msg || "assertion");
|
|
1648
|
+
}
|
|
1649
|
+
function assertIsBuffer(arg, argname = "arg") {
|
|
1650
|
+
assert_(Buffer.isBuffer(arg), `Type error: ${argname} must be a buffer`);
|
|
1651
|
+
}
|
|
1652
|
+
function assertIsBigInteger(arg, argname = "arg") {
|
|
1653
|
+
assert_(arg instanceof BigInteger, `Type error: ${argname} must be a BigInteger`);
|
|
1654
|
+
}
|
|
1655
|
+
/**
|
|
1656
|
+
* If a conversion is explicitly specified with the operator PAD(),
|
|
1657
|
+
* the integer will first be implicitly converted, then the resultant
|
|
1658
|
+
* byte-string will be left-padded with zeros (if necessary) until its
|
|
1659
|
+
* length equals the implicitly-converted length of N.
|
|
1660
|
+
*
|
|
1661
|
+
* @param {Buffer} n Number to pad
|
|
1662
|
+
* @param {number} len Length of the resulting Buffer
|
|
1663
|
+
* @return {Buffer}
|
|
1664
|
+
*/
|
|
1665
|
+
function padTo(n, len) {
|
|
1666
|
+
assertIsBuffer(n, "n");
|
|
1667
|
+
const padding = len - n.length;
|
|
1668
|
+
assert_(padding > -1, "Negative padding. Very uncomfortable.");
|
|
1669
|
+
const result = Buffer.alloc(len);
|
|
1670
|
+
result.fill(0, 0, padding);
|
|
1671
|
+
n.copy(result, padding);
|
|
1672
|
+
assert_1.default.strictEqual(result.length, len);
|
|
1673
|
+
return result;
|
|
1674
|
+
}
|
|
1675
|
+
function padToN(number, params) {
|
|
1676
|
+
assertIsBigInteger(number);
|
|
1677
|
+
const n = number.toString(16).length % 2 !== 0 ? "0" + number.toString(16) : number.toString(16);
|
|
1678
|
+
return padTo(Buffer.from(n, "hex"), params.N_length_bits / 8);
|
|
1679
|
+
}
|
|
1680
|
+
/**
|
|
1681
|
+
* Compute the intermediate value x as a hash of three buffers:
|
|
1682
|
+
* salt, identity, and password. And a colon. FOUR buffers.
|
|
1683
|
+
*
|
|
1684
|
+
* x = H(s | H(I | ":" | P))
|
|
1685
|
+
*
|
|
1686
|
+
* @param {object} params
|
|
1687
|
+
* @param {Buffer} salt
|
|
1688
|
+
* @param {Buffer} I User identity
|
|
1689
|
+
* @param {Buffer} P User password
|
|
1690
|
+
* @return {BigInteger} User secret
|
|
1691
|
+
*/
|
|
1692
|
+
function getx(params, salt, I, P) {
|
|
1693
|
+
assertIsBuffer(salt, "salt (salt)");
|
|
1694
|
+
assertIsBuffer(I, "identity (I)");
|
|
1695
|
+
assertIsBuffer(P, "password (P)");
|
|
1696
|
+
const hashIP = crypto_1.default.createHash(params.hash).update(Buffer.concat([
|
|
1697
|
+
I,
|
|
1698
|
+
Buffer.from(":"),
|
|
1699
|
+
P
|
|
1700
|
+
])).digest();
|
|
1701
|
+
return new BigInteger(crypto_1.default.createHash(params.hash).update(salt).update(hashIP).digest());
|
|
1702
|
+
}
|
|
1703
|
+
var SRP = class SRP {
|
|
1704
|
+
/**
|
|
1705
|
+
* The verifier is calculated as described in Section 3 of [SRP-RFC].
|
|
1706
|
+
* We give the algorithm here for convenience.
|
|
1707
|
+
*
|
|
1708
|
+
* The verifier (v) is computed based on the salt (s), user name (I),
|
|
1709
|
+
* password (P), and group parameters (N, g).
|
|
1710
|
+
*
|
|
1711
|
+
* x = H(s | H(I | ":" | P))
|
|
1712
|
+
* v = g^x % N
|
|
1713
|
+
*
|
|
1714
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
1715
|
+
* @param {Buffer} salt
|
|
1716
|
+
* @param {Buffer} I User identity
|
|
1717
|
+
* @param {Buffer} P User password
|
|
1718
|
+
* @return {Buffer}
|
|
1719
|
+
*/
|
|
1720
|
+
static computeVerifier(params, salt, I, P) {
|
|
1721
|
+
assertIsBuffer(salt, "salt (salt)");
|
|
1722
|
+
assertIsBuffer(I, "identity (I)");
|
|
1723
|
+
assertIsBuffer(P, "password (P)");
|
|
1724
|
+
return params.g.modPow(getx(params, salt, I, P), params.N).toBuffer(params.N_length_bits / 8);
|
|
1725
|
+
}
|
|
1726
|
+
static genKey(bytes = 32, callback) {
|
|
1727
|
+
if (typeof bytes !== "number") {
|
|
1728
|
+
callback = bytes;
|
|
1729
|
+
bytes = 32;
|
|
1730
|
+
}
|
|
1731
|
+
if (!callback) return new Promise((rs, rj) => SRP.genKey(bytes, (err, data) => err ? rj(err) : rs(data)));
|
|
1732
|
+
crypto_1.default.randomBytes(bytes, (err, buf) => {
|
|
1733
|
+
if (err) return callback(err, null);
|
|
1734
|
+
return callback(null, buf);
|
|
1735
|
+
});
|
|
1736
|
+
}
|
|
1737
|
+
};
|
|
1738
|
+
exports.SRP = SRP;
|
|
1739
|
+
SRP.params = params_1.params;
|
|
1740
|
+
/**
|
|
1741
|
+
* Calculate the SRP-6 multiplier.
|
|
1742
|
+
*
|
|
1743
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
1744
|
+
* @return {BigInteger}
|
|
1745
|
+
*/
|
|
1746
|
+
function getk(params) {
|
|
1747
|
+
return new BigInteger(crypto_1.default.createHash(params.hash).update(padToN(params.N, params)).update(padToN(params.g, params)).digest());
|
|
1748
|
+
}
|
|
1749
|
+
/**
|
|
1750
|
+
* The server key exchange message also contains the server's public
|
|
1751
|
+
* value (B). The server calculates this value as B = k*v + g^b % N,
|
|
1752
|
+
* where b is a random number that SHOULD be at least 256 bits in length
|
|
1753
|
+
* and k = H(N | PAD(g)).
|
|
1754
|
+
*
|
|
1755
|
+
* Note: as the tests imply, the entire expression is mod N.
|
|
1756
|
+
*
|
|
1757
|
+
* @param {SrpParams} params Group parameters, with .N, .g, .hash
|
|
1758
|
+
* @param {BigInteger} k
|
|
1759
|
+
* @param {BigInteger} v Verifier (stored)
|
|
1760
|
+
* @param {BigInteger} b Server secret exponent
|
|
1761
|
+
* @return {Buffer} B - The server public message
|
|
1762
|
+
*/
|
|
1763
|
+
function getB(params, k, v, b) {
|
|
1764
|
+
assertIsBigInteger(v);
|
|
1765
|
+
assertIsBigInteger(k);
|
|
1766
|
+
assertIsBigInteger(b);
|
|
1767
|
+
return k.multiply(v).add(params.g.modPow(b, params.N)).mod(params.N).toBuffer(params.N_length_bits / 8);
|
|
1768
|
+
}
|
|
1769
|
+
/**
|
|
1770
|
+
* The client key exchange message carries the client's public value
|
|
1771
|
+
* (A). The client calculates this value as A = g^a % N, where a is a
|
|
1772
|
+
* random number that SHOULD be at least 256 bits in length.
|
|
1773
|
+
*
|
|
1774
|
+
* Note: for this implementation, we take that to mean 256/8 bytes.
|
|
1775
|
+
*
|
|
1776
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
1777
|
+
* @param {BigInteger} a_num Client secret exponent
|
|
1778
|
+
* @return {Buffer} A - The client public message
|
|
1779
|
+
*/
|
|
1780
|
+
function getA(params, a_num) {
|
|
1781
|
+
assertIsBigInteger(a_num);
|
|
1782
|
+
if (Math.ceil(a_num.toString(16).length / 2) < 32) console.warn("getA: client key length %d is less than the recommended 256 bits", a_num.bitLength());
|
|
1783
|
+
return params.g.modPow(a_num, params.N).toBuffer(params.N_length_bits / 8);
|
|
1784
|
+
}
|
|
1785
|
+
/**
|
|
1786
|
+
* getu() hashes the two public messages together, to obtain a scrambling
|
|
1787
|
+
* parameter "u" which cannot be predicted by either party ahead of time.
|
|
1788
|
+
* This makes it safe to use the message ordering defined in the SRP-6a
|
|
1789
|
+
* paper, in which the server reveals their "B" value before the client
|
|
1790
|
+
* commits to their "A" value.
|
|
1791
|
+
*
|
|
1792
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
1793
|
+
* @param {Buffer} A Client ephemeral public key
|
|
1794
|
+
* @param {Buffer} B Server ephemeral public key
|
|
1795
|
+
* @return {BigInteger} u - Shared scrambling parameter
|
|
1796
|
+
*/
|
|
1797
|
+
function getu(params, A, B) {
|
|
1798
|
+
assertIsBuffer(A, "A");
|
|
1799
|
+
assertIsBuffer(B, "B");
|
|
1800
|
+
return new BigInteger(crypto_1.default.createHash(params.hash).update(padTo(A, params.N_length_bits / 8)).update(padTo(B, params.N_length_bits / 8)).digest());
|
|
1801
|
+
}
|
|
1802
|
+
/**
|
|
1803
|
+
* The TLS premaster secret as calculated by the client
|
|
1804
|
+
*
|
|
1805
|
+
* @param {SrpParams} params Group parameters, with .N, .g, .hash
|
|
1806
|
+
* @param {BigInteger} k_num
|
|
1807
|
+
* @param {BigInteger} x_num
|
|
1808
|
+
* @param {BigInteger} a_num
|
|
1809
|
+
* @param {BigInteger} B_num
|
|
1810
|
+
* @param {BigInteger} u_num
|
|
1811
|
+
* @return {Buffer}
|
|
1812
|
+
*/
|
|
1813
|
+
function client_getS(params, k_num, x_num, a_num, B_num, u_num) {
|
|
1814
|
+
assertIsBigInteger(k_num);
|
|
1815
|
+
assertIsBigInteger(x_num);
|
|
1816
|
+
assertIsBigInteger(a_num);
|
|
1817
|
+
assertIsBigInteger(B_num);
|
|
1818
|
+
assertIsBigInteger(u_num);
|
|
1819
|
+
if (zero.compareTo(B_num) >= 0 || params.N.compareTo(B_num) <= 0) throw new Error("invalid server-supplied \"B\", must be 1..N-1");
|
|
1820
|
+
return B_num.subtract(k_num.multiply(params.g.modPow(x_num, params.N))).modPow(a_num.add(u_num.multiply(x_num)), params.N).mod(params.N).toBuffer(params.N_length_bits / 8);
|
|
1821
|
+
}
|
|
1822
|
+
/**
|
|
1823
|
+
* The TLS premastersecret as calculated by the server
|
|
1824
|
+
*
|
|
1825
|
+
* @param {BigInteger} params Group parameters, with .N, .g, .hash
|
|
1826
|
+
* @param {BigInteger} v_num Verifier (stored on server)
|
|
1827
|
+
* @param {BigInteger} A_num Ephemeral client public key (read from client)
|
|
1828
|
+
* @param {BigInteger} b_num Server ephemeral private key (generated for session)
|
|
1829
|
+
* @param {BigInteger} u_num {@see getu}
|
|
1830
|
+
* @return {Buffer}
|
|
1831
|
+
*/
|
|
1832
|
+
function server_getS(params, v_num, A_num, b_num, u_num) {
|
|
1833
|
+
assertIsBigInteger(v_num);
|
|
1834
|
+
assertIsBigInteger(A_num);
|
|
1835
|
+
assertIsBigInteger(b_num);
|
|
1836
|
+
assertIsBigInteger(u_num);
|
|
1837
|
+
if (zero.compareTo(A_num) >= 0 || params.N.compareTo(A_num) <= 0) throw new Error("invalid client-supplied \"A\", must be 1..N-1");
|
|
1838
|
+
return A_num.multiply(v_num.modPow(u_num, params.N)).modPow(b_num, params.N).mod(params.N).toBuffer(params.N_length_bits / 8);
|
|
1839
|
+
}
|
|
1840
|
+
/**
|
|
1841
|
+
* Compute the shared session key K from S
|
|
1842
|
+
*
|
|
1843
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
1844
|
+
* @param {Buffer} S_buf Session key
|
|
1845
|
+
* @return {Buffer}
|
|
1846
|
+
*/
|
|
1847
|
+
function getK(params, S_buf) {
|
|
1848
|
+
assertIsBuffer(S_buf, "S");
|
|
1849
|
+
if (params.hash === "sha1") return Buffer.concat([crypto_1.default.createHash(params.hash).update(S_buf).update(Buffer.from([
|
|
1850
|
+
0,
|
|
1851
|
+
0,
|
|
1852
|
+
0,
|
|
1853
|
+
0
|
|
1854
|
+
])).digest(), crypto_1.default.createHash(params.hash).update(S_buf).update(Buffer.from([
|
|
1855
|
+
0,
|
|
1856
|
+
0,
|
|
1857
|
+
0,
|
|
1858
|
+
1
|
|
1859
|
+
])).digest()]);
|
|
1860
|
+
else return crypto_1.default.createHash(params.hash).update(S_buf).digest();
|
|
1861
|
+
}
|
|
1862
|
+
function getM1(params, u_buf, s_buf, A_buf, B_buf, K_buf) {
|
|
1863
|
+
if (arguments.length > 4) {
|
|
1864
|
+
assertIsBuffer(u_buf, "identity (I)");
|
|
1865
|
+
assertIsBuffer(s_buf, "salt (s)");
|
|
1866
|
+
assertIsBuffer(A_buf, "client public key (A)");
|
|
1867
|
+
assertIsBuffer(B_buf, "server public key (B)");
|
|
1868
|
+
assertIsBuffer(K_buf, "session key (K)");
|
|
1869
|
+
const hN = crypto_1.default.createHash(params.hash).update(params.N.toBuffer(true)).digest();
|
|
1870
|
+
const hG = crypto_1.default.createHash(params.hash).update(params.g.toBuffer(true)).digest();
|
|
1871
|
+
for (let i = 0; i < hN.length; i++) hN[i] ^= hG[i];
|
|
1872
|
+
const hU = crypto_1.default.createHash(params.hash).update(u_buf).digest();
|
|
1873
|
+
return crypto_1.default.createHash(params.hash).update(hN).update(hU).update(s_buf).update(A_buf).update(B_buf).update(K_buf).digest();
|
|
1874
|
+
} else {
|
|
1875
|
+
[A_buf, B_buf, s_buf] = [
|
|
1876
|
+
u_buf,
|
|
1877
|
+
s_buf,
|
|
1878
|
+
A_buf
|
|
1879
|
+
];
|
|
1880
|
+
assertIsBuffer(A_buf, "A");
|
|
1881
|
+
assertIsBuffer(B_buf, "B");
|
|
1882
|
+
assertIsBuffer(s_buf, "S");
|
|
1883
|
+
return crypto_1.default.createHash(params.hash).update(A_buf).update(B_buf).update(s_buf).digest();
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
function getM2(params, A_buf, M1_buf, K_buf) {
|
|
1887
|
+
assertIsBuffer(A_buf, "A");
|
|
1888
|
+
assertIsBuffer(M1_buf, "M1");
|
|
1889
|
+
assertIsBuffer(K_buf, "K");
|
|
1890
|
+
return crypto_1.default.createHash(params.hash).update(A_buf).update(M1_buf).update(K_buf).digest();
|
|
1891
|
+
}
|
|
1892
|
+
function equal(buf1, buf2) {
|
|
1893
|
+
return buf1.toString("hex") === buf2.toString("hex");
|
|
1894
|
+
}
|
|
1895
|
+
var SrpClient = class {
|
|
1896
|
+
/**
|
|
1897
|
+
* Create an SRP client.
|
|
1898
|
+
*
|
|
1899
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
1900
|
+
* @param {Buffer} salt_buf User salt (from server)
|
|
1901
|
+
* @param {Buffer} identity_buf Identity/username
|
|
1902
|
+
* @param {Buffer} password_buf Password
|
|
1903
|
+
* @param {Buffer} secret1_buf Client private key {@see genKey}
|
|
1904
|
+
* @param {boolean} hap
|
|
1905
|
+
*/
|
|
1906
|
+
constructor(params, salt_buf, identity_buf, password_buf, secret1_buf, hap = true) {
|
|
1907
|
+
assertIsBuffer(salt_buf, "salt (s)");
|
|
1908
|
+
assertIsBuffer(identity_buf, "identity (I)");
|
|
1909
|
+
assertIsBuffer(password_buf, "password (P)");
|
|
1910
|
+
assertIsBuffer(secret1_buf, "secret1");
|
|
1911
|
+
this._params = params;
|
|
1912
|
+
this._k = getk(params);
|
|
1913
|
+
this._x = getx(params, salt_buf, identity_buf, password_buf);
|
|
1914
|
+
this._a = new BigInteger(secret1_buf);
|
|
1915
|
+
if (hap) {
|
|
1916
|
+
this._I = identity_buf;
|
|
1917
|
+
this._s = salt_buf;
|
|
1918
|
+
}
|
|
1919
|
+
this._A = getA(params, this._a);
|
|
1920
|
+
}
|
|
1921
|
+
/**
|
|
1922
|
+
* Returns the client's public key (A).
|
|
1923
|
+
*
|
|
1924
|
+
* @return {Buffer}
|
|
1925
|
+
*/
|
|
1926
|
+
computeA() {
|
|
1927
|
+
return this._A;
|
|
1928
|
+
}
|
|
1929
|
+
/**
|
|
1930
|
+
* Sets the server's public key (B).
|
|
1931
|
+
*
|
|
1932
|
+
* @param {Buffer} B_buf The server's public key
|
|
1933
|
+
*/
|
|
1934
|
+
setB(B_buf) {
|
|
1935
|
+
const u_num = getu(this._params, this._A, B_buf);
|
|
1936
|
+
const S_buf_x = client_getS(this._params, this._k, this._x, this._a, new BigInteger(B_buf), u_num);
|
|
1937
|
+
this._K = getK(this._params, S_buf_x);
|
|
1938
|
+
this._u = u_num;
|
|
1939
|
+
this._S = S_buf_x;
|
|
1940
|
+
this._B = B_buf;
|
|
1941
|
+
if (this._I && this._s) this._M1 = getM1(this._params, this._I, this._s, this._A, this._B, this._K);
|
|
1942
|
+
else this._M1 = getM1(this._params, this._A, this._B, this._S);
|
|
1943
|
+
this._M2 = getM2(this._params, this._A, this._M1, this._K);
|
|
1944
|
+
}
|
|
1945
|
+
/**
|
|
1946
|
+
* Gets the M1 value.
|
|
1947
|
+
* This requires setting the server's public key {@see Client.setB}.
|
|
1948
|
+
*
|
|
1949
|
+
* @return {Buffer}
|
|
1950
|
+
*/
|
|
1951
|
+
computeM1() {
|
|
1952
|
+
if (this._M1 === void 0) throw new Error("incomplete protocol");
|
|
1953
|
+
return this._M1;
|
|
1954
|
+
}
|
|
1955
|
+
/**
|
|
1956
|
+
* Checks the server was able to calculate M2.
|
|
1957
|
+
* This requires setting the server's public key {@see Client.setB}.
|
|
1958
|
+
*
|
|
1959
|
+
* @param M2 The server's M2 value
|
|
1960
|
+
*/
|
|
1961
|
+
checkM2(M2) {
|
|
1962
|
+
if (!equal(this._M2, M2)) throw new Error("server is not authentic");
|
|
1963
|
+
}
|
|
1964
|
+
/**
|
|
1965
|
+
* Returns the shared session key.
|
|
1966
|
+
*
|
|
1967
|
+
* @return {Buffer}
|
|
1968
|
+
*/
|
|
1969
|
+
computeK() {
|
|
1970
|
+
if (this._K === void 0) throw new Error("incomplete protocol");
|
|
1971
|
+
return this._K;
|
|
1972
|
+
}
|
|
1973
|
+
};
|
|
1974
|
+
exports.SrpClient = SrpClient;
|
|
1975
|
+
var SrpServer = class {
|
|
1976
|
+
constructor(params, salt_buf, identity_buf, password_buf, secret2_buf) {
|
|
1977
|
+
this._params = params;
|
|
1978
|
+
this._k = getk(params);
|
|
1979
|
+
if (arguments.length > 3) {
|
|
1980
|
+
assertIsBuffer(salt_buf, "salt (salt)");
|
|
1981
|
+
assertIsBuffer(identity_buf, "identity (I)");
|
|
1982
|
+
assertIsBuffer(password_buf, "password (P)");
|
|
1983
|
+
assertIsBuffer(secret2_buf, "secret2");
|
|
1984
|
+
this._b = new BigInteger(secret2_buf);
|
|
1985
|
+
this._v = new BigInteger(SRP.computeVerifier(params, salt_buf, identity_buf, password_buf));
|
|
1986
|
+
this._I = identity_buf;
|
|
1987
|
+
this._s = salt_buf;
|
|
1988
|
+
} else if (salt_buf instanceof Buffer) {
|
|
1989
|
+
const verifier_buf = salt_buf;
|
|
1990
|
+
[secret2_buf, salt_buf, identity_buf, password_buf] = [
|
|
1991
|
+
identity_buf,
|
|
1992
|
+
void 0,
|
|
1993
|
+
void 0,
|
|
1994
|
+
void 0
|
|
1995
|
+
];
|
|
1996
|
+
assertIsBuffer(verifier_buf, "verifier (v)");
|
|
1997
|
+
assertIsBuffer(secret2_buf, "secret2");
|
|
1998
|
+
this._b = new BigInteger(secret2_buf);
|
|
1999
|
+
this._v = new BigInteger(verifier_buf);
|
|
2000
|
+
} else {
|
|
2001
|
+
const identity = salt_buf;
|
|
2002
|
+
[secret2_buf, salt_buf, identity_buf, password_buf] = [
|
|
2003
|
+
identity_buf,
|
|
2004
|
+
void 0,
|
|
2005
|
+
void 0,
|
|
2006
|
+
void 0
|
|
2007
|
+
];
|
|
2008
|
+
(0, assert_1.default)(identity.username instanceof Buffer || typeof identity.username === "string", "identity.username (I) must be a string or Buffer");
|
|
2009
|
+
assertIsBuffer(identity.salt, "identity.salt (s)");
|
|
2010
|
+
(0, assert_1.default)("password" in identity || "verifier" in identity, "identity requires a password or verifier");
|
|
2011
|
+
if ("verifier" in identity) assertIsBuffer(identity.verifier, "identity.verifier (v)");
|
|
2012
|
+
else (0, assert_1.default)(identity.password instanceof Buffer || typeof identity.password === "string", "identity.password (p) must be a string or Buffer");
|
|
2013
|
+
assertIsBuffer(secret2_buf, "secret2");
|
|
2014
|
+
const username = typeof identity.username === "string" ? Buffer.from(identity.username) : identity.username;
|
|
2015
|
+
this._b = new BigInteger(secret2_buf);
|
|
2016
|
+
if ("verifier" in identity) this._v = new BigInteger(identity.verifier);
|
|
2017
|
+
else this._v = new BigInteger(SRP.computeVerifier(params, identity.salt, username, typeof identity.password === "string" ? Buffer.from(identity.password) : identity.password));
|
|
2018
|
+
this._I = username;
|
|
2019
|
+
this._s = identity.salt;
|
|
2020
|
+
}
|
|
2021
|
+
this._B = getB(params, this._k, this._v, this._b);
|
|
2022
|
+
}
|
|
2023
|
+
/**
|
|
2024
|
+
* Returns the server's public key (B).
|
|
2025
|
+
*
|
|
2026
|
+
* @return {Buffer}
|
|
2027
|
+
*/
|
|
2028
|
+
computeB() {
|
|
2029
|
+
return this._B;
|
|
2030
|
+
}
|
|
2031
|
+
/**
|
|
2032
|
+
* Sets the client's public key (A).
|
|
2033
|
+
*
|
|
2034
|
+
* @param {Buffer} A The client's public key
|
|
2035
|
+
*/
|
|
2036
|
+
setA(A) {
|
|
2037
|
+
const u_num = getu(this._params, A, this._B);
|
|
2038
|
+
const S_buf = server_getS(this._params, this._v, new BigInteger(A), this._b, u_num);
|
|
2039
|
+
this._K = getK(this._params, S_buf);
|
|
2040
|
+
this._u = u_num;
|
|
2041
|
+
this._S = S_buf;
|
|
2042
|
+
if (this._I && this._s) this._M1 = getM1(this._params, this._I, this._s, A, this._B, this._K);
|
|
2043
|
+
else this._M1 = getM1(this._params, A, this._B, this._S);
|
|
2044
|
+
this._M2 = getM2(this._params, A, this._M1, this._K);
|
|
2045
|
+
}
|
|
2046
|
+
/**
|
|
2047
|
+
* Checks the client was able to calculate M1.
|
|
2048
|
+
*
|
|
2049
|
+
* @param {Buffer} M1 The client's M1 value
|
|
2050
|
+
*/
|
|
2051
|
+
checkM1(M1) {
|
|
2052
|
+
if (this._M1 === void 0) throw new Error("incomplete protocol");
|
|
2053
|
+
if (!equal(this._M1, M1)) throw new Error("client did not use the same password");
|
|
2054
|
+
}
|
|
2055
|
+
/**
|
|
2056
|
+
* Returns the shared session key.
|
|
2057
|
+
*
|
|
2058
|
+
* @return {Buffer}
|
|
2059
|
+
*/
|
|
2060
|
+
computeK() {
|
|
2061
|
+
if (this._K === void 0) throw new Error("incomplete protocol");
|
|
2062
|
+
return this._K;
|
|
2063
|
+
}
|
|
2064
|
+
/**
|
|
2065
|
+
* Gets the M2 value.
|
|
2066
|
+
* This requires setting the client's public key {@see Server.setA}.
|
|
2067
|
+
*
|
|
2068
|
+
* @return {Buffer}
|
|
2069
|
+
*/
|
|
2070
|
+
computeM2() {
|
|
2071
|
+
if (this._M2 === void 0) throw new Error("incomplete protocol");
|
|
2072
|
+
return this._M2;
|
|
2073
|
+
}
|
|
2074
|
+
};
|
|
2075
|
+
exports.SrpServer = SrpServer;
|
|
2076
|
+
}));
|
|
2077
|
+
|
|
2078
|
+
//#endregion
|
|
2079
|
+
//#region src/pairing/const.ts
|
|
2080
|
+
var import_srp = require_srp();
|
|
2081
|
+
/** The fixed setup code of a HAP transient pair-setup. */
|
|
2082
|
+
const TRANSIENT_PAIRING_PIN = "3939";
|
|
2083
|
+
|
|
2084
|
+
//#endregion
|
|
2085
|
+
//#region src/pairing/client.ts
|
|
2086
|
+
/**
|
|
2087
|
+
* Runs HAP pair-setup as the controller, with SRP-6a followed by an Ed25519 identity exchange.
|
|
2088
|
+
*
|
|
2089
|
+
* A PIN pairing runs M1 to M6 and yields long-term credentials. A transient pairing stops after M4
|
|
2090
|
+
* with the fixed {@link TRANSIENT_PAIRING_PIN} and yields session keys only. The steps are public
|
|
2091
|
+
* for a caller that has to wait for the PIN between M1 and M2.
|
|
2092
|
+
*/
|
|
2093
|
+
var PairSetup = class {
|
|
2094
|
+
#context;
|
|
2095
|
+
#pairingId;
|
|
2096
|
+
#transport;
|
|
2097
|
+
#useAes;
|
|
2098
|
+
#publicKey;
|
|
2099
|
+
#secretKey;
|
|
2100
|
+
#srp;
|
|
2101
|
+
constructor(context, transport, options = {}) {
|
|
2102
|
+
this.#context = context;
|
|
2103
|
+
this.#pairingId = Buffer.from(randomUUID().toUpperCase());
|
|
2104
|
+
this.#transport = transport;
|
|
2105
|
+
this.#useAes = options.useAes ?? false;
|
|
2106
|
+
}
|
|
2107
|
+
/** Generates the controller's long-term Ed25519 key pair. Call before {@link pin}. */
|
|
2108
|
+
async start() {
|
|
2109
|
+
const keyPair = generateKeyPair();
|
|
2110
|
+
this.#publicKey = Buffer.from(keyPair.publicKey);
|
|
2111
|
+
this.#secretKey = Buffer.from(keyPair.secretKey);
|
|
2112
|
+
}
|
|
2113
|
+
/**
|
|
2114
|
+
* Runs a PIN pairing from M1 to M6. It calls `askPin` after M1, once the accessory shows the PIN.
|
|
2115
|
+
*
|
|
2116
|
+
* @throws {PairingError} If a step fails.
|
|
2117
|
+
*/
|
|
2118
|
+
async pin(askPin) {
|
|
2119
|
+
const m1 = await this.m1();
|
|
2120
|
+
const m2 = await this.m2(m1, await askPin());
|
|
2121
|
+
const m3 = await this.m3(m2);
|
|
2122
|
+
const m4 = await this.m4(m3);
|
|
2123
|
+
const m5 = await this.m5(m4);
|
|
2124
|
+
const m6 = await this.m6(m4, m5);
|
|
2125
|
+
if (!m6) throw new PairingError("Pairing failed, could not get accessory keys.");
|
|
2126
|
+
return m6;
|
|
2127
|
+
}
|
|
2128
|
+
/** Runs a transient pairing from M1 to M4 without a PIN. The keys hold for this connection only. */
|
|
2129
|
+
async transient() {
|
|
2130
|
+
const m1 = await this.m1([[Value.Flags, Flags.TransientPairing]]);
|
|
2131
|
+
const m2 = await this.m2(m1);
|
|
2132
|
+
const m3 = await this.m3(m2);
|
|
2133
|
+
const m4 = await this.m4(m3);
|
|
2134
|
+
return {
|
|
2135
|
+
pairingId: this.#pairingId,
|
|
2136
|
+
sharedSecret: m4.sharedSecret,
|
|
2137
|
+
...deriveControlKeys(m4.sharedSecret)
|
|
2138
|
+
};
|
|
2139
|
+
}
|
|
2140
|
+
/** Asks the accessory for its SRP public key and salt. */
|
|
2141
|
+
async m1(additionalTlv = []) {
|
|
2142
|
+
const response = await this.#transport(encode([
|
|
2143
|
+
[Value.Method, Method.PairSetup],
|
|
2144
|
+
[Value.State, State.M1],
|
|
2145
|
+
...additionalTlv
|
|
2146
|
+
]), "m1");
|
|
2147
|
+
const data = decodeTlv(this.#context, response);
|
|
2148
|
+
return {
|
|
2149
|
+
publicKey: data.get(Value.PublicKey),
|
|
2150
|
+
salt: data.get(Value.Salt)
|
|
2151
|
+
};
|
|
2152
|
+
}
|
|
2153
|
+
/** Computes the SRP public key and the proof that we know the PIN. Sends nothing. */
|
|
2154
|
+
async m2(m1, pin = TRANSIENT_PAIRING_PIN) {
|
|
2155
|
+
const srpKey = await import_srp.SRP.genKey(32);
|
|
2156
|
+
this.#srp = new import_srp.SrpClient(import_srp.SRP.params.hap, m1.salt, Buffer.from("Pair-Setup"), Buffer.from(pin), srpKey, true);
|
|
2157
|
+
this.#srp.setB(m1.publicKey);
|
|
2158
|
+
return {
|
|
2159
|
+
publicKey: this.#srp.computeA(),
|
|
2160
|
+
proof: this.#srp.computeM1()
|
|
2161
|
+
};
|
|
2162
|
+
}
|
|
2163
|
+
/** Sends our SRP public key and proof, and receives the accessory's proof. */
|
|
2164
|
+
async m3(m2) {
|
|
2165
|
+
const response = await this.#transport(encode([
|
|
2166
|
+
[Value.State, State.M3],
|
|
2167
|
+
[Value.PublicKey, m2.publicKey],
|
|
2168
|
+
[Value.Proof, m2.proof]
|
|
2169
|
+
]), "m3");
|
|
2170
|
+
return { serverProof: decodeTlv(this.#context, response).get(Value.Proof) };
|
|
2171
|
+
}
|
|
2172
|
+
/** Checks the accessory's proof and derives the SRP shared secret. Sends nothing. */
|
|
2173
|
+
async m4(m3) {
|
|
2174
|
+
this.#srp.checkM2(m3.serverProof);
|
|
2175
|
+
return { sharedSecret: this.#srp.computeK() };
|
|
2176
|
+
}
|
|
2177
|
+
/** Sends our signed, encrypted identity and receives the accessory's encrypted identity. */
|
|
2178
|
+
async m5(m4) {
|
|
2179
|
+
const iosDeviceX = deriveKey(m4.sharedSecret, "Pair-Setup-Controller-Sign-Salt", "Pair-Setup-Controller-Sign-Info");
|
|
2180
|
+
const sessionKey = deriveKey(m4.sharedSecret, "Pair-Setup-Encrypt-Salt", "Pair-Setup-Encrypt-Info");
|
|
2181
|
+
const cipher = pairingCipher("Pair-Setup", m4.sharedSecret, sessionKey, this.#useAes);
|
|
2182
|
+
const signature = sign(Buffer.concat([
|
|
2183
|
+
iosDeviceX,
|
|
2184
|
+
this.#pairingId,
|
|
2185
|
+
this.#publicKey
|
|
2186
|
+
]), this.#secretKey);
|
|
2187
|
+
const innerTlv = encode([
|
|
2188
|
+
[Value.Identifier, this.#pairingId],
|
|
2189
|
+
[Value.PublicKey, this.#publicKey],
|
|
2190
|
+
[Value.Signature, Buffer.from(signature)],
|
|
2191
|
+
[Value.Name, encode$1({ name: this.#context.identity.name ?? "CoreWire" })]
|
|
2192
|
+
]);
|
|
2193
|
+
const response = await this.#transport(encode([[Value.State, State.M5], [Value.EncryptedData, cipher.seal("PS-Msg05", innerTlv)]]), "m5");
|
|
2194
|
+
return {
|
|
2195
|
+
encryptedData: decodeTlv(this.#context, response).get(Value.EncryptedData),
|
|
2196
|
+
sessionKey
|
|
2197
|
+
};
|
|
2198
|
+
}
|
|
2199
|
+
/**
|
|
2200
|
+
* Decrypts and verifies the accessory's identity from M5. Sends nothing.
|
|
2201
|
+
*
|
|
2202
|
+
* @throws {AuthenticationError} If the accessory signature is invalid.
|
|
2203
|
+
*/
|
|
2204
|
+
async m6(m4, m5) {
|
|
2205
|
+
const cipher = pairingCipher("Pair-Setup", m4.sharedSecret, m5.sessionKey, this.#useAes);
|
|
2206
|
+
const tlv = decode(cipher.open("PS-Msg06", m5.encryptedData));
|
|
2207
|
+
const accessoryIdentifier = tlv.get(Value.Identifier);
|
|
2208
|
+
const accessoryLongTermPublicKey = tlv.get(Value.PublicKey);
|
|
2209
|
+
const accessorySignature = tlv.get(Value.Signature);
|
|
2210
|
+
const accessoryX = deriveKey(m4.sharedSecret, "Pair-Setup-Accessory-Sign-Salt", "Pair-Setup-Accessory-Sign-Info");
|
|
2211
|
+
const accessoryInfo = Buffer.concat([
|
|
2212
|
+
accessoryX,
|
|
2213
|
+
accessoryIdentifier,
|
|
2214
|
+
accessoryLongTermPublicKey
|
|
2215
|
+
]);
|
|
2216
|
+
if (!verify(accessoryInfo, accessorySignature, accessoryLongTermPublicKey)) throw new AuthenticationError("Invalid accessory signature.");
|
|
2217
|
+
return {
|
|
2218
|
+
accessoryIdentifier: accessoryIdentifier.toString(),
|
|
2219
|
+
accessoryLongTermPublicKey,
|
|
2220
|
+
pairingId: this.#pairingId,
|
|
2221
|
+
publicKey: this.#publicKey,
|
|
2222
|
+
secretKey: this.#secretKey
|
|
2223
|
+
};
|
|
2224
|
+
}
|
|
2225
|
+
};
|
|
2226
|
+
/**
|
|
2227
|
+
* Runs HAP pair-verify as the controller. An ephemeral Curve25519 exchange proves both long-term
|
|
2228
|
+
* identities from an earlier pair-setup, so every session gets fresh keys without the PIN.
|
|
2229
|
+
*/
|
|
2230
|
+
var PairVerify = class {
|
|
2231
|
+
#context;
|
|
2232
|
+
#ephemeralKeyPair;
|
|
2233
|
+
#transport;
|
|
2234
|
+
#useAes;
|
|
2235
|
+
constructor(context, transport, options = {}) {
|
|
2236
|
+
this.#context = context;
|
|
2237
|
+
this.#ephemeralKeyPair = generateKeyPair$1();
|
|
2238
|
+
this.#transport = transport;
|
|
2239
|
+
this.#useAes = options.useAes ?? false;
|
|
2240
|
+
}
|
|
2241
|
+
/**
|
|
2242
|
+
* Verifies the pairing and returns the shared secret. The channel keys stay empty, because
|
|
2243
|
+
* each protocol derives its own from the secret.
|
|
2244
|
+
*
|
|
2245
|
+
* @throws {AuthenticationError} If the accessory's identity does not match the credentials.
|
|
2246
|
+
*/
|
|
2247
|
+
async start(credentials) {
|
|
2248
|
+
const m1 = await this.#m1();
|
|
2249
|
+
const m2 = await this.#m2(credentials.accessoryIdentifier, credentials.accessoryLongTermPublicKey, m1);
|
|
2250
|
+
await this.#m3(credentials.pairingId, credentials.secretKey, m2);
|
|
2251
|
+
return {
|
|
2252
|
+
accessoryToControllerKey: Buffer.alloc(0),
|
|
2253
|
+
controllerToAccessoryKey: Buffer.alloc(0),
|
|
2254
|
+
pairingId: credentials.pairingId,
|
|
2255
|
+
sharedSecret: m2.sharedSecret
|
|
2256
|
+
};
|
|
2257
|
+
}
|
|
2258
|
+
async #m1() {
|
|
2259
|
+
const response = await this.#transport(encode([[Value.State, State.M1], [Value.PublicKey, Buffer.from(this.#ephemeralKeyPair.publicKey)]]), "m1");
|
|
2260
|
+
const data = decodeTlv(this.#context, response);
|
|
2261
|
+
return {
|
|
2262
|
+
encryptedData: data.get(Value.EncryptedData),
|
|
2263
|
+
serverPublicKey: data.get(Value.PublicKey)
|
|
2264
|
+
};
|
|
2265
|
+
}
|
|
2266
|
+
/** Derives the shared secret and verifies the accessory's signed identity. Sends nothing. */
|
|
2267
|
+
async #m2(localAccessoryIdentifier, longTermPublicKey, m1) {
|
|
2268
|
+
const sharedSecret = Buffer.from(generateSharedSecKey(this.#ephemeralKeyPair.secretKey, m1.serverPublicKey));
|
|
2269
|
+
const cipher = pairingCipher("Pair-Verify", sharedSecret, deriveKey(sharedSecret, "Pair-Verify-Encrypt-Salt", "Pair-Verify-Encrypt-Info"), this.#useAes);
|
|
2270
|
+
const tlv = decode(cipher.open("PV-Msg02", m1.encryptedData));
|
|
2271
|
+
const accessoryIdentifier = tlv.get(Value.Identifier);
|
|
2272
|
+
const accessorySignature = tlv.get(Value.Signature);
|
|
2273
|
+
if (accessoryIdentifier.toString() !== localAccessoryIdentifier) throw new AuthenticationError(`Invalid accessory identifier. Expected ${accessoryIdentifier.toString()} to be ${localAccessoryIdentifier}.`);
|
|
2274
|
+
const accessoryInfo = Buffer.concat([
|
|
2275
|
+
m1.serverPublicKey,
|
|
2276
|
+
accessoryIdentifier,
|
|
2277
|
+
this.#ephemeralKeyPair.publicKey
|
|
2278
|
+
]);
|
|
2279
|
+
if (!verify(accessoryInfo, accessorySignature, longTermPublicKey)) throw new AuthenticationError("Invalid accessory signature.");
|
|
2280
|
+
return {
|
|
2281
|
+
cipher,
|
|
2282
|
+
serverEphemeralPublicKey: m1.serverPublicKey,
|
|
2283
|
+
sharedSecret
|
|
2284
|
+
};
|
|
2285
|
+
}
|
|
2286
|
+
/** Sends our signed proof. The M4 reply carries nothing we use. */
|
|
2287
|
+
async #m3(pairingId, secretKey, m2) {
|
|
2288
|
+
const iosDeviceInfo = Buffer.concat([
|
|
2289
|
+
this.#ephemeralKeyPair.publicKey,
|
|
2290
|
+
pairingId,
|
|
2291
|
+
m2.serverEphemeralPublicKey
|
|
2292
|
+
]);
|
|
2293
|
+
const iosDeviceSignature = Buffer.from(sign(iosDeviceInfo, secretKey));
|
|
2294
|
+
const innerTlv = encode([[Value.Identifier, pairingId], [Value.Signature, iosDeviceSignature]]);
|
|
2295
|
+
await this.#transport(encode([[Value.State, State.M3], [Value.EncryptedData, m2.cipher.seal("PV-Msg03", innerTlv)]]), "m3");
|
|
2296
|
+
}
|
|
2297
|
+
};
|
|
2298
|
+
|
|
2299
|
+
//#endregion
|
|
2300
|
+
//#region src/pairing/server.ts
|
|
2301
|
+
/**
|
|
2302
|
+
* Answers HAP pair-setup as the accessory, M1, M3 and M5 with M2, M4 and M6. After M5,
|
|
2303
|
+
* {@link controller} holds the verified controller identity to persist.
|
|
2304
|
+
*/
|
|
2305
|
+
var PairSetupServer = class {
|
|
2306
|
+
get controller() {
|
|
2307
|
+
return this.#controller;
|
|
2308
|
+
}
|
|
2309
|
+
/** The SRP session key, available after M3. */
|
|
2310
|
+
get sharedSecret() {
|
|
2311
|
+
return this.#sharedSecret;
|
|
2312
|
+
}
|
|
2313
|
+
#context;
|
|
2314
|
+
#identity;
|
|
2315
|
+
#pin;
|
|
2316
|
+
#useAes;
|
|
2317
|
+
#controller;
|
|
2318
|
+
#sharedSecret;
|
|
2319
|
+
#srp;
|
|
2320
|
+
constructor(context, identity, pin, options = {}) {
|
|
2321
|
+
this.#context = context;
|
|
2322
|
+
this.#identity = identity;
|
|
2323
|
+
this.#pin = pin;
|
|
2324
|
+
this.#useAes = options.useAes ?? false;
|
|
2325
|
+
}
|
|
2326
|
+
/**
|
|
2327
|
+
* Answers one pair-setup request with the next message.
|
|
2328
|
+
*
|
|
2329
|
+
* @throws {PairingError} If the request carries an unexpected state.
|
|
2330
|
+
* @throws {AuthenticationError} If the PIN proof or the controller signature is wrong.
|
|
2331
|
+
*/
|
|
2332
|
+
async handle(request) {
|
|
2333
|
+
const data = decodeTlv(this.#context, request);
|
|
2334
|
+
const state = data.get(Value.State)?.[0];
|
|
2335
|
+
switch (state) {
|
|
2336
|
+
case State.M1: return this.#m1();
|
|
2337
|
+
case State.M3: return this.#m3(data);
|
|
2338
|
+
case State.M5: return this.#m5(data);
|
|
2339
|
+
default: throw new PairingError(`Unexpected pair-setup state ${state}.`);
|
|
2340
|
+
}
|
|
2341
|
+
}
|
|
2342
|
+
async #m1() {
|
|
2343
|
+
const salt = randomBytes(16);
|
|
2344
|
+
const secretKey = await import_srp.SRP.genKey(32);
|
|
2345
|
+
this.#srp = new import_srp.SrpServer(import_srp.SRP.params.hap, salt, Buffer.from("Pair-Setup"), Buffer.from(this.#pin), secretKey);
|
|
2346
|
+
return encode([
|
|
2347
|
+
[Value.State, State.M2],
|
|
2348
|
+
[Value.PublicKey, this.#srp.computeB()],
|
|
2349
|
+
[Value.Salt, salt]
|
|
2350
|
+
]);
|
|
2351
|
+
}
|
|
2352
|
+
async #m3(data) {
|
|
2353
|
+
this.#srp.setA(data.get(Value.PublicKey));
|
|
2354
|
+
try {
|
|
2355
|
+
this.#srp.checkM1(data.get(Value.Proof));
|
|
2356
|
+
} catch {
|
|
2357
|
+
throw new AuthenticationError("Invalid controller proof, wrong PIN.");
|
|
2358
|
+
}
|
|
2359
|
+
const serverProof = this.#srp.computeM2();
|
|
2360
|
+
this.#sharedSecret = this.#srp.computeK();
|
|
2361
|
+
return encode([[Value.State, State.M4], [Value.Proof, serverProof]]);
|
|
2362
|
+
}
|
|
2363
|
+
async #m5(data) {
|
|
2364
|
+
const sharedSecret = this.#sharedSecret;
|
|
2365
|
+
const cipher = pairingCipher("Pair-Setup", sharedSecret, deriveKey(sharedSecret, "Pair-Setup-Encrypt-Salt", "Pair-Setup-Encrypt-Info"), this.#useAes);
|
|
2366
|
+
const inner = decode(cipher.open("PS-Msg05", data.get(Value.EncryptedData)));
|
|
2367
|
+
const controllerIdentifier = inner.get(Value.Identifier);
|
|
2368
|
+
const controllerPublicKey = inner.get(Value.PublicKey);
|
|
2369
|
+
const controllerSignature = inner.get(Value.Signature);
|
|
2370
|
+
const iosDeviceX = deriveKey(sharedSecret, "Pair-Setup-Controller-Sign-Salt", "Pair-Setup-Controller-Sign-Info");
|
|
2371
|
+
if (!verify(Buffer.concat([
|
|
2372
|
+
iosDeviceX,
|
|
2373
|
+
controllerIdentifier,
|
|
2374
|
+
controllerPublicKey
|
|
2375
|
+
]), controllerSignature, controllerPublicKey)) throw new AuthenticationError("Invalid controller signature.");
|
|
2376
|
+
this.#controller = {
|
|
2377
|
+
identifier: controllerIdentifier.toString(),
|
|
2378
|
+
longTermPublicKey: controllerPublicKey
|
|
2379
|
+
};
|
|
2380
|
+
const accessoryIdentifier = Buffer.from(this.#identity.identifier);
|
|
2381
|
+
const accessoryX = deriveKey(sharedSecret, "Pair-Setup-Accessory-Sign-Salt", "Pair-Setup-Accessory-Sign-Info");
|
|
2382
|
+
const accessorySignature = Buffer.from(sign(Buffer.concat([
|
|
2383
|
+
accessoryX,
|
|
2384
|
+
accessoryIdentifier,
|
|
2385
|
+
this.#identity.publicKey
|
|
2386
|
+
]), this.#identity.secretKey));
|
|
2387
|
+
const innerTlv = encode([
|
|
2388
|
+
[Value.Identifier, accessoryIdentifier],
|
|
2389
|
+
[Value.PublicKey, this.#identity.publicKey],
|
|
2390
|
+
[Value.Signature, accessorySignature]
|
|
2391
|
+
]);
|
|
2392
|
+
return encode([[Value.State, State.M6], [Value.EncryptedData, cipher.seal("PS-Msg06", innerTlv)]]);
|
|
2393
|
+
}
|
|
2394
|
+
};
|
|
2395
|
+
/**
|
|
2396
|
+
* Answers HAP pair-verify as the accessory, M1 and M3 with M2 and M4. Once M3 verifies the
|
|
2397
|
+
* controller, {@link sharedSecret} feeds the channel key derivation.
|
|
2398
|
+
*/
|
|
2399
|
+
var PairVerifyServer = class {
|
|
2400
|
+
/** The pairing identifier of the verified controller, available after M3. */
|
|
2401
|
+
get pairingId() {
|
|
2402
|
+
return this.#pairingId;
|
|
2403
|
+
}
|
|
2404
|
+
/** The Curve25519 shared secret, available after M1. */
|
|
2405
|
+
get sharedSecret() {
|
|
2406
|
+
return this.#sharedSecret;
|
|
2407
|
+
}
|
|
2408
|
+
#context;
|
|
2409
|
+
#identity;
|
|
2410
|
+
#resolveController;
|
|
2411
|
+
#useAes;
|
|
2412
|
+
#cipher;
|
|
2413
|
+
#controllerPublicKey;
|
|
2414
|
+
#ephemeralKeyPair;
|
|
2415
|
+
#pairingId;
|
|
2416
|
+
#sharedSecret;
|
|
2417
|
+
constructor(context, identity, resolveController, options = {}) {
|
|
2418
|
+
this.#context = context;
|
|
2419
|
+
this.#identity = identity;
|
|
2420
|
+
this.#resolveController = resolveController;
|
|
2421
|
+
this.#useAes = options.useAes ?? false;
|
|
2422
|
+
}
|
|
2423
|
+
/**
|
|
2424
|
+
* Answers one pair-verify request with the next message.
|
|
2425
|
+
*
|
|
2426
|
+
* @throws {PairingError} If the request carries an unexpected state.
|
|
2427
|
+
* @throws {AuthenticationError} If the controller is unknown or its signature is wrong.
|
|
2428
|
+
*/
|
|
2429
|
+
async handle(request) {
|
|
2430
|
+
const data = decodeTlv(this.#context, request);
|
|
2431
|
+
const state = data.get(Value.State)?.[0];
|
|
2432
|
+
switch (state) {
|
|
2433
|
+
case State.M1: return this.#m1(data);
|
|
2434
|
+
case State.M3: return this.#m3(data);
|
|
2435
|
+
default: throw new PairingError(`Unexpected pair-verify state ${state}.`);
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
async #m1(data) {
|
|
2439
|
+
this.#controllerPublicKey = data.get(Value.PublicKey);
|
|
2440
|
+
this.#ephemeralKeyPair = generateKeyPair$1();
|
|
2441
|
+
const sharedSecret = Buffer.from(generateSharedSecKey(this.#ephemeralKeyPair.secretKey, this.#controllerPublicKey));
|
|
2442
|
+
const sessionKey = deriveKey(sharedSecret, "Pair-Verify-Encrypt-Salt", "Pair-Verify-Encrypt-Info");
|
|
2443
|
+
this.#sharedSecret = sharedSecret;
|
|
2444
|
+
this.#cipher = pairingCipher("Pair-Verify", sharedSecret, sessionKey, this.#useAes);
|
|
2445
|
+
const accessoryEphemeralPublicKey = Buffer.from(this.#ephemeralKeyPair.publicKey);
|
|
2446
|
+
const accessoryIdentifier = Buffer.from(this.#identity.identifier);
|
|
2447
|
+
const accessoryInfo = Buffer.concat([
|
|
2448
|
+
accessoryEphemeralPublicKey,
|
|
2449
|
+
accessoryIdentifier,
|
|
2450
|
+
this.#controllerPublicKey
|
|
2451
|
+
]);
|
|
2452
|
+
const accessorySignature = Buffer.from(sign(accessoryInfo, this.#identity.secretKey));
|
|
2453
|
+
const innerTlv = encode([[Value.Identifier, accessoryIdentifier], [Value.Signature, accessorySignature]]);
|
|
2454
|
+
return encode([
|
|
2455
|
+
[Value.State, State.M2],
|
|
2456
|
+
[Value.PublicKey, accessoryEphemeralPublicKey],
|
|
2457
|
+
[Value.EncryptedData, this.#cipher.seal("PV-Msg02", innerTlv)]
|
|
2458
|
+
]);
|
|
2459
|
+
}
|
|
2460
|
+
async #m3(data) {
|
|
2461
|
+
const inner = decode(this.#cipher.open("PV-Msg03", data.get(Value.EncryptedData)));
|
|
2462
|
+
const controllerIdentifier = inner.get(Value.Identifier);
|
|
2463
|
+
const controllerSignature = inner.get(Value.Signature);
|
|
2464
|
+
const longTermPublicKey = this.#resolveController(controllerIdentifier.toString());
|
|
2465
|
+
if (!longTermPublicKey) throw new AuthenticationError("Unknown controller. Pair before verifying.");
|
|
2466
|
+
const controllerInfo = Buffer.concat([
|
|
2467
|
+
this.#controllerPublicKey,
|
|
2468
|
+
controllerIdentifier,
|
|
2469
|
+
Buffer.from(this.#ephemeralKeyPair.publicKey)
|
|
2470
|
+
]);
|
|
2471
|
+
if (!verify(controllerInfo, controllerSignature, longTermPublicKey)) throw new AuthenticationError("Invalid controller signature.");
|
|
2472
|
+
this.#pairingId = controllerIdentifier;
|
|
2473
|
+
return encode([[Value.State, State.M4]]);
|
|
2474
|
+
}
|
|
2475
|
+
};
|
|
2476
|
+
/** Generates an accessory identity with a random MAC-style identifier and a new Ed25519 key pair. */
|
|
2477
|
+
function generateAccessoryIdentity() {
|
|
2478
|
+
const keyPair = generateKeyPair();
|
|
2479
|
+
return {
|
|
2480
|
+
identifier: Array.from(randomBytes(6)).map((byte) => byte.toString(16).padStart(2, "0").toUpperCase()).join(":"),
|
|
2481
|
+
publicKey: Buffer.from(keyPair.publicKey),
|
|
2482
|
+
secretKey: Buffer.from(keyPair.secretKey)
|
|
2483
|
+
};
|
|
2484
|
+
}
|
|
2485
|
+
|
|
2486
|
+
//#endregion
|
|
2487
|
+
export { PairSetup, PairSetupServer, PairVerify, PairVerifyServer, TRANSIENT_PAIRING_PIN, deriveControlKeys, generateAccessoryIdentity };
|