@mtcute/core 0.1.0 → 0.1.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/cjs/base-client.js +4 -1
- package/cjs/base-client.js.map +1 -1
- package/cjs/network/auth-key.d.ts +3 -3
- package/cjs/network/auth-key.js +12 -12
- package/cjs/network/auth-key.js.map +1 -1
- package/cjs/network/authorization.js +62 -57
- package/cjs/network/authorization.js.map +1 -1
- package/cjs/network/mtproto-session.d.ts +2 -2
- package/cjs/network/mtproto-session.js +2 -2
- package/cjs/network/mtproto-session.js.map +1 -1
- package/cjs/network/multi-session-connection.d.ts +1 -1
- package/cjs/network/multi-session-connection.js +2 -2
- package/cjs/network/multi-session-connection.js.map +1 -1
- package/cjs/network/network-manager.d.ts +1 -0
- package/cjs/network/network-manager.js +21 -27
- package/cjs/network/network-manager.js.map +1 -1
- package/cjs/network/session-connection.d.ts +2 -1
- package/cjs/network/session-connection.js +48 -35
- package/cjs/network/session-connection.js.map +1 -1
- package/cjs/network/transports/intermediate.d.ts +3 -0
- package/cjs/network/transports/intermediate.js +5 -3
- package/cjs/network/transports/intermediate.js.map +1 -1
- package/cjs/network/transports/obfuscated.js +14 -18
- package/cjs/network/transports/obfuscated.js.map +1 -1
- package/cjs/storage/abstract.d.ts +23 -2
- package/cjs/storage/abstract.js.map +1 -1
- package/cjs/storage/json.js +8 -9
- package/cjs/storage/json.js.map +1 -1
- package/cjs/storage/localstorage.js +5 -2
- package/cjs/storage/localstorage.js.map +1 -1
- package/cjs/storage/memory.d.ts +7 -8
- package/cjs/storage/memory.js +7 -7
- package/cjs/storage/memory.js.map +1 -1
- package/cjs/utils/bigint-utils.d.ts +18 -11
- package/cjs/utils/bigint-utils.js +148 -47
- package/cjs/utils/bigint-utils.js.map +1 -1
- package/cjs/utils/buffer-utils.d.ts +7 -1
- package/cjs/utils/buffer-utils.js +16 -3
- package/cjs/utils/buffer-utils.js.map +1 -1
- package/cjs/utils/crypto/abstract.d.ts +16 -9
- package/cjs/utils/crypto/abstract.js +6 -9
- package/cjs/utils/crypto/abstract.js.map +1 -1
- package/cjs/utils/crypto/factorization.d.ts +2 -1
- package/cjs/utils/crypto/factorization.js +28 -36
- package/cjs/utils/crypto/factorization.js.map +1 -1
- package/cjs/utils/crypto/index.d.ts +4 -0
- package/cjs/utils/crypto/index.js +4 -0
- package/cjs/utils/crypto/index.js.map +1 -1
- package/cjs/utils/crypto/keys.d.ts +2 -2
- package/cjs/utils/crypto/keys.js +4 -4
- package/cjs/utils/crypto/keys.js.map +1 -1
- package/cjs/utils/crypto/miller-rabin.d.ts +2 -2
- package/cjs/utils/crypto/miller-rabin.js +20 -22
- package/cjs/utils/crypto/miller-rabin.js.map +1 -1
- package/cjs/utils/crypto/mtproto.d.ts +3 -3
- package/cjs/utils/crypto/mtproto.js +12 -12
- package/cjs/utils/crypto/mtproto.js.map +1 -1
- package/cjs/utils/crypto/node.d.ts +21 -0
- package/cjs/utils/crypto/node.js +74 -0
- package/cjs/utils/crypto/node.js.map +1 -0
- package/cjs/utils/crypto/password.js +22 -33
- package/cjs/utils/crypto/password.js.map +1 -1
- package/cjs/utils/crypto/wasm.d.ts +22 -0
- package/cjs/utils/crypto/wasm.js +41 -0
- package/cjs/utils/crypto/wasm.js.map +1 -0
- package/cjs/utils/crypto/web.d.ts +11 -0
- package/cjs/utils/crypto/web.js +40 -0
- package/cjs/utils/crypto/web.js.map +1 -0
- package/cjs/utils/deque.d.ts +1 -1
- package/cjs/utils/deque.js +9 -3
- package/cjs/utils/deque.js.map +1 -1
- package/cjs/utils/function-utils.d.ts +4 -1
- package/cjs/utils/function-utils.js +8 -1
- package/cjs/utils/function-utils.js.map +1 -1
- package/cjs/utils/index.d.ts +0 -1
- package/cjs/utils/index.js +0 -1
- package/cjs/utils/index.js.map +1 -1
- package/cjs/utils/logger.d.ts +0 -1
- package/cjs/utils/logger.js +3 -4
- package/cjs/utils/logger.js.map +1 -1
- package/cjs/utils/lru-map.d.ts +1 -0
- package/cjs/utils/lru-map.js +6 -0
- package/cjs/utils/lru-map.js.map +1 -1
- package/cjs/utils/peer-utils.js +5 -1
- package/cjs/utils/peer-utils.js.map +1 -1
- package/cjs/utils/platform/crypto.js +2 -2
- package/cjs/utils/platform/crypto.js.map +1 -1
- package/cjs/utils/platform/crypto.web.js +2 -2
- package/cjs/utils/platform/crypto.web.js.map +1 -1
- package/cjs/utils/string-session.js +4 -2
- package/cjs/utils/string-session.js.map +1 -1
- package/esm/base-client.js +4 -1
- package/esm/base-client.js.map +1 -1
- package/esm/network/auth-key.d.ts +3 -3
- package/esm/network/auth-key.js +13 -13
- package/esm/network/auth-key.js.map +1 -1
- package/esm/network/authorization.js +64 -59
- package/esm/network/authorization.js.map +1 -1
- package/esm/network/mtproto-session.d.ts +2 -2
- package/esm/network/mtproto-session.js +2 -2
- package/esm/network/mtproto-session.js.map +1 -1
- package/esm/network/multi-session-connection.d.ts +1 -1
- package/esm/network/multi-session-connection.js +2 -2
- package/esm/network/multi-session-connection.js.map +1 -1
- package/esm/network/network-manager.d.ts +1 -0
- package/esm/network/network-manager.js +21 -27
- package/esm/network/network-manager.js.map +1 -1
- package/esm/network/session-connection.d.ts +2 -1
- package/esm/network/session-connection.js +50 -37
- package/esm/network/session-connection.js.map +1 -1
- package/esm/network/transports/intermediate.d.ts +3 -0
- package/esm/network/transports/intermediate.js +6 -4
- package/esm/network/transports/intermediate.js.map +1 -1
- package/esm/network/transports/obfuscated.js +15 -19
- package/esm/network/transports/obfuscated.js.map +1 -1
- package/esm/storage/abstract.d.ts +23 -2
- package/esm/storage/abstract.js.map +1 -1
- package/esm/storage/json.js +8 -9
- package/esm/storage/json.js.map +1 -1
- package/esm/storage/localstorage.js +5 -2
- package/esm/storage/localstorage.js.map +1 -1
- package/esm/storage/memory.d.ts +7 -8
- package/esm/storage/memory.js +7 -7
- package/esm/storage/memory.js.map +1 -1
- package/esm/utils/bigint-utils.d.ts +18 -11
- package/esm/utils/bigint-utils.js +142 -44
- package/esm/utils/bigint-utils.js.map +1 -1
- package/esm/utils/buffer-utils.d.ts +7 -1
- package/esm/utils/buffer-utils.js +14 -1
- package/esm/utils/buffer-utils.js.map +1 -1
- package/esm/utils/crypto/abstract.d.ts +16 -9
- package/esm/utils/crypto/abstract.js +6 -9
- package/esm/utils/crypto/abstract.js.map +1 -1
- package/esm/utils/crypto/factorization.d.ts +2 -1
- package/esm/utils/crypto/factorization.js +29 -34
- package/esm/utils/crypto/factorization.js.map +1 -1
- package/esm/utils/crypto/index.d.ts +4 -0
- package/esm/utils/crypto/index.js +4 -0
- package/esm/utils/crypto/index.js.map +1 -1
- package/esm/utils/crypto/keys.d.ts +2 -2
- package/esm/utils/crypto/keys.js +4 -4
- package/esm/utils/crypto/keys.js.map +1 -1
- package/esm/utils/crypto/miller-rabin.d.ts +2 -2
- package/esm/utils/crypto/miller-rabin.js +21 -20
- package/esm/utils/crypto/miller-rabin.js.map +1 -1
- package/esm/utils/crypto/mtproto.d.ts +3 -3
- package/esm/utils/crypto/mtproto.js +12 -12
- package/esm/utils/crypto/mtproto.js.map +1 -1
- package/esm/utils/crypto/node.d.ts +21 -0
- package/esm/utils/crypto/node.js +69 -0
- package/esm/utils/crypto/node.js.map +1 -0
- package/esm/utils/crypto/password.js +24 -32
- package/esm/utils/crypto/password.js.map +1 -1
- package/esm/utils/crypto/wasm.d.ts +22 -0
- package/esm/utils/crypto/wasm.js +37 -0
- package/esm/utils/crypto/wasm.js.map +1 -0
- package/esm/utils/crypto/web.d.ts +11 -0
- package/esm/utils/crypto/web.js +36 -0
- package/esm/utils/crypto/web.js.map +1 -0
- package/esm/utils/deque.d.ts +1 -1
- package/esm/utils/deque.js +9 -3
- package/esm/utils/deque.js.map +1 -1
- package/esm/utils/function-utils.d.ts +4 -1
- package/esm/utils/function-utils.js +8 -1
- package/esm/utils/function-utils.js.map +1 -1
- package/esm/utils/index.d.ts +0 -1
- package/esm/utils/index.js +0 -1
- package/esm/utils/index.js.map +1 -1
- package/esm/utils/logger.d.ts +0 -1
- package/esm/utils/logger.js +3 -4
- package/esm/utils/logger.js.map +1 -1
- package/esm/utils/lru-map.d.ts +1 -0
- package/esm/utils/lru-map.js +6 -0
- package/esm/utils/lru-map.js.map +1 -1
- package/esm/utils/peer-utils.js +5 -1
- package/esm/utils/peer-utils.js.map +1 -1
- package/esm/utils/platform/crypto.js +1 -1
- package/esm/utils/platform/crypto.js.map +1 -1
- package/esm/utils/platform/crypto.web.js +2 -2
- package/esm/utils/platform/crypto.web.js.map +1 -1
- package/esm/utils/string-session.js +4 -2
- package/esm/utils/string-session.js.map +1 -1
- package/package.json +4 -5
- package/cjs/utils/crypto/common.d.ts +0 -12
- package/cjs/utils/crypto/common.js +0 -54
- package/cjs/utils/crypto/common.js.map +0 -1
- package/cjs/utils/crypto/node-crypto.d.ts +0 -10
- package/cjs/utils/crypto/node-crypto.js +0 -46
- package/cjs/utils/crypto/node-crypto.js.map +0 -1
- package/cjs/utils/crypto/subtle.d.ts +0 -12
- package/cjs/utils/crypto/subtle.js +0 -86
- package/cjs/utils/crypto/subtle.js.map +0 -1
- package/cjs/utils/flood-control.d.ts +0 -14
- package/cjs/utils/flood-control.js +0 -65
- package/cjs/utils/flood-control.js.map +0 -1
- package/cjs/utils/platform/random.d.ts +0 -1
- package/cjs/utils/platform/random.js +0 -7
- package/cjs/utils/platform/random.js.map +0 -1
- package/cjs/utils/platform/random.web.d.ts +0 -1
- package/cjs/utils/platform/random.web.js +0 -10
- package/cjs/utils/platform/random.web.js.map +0 -1
- package/esm/utils/crypto/common.d.ts +0 -12
- package/esm/utils/crypto/common.js +0 -50
- package/esm/utils/crypto/common.js.map +0 -1
- package/esm/utils/crypto/node-crypto.d.ts +0 -10
- package/esm/utils/crypto/node-crypto.js +0 -42
- package/esm/utils/crypto/node-crypto.js.map +0 -1
- package/esm/utils/crypto/subtle.d.ts +0 -12
- package/esm/utils/crypto/subtle.js +0 -59
- package/esm/utils/crypto/subtle.js.map +0 -1
- package/esm/utils/flood-control.d.ts +0 -14
- package/esm/utils/flood-control.js +0 -61
- package/esm/utils/flood-control.js.map +0 -1
- package/esm/utils/platform/random.d.ts +0 -1
- package/esm/utils/platform/random.js +0 -4
- package/esm/utils/platform/random.js.map +0 -1
- package/esm/utils/platform/random.web.d.ts +0 -1
- package/esm/utils/platform/random.web.js +0 -6
- package/esm/utils/platform/random.web.js.map +0 -1
|
@@ -1,59 +1,87 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { bufferToReversed } from './buffer-utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Get the minimum number of bits required to represent a number
|
|
4
|
+
*/
|
|
5
|
+
export function bigIntBitLength(n) {
|
|
6
|
+
// not the fastest way, but at least not .toString(2) and not too complex
|
|
7
|
+
// taken from: https://stackoverflow.com/a/76616288/22656950
|
|
8
|
+
const i = (n.toString(16).length - 1) * 4;
|
|
9
|
+
return i + 32 - Math.clz32(Number(n >> BigInt(i)));
|
|
10
|
+
}
|
|
3
11
|
/**
|
|
4
12
|
* Convert a big integer to a buffer
|
|
5
13
|
*
|
|
6
14
|
* @param value Value to convert
|
|
7
|
-
* @param length Length of the resulting buffer (by default it's
|
|
15
|
+
* @param length Length of the resulting buffer (by default it's the minimum required)
|
|
8
16
|
* @param le Whether to use little-endian encoding
|
|
9
17
|
*/
|
|
10
18
|
export function bigIntToBuffer(value, length = 0, le = false) {
|
|
11
|
-
const
|
|
12
|
-
|
|
19
|
+
const bits = bigIntBitLength(value);
|
|
20
|
+
const bytes = Math.ceil(bits / 8);
|
|
21
|
+
if (length !== 0 && bytes > length) {
|
|
13
22
|
throw new Error('Value out of bounds');
|
|
14
23
|
}
|
|
15
|
-
if (length
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
if (length === 0)
|
|
25
|
+
length = bytes;
|
|
26
|
+
const buf = new ArrayBuffer(length);
|
|
27
|
+
const u8 = new Uint8Array(buf);
|
|
28
|
+
const unaligned = length % 8;
|
|
29
|
+
const dv = new DataView(buf, 0, length - unaligned);
|
|
30
|
+
// it is faster to work with 64-bit words than with bytes directly
|
|
31
|
+
for (let i = 0; i < dv.byteLength; i += 8) {
|
|
32
|
+
dv.setBigUint64(i, value & 0xffffffffffffffffn, true);
|
|
33
|
+
value >>= 64n;
|
|
19
34
|
}
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
if (unaligned > 0) {
|
|
36
|
+
for (let i = length - unaligned; i < length; i++) {
|
|
37
|
+
u8[i] = Number(value & 0xffn);
|
|
38
|
+
value >>= 8n;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (!le)
|
|
42
|
+
u8.reverse();
|
|
43
|
+
return u8;
|
|
25
44
|
}
|
|
26
45
|
/**
|
|
27
46
|
* Convert a buffer to a big integer
|
|
28
47
|
*
|
|
29
48
|
* @param buffer Buffer to convert
|
|
30
|
-
* @param offset Offset to start reading from
|
|
31
|
-
* @param length Length to read
|
|
32
49
|
* @param le Whether to use little-endian encoding
|
|
33
50
|
*/
|
|
34
|
-
export function bufferToBigInt(buffer,
|
|
35
|
-
const arr = [...buffer.subarray(offset, offset + length)];
|
|
51
|
+
export function bufferToBigInt(buffer, le = false) {
|
|
36
52
|
if (le)
|
|
37
|
-
|
|
38
|
-
|
|
53
|
+
buffer = bufferToReversed(buffer);
|
|
54
|
+
const unaligned = buffer.length % 8;
|
|
55
|
+
const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength - unaligned);
|
|
56
|
+
let res = 0n;
|
|
57
|
+
// it is faster to work with 64-bit words than with bytes directly
|
|
58
|
+
for (let i = 0; i < dv.byteLength; i += 8) {
|
|
59
|
+
res = (res << 64n) | BigInt(dv.getBigUint64(i, false));
|
|
60
|
+
}
|
|
61
|
+
if (unaligned > 0) {
|
|
62
|
+
for (let i = buffer.length - unaligned; i < buffer.length; i++) {
|
|
63
|
+
res = (res << 8n) | BigInt(buffer[i]);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return res;
|
|
39
67
|
}
|
|
40
68
|
/**
|
|
41
|
-
* Generate a random big integer of the given size (in bytes)
|
|
69
|
+
* Generate a cryptographically safe random big integer of the given size (in bytes)
|
|
42
70
|
* @param size Size in bytes
|
|
43
71
|
*/
|
|
44
|
-
export function randomBigInt(size) {
|
|
45
|
-
return bufferToBigInt(randomBytes(size));
|
|
72
|
+
export function randomBigInt(crypto, size) {
|
|
73
|
+
return bufferToBigInt(crypto.randomBytes(size));
|
|
46
74
|
}
|
|
47
75
|
/**
|
|
48
76
|
* Generate a random big integer of the given size (in bits)
|
|
49
77
|
* @param bits
|
|
50
78
|
*/
|
|
51
|
-
export function randomBigIntBits(bits) {
|
|
52
|
-
let num = randomBigInt(Math.ceil(bits / 8));
|
|
53
|
-
const bitLength = num
|
|
54
|
-
if (bitLength
|
|
55
|
-
const toTrim =
|
|
56
|
-
num
|
|
79
|
+
export function randomBigIntBits(crypto, bits) {
|
|
80
|
+
let num = randomBigInt(crypto, Math.ceil(bits / 8));
|
|
81
|
+
const bitLength = bigIntBitLength(num);
|
|
82
|
+
if (bitLength > bits) {
|
|
83
|
+
const toTrim = bitLength - bits;
|
|
84
|
+
num >>= BigInt(toTrim);
|
|
57
85
|
}
|
|
58
86
|
return num;
|
|
59
87
|
}
|
|
@@ -63,30 +91,100 @@ export function randomBigIntBits(bits) {
|
|
|
63
91
|
* @param max Maximum value (exclusive)
|
|
64
92
|
* @param min Minimum value (inclusive)
|
|
65
93
|
*/
|
|
66
|
-
export function randomBigIntInRange(max, min =
|
|
67
|
-
const interval = max
|
|
68
|
-
if (interval
|
|
94
|
+
export function randomBigIntInRange(crypto, max, min = 1n) {
|
|
95
|
+
const interval = max - min;
|
|
96
|
+
if (interval < 0n)
|
|
69
97
|
throw new Error('expected min < max');
|
|
70
|
-
const byteSize = interval
|
|
71
|
-
let result = randomBigInt(byteSize);
|
|
72
|
-
while (result
|
|
73
|
-
result
|
|
74
|
-
return min
|
|
98
|
+
const byteSize = bigIntBitLength(interval) / 8;
|
|
99
|
+
let result = randomBigInt(crypto, byteSize);
|
|
100
|
+
while (result > interval)
|
|
101
|
+
result -= interval;
|
|
102
|
+
return min + result;
|
|
75
103
|
}
|
|
76
104
|
/**
|
|
77
105
|
* Compute the multiplicity of 2 in the prime factorization of n
|
|
78
106
|
* @param n
|
|
79
107
|
*/
|
|
80
108
|
export function twoMultiplicity(n) {
|
|
81
|
-
if (n ===
|
|
82
|
-
return
|
|
83
|
-
let m =
|
|
84
|
-
let pow =
|
|
109
|
+
if (n === 0n)
|
|
110
|
+
return 0n;
|
|
111
|
+
let m = 0n;
|
|
112
|
+
let pow = 1n;
|
|
85
113
|
while (true) {
|
|
86
|
-
if (
|
|
114
|
+
if ((n & pow) !== 0n)
|
|
87
115
|
return m;
|
|
88
|
-
m
|
|
89
|
-
pow
|
|
116
|
+
m += 1n;
|
|
117
|
+
pow <<= 1n;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
export function bigIntMin(a, b) {
|
|
121
|
+
return a < b ? a : b;
|
|
122
|
+
}
|
|
123
|
+
export function bigIntAbs(a) {
|
|
124
|
+
return a < 0n ? -a : a;
|
|
125
|
+
}
|
|
126
|
+
export function bigIntGcd(a, b) {
|
|
127
|
+
// using euclidean algorithm is fast enough on smaller numbers
|
|
128
|
+
// https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
|
|
129
|
+
while (b !== 0n) {
|
|
130
|
+
const t = b;
|
|
131
|
+
b = a % b;
|
|
132
|
+
a = t;
|
|
133
|
+
}
|
|
134
|
+
return a;
|
|
135
|
+
}
|
|
136
|
+
export function bigIntModPow(base, exp, mod) {
|
|
137
|
+
// using the binary method is good enough for our use case
|
|
138
|
+
// https://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method
|
|
139
|
+
base %= mod;
|
|
140
|
+
let result = 1n;
|
|
141
|
+
while (exp > 0n) {
|
|
142
|
+
if (exp % 2n === 1n) {
|
|
143
|
+
result = (result * base) % mod;
|
|
144
|
+
}
|
|
145
|
+
exp >>= 1n;
|
|
146
|
+
base = base ** 2n % mod;
|
|
147
|
+
}
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
// below code is based on https://github.com/juanelas/bigint-mod-arith, MIT license
|
|
151
|
+
function eGcd(a, b) {
|
|
152
|
+
let x = 0n;
|
|
153
|
+
let y = 1n;
|
|
154
|
+
let u = 1n;
|
|
155
|
+
let v = 0n;
|
|
156
|
+
while (a !== 0n) {
|
|
157
|
+
const q = b / a;
|
|
158
|
+
const r = b % a;
|
|
159
|
+
const m = x - u * q;
|
|
160
|
+
const n = y - v * q;
|
|
161
|
+
b = a;
|
|
162
|
+
a = r;
|
|
163
|
+
x = u;
|
|
164
|
+
y = v;
|
|
165
|
+
u = m;
|
|
166
|
+
v = n;
|
|
167
|
+
}
|
|
168
|
+
return [b, x, y];
|
|
169
|
+
}
|
|
170
|
+
function toZn(a, n) {
|
|
171
|
+
if (typeof a === 'number')
|
|
172
|
+
a = BigInt(a);
|
|
173
|
+
if (typeof n === 'number')
|
|
174
|
+
n = BigInt(n);
|
|
175
|
+
if (n <= 0n) {
|
|
176
|
+
throw new RangeError('n must be > 0');
|
|
177
|
+
}
|
|
178
|
+
const aZn = a % n;
|
|
179
|
+
return aZn < 0n ? aZn + n : aZn;
|
|
180
|
+
}
|
|
181
|
+
export function bigIntModInv(a, n) {
|
|
182
|
+
const [g, x] = eGcd(toZn(a, n), n);
|
|
183
|
+
if (g !== 1n) {
|
|
184
|
+
throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`); // modular inverse does not exist
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
return toZn(x, n);
|
|
90
188
|
}
|
|
91
189
|
}
|
|
92
190
|
//# sourceMappingURL=bigint-utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bigint-utils.js","sourceRoot":"","sources":["../../../src/utils/bigint-utils.ts"],"names":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AAI,CAAA;IAEzC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAA;IACnB,IAAI,GAAG,GAAG,MAAM,CAAC,GAAG,CAAA;IAEpB,OAAO,IAAI,EAAE;QACT,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE;YAAE,OAAO,CAAC,CAAA;QAClC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACtB,GAAG,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;KACzB;AACL,CAAC","sourcesContent":["import bigInt, { BigInteger } from 'big-integer'\n\nimport { randomBytes } from './buffer-utils.js'\n\n/**\n * Convert a big integer to a buffer\n *\n * @param value Value to convert\n * @param length Length of the resulting buffer (by default it's computed automatically)\n * @param le Whether to use little-endian encoding\n */\nexport function bigIntToBuffer(value: BigInteger, length = 0, le = false): Uint8Array {\n const array = value.toArray(256).value\n\n if (length !== 0 && array.length > length) {\n throw new Error('Value out of bounds')\n }\n\n if (length !== 0) {\n // padding\n while (array.length !== length) array.unshift(0)\n }\n\n if (le) array.reverse()\n\n const buffer = new Uint8Array(length || array.length)\n buffer.set(array, 0)\n\n return buffer\n}\n\n/**\n * Convert a buffer to a big integer\n *\n * @param buffer Buffer to convert\n * @param offset Offset to start reading from\n * @param length Length to read\n * @param le Whether to use little-endian encoding\n */\nexport function bufferToBigInt(buffer: Uint8Array, offset = 0, length = buffer.length, le = false): BigInteger {\n const arr = [...buffer.subarray(offset, offset + length)]\n\n if (le) arr.reverse()\n\n return bigInt.fromArray(arr as unknown as number[], 256)\n}\n\n/**\n * Generate a random big integer of the given size (in bytes)\n * @param size Size in bytes\n */\nexport function randomBigInt(size: number): BigInteger {\n return bufferToBigInt(randomBytes(size))\n}\n\n/**\n * Generate a random big integer of the given size (in bits)\n * @param bits\n */\nexport function randomBigIntBits(bits: number): BigInteger {\n let num = randomBigInt(Math.ceil(bits / 8))\n\n const bitLength = num.bitLength()\n\n if (bitLength.gt(bits)) {\n const toTrim = bigInt.randBetween(bitLength.minus(bits), 8)\n num = num.shiftRight(toTrim)\n }\n\n return num\n}\n\n/**\n * Generate a random big integer in the range [min, max)\n *\n * @param max Maximum value (exclusive)\n * @param min Minimum value (inclusive)\n */\nexport function randomBigIntInRange(max: BigInteger, min = bigInt.one): BigInteger {\n const interval = max.minus(min)\n if (interval.isNegative()) throw new Error('expected min < max')\n\n const byteSize = interval.bitLength().divide(8).toJSNumber()\n\n let result = randomBigInt(byteSize)\n while (result.gt(interval)) result = result.minus(interval)\n\n return min.plus(result)\n}\n\n/**\n * Compute the multiplicity of 2 in the prime factorization of n\n * @param n\n */\nexport function twoMultiplicity(n: BigInteger): BigInteger {\n if (n === bigInt.zero) return bigInt.zero\n\n let m = bigInt.zero\n let pow = bigInt.one\n\n while (true) {\n if (!n.and(pow).isZero()) return m\n m = m.plus(bigInt.one)\n pow = pow.shiftLeft(1)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"bigint-utils.js","sourceRoot":"","sources":["../../../src/utils/bigint-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAGpD;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,CAAS;IACrC,yEAAyE;IACzE,4DAA4D;IAE5D,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;IAEzC,OAAO,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AACtD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE,GAAG,KAAK;IAChE,MAAM,IAAI,GAAG,eAAe,CAAC,KAAK,C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G,CAAC,CAAA;QACL,CAAC,GAAG,CAAC,CAAA;QACL,CAAC,GAAG,CAAC,CAAA;QACL,CAAC,GAAG,CAAC,CAAA;KACR;IAED,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;AACpB,CAAC;AAED,SAAS,IAAI,CAAC,CAAkB,EAAE,CAAkB;IAChD,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAA;IACxC,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAA;IAExC,IAAI,CAAC,IAAI,EAAE,EAAE;QACT,MAAM,IAAI,UAAU,CAAC,eAAe,CAAC,CAAA;KACxC;IAED,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,CAAA;IAEjB,OAAO,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAA;AACnC,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,CAAS,EAAE,CAAS;IAC7C,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAElC,IAAI,CAAC,KAAK,EAAE,EAAE;QACV,MAAM,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,iCAAiC,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA,CAAC,iCAAiC;KACzH;SAAM;QACH,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;KACpB;AACL,CAAC","sourcesContent":["import { bufferToReversed } from './buffer-utils.js'\nimport { ICryptoProvider } from './crypto/abstract.js'\n\n/**\n * Get the minimum number of bits required to represent a number\n */\nexport function bigIntBitLength(n: bigint) {\n // not the fastest way, but at least not .toString(2) and not too complex\n // taken from: https://stackoverflow.com/a/76616288/22656950\n\n const i = (n.toString(16).length - 1) * 4\n\n return i + 32 - Math.clz32(Number(n >> BigInt(i)))\n}\n\n/**\n * Convert a big integer to a buffer\n *\n * @param value Value to convert\n * @param length Length of the resulting buffer (by default it's the minimum required)\n * @param le Whether to use little-endian encoding\n */\nexport function bigIntToBuffer(value: bigint, length = 0, le = false): Uint8Array {\n const bits = bigIntBitLength(value)\n const bytes = Math.ceil(bits / 8)\n\n if (length !== 0 && bytes > length) {\n throw new Error('Value out of bounds')\n }\n\n if (length === 0) length = bytes\n\n const buf = new ArrayBuffer(length)\n const u8 = new Uint8Array(buf)\n\n const unaligned = length % 8\n const dv = new DataView(buf, 0, length - unaligned)\n\n // it is faster to work with 64-bit words than with bytes directly\n for (let i = 0; i < dv.byteLength; i += 8) {\n dv.setBigUint64(i, value & 0xffffffffffffffffn, true)\n value >>= 64n\n }\n\n if (unaligned > 0) {\n for (let i = length - unaligned; i < length; i++) {\n u8[i] = Number(value & 0xffn)\n value >>= 8n\n }\n }\n\n if (!le) u8.reverse()\n\n return u8\n}\n\n/**\n * Convert a buffer to a big integer\n *\n * @param buffer Buffer to convert\n * @param le Whether to use little-endian encoding\n */\nexport function bufferToBigInt(buffer: Uint8Array, le = false): bigint {\n if (le) buffer = bufferToReversed(buffer)\n\n const unaligned = buffer.length % 8\n const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength - unaligned)\n\n let res = 0n\n\n // it is faster to work with 64-bit words than with bytes directly\n for (let i = 0; i < dv.byteLength; i += 8) {\n res = (res << 64n) | BigInt(dv.getBigUint64(i, false))\n }\n\n if (unaligned > 0) {\n for (let i = buffer.length - unaligned; i < buffer.length; i++) {\n res = (res << 8n) | BigInt(buffer[i])\n }\n }\n\n return res\n}\n\n/**\n * Generate a cryptographically safe random big integer of the given size (in bytes)\n * @param size Size in bytes\n */\nexport function randomBigInt(crypto: ICryptoProvider, size: number): bigint {\n return bufferToBigInt(crypto.randomBytes(size))\n}\n\n/**\n * Generate a random big integer of the given size (in bits)\n * @param bits\n */\nexport function randomBigIntBits(crypto: ICryptoProvider, bits: number): bigint {\n let num = randomBigInt(crypto, Math.ceil(bits / 8))\n\n const bitLength = bigIntBitLength(num)\n\n if (bitLength > bits) {\n const toTrim = bitLength - bits\n num >>= BigInt(toTrim)\n }\n\n return num\n}\n\n/**\n * Generate a random big integer in the range [min, max)\n *\n * @param max Maximum value (exclusive)\n * @param min Minimum value (inclusive)\n */\nexport function randomBigIntInRange(crypto: ICryptoProvider, max: bigint, min = 1n): bigint {\n const interval = max - min\n if (interval < 0n) throw new Error('expected min < max')\n\n const byteSize = bigIntBitLength(interval) / 8\n\n let result = randomBigInt(crypto, byteSize)\n while (result > interval) result -= interval\n\n return min + result\n}\n\n/**\n * Compute the multiplicity of 2 in the prime factorization of n\n * @param n\n */\nexport function twoMultiplicity(n: bigint): bigint {\n if (n === 0n) return 0n\n\n let m = 0n\n let pow = 1n\n\n while (true) {\n if ((n & pow) !== 0n) return m\n m += 1n\n pow <<= 1n\n }\n}\n\nexport function bigIntMin(a: bigint, b: bigint): bigint {\n return a < b ? a : b\n}\n\nexport function bigIntAbs(a: bigint): bigint {\n return a < 0n ? -a : a\n}\n\nexport function bigIntGcd(a: bigint, b: bigint): bigint {\n // using euclidean algorithm is fast enough on smaller numbers\n // https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations\n\n while (b !== 0n) {\n const t = b\n b = a % b\n a = t\n }\n\n return a\n}\n\nexport function bigIntModPow(base: bigint, exp: bigint, mod: bigint): bigint {\n // using the binary method is good enough for our use case\n // https://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method\n\n base %= mod\n\n let result = 1n\n\n while (exp > 0n) {\n if (exp % 2n === 1n) {\n result = (result * base) % mod\n }\n\n exp >>= 1n\n base = base ** 2n % mod\n }\n\n return result\n}\n\n// below code is based on https://github.com/juanelas/bigint-mod-arith, MIT license\n\nfunction eGcd(a: bigint, b: bigint): [bigint, bigint, bigint] {\n let x = 0n\n let y = 1n\n let u = 1n\n let v = 0n\n\n while (a !== 0n) {\n const q = b / a\n const r: bigint = b % a\n const m = x - u * q\n const n = y - v * q\n b = a\n a = r\n x = u\n y = v\n u = m\n v = n\n }\n\n return [b, x, y]\n}\n\nfunction toZn(a: number | bigint, n: number | bigint): bigint {\n if (typeof a === 'number') a = BigInt(a)\n if (typeof n === 'number') n = BigInt(n)\n\n if (n <= 0n) {\n throw new RangeError('n must be > 0')\n }\n\n const aZn = a % n\n\n return aZn < 0n ? aZn + n : aZn\n}\n\nexport function bigIntModInv(a: bigint, n: bigint): bigint {\n const [g, x] = eGcd(toZn(a, n), n)\n\n if (g !== 1n) {\n throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`) // modular inverse does not exist\n } else {\n return toZn(x, n)\n }\n}\n"]}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export { _randomBytes as randomBytes } from './platform/random.js';
|
|
2
1
|
/**
|
|
3
2
|
* Check if two buffers are equal
|
|
4
3
|
*
|
|
@@ -18,4 +17,11 @@ export declare function cloneBuffer(buf: Uint8Array, start?: number, end?: numbe
|
|
|
18
17
|
* Concatenate multiple buffers into one
|
|
19
18
|
*/
|
|
20
19
|
export declare function concatBuffers(buffers: Uint8Array[]): Uint8Array;
|
|
20
|
+
/**
|
|
21
|
+
* Shortcut for creating a DataView from a Uint8Array
|
|
22
|
+
*/
|
|
21
23
|
export declare function dataViewFromBuffer(buf: Uint8Array): DataView;
|
|
24
|
+
/**
|
|
25
|
+
* Reverse a buffer (or a part of it) into a new buffer
|
|
26
|
+
*/
|
|
27
|
+
export declare function bufferToReversed(buf: Uint8Array, start?: number, end?: number): Uint8Array;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export { _randomBytes as randomBytes } from './platform/random.js';
|
|
2
1
|
/**
|
|
3
2
|
* Check if two buffers are equal
|
|
4
3
|
*
|
|
@@ -49,7 +48,21 @@ export function concatBuffers(buffers) {
|
|
|
49
48
|
}
|
|
50
49
|
return ret;
|
|
51
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Shortcut for creating a DataView from a Uint8Array
|
|
53
|
+
*/
|
|
52
54
|
export function dataViewFromBuffer(buf) {
|
|
53
55
|
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
54
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Reverse a buffer (or a part of it) into a new buffer
|
|
59
|
+
*/
|
|
60
|
+
export function bufferToReversed(buf, start = 0, end = buf.length) {
|
|
61
|
+
const len = end - start;
|
|
62
|
+
const ret = new Uint8Array(len);
|
|
63
|
+
for (let i = 0; i < len; i++) {
|
|
64
|
+
ret[i] = buf[end - i - 1];
|
|
65
|
+
}
|
|
66
|
+
return ret;
|
|
67
|
+
}
|
|
55
68
|
//# sourceMappingURL=buffer-utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buffer-utils.js","sourceRoot":"","sources":["../../../src/utils/buffer-utils.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"buffer-utils.js","sourceRoot":"","sources":["../../../src/utils/buffer-utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,CAAa,EAAE,CAAa;IACrD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAA;IAEvC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC/B,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAA;KAClC;IAED,OAAO,IAAI,CAAA;AACf,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,GAAe,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC,MAAM;IACpE,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,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I,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;QAC1B,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;KAC5B;IAED,OAAO,GAAG,CAAA;AACd,CAAC","sourcesContent":["/**\n * Check if two buffers are equal\n *\n * @param a First buffer\n * @param b Second buffer\n */\nexport function buffersEqual(a: Uint8Array, b: Uint8Array): boolean {\n if (a.length !== b.length) return false\n\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false\n }\n\n return true\n}\n\n/**\n * Copy a buffer\n *\n * @param buf Buffer to copy\n * @param start Start offset\n * @param end End offset\n */\nexport function cloneBuffer(buf: Uint8Array, start = 0, end = buf.length): Uint8Array {\n const ret = new Uint8Array(end - start)\n ret.set(buf.subarray(start, end))\n\n return ret\n}\n\n/**\n * Concatenate multiple buffers into one\n */\nexport function concatBuffers(buffers: Uint8Array[]): Uint8Array {\n if (buffers.length === 1) return buffers[0]\n\n /* eslint-disable no-restricted-globals */\n if (typeof Buffer !== 'undefined') {\n return Buffer.concat(buffers)\n }\n /* eslint-enable no-restricted-globals */\n\n let length = 0\n\n for (const buf of buffers) {\n length += buf.length\n }\n\n const ret = new Uint8Array(length)\n let offset = 0\n\n for (const buf of buffers) {\n ret.set(buf, offset)\n offset += buf.length\n }\n\n return ret\n}\n\n/**\n * Shortcut for creating a DataView from a Uint8Array\n */\nexport function dataViewFromBuffer(buf: Uint8Array): DataView {\n return new DataView(buf.buffer, buf.byteOffset, buf.byteLength)\n}\n\n/**\n * Reverse a buffer (or a part of it) into a new buffer\n */\nexport function bufferToReversed(buf: Uint8Array, start = 0, end = buf.length): Uint8Array {\n const len = end - start\n const ret = new Uint8Array(len)\n\n for (let i = 0; i < len; i++) {\n ret[i] = buf[end - i - 1]\n }\n\n return ret\n}\n"]}
|
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
import { MaybeAsync } from '../../types/index.js';
|
|
2
2
|
export interface IEncryptionScheme {
|
|
3
|
-
encrypt(data: Uint8Array):
|
|
4
|
-
decrypt(data: Uint8Array):
|
|
3
|
+
encrypt(data: Uint8Array): Uint8Array;
|
|
4
|
+
decrypt(data: Uint8Array): Uint8Array;
|
|
5
|
+
}
|
|
6
|
+
export interface IAesCtr {
|
|
7
|
+
process(data: Uint8Array): Uint8Array;
|
|
8
|
+
close?(): void;
|
|
5
9
|
}
|
|
6
10
|
export interface ICryptoProvider {
|
|
7
11
|
initialize?(): MaybeAsync<void>;
|
|
8
|
-
sha1(data: Uint8Array):
|
|
9
|
-
sha256(data: Uint8Array):
|
|
12
|
+
sha1(data: Uint8Array): Uint8Array;
|
|
13
|
+
sha256(data: Uint8Array): Uint8Array;
|
|
10
14
|
pbkdf2(password: Uint8Array, salt: Uint8Array, iterations: number, keylen?: number, // = 64
|
|
11
15
|
algo?: string): MaybeAsync<Uint8Array>;
|
|
12
16
|
hmacSha256(data: Uint8Array, key: Uint8Array): MaybeAsync<Uint8Array>;
|
|
13
|
-
createAesCtr(key: Uint8Array, iv: Uint8Array, encrypt: boolean):
|
|
14
|
-
createAesIge(key: Uint8Array, iv: Uint8Array):
|
|
15
|
-
createAesEcb(key: Uint8Array): MaybeAsync<IEncryptionScheme>;
|
|
17
|
+
createAesCtr(key: Uint8Array, iv: Uint8Array, encrypt: boolean): IAesCtr;
|
|
18
|
+
createAesIge(key: Uint8Array, iv: Uint8Array): IEncryptionScheme;
|
|
16
19
|
factorizePQ(pq: Uint8Array): MaybeAsync<[Uint8Array, Uint8Array]>;
|
|
20
|
+
gzip(data: Uint8Array, maxSize: number): Uint8Array | null;
|
|
21
|
+
gunzip(data: Uint8Array): Uint8Array;
|
|
22
|
+
randomFill(buf: Uint8Array): void;
|
|
23
|
+
randomBytes(size: number): Uint8Array;
|
|
17
24
|
}
|
|
18
25
|
export declare abstract class BaseCryptoProvider {
|
|
19
|
-
|
|
26
|
+
abstract randomFill(buf: Uint8Array): void;
|
|
20
27
|
factorizePQ(pq: Uint8Array): [Uint8Array, Uint8Array];
|
|
21
|
-
|
|
28
|
+
randomBytes(size: number): Uint8Array;
|
|
22
29
|
}
|
|
23
30
|
export type CryptoProviderFactory = () => ICryptoProvider;
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
import { AesModeOfOperationIge } from './common.js';
|
|
2
1
|
import { factorizePQSync } from './factorization.js';
|
|
3
2
|
export class BaseCryptoProvider {
|
|
4
|
-
createAesIge(key, iv) {
|
|
5
|
-
const ecb = this.createAesEcb(key);
|
|
6
|
-
if ('then' in ecb) {
|
|
7
|
-
return ecb.then((ecb) => new AesModeOfOperationIge(key, iv, ecb));
|
|
8
|
-
}
|
|
9
|
-
return new AesModeOfOperationIge(key, iv, ecb);
|
|
10
|
-
}
|
|
11
3
|
factorizePQ(pq) {
|
|
12
|
-
return factorizePQSync(pq);
|
|
4
|
+
return factorizePQSync(this, pq);
|
|
5
|
+
}
|
|
6
|
+
randomBytes(size) {
|
|
7
|
+
const buf = new Uint8Array(size);
|
|
8
|
+
this.randomFill(buf);
|
|
9
|
+
return buf;
|
|
13
10
|
}
|
|
14
11
|
}
|
|
15
12
|
//# sourceMappingURL=abstract.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abstract.js","sourceRoot":"","sources":["../../../../src/utils/crypto/abstract.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"abstract.js","sourceRoot":"","sources":["../../../../src/utils/crypto/abstract.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AA0CpD,MAAM,OAAgB,kBAAkB;IAGpC,WAAW,CAAC,EAAc;QACtB,OAAO,eAAe,CAAC,IAAkC,EAAE,EAAE,CAAC,CAAA;IAClE,CAAC;IAED,WAAW,CAAC,IAAY;QACpB,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,CAAA;QAChC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;QAEpB,OAAO,GAAG,CAAA;IACd,CAAC;CACJ","sourcesContent":["import { MaybeAsync } from '../../types/index.js'\nimport { factorizePQSync } from './factorization.js'\n\nexport interface IEncryptionScheme {\n encrypt(data: Uint8Array): Uint8Array\n decrypt(data: Uint8Array): Uint8Array\n}\n\nexport interface IAesCtr {\n process(data: Uint8Array): Uint8Array\n close?(): void\n}\n\nexport interface ICryptoProvider {\n initialize?(): MaybeAsync<void>\n\n sha1(data: Uint8Array): Uint8Array\n\n sha256(data: Uint8Array): Uint8Array\n\n pbkdf2(\n password: Uint8Array,\n salt: Uint8Array,\n iterations: number,\n keylen?: number, // = 64\n algo?: string, // sha1 or sha512 (default sha512)\n ): MaybeAsync<Uint8Array>\n\n hmacSha256(data: Uint8Array, key: Uint8Array): MaybeAsync<Uint8Array>\n\n createAesCtr(key: Uint8Array, iv: Uint8Array, encrypt: boolean): IAesCtr\n\n createAesIge(key: Uint8Array, iv: Uint8Array): IEncryptionScheme\n\n factorizePQ(pq: Uint8Array): MaybeAsync<[Uint8Array, Uint8Array]>\n\n gzip(data: Uint8Array, maxSize: number): Uint8Array | null\n gunzip(data: Uint8Array): Uint8Array\n\n randomFill(buf: Uint8Array): void\n randomBytes(size: number): Uint8Array\n}\n\nexport abstract class BaseCryptoProvider {\n abstract randomFill(buf: Uint8Array): void\n\n factorizePQ(pq: Uint8Array) {\n return factorizePQSync(this as unknown as ICryptoProvider, pq)\n }\n\n randomBytes(size: number) {\n const buf = new Uint8Array(size)\n this.randomFill(buf)\n\n return buf\n }\n}\n\nexport type CryptoProviderFactory = () => ICryptoProvider\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { ICryptoProvider } from './abstract.js';
|
|
1
2
|
/**
|
|
2
3
|
* Factorize `p*q` to `p` and `q` synchronously using Brent-Pollard rho algorithm
|
|
3
4
|
* @param pq
|
|
4
5
|
*/
|
|
5
|
-
export declare function factorizePQSync(pq: Uint8Array): [Uint8Array, Uint8Array];
|
|
6
|
+
export declare function factorizePQSync(crypto: ICryptoProvider, pq: Uint8Array): [Uint8Array, Uint8Array];
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { bigIntToBuffer, bufferToBigInt, randomBigIntInRange } from '../bigint-utils.js';
|
|
1
|
+
import { bigIntAbs, bigIntGcd, bigIntMin, bigIntToBuffer, bufferToBigInt, randomBigIntInRange, } from '../bigint-utils.js';
|
|
3
2
|
/**
|
|
4
3
|
* Factorize `p*q` to `p` and `q` synchronously using Brent-Pollard rho algorithm
|
|
5
4
|
* @param pq
|
|
6
5
|
*/
|
|
7
|
-
export function factorizePQSync(pq) {
|
|
6
|
+
export function factorizePQSync(crypto, pq) {
|
|
8
7
|
const pq_ = bufferToBigInt(pq);
|
|
9
|
-
const n = PollardRhoBrent(pq_);
|
|
10
|
-
const m = pq_
|
|
8
|
+
const n = PollardRhoBrent(crypto, pq_);
|
|
9
|
+
const m = pq_ / n;
|
|
11
10
|
let p;
|
|
12
11
|
let q;
|
|
13
|
-
if (n
|
|
12
|
+
if (n < m) {
|
|
14
13
|
p = n;
|
|
15
14
|
q = m;
|
|
16
15
|
}
|
|
@@ -20,42 +19,38 @@ export function factorizePQSync(pq) {
|
|
|
20
19
|
}
|
|
21
20
|
return [bigIntToBuffer(p), bigIntToBuffer(q)];
|
|
22
21
|
}
|
|
23
|
-
function PollardRhoBrent(n) {
|
|
24
|
-
if (n
|
|
25
|
-
return
|
|
26
|
-
let y = randomBigIntInRange(n
|
|
27
|
-
const c = randomBigIntInRange(n
|
|
28
|
-
const m = randomBigIntInRange(n
|
|
29
|
-
let g =
|
|
30
|
-
let r =
|
|
31
|
-
let q =
|
|
22
|
+
function PollardRhoBrent(crypto, n) {
|
|
23
|
+
if (n % 2n === 0n)
|
|
24
|
+
return 2n;
|
|
25
|
+
let y = randomBigIntInRange(crypto, n - 1n);
|
|
26
|
+
const c = randomBigIntInRange(crypto, n - 1n);
|
|
27
|
+
const m = randomBigIntInRange(crypto, n - 1n);
|
|
28
|
+
let g = 1n;
|
|
29
|
+
let r = 1n;
|
|
30
|
+
let q = 1n;
|
|
32
31
|
let ys;
|
|
33
32
|
let x;
|
|
34
|
-
while (g
|
|
33
|
+
while (g === 1n) {
|
|
35
34
|
x = y;
|
|
36
|
-
for (let i = 0; r
|
|
37
|
-
y =
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
while (k.lt(r) && g.eq(1)) {
|
|
35
|
+
for (let i = 0; r >= i; i++)
|
|
36
|
+
y = (((y * y) % n) + c) % n;
|
|
37
|
+
let k = 0n;
|
|
38
|
+
while (k < r && g === 1n) {
|
|
41
39
|
ys = y;
|
|
42
|
-
for (let i =
|
|
43
|
-
y =
|
|
44
|
-
q = q
|
|
45
|
-
// y = (y * y % n + c) % n
|
|
46
|
-
// q = q * abs(x - y) % n
|
|
40
|
+
for (let i = 0n; i < bigIntMin(m, r - k); i++) {
|
|
41
|
+
y = (((y * y) % n) + c) % n;
|
|
42
|
+
q = (q * bigIntAbs(x - y)) % n;
|
|
47
43
|
}
|
|
48
|
-
g =
|
|
49
|
-
k = k
|
|
44
|
+
g = bigIntGcd(q, n);
|
|
45
|
+
k = k + m;
|
|
50
46
|
}
|
|
51
|
-
r
|
|
47
|
+
r <<= 1n;
|
|
52
48
|
}
|
|
53
|
-
if (g
|
|
49
|
+
if (g === n) {
|
|
54
50
|
do {
|
|
55
|
-
ys =
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
} while (g.leq(bigInt.one));
|
|
51
|
+
ys = (((ys * ys) % n) + c) % n;
|
|
52
|
+
g = bigIntGcd(x - ys, n);
|
|
53
|
+
} while (g <= 1n);
|
|
59
54
|
}
|
|
60
55
|
return g;
|
|
61
56
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factorization.js","sourceRoot":"","sources":["../../../../src/utils/crypto/factorization.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
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igIntToBuffer,\n bufferToBigInt,\n randomBigIntInRange,\n} from '../bigint-utils.js'\nimport { ICryptoProvider } from './abstract.js'\n\n/**\n * Factorize `p*q` to `p` and `q` synchronously using Brent-Pollard rho algorithm\n * @param pq\n */\nexport function factorizePQSync(crypto: ICryptoProvider, pq: Uint8Array): [Uint8Array, Uint8Array] {\n const pq_ = bufferToBigInt(pq)\n\n const n = PollardRhoBrent(crypto, pq_)\n const m = pq_ / n\n\n let p\n let q\n\n if (n < m) {\n p = n\n q = m\n } else {\n p = m\n q = n\n }\n\n return [bigIntToBuffer(p), bigIntToBuffer(q)]\n}\n\nfunction PollardRhoBrent(crypto: ICryptoProvider, n: bigint): bigint {\n if (n % 2n === 0n) return 2n\n\n let y = randomBigIntInRange(crypto, n - 1n)\n const c = randomBigIntInRange(crypto, n - 1n)\n const m = randomBigIntInRange(crypto, n - 1n)\n let g = 1n\n let r = 1n\n let q = 1n\n\n let ys: bigint\n let x: bigint\n\n while (g === 1n) {\n x = y\n for (let i = 0; r >= i; i++) y = (((y * y) % n) + c) % n\n\n let k = 0n\n\n while (k < r && g === 1n) {\n ys = y\n\n for (let i = 0n; i < bigIntMin(m, r - k); i++) {\n y = (((y * y) % n) + c) % n\n q = (q * bigIntAbs(x - y)) % n\n }\n\n g = bigIntGcd(q, n)\n k = k + m\n }\n\n r <<= 1n\n }\n\n if (g === n) {\n do {\n ys = (((ys! * ys!) % n) + c) % n\n\n g = bigIntGcd(x! - ys!, n)\n } while (g <= 1n)\n }\n\n return g\n}\n"]}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export * from './abstract.js';
|
|
2
|
+
export * from './factorization.js';
|
|
2
3
|
export * from './keys.js';
|
|
4
|
+
export * from './miller-rabin.js';
|
|
5
|
+
export * from './mtproto.js';
|
|
3
6
|
export * from './password.js';
|
|
7
|
+
export * from './utils.js';
|
|
4
8
|
import { CryptoProviderFactory } from './abstract.js';
|
|
5
9
|
export declare const defaultCryptoProviderFactory: CryptoProviderFactory;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
export * from './abstract.js';
|
|
2
|
+
export * from './factorization.js';
|
|
2
3
|
export * from './keys.js';
|
|
4
|
+
export * from './miller-rabin.js';
|
|
5
|
+
export * from './mtproto.js';
|
|
3
6
|
export * from './password.js';
|
|
7
|
+
export * from './utils.js';
|
|
4
8
|
import { _defaultCryptoProviderFactory } from '../platform/crypto.js';
|
|
5
9
|
export const defaultCryptoProviderFactory = _defaultCryptoProviderFactory;
|
|
6
10
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/crypto/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/crypto/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,oBAAoB,CAAA;AAClC,cAAc,WAAW,CAAA;AACzB,cAAc,mBAAmB,CAAA;AACjC,cAAc,cAAc,CAAA;AAC5B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAE1B,OAAO,EAAE,6BAA6B,EAAE,MAAM,uBAAuB,CAAA;AAGrE,MAAM,CAAC,MAAM,4BAA4B,GAA0B,6BAA6B,CAAA","sourcesContent":["export * from './abstract.js'\nexport * from './factorization.js'\nexport * from './keys.js'\nexport * from './miller-rabin.js'\nexport * from './mtproto.js'\nexport * from './password.js'\nexport * from './utils.js'\n\nimport { _defaultCryptoProviderFactory } from '../platform/crypto.js'\nimport { CryptoProviderFactory } from './abstract.js'\n\nexport const defaultCryptoProviderFactory: CryptoProviderFactory = _defaultCryptoProviderFactory\n"]}
|
|
@@ -9,7 +9,7 @@ import { ICryptoProvider } from './abstract.js';
|
|
|
9
9
|
* @param key PEM-encoded RSA public key
|
|
10
10
|
* @param old Whether this is an "old" key
|
|
11
11
|
*/
|
|
12
|
-
export declare function parsePublicKey(crypto: ICryptoProvider, key: string, old?: boolean):
|
|
12
|
+
export declare function parsePublicKey(crypto: ICryptoProvider, key: string, old?: boolean): TlPublicKey;
|
|
13
13
|
/**
|
|
14
14
|
* Add public key to the global index.
|
|
15
15
|
*
|
|
@@ -17,7 +17,7 @@ export declare function parsePublicKey(crypto: ICryptoProvider, key: string, old
|
|
|
17
17
|
* @param key PEM-encoded RSA public key
|
|
18
18
|
* @param old Whether this is an "old" key
|
|
19
19
|
*/
|
|
20
|
-
export declare function addPublicKey(crypto: ICryptoProvider, key: string, old?: boolean):
|
|
20
|
+
export declare function addPublicKey(crypto: ICryptoProvider, key: string, old?: boolean): void;
|
|
21
21
|
/**
|
|
22
22
|
* Get public key by its fingerprint.
|
|
23
23
|
*
|
package/esm/utils/crypto/keys.js
CHANGED
|
@@ -9,7 +9,7 @@ import { parseAsn1, parsePemContents } from '../binary/asn1-parser.js';
|
|
|
9
9
|
* @param key PEM-encoded RSA public key
|
|
10
10
|
* @param old Whether this is an "old" key
|
|
11
11
|
*/
|
|
12
|
-
export
|
|
12
|
+
export function parsePublicKey(crypto, key, old = false) {
|
|
13
13
|
const asn1 = parseAsn1(parsePemContents(key));
|
|
14
14
|
const modulus = asn1.children?.[0].value;
|
|
15
15
|
const exponent = asn1.children?.[1].value;
|
|
@@ -20,7 +20,7 @@ export async function parsePublicKey(crypto, key, old = false) {
|
|
|
20
20
|
writer.bytes(modulus);
|
|
21
21
|
writer.bytes(exponent);
|
|
22
22
|
const data = writer.result();
|
|
23
|
-
const sha =
|
|
23
|
+
const sha = crypto.sha1(data);
|
|
24
24
|
const fp = hexEncode(sha.slice(-8).reverse());
|
|
25
25
|
return {
|
|
26
26
|
modulus: hexEncode(modulus),
|
|
@@ -36,8 +36,8 @@ export async function parsePublicKey(crypto, key, old = false) {
|
|
|
36
36
|
* @param key PEM-encoded RSA public key
|
|
37
37
|
* @param old Whether this is an "old" key
|
|
38
38
|
*/
|
|
39
|
-
export
|
|
40
|
-
const parsed =
|
|
39
|
+
export function addPublicKey(crypto, key, old = false) {
|
|
40
|
+
const parsed = parsePublicKey(crypto, key, old);
|
|
41
41
|
keysIndex[parsed.fingerprint] = parsed;
|
|
42
42
|
}
|
|
43
43
|
/**
|