// Sebastien Pouliot <spouliot@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004,2008 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
// http://www.faqs.org/rfcs/rfc3211.html
[TestFixture]
- public class Rfc2898DeriveBytesTest : Assertion {
-
- public void AssertEquals (string msg, byte[] array1, byte[] array2)
- {
- AllTests.AssertEquals (msg, array1, array2);
- }
+ public class Rfc2898DeriveBytesTest {
static private byte[] salt = { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 };
public void ConstructorPasswordSalt ()
{
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
- AssertEquals ("IterationCount", 1000, pkcs5.IterationCount);
- AssertEquals ("Salt", salt, pkcs5.Salt);
+ Assert.AreEqual (1000, pkcs5.IterationCount, "IterationCount");
+ Assert.AreEqual (salt, pkcs5.Salt, "Salt");
}
[Test]
public void ConstructorPasswordSaltIterations ()
{
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
- AssertEquals ("IterationCount", 5, pkcs5.IterationCount);
- AssertEquals ("Salt", salt, pkcs5.Salt);
+ Assert.AreEqual (5, pkcs5.IterationCount, "IterationCount");
+ Assert.AreEqual (salt, pkcs5.Salt, "Salt");
}
[Test]
public void ConstructorPasswordSaltLength ()
{
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", 8);
- AssertEquals ("IterationCount", 1000, pkcs5.IterationCount);
- AssertEquals ("Salt", 8, pkcs5.Salt.Length);
+ Assert.AreEqual (1000, pkcs5.IterationCount, "IterationCount");
+ Assert.AreEqual (8, pkcs5.Salt.Length, "Salt");
}
[Test]
public void ConstructorPasswordSaltLengthIterations ()
{
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", 8, 5);
- AssertEquals ("IterationCount", 5, pkcs5.IterationCount);
- AssertEquals ("Salt", 8, pkcs5.Salt.Length);
+ Assert.AreEqual (5, pkcs5.IterationCount, "IterationCount");
+ Assert.AreEqual (8, pkcs5.Salt.Length, "Salt");
}
[Test]
public void IterationCount ()
{
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
- AssertEquals ("IterationCount", 5, pkcs5.IterationCount);
+ Assert.AreEqual (5, pkcs5.IterationCount, "IterationCount");
pkcs5.IterationCount = Int32.MaxValue;
- AssertEquals ("IterationCount", Int32.MaxValue, pkcs5.IterationCount);
+ Assert.AreEqual (Int32.MaxValue, pkcs5.IterationCount, "IterationCount");
}
[Test]
public void SaltNew ()
{
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
- AssertEquals ("Salt", salt, pkcs5.Salt);
+ Assert.AreEqual (salt, pkcs5.Salt, "Salt");
byte[] newSalt = (byte[]) salt.Clone ();
newSalt [0] = 0xFF;
pkcs5.Salt = newSalt;
- AssertEquals ("Salt(new)", newSalt, pkcs5.Salt);
+ Assert.AreEqual (newSalt, pkcs5.Salt, "Salt(new)");
}
[Test]
public void SaltCantModifyInternal ()
{
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
- AssertEquals ("Salt", salt, pkcs5.Salt);
+ Assert.AreEqual (salt, pkcs5.Salt, "Salt");
byte[] modSalt = (byte[]) salt.Clone ();
modSalt [0] = 0xFF;
- Assert ("Can't modify internal salt", (BitConverter.ToString(pkcs5.Salt) != BitConverter.ToString(modSalt)));
+ Assert.IsFalse ((BitConverter.ToString (pkcs5.Salt) == BitConverter.ToString (modSalt)), "Can't modify internal salt");
}
[Test]
byte[] expected = { 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6 };
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
byte[] key = pkcs5.GetBytes (0);
- AssertEquals ("GetBytesZero", 0, key.Length);
+ Assert.AreEqual (0, key.Length, "GetBytesZero");
}
[Test]
byte[] key = pkcs5.GetBytes (32768);
// just check the last 32 bytes
string actual = BitConverter.ToString (key, key.Length - 32);
- AssertEquals ("Endian", expected, actual);
+ Assert.AreEqual (expected, actual, "Endian");
}
[Test]
byte[] expected = { 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6 };
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
byte[] key = pkcs5.GetBytes (8);
- AssertEquals ("RFC3211_TC1", expected, key);
+ Assert.AreEqual (expected, key, "RFC3211_TC1");
}
[Test]
byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE, 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86 };
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
byte[] key = pkcs5.GetBytes (16);
- AssertEquals ("RFC3211_TC2", expected, key);
+ Assert.AreEqual (expected, key, "RFC3211_TC2");
}
[Test]
byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE, 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86, 0x07, 0x12, 0x63, 0x80, 0xcc, 0x47, 0xab, 0x2d, 0xa6, 0xcc, 0xda, 0xfb, 0x26, 0x83, 0xdf, 0xe8 };
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
byte[] key = pkcs5.GetBytes (32);
- AssertEquals ("RFC3211_TC2_TwoBlocks", expected, key);
+ Assert.AreEqual (expected, key, "RFC3211_TC2_TwoBlocks");
}
[Test]
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
byte[] key1 = pkcs5.GetBytes (8);
byte[] expected_part1 = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE };
- AssertEquals ("RFC3211_TC2_Splitted_OneBlock-1", expected_part1, key1);
+ Assert.AreEqual (expected_part1, key1, "RFC3211_TC2_Splitted_OneBlock-1");
byte[] expected_part2 = { 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86 };
byte[] key2 = pkcs5.GetBytes (8);
- AssertEquals ("RFC3211_TC2_Splitted_OneBlock-2", expected_part2, key2);
+ Assert.AreEqual (expected_part2, key2, "RFC3211_TC2_Splitted_OneBlock-2");
}
[Test]
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
byte[] key1 = pkcs5.GetBytes (16);
byte[] expected_part1 = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE, 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86 };
- AssertEquals ("RFC3211_TC2_Splitted_TwoBlocks-1", expected_part1, key1);
+ Assert.AreEqual (expected_part1, key1, "RFC3211_TC2_Splitted_TwoBlocks-1");
byte[] expected_part2 = { 0x07, 0x12, 0x63, 0x80, 0xcc, 0x47, 0xab, 0x2d, 0xa6, 0xcc, 0xda, 0xfb, 0x26, 0x83, 0xdf, 0xe8 };
byte[] key2 = pkcs5.GetBytes (16);
- AssertEquals ("RFC3211_TC2_Splitted_TwoBlocks-2", expected_part2, key2);
+ Assert.AreEqual (expected_part2, key2, "RFC3211_TC2_Splitted_TwoBlocks-2");
}
[Test]
byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE };
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
byte[] key1 = pkcs5.GetBytes (8);
- AssertEquals ("RFC3211_TC2_part1", expected, key1);
+ Assert.AreEqual (expected, key1, "RFC3211_TC2_part1");
pkcs5.Reset ();
byte[] key2 = pkcs5.GetBytes (8);
- AssertEquals ("RFC3211_TC2_part2", expected, key2);
+ Assert.AreEqual (expected, key2, "RFC3211_TC2_part2");
+ }
+
+ private void CompareBuffers (byte [] big, byte [] a, byte [] b)
+ {
+ int n = 0;
+ for (int i = 0; i < a.Length; i++) {
+ Assert.AreEqual (big [n], a [i], n.ToString ());
+ n++;
+ }
+ for (int i = 0; i < b.Length; i++) {
+ Assert.AreEqual (big [n], b [i], n.ToString ());
+ n++;
+ }
+ }
+
+ [Test]
+ public void SplitCalls_Reset ()
+ {
+ Rfc2898DeriveBytes keygen = new Rfc2898DeriveBytes ("password", salt, 12345);
+ byte [] big = keygen.GetBytes (48);
+
+ keygen.Reset ();
+ byte [] a = keygen.GetBytes (16);
+ byte [] b = keygen.GetBytes (32);
+
+ CompareBuffers (big, a, b);
+ }
+
+ [Test]
+ public void SplitCalls_TwoInstances ()
+ {
+ Rfc2898DeriveBytes keygen1 = new Rfc2898DeriveBytes ("password", salt, 12345);
+ byte [] big = keygen1.GetBytes (48);
+
+ Rfc2898DeriveBytes keygen2 = new Rfc2898DeriveBytes ("password", salt, 12345);
+ byte [] a = keygen2.GetBytes (16);
+ byte [] b = keygen2.GetBytes (32);
+
+ CompareBuffers (big, a, b);
}
}
}