private Srp6(string initialVector, string modulus, int generator, byte[] identityHash) { HashAlgorithm = "SHA1"; PasswordIterations = 2; KeySize = 256; InitialVector = initialVector; IdentityHash = identityHash; Modulus = BigIntegerExtensions.CreateBigInteger(modulus, 16); Generator = BigIntegerExtensions.CreateBigInteger("" + generator, 10); Multiplier = BigIntegerExtensions.CreateBigInteger("3", 10); }
public void SetSessionKey(String pubKeyString, String scrambler = null) { BigInteger pubKey = BigIntegerExtensions.CreateBigInteger(pubKeyString, 16); if (IsServerInstance) // Server SessionKey { // (Av^u) ^ b (mod N) SessionKey = pubKey.Multiply(Verifier.ModPow(Scrambler, Modulus)).ModPow(PrivateKey, Modulus); } else // Client SessionKey { Scrambler = BigIntegerExtensions.CreateBigInteger(scrambler, 16); BigInteger temp = PrivateKey.Add(Scrambler.Multiply(SaltedIdentityHash)); SessionKey = pubKey.Subtract((Generator.ModPow(SaltedIdentityHash, Modulus)) .Multiply(Multiplier)).ModPow(temp, Modulus); } }
// Client Constructor, salt not generated by client public Srp6(byte[] identityHash, String modulus, int generator, String salt, string initialVector = "OFRna73m*aze01xY") : this(initialVector, modulus, generator, identityHash) { // This SRP6 instance is a client instance IsServerInstance = false; // Convert the salt string to a BigInteger Salt = BigIntegerExtensions.CreateBigInteger(salt, 16); // Set the salted identity hash SaltedIdentityHash = Salt.CreateSaltedIdentityHash(identityHash); // Generate a pseudo prime to use for the private key PrivateKey = BigIntegerExtensions.GeneratePseudoPrime(128, 100, new Random()); // g^a (mod N) PublicKey = Generator.ModPow(PrivateKey, Modulus); }
// Server Constructor, Radix 16 strings, 256-bit predef values public Srp6(byte[] identityHash, string modulus, int generator, string inputSalt, int scramblerBits, string initialVector = "OFRna73m*aze01xY") : this(initialVector, modulus, generator, identityHash) { // This SRP6 instance is a server instance IsServerInstance = true; // Generate the Salt Salt = BigIntegerExtensions.CreateBigInteger(inputSalt, 16); // Set the salted identity hash, scrambler, and verifier Scrambler = BigIntegerExtensions.CreateBigInteger(scramblerBits, new Random()); SaltedIdentityHash = Salt.CreateSaltedIdentityHash(identityHash); Verifier = Generator.ModPow(SaltedIdentityHash, Modulus); // Random 128 bit number that is a probable prime PrivateKey = BigIntegerExtensions.GeneratePseudoPrime(128, 100, new Random()); // kv + g^b (mod N) PublicKey = Multiplier.Multiply(Verifier).Add(Generator.ModPow(PrivateKey, Modulus)); }
public static BigInteger CreateSaltedIdentityHash(this BigInteger salt, byte[] identityHash) { return(BigIntegerExtensions.CreateBigInteger( Concatenate(salt.ToByteArray(), identityHash).Sha1Hash().ToHexString(), 16)); }
public static BigInteger generateSalt(int saltBits) { return(BigIntegerExtensions.CreateBigInteger(saltBits, new Random())); }