Skip to content

Commit 67af7b2

Browse files
committed
Made signature verification time constant
1 parent 5b7485f commit 67af7b2

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,14 @@ internal static void HandleHailResponse(uint clientId, Stream stream, int channe
129129
{
130130
byte[] clientHash = rsa.Decrypt(diffieHellmanPublicSignature, false);
131131
byte[] serverHash = sha.ComputeHash(diffieHellmanPublic);
132-
if (clientHash.Length != serverHash.Length)
132+
133+
if (!CryptographyHelper.ConstTimeArrayEqual(clientHash, serverHash))
133134
{
134135
//Man in the middle.
135-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Signature length doesnt match for the key exchange public part. Disconnecting");
136+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Signature doesnt match for the key exchange public part. Disconnecting");
136137
netManager.DisconnectClient(clientId);
137138
return;
138139
}
139-
for (int i = 0; i < clientHash.Length; i++)
140-
{
141-
if (clientHash[i] != serverHash[i])
142-
{
143-
//Man in the middle.
144-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Signature doesnt match for the key exchange public part. Disconnecting");
145-
netManager.DisconnectClient(clientId);
146-
return;
147-
}
148-
}
149140
}
150141
}
151142
else

MLAPI/NetworkingManagerComponents/Cryptography/CryptographyHelper.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ public static byte[] GetServerKey()
7878
return NetworkingManager.Singleton.clientAesKey;
7979
}
8080
}
81+
82+
internal static bool ConstTimeArrayEqual(byte[] a, byte[] b)
83+
{
84+
if (a.Length != b.Length)
85+
return false;
86+
87+
int i = a.Length;
88+
int cmp = 0;
89+
90+
while (i != 0)
91+
{
92+
--i;
93+
cmp |= (a[i] ^ b[i]);
94+
}
95+
96+
return cmp == 0;
97+
}
8198
}
8299
}
83100
#endif

0 commit comments

Comments
 (0)