コード例 #1
0
        private SymmetricCipherAeadResult Decrypt(IAeadModeBlockCipherParameters param)
        {
            var ecbParams = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                param.Key,
                new BitString(_engine.BlockSizeBits)
                );
            var ecb = _factory.GetStandardCipher(_engine, BlockCipherModesOfOperation.Ecb);
            var h   = ecb.ProcessPayload(ecbParams);
            var j0  = _gcmInternals.Getj0(h.Result, param.Iv);

            var plainText = _gcmInternals.GCTR(_gcmInternals.inc_s(32, j0), param.Payload, param.Key);

            int u = 128 * plainText.BitLength.CeilingDivide(128) - plainText.BitLength;
            int v = 128 * param.AdditionalAuthenticatedData.BitLength.CeilingDivide(128) - param.AdditionalAuthenticatedData.BitLength;

            var decryptedBits =
                param.AdditionalAuthenticatedData.ConcatenateBits(new BitString(v))
                .ConcatenateBits(param.Payload)
                .ConcatenateBits(new BitString(u))
                .ConcatenateBits(BitString.To64BitString(param.AdditionalAuthenticatedData.BitLength))
                .ConcatenateBits(BitString.To64BitString(param.Payload.BitLength));

            var s        = _gcmInternals.GHash(h.Result, decryptedBits);
            var tagPrime = _gcmInternals.GCTR(j0, s, param.Key).GetMostSignificantBits(param.Tag.BitLength);

            if (!param.Tag.Equals(tagPrime))
            {
                return(new SymmetricCipherAeadResult(INVALID_TAG_MESSAGE));
            }

            return(new SymmetricCipherAeadResult(plainText));
        }
コード例 #2
0
        protected override BitString Wrap(BitString K, BitString S, bool wrapWithInverseCipher)
        {
            // 0. Pre-conditions
            var n = S.BitLength / 32;

            if ((n < 3) || (S.BitLength % 32 != 0))
            {
                throw new ArgumentException($"Invalid {nameof(S)} length.");
            }

            var keylen = K.BitLength;
            var K2     = K.GetDeepCopy();
            //if (keylen == 192)
            //{
            //    // Convert to 168 bits because TDES Block encrypt takes 168 bits
            //    K2 = to168BitKey(K);
            //    keylen = K2.BitLength;
            //}
            //else
            //{
            //    K2 = K;
            //}
            //if (keylen != 168)
            //{
            //    throw new ArgumentException($"Invalid {nameof(keylen)}");
            //}

            // 1. Initialize variables
            // 1.a) Let s = 6(n-1)
            int s = 6 * (n - 1);
            // 1.b) Let S1,S2,...,Sn be the semi-blocks s.t. S=S1 || S2 || ... || Sn
            // 1.c) Let A0 = S1
            BitString A = S.GetMostSignificantBits(32);
            // 1.d) For i=2,...,n: let Ri0 = Si
            BitString R2n = S.GetLeastSignificantBits(S.BitLength - 32);

            // 2. Calculate the intermediate values.  For t = 1,...,s update variables
            //    as follows:
            for (int t = 1; t <= s; ++t)
            {
                // a) A^t = MSB32(CIPH_K(A^t-1 || R2^t-1)) xor [t]32
                BitString R2 = R2n.GetMostSignificantBits(32);

                //BitString t32 = to_bitstring32((unsigned long long)t);
                BitString t32 = BitString.To64BitString(t).GetLeastSignificantBits(32);

                var       param   = new ModeBlockCipherParameters(BlockCipherDirections.Encrypt, K2, A.ConcatenateBits(R2), wrapWithInverseCipher);
                BitString block_t = Cipher.ProcessPayload(param).Result;
                A = block_t.GetMostSignificantBits(32).XOR(t32);
                // b) For i=2,...,n: Ri^t = Ri+1^t-1
                // c) Rn^t = LSB32(CIPH_K(CIPH_K(A^t-1 || R2^t-1)))
                R2n = R2n.GetLeastSignificantBits(R2n.BitLength - 32).ConcatenateBits(block_t.GetLeastSignificantBits(32));
            }

            // 3. Output the results:
            // a) Let C1 = A^s
            // b) For i=2,...,n: Ci = Ri^s
            // c) Return C1 || C2 || ... || Cn
            return(A.ConcatenateBits(R2n));
        }
