Пример #1
0
 public static BigInteger CreateRandomInRange(BigInteger min, BigInteger max, SecureRandom random)
 {
     int num = min.CompareTo(max);
     if (num >= 0)
     {
         if (num > 0)
         {
             throw new ArgumentException("'min' may not be greater than 'max'");
         }
         return min;
     }
     if (min.BitLength > (max.BitLength / 2))
     {
         return CreateRandomInRange(BigInteger.Zero, max.Subtract(min), random).Add(min);
     }
     for (int i = 0; i < 0x3e8; i++)
     {
         BigInteger integer = new BigInteger(max.BitLength, random);
         if ((integer.CompareTo(min) >= 0) && (integer.CompareTo(max) <= 0))
         {
             return integer;
         }
     }
     return new BigInteger(max.Subtract(min).BitLength - 1, random).Add(min);
 }
Пример #2
0
 public virtual BigInteger GenerateClientCredentials(byte[] salt, byte[] identity, byte[] password)
 {
     this.x = Srp6Utilities.CalculateX(this.digest, this.N, salt, identity, password);
     this.privA = this.SelectPrivateValue();
     this.pubA = this.g.ModPow(this.privA, this.N);
     return this.pubA;
 }
Пример #3
0
 public virtual BigInteger CalculateSecret(BigInteger serverB)
 {
     this.B = Srp6Utilities.ValidatePublicValue(this.N, serverB);
     this.u = Srp6Utilities.CalculateU(this.digest, this.N, this.pubA, this.B);
     this.S = this.CalculateS();
     return this.S;
 }
Пример #4
0
 public virtual BigInteger CalculateSecret(BigInteger clientA)
 {
     this.A = Srp6Utilities.ValidatePublicValue(this.N, clientA);
     this.u = Srp6Utilities.CalculateU(this.digest, this.N, this.A, this.pubB);
     this.S = this.CalculateS();
     return this.S;
 }
Пример #5
0
 public virtual void Init(BigInteger N, BigInteger g, IDigest digest, SecureRandom random)
 {
     this.N = N;
     this.g = g;
     this.digest = digest;
     this.random = random;
 }
Пример #6
0
 public static BigInteger GeneratePrivateValue(IDigest digest, BigInteger N, BigInteger g, SecureRandom random)
 {
     int num = Math.Min(0x100, N.BitLength / 2);
     BigInteger min = BigInteger.One.ShiftLeft(num - 1);
     BigInteger max = N.Subtract(BigInteger.One);
     return BigIntegers.CreateRandomInRange(min, max, random);
 }
Пример #7
0
 public virtual BigInteger GenerateServerCredentials()
 {
     BigInteger integer = Srp6Utilities.CalculateK(this.digest, this.N, this.g);
     this.privB = this.SelectPrivateValue();
     this.pubB = integer.Multiply(this.v).Mod(this.N).Add(this.g.ModPow(this.privB, this.N)).Mod(this.N);
     return this.pubB;
 }
Пример #8
0
 public static BigInteger ValidatePublicValue(BigInteger N, BigInteger val)
 {
     val = val.Mod(N);
     if (val.Equals(BigInteger.Zero))
     {
         throw new Exception("Invalid public value: 0");
     }
     return val;
 }
Пример #9
0
 private static BigInteger HashPaddedPair(IDigest digest, BigInteger N, BigInteger n1, BigInteger n2)
 {
     int length = (N.BitLength + 7) / 8;
     byte[] padded = GetPadded(n1, length);
     byte[] input = GetPadded(n2, length);
     digest.BlockUpdate(padded, 0, padded.Length);
     digest.BlockUpdate(input, 0, input.Length);
     byte[] output = new byte[digest.GetDigestSize()];
     digest.DoFinal(output, 0);
     return new BigInteger(1, output).Mod(N);
 }
Пример #10
0
 private static byte[] GetPadded(BigInteger n, int length)
 {
     byte[] sourceArray = BigIntegers.AsUnsignedByteArray(n);
     if (sourceArray.Length < length)
     {
         byte[] destinationArray = new byte[length];
         Array.Copy(sourceArray, 0, destinationArray, length - sourceArray.Length, sourceArray.Length);
         sourceArray = destinationArray;
     }
     return sourceArray;
 }
