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 {
18 public class MACTripleDESTest : Assertion {
20 protected MACTripleDES algo;
22 private static byte[] key1 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
23 private static byte[] key2 = { 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01 };
24 private static byte[] key3 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
26 public void AssertEquals (string msg, byte[] array1, byte[] array2)
28 AllTests.AssertEquals (msg, array1, array2);
31 protected byte[] CombineKeys (byte[] key1, byte[] key2, byte[] key3)
33 int k1l = key1.Length;
34 int k2l = key2.Length;
35 int k3l = key3.Length;
36 byte[] key = new byte [k1l + k2l + k3l];
37 Array.Copy (key1, 0, key, 0, k1l);
38 Array.Copy (key2, 0, key, k1l, k2l);
39 Array.Copy (key3, 0, key, k1l + k2l, k3l);
44 public void ConstructorEmpty ()
46 algo = new MACTripleDES ();
47 AssertNotNull ("MACTripleDES ()", algo);
51 public void ConstructorKey ()
53 byte[] key = CombineKeys (key1, key2, key3);
54 algo = new MACTripleDES (key);
55 AssertNotNull ("MACTripleDES (key)", algo);
59 [ExpectedException (typeof (ArgumentNullException))]
60 public void ConstructorKeyNull ()
62 algo = new MACTripleDES (null);
66 public void ConstructorNameKey ()
68 byte[] key = CombineKeys (key1, key2, key3);
69 algo = new MACTripleDES ("TripleDES", key);
70 AssertNotNull ("MACTripleDES ('TripleDES',key)", algo);
74 // LAMESPEC: [ExpectedException (typeof (ArgumentNullException))]
75 public void ConstructorNameNullKey ()
77 byte[] key = CombineKeys (key1, key2, key3);
78 // funny null is a valid name!
79 algo = new MACTripleDES (null, key);
80 AssertNotNull ("MACTripleDES (null,key)", algo);
84 [ExpectedException (typeof (ArgumentNullException))]
85 public void ConstructorNameNullKeyNull ()
87 algo = new MACTripleDES (null, null);
91 public void Invariants ()
93 algo = new MACTripleDES ();
94 AssertEquals ("MACTripleDES.CanReuseTransform", true, algo.CanReuseTransform);
95 AssertEquals ("MACTripleDES.CanTransformMultipleBlocks", true, algo.CanTransformMultipleBlocks);
96 AssertEquals ("MACTripleDES.HashSize", 64, algo.HashSize);
97 AssertEquals ("MACTripleDES.InputBlockSize", 1, algo.InputBlockSize);
98 AssertEquals ("MACTripleDES.OutputBlockSize", 1, algo.OutputBlockSize);
99 AssertEquals ("MACTripleDES.ToString()", "System.Security.Cryptography.MACTripleDES", algo.ToString ());
100 AssertNotNull ("MACTripleDES.Key", algo.Key);
104 [ExpectedException (typeof (InvalidCastException))]
105 public void InvalidAlgorithmName ()
107 byte[] key = CombineKeys (key1, key2, key3);
108 algo = new MACTripleDES ("DES", key);
112 [ExpectedException (typeof (ObjectDisposedException))]
113 public void ObjectDisposed ()
115 byte[] key = CombineKeys (key1, key2, key3);
116 algo = new MACTripleDES (key);
118 algo.ComputeHash (new byte[1]);
121 public void Check (string testName, byte[] key, byte[] data, byte[] result)
123 string classTestName = "MACTripleDES-" + testName;
124 CheckA (testName, key, data, result);
125 CheckB (testName, key, data, result);
126 CheckC (testName, key, data, result);
127 CheckD (testName, key, data, result);
128 CheckE (testName, key, data, result);
131 // - Test constructor #1 ()
132 // - Test ComputeHash (byte[]);
133 public void CheckA (string testName, byte[] key, byte[] data, byte[] result)
135 algo = new MACTripleDES ();
137 byte[] hmac = algo.ComputeHash (data);
138 AssertEquals (testName + "a1", result, hmac);
139 AssertEquals (testName + "a2", result, algo.Hash);
142 // - Test constructor #2 (byte[])
143 // - Test ComputeHash (byte[], int, int);
144 public void CheckB (string testName, byte[] key, byte[] data, byte[] result)
146 algo = new MACTripleDES (key);
147 byte[] hmac = algo.ComputeHash (data, 0, data.Length);
148 AssertEquals (testName + "b1", result, hmac);
149 AssertEquals (testName + "b2", result, algo.Hash);
152 // - Test constructor #3 (string, byte[])
153 // - Test ComputeHash (stream);
154 public void CheckC (string testName, byte[] key, byte[] data, byte[] result)
156 algo = new MACTripleDES ("TripleDES", key);
158 MemoryStream ms = new MemoryStream (data);
159 byte[] hmac = algo.ComputeHash (ms);
160 AssertEquals (testName + "c1", result, hmac);
161 AssertEquals (testName + "c2", result, algo.Hash);
164 // - Test TransformFinalBlock - alone;
165 public void CheckD (string testName, byte[] key, byte[] data, byte[] result)
167 algo = new MACTripleDES ();
169 // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
170 algo.TransformFinalBlock (data, 0, data.Length);
171 AssertEquals (testName + "d", result, algo.Hash);
174 // - Test TransformBlock/TransformFinalBlock
175 public void CheckE (string testName, byte[] key, byte[] data, byte[] result)
177 algo = new MACTripleDES ();
179 byte[] copy = new byte [data.Length];
180 // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
181 for (int i=0; i < data.Length - 1; i++)
182 algo.TransformBlock (data, i, 1, copy, i);
183 algo.TransformFinalBlock (data, data.Length - 1, 1);
184 AssertEquals (testName + "e", result, algo.Hash);
187 // Here data is smaller than the 3DES block size (8 bytes)
189 public void SmallerThanOneBlockSize ()
191 byte[] key = CombineKeys (key1, key2, key3);
192 byte[] expected = { 0x86, 0xE9, 0x65, 0xBD, 0x1E, 0xC4, 0x44, 0x61 };
193 byte[] data = new byte [7];
194 Check ("3DESMAC-A1", key, data, expected);
197 // Here data is exactly one 3DES block size (8 bytes)
199 public void ExactlyOneBlockSize ()
201 byte[] key = CombineKeys (key1, key2, key3);
203 // Believe it or not, MACTripleDES returns different values in 1.1
204 byte[] expected = { 0x23, 0xD6, 0x92, 0xA0, 0x80, 0x6E, 0xC9, 0x30 };
206 byte[] expected = { 0x86, 0xE9, 0x65, 0xBD, 0x1E, 0xC4, 0x44, 0x61 };
208 byte[] data = new byte [8];
209 Check ("3DESMAC-A2", key, data, expected);
212 // Here data is more then one 3DES block size (8 bytes)
214 public void MoreThanOneBlockSize ()
216 byte[] key = CombineKeys (key1, key2, key3);
217 // note: same result as A2 because of the Zero padding (and that
218 // we use zeros as data
219 byte[] expected = { 0x23, 0xD6, 0x92, 0xA0, 0x80, 0x6E, 0xC9, 0x30 };
220 byte[] data = new byte [14];
221 Check ("3DESMAC-A3", key, data, expected);
224 // Here data is a multiple of 3DES block size (8 bytes)
226 public void ExactMultipleBlockSize ()
228 byte[] key = CombineKeys (key1, key2, key3);
230 // Believe it or not, MACTripleDES returns different values in 1.1
231 byte[] expected = { 0xD6, 0x6D, 0x75, 0xD4, 0x75, 0xF1, 0x01, 0x71 };
233 byte[] expected = { 0xA3, 0x0E, 0x34, 0x26, 0x8B, 0x49, 0xEF, 0x49 };
235 byte[] data = new byte [48];
236 Check ("3DESMAC-A4", key, data, expected);