2 // MACTripleDESTest.cs - NUnit Test Cases for MACTripleDES
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
10 using NUnit.Framework;
13 using System.Security.Cryptography;
15 namespace MonoTests.System.Security.Cryptography {
17 public class MACTripleDESTest : TestCase {
19 protected MACTripleDES algo;
21 private static byte[] key1 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
22 private static byte[] key2 = { 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01 };
23 private static byte[] key3 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
25 protected override void SetUp () {}
27 protected override void TearDown () {}
29 public void AssertEquals (string msg, byte[] array1, byte[] array2)
31 AllTests.AssertEquals (msg, array1, array2);
34 protected byte[] CombineKeys (byte[] key1, byte[] key2, byte[] key3)
36 int k1l = key1.Length;
37 int k2l = key2.Length;
38 int k3l = key3.Length;
39 byte[] key = new byte [k1l + k2l + k3l];
40 Array.Copy (key1, 0, key, 0, k1l);
41 Array.Copy (key2, 0, key, k1l, k2l);
42 Array.Copy (key3, 0, key, k1l + k2l, k3l);
46 public void TestConstructors ()
48 algo = new MACTripleDES ();
49 AssertNotNull ("MACTripleDES ()", algo);
51 byte[] key = new byte [24];
52 key [0] = 1; // so this isn't a weak key
54 algo = new MACTripleDES (key);
55 AssertNotNull ("MACTripleDES (key)", algo);
58 algo = new MACTripleDES (null);
59 Fail ("MACTripleDES (null) - Expected ArgumentNullException but got none");
61 catch (ArgumentNullException) {
65 Fail ("MACTripleDES (null) - Expected ArgumentNullException but got: " + e.ToString ());
68 algo = new MACTripleDES ("TripleDES", key);
69 AssertNotNull ("MACTripleDES ('TripleDES',key)", algo);
72 algo = new MACTripleDES ("TripleDES", null);
73 Fail ("MACTripleDES ('TripleDES', null) - Expected ArgumentNullException but got none");
75 catch (ArgumentNullException) {
79 Fail ("MACTripleDES ('TripleDES', null) - Expected ArgumentNullException but got: " + e.ToString ());
82 // funny null is a valid name!
83 algo = new MACTripleDES (null, key);
84 AssertNotNull ("MACTripleDES (null,key)", algo);
87 algo = new MACTripleDES (null, null);
88 Fail ("MACTripleDES (null, null) - Expected ArgumentNullException but got none");
90 catch (ArgumentNullException) {
94 Fail ("MACTripleDES (null, null) - Expected ArgumentNullException but got: " + e.ToString ());
98 public void TestInvariants ()
100 algo = new MACTripleDES ();
101 AssertEquals ("MACTripleDES.CanReuseTransform", true, algo.CanReuseTransform);
102 AssertEquals ("MACTripleDES.CanTransformMultipleBlocks", true, algo.CanTransformMultipleBlocks);
103 AssertEquals ("MACTripleDES.HashSize", 64, algo.HashSize);
104 AssertEquals ("MACTripleDES.InputBlockSize", 1, algo.InputBlockSize);
105 AssertEquals ("MACTripleDES.OutputBlockSize", 1, algo.OutputBlockSize);
106 AssertEquals ("MACTripleDES.ToString()", "System.Security.Cryptography.MACTripleDES", algo.ToString ());
107 AssertNotNull ("MACTripleDES.Key", algo.Key);
110 public void TestExceptions ()
112 byte[] key = CombineKeys (key1, key2, key3);
114 algo = new MACTripleDES ("DES", key);
115 Fail ("Expected InvalidCastException but got none");
117 catch (InvalidCastException) {
118 // do nothing, this is what we expect
120 catch (Exception e) {
121 Fail ("Expected InvalidCastException but got " + e.ToString ());
124 algo = new MACTripleDES (key);
127 algo.ComputeHash (new byte[1]);
128 Fail ("Expected ObjectDisposedException but got none");
130 catch (ObjectDisposedException) {
131 // do nothing, this is what we expect
133 catch (Exception e) {
134 Fail ("Expected ObjectDisposedException but got " + e.ToString ());
138 public void Check (string testName, byte[] key, byte[] data, byte[] result)
140 string classTestName = "MACTripleDES-" + testName;
141 CheckA (testName, key, data, result);
142 CheckB (testName, key, data, result);
143 CheckC (testName, key, data, result);
144 CheckD (testName, key, data, result);
145 CheckE (testName, key, data, result);
148 // - Test constructor #1 ()
149 // - Test ComputeHash (byte[]);
150 public void CheckA (string testName, byte[] key, byte[] data, byte[] result)
152 algo = new MACTripleDES ();
154 byte[] hmac = algo.ComputeHash (data);
155 AssertEquals (testName + "a1", result, hmac);
156 AssertEquals (testName + "a2", result, algo.Hash);
159 // - Test constructor #2 (byte[])
160 // - Test ComputeHash (byte[], int, int);
161 public void CheckB (string testName, byte[] key, byte[] data, byte[] result)
163 algo = new MACTripleDES (key);
164 byte[] hmac = algo.ComputeHash (data, 0, data.Length);
165 AssertEquals (testName + "b1", result, hmac);
166 AssertEquals (testName + "b2", result, algo.Hash);
169 // - Test constructor #3 (string, byte[])
170 // - Test ComputeHash (stream);
171 public void CheckC (string testName, byte[] key, byte[] data, byte[] result)
173 algo = new MACTripleDES ("TripleDES", key);
175 MemoryStream ms = new MemoryStream (data);
176 byte[] hmac = algo.ComputeHash (ms);
177 AssertEquals (testName + "c1", result, hmac);
178 AssertEquals (testName + "c2", result, algo.Hash);
181 // - Test TransformFinalBlock - alone;
182 public void CheckD (string testName, byte[] key, byte[] data, byte[] result)
184 algo = new MACTripleDES ();
186 // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
187 algo.TransformFinalBlock (data, 0, data.Length);
188 AssertEquals (testName + "d", result, algo.Hash);
191 // - Test TransformBlock/TransformFinalBlock
192 public void CheckE (string testName, byte[] key, byte[] data, byte[] result)
194 algo = new MACTripleDES ();
196 byte[] copy = new byte [data.Length];
197 // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
198 for (int i=0; i < data.Length - 1; i++)
199 algo.TransformBlock (data, i, 1, copy, i);
200 algo.TransformFinalBlock (data, data.Length - 1, 1);
201 AssertEquals (testName + "e", result, algo.Hash);
204 // Here data is smaller than the 3DES block size (8 bytes)
205 public void Test_A1 ()
207 byte[] key = CombineKeys (key1, key2, key3);
208 byte[] expected = { 0x86, 0xE9, 0x65, 0xBD, 0x1E, 0xC4, 0x44, 0x61 };
209 byte[] data = new byte [7];
210 Check ("3DESMAC-A1", key, data, expected);
213 // Here data is exactly one 3DES block size (8 bytes)
214 public void Test_A2 ()
216 byte[] key = CombineKeys (key1, key2, key3);
217 byte[] expected = { 0x23, 0xD6, 0x92, 0xA0, 0x80, 0x6E, 0xC9, 0x30 };
218 byte[] data = new byte [8];
219 Check ("3DESMAC-A2", key, data, expected);
222 // Here data is more then one 3DES block size (8 bytes)
223 public void Test_A3 ()
225 byte[] key = CombineKeys (key1, key2, key3);
226 // note: same result as A2 because of the Zero padding (and that
227 // we use zeros as data
228 byte[] expected = { 0x23, 0xD6, 0x92, 0xA0, 0x80, 0x6E, 0xC9, 0x30 };
229 byte[] data = new byte [14];
230 Check ("3DESMAC-A3", key, data, expected);
233 // Here data is a multiple of 3DES block size (8 bytes)
234 public void Test_A4 ()
236 byte[] key = CombineKeys (key1, key2, key3);
237 byte[] expected = { 0xD6, 0x6D, 0x75, 0xD4, 0x75, 0xF1, 0x01, 0x71 };
238 byte[] data = new byte [48];
239 Check ("3DESMAC-A4", key, data, expected);