コード例 #3
0
ファイル: MCTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldMonteCarloTestEncrypt192BitKey()
        {
            BitString key       = new BitString("a8d4bbb2f90bd46df8c1d53f3ffc3c0448178985c9c2399d");
            BitString iv        = new BitString("e2144ab30656bbfe92b2eae5bbd6df26");
            BitString plainText = new BitString("f92a1ab2ddf68e13ca40baa9f638a4b9");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                iv,
                key,
                plainText
                );
            var result = _subject.ProcessMonteCarloTest(param);

            var firstExpectedCipherText = new BitString("b9d0ef07ececec4fe34b714b5ee5307d");
            var lastExpectedCipherText  = new BitString("1144065d566559e03276ae6cfa5540b5");

            var firstCipherText = result.Response[0].CipherText.ToHex();
            var lastCipherText  = result.Response[result.Response.Count - 1].CipherText.ToHex();

            Assert.AreEqual(firstExpectedCipherText.ToHex(), firstCipherText, nameof(firstCipherText));
            Assert.AreEqual(lastExpectedCipherText.ToHex(), lastCipherText, nameof(lastCipherText));

            Assert.IsTrue(result.Success);
        }
コード例 #4
0
ファイル: MCTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldMonteCarloTestDecrypt192BitKey()
        {
            BitString key        = new BitString("0f0805c4127c6f747d0a99ba9a233f371906b3c105e376aa");
            BitString iv         = new BitString("88e19bcb11e9d566a63de651cb5c1d2e");
            BitString cipherText = new BitString("03dcc1f22dc222d4b9c75a9f44a5850b");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                cipherText
                );
            var result = _subject.ProcessMonteCarloTest(param);

            var firstExpectedPlainText = new BitString("ff717172df5b344777aede8919f716b2");
            var lastExpectedPlainText  = new BitString("f3ae7bc4987f45a50d11c5a61e42cd79");

            var firstPlaintText = result.Response[0].PlainText.ToHex();
            var lastPlainText   = result.Response[result.Response.Count - 1].PlainText.ToHex();

            Assert.AreEqual(firstExpectedPlainText.ToHex(), firstPlaintText, nameof(firstExpectedPlainText));
            Assert.AreEqual(lastExpectedPlainText.ToHex(), lastPlainText, nameof(lastPlainText));

            Assert.IsTrue(result.Success);
        }
コード例 #5
0
ファイル: MCTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldMonteCarloTestDecrypt256BitKey()
        {
            BitString key        = new BitString("82f686042a40193035be076ea23e759dea24c4eb93b2c23b028621cb91642813");
            BitString iv         = new BitString("ec5acfa04ae86e2cb4c1d84d745cbbcb");
            BitString cipherText = new BitString("340a0a7715f8c9d46af32cfa526e5eb0");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                cipherText
                );
            var result = _subject.ProcessMonteCarloTest(param);

            var firstExpectedPlainText = new BitString("5ce78446e7a266fd9312ecb2011d2d9f");
            var lastExpectedPlainText  = new BitString("7f3df663c831a78f9931ad580d2a75df");

            var firstPlaintText = result.Response[0].PlainText.ToHex();
            var lastPlainText   = result.Response[result.Response.Count - 1].PlainText.ToHex();

            Assert.AreEqual(firstExpectedPlainText.ToHex(), firstPlaintText, nameof(firstExpectedPlainText));
            Assert.AreEqual(lastExpectedPlainText.ToHex(), lastPlainText, nameof(lastPlainText));

            Assert.IsTrue(result.Success);
        }
コード例 #6
0
ファイル: MCTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldMonteCarloTestEncrypt256BitKey()
        {
            BitString key       = new BitString("99be5720f51d234530a9fe6ec015a5fbd7da795588cfc8232bf94ec102a4fced");
            BitString iv        = new BitString("7981e5f1101f242eb55e17807a1cf6d4");
            BitString plainText = new BitString("9ac7e60acaf55dd9fbc31491789bdb94");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                iv,
                key,
                plainText
                );
            var result = _subject.ProcessMonteCarloTest(param);

            var firstExpectedCipherText = new BitString("17de51ae26223e52f85e898916ea6e60");
            var lastExpectedCipherText  = new BitString("35991f5535b05a332ed66ec17f4dd9e6");

            var firstCipherText = result.Response[0].CipherText.ToHex();
            var lastCipherText  = result.Response[result.Response.Count - 1].CipherText.ToHex();

            Assert.AreEqual(firstExpectedCipherText.ToHex(), firstCipherText, nameof(firstCipherText));
            Assert.AreEqual(lastExpectedCipherText.ToHex(), lastCipherText, nameof(lastCipherText));

            Assert.IsTrue(result.Success);
        }
