public static void TestHmacFromSimpleFile() { DataHmac expectedHmac = new DataHmac(new byte[] { 0xF9, 0xAF, 0x2E, 0x67, 0x7D, 0xCF, 0xC9, 0xFE, 0x06, 0x4B, 0x39, 0x08, 0xE7, 0x5A, 0x87, 0x81 }); using (AxCryptDocument document = new AxCryptDocument()) { Passphrase passphrase = new Passphrase("a"); bool keyIsOk = document.Load(FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt), passphrase.DerivedPassphrase); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); DataHmac hmac = document.DocumentHeaders.Hmac; Assert.That(hmac.GetBytes(), Is.EqualTo(expectedHmac.GetBytes()), "Wrong HMAC"); } }
public static void TestInvalidArguments() { DataHmac hmac = null; Assert.Throws <ArgumentNullException>(() => { hmac = new DataHmac(null); }); Assert.Throws <InternalErrorException>(() => { hmac = new DataHmac(new byte[20]); }); // Use the instance to avoid FxCop errors. Object.Equals(hmac, null); }
public static void TestHmac() { using (Stream inputStream = FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { using (AxCryptReader axCryptReader = new AxCryptStreamReader(inputStream)) { Assert.Throws <InvalidOperationException>(() => { if (axCryptReader.Hmac == null) { } }, "The reader is not positioned properly to get the HMAC."); Passphrase passphrase = new Passphrase("a"); DocumentHeaders documentHeaders = new DocumentHeaders(passphrase.DerivedPassphrase); bool keyIsOk = documentHeaders.Load(axCryptReader); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); using (Stream encrypedDataStream = axCryptReader.CreateEncryptedDataStream(documentHeaders.HmacSubkey.Key, documentHeaders.CipherTextLength, new ProgressContext())) { Assert.Throws <InvalidOperationException>(() => { if (axCryptReader.Hmac == null) { } }, "We have not read the encrypted data yet."); Assert.That(axCryptReader.Read(), Is.False, "The reader should be at end of stream now, and Read() should return false."); encrypedDataStream.CopyTo(Stream.Null, 4096); Assert.That(documentHeaders.Hmac, Is.EqualTo(axCryptReader.Hmac), "The HMAC should be correct."); axCryptReader.Dispose(); Assert.Throws <ObjectDisposedException>(() => { DataHmac disposedHmac = axCryptReader.Hmac; Object.Equals(disposedHmac, null); }, "The reader is disposed."); } } } }
public static void TestMethods() { DataHmac hmac = new DataHmac(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }); Assert.That(hmac.Length, Is.EqualTo(16), "The length should always be 16."); Assert.That(hmac.GetBytes(), Is.EquivalentTo(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }), "Check that GetBytes() returns the expected."); Assert.That(hmac, Is.EqualTo(new DataHmac(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 })), "Check Equals() override."); Assert.That(hmac == new DataHmac(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }), Is.True, "Check operator== override."); Assert.That(hmac != new DataHmac(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }), Is.False, "Check operator!= override."); DataHmac hmac2 = new DataHmac(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }); Assert.That(hmac.GetHashCode(), Is.Not.EqualTo(0), "The hash code should not be zero (can be, but it's not in this case)."); Assert.That(hmac.GetHashCode(), Is.EqualTo(hmac2.GetHashCode()), "The hash code for two different instances with same value should be the same."); Assert.That(hmac.Equals(null), Is.False, "An instance is never equal to null."); Assert.That(hmac.Equals(new object()), Is.False, "An instance is never equal to another instance of a differing type."); DataHmac hmacSynonym = hmac; Assert.That(hmac == hmacSynonym, Is.True, "These should compare equal via reference equality."); }
public static void TestHmacStream() { Assert.Throws <ArgumentNullException>(() => { using (HmacStream hmacStream = new HmacStream(null)) { } }); AesKey key = new AesKey(new byte[16]); using (HmacStream hmacStream = new HmacStream(key)) { Assert.That(hmacStream.CanRead, Is.False, "HmacStream does not support reading."); Assert.That(hmacStream.CanSeek, Is.False, "HmacStream does not support seeking."); Assert.That(hmacStream.CanWrite, Is.True, "HmacStream does support writing."); Assert.Throws <NotSupportedException>(() => { byte[] buffer = new byte[5]; hmacStream.Read(buffer, 0, buffer.Length); }); Assert.Throws <NotSupportedException>(() => { hmacStream.Seek(0, SeekOrigin.Begin); }); Assert.Throws <NotSupportedException>(() => { hmacStream.SetLength(0); }); Assert.Throws <ArgumentNullException>(() => { hmacStream.ReadFrom(null); }); hmacStream.Write(new byte[10], 0, 10); using (Stream dataStream = new MemoryStream()) { dataStream.Write(new byte[10], 0, 10); dataStream.Position = 0; hmacStream.ReadFrom(dataStream); } Assert.That(hmacStream.Position, Is.EqualTo(20), "There are 20 bytes written so the position should be 20."); Assert.That(hmacStream.Length, Is.EqualTo(20), "There are 20 bytes written so the position should be 20."); hmacStream.Flush(); Assert.That(hmacStream.Position, Is.EqualTo(20), "Nothing should change after Flush(), this is not a buffered stream."); Assert.That(hmacStream.Length, Is.EqualTo(20), "Nothing should change after Flush(), this is not a buffered stream."); Assert.Throws <NotSupportedException>(() => { hmacStream.Position = 0; }, "Position is not supported."); DataHmac dataHmac = hmacStream.HmacResult; Assert.That(dataHmac.GetBytes(), Is.EquivalentTo(new byte[] { 0x62, 0x6f, 0x2c, 0x61, 0xc7, 0x68, 0x00, 0xb3, 0xa6, 0x8d, 0xf9, 0x55, 0x95, 0xbc, 0x1f, 0xd1 }), "The HMAC of 20 bytes of zero with 128-bit AesKey all zero should be this."); Assert.Throws <InvalidOperationException>(() => { hmacStream.Write(new byte[1], 0, 1); }, "Can't write to the stream after checking and thus finalizing the HMAC"); // This also implicitly covers double-dispose since we're in a using block. hmacStream.Dispose(); Assert.Throws <ObjectDisposedException>(() => { DataHmac invalidDataHmac = hmacStream.HmacResult; // Remove FxCop warning Object.Equals(invalidDataHmac, null); }); Assert.Throws <ObjectDisposedException>(() => { hmacStream.Write(new byte[1], 0, 1); }); Assert.Throws <ObjectDisposedException>(() => { using (Stream stream = new MemoryStream()) { hmacStream.ReadFrom(stream); } }); } }