Пример #11
0
 public static BigInteger CalculateX(IDigest digest, BigInteger N, byte[] salt, byte[] identity, byte[] password)
 {
     byte[] output = new byte[digest.GetDigestSize()];
     digest.BlockUpdate(identity, 0, identity.Length);
     digest.Update(0x3a);
     digest.BlockUpdate(password, 0, password.Length);
     digest.DoFinal(output, 0);
     digest.BlockUpdate(salt, 0, salt.Length);
     digest.BlockUpdate(output, 0, output.Length);
     digest.DoFinal(output, 0);
     return new BigInteger(1, output).Mod(N);
 }
Пример #12
0
 internal bool RabinMillerTest(int certainty, Random random)
 {
     BigInteger integer4;
     BigInteger m = this;
     BigInteger integer2 = m.Subtract(One);
     int lowestSetBit = integer2.GetLowestSetBit();
     BigInteger exponent = integer2.ShiftRight(lowestSetBit);
     Label_001D:
     integer4 = new BigInteger(m.BitLength, random);
     if ((integer4.CompareTo(One) <= 0) || (integer4.CompareTo(integer2) >= 0))
     {
         goto Label_001D;
     }
     BigInteger integer5 = integer4.ModPow(exponent, m);
     if (!integer5.Equals(One))
     {
         int num2 = 0;
         while (!integer5.Equals(integer2))
         {
             if (++num2 == lowestSetBit)
             {
                 return false;
             }
             integer5 = integer5.ModPow(Two, m);
             if (integer5.Equals(One))
             {
                 return false;
             }
         }
     }
     certainty -= 2;
     if (certainty > 0)
     {
         goto Label_001D;
     }
     return true;
 }
Пример #13
0
 public BigInteger Subtract(BigInteger n)
 {
     BigInteger integer;
     BigInteger integer2;
     if (n.sign == 0)
     {
         return this;
     }
     if (this.sign == 0)
     {
         return n.Negate();
     }
     if (this.sign != n.sign)
     {
         return this.Add(n.Negate());
     }
     int num = CompareNoLeadingZeroes(0, this.magnitude, 0, n.magnitude);
     if (num == 0)
     {
         return Zero;
     }
     if (num < 0)
     {
         integer = n;
         integer2 = this;
     }
     else
     {
         integer = this;
         integer2 = n;
     }
     return new BigInteger(this.sign * num, doSubBigLil(integer.magnitude, integer2.magnitude), true);
 }
