コード例 #1
0
        public override void PerformTest()
        {
            DrbgTestVector[] tests = CreateTestVectorData();
            ISP80090Drbg     d;
            DrbgTestVector   tv;

            byte[] output;

            for (int i = 0; i != tests.Length; i++)
            {
                tv = tests[i];

                byte[] nonce = tv.GetNonce();
                byte[] personalisationString = tv.GetPersonalizationString();

                d = new CtrSP800Drbg(tv.Cipher, tv.KeySizeInBits, tv.SecurityStrength, tv.EntropySource, personalisationString, nonce);

                output = new byte[tv.GetExpectedValue(0).Length];

                d.Generate(output, tv.GetAdditionalInput(0), tv.PredictionResistance);

                byte[] expected = tv.GetExpectedValue(0);

                if (!AreEqual(expected, output))
                {
                    Fail("Test #" + (i + 1) + ".1 failed, expected " + Hex.ToHexString(tv.GetExpectedValue(0)) + " got " + Hex.ToHexString(output));
                }

                output = new byte[tv.GetExpectedValue(0).Length];

                d.Generate(output, tv.GetAdditionalInput(1), tv.PredictionResistance);

                expected = tv.GetExpectedValue(1);
                if (!AreEqual(expected, output))
                {
                    Fail("Test #" + (i + 1) + ".2 failed, expected " + Hex.ToHexString(tv.GetExpectedValue(1)) + " got " + Hex.ToHexString(output));
                }
            }

            // DESede/TDEA key parity test
            tv = tests[0];

            ISP80090Drbg drbg = new CtrSP800Drbg(new KeyParityCipher(tv.Cipher), tv.KeySizeInBits, tv.SecurityStrength, tv.EntropySource,
                                                 tv.GetPersonalizationString(), tv.GetNonce());

            output = new byte[tv.GetExpectedValue(0).Length];

            drbg.Generate(output, tv.GetAdditionalInput(0), tv.PredictionResistance);

            // Exception tests
            try
            {
                d = new CtrSP800Drbg(new AesEngine(), 256, 256, new Bit232EntropyProvider().Get(128), null, null);
                Fail("no exception thrown");
            }
            catch (ArgumentException e)
            {
                if (!e.Message.Equals("Not enough entropy for security strength required"))
                {
                    Fail("Wrong exception", e);
                }
            }

            try
            {
                d = new CtrSP800Drbg(new DesEdeEngine(), 256, 256, new Bit232EntropyProvider().Get(232), null, null);
                Fail("no exception thrown");
            }
            catch (ArgumentException e)
            {
                if (!e.Message.Equals("Requested security strength is not supported by block cipher and key size"))
                {
                    Fail("Wrong exception", e);
                }
            }

            try
            {
                d = new CtrSP800Drbg(new DesEdeEngine(), 168, 256, new Bit232EntropyProvider().Get(232), null, null);
                Fail("no exception thrown");
            }
            catch (ArgumentException e)
            {
                if (!e.Message.Equals("Requested security strength is not supported by block cipher and key size"))
                {
                    Fail("Wrong exception", e);
                }
            }

            try
            {
                d = new CtrSP800Drbg(new AesEngine(), 192, 256, new Bit232EntropyProvider().Get(232), null, null);
                Fail("no exception thrown");
            }
            catch (ArgumentException e)
            {
                if (!e.Message.Equals("Requested security strength is not supported by block cipher and key size"))
                {
                    Fail("Wrong exception", e);
                }
            }
        }
コード例 #2
0
 private void ReinstantiateInternal()
 {
     drbgEngine = new CtrSP800Drbg(new AesEngine(), keySizeInBits: 256, securityStrength: 256, entropySource: fakeEntropy,
                                   personalizationString: null, nonce: null, withDerivationFuction: false);
 }