Skip to content

Commit

Permalink
update to 1.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKLO authored and DrKLO committed Dec 23, 2013
1 parent 02c8efb commit 50a92dc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion TMessagesProj/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.telegram.messenger"
android:versionCode="126"
android:versionName="1.3.4">
android:versionName="1.3.5">

<supports-screens android:anyDensity="true"
android:smallScreens="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void run() {
return;
}

if (!Utilities.isGoodPrime(dhInnerData.dh_prime)) {
if (!Utilities.isGoodPrime(dhInnerData.dh_prime, dhInnerData.g)) {
throw new RuntimeException("bad prime");
}

Expand All @@ -344,15 +344,17 @@ public void run() {
b[a] = (byte)(MessagesController.random.nextDouble() * 255);
}

BigInteger dhBI = new BigInteger(1, dhInnerData.dh_prime);
BigInteger i_g_b = BigInteger.valueOf(dhInnerData.g);
i_g_b = i_g_b.modPow(new BigInteger(1, b), dhBI);
byte[] g_b = i_g_b.toByteArray();
BigInteger p = new BigInteger(1, dhInnerData.dh_prime);
BigInteger g_b = BigInteger.valueOf(dhInnerData.g);
BigInteger g_a = new BigInteger(1, dhInnerData.g_a);
if (!Utilities.isGoodGaAndGb(g_a, g_b, p)) {
throw new RuntimeException("bad prime");
}

BigInteger i_authKey = new BigInteger(1, dhInnerData.g_a);
i_authKey = i_authKey.modPow(new BigInteger(1, b), dhBI);
g_b = g_b.modPow(new BigInteger(1, b), p);
g_a = g_a.modPow(new BigInteger(1, b), p);

authKey = i_authKey.toByteArray();
authKey = g_a.toByteArray();
if (authKey.length > 256) {
byte[] correctedAuth = new byte[256];
System.arraycopy(authKey, 1, correctedAuth, 0, 256);
Expand Down Expand Up @@ -390,7 +392,7 @@ public void run() {
TLRPC.TL_client_DH_inner_data clientInnerData = new TLRPC.TL_client_DH_inner_data();
clientInnerData.nonce = authNonce;
clientInnerData.server_nonce = authServerNonce;
clientInnerData.g_b = g_b;
clientInnerData.g_b = g_b.toByteArray();
clientInnerData.retry_id = 0;
SerializedData os = new SerializedData();
clientInnerData.serializeToStream(os);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4811,8 +4811,15 @@ public TLRPC.Message decryptMessage(TLRPC.EncryptedMessage message) {
}

public void processAcceptedSecretChat(final TLRPC.EncryptedChat encryptedChat) {
BigInteger p = new BigInteger(1, MessagesStorage.secretPBytes);
BigInteger i_authKey = new BigInteger(1, encryptedChat.g_a_or_b);
i_authKey = i_authKey.modPow(new BigInteger(1, encryptedChat.a_or_b), new BigInteger(1, MessagesStorage.secretPBytes));

if (!Utilities.isGoodGaAndGb(null, i_authKey, p)) {
declineSecretChat(encryptedChat.id);
return;
}

i_authKey = i_authKey.modPow(new BigInteger(1, encryptedChat.a_or_b), p);

byte[] authKey = i_authKey.toByteArray();
if (authKey.length > 256) {
Expand Down Expand Up @@ -4883,7 +4890,7 @@ public void run(TLObject response, TLRPC.TL_error error) {
if (error == null) {
TLRPC.messages_DhConfig res = (TLRPC.messages_DhConfig)response;
if (response instanceof TLRPC.TL_messages_dhConfig) {
if (!Utilities.isGoodPrime(res.p)) {
if (!Utilities.isGoodPrime(res.p, res.g)) {
acceptingChats.remove(encryptedChat.id);
declineSecretChat(encryptedChat.id);
return;
Expand All @@ -4899,19 +4906,27 @@ public void run(TLObject response, TLRPC.TL_error error) {
salt[a] = (byte)((byte)(random.nextDouble() * 255) ^ res.random[a]);
}
encryptedChat.a_or_b = salt;
BigInteger i_g_b = BigInteger.valueOf(MessagesStorage.secretG);
i_g_b = i_g_b.modPow(new BigInteger(1, salt), new BigInteger(1, MessagesStorage.secretPBytes));
byte[] g_b = i_g_b.toByteArray();
if (g_b.length > 256) {
BigInteger p = new BigInteger(1, MessagesStorage.secretPBytes);
BigInteger g_b = BigInteger.valueOf(MessagesStorage.secretG);
g_b = g_b.modPow(new BigInteger(1, salt), p);
BigInteger g_a = new BigInteger(1, encryptedChat.g_a);

if (!Utilities.isGoodGaAndGb(g_a, g_b, p)) {
acceptingChats.remove(encryptedChat.id);
declineSecretChat(encryptedChat.id);
return;
}

byte[] g_b_bytes = g_b.toByteArray();
if (g_b_bytes.length > 256) {
byte[] correctedAuth = new byte[256];
System.arraycopy(g_b, 1, correctedAuth, 0, 256);
g_b = correctedAuth;
System.arraycopy(g_b_bytes, 1, correctedAuth, 0, 256);
g_b_bytes = correctedAuth;
}

BigInteger i_authKey = new BigInteger(1, encryptedChat.g_a);
i_authKey = i_authKey.modPow(new BigInteger(1, salt), new BigInteger(1, MessagesStorage.secretPBytes));
g_a = g_a.modPow(new BigInteger(1, salt), p);

byte[] authKey = i_authKey.toByteArray();
byte[] authKey = g_a.toByteArray();
if (authKey.length > 256) {
byte[] correctedAuth = new byte[256];
System.arraycopy(authKey, authKey.length - 256, correctedAuth, 0, 256);
Expand All @@ -4930,7 +4945,7 @@ public void run(TLObject response, TLRPC.TL_error error) {
encryptedChat.auth_key = authKey;

TLRPC.TL_messages_acceptEncryption req2 = new TLRPC.TL_messages_acceptEncryption();
req2.g_b = g_b;
req2.g_b = g_b_bytes;
req2.peer = new TLRPC.TL_inputEncryptedChat();
req2.peer.chat_id = encryptedChat.id;
req2.peer.access_hash = encryptedChat.access_hash;
Expand Down Expand Up @@ -4976,7 +4991,7 @@ public void run(TLObject response, TLRPC.TL_error error) {
if (error == null) {
TLRPC.messages_DhConfig res = (TLRPC.messages_DhConfig)response;
if (response instanceof TLRPC.TL_messages_dhConfig) {
if (!Utilities.isGoodPrime(res.p)) {
if (!Utilities.isGoodPrime(res.p, res.g)) {
Utilities.RunOnUIThread(new Runnable() {
@Override
public void run() {
Expand All @@ -4996,6 +5011,7 @@ public void run() {
for (int a = 0; a < 256; a++) {
salt[a] = (byte)((byte)(random.nextDouble() * 255) ^ res.random[a]);
}

BigInteger i_g_a = BigInteger.valueOf(MessagesStorage.secretG);
i_g_a = i_g_a.modPow(new BigInteger(1, salt), new BigInteger(1, MessagesStorage.secretPBytes));
byte[] g_a = i_g_a.toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ public static String bytesToHex(byte[] bytes) {
return new String(hexChars);
}

public static boolean isGoodPrime(byte[] prime) {
public static boolean isGoodPrime(byte[] prime, int g) {
if (!(g >= 2 && g <= 7)) {
return false;
}
String hex = bytesToHex(prime);
for (String cached : goodPrimes) {
if (cached.equals(hex)) {
Expand All @@ -121,6 +124,16 @@ public static boolean isGoodPrime(byte[] prime) {
return true;
}

public static boolean isGoodGaAndGb(BigInteger g_a, BigInteger g_b, BigInteger p) {

This comment has been minimized.

Copy link
@hellman

hellman Dec 23, 2013

g_a > 1 and g_b < (p-1)?
What if g_a >= (p-1) or g_b <= 1?

This comment has been minimized.

Copy link
@DrKLO

DrKLO Dec 23, 2013

Owner

yes, my bad, fixed it

if (g_a != null && g_a.compareTo(BigInteger.valueOf(1)) != 1) {
return false;
}
if (g_b != null && p != null && g_b.compareTo(p.subtract(BigInteger.valueOf(1))) != -1) {
return false;
}
return true;
}

public static TPFactorizedValue getFactorizedValue(long what) {
long g = doPQNative(what);
if (g > 1 && g < what) {
Expand Down

0 comments on commit 50a92dc

Please sign in to comment.