Esempio n. 1
0
        public byte[] GetEntropy()
        {
            lock (this)
            {
                byte[] nxt;

                if (buf == null)
                {
                    buf = entropySource.GetEntropy();
                }

                // FSM_STATE:5.1, "CONTINUOUS NDRBG TEST", "The module is performing Continuous NDRNG self-test"
                // FSM_TRANS:5.1, "CONDITIONAL TEST", "CONTINUOUS NDRNG TEST", "Invoke Continuous NDRNG test"
                nxt = entropySource.GetEntropy();

                if (Arrays.AreEqual(nxt, buf))
                {
                    CryptoStatus.MoveToErrorStatus("Duplicate block detected in EntropySource output");
                }
                // FSM_TRANS:5.2, "CONTINUOUS NDRNG TEST", "CONDITIONAL TEST", "Continuous NDRNG test successful"
                Array.Copy(nxt, 0, buf, 0, buf.Length);

                return(nxt);
            }
        }
Esempio n. 2
0
        internal int Generate(byte[] output, bool predictionResistant)
        {
            //IL_0041: Unknown result type (might be due to invalid IL or missing references)
            //IL_007d: Unknown result type (might be due to invalid IL or missing references)
            //IL_00b9: Unknown result type (might be due to invalid IL or missing references)
            if (mR.Length == 8)
            {
                if (mReseedCounter > 32768)
                {
                    return(-1);
                }
                if (IsTooLarge(output, 512))
                {
                    throw new ArgumentException(string.Concat((object)"Number of bits per request limited to ", (object)4096), "output");
                }
            }
            else
            {
                if (mReseedCounter > 8388608)
                {
                    return(-1);
                }
                if (IsTooLarge(output, 32768))
                {
                    throw new ArgumentException(string.Concat((object)"Number of bits per request limited to ", (object)262144), "output");
                }
            }
            if (predictionResistant || mV == null)
            {
                mV = mEntropySource.GetEntropy();
                if (mV.Length != mEngine.GetBlockSize())
                {
                    throw new InvalidOperationException("Insufficient entropy returned");
                }
            }
            int num = output.Length / mR.Length;

            for (int i = 0; i < num; i++)
            {
                mEngine.ProcessBlock(mDT, 0, mI, 0);
                Process(mR, mI, mV);
                Process(mV, mR, mI);
                global::System.Array.Copy((global::System.Array)mR, 0, (global::System.Array)output, i * mR.Length, mR.Length);
                Increment(mDT);
            }
            int num2 = output.Length - num * mR.Length;

            if (num2 > 0)
            {
                mEngine.ProcessBlock(mDT, 0, mI, 0);
                Process(mR, mI, mV);
                Process(mV, mR, mI);
                global::System.Array.Copy((global::System.Array)mR, 0, (global::System.Array)output, num * mR.Length, num2);
                Increment(mDT);
            }
            mReseedCounter++;
            return(output.Length);
        }
Esempio n. 3
0
    internal int Generate(byte[] output, bool predictionResistant)
    {
        if (mR.Length == 8)
        {
            if (mReseedCounter > 32768)
            {
                return(-1);
            }
            if (IsTooLarge(output, 512))
            {
                throw new ArgumentException("Number of bits per request limited to " + 4096, "output");
            }
        }
        else
        {
            if (mReseedCounter > 8388608)
            {
                return(-1);
            }
            if (IsTooLarge(output, 32768))
            {
                throw new ArgumentException("Number of bits per request limited to " + 262144, "output");
            }
        }
        if (predictionResistant || mV == null)
        {
            mV = mEntropySource.GetEntropy();
            if (mV.Length != mEngine.GetBlockSize())
            {
                throw new InvalidOperationException("Insufficient entropy returned");
            }
        }
        int num = output.Length / mR.Length;

        for (int i = 0; i < num; i++)
        {
            mEngine.ProcessBlock(mDT, 0, mI, 0);
            Process(mR, mI, mV);
            Process(mV, mR, mI);
            Array.Copy(mR, 0, output, i * mR.Length, mR.Length);
            Increment(mDT);
        }
        int num2 = output.Length - num * mR.Length;

        if (num2 > 0)
        {
            mEngine.ProcessBlock(mDT, 0, mI, 0);
            Process(mR, mI, mV);
            Process(mV, mR, mI);
            Array.Copy(mR, 0, output, num * mR.Length, num2);
            Increment(mDT);
        }
        mReseedCounter++;
        return(output.Length);
    }
