/// <summary> /// Calculates B. /// </summary> /// <param name="N"></param> /// <param name="g"></param> /// <param name="b"></param> /// <param name="v"></param> /// <returns></returns> public static NetBigInteger CalcB(NetBigInteger N, NetBigInteger g, NetBigInteger b, NetBigInteger v) { //B = k*v + g^b % N NetBigInteger k = Calck(N, g); return((k.Multiply(v).Add(g.ModPow(b, N))).Mod(N)); }
/// <summary> /// Calculates server's S. /// </summary> /// <param name="N"></param> /// <param name="A"></param> /// <param name="v"></param> /// <param name="u"></param> /// <param name="b"></param> /// <returns></returns> public static NetBigInteger CalcSServer(NetBigInteger N, NetBigInteger A, NetBigInteger v, NetBigInteger u, NetBigInteger b) { // Host: S = (Av^u) ^ b (computes session key) NetBigInteger S = (A.Multiply(v.ModPow(u, N))).ModPow(b, N); return(S); }
/// <summary> /// Calculates client's S. /// </summary> /// <param name="N"></param> /// <param name="g"></param> /// <param name="B"></param> /// <param name="k"></param> /// <param name="x"></param> /// <param name="a"></param> /// <param name="u"></param> /// <returns></returns> public static NetBigInteger CalcSClient(NetBigInteger N, NetBigInteger g, NetBigInteger B, NetBigInteger k, NetBigInteger x, NetBigInteger a, NetBigInteger u) { // <premaster secret> = (B - (k * g^x)) ^ (a + (u * x)) % N // (B + (N - ((k*g.ModExp(x,N))%N))) - Do it this way. Thanks Valery. NetBigInteger S = (B.Add(N.Subtract((k.Multiply(g.ModPow(x, N))).Mod(N)))).ModPow(a.Add(u.Multiply(x)), N); return(S); }
/// <summary> /// Compute server ephemeral value (B) /// </summary> public static byte[] ComputeServerEphemeral(byte[] serverPrivateEphemeral, byte[] verifier) // b { var b = new NetBigInteger(NetUtility.ToHexString(serverPrivateEphemeral), 16); var v = new NetBigInteger(NetUtility.ToHexString(verifier), 16); // B = kv + g^b (mod N) var bb = g.ModPow(b, N); var kv = v.Multiply(k); var B = (kv.Add(bb)).Mod(N); return(B.ToByteArrayUnsigned()); }
public static byte[] ComputeClientSessionValue(byte[] serverPublicEphemeral, byte[] xdata, byte[] udata, byte[] clientPrivateEphemeral) { // (B - kg^x) ^ (a + ux) (mod N) var B = new NetBigInteger(NetUtility.ToHexString(serverPublicEphemeral), 16); var x = new NetBigInteger(NetUtility.ToHexString(xdata), 16); var u = new NetBigInteger(NetUtility.ToHexString(udata), 16); var a = new NetBigInteger(NetUtility.ToHexString(clientPrivateEphemeral), 16); var bx = g.ModPow(x, N); var btmp = B.Add(N.Multiply(k)).Subtract(bx.Multiply(k)).Mod(N); return(btmp.ModPow(x.Multiply(u).Add(a), N).ToByteArrayUnsigned()); }
/// <summary> /// Computes the client session value /// </summary> public static byte[] ComputeClientSessionValue(byte[] serverPublicEphemeral, byte[] xdata, byte[] udata, byte[] clientPrivateEphemeral) { // (B - kg^x) ^ (a + ux) (mod N) var B = new NetBigInteger(NetUtility.ToHexString(serverPublicEphemeral), 16); var x = new NetBigInteger(NetUtility.ToHexString(xdata), 16); var u = new NetBigInteger(NetUtility.ToHexString(udata), 16); var a = new NetBigInteger(NetUtility.ToHexString(clientPrivateEphemeral), 16); var bx = g.ModPow(x, N); var btmp = B.Add(N.Multiply(k)).Subtract(bx.Multiply(k)).Mod(N); return btmp.ModPow(x.Multiply(u).Add(a), N).ToByteArrayUnsigned(); }
/// <summary> /// Compute server ephemeral value (B) /// </summary> public static byte[] ComputeServerEphemeral(byte[] serverPrivateEphemeral, byte[] verifier) // b { var b = new NetBigInteger(NetUtility.ToHexString(serverPrivateEphemeral), 16); var v = new NetBigInteger(NetUtility.ToHexString(verifier), 16); // B = kv + g^b (mod N) var bb = g.ModPow(b, N); var kv = v.Multiply(k); var B = (kv.Add(bb)).Mod(N); return B.ToByteArrayUnsigned(); }
/// <summary> /// Calculates server's S. /// </summary> /// <param name="N"></param> /// <param name="A"></param> /// <param name="v"></param> /// <param name="u"></param> /// <param name="b"></param> /// <returns></returns> public static NetBigInteger CalcSServer(NetBigInteger N, NetBigInteger A, NetBigInteger v, NetBigInteger u, NetBigInteger b) { // Host: S = (Av^u) ^ b (computes session key) NetBigInteger S = (A.Multiply(v.ModPow(u, N))).ModPow(b, N); return S; }
/// <summary> /// Calculates client's S. /// </summary> /// <param name="N"></param> /// <param name="g"></param> /// <param name="B"></param> /// <param name="k"></param> /// <param name="x"></param> /// <param name="a"></param> /// <param name="u"></param> /// <returns></returns> public static NetBigInteger CalcSClient(NetBigInteger N, NetBigInteger g, NetBigInteger B, NetBigInteger k, NetBigInteger x, NetBigInteger a, NetBigInteger u) { // <premaster secret> = (B - (k * g^x)) ^ (a + (u * x)) % N // (B + (N - ((k*g.ModExp(x,N))%N))) - Do it this way. Thanks Valery. NetBigInteger S = (B.Add(N.Subtract((k.Multiply(g.ModPow(x, N))).Mod(N)))).ModPow(a.Add(u.Multiply(x)), N); return S; }