/// <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); }
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()); }
public NetBigInteger And( NetBigInteger value) { if (m_sign == 0 || value.m_sign == 0) { return Zero; } int[] aMag = m_sign > 0 ? m_magnitude : Add(One).m_magnitude; int[] bMag = value.m_sign > 0 ? value.m_magnitude : value.Add(One).m_magnitude; bool resultNeg = m_sign < 0 && value.m_sign < 0; int resultLength = System.Math.Max(aMag.Length, bMag.Length); int[] resultMag = new int[resultLength]; int aStart = resultMag.Length - aMag.Length; int bStart = resultMag.Length - bMag.Length; for (int i = 0; i < resultMag.Length; ++i) { int aWord = i >= aStart ? aMag[i - aStart] : 0; int bWord = i >= bStart ? bMag[i - bStart] : 0; if (m_sign < 0) { aWord = ~aWord; } if (value.m_sign < 0) { bWord = ~bWord; } resultMag[i] = aWord & bWord; if (resultNeg) { resultMag[i] = ~resultMag[i]; } } NetBigInteger result = new NetBigInteger(1, resultMag, true); if (resultNeg) { result = result.Not(); } return result; }
/// <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> /// 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; }