Пример #1
0
        public void TestInsufficientData()
        {
            byte[] buffer  = new byte[16];
            byte[] hmacKey = new byte[32];
            byte[] aesKey  = new byte[32];

            Assert.ThrowsException <ArgumentException>(() => AesStatic.DecryptWithHmac(buffer, hmacKey, aesKey));
        }
Пример #2
0
        public void TestIllegalKeySize()
        {
            byte[] buffer   = new byte[64];
            byte[] rightKey = new byte[32];
            byte[] wrongKey = new byte[16];

            Assert.ThrowsException <ArgumentException>(() => AesStatic.EncryptWithHmac(buffer, wrongKey, rightKey));
            Assert.ThrowsException <ArgumentException>(() => AesStatic.EncryptWithHmac(buffer, rightKey, wrongKey));
            Assert.ThrowsException <ArgumentException>(() => AesStatic.DecryptWithHmac(buffer, wrongKey, rightKey));
            Assert.ThrowsException <ArgumentException>(() => AesStatic.DecryptWithHmac(buffer, rightKey, wrongKey));
        }
Пример #3
0
        public void TestNullKeys()
        {
            byte[] buffer  = new byte[64];
            byte[] hmacKey = new byte[32];
            byte[] aesKey  = new byte[32];

            Assert.ThrowsException <ArgumentNullException>(() => AesStatic.EncryptWithHmac(buffer, null !, aesKey));
            Assert.ThrowsException <ArgumentNullException>(() => AesStatic.EncryptWithHmac(buffer, hmacKey, null !));
            Assert.ThrowsException <ArgumentNullException>(() => AesStatic.DecryptWithHmac(buffer, null !, aesKey));
            Assert.ThrowsException <ArgumentNullException>(() => AesStatic.DecryptWithHmac(buffer, hmacKey, null !));
        }
Пример #4
0
        public void TestEncryptDecrypt(string text)
        {
            byte[] hmacKey = new byte[32];
            byte[] aesKey  = new byte[32];
            RandomNumberGenerator.Fill(hmacKey);
            RandomNumberGenerator.Fill(aesKey);

            ReadOnlyMemory <byte> data       = Encoding.UTF8.GetBytes(text);
            ReadOnlyMemory <byte> ciphertext = AesStatic.EncryptWithHmac(data, hmacKey, aesKey);
            ReadOnlyMemory <byte> plaintext  = AesStatic.DecryptWithHmac(ciphertext, hmacKey, aesKey);

            MemoryAssert.AreEqual(data, plaintext);
        }
Пример #5
0
        [DataRow(LoremIpsum, 93)] // tamper later block
        public void TestIntegrityCheck(string text, int tamperIndex)
        {
            byte[] hmacKey = new byte[32];
            byte[] aesKey  = new byte[32];
            RandomNumberGenerator.Fill(hmacKey);
            RandomNumberGenerator.Fill(aesKey);

            ReadOnlyMemory <byte> data       = Encoding.UTF8.GetBytes(text);
            Memory <byte>         ciphertext = MemoryMarshal.AsMemory(AesStatic.EncryptWithHmac(data, hmacKey, aesKey));

            ciphertext.Span[tamperIndex] = (byte)~ciphertext.Span[tamperIndex];

            Assert.ThrowsException <CryptographicException>(() => AesStatic.DecryptWithHmac(ciphertext, hmacKey, aesKey));
        }
Пример #6
0
        public void TestAesHmac()
        {
            byte[] hmac = AesStatic.GenerateKey();
            byte[] key  = AesStatic.GenerateKey();
            byte[] iv   = AesStatic.GenerateIV();

            Random random = new Random();

            byte[] plaintext = new byte[69854];
            random.NextBytes(plaintext);

            PacketBuffer ciphertext = PacketBuffer.CreateDynamic();

            AesStatic.EncryptWithHmac(plaintext, ciphertext, false, hmac, key);
            ciphertext.Position = 0;
            byte[] result = AesStatic.DecryptWithHmac(ciphertext, -1, hmac, key);

            CollectionAssert.AreEqual(plaintext, result);
        }
Пример #7
0
 private void Read_v1_2(PacketBuffer buf, byte[] hmacKey, byte[] aesKey)
 {
     Read_v1_2_Header(buf);
     if (Algorithm == ContentAlgorithm.None)
     {
         Read_v1_2_Core(buf);
     }
     else if (Algorithm == ContentAlgorithm.Aes256CbcHmacSha256)
     {
         byte[] plain = AesStatic.DecryptWithHmac(buf, -1, hmacKey, aesKey);
         HmacKey = hmacKey; // Assign public properties after successful decryption
         AesKey  = aesKey;
         using (PacketBuffer innerBuf = PacketBuffer.CreateStatic(plain))
             Read_v1_2_Core(innerBuf);
         Available = true;
     }
     else
     {
         encryptedContent = buf.ReadByteArray(buf.Pending);
     }
 }