コード例 #7
0
ファイル: MCTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldMonteCarloTestEncrypt256BitKey()
        {
            BitString key       = new BitString("498a4d6237eddcceeb2867b3e19475d3e3a538e6db70053ce66a07a3a973338b");
            BitString iv        = new BitString("252f3d42969df16b8da551260f4a23af");
            BitString plainText = new BitString("d9bc64269d31f9c6dc562a9b70a9483a");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                iv,
                key,
                plainText
                );
            var result = _subject.ProcessMonteCarloTest(param);

            var firstExpectedCipherText = new BitString("32a7689a0b5b628bbb749d3abc0189aa");
            var lastExpectedCipherText  = new BitString("10cd655c9034a064158f91634593472a");

            var firstCipherText = result.Response[0].CipherText.ToHex();
            var lastCipherText  = result.Response[result.Response.Count - 1].CipherText.ToHex();

            Assert.AreEqual(firstExpectedCipherText.ToHex(), firstCipherText, nameof(firstCipherText));
            Assert.AreEqual(lastExpectedCipherText.ToHex(), lastCipherText, nameof(lastCipherText));

            Assert.IsTrue(result.Success);
        }
コード例 #8
0
ファイル: MCTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldMonteCarloTestDecrypt256BitKey()
        {
            BitString key        = new BitString("c6d1ef08d4da7baf3dc77a9bfe307836ade554f4ba24a12bf60f4d26515c8075");
            BitString iv         = new BitString("e0c59e6838ae504fd03fcc885e2897d3");
            BitString cipherText = new BitString("7577a2ba16c466b2a7e9b1df32538e36");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                cipherText
                );
            var result = _subject.ProcessMonteCarloTest(param);

            var firstExpectedPlainText = new BitString("10024966f0e39a07473cd7ec85adad73");
            var lastExpectedPlainText  = new BitString("8b175090b4d6cd40e4e1ab04a7fc0098");

            var firstPlaintText = result.Response[0].PlainText.ToHex();
            var lastPlainText   = result.Response[result.Response.Count - 1].PlainText.ToHex();

            Assert.AreEqual(firstExpectedPlainText.ToHex(), firstPlaintText, nameof(firstExpectedPlainText));
            Assert.AreEqual(lastExpectedPlainText.ToHex(), lastPlainText, nameof(lastPlainText));

            Assert.IsTrue(result.Success);
        }
コード例 #9
0
ファイル: MCTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldMonteCarloTestEncrypt192BitKey()
        {
            BitString key       = new BitString("4d647b53e1a7f64fe00cde9362df9febd071905dec101db8");
            BitString iv        = new BitString("ba5b5ecf7a33ced8957e56457d789158");
            BitString plainText = new BitString("62662f85e743969bfd317830d3371395");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                iv,
                key,
                plainText
                );
            var result = _subject.ProcessMonteCarloTest(param);

            var firstExpectedCipherText = new BitString("a87a94f4791bef0a345ebe92d935531e");
            var lastExpectedCipherText  = new BitString("7cec0fea1800b246db28832620f7d5ad");

            var firstCipherText = result.Response[0].CipherText.ToHex();
            var lastCipherText  = result.Response[result.Response.Count - 1].CipherText.ToHex();

            Assert.AreEqual(firstExpectedCipherText.ToHex(), firstCipherText, nameof(firstCipherText));
            Assert.AreEqual(lastExpectedCipherText.ToHex(), lastCipherText, nameof(lastCipherText));

            Assert.IsTrue(result.Success);
        }
コード例 #10
0
ファイル: MCTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldMonteCarloTestDecrypt128BitKey()
        {
            BitString key        = new BitString("40212e6752be1b903e0185002eb7128c");
            BitString iv         = new BitString("c1bd1dede054c4fede03f3f6a5e4f547");
            BitString cipherText = new BitString("945860fcffa51451ed55d8ca6d4ad67d");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                cipherText
                );
            var result = _subject.ProcessMonteCarloTest(param);

            var firstExpectedPlainText = new BitString("e0183c620a21991788afaa3fb915de9d");
            var lastExpectedPlainText  = new BitString("0d097f9b5641f5264dfb1143f15fe10f");

            var firstPlaintText = result.Response[0].PlainText.ToHex();
            var lastPlainText   = result.Response[result.Response.Count - 1].PlainText.ToHex();

            Assert.AreEqual(firstExpectedPlainText.ToHex(), firstPlaintText, nameof(firstExpectedPlainText));
            Assert.AreEqual(lastExpectedPlainText.ToHex(), lastPlainText, nameof(lastPlainText));

            Assert.IsTrue(result.Success);
        }
