@mtkruto/node 0.0.61 → 0.0.63
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/esm/deps/deno.land/x/{tgcrypto@0.1.1 → tgcrypto@0.1.3}/tgcrypto.js +7 -56
- package/esm/deps.js +1 -1
- package/esm/mod.js +10 -2
- package/esm/transport/transport_abridged.js +69 -0
- package/esm/transport/transport_provider.js +1 -1
- package/esm/utilities/0_bigint.js +3 -0
- package/esm/utilities/1_password.js +140 -0
- package/package.json +1 -1
- package/script/deps/deno.land/x/{tgcrypto@0.1.1 → tgcrypto@0.1.3}/tgcrypto.js +7 -79
- package/script/deps.js +1 -1
- package/script/mod.js +14 -5
- package/script/transport/transport_abridged.js +73 -0
- package/script/transport/transport_provider.js +1 -1
- package/script/utilities/0_bigint.js +5 -1
- package/script/utilities/1_password.js +174 -0
- package/types/deps/deno.land/x/tgcrypto@0.1.3/tgcrypto.d.ts +2 -0
- package/types/deps.d.ts +1 -1
- package/types/mod.d.ts +13 -2
- package/types/transport/transport_abridged.d.ts +11 -0
- package/types/utilities/0_bigint.d.ts +1 -0
- package/types/utilities/1_password.d.ts +11 -0
- package/types/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.d.ts +0 -2
- /package/esm/deps/deno.land/x/{tgcrypto@0.1.1 → tgcrypto@0.1.3}/mod.js +0 -0
- /package/script/deps/deno.land/x/{tgcrypto@0.1.1 → tgcrypto@0.1.3}/mod.js +0 -0
- /package/types/deps/deno.land/x/{tgcrypto@0.1.1 → tgcrypto@0.1.3}/mod.d.ts +0 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.checkPassword = exports.pad = exports.isGoodModExpFirst = exports.ph2 = exports.pbkdf2 = exports.ph1 = exports.sh = exports.h = exports.isSafePrime = void 0;
|
|
27
|
+
const dntShim = __importStar(require("../_dnt.shims.js"));
|
|
28
|
+
const types = __importStar(require("../tl/2_types.js"));
|
|
29
|
+
const _0_buffer_js_1 = require("./0_buffer.js");
|
|
30
|
+
const _0_hash_js_1 = require("./0_hash.js");
|
|
31
|
+
const _0_bigint_js_1 = require("./0_bigint.js");
|
|
32
|
+
function isSafePrime(primeBytes, g) {
|
|
33
|
+
// deno-fmt-ignore
|
|
34
|
+
const goodPrime = new Uint8Array([
|
|
35
|
+
0xC7, 0x1C, 0xAE, 0xB9, 0xC6, 0xB1, 0xC9, 0x04, 0x8E, 0x6C, 0x52, 0x2F,
|
|
36
|
+
0x70, 0xF1, 0x3F, 0x73, 0x98, 0x0D, 0x40, 0x23, 0x8E, 0x3E, 0x21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0xD6, 0xFF, 0x9C, 0xA3, 0x92, 0x8F, 0xEF, 0x5B, 0x9A, 0xE4,
|
|
51
|
+
0xE4, 0x18, 0xFC, 0x15, 0xE8, 0x3E, 0xBE, 0xA0, 0xF8, 0x7F, 0xA9, 0xFF,
|
|
52
|
+
0x5E, 0xED, 0x70, 0x05, 0x0D, 0xED, 0x28, 0x49, 0xF4, 0x7B, 0xF9, 0x59,
|
|
53
|
+
0xD9, 0x56, 0x85, 0x0C, 0xE9, 0x29, 0x85, 0x1F, 0x0D, 0x81, 0x15, 0xF6,
|
|
54
|
+
0x35, 0xB1, 0x05, 0xEE, 0x2E, 0x4E, 0x15, 0xD0, 0x4B, 0x24, 0x54, 0xBF,
|
|
55
|
+
0x6F, 0x4F, 0xAD, 0xF0, 0x34, 0xB1, 0x04, 0x03, 0x11, 0x9C, 0xD8, 0xE3,
|
|
56
|
+
0xB9, 0x2F, 0xCC, 0x5B,
|
|
57
|
+
]);
|
|
58
|
+
if (goodPrime.every((v, i) => v == primeBytes[i])) {
|
|
59
|
+
if ([3, 4, 5, 7].includes(g)) { // It's good
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
exports.isSafePrime = isSafePrime;
|
|
66
|
+
// H(data) := sha256(data)
|
|
67
|
+
exports.h = _0_hash_js_1.sha256;
|
|
68
|
+
// SH(data, salt) := H(salt | data | salt)
|
|
69
|
+
const sh = (data, salt) => (0, exports.h)((0, _0_buffer_js_1.concat)(salt, data, salt));
|
|
70
|
+
exports.sh = sh;
|
|
71
|
+
// PH1(password, salt1, salt2) := SH(SH(password, salt1), salt2)
|
|
72
|
+
const ph1 = async (password, salt1, salt2) => await (0, exports.sh)(await (0, exports.sh)(password, salt1), salt2);
|
|
73
|
+
exports.ph1 = ph1;
|
|
74
|
+
async function pbkdf2(password, salt, iterations) {
|
|
75
|
+
const key = await dntShim.dntGlobalThis.crypto.subtle.importKey("raw", password, "PBKDF2", false, ["deriveBits"]);
|
|
76
|
+
const buffer = await dntShim.dntGlobalThis.crypto.subtle.deriveBits({ name: "PBKDF2", salt, iterations, hash: "SHA-512" }, key, 512);
|
|
77
|
+
return new Uint8Array(buffer);
|
|
78
|
+
}
|
|
79
|
+
exports.pbkdf2 = pbkdf2;
|
|
80
|
+
// PH2(password, salt1, salt2) := SH(pbkdf2(sha512, PH1(password, salt1, salt2), salt1, 100000), salt2)
|
|
81
|
+
const ph2 = async (password, salt1, salt2) => await (0, exports.sh)(await pbkdf2(await (0, exports.ph1)(password, salt1, salt2), salt1, 100000), salt2);
|
|
82
|
+
exports.ph2 = ph2;
|
|
83
|
+
function isGoodModExpFirst(modexp, prime) {
|
|
84
|
+
const diff = prime - modexp;
|
|
85
|
+
const minDiffBitsCount = 2048 - 64;
|
|
86
|
+
const maxModExpSize = 256;
|
|
87
|
+
return !(diff < 0n ||
|
|
88
|
+
diff.toString(2).length < minDiffBitsCount ||
|
|
89
|
+
modexp.toString(2).length < minDiffBitsCount ||
|
|
90
|
+
Math.floor((modexp.toString(2).length + 7) / 8) > maxModExpSize);
|
|
91
|
+
}
|
|
92
|
+
exports.isGoodModExpFirst = isGoodModExpFirst;
|
|
93
|
+
function pad(bigint) {
|
|
94
|
+
if (typeof bigint === "number") {
|
|
95
|
+
bigint = BigInt(bigint);
|
|
96
|
+
}
|
|
97
|
+
if (typeof bigint === "bigint") {
|
|
98
|
+
return (0, _0_buffer_js_1.bufferFromBigInt)(bigint, 256, false);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
return (0, _0_buffer_js_1.concat)(new Uint8Array(256 - bigint.length), bigint);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.pad = pad;
|
|
105
|
+
async function checkPassword(password, ap) {
|
|
106
|
+
const algo = ap.currentAlgo;
|
|
107
|
+
if (!(algo instanceof
|
|
108
|
+
types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow)) {
|
|
109
|
+
throw new Error("Unexpected algorithm");
|
|
110
|
+
}
|
|
111
|
+
// g := algo.g
|
|
112
|
+
const g = algo.g;
|
|
113
|
+
// p := algo.p
|
|
114
|
+
const p = (0, _0_bigint_js_1.bigIntFromBuffer)(algo.p, false);
|
|
115
|
+
if (!isSafePrime(algo.p, g)) {
|
|
116
|
+
throw new Error("Got unsafe prime");
|
|
117
|
+
}
|
|
118
|
+
const srpB = ap.srpB;
|
|
119
|
+
const srpId = ap.srpId;
|
|
120
|
+
{
|
|
121
|
+
if (!srpB) {
|
|
122
|
+
throw new Error("srbB is not set");
|
|
123
|
+
}
|
|
124
|
+
if (!srpId) {
|
|
125
|
+
throw new Error("srpId is not set");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// salt1 := algo.salt1
|
|
129
|
+
const salt1 = algo.salt1;
|
|
130
|
+
// salt2 := algo.salt2
|
|
131
|
+
const salt2 = algo.salt2;
|
|
132
|
+
// g_b := srp_B
|
|
133
|
+
const gB = (0, _0_bigint_js_1.bigIntFromBuffer)(srpB, false);
|
|
134
|
+
// k := H(p | g)
|
|
135
|
+
const k = (0, _0_bigint_js_1.bigIntFromBuffer)(await (0, exports.h)((0, _0_buffer_js_1.concat)(pad(p), pad(g))), false);
|
|
136
|
+
let u = 0n;
|
|
137
|
+
let a = 0n;
|
|
138
|
+
let gA = 0n;
|
|
139
|
+
for (let i = 0; i < 1000; i++) {
|
|
140
|
+
a = (0, _0_bigint_js_1.getRandomBigInt)(256, false);
|
|
141
|
+
// g_a := pow(g, a) mod p
|
|
142
|
+
gA = (0, _0_bigint_js_1.modExp)(BigInt(g), a, p);
|
|
143
|
+
if (isGoodModExpFirst(gA, p)) {
|
|
144
|
+
u = (0, _0_bigint_js_1.bigIntFromBuffer)(await (0, _0_hash_js_1.sha256)((0, _0_buffer_js_1.concat)(pad(gA), pad(gB))), false);
|
|
145
|
+
if (u > 0n) {
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!a || !u || !gA) {
|
|
151
|
+
throw new Error();
|
|
152
|
+
}
|
|
153
|
+
// x := PH2(password, salt1, salt2)
|
|
154
|
+
const x = (0, _0_bigint_js_1.bigIntFromBuffer)(await (0, exports.ph2)(password, salt1, salt2), false);
|
|
155
|
+
// v := pow(g, x) mod p
|
|
156
|
+
const v = (0, _0_bigint_js_1.modExp)(BigInt(g), x, p);
|
|
157
|
+
// k_v := (k * v) mod p
|
|
158
|
+
const kV = (0, _0_bigint_js_1.mod)(k * v, p);
|
|
159
|
+
// t := (g_b - k_v) mod p
|
|
160
|
+
const t = (0, _0_bigint_js_1.mod)(gB - kV, p);
|
|
161
|
+
// s_a := pow(t, a + u * x) mod p
|
|
162
|
+
const sA = (0, _0_bigint_js_1.modExp)(t, a + u * x, p);
|
|
163
|
+
// k_a := H(s_a)
|
|
164
|
+
const kA = await (0, exports.h)(pad(sA));
|
|
165
|
+
// M1 := H(H(p) xor H(g) | H(salt1) | H(salt2) | g_a | g_b | k_a)
|
|
166
|
+
const hG = await (0, exports.h)(pad(g));
|
|
167
|
+
const m1 = await (0, exports.h)((0, _0_buffer_js_1.concat)((await (0, exports.h)(pad(p))).map((v, i) => v ^ hG[i]), await (0, exports.h)(salt1), await (0, exports.h)(salt2), pad(gA), pad(gB), kA));
|
|
168
|
+
return new types.InputCheckPasswordSRP({
|
|
169
|
+
srpId: srpId,
|
|
170
|
+
A: pad(gA),
|
|
171
|
+
M1: m1,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
exports.checkPassword = checkPassword;
|
package/types/deps.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export * from "./deps/deno.land/std@0.186.0/testing/asserts.js";
|
|
2
|
-
export { ctr256Decrypt, ctr256Encrypt, factorize, ige256Decrypt, ige256Encrypt, init as initTgCrypto } from "./deps/deno.land/x/tgcrypto@0.1.
|
|
2
|
+
export { ctr256Decrypt, ctr256Encrypt, factorize, ige256Decrypt, ige256Encrypt, init as initTgCrypto } from "./deps/deno.land/x/tgcrypto@0.1.3/mod.js";
|
|
3
3
|
export { gunzip, gzip } from "./deps/raw.githubusercontent.com/MTKruto/compress/master/gzip/gzip.js";
|
|
4
4
|
export { Mutex } from "async-mutex";
|
package/types/mod.d.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
+
import { checkPassword } from "./utilities/1_password.js";
|
|
2
|
+
import { getRandomId } from "./utilities/0_bigint.js";
|
|
3
|
+
export declare const utils: {
|
|
4
|
+
checkPassword: typeof checkPassword;
|
|
5
|
+
getRandomId: typeof getRandomId;
|
|
6
|
+
};
|
|
1
7
|
export * as types from "./tl/2_types.js";
|
|
2
8
|
export * as functions from "./tl/3_functions.js";
|
|
3
|
-
export
|
|
4
|
-
export
|
|
9
|
+
export * from "./client/client_plain.js";
|
|
10
|
+
export * from "./client/client.js";
|
|
11
|
+
export * from "./transport/transport_abridged.js";
|
|
12
|
+
export * from "./transport/transport_intermediate.js";
|
|
13
|
+
export * from "./transport/transport.js";
|
|
14
|
+
export * from "./transport/transport_provider.js";
|
|
15
|
+
export * from "./connection/connection.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Connection } from "../connection/connection.js";
|
|
2
|
+
import { Transport } from "./transport.js";
|
|
3
|
+
export declare class TransportAbridged extends Transport implements Transport {
|
|
4
|
+
private readonly connection;
|
|
5
|
+
private readonly obfuscated;
|
|
6
|
+
constructor(connection: Connection, obfuscated?: boolean);
|
|
7
|
+
initialize(): Promise<void>;
|
|
8
|
+
receive(): Promise<Uint8Array>;
|
|
9
|
+
send(buffer: Uint8Array): Promise<void>;
|
|
10
|
+
deinitialize(): void;
|
|
11
|
+
}
|
|
@@ -2,3 +2,4 @@ export declare function modExp(a: bigint, b: bigint, n: bigint): bigint;
|
|
|
2
2
|
export declare function mod(n: bigint, m: bigint): bigint;
|
|
3
3
|
export declare function bigIntFromBuffer(buffer: Uint8Array, little?: boolean, signed?: boolean): bigint;
|
|
4
4
|
export declare function getRandomBigInt(byteLength: number, little?: boolean, signed?: boolean): bigint;
|
|
5
|
+
export declare function getRandomId(): bigint;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as types from "../tl/2_types.js";
|
|
2
|
+
import { sha256 } from "./0_hash.js";
|
|
3
|
+
export declare function isSafePrime(primeBytes: Uint8Array, g: number): boolean;
|
|
4
|
+
export declare const h: typeof sha256;
|
|
5
|
+
export declare const sh: (data: Uint8Array, salt: Uint8Array) => Promise<Uint8Array>;
|
|
6
|
+
export declare const ph1: (password: Uint8Array, salt1: Uint8Array, salt2: Uint8Array) => Promise<Uint8Array>;
|
|
7
|
+
export declare function pbkdf2(password: Uint8Array, salt: Uint8Array, iterations: number): Promise<Uint8Array>;
|
|
8
|
+
export declare const ph2: (password: Uint8Array, salt1: Uint8Array, salt2: Uint8Array) => Promise<Uint8Array>;
|
|
9
|
+
export declare function isGoodModExpFirst(modexp: bigint, prime: bigint): boolean;
|
|
10
|
+
export declare function pad(bigint: number | bigint | Uint8Array): Uint8Array;
|
|
11
|
+
export declare function checkPassword(password: Uint8Array, ap: types.AccountPassword): Promise<types.InputCheckPasswordSRP>;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|