Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,347 @@
//
// ARC4ManagedTest.cs - NUnit Test Cases for Alleged RC4(tm)
// RC4 is a trademark of RSA Security
//
// Author:
// Sebastien Pouliot (spouliot@motus.com)
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
//
using NUnit.Framework;
using System;
using System.Security.Cryptography;
using Mono.Security.Cryptography;
namespace MonoTests.Mono.Security.Cryptography {
// References
// a. Usenet 1994 - RC4 Algorithm revealed
// http://www.qrst.de/html/dsds/rc4.htm
// b. Netscape SSL version 3 implementation details
// Export Client SSL Connection Details
// http://wp.netscape.com/eng/ssl3/traces/trc-clnt-ex.html
[TestFixture]
public class ARC4ManagedTest {
// because most crypto stuff works with byte[] buffers
static public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
if ((array1 == null) && (array2 == null))
return;
if (array1 == null)
Assert.Fail (msg + " -> First array is NULL");
if (array2 == null)
Assert.Fail (msg + " -> Second array is NULL");
bool a = (array1.Length == array2.Length);
if (a) {
for (int i = 0; i < array1.Length; i++) {
if (array1 [i] != array2 [i]) {
a = false;
break;
}
}
}
msg += " -> Expected " + BitConverter.ToString (array1, 0);
msg += " is different than " + BitConverter.ToString (array2, 0);
Assert.IsTrue (a, msg);
}
// from ref. a
[Test]
public void Vector0 ()
{
byte[] key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] input = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] expected = { 0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96 };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = key;
ICryptoTransform stream = rc4.CreateEncryptor ();
byte[] output = stream.TransformFinalBlock (input, 0, input.Length);
AssertEquals ("RC4 - Test Vector 0", expected, output);
}
// from ref. a
[Test]
public void Vector1 ()
{
byte[] key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] input = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte[] expected = { 0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79 };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = key;
ICryptoTransform stream = rc4.CreateEncryptor ();
byte[] output = stream.TransformFinalBlock (input, 0, input.Length);
AssertEquals ("RC4 - Test Vector 1", expected, output);
}
// from ref. a
[Test]
public void Vector2 ()
{
byte[] key = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte[] input = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte[] expected = { 0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = key;
ICryptoTransform stream = rc4.CreateEncryptor ();
byte[] output = stream.TransformFinalBlock (input, 0, input.Length);
AssertEquals ("RC4 - Test Vector 2", expected, output);
}
// from ref. a
[Test]
public void Vector3 ()
{
byte[] key = { 0xef, 0x01, 0x23, 0x45 };
byte[] input = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte[] expected = { 0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 0xbd, 0x61 };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = key;
ICryptoTransform stream = rc4.CreateEncryptor ();
byte[] output = stream.TransformFinalBlock (input, 0, input.Length);
AssertEquals ("RC4 - Test Vector 3", expected, output);
}
// from ref. a
[Test]
public void Vector4 ()
{
byte[] key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] input = new byte [512];
for (int i=0; i < input.Length; i++)
input [i] = 0x01;
byte[] expected = { 0x75, 0x95, 0xc3, 0xe6, 0x11, 0x4a, 0x09, 0x78, 0x0c, 0x4a, 0xd4,
0x52, 0x33, 0x8e, 0x1f, 0xfd, 0x9a, 0x1b, 0xe9, 0x49, 0x8f,
0x81, 0x3d, 0x76, 0x53, 0x34, 0x49, 0xb6, 0x77, 0x8d, 0xca,
0xd8, 0xc7, 0x8a, 0x8d, 0x2b, 0xa9, 0xac, 0x66, 0x08, 0x5d,
0x0e, 0x53, 0xd5, 0x9c, 0x26, 0xc2, 0xd1, 0xc4, 0x90, 0xc1,
0xeb, 0xbe, 0x0c, 0xe6, 0x6d, 0x1b, 0x6b, 0x1b, 0x13, 0xb6,
0xb9, 0x19, 0xb8, 0x47, 0xc2, 0x5a, 0x91, 0x44, 0x7a, 0x95,
0xe7, 0x5e, 0x4e, 0xf1, 0x67, 0x79, 0xcd, 0xe8, 0xbf, 0x0a,
0x95, 0x85, 0x0e, 0x32, 0xaf, 0x96, 0x89, 0x44, 0x4f, 0xd3,
0x77, 0x10, 0x8f, 0x98, 0xfd, 0xcb, 0xd4, 0xe7, 0x26, 0x56,
0x75, 0x00, 0x99, 0x0b, 0xcc, 0x7e, 0x0c, 0xa3, 0xc4, 0xaa,
0xa3, 0x04, 0xa3, 0x87, 0xd2, 0x0f, 0x3b, 0x8f, 0xbb, 0xcd,
0x42, 0xa1, 0xbd, 0x31, 0x1d, 0x7a, 0x43, 0x03, 0xdd, 0xa5,
0xab, 0x07, 0x88, 0x96, 0xae, 0x80, 0xc1, 0x8b, 0x0a, 0xf6,
0x6d, 0xff, 0x31, 0x96, 0x16, 0xeb, 0x78, 0x4e, 0x49, 0x5a,
0xd2, 0xce, 0x90, 0xd7, 0xf7, 0x72, 0xa8, 0x17, 0x47, 0xb6,
0x5f, 0x62, 0x09, 0x3b, 0x1e, 0x0d, 0xb9, 0xe5, 0xba, 0x53,
0x2f, 0xaf, 0xec, 0x47, 0x50, 0x83, 0x23, 0xe6, 0x71, 0x32,
0x7d, 0xf9, 0x44, 0x44, 0x32, 0xcb, 0x73, 0x67, 0xce, 0xc8,
0x2f, 0x5d, 0x44, 0xc0, 0xd0, 0x0b, 0x67, 0xd6, 0x50, 0xa0,
0x75, 0xcd, 0x4b, 0x70, 0xde, 0xdd, 0x77, 0xeb, 0x9b, 0x10,
0x23, 0x1b, 0x6b, 0x5b, 0x74, 0x13, 0x47, 0x39, 0x6d, 0x62,
0x89, 0x74, 0x21, 0xd4, 0x3d, 0xf9, 0xb4, 0x2e, 0x44, 0x6e,
0x35, 0x8e, 0x9c, 0x11, 0xa9, 0xb2, 0x18, 0x4e, 0xcb, 0xef,
0x0c, 0xd8, 0xe7, 0xa8, 0x77, 0xef, 0x96, 0x8f, 0x13, 0x90,
0xec, 0x9b, 0x3d, 0x35, 0xa5, 0x58, 0x5c, 0xb0, 0x09, 0x29,
0x0e, 0x2f, 0xcd, 0xe7, 0xb5, 0xec, 0x66, 0xd9, 0x08, 0x4b,
0xe4, 0x40, 0x55, 0xa6, 0x19, 0xd9, 0xdd, 0x7f, 0xc3, 0x16,
0x6f, 0x94, 0x87, 0xf7, 0xcb, 0x27, 0x29, 0x12, 0x42, 0x64,
0x45, 0x99, 0x85, 0x14, 0xc1, 0x5d, 0x53, 0xa1, 0x8c, 0x86,
0x4c, 0xe3, 0xa2, 0xb7, 0x55, 0x57, 0x93, 0x98, 0x81, 0x26,
0x52, 0x0e, 0xac, 0xf2, 0xe3, 0x06, 0x6e, 0x23, 0x0c, 0x91,
0xbe, 0xe4, 0xdd, 0x53, 0x04, 0xf5, 0xfd, 0x04, 0x05, 0xb3,
0x5b, 0xd9, 0x9c, 0x73, 0x13, 0x5d, 0x3d, 0x9b, 0xc3, 0x35,
0xee, 0x04, 0x9e, 0xf6, 0x9b, 0x38, 0x67, 0xbf, 0x2d, 0x7b,
0xd1, 0xea, 0xa5, 0x95, 0xd8, 0xbf, 0xc0, 0x06, 0x6f, 0xf8,
0xd3, 0x15, 0x09, 0xeb, 0x0c, 0x6c, 0xaa, 0x00, 0x6c, 0x80,
0x7a, 0x62, 0x3e, 0xf8, 0x4c, 0x3d, 0x33, 0xc1, 0x95, 0xd2,
0x3e, 0xe3, 0x20, 0xc4, 0x0d, 0xe0, 0x55, 0x81, 0x57, 0xc8,
0x22, 0xd4, 0xb8, 0xc5, 0x69, 0xd8, 0x49, 0xae, 0xd5, 0x9d,
0x4e, 0x0f, 0xd7, 0xf3, 0x79, 0x58, 0x6b, 0x4b, 0x7f, 0xf6,
0x84, 0xed, 0x6a, 0x18, 0x9f, 0x74, 0x86, 0xd4, 0x9b, 0x9c,
0x4b, 0xad, 0x9b, 0xa2, 0x4b, 0x96, 0xab, 0xf9, 0x24, 0x37,
0x2c, 0x8a, 0x8f, 0xff, 0xb1, 0x0d, 0x55, 0x35, 0x49, 0x00,
0xa7, 0x7a, 0x3d, 0xb5, 0xf2, 0x05, 0xe1, 0xb9, 0x9f, 0xcd,
0x86, 0x60, 0x86, 0x3a, 0x15, 0x9a, 0xd4, 0xab, 0xe4, 0x0f,
0xa4, 0x89, 0x34, 0x16, 0x3d, 0xdd, 0xe5, 0x42, 0xa6, 0x58,
0x55, 0x40, 0xfd, 0x68, 0x3c, 0xbf, 0xd8, 0xc0, 0x0f, 0x12,
0x12, 0x9a, 0x28, 0x4d, 0xea, 0xcc, 0x4c, 0xde, 0xfe, 0x58,
0xbe, 0x71, 0x37, 0x54, 0x1c, 0x04, 0x71, 0x26, 0xc8, 0xd4,
0x9e, 0x27, 0x55, 0xab, 0x18, 0x1a, 0xb7, 0xe9, 0x40, 0xb0,
0xc0 };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = key;
ICryptoTransform stream = rc4.CreateEncryptor ();
byte[] output = stream.TransformFinalBlock (input, 0, input.Length);
AssertEquals ("RC4 - Test Vector 4", expected, output);
}
static byte[] clientWriteKey = { 0x32, 0x10, 0xcd, 0xe1, 0xd6, 0xdc, 0x07, 0x83, 0xf3, 0x75, 0x4c, 0x32, 0x2e, 0x59, 0x96, 0x61 };
static byte[] serverWriteKey = { 0xed, 0x0e, 0x56, 0xc8, 0x95, 0x12, 0x37, 0xb6, 0x21, 0x17, 0x1c, 0x72, 0x79, 0x91, 0x12, 0x1e };
// SSL3 Client's Finished Handshake (from ref. b)
[Test]
public void SSLClient ()
{
byte[] data = { 0x14, 0x00, 0x00, 0x24, 0xf2, 0x40, 0x10, 0x3f, 0x74, 0x63, 0xea, 0xe8, 0x7a, 0x27, 0x23, 0x56, 0x5f, 0x59, 0x07, 0xd2, 0xa3, 0x79, 0x5d, 0xb7, 0x8b, 0x94, 0xdb, 0xcf, 0xfa, 0xf5, 0x18, 0x22, 0x15, 0x7b, 0xf2, 0x4a, 0x96, 0x52, 0x9a, 0x0e, 0xd3, 0x09, 0xde, 0x28, 0x84, 0xa7, 0x07, 0x5c, 0x7c, 0x0c, 0x08, 0x85, 0x6b, 0x4f, 0x63, 0x04 };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = clientWriteKey;
// encrypt inplace (in same buffer)
rc4.TransformBlock (data, 0, data.Length, data, 0);
byte[] expectedData = { 0xed, 0x37, 0x7f, 0x16, 0xd3, 0x11, 0xe8, 0xa3, 0xe1, 0x2a, 0x20, 0xb7, 0x88, 0xf6, 0x11, 0xf3, 0xa6, 0x7d, 0x37, 0xf7, 0x17, 0xac, 0x67, 0x20, 0xb8, 0x0e, 0x88, 0xd1, 0xa0, 0xc6, 0x83, 0xe4, 0x80, 0xe8, 0xc7, 0xe3, 0x0b, 0x91, 0x29, 0x30, 0x29, 0xe4, 0x28, 0x47, 0xb7, 0x40, 0xa4, 0xd1, 0x3c, 0xda, 0x82, 0xb7, 0xb3, 0x9f, 0x67, 0x10 };
AssertEquals ("RC4 - Client's Finished Handshake", expectedData, data);
}
// SSL3 Server Finished Handshake (from ref. b)
[Test]
public void SSLServer ()
{
byte[] encryptedData = { 0x54, 0x3c, 0xe1, 0xe7, 0x4d, 0x77, 0x76, 0x62, 0x86, 0xfa, 0x4e, 0x0a, 0x6f, 0x5f, 0x6a, 0x3d, 0x43, 0x26, 0xf4, 0xad, 0x8d, 0x3e, 0x09, 0x0b, 0x2b, 0xf7, 0x9f, 0x49, 0x44, 0x92, 0xfb, 0xa9, 0xa4, 0xb0, 0x5a, 0xd8, 0x72, 0x77, 0x6e, 0x8b, 0xb3, 0x78, 0xfb, 0xda, 0xe0, 0x25, 0xef, 0xb3, 0xf5, 0xa7, 0x90, 0x08, 0x6d, 0x60, 0xd5, 0x4e };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = serverWriteKey;
// decrypt inplace (in same buffer)
rc4.TransformBlock (encryptedData, 0, encryptedData.Length, encryptedData, 0);
byte[] expectedData = { 0x14, 0x00, 0x00, 0x24, 0xb7, 0xcc, 0xd6, 0x05, 0x6b, 0xfc, 0xfa, 0x6d, 0xfa, 0xdd, 0x76, 0x81, 0x45, 0x36, 0xe4, 0xf4, 0x26, 0x35, 0x72, 0x2c, 0xec, 0x87, 0x62, 0x1f, 0x55, 0x08, 0x05, 0x4f, 0xc8, 0xf5, 0x7c, 0x49, 0xe2, 0xee, 0xc5, 0xba, 0xbd, 0x69, 0x27, 0x3b, 0xd0, 0x13, 0x23, 0x52, 0xed, 0xec, 0x11, 0x55, 0xd8, 0xb9, 0x90, 0x8c };
AssertEquals ("RC4 - Server's Finished Handshake", expectedData, encryptedData);
}
[Test]
public void DefaultProperties ()
{
ARC4Managed rc4 = new ARC4Managed ();
Assert.IsFalse (rc4.CanReuseTransform, "CanReuseTransform");
Assert.IsTrue (rc4.CanTransformMultipleBlocks, "CanTransformMultipleBlocks");
Assert.AreEqual (1, rc4.InputBlockSize, "InputBlockSize");
Assert.AreEqual (1, rc4.OutputBlockSize, "OutputBlockSize");
}
[Test]
public void DefaultSizes ()
{
ARC4Managed rc4 = new ARC4Managed ();
rc4.GenerateKey ();
rc4.GenerateIV ();
Assert.AreEqual (16, rc4.Key.Length, "Key.Length");
Assert.AreEqual (128, rc4.KeySize, "KeySize");
Assert.AreEqual (0, rc4.IV.Length, "IV.Length");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TransformBlock_InputBuffer_Null ()
{
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (null, 0, 1, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformBlock_InputOffset_Negative ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, -1, 1, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_InputOffset_Overflow ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, Int32.MaxValue, 1, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformBlock_InputCount_Negative ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, 0, -1, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_InputCount_Overflow ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, 1, Int32.MaxValue, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TransformBlock_OutputBuffer_Null ()
{
byte[] input = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, 0, 1, null, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformBlock_OutputOffset_Negative ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, 0, 1, output, -1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_OutputOffset_Overflow ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, 0, 1, output, Int32.MaxValue);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TransformFinalBlock_InputBuffer_Null ()
{
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformFinalBlock (null, 0, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformFinalBlock_InputOffset_Negative ()
{
byte[] input = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformFinalBlock (input, -1, 1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformFinalBlock_InputOffset_Overflow ()
{
byte[] input = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformFinalBlock (input, Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformFinalBlock_InputCount_Negative ()
{
byte[] input = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformFinalBlock (input, 0, -1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformFinalBlock_InputCount_Overflow ()
{
byte[] input = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformFinalBlock (input, 1, Int32.MaxValue);
}
}
}

View File

@@ -0,0 +1,103 @@
2007-05-08 Randolph Chung <tausq@debian.org>
* CryptoConvertTest.cs: Add tests for DSA conversion functions.
2006-10-04 Sebastien Pouliot <sebastien@ximian.com>
* PKCS1Test.cs: Ensure the latest changes didn't break some SSL3
features that uses MD5SHA1 as a hash function.
* RSAManagedTest.cs: Forgot to update test cases after right padding
was added.
2006-09-05 Sebastien Pouliot <sebastien@ximian.com>
* RSAManagedTest.cs: Add a test case for #79269 (wrong exception).
2005-04-27 Sebastien Pouliot <sebastien@ximian.com>
* KeyPairPersistenceTest.cs: Upgrade to NUnit 2.2 API to use Ignore.
Now Ignore all tests that results in UnauthorizedAccessException.
2004-12-06 Sebastien Pouliot <sebastien@ximian.com>
* RSAManagedTest.cs: New. Unit tests to validate RSA decryption when
using key blinding with or without using CRT.
2004-11-05 Sebastien Pouliot <sebastien@ximian.com>
* KeyPairPersistenceTest.cs: Forgot to re-fix the second failing test.
2004-11-03 Sebastien Pouliot <sebastien@ximian.com>
* KeyPairPersistenceTest.cs: Re-fix as the UnauthorizedAccessException
is the InnerException of a CryptographicException.
2004-10-28 Sebastien Pouliot <sebastien@ximian.com>
* KeyPairPersistenceTest.cs: Fix two tests to work even if the user
doesn't have access to the machine key store.
2004-09-22 Sebastien Pouliot <sebastien@ximian.com>
* SHA224Test.cs: Fix compilation error with NUnit 2.1.91 (CVS).
2004-09-16 Sebastien Pouliot <sebastien@ximian.com>
* SHA224ManagedTest.cs: New. Unit tests for the managed implementation
of SHA-224.
* SHA224Test.cs: New. Unit tests for SHA-224. Use test vectors from
RFC3874.
2004-05-27 Sebastien Pouliot <sebastien@ximian.com>
* ARC4ManagedTest.cs: Added unit tests for exceptions coming from
ICryptoTransform interface (TransformBlock, TransformFinalBlock).
2004-05-11 Sebastien Pouliot <sebastien@ximian.com>
* ARC4ManagedTest.cs: Added missing unit tests for complete coverage.
Completed convertion to NUnit 2.
* CryptoConvertTest.cs: Added/modified unit tests for better coverage.
* MD2ManagedTest.cs: Added unit tests for better coverage.
* MD2Test.cs: Added unit tests for better coverage.
* MD4ManagedTest.cs: Added unit tests for better coverage.
* MD4Test.cs: Added unit tests for better coverage.
* PKCS8.cs: Added unit tests for better coverage.
2004-05-01 Sebastien Pouliot <sebastien@ximian.com>
* CryptoConvertTest.cs: Added test case where a strongname key pair
(RSA 1024 bits) was truncated in the last parameter (D) (i.e. the
private key) but saved by the use of CRT (Chinese Remainder Theorem).
2004-03-23 Sebastien Pouliot <sebastien@ximian.com>
* CryptoConvertTest.cs: Added test case where the public key structure
is inside preceded by an header (like the one produced by "sn -e").
2004-03-23 Sebastien Pouliot <sebastien@ximian.com>
* CryptoConvertTest.cs: New. Unit tests for CryptoConvert.
2004-02-13 Sebastien Pouliot <sebastien@ximian.com>
* DiffieHellmanManagedTest.cs: New. Basic unit test for DH.
2004-02-07 Sebastien Pouliot <sebastien@ximian.com>
* KeyPairPersistenceTest.cs: New. Unit tests for key pair persistence.
2003-11-27 Sebastien Pouliot <spouliot@videotron.ca>
* MD4Test.cs: New. Contains all the test vectors provided in RFC1320
- but none are executed because MD4 is an abstract class.
* MD4ManagedTest.cs: New. Run the unit tests from MD4Test.cs using
the MD4Managed implementation of MD4.
* PKCS8Test.cs: New. Unit tests for PKCS8.
2003-03-07 Sebastien Pouliot <spouliot@videotron.ca>
* ARC4ManagedTest.cs: New. Test vectors for RC4(tm) taken from
usenet and Netscape SSL version 3 implementation details.
* MD2ManagedTest.cs: New. Call tests defined in MD2Test class.
* MD2Test.cs: New. Test vectors defined in RFC1319.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,78 @@
//
// DiffieHellmanManagedTest.cs - NUnit Test Cases for DH (PKCS#3)
//
// Authors:
// Pieter Philippaerts (Pieter@mentalis.org)
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 The Mentalis.org Team (http://www.mentalis.org/)
// (C) 2004 Novell (http://www.novell.com)
//
using NUnit.Framework;
using System;
using System.IO;
using Mono.Security.Cryptography;
using System.Text;
namespace MonoTests.Mono.Security.Cryptography {
// References:
// a. PKCS #3: Diffie-Hellman Key-Agreement Standard (version 1.4)
// ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-3.asc
// b. Diffie-Hellman Key Agreement Method
// http://www.ietf.org/rfc/rfc2631.txt
[TestFixture]
public class DiffieHellmanManagedTest : Assertion {
// because most crypto stuff works with byte[] buffers
static public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
if ((array1 == null) && (array2 == null))
return;
if (array1 == null)
Assertion.Fail (msg + " -> First array is NULL");
if (array2 == null)
Assertion.Fail (msg + " -> Second array is NULL");
bool a = (array1.Length == array2.Length);
if (a) {
for (int i = 0; i < array1.Length; i++) {
if (array1 [i] != array2 [i]) {
a = false;
break;
}
}
}
if (array1.Length > 0) {
msg += " -> Expected " + BitConverter.ToString (array1, 0);
msg += " is different than " + BitConverter.ToString (array2, 0);
}
Assertion.Assert (msg, a);
}
[Test]
public void KeyExchange ()
{
// create a new DH instance
DiffieHellman dh1 = new DiffieHellmanManaged ();
// export the public parameters of the first DH instance
DHParameters dhp = dh1.ExportParameters (false);
// create a second DH instance and initialize it with the public parameters of the first instance
DiffieHellman dh2 = new DiffieHellmanManaged (dhp.P, dhp.G, 160);
// generate the public key of the first DH instance
byte[] ke1 = dh1.CreateKeyExchange ();
// generate the public key of the second DH instance
byte[] ke2 = dh2.CreateKeyExchange ();
// let the first DH instance compute the shared secret using the second DH public key
byte[] dh1k = dh1.DecryptKeyExchange (ke2);
// let the second DH instance compute the shared secret using the first DH public key
byte[] dh2k = dh2.DecryptKeyExchange (ke1);
// both shared secrets are the same
AssertEquals ("Shared Secret", dh1k, dh2k);
}
// TODO: More is needed !
}
}

View File

@@ -0,0 +1,247 @@
//
// KeyPairPersistenceTest.cs: Unit tests for keypair persistence
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.IO;
using System.Security.Cryptography;
using Mono.Security.Cryptography;
namespace MonoTests.Mono.Security.Cryptography {
[TestFixture]
public class KeyPairPersistenceTest {
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Constructor_Null ()
{
KeyPairPersistence kpp = new KeyPairPersistence (null);
}
private void Compare (KeyPairPersistence saved, KeyPairPersistence loaded)
{
// Note: there is an additional Environement.NewLine
// at the end of the loaded string - that's why we do
// not use AssertEquals (for strings)
Assert.IsTrue (loaded.Filename.StartsWith (saved.Filename), "Filename");
Assert.IsTrue (loaded.KeyValue.StartsWith (saved.KeyValue), "KeyValue");
Assert.IsTrue (loaded.Parameters.KeyContainerName.StartsWith (saved.Parameters.KeyContainerName), "Parameters.KeyContainerName");
Assert.AreEqual (saved.Parameters.KeyNumber, loaded.Parameters.KeyNumber, "Parameters.KeyNumber");
Assert.IsTrue (loaded.Parameters.ProviderName.StartsWith (saved.Parameters.ProviderName), "Parameters.ProviderName");
Assert.AreEqual (saved.Parameters.ProviderType, loaded.Parameters.ProviderType, "Parameters.ProviderType");
}
[Test]
public void CspType ()
{
try {
CspParameters cp = new CspParameters (-1);
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
// we didn't supply a name so we can't load it back
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspTypeProvider ()
{
try {
CspParameters cp = new CspParameters (-2, "Provider");
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
// we didn't supply a name so we can't load it back
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspTypeProviderContainer ()
{
try {
CspParameters cp = new CspParameters (-3, "Provider", "Container");
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
KeyPairPersistence kpp2 = new KeyPairPersistence (cp);
Assert.IsTrue (kpp2.Load (), "Load");
Compare (kpp, kpp2);
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspTypeProviderContainerKeyNumber ()
{
try {
CspParameters cp = new CspParameters (-4, "Provider", "Container");
cp.KeyNumber = 0;
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
KeyPairPersistence kpp2 = new KeyPairPersistence (cp);
Assert.IsTrue (kpp2.Load (), "Load");
Compare (kpp, kpp2);
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspFlagsDefault ()
{
try {
CspParameters cp = new CspParameters (-5, "Provider", "Container");
cp.Flags = CspProviderFlags.UseDefaultKeyContainer;
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
KeyPairPersistence kpp2 = new KeyPairPersistence (cp);
Assert.IsTrue (kpp2.Load (), "Load");
Compare (kpp, kpp2);
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspFlagsMachine ()
{
try {
CspParameters cp = new CspParameters (-6, "Provider", "Container");
cp.Flags = CspProviderFlags.UseMachineKeyStore;
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
KeyPairPersistence kpp2 = new KeyPairPersistence (cp);
Assert.IsTrue (kpp2.Load (), "Load");
Compare (kpp, kpp2);
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (CryptographicException ce) {
// not everyone can write to the machine store
if (!(ce.InnerException is UnauthorizedAccessException))
throw;
Assert.Ignore ("Access denied to key containers files.");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspFlagsDefaultMachine ()
{
try {
CspParameters cp = new CspParameters (-7, "Provider", "Container");
cp.Flags = CspProviderFlags.UseDefaultKeyContainer | CspProviderFlags.UseMachineKeyStore;
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
KeyPairPersistence kpp2 = new KeyPairPersistence (cp);
Assert.IsTrue (kpp2.Load (), "Load");
Compare (kpp, kpp2);
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (CryptographicException ce) {
// not everyone can write to the machine store
if (!(ce.InnerException is UnauthorizedAccessException))
throw;
Assert.Ignore ("Access denied to key containers files.");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspNoChangesPermitted ()
{
try {
CspParameters cp = new CspParameters (-8, "Provider", "Container");
cp.KeyNumber = 0;
cp.Flags = CspProviderFlags.UseMachineKeyStore;
KeyPairPersistence kpp = new KeyPairPersistence (cp);
CspParameters copy = kpp.Parameters;
copy.Flags = CspProviderFlags.UseDefaultKeyContainer;
copy.KeyContainerName = "NewContainerName";
copy.KeyNumber = 1;
copy.ProviderName = "NewProviderName";
copy.ProviderType = -9;
Assert.IsTrue (cp.Flags != copy.Flags, "Flags");
Assert.IsTrue (cp.KeyContainerName != copy.KeyContainerName, "KeyContainerName");
Assert.IsTrue (cp.KeyNumber != copy.KeyNumber, "KeyNumber");
Assert.IsTrue (cp.ProviderName != copy.ProviderName, "ProviderName");
Assert.IsTrue (cp.ProviderType != copy.ProviderType, "ProviderType");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
}
}

View File

@@ -0,0 +1,38 @@
//
// MD2ManagedTest.cs - NUnit Test Cases for MD2 (RFC1319)
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// (C) 2004 Novell (http://www.novell.com)
//
using System;
using System.Security.Cryptography;
using Mono.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.Mono.Security.Cryptography {
[TestFixture]
public class MD2ManagedTest : MD2Test {
[SetUp]
public void Setup ()
{
hash = new MD2Managed ();
}
// this will run ALL tests defined in MD2Test.cs with the MD2Managed implementation
[Test]
public override void Create ()
{
// try creating ourselve using Create
HashAlgorithm h = MD2.Create ("MD2Managed");
Assert.IsTrue ((h is MD2Managed), "MD2Managed");
}
}
}

View File

@@ -0,0 +1,249 @@
//
// MD2Test.cs - NUnit Test Cases for MD2 (RFC1319)
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
// (C) 2004 Novell (http://www.novell.com)
//
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Mono.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.Mono.Security.Cryptography {
// References:
// a. The MD2 Message-Digest Algorithm
// http://www.ietf.org/rfc/rfc1319.txt
// MD2 is a abstract class - so ALL of the test included here wont be tested
// on the abstract class but should be tested in ALL its descendants.
public abstract class MD2Test {
protected MD2 hash;
static void Fail (string msg)
{
Assert.Fail (msg);
}
static void AssertEquals (string msg, int expected, int actual)
{
Assert.AreEqual (expected, actual, msg);
}
// because most crypto stuff works with byte[] buffers
static public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
if ((array1 == null) && (array2 == null))
return;
if (array1 == null)
Fail (msg + " -> First array is NULL");
if (array2 == null)
Fail (msg + " -> Second array is NULL");
bool a = (array1.Length == array2.Length);
if (a) {
for (int i = 0; i < array1.Length; i++) {
if (array1 [i] != array2 [i]) {
a = false;
break;
}
}
}
if (array1.Length > 0) {
msg += " -> Expected " + BitConverter.ToString (array1, 0);
msg += " is different than " + BitConverter.ToString (array2, 0);
}
Assert.IsTrue (a, msg);
}
// MD2 ("") = 8350e5a3e24c153df2275c9f80692773
[Test]
public void RFC1319_Test1 ()
{
string className = hash.ToString ();
byte[] result = { 0x83, 0x50, 0xe5, 0xa3, 0xe2, 0x4c, 0x15, 0x3d, 0xf2, 0x27, 0x5c, 0x9f, 0x80, 0x69, 0x27, 0x73 };
byte[] input = new byte [0];
string testName = className + " 1";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
// N/A RFC1319_e (testName, hash, input, result);
}
// MD2 ("a") = 32ec01ec4a6dac72c0ab96fb34c0b5d1
[Test]
public void RFC1319_Test2 ()
{
string className = hash.ToString ();
byte[] result = { 0x32, 0xec, 0x01, 0xec, 0x4a, 0x6d, 0xac, 0x72, 0xc0, 0xab, 0x96, 0xfb, 0x34, 0xc0, 0xb5, 0xd1 };
byte[] input = Encoding.Default.GetBytes ("a");
string testName = className + " 2";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
// MD2 ("abc") = da853b0d3f88d99b30283a69e6ded6bb
[Test]
public void RFC1319_Test3 ()
{
string className = hash.ToString ();
byte[] result = { 0xda, 0x85, 0x3b, 0x0d, 0x3f, 0x88, 0xd9, 0x9b, 0x30, 0x28, 0x3a, 0x69, 0xe6, 0xde, 0xd6, 0xbb };
byte[] input = Encoding.Default.GetBytes ("abc");
string testName = className + " 3";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
// MD2 ("message digest") = ab4f496bfb2a530b219ff33031fe06b0
[Test]
public void RFC1319_Test4 ()
{
string className = hash.ToString ();
byte[] result = { 0xab, 0x4f, 0x49, 0x6b, 0xfb, 0x2a, 0x53, 0x0b, 0x21, 0x9f, 0xf3, 0x30, 0x31, 0xfe, 0x06, 0xb0 };
byte[] input = Encoding.Default.GetBytes ("message digest");
string testName = className + " 4";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
// MD2 ("abcdefghijklmnopqrstuvwxyz") = 4e8ddff3650292ab5a4108c3aa47940b
[Test]
public void RFC1319_Test5 ()
{
string className = hash.ToString ();
byte[] result = { 0x4e, 0x8d, 0xdf, 0xf3, 0x65, 0x02, 0x92, 0xab, 0x5a, 0x41, 0x08, 0xc3, 0xaa, 0x47, 0x94, 0x0b };
byte[] input = Encoding.Default.GetBytes ("abcdefghijklmnopqrstuvwxyz");
string testName = className + " 5";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
// MD2 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
// da33def2a42df13975352846c30338cd
[Test]
public void RFC1319_Test6 ()
{
string className = hash.ToString ();
byte[] result = { 0xda, 0x33, 0xde, 0xf2, 0xa4, 0x2d, 0xf1, 0x39, 0x75, 0x35, 0x28, 0x46, 0xc3, 0x03, 0x38, 0xcd };
byte[] input = Encoding.Default.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
string testName = className + " 6";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
// MD2 ("123456789012345678901234567890123456789012345678901234567890123456
// 78901234567890") = d5976f79d83d3a0dc9806c3c66f3efd8
[Test]
public void RFC1319_Test7 ()
{
string className = hash.ToString ();
byte[] result = { 0xd5, 0x97, 0x6f, 0x79, 0xd8, 0x3d, 0x3a, 0x0d, 0xc9, 0x80, 0x6c, 0x3c, 0x66, 0xf3, 0xef, 0xd8 };
byte[] input = Encoding.Default.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
string testName = className + " 7";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
public void RFC1319_a (string testName, MD2 hash, byte[] input, byte[] result)
{
byte[] output = hash.ComputeHash (input);
AssertEquals (testName + ".a.1", result, output);
AssertEquals (testName + ".a.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1319_b (string testName, MD2 hash, byte[] input, byte[] result)
{
byte[] output = hash.ComputeHash (input, 0, input.Length);
AssertEquals (testName + ".b.1", result, output);
AssertEquals (testName + ".b.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1319_c (string testName, MD2 hash, byte[] input, byte[] result)
{
MemoryStream ms = new MemoryStream (input);
byte[] output = hash.ComputeHash (ms);
AssertEquals (testName + ".c.1", result, output);
AssertEquals (testName + ".c.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1319_d (string testName, MD2 hash, byte[] input, byte[] result)
{
byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
AssertEquals (testName + ".d.1", input, output);
AssertEquals (testName + ".d.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1319_e (string testName, MD2 hash, byte[] input, byte[] result)
{
byte[] copy = new byte [input.Length];
for (int i=0; i < input.Length - 1; i++)
hash.TransformBlock (input, i, 1, copy, i);
byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
AssertEquals (testName + ".e.1", input [input.Length - 1], output [0]);
AssertEquals (testName + ".e.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
// none of those values changes for any implementation of MD2
[Test]
public virtual void StaticInfo ()
{
string className = hash.ToString ();
AssertEquals (className + ".HashSize", 128, hash.HashSize);
AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);
}
[Test]
public virtual void Create ()
{
// create the default implementation
HashAlgorithm h = MD2.Create ();
Assert.IsTrue ((h is MD2Managed), "MD2Managed");
// Note: will fail is default is changed in machine.config
}
}
}

View File

@@ -0,0 +1,38 @@
//
// MD4ManagedTest.cs - NUnit Test Cases for MD4 (RFC1320)
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// (C) 2004 Novell (http://www.novell.com)
//
using System;
using System.Security.Cryptography;
using NUnit.Framework;
using Mono.Security.Cryptography;
namespace MonoTests.Mono.Security.Cryptography {
[TestFixture]
public class MD4ManagedTest : MD4Test {
[SetUp]
public void Setup ()
{
hash = new MD4Managed ();
}
// this will run ALL tests defined in MD4Test.cs with the MD4Managed implementation
[Test]
public override void Create ()
{
// try creating ourselve using Create
HashAlgorithm h = MD4.Create ("MD4Managed");
Assert.IsTrue ((h is MD4Managed), "MD4Managed");
}
}
}

View File

@@ -0,0 +1,244 @@
//
// MD4Test.cs - NUnit Test Cases for MD4 (RFC1320)
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// (C) 2004 Novell (http://www.novell.com)
//
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Mono.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.Mono.Security.Cryptography {
// References:
// a. The MD4 Message-Digest Algorithm
// http://www.ietf.org/rfc/RFC1320.txt
// MD4 is a abstract class - so ALL of the test included here wont be tested
// on the abstract class but should be tested in ALL its descendants.
public abstract class MD4Test {
protected MD4 hash;
static void AssertEquals (string msg, int expected, int actual)
{
Assert.AreEqual (expected, actual, msg);
}
// because most crypto stuff works with byte[] buffers
static public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
if ((array1 == null) && (array2 == null))
return;
if (array1 == null)
Assert.Fail (msg + " -> First array is NULL");
if (array2 == null)
Assert.Fail (msg + " -> Second array is NULL");
bool a = (array1.Length == array2.Length);
if (a) {
for (int i = 0; i < array1.Length; i++) {
if (array1 [i] != array2 [i]) {
a = false;
break;
}
}
}
if (array1.Length > 0) {
msg += " -> Expected " + BitConverter.ToString (array1, 0);
msg += " is different than " + BitConverter.ToString (array2, 0);
}
Assert.IsTrue (a, msg);
}
// MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
[Test]
public void RFC1320_Test1 ()
{
string className = hash.ToString ();
byte[] result = { 0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0 };
byte[] input = new byte [0];
string testName = className + " 1";
RFC1320_a (testName, hash, input, result);
RFC1320_b (testName, hash, input, result);
RFC1320_c (testName, hash, input, result);
RFC1320_d (testName, hash, input, result);
// N/A RFC1320_e (testName, hash, input, result);
}
// MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
[Test]
public void RFC1320_Test2 ()
{
string className = hash.ToString ();
byte[] result = { 0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46, 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24 };
byte[] input = Encoding.Default.GetBytes ("a");
string testName = className + " 2";
RFC1320_a (testName, hash, input, result);
RFC1320_b (testName, hash, input, result);
RFC1320_c (testName, hash, input, result);
RFC1320_d (testName, hash, input, result);
RFC1320_e (testName, hash, input, result);
}
// MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
[Test]
public void RFC1320_Test3 ()
{
string className = hash.ToString ();
byte[] result = { 0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d };
byte[] input = Encoding.Default.GetBytes ("abc");
string testName = className + " 3";
RFC1320_a (testName, hash, input, result);
RFC1320_b (testName, hash, input, result);
RFC1320_c (testName, hash, input, result);
RFC1320_d (testName, hash, input, result);
RFC1320_e (testName, hash, input, result);
}
// MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
[Test]
public void RFC1320_Test4 ()
{
string className = hash.ToString ();
byte[] result = { 0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b };
byte[] input = Encoding.Default.GetBytes ("message digest");
string testName = className + " 4";
RFC1320_a (testName, hash, input, result);
RFC1320_b (testName, hash, input, result);
RFC1320_c (testName, hash, input, result);
RFC1320_d (testName, hash, input, result);
RFC1320_e (testName, hash, input, result);
}
// MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
[Test]
public void RFC1320_Test5 ()
{
string className = hash.ToString ();
byte[] result = { 0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd, 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9 };
byte[] input = Encoding.Default.GetBytes ("abcdefghijklmnopqrstuvwxyz");
string testName = className + " 5";
RFC1320_a (testName, hash, input, result);
RFC1320_b (testName, hash, input, result);
RFC1320_c (testName, hash, input, result);
RFC1320_d (testName, hash, input, result);
RFC1320_e (testName, hash, input, result);
}
// MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
// 043f8582f241db351ce627e153e7f0e4
[Test]
public void RFC1320_Test6 ()
{
string className = hash.ToString ();
byte[] result = { 0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35, 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4 };
byte[] input = Encoding.Default.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
string testName = className + " 6";
RFC1320_a (testName, hash, input, result);
RFC1320_b (testName, hash, input, result);
RFC1320_c (testName, hash, input, result);
RFC1320_d (testName, hash, input, result);
RFC1320_e (testName, hash, input, result);
}
// MD4 ("123456789012345678901234567890123456789012345678901234567890123456
// 78901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
[Test]
public void RFC1320_Test7 ()
{
string className = hash.ToString ();
byte[] result = { 0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36 };
byte[] input = Encoding.Default.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
string testName = className + " 7";
RFC1320_a (testName, hash, input, result);
RFC1320_b (testName, hash, input, result);
RFC1320_c (testName, hash, input, result);
RFC1320_d (testName, hash, input, result);
RFC1320_e (testName, hash, input, result);
}
public void RFC1320_a (string testName, MD4 hash, byte[] input, byte[] result)
{
byte[] output = hash.ComputeHash (input);
AssertEquals (testName + ".a.1", result, output);
AssertEquals (testName + ".a.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1320_b (string testName, MD4 hash, byte[] input, byte[] result)
{
byte[] output = hash.ComputeHash (input, 0, input.Length);
AssertEquals (testName + ".b.1", result, output);
AssertEquals (testName + ".b.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1320_c (string testName, MD4 hash, byte[] input, byte[] result)
{
MemoryStream ms = new MemoryStream (input);
byte[] output = hash.ComputeHash (ms);
AssertEquals (testName + ".c.1", result, output);
AssertEquals (testName + ".c.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1320_d (string testName, MD4 hash, byte[] input, byte[] result)
{
byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
AssertEquals (testName + ".d.1", input, output);
AssertEquals (testName + ".d.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1320_e (string testName, MD4 hash, byte[] input, byte[] result)
{
byte[] copy = new byte [input.Length];
for (int i=0; i < input.Length - 1; i++)
hash.TransformBlock (input, i, 1, copy, i);
byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
AssertEquals (testName + ".e.1", input [input.Length - 1], output [0]);
AssertEquals (testName + ".e.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
// none of those values changes for any implementation of MD4
[Test]
public virtual void StaticInfo ()
{
string className = hash.ToString ();
AssertEquals (className + ".HashSize", 128, hash.HashSize);
AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);
}
[Test]
public virtual void Create ()
{
// create the default implementation
HashAlgorithm h = MD4.Create ();
Assert.IsTrue ((h is MD4Managed), "MD4Managed");
// Note: will fail is default is changed in machine.config
}
}
}

View File

@@ -0,0 +1,156 @@
//
// PKCS1Test.cs - NUnit Test Cases for PKCS #1
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Mono.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.Mono.Security.Cryptography {
/* copied from MD5SHA1 which is internal in Mono.Security.dll */
class MD5SHA1: HashAlgorithm {
private HashAlgorithm md5;
private HashAlgorithm sha;
public MD5SHA1 ()
: base ()
{
this.md5 = MD5.Create ();
this.sha = SHA1.Create ();
// Set HashSizeValue
this.HashSizeValue = this.md5.HashSize + this.sha.HashSize;
}
public override void Initialize ()
{
this.md5.Initialize ();
this.sha.Initialize ();
}
protected override byte[] HashFinal ()
{
// Finalize the original hash
this.md5.TransformFinalBlock (new byte[0], 0, 0);
this.sha.TransformFinalBlock (new byte[0], 0, 0);
byte[] hash = new byte[36];
Buffer.BlockCopy (this.md5.Hash, 0, hash, 0, 16);
Buffer.BlockCopy (this.sha.Hash, 0, hash, 16, 20);
return hash;
}
protected override void HashCore (
byte[] array,
int ibStart,
int cbSize)
{
this.md5.TransformBlock (array, ibStart, cbSize, array, ibStart);
this.sha.TransformBlock (array, ibStart, cbSize, array, ibStart);
}
}
[TestFixture]
public class PKCS1Test {
// Test cases and key pair provided by Joerg Rosenkranz
// Note: I couldn't produce any similar (short) results with Fx 2.0 beta 1
// so I was either (very) unlucky or this was fixed post 1.1.
private const string short017 = "JbLFk42L8GEXm2xfJ79RopitBBwPCnS5BjYbkL44EL9iVYKEHLEIuO1LGqqzjh9DKB+KhYUQEz+5KRWNeHsqY4FfvoRdmj9yhbffPxw/TA5P7ez6i7R3K7U0p2sLsP4jSQS+aMPumudMJDXAfRPq5raSFvYS03IldEC3IkZDBg==";
private const string short063 = "kws/A0BZI3si9Rjbt5gG6B3T/h0jFyEagnq31vl9kSakfry3369ncsHMtDeQAPnMaMHSegUvMgrTLv+ZmCFwtldhXhp4ICzu9HpVrZyYj55Q8cdO9DrOqJsTW89rdwYStIXfWsLw2wPNNrfe6vSb5yjI2sC4iFTbNbxdAyPqjw==";
private const string short136 = "0vf3pwg0qWF36ff2KTt6DB7QlRKtOEWn+urZKnZ8Gh+EalTHsbmiBaOxhpl16MYxdMt9oeW+ioNWXZAnF4XomKPeQpBwbUDTTVvoPEDudkOo271R1O87A7oE4w+n2qz9Xgp9pLe2IlEw2jSOZkUK6hv4Zm6gv79h+g3Dz+EQOw==";
private const string short264 = "NAWbAU7roLAQT9TZJRXhJoAEhRgBDubUAMTZUr+gs9Soszzb3eh/Hy/pqHdgIwqSzQIqDiMmwZvgiFgkQxWquI9xLwffCES4bnO1eVOFHSPeHkMIR+o6YmNKhsud8fr3y5SSTn1zb2UHgNN5ooX9A7IwjAIHuJkCR9zW1kmkzg==";
private const string short482 = "tR27JbaiCrKu/1L2xBohbrtlrTOtRd4c2EEI2ThlwIby7LAJr6i8GAIxpPQFhs/RFSISW1lujfMIIlPH13HrzyfblluYunqhGzgNk57/kM/Wo2y1TRUbBZ1uWpD96kqpRBqN63MY8E1L0kMIICHMPn4gaaHNv/wIvd3bRgVrOQ==";
private const string short631 = "4afUaTo+bHTVwAlg/XE/saI7KlCGHAbIA4y7hbDIkUWrkwAU3BeMNoNMEBBFKyVXobpDdgNxE7K64Tkmc8KLDhyUHsgCNvPoBL8OQnFIxLRxIOzrpT7I461vIm7dqaFMvAkodLDiy36l/OS8V9r4E1InSLEH+4h/0La4SWEBeA==";
private const string short634 = "s56QfTTHzvP0h9I5LNu1lq8KrRXdOficbnzbzT15WxST0psg03I+c6ZiCS/y2WsXTVzL183rQjrT7OxE4NBZMLh3g86QGNeIjarxrJnY3sqb43jeNmC8+EuTGpuDigEW0GEGMUGmv//pnwpmE9xnOseuLPPPJx2z5sWcCYHsgg==";
private const string short890 = "UiLJObGTgKTYkWQLnZbleG7MmLlGg1/uJWsoBCQbSAd1R2+2JMFgGbO+j3G2sR60Ds8ZFwQ7IVSOp7dAOb85LughCSkkTzSPiewTLohPw9LWUn1Cj6oi19C+de87VIM4abTwolUlSwsggm2zBnIMwZud5EKJAoYE4OQKnJX3cw==";
private const string rsaxml = "<RSAKeyValue><Modulus>hEoYW3qOJ/MySeZKIWf0nkKswvkB6xdn7EQQvxkd9EfAQMAVxyBsR/Rch407RTbcYl70mcotYKGYkAg0vq1miGFGfp8N+tum7zhBNJwFOd9k4H8A8g+5CUu15BZTw11mr68PCCJMTTNAstH3vx3S4aRgty9IlztfpQVi/e57u7M=</Modulus><Exponent>EQ==</Exponent><P>rlhw/xADkDYRn1U07RXuVb4WLAZwIpTc93xVD/Evz6o6/y/nEdzRa49ob9taKPsNKYqU9Bbec5+mt+xzIbgggw==</P><Q>wj8/FXZUvScP+EUuv4FeQxZVVwl4dduKZyQHWyM53K1y7Yv6xc821ie6Z7lWGSeS9Z7VYJtX4F/gGuZHWW2xEQ==</Q><DP>hVKwwww+9c7+asiwAJhM2Cfy1l9GsQhsvUD1wOWdCDbh0jOws06CBvUxvvMIte0oLtNi2MYxo7ZSUGmFN+cn6w==</DP><DQ>fbB0HPI21L7sNzvS9GLEhcMoKUJdAPd3rCZfHNqO2hXg9A9H6Wf2TlXxFfBk4xmbU6MCic3tkS76a8IuKs6BoQ==</DQ><InverseQ>b8VOOfI/6Wvx/ZE7NrmS/NSawO7wAZviBmoNwyO40si3N8bDJQgsCl1Zbz93mBUEiKG+BF8tf04Y85NDBilmKw==</InverseQ><D>TdE7gRrqNZ4dlOHRQNO9EczeGFY9XRzTuCgJ2dKKFzlD6dpnKdbWSHGf10QEv01ylDfbLUnAVvWlCW49JN54i5yjEBVAuflYlxM081+qExpzF6NCfgnufV5I5mS72mgm8fnWjv64oQaNJ4Q6RPmy+V1r0trvC9Nlt2kJkRarAjE=</D></RSAKeyValue>";
private RSAManaged rsa;
[TestFixtureSetUp]
public void FixtureSetUp ()
{
rsa = new RSAManaged ();
rsa.FromXmlString (rsaxml);
}
[Test]
public void ShortResults ()
{
byte[] decdata = PKCS1.Decrypt_v15 (rsa, Convert.FromBase64String (short017));
Assert.AreEqual ("<password>", Encoding.UTF8.GetString (decdata), "017");
decdata = PKCS1.Decrypt_v15 (rsa, Convert.FromBase64String (short063));
Assert.AreEqual ("<password>", Encoding.UTF8.GetString (decdata), "063");
decdata = PKCS1.Decrypt_v15 (rsa, Convert.FromBase64String (short136));
Assert.AreEqual ("<password>", Encoding.UTF8.GetString (decdata), "136");
decdata = PKCS1.Decrypt_v15 (rsa, Convert.FromBase64String (short264));
Assert.AreEqual ("<password>", Encoding.UTF8.GetString (decdata), "264");
decdata = PKCS1.Decrypt_v15 (rsa, Convert.FromBase64String (short482));
Assert.AreEqual ("<password>", Encoding.UTF8.GetString (decdata), "482");
decdata = PKCS1.Decrypt_v15 (rsa, Convert.FromBase64String (short631));
Assert.AreEqual ("<password>", Encoding.UTF8.GetString (decdata), "631");
decdata = PKCS1.Decrypt_v15 (rsa, Convert.FromBase64String (short634));
Assert.AreEqual ("<password>", Encoding.UTF8.GetString (decdata), "634");
decdata = PKCS1.Decrypt_v15 (rsa, Convert.FromBase64String (short890));
Assert.AreEqual ("<password>", Encoding.UTF8.GetString (decdata), "890");
}
private byte[] GetValue (int size)
{
byte[] data = new byte [size];
for (int i = 0; i < size; i++)
data[i] = (byte) (i + 1);
return data;
}
[Test]
public void PKCS15_SignAndVerify_UnknownHash ()
{
// this hash algorithm isn't known from CryptoConfig so no OID is available
MD5SHA1 hash = new MD5SHA1 ();
byte[] value = GetValue (hash.HashSize >> 3);
byte[] unknown = PKCS1.Sign_v15 (rsa, hash, value);
Assert.IsTrue (PKCS1.Verify_v15 (rsa, hash, value, unknown), "Verify");
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,98 @@
//
// SHA224ManagedTest.cs - NUnit Test Cases for SHA224Managed
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.Security.Cryptography;
using System.Text;
using Mono.Security.Cryptography;
namespace MonoTests.System.Security.Cryptography {
// References:
// a. RFC 3874 - A 224-bit One-way Hash Function: SHA-224, September 2004
// http://www.faqs.org/rfc/rfc3874.txt
// b. FIPS PUB 180-2: Secure Hash Standard
// http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
// we inherit from SHA224Test because all SHA224 implementation must return the
// same results (hence should run a common set of unit tests).
[TestFixture]
public class SHA224ManagedTest : SHA224Test {
[SetUp]
public override void SetUp ()
{
hash = new SHA224Managed ();
}
[Test]
public override void Create ()
{
// no need to repeat this test
}
// none of those values changes for a particuliar implementation of SHA224
[Test]
public override void StaticInfo ()
{
// test all values static for SHA224
base.StaticInfo ();
string className = hash.ToString ();
Assert.AreEqual (true, hash.CanReuseTransform, className + ".CanReuseTransform");
Assert.AreEqual (true, hash.CanTransformMultipleBlocks, className + ".CanTransformMultipleBlocks");
// FIXME: Change namespace when (or if) classes are moved into corlib
Assert.AreEqual ("Mono.Security.Cryptography.SHA224Managed", className, className + ".ToString()");
}
[Test]
public void FIPSCompliance_Test1 ()
{
SHA224 sha = (SHA224) hash;
// First test, we hash the string "abc"
FIPS186_Test1 (sha);
}
[Test]
public void FIPSCompliance_Test2 ()
{
SHA224 sha = (SHA224) hash;
// Second test, we hash the string "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
FIPS186_Test2 (sha);
}
[Test]
public void FIPSCompliance_Test3 ()
{
SHA224 sha = (SHA224) hash;
// Third test, we hash 1,000,000 times the character "a"
FIPS186_Test3 (sha);
}
}
}

View File

@@ -0,0 +1,235 @@
//
// SHA224Test.cs - NUnit Test Cases for SHA224
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Mono.Security.Cryptography;
namespace MonoTests.System.Security.Cryptography {
// References:
// a. RFC 3874 - A 224-bit One-way Hash Function: SHA-224, September 2004
// http://www.faqs.org/rfc/rfc3874.txt
// b. FIPS PUB 180-2: Secure Hash Standard
// http://csrc.nist.gov/publications/fips/fips180-2/fip180-2.txt
// SHA224 is a abstract class - so most of the test included here wont be tested
// on the abstract class but should be tested in ALL its descendants.
[TestFixture]
public class SHA224Test {
protected HashAlgorithm hash;
[SetUp]
public virtual void SetUp ()
{
hash = SHA224.Create ();
}
internal void AssertEquals (string msg, byte[] expected, byte[] actual)
{
if (expected == null) {
if (actual != null) {
string a = BitConverter.ToString (actual);
Assert.Fail (String.Format ("{0} - Expected null value but got: {1}", msg, a));
}
}
else if (actual == null) {
string e = BitConverter.ToString (expected);
Assert.Fail (String.Format ("{0} - Got null value but expected: {1}", msg, e));
}
Assert.AreEqual (BitConverter.ToString (expected), BitConverter.ToString (actual), msg);
}
// test vectors from RFC 3874 (as NIST FIPS 186-2 hasn't yet
// been officially updated for SHA224 test vectors).
private string input1 = "abc";
private string input2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
public void FIPS186_Test1 (SHA224 hash)
{
string className = hash.ToString ();
byte[] result = { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22, 0x86, 0x42,
0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3, 0x2A, 0xAD, 0xBC, 0xE4,
0xBD, 0xA0, 0xB3, 0xF7, 0xE3, 0x6C, 0x9D, 0xA7 };
byte[] input = Encoding.Default.GetBytes (input1);
string testName = className + " 1";
FIPS186_a (testName, hash, input, result);
FIPS186_b (testName, hash, input, result);
FIPS186_c (testName, hash, input, result);
FIPS186_d (testName, hash, input, result);
FIPS186_e (testName, hash, input, result);
}
public void FIPS186_Test2 (SHA224 hash)
{
string className = hash.ToString ();
byte[] result = { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC, 0x5D, 0xBA,
0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50, 0xB0, 0xC6, 0x45, 0x5C,
0xB4, 0xF5, 0x8B, 0x19, 0x52, 0x52, 0x25, 0x25 };
byte[] input = Encoding.Default.GetBytes (input2);
string testName = className + " 2";
FIPS186_a (testName, hash, input, result);
FIPS186_b (testName, hash, input, result);
FIPS186_c (testName, hash, input, result);
FIPS186_d (testName, hash, input, result);
FIPS186_e (testName, hash, input, result);
}
public void FIPS186_Test3 (SHA224 hash)
{
string className = hash.ToString ();
byte [] result = { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8, 0xBB, 0xB4,
0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B, 0xF0, 0x3F, 0x42, 0x58,
0x19, 0x48, 0xB2, 0xEE, 0x4E, 0xE7, 0xAD, 0x67 };
byte[] input = new byte [1000000];
for (int i = 0; i < 1000000; i++)
input[i] = 0x61; // a
string testName = className + " 3";
FIPS186_a (testName, hash, input, result);
FIPS186_b (testName, hash, input, result);
FIPS186_c (testName, hash, input, result);
FIPS186_d (testName, hash, input, result);
FIPS186_e (testName, hash, input, result);
}
public void FIPS186_a (string testName, SHA224 hash, byte[] input, byte[] result)
{
byte[] output = hash.ComputeHash (input);
AssertEquals (testName + ".a.1", result, output);
AssertEquals (testName + ".a.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void FIPS186_b (string testName, SHA224 hash, byte[] input, byte[] result)
{
byte[] output = hash.ComputeHash (input, 0, input.Length);
AssertEquals (testName + ".b.1", result, output);
AssertEquals (testName + ".b.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void FIPS186_c (string testName, SHA224 hash, byte[] input, byte[] result)
{
MemoryStream ms = new MemoryStream (input);
byte[] output = hash.ComputeHash (ms);
AssertEquals (testName + ".c.1", result, output);
AssertEquals (testName + ".c.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void FIPS186_d (string testName, SHA224 hash, byte[] input, byte[] result)
{
hash.TransformFinalBlock (input, 0, input.Length);
// Note: TransformFinalBlock doesn't return HashValue !
// AssertEquals( testName + ".d.1", result, output );
AssertEquals (testName + ".d", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void FIPS186_e (string testName, SHA224 hash, byte[] input, byte[] result)
{
byte[] copy = new byte [input.Length];
for (int i=0; i < input.Length - 1; i++)
hash.TransformBlock (input, i, 1, copy, i);
hash.TransformFinalBlock (input, input.Length - 1, 1);
// Note: TransformFinalBlock doesn't return HashValue !
// AssertEquals (testName + ".e.1", result, output);
AssertEquals (testName + ".e", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
[Test]
public virtual void Create ()
{
// Note: These tests will only be valid without a "machine.config" file
// or a "machine.config" file that do not modify the default algorithm
// configuration.
// FIXME: Change namespace when (or if) classes are moved into corlib
const string defaultSHA224 = "Mono.Security.Cryptography.SHA224Managed";
// try to build the default implementation
SHA224 hash = SHA224.Create ();
Assert.AreEqual (hash.ToString (), defaultSHA224, "SHA224.Create()");
// try to build, in every way, a SHA224 implementation
hash = SHA224.Create ("SHA224");
Assert.AreEqual (hash.ToString (), defaultSHA224, "SHA224.Create('SHA224')");
hash = SHA224.Create ("SHA-224");
Assert.AreEqual (hash.ToString (), defaultSHA224, "SHA224.Create('SHA-224')");
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void CreateIncorrect ()
{
// try to build an incorrect hash algorithms
hash = SHA224.Create ("MD5");
}
/* Uncomment when (or if) the SHA224 classes are moved into corlib
[Test]
public void CreateInvalid ()
{
// try to build invalid implementation
hash = SHA224.Create ("InvalidHash");
Assert.IsNull (hash, "SHA224.Create('InvalidHash')");
}*/
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void CreateNull ()
{
// try to build null implementation
hash = SHA224.Create (null);
}
// none of those values changes for any implementation of defaultSHA224
[Test]
public virtual void StaticInfo ()
{
string className = hash.ToString ();
Assert.AreEqual (224, hash.HashSize, className + ".HashSize");
Assert.AreEqual (1, hash.InputBlockSize, className + ".InputBlockSize");
Assert.AreEqual (1, hash.OutputBlockSize, className + ".OutputBlockSize");
}
}
}