コード例 #11
0
ファイル: MCTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldMonteCarloTestDecrypt192BitKey()
        {
            BitString key        = new BitString("3832711e0dcb9fe4073d6a68129536c5ee64e3b482c2b7ae");
            BitString iv         = new BitString("a4493c2caf1e1bb3ed2be2de37364851");
            BitString cipherText = new BitString("33b27eb00d0b07a0dc0132332461af2a");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                cipherText
                );
            var result = _subject.ProcessMonteCarloTest(param);

            var firstExpectedPlainText = new BitString("d09df30f71e25d9b9611152407d9f1aa");
            var lastExpectedPlainText  = new BitString("6f616ceec053d3cfb029d3d3f7ececb9");

            var firstPlaintText = result.Response[0].PlainText.ToHex();
            var lastPlainText   = result.Response[result.Response.Count - 1].PlainText.ToHex();

            Assert.AreEqual(firstExpectedPlainText.ToHex(), firstPlaintText, nameof(firstExpectedPlainText));
            Assert.AreEqual(lastExpectedPlainText.ToHex(), lastPlainText, nameof(lastPlainText));

            Assert.IsTrue(result.Success);
        }
コード例 #12
0
        public void DecryptTestsCs2(string label, BitString key, BitString iv, BitString pt, BitString ct, BitString nextIv)
        {
            // transform the cs3 ct to cs3 again (to cancel the sample vector CS3)
            var ctBytes        = BitString.PadToModulus(ct, _engine.BlockSizeBits).ToBytes();
            var numberOfBlocks = GetNumberOfBlocks(pt.BitLength);
            var cs3Transformer = _csTransform.Get(CiphertextStealingMode.CS3);

            cs3Transformer.TransformCiphertext(ctBytes, _engine, numberOfBlocks, pt.BitLength);

            // apply a cs2 transform
            var cs2Transformer = _csTransform.Get(CiphertextStealingMode.CS2);

            cs2Transformer.TransformCiphertext(ctBytes, _engine, numberOfBlocks, pt.BitLength);
            ct = new BitString(ctBytes).GetMostSignificantBits(pt.BitLength);

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                ct
                );
            var result = _subjectCs2.ProcessPayload(param);

            Assert.AreEqual(pt.ToHex(), result.Result.ToHex(), nameof(pt));
            Assert.AreEqual(nextIv.ToHex(), param.Iv.ToHex(), nameof(nextIv));
        }
コード例 #13
0
        private SymmetricCipherAeadResult Encrypt(IAeadModeBlockCipherParameters param)
        {
            var ecbParams = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                param.Key,
                new BitString(_engine.BlockSizeBits)
                );
            var ecb = _factory.GetStandardCipher(_engine, BlockCipherModesOfOperation.Ecb);
            var h   = ecb.ProcessPayload(ecbParams);

            var j0         = _gcmInternals.Getj0(h.Result, param.Iv);
            var cipherText = _gcmInternals.GCTR(_gcmInternals.inc_s(32, j0), param.Payload, param.Key);
            int u          = _engine.BlockSizeBits * cipherText.BitLength.CeilingDivide(_engine.BlockSizeBits) -
                             cipherText.BitLength;
            int v = _engine.BlockSizeBits *
                    param.AdditionalAuthenticatedData.BitLength.CeilingDivide(_engine.BlockSizeBits) -
                    param.AdditionalAuthenticatedData.BitLength;
            var encryptedBits =
                param.AdditionalAuthenticatedData.ConcatenateBits(new BitString(v))
                .ConcatenateBits(cipherText)
                .ConcatenateBits(new BitString(u))
                .ConcatenateBits(BitString.To64BitString(param.AdditionalAuthenticatedData.BitLength))
                .ConcatenateBits(BitString.To64BitString(cipherText.BitLength));
            var s   = _gcmInternals.GHash(h.Result, encryptedBits);
            var tag = _gcmInternals.GCTR(j0, s, param.Key).GetMostSignificantBits(param.TagLength);

            return(new SymmetricCipherAeadResult(cipherText, tag));
        }
