mirror of
https://github.com/wiz0u/WTelegramClient.git
synced 2026-04-05 22:45:38 +00:00
various optimizations
This commit is contained in:
parent
3784ad00ad
commit
a45cd0f44e
4 changed files with 40 additions and 20 deletions
|
|
@ -33,7 +33,7 @@ namespace WTelegram
|
|||
|
||||
//1)
|
||||
var nonce = new Int128(RNG);
|
||||
var resPQ = await client.ReqPqMulti(nonce);
|
||||
var resPQ = await client.ReqPqMulti(nonce);
|
||||
//2)
|
||||
if (resPQ.nonce != nonce) throw new ApplicationException("Nonce mismatch");
|
||||
var fingerprint = resPQ.server_public_key_fingerprints.FirstOrDefault(PublicKeys.ContainsKey);
|
||||
|
|
@ -182,7 +182,7 @@ namespace WTelegram
|
|||
}
|
||||
}
|
||||
|
||||
private static void ValidityChecks(BigInteger p, int g)
|
||||
internal static void ValidityChecks(BigInteger p, int g)
|
||||
{
|
||||
Helpers.Log(2, "Verifying encryption key safety... (this should happen only once per DC)");
|
||||
// check that 2^2047 <= p < 2^2048
|
||||
|
|
@ -201,11 +201,33 @@ namespace WTelegram
|
|||
})
|
||||
throw new ApplicationException("Bad prime mod 4g");
|
||||
// check whether p is a safe prime (meaning that both p and (p - 1) / 2 are prime)
|
||||
if (SafePrimes.Contains(p)) return;
|
||||
if (!p.IsProbablePrime()) throw new ApplicationException("p is not a prime number");
|
||||
if (!((p - 1) / 2).IsProbablePrime()) throw new ApplicationException("(p - 1) / 2 is not a prime number");
|
||||
SafePrimes.Add(p);
|
||||
}
|
||||
|
||||
private static void ValidityChecksDH(BigInteger g_a, BigInteger g_b, BigInteger dh_prime)
|
||||
private static readonly HashSet<BigInteger> SafePrimes = new() { new(new byte[] // C71CAEB9C6B1C904...
|
||||
{
|
||||
0x5B, 0xCC, 0x2F, 0xB9, 0xE3, 0xD8, 0x9C, 0x11, 0x03, 0x04, 0xB1, 0x34, 0xF0, 0xAD, 0x4F, 0x6F,
|
||||
0xBF, 0x54, 0x24, 0x4B, 0xD0, 0x15, 0x4E, 0x2E, 0xEE, 0x05,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x95, 0x13, 0x25, 0xC9, 0x0A, 0x6E, 0x6F, 0x33, 0xFA,
|
||||
0xDB, 0xF4, 0x30, 0x25, 0xD2, 0x93, 0x94, 0x22, 0x58, 0x40, 0xC1, 0xA7, 0x0A, 0x8A, 0x19, 0x48,
|
||||
0x0F, 0x93, 0x3D, 0x56, 0x37, 0xD0, 0x34, 0x49, 0xC1, 0x21, 0x3E, 0x8E, 0x23, 0x40, 0x0D, 0x98,
|
||||
0x73, 0x3F, 0xF1, 0x70, 0x2F, 0x52, 0x6C, 0x8E, 0x04, 0xC9, 0xB1, 0xC6, 0xB9, 0xAE, 0x1C, 0xC7, 0x00
|
||||
})};
|
||||
|
||||
internal static void ValidityChecksDH(BigInteger g_a, BigInteger g_b, BigInteger dh_prime)
|
||||
{
|
||||
// check that g, g_a and g_b are greater than 1 and less than dh_prime - 1.
|
||||
// We recommend checking that g_a and g_b are between 2^{2048-64} and dh_prime - 2^{2048-64} as well.
|
||||
|
|
@ -232,7 +254,7 @@ namespace WTelegram
|
|||
Helpers.Log(1, $"Loaded a public key with fingerprint {fingerprint:X}");
|
||||
}
|
||||
|
||||
private static void LoadDefaultPublicKeys()
|
||||
private static void LoadDefaultPublicKeys()
|
||||
{
|
||||
// Production Public Key (D09D1D85DE64FD85)
|
||||
LoadPublicKey(@"-----BEGIN RSA PUBLIC KEY-----
|
||||
|
|
@ -254,11 +276,10 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB
|
|||
-----END RSA PUBLIC KEY-----");
|
||||
}
|
||||
|
||||
internal static byte[] EncryptDecryptMessage(Span<byte> input, bool encrypt, byte[] authKey, byte[] msgKey, int msgKeyOffset, SHA256 sha256)
|
||||
internal static byte[] EncryptDecryptMessage(Span<byte> input, bool encrypt, int x, byte[] authKey, byte[] msgKey, int msgKeyOffset, SHA256 sha256)
|
||||
{
|
||||
// first, construct AES key & IV
|
||||
byte[] aes_key = new byte[32], aes_iv = new byte[32];
|
||||
int x = encrypt ? 0 : 8;
|
||||
sha256.TransformBlock(msgKey, msgKeyOffset, 16, null, 0); // msgKey
|
||||
sha256.TransformFinalBlock(authKey, x, 36); // authKey[x:36]
|
||||
var sha256_a = sha256.Hash;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue