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)); }
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 !)); }
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); }
[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)); }
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); }
/// <summary> /// Returns the binary expression of this <see cref="FileMeta"/> like it will be sent over the internet. /// </summary> /// <param name="version"></param> /// <returns></returns> public byte[] GetBinaryData(ushort version) { using (PacketBuffer buf = PacketBuffer.CreateDynamic()) { if (version == 1) { Write_v1_1(buf); } else { if (Algorithm == ContentAlgorithm.None) { Write_v1_2_Header(buf); Write_v1_2_Core(buf); } else if (Algorithm == ContentAlgorithm.Aes256CbcHmacSha256) { Write_v1_2_Header(buf); // write header anyway because we always these data if (Available) { using (PacketBuffer ibuf = PacketBuffer.CreateDynamic()) { Write_v1_2_Core(ibuf); AesStatic.EncryptWithHmac(ibuf.ToArray(), buf, false, HmacKey, AesKey); } } else { buf.WriteByteArray(encryptedContent, false); // write all pre-read encrypted content including hmac, iv, etc. } } } return(buf.ToArray()); } }