コード例 #14
0
ファイル: MCTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldMonteCarloTestDecrypt128BitKey()
        {
            BitString key        = new BitString("fcd8afd687755a906dcb7fed331aeecc");
            BitString iv         = new BitString("b57f3da4ba033569a3e4fd0ff36b4bc6");
            BitString cipherText = new BitString("a759240ea1683e275d5b341f2c0dfe5b");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                cipherText
                );
            var result = _subject.ProcessMonteCarloTest(param);

            var firstExpectedPlainText = new BitString("ad4468da8bb9fa9fe4c8a8b395f262e8");
            var lastExpectedPlainText  = new BitString("6d4b5b96d9bae9a2ac0aac92ee666a20");

            var firstPlaintText = result.Response[0].PlainText.ToHex();
            var lastPlainText   = result.Response[result.Response.Count - 1].PlainText.ToHex();

            Assert.AreEqual(firstExpectedPlainText.ToHex(), firstPlaintText, nameof(firstExpectedPlainText));
            Assert.AreEqual(lastExpectedPlainText.ToHex(), lastPlainText, nameof(lastPlainText));

            Assert.IsTrue(result.Success);
        }
コード例 #15
0
        protected override BitString WrapInverse(BitString K, BitString C, bool wrappedWithInverseCipher)
        {
            // 0. Pre-conditions
            int n = C.BitLength / 32;

            if ((n < 3) || (C.BitLength % 32 != 0))
            {
                throw new ArgumentException($"Invalid {nameof(C)} length.");
            }
            int keyLength = K.BitLength;
            var K2        = K.GetDeepCopy();
            //if (keyLength == 192)
            //{
            //    // Convert to 168 bits because TDES Block encrypt takes 168 bits
            //    K2 = to168BitKey(K);
            //    keyLength = K2.BitLength;
            //}
            //else
            //{
            //    K2 = K;
            //}
            //if (keyLength != 168)
            //{
            //    throw new ArgumentException($"Invalid {nameof(keyLength)}");
            //}

            // 1. Initialize variables
            // 1.a) Let s = 6(n-1)
            int s = 6 * (n - 1);
            // 1.b) Let C1,C2,...,Cn be the semi-blocks s.t. C=C1 || C2 || ... || Cn
            // 1.c) Let As = C1
            BitString A = C.GetMostSignificantBits(32);
            // 1.d) For i=2,...,n: let Ri0 = Ci
            BitString R2n = C.GetLeastSignificantBits(C.BitLength - 32);


            // 2. Calculate the intermediate values.  For t = s, s-1, ..., 1,
            //    update the variables as follows:
            for (int t = s; t >= 1; --t)
            {
                // a) A^t-1 = MSB(CIPH^-1K((A^t xor [t]32) || Rn^t))
                BitString t32 = BitString.To64BitString(t).GetLeastSignificantBits(32);
                BitString Rn  = R2n.GetLeastSignificantBits(32);

                var       param   = new ModeBlockCipherParameters(BlockCipherDirections.Decrypt, K2, A.XOR(t32).ConcatenateBits(Rn), wrappedWithInverseCipher);
                BitString block_t = Cipher.ProcessPayload(param).Result;
                A = block_t.GetMostSignificantBits(32);
                // b) R2^t-1 = LSB(CIPH^-1K((A^t xor [t]32) || Rn^t))
                // c) For i=2,...,n-1, Ri+1^t-1 = Ri^t
                R2n = block_t.GetLeastSignificantBits(32).ConcatenateBits(R2n.GetMostSignificantBits(R2n.BitLength - 32));
            }

            // 3. Output the results:
            // 3.a) Let S1 = A0
            // 3.b) For i=2,...,n: Si = Ri0
            // 3.c) Return S1 || S2 || ... || Sn
            return(A.ConcatenateBits(R2n));
        }