Пример #14
0
 public BigInteger ModPow(BigInteger exponent, BigInteger m)
 {
     if (m.sign < 1)
     {
         throw new ArithmeticException("Modulus must be positive");
     }
     if (m.Equals(One))
     {
         return Zero;
     }
     if (exponent.sign == 0)
     {
         return One;
     }
     if (this.sign == 0)
     {
         return Zero;
     }
     int[] array = null;
     int[] a = null;
     bool flag = (m.magnitude[m.magnitude.Length - 1] & 1) == 1;
     long mQuote = 0L;
     if (flag)
     {
         mQuote = m.GetMQuote();
         array = this.ShiftLeft(0x20 * m.magnitude.Length).Mod(m).magnitude;
         flag = array.Length <= m.magnitude.Length;
         if (flag)
         {
             a = new int[m.magnitude.Length + 1];
             if (array.Length < m.magnitude.Length)
             {
                 int[] numArray4 = new int[m.magnitude.Length];
                 array.CopyTo(numArray4, (int)(numArray4.Length - array.Length));
                 array = numArray4;
             }
         }
     }
     if (!flag)
     {
         if (this.magnitude.Length <= m.magnitude.Length)
         {
             array = new int[m.magnitude.Length];
             this.magnitude.CopyTo(array, (int)(array.Length - this.magnitude.Length));
         }
         else
         {
             BigInteger integer2 = this.Remainder(m);
             array = new int[m.magnitude.Length];
             integer2.magnitude.CopyTo(array, (int)(array.Length - integer2.magnitude.Length));
         }
         a = new int[m.magnitude.Length * 2];
     }
     int[] numArray3 = new int[m.magnitude.Length];
     for (int i = 0; i < exponent.magnitude.Length; i++)
     {
         int num3 = exponent.magnitude[i];
         int num4 = 0;
         if (i == 0)
         {
             while (num3 > 0)
             {
                 num3 = num3 << 1;
                 num4++;
             }
             array.CopyTo(numArray3, 0);
             num3 = num3 << 1;
             num4++;
         }
         while (num3 != 0)
         {
             if (flag)
             {
                 MultiplyMonty(a, numArray3, numArray3, m.magnitude, mQuote);
             }
             else
             {
                 Square(a, numArray3);
                 this.Remainder(a, m.magnitude);
                 Array.Copy(a, a.Length - numArray3.Length, numArray3, 0, numArray3.Length);
                 ZeroOut(a);
             }
             num4++;
             if (num3 < 0)
             {
                 if (flag)
                 {
                     MultiplyMonty(a, numArray3, array, m.magnitude, mQuote);
                 }
                 else
                 {
                     Multiply(a, numArray3, array);
                     this.Remainder(a, m.magnitude);
                     Array.Copy(a, a.Length - numArray3.Length, numArray3, 0, numArray3.Length);
                     ZeroOut(a);
                 }
             }
             num3 = num3 << 1;
         }
         while (num4 < 0x20)
         {
             if (flag)
             {
                 MultiplyMonty(a, numArray3, numArray3, m.magnitude, mQuote);
             }
             else
             {
                 Square(a, numArray3);
                 this.Remainder(a, m.magnitude);
                 Array.Copy(a, a.Length - numArray3.Length, numArray3, 0, numArray3.Length);
                 ZeroOut(a);
             }
             num4++;
         }
     }
     if (flag)
     {
         ZeroOut(array);
         array[array.Length - 1] = 1;
         MultiplyMonty(a, numArray3, array, m.magnitude, mQuote);
     }
     BigInteger integer3 = new BigInteger(1, numArray3, true);
     if (exponent.sign <= 0)
     {
         return integer3.ModInverse(m);
     }
     return integer3;
 }
Пример #15
0
 public static byte[] AsUnsignedByteArray(BigInteger n)
 {
     return n.ToByteArrayUnsigned();
 }
Пример #16
0
 public BigInteger Add(BigInteger value)
 {
     if (this.sign == 0)
     {
         return value;
     }
     if (this.sign == value.sign)
     {
         return this.AddToMagnitude(value.magnitude);
     }
     if (value.sign == 0)
     {
         return this;
     }
     if (value.sign < 0)
     {
         return this.Subtract(value.Negate());
     }
     return value.Subtract(this.Negate());
 }
Пример #17
0
 public BigInteger ModInverse(BigInteger m)
 {
     if (m.sign < 1)
     {
         throw new ArithmeticException("Modulus must be positive");
     }
     BigInteger integer = new BigInteger();
     if (!ExtEuclid(this.Mod(m), m, integer, null).Equals(One))
     {
         throw new ArithmeticException("Numbers not relatively prime.");
     }
     if (integer.sign < 0)
     {
         integer.sign = 1;
         integer.magnitude = doSubBigLil(m.magnitude, integer.magnitude);
     }
     return integer;
 }
Пример #18
0
 public BigInteger Gcd(BigInteger value)
 {
     BigInteger integer;
     if (value.sign == 0)
     {
         return this.Abs();
     }
     if (this.sign == 0)
     {
         return value.Abs();
     }
     BigInteger integer2 = this;
     for (BigInteger integer3 = value; integer3.sign != 0; integer3 = integer)
     {
         integer = integer2.Mod(integer3);
         integer2 = integer3;
     }
     return integer2;
 }
