Remember the good primes to prevent verifying them twice

This commit is contained in:
Wizou 2021-09-01 23:03:04 +02:00
parent 3701ba6f72
commit 67285c1c08
4 changed files with 13 additions and 11 deletions

2
.github/ci.yml vendored
View file

@ -2,7 +2,7 @@ pr: none
trigger:
- master
name: 0.9.4-ci.$(Rev:r)
name: 0.9.5-ci.$(Rev:r)
pool:
vmImage: ubuntu-latest

View file

@ -190,7 +190,7 @@ namespace WTelegram
writer.Write(0L); // int64 auth_key_id = 0 (Unencrypted)
writer.Write(msgId); // int64 message_id
writer.Write(0); // int32 message_data_length (to be patched)
var typeName = func(writer); // bytes message_data
var typeName = func(writer); // bytes message_data
Helpers.Log(1, $"Sending {typeName}...");
BinaryPrimitives.WriteInt32LittleEndian(memStream.GetBuffer().AsSpan(24), (int)memStream.Length - 28); // patch message_data_length
}
@ -204,12 +204,12 @@ namespace WTelegram
const int prepend = 32;
clearWriter.Write(_session.AuthKey, 88, prepend);
#endif
clearWriter.Write(_session.Salt); // int64 salt
clearWriter.Write(_session.Id); // int64 session_id
clearWriter.Write(msgId); // int64 message_id
clearWriter.Write(seqno); // int32 msg_seqno
clearWriter.Write(0); // int32 message_data_length (to be patched)
var typeName = func(clearWriter); // bytes message_data
clearWriter.Write(_session.Salt); // int64 salt
clearWriter.Write(_session.Id); // int64 session_id
clearWriter.Write(msgId); // int64 message_id
clearWriter.Write(seqno); // int32 msg_seqno
clearWriter.Write(0); // int32 message_data_length (to be patched)
var typeName = func(clearWriter); // bytes message_data
if ((seqno & 1) != 0)
Helpers.Log(1, $"Sending {typeName,-50} #{(short)msgId.GetHashCode():X4}");
else
@ -558,7 +558,7 @@ namespace WTelegram
}
catch (OperationCanceledException)
{ }
catch (Exception ex)
catch (Exception ex) when (!ct.IsCancellationRequested)
{
Helpers.Log(5, $"An exception occured in the reactor: {ex}");
}

View file

@ -194,7 +194,7 @@ namespace WTelegram
private static void ValidityChecks(BigInteger p, int g)
{
Helpers.Log(2, "Verifying encryption key safety... (this happens only during session negociation)");
Helpers.Log(2, "Verifying encryption key safety... (this should happen only once)");
// check that 2^2047 <= p < 2^2048
if (p.GetBitLength() != 2048) throw new ApplicationException("p is not 2048-bit number");
// check that g generates a cyclic subgroup of prime order (p - 1) / 2, i.e. is a quadratic residue mod p.

View file

@ -147,12 +147,13 @@ namespace WTelegram
}
public static int MillerRabinIterations { get; set; } = 64; // 64 is OpenSSL default for 2048-bits numbers
private static readonly HashSet<BigInteger> GoodPrimes = new();
// MillerRabin primality test
public static bool IsProbablePrime(this BigInteger n)
{
var n_minus_one = n - BigInteger.One;
if (n_minus_one.Sign <= 0) return false;
if (GoodPrimes.Contains(n)) return true;
int s;
var d = n_minus_one;
@ -187,6 +188,7 @@ namespace WTelegram
}
if (r == 0) return false;
}
GoodPrimes.Add(n);
return true;
}
}