コード例 #16
0
        public void ShouldEncryptWith192BitKey()
        {
            byte[]    keyBytes = new byte[192 / 8];
            BitString keyBits  = new BitString(keyBytes);

            byte[] plainText = new byte[16];

            // PT
            plainText[0]  = 0x1b;
            plainText[1]  = 0x07;
            plainText[2]  = 0x7a;
            plainText[3]  = 0x6a;
            plainText[4]  = 0xf4;
            plainText[5]  = 0xb7;
            plainText[6]  = 0xf9;
            plainText[7]  = 0x82;
            plainText[8]  = 0x29;
            plainText[9]  = 0xde;
            plainText[10] = 0x78;
            plainText[11] = 0x6d;
            plainText[12] = 0x75;
            plainText[13] = 0x16;
            plainText[14] = 0xb6;
            plainText[15] = 0x39;

            BitString plainTextBits = new BitString(plainText);

            var param            = new ModeBlockCipherParameters(BlockCipherDirections.Encrypt, keyBits, plainTextBits);
            var encryptOperation = _subject.ProcessPayload(param);

            Assert.IsTrue(encryptOperation.Success, nameof(encryptOperation));

            var ct = encryptOperation.Result.ToBytes();

            if ((ct[0] != 0x27) || (ct[1] != 0x5c) || (ct[2] != 0xfc) || (ct[3] != 0x04) ||
                (ct[4] != 0x13) || (ct[5] != 0xd8) || (ct[6] != 0xcc) || (ct[7] != 0xb7) ||
                (ct[8] != 0x05) || (ct[9] != 0x13) || (ct[10] != 0xc3) || (ct[11] != 0x85) ||
                (ct[12] != 0x9b) || (ct[13] != 0x1d) || (ct[14] != 0x0f) || (ct[15] != 0x72))
            {
                Assert.Fail("Invalid cipher");
            }

            // Do the decryption to ensure we arrive back at plain text
            var param2           = new ModeBlockCipherParameters(BlockCipherDirections.Decrypt, keyBits, encryptOperation.Result);
            var decryptOperation = _subject.ProcessPayload(param2);
            var pt = decryptOperation.Result.ToBytes();

            for (int i = 0; i < pt.Length; i++)
            {
                Assert.AreEqual(plainText[i], pt[i], $"Failed on index {i}");
            }

            Assert.Pass();
        }
コード例 #17
0
        public void ShouldEncryptWith256BitKey()
        {
            byte[]    keyBytes = new byte[256 / 8];
            BitString keyBits  = new BitString(keyBytes);

            byte[] plainText = new byte[16];

            plainText[0]  = 0x01;
            plainText[1]  = 0x47;
            plainText[2]  = 0x30;
            plainText[3]  = 0xf8;
            plainText[4]  = 0x0a;
            plainText[5]  = 0xc6;
            plainText[6]  = 0x25;
            plainText[7]  = 0xfe;
            plainText[8]  = 0x84;
            plainText[9]  = 0xf0;
            plainText[10] = 0x26;
            plainText[11] = 0xc6;
            plainText[12] = 0x0b;
            plainText[13] = 0xfd;
            plainText[14] = 0x54;
            plainText[15] = 0x7d;

            BitString plainTextBits = new BitString(plainText);

            var param            = new ModeBlockCipherParameters(BlockCipherDirections.Encrypt, keyBits, plainTextBits);
            var encryptOperation = _subject.ProcessPayload(param);

            Assert.IsTrue(encryptOperation.Success, nameof(encryptOperation));

            var ct = encryptOperation.Result.ToBytes();

            if ((ct[0] != 0x5c) || (ct[1] != 0x9d) || (ct[2] != 0x84) || (ct[3] != 0x4e) ||
                (ct[4] != 0xd4) || (ct[5] != 0x6f) || (ct[6] != 0x98) || (ct[7] != 0x85) ||
                (ct[8] != 0x08) || (ct[9] != 0x5e) || (ct[10] != 0x5d) || (ct[11] != 0x6a) ||
                (ct[12] != 0x4f) || (ct[13] != 0x94) || (ct[14] != 0xc7) || (ct[15] != 0xd7))
            {
                Assert.Fail("Invalid cipher");
            }

            // Do the decryption to ensure we arrive back at plain text
            var param2           = new ModeBlockCipherParameters(BlockCipherDirections.Decrypt, keyBits, encryptOperation.Result);
            var decryptOperation = _subject.ProcessPayload(param2);
            var pt = decryptOperation.Result.ToBytes();

            for (int i = 0; i < pt.Length; i++)
            {
                Assert.AreEqual(plainText[i], pt[i], $"Failed on index {i}");
            }

            Assert.Pass();
        }
コード例 #18
0
ファイル: KATs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldVarKeyCorrectlyNewEngine(string expectedCipherText, AlgoArrayResponse algoArrayResponse)
        {
            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                algoArrayResponse.Key,
                algoArrayResponse.PlainText
                );
            var result = _newSubject.ProcessPayload(param);

            Assert.IsTrue(result.Success, nameof(result.Success));
            Assert.AreEqual(algoArrayResponse.CipherText, result.Result, nameof(algoArrayResponse.CipherText));
        }
