Ejemplo 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);
            }
        }
Ejemplo n.º 2
0
        internal static T Validate <T>(Algorithm algorithm, T parameters, IConsistencyTest <T> test)
        {
            try
            {
                if (!test.HasTestPassed(parameters))
                {
                    CryptoStatus.MoveToErrorStatus(new ConsistencyTestFailedError("Consistency test failed", algorithm));
                }

                return(parameters);
            }
            catch (Exception e)
            {
                CryptoStatus.MoveToErrorStatus(new ConsistencyTestFailedError("Exception on consistency test: " + e.Message, algorithm));
            }

            return(default(T));            // we'll never get this far
        }
Ejemplo n.º 3
0
        internal static T Validate <T>(Algorithm algorithm, T engine, IBasicKatTest <T> test)
        {
            try
            {
                if (!test.HasTestPassed(engine))
                {
                    CryptoStatus.MoveToErrorStatus(new SelfTestFailedError("Self test failed", algorithm));
                }

                return(engine);
            }
            catch (Exception e)
            {
                CryptoStatus.MoveToErrorStatus(new SelfTestFailedError("Exception on self test: " + e.Message, algorithm));
            }

            return(default(T));            // we'll never get this far
        }
Ejemplo n.º 4
0
        internal static T Validate <T>(Algorithm algorithm, T engine, VariantKatTest <T> test)
        {
            try
            {
                test.Evaluate(engine);

                return(engine);
            }
            catch (TestFailedException e)
            {
                CryptoStatus.MoveToErrorStatus(new SelfTestFailedError(e.Message, algorithm));
            }
            catch (Exception e)
            {
                CryptoStatus.MoveToErrorStatus(new SelfTestFailedError("Exception on self test: " + e.Message, algorithm));
            }

            return(default(T));            // we'll never get this far
        }
Ejemplo n.º 5
0
        internal static void Validate(Algorithm algorithm, VariantInternalKatTest test)
        {
            try
            {
                if (!algorithm.Equals(test.Algorithm))
                {
                    throw new TestFailedException("Inconsistent algorithm tag for " + algorithm);
                }

                test.Evaluate();
            }
            catch (TestFailedException e)
            {
                CryptoStatus.MoveToErrorStatus(new SelfTestFailedError(e.Message, algorithm));
            }
            catch (Exception e)
            {
                CryptoStatus.MoveToErrorStatus(new SelfTestFailedError("Exception on self test: " + e.Message, algorithm));
            }
        }
        public int Generate(byte[] output, byte[] additionalInput, bool predictionResistant)
        {
            if (CryptoStatus.IsErrorStatus())
            {
                throw new CryptoOperationError(CryptoStatus.GetStatusMessage());
            }

            lock (this)
            {
                int rv;

                if (block.Length != output.Length)
                {
                    if (block.Length < output.Length)
                    {
                        block     = new byte[GetTestBlockSize(output.Length)];
                        nextBlock = new byte[block.Length];

                        if (initialAdditionalInput != null)
                        {
                            rv = drbg.Generate(block, initialAdditionalInput, predictionResistant);
                            initialAdditionalInput = null;
                        }
                        else
                        {
                            rv = drbg.Generate(block, null, predictionResistant);
                        }

                        if (rv < 0)
                        {
                            CryptoStatus.MoveToErrorStatus("DRBG unable to initialise");
                        }
                    }
                    else if (block.Length != MIN_RESOLUTION)
                    {
                        byte[] tmp = new byte[GetTestBlockSize(output.Length)];

                        Array.Copy(block, block.Length - tmp.Length, tmp, 0, tmp.Length);

                        block     = tmp;
                        nextBlock = new byte[GetTestBlockSize(output.Length)];
                    }
                }

                rv = drbg.Generate(nextBlock, additionalInput, predictionResistant);
                if (rv < 0)
                {
                    return(rv);
                }

                // FSM_STATE:5.2, "CONTINUOUS DRBG TEST", "The module is performing Continuous DRBG self-test"
                // FSM_TRANS:5.3, "CONDITIONAL TEST", "CONTINUOUS DRBG TEST", "Invoke Continuous DRBG test"
                if (areEqual(block, nextBlock, 0))
                {
                    CryptoStatus.MoveToErrorStatus("Duplicate block detected in DRBG output");
                }
                // FSM_TRANS:5.4, "CONTINUOUS DRBG TEST", "CONDITIONAL TEST", "Continuous DRBG test successful"

                // note we only return output bytes to output array when we are sure there is no issue.
                Array.Copy(nextBlock, 0, output, 0, output.Length);
                Array.Copy(nextBlock, 0, block, 0, block.Length);
            }

            if (CryptoStatus.IsErrorStatus())
            {
                throw new CryptoOperationError(CryptoStatus.GetStatusMessage());
            }

            return(output.Length);
        }