Пример #19
0
 public BigInteger Multiply(BigInteger val)
 {
     if ((this.sign == 0) || (val.sign == 0))
     {
         return Zero;
     }
     if (val.QuickPow2Check())
     {
         BigInteger integer = this.ShiftLeft(val.Abs().BitLength - 1);
         if (val.sign <= 0)
         {
             return integer.Negate();
         }
         return integer;
     }
     if (this.QuickPow2Check())
     {
         BigInteger integer2 = val.ShiftLeft(this.Abs().BitLength - 1);
         if (this.sign <= 0)
         {
             return integer2.Negate();
         }
         return integer2;
     }
     int num = ((this.BitLength + val.BitLength) / 0x20) + 1;
     int[] w = new int[num];
     if (val == this)
     {
         Square(w, this.magnitude);
     }
     else
     {
         Multiply(w, this.magnitude, val.magnitude);
     }
     return new BigInteger(this.sign * val.sign, w, true);
 }
Пример #20
0
 public BigInteger Divide(BigInteger val)
 {
     if (val.sign == 0)
     {
         throw new ArithmeticException("Division by zero error");
     }
     if (this.sign == 0)
     {
         return Zero;
     }
     if (val.QuickPow2Check())
     {
         BigInteger integer = this.Abs().ShiftRight(val.Abs().BitLength - 1);
         if (val.sign != this.sign)
         {
             return integer.Negate();
         }
         return integer;
     }
     int[] x = (int[])this.magnitude.Clone();
     return new BigInteger(this.sign * val.sign, this.Divide(x, val.magnitude), true);
 }
Пример #21
0
 public BigInteger[] DivideAndRemainder(BigInteger val)
 {
     if (val.sign == 0)
     {
         throw new ArithmeticException("Division by zero error");
     }
     BigInteger[] integerArray = new BigInteger[2];
     if (this.sign == 0)
     {
         integerArray[0] = Zero;
         integerArray[1] = Zero;
         return integerArray;
     }
     if (val.QuickPow2Check())
     {
         int n = val.Abs().BitLength - 1;
         BigInteger integer = this.Abs().ShiftRight(n);
         int[] numArray = this.LastNBits(n);
         integerArray[0] = (val.sign == this.sign) ? integer : integer.Negate();
         integerArray[1] = new BigInteger(this.sign, numArray, true);
         return integerArray;
     }
     int[] x = (int[])this.magnitude.Clone();
     int[] mag = this.Divide(x, val.magnitude);
     integerArray[0] = new BigInteger(this.sign * val.sign, mag, true);
     integerArray[1] = new BigInteger(this.sign, x, true);
     return integerArray;
 }
Пример #22
0
 private static BigInteger createUValueOf(ulong value)
 {
     int num = (int)(value >> 0x20);
     int num2 = (int)value;
     if (num != 0)
     {
         return new BigInteger(1, new int[] { num, num2 }, false);
     }
     if (num2 == 0)
     {
         return Zero;
     }
     BigInteger integer = new BigInteger(1, new int[] { num2 }, false);
     if ((num2 & -num2) == num2)
     {
         integer.nBits = 1;
     }
     return integer;
 }
Пример #23
0
 public BigInteger AndNot(BigInteger val)
 {
     return this.And(val.Not());
 }
Пример #24
0
 public BigInteger And(BigInteger value)
 {
     if ((this.sign == 0) || (value.sign == 0))
     {
         return Zero;
     }
     int[] numArray = (this.sign > 0) ? this.magnitude : this.Add(One).magnitude;
     int[] numArray2 = (value.sign > 0) ? value.magnitude : value.Add(One).magnitude;
     bool flag = (this.sign < 0) && (value.sign < 0);
     int[] mag = new int[Math.Max(numArray.Length, numArray2.Length)];
     int num2 = mag.Length - numArray.Length;
     int num3 = mag.Length - numArray2.Length;
     for (int i = 0; i < mag.Length; i++)
     {
         int num5 = (i >= num2) ? numArray[i - num2] : 0;
         int num6 = (i >= num3) ? numArray2[i - num3] : 0;
         if (this.sign < 0)
         {
             num5 = ~num5;
         }
         if (value.sign < 0)
         {
             num6 = ~num6;
         }
         mag[i] = num5 & num6;
         if (flag)
         {
             mag[i] = ~mag[i];
         }
     }
     BigInteger integer = new BigInteger(1, mag, true);
     if (flag)
     {
         integer = integer.Not();
     }
     return integer;
 }