コード例 #19
0
        public void ShouldEncryptWith128BitKey()
        {
            byte[]    keyBytes = new byte[128 / 8];
            BitString keyBits  = new BitString(keyBytes);

            byte[] plainText = new byte[16];

            plainText[0]  = 0xf3;
            plainText[1]  = 0x44;
            plainText[2]  = 0x81;
            plainText[3]  = 0xec;
            plainText[4]  = 0x3c;
            plainText[5]  = 0xc6;
            plainText[6]  = 0x27;
            plainText[7]  = 0xba;
            plainText[8]  = 0xcd;
            plainText[9]  = 0x5d;
            plainText[10] = 0xc3;
            plainText[11] = 0xfb;
            plainText[12] = 0x08;
            plainText[13] = 0xf2;
            plainText[14] = 0x73;
            plainText[15] = 0xe6;

            BitString plainTextBits = new BitString(plainText);

            var param            = new ModeBlockCipherParameters(BlockCipherDirections.Encrypt, keyBits, plainTextBits);
            var encryptOperation = _subject.ProcessPayload(param);

            Assert.IsTrue(encryptOperation.Success, nameof(encryptOperation));
            var ct = encryptOperation.Result.ToBytes();

            if ((ct[0] != 0x03) || (ct[1] != 0x36) || (ct[2] != 0x76) || (ct[3] != 0x3e) ||
                (ct[4] != 0x96) || (ct[5] != 0x6d) || (ct[6] != 0x92) || (ct[7] != 0x59) ||
                (ct[8] != 0x5a) || (ct[9] != 0x56) || (ct[10] != 0x7c) || (ct[11] != 0xc9) ||
                (ct[12] != 0xce) || (ct[13] != 0x53) || (ct[14] != 0x7f) || (ct[15] != 0x5e))
            {
                Assert.Fail("Invalid cipher");
            }

            // Do the decryption to ensure we arrive back at plain text
            var param2           = new ModeBlockCipherParameters(BlockCipherDirections.Decrypt, keyBits, encryptOperation.Result);
            var decryptOperation = _subject.ProcessPayload(param2);
            var pt = decryptOperation.Result.ToBytes();

            for (int i = 0; i < pt.Length; i++)
            {
                Assert.AreEqual(plainText[i], pt[i], $"Failed on index {i}");
            }

            Assert.Pass();
        }
コード例 #20
0
        public void DecryptTestsCs3(string label, BitString key, BitString iv, BitString pt, BitString ct, BitString nextIv)
        {
            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                ct
                );
            var result = _subjectCs3.ProcessPayload(param);

            Assert.AreEqual(pt.ToHex(), result.Result.ToHex(), nameof(pt));
            Assert.AreEqual(nextIv.ToHex(), param.Iv.ToHex(), nameof(nextIv));
        }
コード例 #21
0
ファイル: KATs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldKeySboxCorrectlyDecrypt(string expectedText, AlgoArrayResponse algoArrayResponse)
        {
            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                algoArrayResponse.IV,
                algoArrayResponse.Key,
                algoArrayResponse.CipherText
                );
            var result = _subject.ProcessPayload(param);

            Assert.IsTrue(result.Success, nameof(result.Success));
            Assert.AreEqual(algoArrayResponse.PlainText, result.Result, nameof(algoArrayResponse.CipherText));
        }
コード例 #22
0
        public BitString Prf(BitString x, BitString key)
        {
            // This method runs cbc mac on the full block string against the key
            var engine = _engineFactory.GetSymmetricCipherPrimitive(BlockCipherEngines.Aes);
            var mode   = _modeFactory.GetStandardCipher(engine, BlockCipherModesOfOperation.CbcMac);

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                new BitString(engine.BlockSizeBits),
                key,
                x
                );

            return(mode.ProcessPayload(param).Result);
        }
コード例 #23
0
ファイル: MMTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldCorrectlyMMTDecrypt256()
        {
            BitString key               = new BitString("9c45cf50619e071ed5e97238b8f575fe335328bbbeb05a904162d5ae7940e2c8");
            BitString iv                = new BitString("2f53d242194fea139f17c91209dfa13d");
            BitString cipherText        = new BitString("b2520a85db76ffc6fe9d");
            BitString expectedPlainText = new BitString("2747e1ab03aae6fcc9c6");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                cipherText
                );
            var result = _subject.ProcessPayload(param);

            Assert.AreEqual(expectedPlainText, result.Result);
        }
