Exemplo n.º 1
0
        /// <summary>
        /// Initialize class with Prime and State Seed values. Values must be probable primes.
        /// </summary>
        /// 
        /// <param name="P">Random Prime with probability &lt; 2 ** -100</param>
        /// <param name="G">Random Generator State</param>
        /// 
        /// <exception cref="CryptoRandomException">Thrown if P is not a valid prime</exception>
        public QCG2(BigInteger P, BigInteger G)
        {
            if (!P.IsProbablePrime(90))
                throw new CryptoRandomException("QCG2:Ctor", "P is not a valid prime number!", new ArgumentOutOfRangeException());

            _secRand = new SecureRandom();

            _P = P;
            _G = G;
            _G0 = G;
        }
Exemplo n.º 2
0
 /// <summary>
 /// A convenience method that generates a random salt vector for key pair generation.
 /// </summary>
 /// 
 /// <param name="Size">Byte length of the new salt</param>
 /// 
 /// <returns>A new salt vector</returns>
 public byte[] GenerateSalt(int Size = 16)
 {
     using (SecureRandom rnd = new SecureRandom())
         return rnd.GetBytes(Size);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Initialize the class
        /// </summary>
        /// 
        /// <param name="BitLength">Length of integers used in equations; must be at least 512 bits</param>
        public QCG2(int BitLength)
        {
            _secRand = new SecureRandom();

            if (BitLength < G_BITS)
                Initialize(G_BITS);
            else
                Initialize(BitLength);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create a random BigInteger
        /// </summary>
        /// 
        /// <param name="UpperBound">The upper bound</param>
        /// 
        /// <returns>A random BigInteger</returns>
        public static BigInteger Randomize(BigInteger UpperBound)
        {
            if (_secRnd == null)
                _secRnd = new SecureRandom();

            return Randomize(UpperBound, _secRnd);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Create a random BigInteger
        /// </summary>
        /// 
        /// <param name="UpperBound">The upper bound</param>
        /// <param name="SecRnd">An instance of the SecureRandom class</param>
        /// 
        /// <returns>A random BigInteger</returns>
        public static BigInteger Randomize(BigInteger UpperBound, SecureRandom SecRnd)
        {
            int blen = UpperBound.BitLength;
            BigInteger randomNum = BigInteger.ValueOf(0);

            if (SecRnd == null)
                SecRnd = _secRnd != null ? _secRnd : new SecureRandom();

            for (int i = 0; i < 20; i++)
            {
                randomNum = new BigInteger(blen, SecRnd);
                if (randomNum.CompareTo(UpperBound) < 0)
                    return randomNum;
            }

            return randomNum.Mod(UpperBound);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initialize the class
        /// </summary>
        public QCG2()
        {
            m_secRand = new SecureRandom();

            Initialize(G_BITS);
        }
Exemplo n.º 7
0
        private void SecRandTest()
        {
            using (SecureRandom rnd = new SecureRandom())
            {
                double x1 = 0.0;
                for (int i = 0; i < 1000; i++)
                {
                    x1 = rnd.NextDouble();
                    if (x1 > 1.0)
                        throw new Exception("SecureRandom: NextDouble returned a value outside of the expected range.");
                }

                short x2 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x2 = rnd.NextInt16(1, 6);
                    if (x2 > 6)
                        throw new Exception("SecureRandom: NextInt16 returned a value outside of the expected range.");
                    if (x2 < 1)
                        throw new Exception("SecureRandom: NextInt16 returned a value outside of the expected range.");
                }

                ushort x3 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x3 = rnd.NextUInt16(1, 52);
                    if (x3 > 52)
                        throw new Exception("SecureRandom: NextUInt16 returned a value outside of the expected range.");
                    if (x3 < 1)
                        throw new Exception("SecureRandom: NextUInt16 returned a value outside of the expected range.");
                }

                int x4 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x4 = rnd.NextInt32(3371, 16777216);
                    if (x4 > 16777216)
                        throw new Exception("SecureRandom: NextInt32 returned a value outside of the expected range.");
                    if (x4 < 3371)
                        throw new Exception("SecureRandom: NextInt32 returned a value outside of the expected range.");
                }

                uint x5 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x5 = rnd.NextUInt32(77721, 777216);
                    if (x5 > 777216)
                        throw new Exception("SecureRandom: NextUInt32 returned a value outside of the expected range.");
                    if (x5 < 77721)
                        throw new Exception("SecureRandom: NextUInt32 returned a value outside of the expected range.");
                }

                long x6 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x6 = rnd.NextInt64(2814749767, 281474976710653);
                    if (x6 > 281474976710656)
                        throw new Exception("SecureRandom: NextInt64 returned a value outside of the expected range.");
                    if (x6 < 2814749767)
                        throw new Exception("SecureRandom: NextInt64 returned a value outside of the expected range.");
                }

                ulong x7 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x7 = rnd.NextUInt64(5759403792, 72057594037927934);
                    if (x7 > 72057594037927936)
                        throw new Exception("SecureRandom: NextUInt64 returned a value outside of the expected range.");
                    if (x7 < 5759403792)
                        throw new Exception("SecureRandom: NextUInt64 returned a value outside of the expected range.");
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initialize class with Primes, and State Seed values. Values must be probable primes.
        /// </summary>
        /// 
        /// <param name="X">Random Generator State (X = X ** 2 mod N)</param>
        /// <param name="P">P Random Prime</param>
        /// <param name="Q">Q Random Prime</param>
        /// <param name="N">Random Prime (N = P * Q)</param>
        /// 
        /// <exception cref="CryptoRandomException">Thrown if P or Q is not a valid prime</exception>
        public BBSG(BigInteger X, BigInteger P, BigInteger Q, BigInteger N)
        {
            if (!P.IsProbablePrime(90))
                throw new CryptoRandomException("BBSG:Ctor", "P is not a valid prime number!", new ArgumentOutOfRangeException());
            if (!Q.IsProbablePrime(90))
                throw new CryptoRandomException("BBSG:Ctor", "Q is not a valid prime number!", new ArgumentOutOfRangeException());

            _secRand = new SecureRandom();

            _P = P;
            _X = X;
            _N = N;
            _Q = Q;
            _X0 = X;
        }
Exemplo n.º 9
0
 private void Dispose(bool Disposing)
 {
     if (!_isDisposed && Disposing)
     {
         try
         {
             if (_secRand != null)
             {
                 _secRand.Dispose();
                 _secRand = null;
             }
             if (_N != null)
                 _N = null;
             if (_P != null)
                 _P = null;
             if (_Q != null)
                 _Q = null;
             if (_X != null)
                 _X = null;
             if (_X0 != null)
                 _X0 = null;
         }
         finally
         {
             _isDisposed = true;
         }
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Initialize the class
        /// </summary>
        public MODEXPG()
        {
            _secRand = new SecureRandom();

            Initialize(G_BITS);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Initialize the class
        /// </summary>
        /// 
        /// <param name="BitLength">Length of integers used in equations, must be at least 512 bits</param>
        public BBSG(int BitLength)
        {
            _secRand = new SecureRandom();

            if (BitLength < N_BITS)
                Initialize(N_BITS);
            else
                Initialize(BitLength);
        }
 /// <summary>
 /// 
 /// </summary>
 /// 
 /// <param name="Rng"></param>
 /// <param name="Param"></param>
 public McElieceKeyGenerationParameters(SecureRandom Rng, MPKCParameters Param)
     : base(Rng, 256)
 {
     // XXX key size?
     _param = Param;
 }
Exemplo n.º 13
0
        private static bool MillerRabin(BigInteger X, int T)
        {
            // The Miller-Rabin primality test
            // n >= 0, t >= 0
            BigInteger x; // x := UNIFORM{2...n-1}
            BigInteger y; // y := x^(q * 2^j) mod n
            BigInteger n_minus_1 = X.Subtract(BigInteger.One); // n-1
            int bitLength = n_minus_1.BitLength; // ~ log2(n-1)
            // (q,k) such that: n-1 = q * 2^k and q is odd
            int k = n_minus_1.LowestSetBit;
            BigInteger q = n_minus_1.ShiftRight(k);
            SecureRandom rnd = new SecureRandom();

            for (int i = 0; i < T; i++)
            {
                // To generate a witness 'x', first it use the primes of table
                if (i < _primes.Length)
                {
                    x = _biPrimes[i];
                }
                else
                {
                    // It generates random witness only if it's necesssary. 
                    // Note that all methods would call Miller-Rabin with t <= 50 so this part is only to do more robust the algorithm
                    do
                    {
                        x = new BigInteger(bitLength, rnd);
                    } while ((x.CompareTo(X) >= BigInteger.EQUALS) || (x._sign == 0) || x.IsOne());
                }
                y = x.ModPow(q, X);
                if (y.IsOne() || y.Equals(n_minus_1))
                    continue;
                
                for (int j = 1; j < k; j++)
                {
                    if (y.Equals(n_minus_1))
                        continue;
                    
                    y = y.Multiply(y).Mod(X);

                    if (y.IsOne())
                        return false;
                }
                if (!y.Equals(n_minus_1))
                    return false;
            }
            return true;
        }
Exemplo n.º 14
0
        /// <summary>
        /// A random number is generated until a probable prime number is found
        /// </summary>
        internal static BigInteger ConsBigInteger(int BitLength, int Certainty, SecureRandom Rnd)
        {
            // PRE: bitLength >= 2;
            // For small numbers get a random prime from the prime table
            if (BitLength <= 10)
            {
                int[] rp = _offsetPrimes[BitLength];
                return _biPrimes[rp[0] + Rnd.NextInt32(rp[1])];
            }
            int shiftCount = (-BitLength) & 31;
            int last = (BitLength + 31) >> 5;
            BigInteger n = new BigInteger(1, last, new int[last]);

            last--;
            do
            {// To fill the array with random integers
                for (int i = 0; i < n._numberLength; i++)
                    n._digits[i] = Rnd.Next();
                
                // To fix to the correct bitLength
                // n.digits[last] |= 0x80000000;
                n._digits[last] |= Int32.MinValue;
                n._digits[last] = IntUtils.URShift(n._digits[last], shiftCount);
                // To create an odd number
                n._digits[0] |= 1;
            } while (!IsProbablePrime(n, Certainty));

            return n;
        }
Exemplo n.º 15
0
        private void Dispose(bool Disposing)
        {
            if (!_isDisposed && Disposing)
            {
                try
                {
                    if (_secRand != null)
                    {
                        _secRand.Dispose();
                        _secRand = null;
                    }

                    if (_BI3 != null)
                        _BI3 = null;
                    if (_G != null)
                        _G = null;
                    if (_G0 != null)
                        _G0 = null;
                    if (_P != null)
                        _P = null;
                }
                finally
                {
                    _isDisposed = true;
                }
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Initialize the class
        /// </summary>
        public BBSG()
        {
            _secRand = new SecureRandom();

            Initialize(N_BITS);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Initialize the class
        /// </summary>
        public QCG2()
        {
            _secRand = new SecureRandom();

            Initialize(G_BITS);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Initialize the class
        /// </summary>
        public CCG()
        {
            _secRand = new SecureRandom();

            Initialize(G_BITS);
        }