Ejemplo n.º 1
0
        public static void Run()
        {
            var cipherText = Convert.FromBase64String(
                "yptyoDdVBdQtGhgoePppYHnWyugGmy0j81sf3zBeUXEO/LYRw+2XmVa0/v6YiSy9Kj8gMn/gNu2I7dPmfgSEHPUDJpNpiOWmmW1/jw/Pt29Are5tumWmnfkazcAb23xe7B4ruPZVxUEhfn/IrZPNZdr4cQNrHNgEv2ts8gVFuOBU+p792UPy8/mEIhW5ECppxGIb7Yrpg4w7IYNeFtX5d9W4W1t2e+6PcdcjkBK4a8y1cjEtuQ07RpPChOvLcSzlB/Bg7UKntzorRsn+y/d72qD2QxRzcXgbynCNalF7zaT6pEnwKB4i05fTQw6nB7SU1w2/EvCGlfiyR2Ia08mA0GikqegYA6xG/EAGs3ZJ0aQUGt0YZz0P7uBsQKdmCg7jzzEMHyGZDNGTj0F2dOFHLSOTT2/GGSht8eD/Ae7u/xnJj0bGgAKMtNttGFlNyvKpt2vDDT3Orfk6Jk/rD4CIz6O/Tnt0NkJLucHtIyvBYGtQR4+mhbfUELkczeDSxTXGDLaiU3de6tPaa0/vjzizoUbNFdfkIly/HWINdHoO83E=").AsSpan();
            var iv = Convert.FromBase64String("DkBbcmQo1QH+ed1wTyBynA==").AsSpan();

            var plaintext = new byte[cipherText.Length].AsSpan();
            var keyBuffer = new byte[32].AsSpan();
            var key       = AesKey.Create(keyBuffer);

            ref var a = ref keyBuffer[0];
Ejemplo n.º 2
0
        public static void BasicEncryptionCBC128Test()
        {
            byte[] key =
            {
                0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
                0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
            };

            byte[] iv =
            {
                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
            };

            byte[] plainText =
            {
                0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
                0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
                0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
                0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
                0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
                0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
                0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
                0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
            };

            byte[] cipherText =
            {
                0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
                0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
                0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
                0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
                0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b,
                0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
                0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09,
                0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7
            };

            byte[] output = new byte[cipherText.Length];

            AesKey k = AesKey.Create(key);

            AES.Encrypt(plainText, output, k, iv);

            Assert.AreEqual(cipherText, output);
        }
Ejemplo n.º 3
0
 public void TestKatVector(TestSet testSet)
 {
     if (testSet.Encrypt)
     {
         var acutal = new byte[testSet.Ciphertext.Length];
         Aes.Encrypt(testSet.Plaintext, acutal, testSet.Iv, AesKey.Create(testSet.Key), testSet.Mode,
                     PaddingMode.None);
         Assert.Equal(testSet.Ciphertext, acutal);
     }
     else
     {
         var acutal = new byte[testSet.Plaintext.Length];
         Aes.Decrypt(testSet.Ciphertext, acutal, testSet.Iv, AesKey.Create(testSet.Key), testSet.Mode,
                     PaddingMode.None);
         Assert.Equal(testSet.Plaintext, acutal);
     }
 }
Ejemplo n.º 4
0
        public static void Aes128KeyTest()
        {
            byte[] key =
            {
                0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
            };

            // todo improve this
            // these are in big endian so the bytes need to be reversed
            uint[] expandedKeyWords =
            {
                0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c,
                0xa0fafe17, 0x88542cb1, 0x23a33939, 0x2a6c7605,
                0xf2c295f2, 0x7a96b943, 0x5935807a, 0x7359f67f,
                0x3d80477d, 0x4716fe3e, 0x1e237e44, 0x6d7a883b,
                0xef44a541, 0xa8525b7f, 0xb671253b, 0xdb0bad00,
                0xd4d1c6f8, 0x7c839d87, 0xcaf2b8bc, 0x11f915bc,
                0x6d88a37a, 0x110b3efd, 0xdbf98641, 0xca0093fd,
                0x4e54f70e, 0x5f5fc9f3, 0x84a64fb2, 0x4ea6dc4f,
                0xead27321, 0xb58dbad2, 0x312bf560, 0x7f8d292f,
                0xac7766f3, 0x19fadc21, 0x28d12941, 0x575c006e,
                0xd014f9a8, 0xc9ee2589, 0xe13f0cc8, 0xb6630ca6
            };

            for (int i = 0; i < expandedKeyWords.Length; i++)
            {
                expandedKeyWords[i] = ByteUtils.ReverseBytes(expandedKeyWords[i]);
            }

            ReadOnlySpan <byte> expKey = MemoryMarshal.Cast <uint, byte>(expandedKeyWords);

            AesKey aesKey = AesKey.Create(key);
            ReadOnlySpan <byte> encryptionRoundKeys = aesKey.ExpandedKey.Slice(0, 11 * AesKey.BytesPerRoundKey);

            Assert.IsTrue(expKey.SequenceEqual(encryptionRoundKeys));
        }
Ejemplo n.º 5
0
        public void TestKatVector(TestSet testSet)
        {
            if (testSet.Encrypt)
            {
                var actualCt  = new byte[testSet.Ciphertext?.Length ?? 0];
                var actualTag = new byte[testSet.Tag.Length];
                Aes.Encrypt(testSet.Plaintext, actualCt, testSet.Iv, testSet.Aad, actualTag, AesKey.Create(testSet.Key));
                Assert.Equal(testSet.Ciphertext, actualCt);
                Assert.Equal(testSet.Tag, actualTag);
            }
            else
            {
                var actualPt = new byte[testSet.Plaintext?.Length ?? 0];

                var result = Aes.Decrypt(testSet.Ciphertext, actualPt, testSet.Iv, testSet.Aad, testSet.Tag, AesKey.Create(testSet.Key));

                Assert.Equal(testSet.Pass, result);
                if (result)
                {
                    Assert.Equal(testSet.Plaintext, actualPt);
                }
            }
        }