/// <summary> /// Creates a verifier that the server can later use to authenticate users later on (v) /// </summary> public static byte[] ComputeServerVerifier(byte[] privateKey) { NetBigInteger x = new NetBigInteger(NetUtility.ToHexString(privateKey), 16); // Verifier (v) = g^x (mod N) var serverVerifier = g.ModPow(x, N); return(serverVerifier.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); }
//public byte[] GenerateObfuscationCallback() //{ // var _loc5_ = 0; // var _loc6_ = ""; // var _loc1_ = ""; // var _loc2_ = 0; // while (_loc2_ < 128) // { // _loc5_ = (int)(MathF.random() * 256); // _loc6_ = Convert.ToString(_loc5_, 16); // if (_loc6_.Length == 1) // { // _loc6_ = "0" + _loc6_; // } // _loc1_ = _loc1_ + _loc6_; // _loc2_++; // } // this.privateKey = new NetBigInteger(_loc1_, 16); // var _loc3_ = ENC_BASE_GENERATOR.ModPow(privateKey, ENC_PRIME_MODULUS); // var loc3Bytes = _loc3_.ToByteArray(); // Console.WriteLine("Generated key, L:" + loc3Bytes.Length); // return loc3Bytes; //} public byte[] GenerateObfuscationCallback() { var finalizedString = ""; for (int i = 0; i < 128; i++) { var randomNum = Convert.ToInt32(MathF.random() * 256); var randomizedString = Convert.ToString(randomNum, 16); if (randomizedString.Length == 1) { randomizedString = "0" + randomizedString; } finalizedString = finalizedString + randomizedString; } Console.WriteLine(finalizedString); // Output 0: a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 // Output 1: b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7 // Output 2: dededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededede3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a privateKey = new NetBigInteger(finalizedString, 16); var generatedKey = ENC_BASE_GENERATOR.ModPow(privateKey, ENC_PRIME_MODULUS); var bytes = generatedKey.ToByteArray(); Console.WriteLine("Generated key, L:" + bytes.Length); return(bytes); }
/// <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 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[] ComputeServerSessionValue(byte[] clientPublicEphemeral, byte[] verifier, byte[] udata, byte[] serverPrivateEphemeral) { // S = (Av^u) ^ b (mod N) var A = new NetBigInteger(NetUtility.ToHexString(clientPublicEphemeral), 16); var v = new NetBigInteger(NetUtility.ToHexString(verifier), 16); var u = new NetBigInteger(NetUtility.ToHexString(udata), 16); var b = new NetBigInteger(NetUtility.ToHexString(serverPrivateEphemeral), 16); NetBigInteger retval = v.ModPow(u, N).Multiply(A).Mod(N).ModPow(b, N).Mod(N); return(retval.ToByteArrayUnsigned()); }
/// <summary> /// Returns A. /// </summary> /// <param name="N"></param> /// <param name="g"></param> /// <param name="a"></param> /// <returns></returns> public static NetBigInteger CalcA(NetBigInteger N, NetBigInteger g, NetBigInteger a) { // A = g^a % N if (g == null) { throw new ArgumentNullException("g"); } if (N == null) { throw new ArgumentNullException("N"); } return(g.ModPow(a, N)); }
/// <summary> /// Calculates V. The password verifier. /// </summary> /// <param name="N"></param> /// <param name="g"></param> /// <param name="x"></param> /// <returns></returns> public static NetBigInteger CalcV(NetBigInteger N, NetBigInteger g, NetBigInteger x) { // v = g^x % N return(g.ModPow(x, N)); }
/// <summary> /// Computes the server session value /// </summary> public static byte[] ComputeServerSessionValue(byte[] clientPublicEphemeral, byte[] verifier, byte[] udata, byte[] serverPrivateEphemeral) { // S = (Av^u) ^ b (mod N) var A = new NetBigInteger(NetUtility.ToHexString(clientPublicEphemeral), 16); var v = new NetBigInteger(NetUtility.ToHexString(verifier), 16); var u = new NetBigInteger(NetUtility.ToHexString(udata), 16); var b = new NetBigInteger(NetUtility.ToHexString(serverPrivateEphemeral), 16); NetBigInteger retval = v.ModPow(u, N).Multiply(A).Mod(N).ModPow(b, N).Mod(N); return retval.ToByteArrayUnsigned(); }
/// <summary> /// Calculates V. The password verifier. /// </summary> /// <param name="N"></param> /// <param name="g"></param> /// <param name="x"></param> /// <returns></returns> public static NetBigInteger CalcV(NetBigInteger N, NetBigInteger g, NetBigInteger x) { // v = g^x % N return g.ModPow(x, 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> /// 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> /// Returns A. /// </summary> /// <param name="N"></param> /// <param name="g"></param> /// <param name="a"></param> /// <returns></returns> public static NetBigInteger CalcA(NetBigInteger N, NetBigInteger g, NetBigInteger a) { // A = g^a % N if (g == null) throw new ArgumentNullException("g"); if (N == null) throw new ArgumentNullException("N"); return g.ModPow(a, N); }