Esempio n. 4
0
 private byte[] GetEntropy()
 {
     byte[] entropy = mEntropySource.GetEntropy();
     if (entropy.Length < (mSecurityStrength + 7) / 8)
     {
         throw new InvalidOperationException("Insufficient entropy provided by entropy source");
     }
     return(entropy);
 }
Esempio n. 5
0
 private byte[] GetEntropy()
 {
     //IL_0020: Unknown result type (might be due to invalid IL or missing references)
     byte[] entropy = mEntropySource.GetEntropy();
     if (entropy.Length < (mSecurityStrength + 7) / 8)
     {
         throw new InvalidOperationException("Insufficient entropy provided by entropy source");
     }
     return(entropy);
 }
Esempio n. 6
0
 /**
  * Generate numBytes worth of entropy from the passed in entropy source.
  *
  * @param entropySource the entropy source to request the data from.
  * @param numBytes the number of bytes of entropy requested.
  * @return a byte array populated with the random data.
  */
 public static byte[] GenerateSeed(IEntropySource entropySource, int numBytes)
 {
     byte[] bytes = new byte[numBytes];
     int count = 0;
     while (count < numBytes)
     {
         byte[] entropy = entropySource.GetEntropy();
         int toCopy = System.Math.Min(bytes.Length, numBytes - count);
         Array.Copy(entropy, 0, bytes, count, toCopy);
         count += toCopy;
     }
     return bytes;
 }
Esempio n. 7
0
    public static byte[] GenerateSeed(IEntropySource entropySource, int numBytes)
    {
        byte[] array = new byte[numBytes];
        int    num;

        for (int i = 0; i < numBytes; i += num)
        {
            byte[] entropy = entropySource.GetEntropy();
            num = Math.Min(array.Length, numBytes - i);
            Array.Copy(entropy, 0, array, i, num);
        }
        return(array);
    }
Esempio n. 8
0
        /**
         * Generate numBytes worth of entropy from the passed in entropy source.
         *
         * @param entropySource the entropy source to request the data from.
         * @param numBytes the number of bytes of entropy requested.
         * @return a byte array populated with the random data.
         */
        public static byte[] GenerateSeed(IEntropySource entropySource, int numBytes)
        {
            byte[] bytes = new byte[numBytes];
            int    count = 0;

            while (count < numBytes)
            {
                byte[] entropy = entropySource.GetEntropy();
                int    toCopy  = System.Math.Min(bytes.Length, numBytes - count);
                Array.Copy(entropy, 0, bytes, count, toCopy);
                count += toCopy;
            }
            return(bytes);
        }
Esempio n. 9
0
        /**
         * Populate a passed in array with random data.
         *
         * @param output output array for generated bits.
         * @param predictionResistant true if a reseed should be forced, false otherwise.
         *
         * @return number of bits generated, -1 if a reseed required.
         */
        internal int Generate(byte[] output, bool predictionResistant)
        {
            if (mR.Length == 8) // 64 bit block size
            {
                if (mReseedCounter > BLOCK64_RESEED_MAX)
                {
                    return(-1);
                }

                if (IsTooLarge(output, BLOCK64_MAX_BITS_REQUEST / 8))
                {
                    throw new ArgumentException("Number of bits per request limited to " + BLOCK64_MAX_BITS_REQUEST, "output");
                }
            }
            else
            {
                if (mReseedCounter > BLOCK128_RESEED_MAX)
                {
                    return(-1);
                }

                if (IsTooLarge(output, BLOCK128_MAX_BITS_REQUEST / 8))
                {
                    throw new ArgumentException("Number of bits per request limited to " + BLOCK128_MAX_BITS_REQUEST, "output");
                }
            }

            if (predictionResistant || mV == null)
            {
                mV = mEntropySource.GetEntropy();
                if (mV.Length != mEngine.GetBlockSize())
                {
                    throw new InvalidOperationException("Insufficient entropy returned");
                }
            }

            int m = output.Length / mR.Length;

            for (int i = 0; i < m; i++)
            {
                mEngine.ProcessBlock(mDT, 0, mI, 0);
                Process(mR, mI, mV);
                Process(mV, mR, mI);

                Array.Copy(mR, 0, output, i * mR.Length, mR.Length);

                Increment(mDT);
            }

            int bytesToCopy = (output.Length - m * mR.Length);

            if (bytesToCopy > 0)
            {
                mEngine.ProcessBlock(mDT, 0, mI, 0);
                Process(mR, mI, mV);
                Process(mV, mR, mI);

                Array.Copy(mR, 0, output, m * mR.Length, bytesToCopy);

                Increment(mDT);
            }

            mReseedCounter++;

            return(output.Length);
        }