Пример #25
0
 public BigInteger ShiftLeft(int n)
 {
     if ((this.sign == 0) || (this.magnitude.Length == 0))
     {
         return Zero;
     }
     if (n == 0)
     {
         return this;
     }
     if (n < 0)
     {
         return this.ShiftRight(-n);
     }
     BigInteger integer = new BigInteger(this.sign, ShiftLeft(this.magnitude, n), true);
     if (this.nBits != -1)
     {
         integer.nBits = (this.sign > 0) ? this.nBits : (this.nBits + n);
     }
     if (this.nBitLength != -1)
     {
         integer.nBitLength = this.nBitLength + n;
     }
     return integer;
 }
Пример #26
0
 public BigInteger Min(BigInteger value)
 {
     if (this.CompareTo(value) >= 0)
     {
         return value;
     }
     return this;
 }
Пример #27
0
 public BigInteger Remainder(BigInteger n)
 {
     int[] numArray;
     if (n.sign == 0)
     {
         throw new ArithmeticException("Division by zero error");
     }
     if (this.sign == 0)
     {
         return Zero;
     }
     if (n.magnitude.Length == 1)
     {
         int m = n.magnitude[0];
         if (m > 0)
         {
             if (m != 1)
             {
                 int num2 = this.Remainder(m);
                 if (num2 != 0)
                 {
                     return new BigInteger(this.sign, new int[] { num2 }, false);
                 }
             }
             return Zero;
         }
     }
     if (CompareNoLeadingZeroes(0, this.magnitude, 0, n.magnitude) < 0)
     {
         return this;
     }
     if (n.QuickPow2Check())
     {
         numArray = this.LastNBits(n.Abs().BitLength - 1);
     }
     else
     {
         numArray = (int[])this.magnitude.Clone();
         numArray = this.Remainder(numArray, n.magnitude);
     }
     return new BigInteger(this.sign, numArray, true);
 }
Пример #28
0
 public BigInteger Mod(BigInteger m)
 {
     if (m.sign < 1)
     {
         throw new ArithmeticException("Modulus must be positive");
     }
     BigInteger integer = this.Remainder(m);
     if (integer.sign < 0)
     {
         return integer.Add(m);
     }
     return integer;
 }
Пример #29
0
 public int CompareTo(BigInteger value)
 {
     if (this.sign < value.sign)
     {
         return -1;
     }
     if (this.sign > value.sign)
     {
         return 1;
     }
     if (this.sign != 0)
     {
         return (this.sign * CompareNoLeadingZeroes(0, this.magnitude, 0, value.magnitude));
     }
     return 0;
 }
Пример #30
0
 private static BigInteger ExtEuclid(BigInteger a, BigInteger b, BigInteger u1Out, BigInteger u2Out)
 {
     BigInteger[] integerArray;
     BigInteger one = One;
     BigInteger integer2 = a;
     BigInteger zero = Zero;
     for (BigInteger integer4 = b; integer4.sign > 0; integer4 = integerArray[1])
     {
         integerArray = integer2.DivideAndRemainder(integer4);
         BigInteger n = zero.Multiply(integerArray[0]);
         BigInteger integer6 = one.Subtract(n);
         one = zero;
         zero = integer6;
         integer2 = integer4;
     }
     if (u1Out != null)
     {
         u1Out.sign = one.sign;
         u1Out.magnitude = one.magnitude;
     }
     if (u2Out != null)
     {
         BigInteger integer7 = one.Multiply(a);
         BigInteger integer8 = integer2.Subtract(integer7).Divide(b);
         u2Out.sign = integer8.sign;
         u2Out.magnitude = integer8.magnitude;
     }
     return integer2;
 }