コード例 #24
0
ファイル: KATs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldVarTxtCorrectlyWithEachKeySize(int keyLen)
        {
            foreach (var test in KatDataCtr.GetVarTxt(keyLen))
            {
                _mockCounter.Setup(s => s.GetNextIV()).Returns(test.IV);

                var param = new ModeBlockCipherParameters(
                    BlockCipherDirections.Encrypt,
                    test.Key,
                    test.PlainText
                    );
                var result = _subject.ProcessPayload(param);

                Assert.IsTrue(result.Success, nameof(result.Success));
                Assert.AreEqual(test.CipherText, result.Result, test.CipherText.ToHex());
            }
        }
コード例 #25
0
ファイル: MMTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldCorrectlyMMTDecrypt192()
        {
            BitString key               = new BitString("f28f1db3de8933a89fa6fd29c1918ad9eba6d59711569b00");
            BitString iv                = new BitString("f406c129d611cdbe32eca63117c1e199");
            BitString cipherText        = new BitString("2a5169adacf924a3922b");
            BitString expectedPlainText = new BitString("d1385049b0ed2be53528");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                cipherText
                );
            var result = _subject.ProcessPayload(param);

            Assert.AreEqual(expectedPlainText, result.Result);
        }
コード例 #26
0
ファイル: MMTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldCorrectlyMMTEncrypt192()
        {
            BitString key                = new BitString("0a3d6df4cbedc20898ab590e64ede2c6810d5ffd14ee3356");
            BitString iv                 = new BitString("f37bbc1994a365fe86231f65d5eaeadb");
            BitString plainText          = new BitString("7ef7b0c9a0e23b8690e1");
            BitString expectedCipherText = new BitString("12d5726fd26bb1ec01a9");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                iv,
                key,
                plainText
                );
            var result = _subject.ProcessPayload(param);

            Assert.AreEqual(expectedCipherText, result.Result);
        }
コード例 #27
0
ファイル: MMTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldCorrectlyMMTEncrypt256()
        {
            BitString key                = new BitString("49d76c1b3621a091e32d7c39712ebafcf966e54114970603a1bb8026c2f4390b");
            BitString iv                 = new BitString("91a58f90be53223fbe70bae9a44db291");
            BitString plainText          = new BitString("957b955599316d1d919c");
            BitString expectedCipherText = new BitString("f991b412bf221b6e878c");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                iv,
                key,
                plainText
                );
            var result = _subject.ProcessPayload(param);

            Assert.AreEqual(expectedCipherText, result.Result);
        }
コード例 #28
0
ファイル: MMTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldCorrectlyMMTDecrypt128()
        {
            BitString key               = new BitString("81d78365438bbb00e7807546f6ee99d1");
            BitString iv                = new BitString("d27d153413f24ffba2db18589ee6319c");
            BitString cipherText        = new BitString("643644bbe279795c7c73");
            BitString expectedPlainText = new BitString("4f34dba6219cc94d86a8");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                iv,
                key,
                cipherText
                );
            var result = _subject.ProcessPayload(param);

            Assert.AreEqual(expectedPlainText, result.Result);
        }
コード例 #29
0
        public void ShouldReturnEncrypResponseWith100Count(int keySize, BlockCipherDirections direction)
        {
            BitString iv      = new BitString(128);
            BitString key     = new BitString(keySize);
            BitString payload = new BitString(128);

            var p = new ModeBlockCipherParameters(
                direction,
                iv,
                key,
                payload
                );

            var result = _subject.ProcessMonteCarloTest(p);

            Assert.AreEqual(100, result.Response.Count);
        }
コード例 #30
0
ファイル: MMTs.cs プロジェクト: usnistgov/ACVP-Server
        public void ShouldCorrectlyMMTEncrypt128()
        {
            BitString key                = new BitString("811747ce1fea043a39c342625ec52b1a");
            BitString iv                 = new BitString("e09c8d3be53be9bdd63f99b70f630149");
            BitString plainText          = new BitString("eb0a89a9e21abb266eaf");
            BitString expectedCipherText = new BitString("751c5e72da27a3a616f4");

            var param = new ModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                iv,
                key,
                plainText
                );
            var result = _subject.ProcessPayload(param);

            Assert.AreEqual(expectedCipherText, result.Result);
        }