You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-16 23:26:32 +09:00
SRP work
This commit is contained in:
@@ -154,9 +154,9 @@ namespace Lidgren.Network
|
||||
|
||||
public static class NetSRP
|
||||
{
|
||||
private static readonly BigInteger N = new BigInteger(NetUtility.ToByteArray("0115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3"));
|
||||
private static readonly BigInteger g = new BigInteger((uint)2);
|
||||
private static readonly BigInteger k = ComputeMultiplier();
|
||||
public static readonly BigInteger N = new BigInteger(NetUtility.ToByteArray("0115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3"));
|
||||
public static readonly BigInteger g = new BigInteger((uint)2);
|
||||
public static readonly BigInteger k = ComputeMultiplier();
|
||||
|
||||
/// <summary>
|
||||
/// Compute multiplier (k)
|
||||
@@ -172,7 +172,7 @@ namespace Lidgren.Network
|
||||
/// <summary>
|
||||
/// Creates a verifier that the server can use to authenticate users later on (v)
|
||||
/// </summary>
|
||||
public static byte[] ComputePasswordVerifier(string username, string password, byte[] salt)
|
||||
public static byte[] ComputePasswordVerifier(string username, string password, byte[] salt, out byte[] x)
|
||||
{
|
||||
byte[] tmp = Encoding.ASCII.GetBytes(username + ":" + password);
|
||||
byte[] innerHash = NetSha.Hash(tmp);
|
||||
@@ -181,7 +181,7 @@ namespace Lidgren.Network
|
||||
Buffer.BlockCopy(salt, 0, total, 0, salt.Length);
|
||||
Buffer.BlockCopy(innerHash, 0, total, salt.Length, innerHash.Length);
|
||||
|
||||
byte[] x = NetSha.Hash(total);
|
||||
x = NetSha.Hash(total);
|
||||
|
||||
// Verifier (v) = g^x (mod N)
|
||||
BigInteger xx = new BigInteger(x);
|
||||
@@ -227,7 +227,8 @@ namespace Lidgren.Network
|
||||
|
||||
string one = NetUtility.ToHexString(A);
|
||||
string two = NetUtility.ToHexString(B);
|
||||
string compound = one.PadLeft(64, '0') + two.PadLeft(64, '0');
|
||||
string compound = one.PadLeft(66, '0') + two.PadLeft(66, '0');
|
||||
|
||||
byte[] cc = NetUtility.ToByteArray(compound);
|
||||
|
||||
return NetSha.Hash(cc);
|
||||
@@ -320,5 +321,52 @@ function srp_compute_client_S(BB, xx, uu, aa, kk) {
|
||||
return r2.ModPow(new BigInteger(serverChallengeSalt), N).GetBytes();
|
||||
//return vv.modPow(uu, N).multiply(A).mod(N).modPow(bb, N);
|
||||
}
|
||||
|
||||
public static byte[] ComputeServerCompareValue(byte[] A, byte[] verifier, byte[] u, byte[] b)
|
||||
{
|
||||
// S = (Av^u) ^ b (mod N)
|
||||
|
||||
BigInteger verBi = new BigInteger(verifier);
|
||||
BigInteger uBi = new BigInteger(u);
|
||||
BigInteger ABi = new BigInteger(A);
|
||||
BigInteger bBi = new BigInteger(b);
|
||||
|
||||
BigInteger res1 = verBi.ModPow(uBi, N);
|
||||
BigInteger res2 = BigInteger.Multiply(res1, ABi);
|
||||
BigInteger res3 = BigInteger.Modulus(res2, N);
|
||||
BigInteger res4 = res3.ModPow(bBi, N);
|
||||
|
||||
return res4.GetBytes();
|
||||
}
|
||||
|
||||
public static byte[] ComputeClientCompareValue(byte[] B, byte[] x, byte[] u, byte[] A)
|
||||
{
|
||||
// S = (B - kg^x) ^ (a + ux) (mod N)
|
||||
BigInteger xBi = new BigInteger(x);
|
||||
BigInteger BBi = new BigInteger(B);
|
||||
BigInteger uBi = new BigInteger(u);
|
||||
BigInteger ABi = new BigInteger(A);
|
||||
|
||||
|
||||
//var btmp = BB.add(N.multiply(kk)).subtract(bx.multiply(kk)).mod(N);
|
||||
|
||||
//return btmp.modPow(xx.multiply(uu).add(aa), N);
|
||||
|
||||
|
||||
|
||||
BigInteger bx = g.ModPow(xBi, N);
|
||||
|
||||
BigInteger res1 = BigInteger.Multiply(N, k);
|
||||
BigInteger btmp1 = BigInteger.Add(BBi, res1);
|
||||
|
||||
BigInteger res2 = BigInteger.Multiply(bx, k);
|
||||
BigInteger res3 = BigInteger.Subtract(btmp1, res2);
|
||||
BigInteger btmp = BigInteger.Modulus(res3, N);
|
||||
|
||||
BigInteger res5 = BigInteger.Multiply(xBi, uBi);
|
||||
BigInteger res6 = BigInteger.Add(res5, ABi);
|
||||
|
||||
return btmp.ModPow(res6, N).GetBytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -398,9 +398,6 @@ namespace Lidgren.Network
|
||||
continue;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
// heartbeat done
|
||||
return;
|
||||
}
|
||||
|
||||
private void HandleUnconnectedLibraryMessage(NetMessageLibraryType libType, int ptr, int payloadLengthBits, IPEndPoint senderEndpoint)
|
||||
|
||||
@@ -111,7 +111,6 @@ namespace DurableServer
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Bad NetDeliveryMethod: " + msg.DeliveryMethod);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -58,22 +58,32 @@ namespace UnitTests
|
||||
|
||||
Console.WriteLine("Message encryption OK");
|
||||
|
||||
byte[] salt = NetUtility.ToByteArray("62191568b7a1aa18f8eb"); // s
|
||||
byte[] verifier = NetSRP.ComputePasswordVerifier("user", "password", salt);
|
||||
byte[] salt = NetUtility.ToByteArray("47d980ce4c2333b6ce5b"); // s
|
||||
byte[] x;
|
||||
byte[] verifier = NetSRP.ComputePasswordVerifier("user", "password", salt, out x);
|
||||
|
||||
Console.WriteLine("v = " + NetUtility.ToHexString(verifier));
|
||||
|
||||
byte[] a = NetUtility.ToByteArray("129aac7ce0be45ab5f65ec0c6879222386c32177cb4024fe7ad593341c0a5085");
|
||||
byte[] a = NetUtility.ToByteArray("94f5a7f6875df8b569840a917b918c84aa002b145e24e77dabdd3941de82e6f5");
|
||||
byte[] A = NetSRP.ComputeClientChallenge(a);
|
||||
Console.WriteLine("A = " + NetUtility.ToHexString(A));
|
||||
|
||||
byte[] b = NetUtility.ToByteArray("cdbe8cec49e33c78c0b434be67fa2fdb7646776e757bcf59fad51bbbee0d53a1");
|
||||
byte[] b = NetUtility.ToByteArray("a4ae167ba24c498a52d9a6963c285bb999246d3ce4c5e1028be5206809611358");
|
||||
Console.WriteLine("b = " + NetUtility.ToHexString(b));
|
||||
byte[] B = NetSRP.ComputeServerChallenge(b, verifier);
|
||||
Console.WriteLine("B = " + NetUtility.ToHexString(B));
|
||||
|
||||
byte[] u = NetSRP.ComputeU(A, B);
|
||||
Console.WriteLine("u = " + NetUtility.ToHexString(u));
|
||||
|
||||
byte[] serverCompareValue; // Ss
|
||||
serverCompareValue = NetSRP.ComputeServerCompareValue(A, verifier, u, b);
|
||||
Console.WriteLine("Ss = " + NetUtility.ToHexString(serverCompareValue));
|
||||
|
||||
byte[] clientCompareValue; // Ss
|
||||
clientCompareValue = NetSRP.ComputeClientCompareValue(B, x, u, A);
|
||||
Console.WriteLine("Sc = " + NetUtility.ToHexString(clientCompareValue));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,6 @@ namespace UnitTests
|
||||
break;
|
||||
case NetIncomingMessageType.Error:
|
||||
throw new Exception("Received error message!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user