2003-01-19 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / MACTripleDESTest.cs
1 //
2 // MACTripleDESTest.cs - NUnit Test Cases for MACTripleDES
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.IO;
13 using System.Security.Cryptography;
14
15 namespace MonoTests.System.Security.Cryptography {
16
17 public class MACTripleDESTest : TestCase {
18
19         protected MACTripleDES algo;
20
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 };
24
25         protected override void SetUp () {}
26
27         protected override void TearDown () {}
28
29         public void AssertEquals (string msg, byte[] array1, byte[] array2) 
30         {
31                 AllTests.AssertEquals (msg, array1, array2);
32         }
33
34         protected byte[] CombineKeys (byte[] key1, byte[] key2, byte[] key3) 
35         {
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);
43                 return key;
44         }
45
46         public void TestConstructors () 
47         {
48                 algo = new MACTripleDES ();
49                 AssertNotNull ("MACTripleDES ()", algo);
50
51                 byte[] key = new byte [24];
52                 key [0] = 1;  // so this isn't a weak key
53                 key [23] = 1;
54                 algo = new MACTripleDES (key);
55                 AssertNotNull ("MACTripleDES (key)", algo);
56
57                 try {
58                         algo = new MACTripleDES (null);
59                         Fail ("MACTripleDES (null) - Expected ArgumentNullException but got none");
60                 }
61                 catch (ArgumentNullException) {
62                         // this is expected
63                 }
64                 catch (Exception e) {
65                         Fail ("MACTripleDES (null) - Expected ArgumentNullException but got: " + e.ToString ());
66                 }
67
68                 algo = new MACTripleDES ("TripleDES", key);
69                 AssertNotNull ("MACTripleDES ('TripleDES',key)", algo);
70
71                 try {
72                         algo = new MACTripleDES ("TripleDES", null);
73                         Fail ("MACTripleDES ('TripleDES', null) - Expected ArgumentNullException but got none");
74                 }
75                 catch (ArgumentNullException) {
76                         // this is expected
77                 }
78                 catch (Exception e) {
79                         Fail ("MACTripleDES ('TripleDES', null) - Expected ArgumentNullException but got: " + e.ToString ());
80                 }
81
82                 // funny null is a valid name!
83                 algo = new MACTripleDES (null, key);
84                 AssertNotNull ("MACTripleDES (null,key)", algo);
85
86                 try {
87                         algo = new MACTripleDES (null, null);
88                         Fail ("MACTripleDES (null, null) - Expected ArgumentNullException but got none");
89                 }
90                 catch (ArgumentNullException) {
91                         // this is expected
92                 }
93                 catch (Exception e) {
94                         Fail ("MACTripleDES (null, null) - Expected ArgumentNullException but got: " + e.ToString ());
95                 }
96         }
97
98         public void TestInvariants () 
99         {
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);
108         }
109
110         public void TestExceptions () 
111         {
112                 byte[] key = CombineKeys (key1, key2, key3);
113                 try {
114                         algo = new MACTripleDES ("DES", key);
115                         Fail ("Expected InvalidCastException but got none");
116                 }
117                 catch (InvalidCastException) {
118                         // do nothing, this is what we expect
119                 }
120                 catch (Exception e) {
121                         Fail ("Expected InvalidCastException but got " + e.ToString ());
122                 }
123
124                 algo = new MACTripleDES (key);
125                 algo.Clear ();
126                 try {
127                         algo.ComputeHash (new byte[1]);
128                         Fail ("Expected ObjectDisposedException but got none");
129                 }
130                 catch (ObjectDisposedException) {
131                         // do nothing, this is what we expect
132                 }
133                 catch (Exception e) {
134                         Fail ("Expected ObjectDisposedException but got " + e.ToString ());
135                 }
136         }
137
138         public void Check (string testName, byte[] key, byte[] data, byte[] result) 
139         {
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);
146         }
147
148         // - Test constructor #1 ()
149         // - Test ComputeHash (byte[]);
150         public void CheckA (string testName, byte[] key, byte[] data, byte[] result) 
151         {
152                 algo = new MACTripleDES ();
153                 algo.Key = key;
154                 byte[] hmac = algo.ComputeHash (data);
155                 AssertEquals (testName + "a1", result, hmac);
156                 AssertEquals (testName + "a2", result, algo.Hash);
157         }
158
159         // - Test constructor #2 (byte[])
160         // - Test ComputeHash (byte[], int, int);
161         public void CheckB (string testName, byte[] key, byte[] data, byte[] result) 
162         {
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);
167         }
168         
169         // - Test constructor #3 (string, byte[])
170         // - Test ComputeHash (stream);
171         public void CheckC (string testName, byte[] key, byte[] data, byte[] result) 
172         {
173                 algo = new MACTripleDES ("TripleDES", key);
174                 algo.Key = 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);
179         }
180
181         // - Test TransformFinalBlock - alone;
182         public void CheckD (string testName, byte[] key, byte[] data, byte[] result) 
183         {
184                 algo = new MACTripleDES ();
185                 algo.Key = key;
186                 // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
187                 algo.TransformFinalBlock (data, 0, data.Length);
188                 AssertEquals (testName + "d", result, algo.Hash);
189         }
190
191         // - Test TransformBlock/TransformFinalBlock
192         public void CheckE (string testName, byte[] key, byte[] data, byte[] result) 
193         {
194                 algo = new MACTripleDES ();
195                 algo.Key = key;
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);
202         }
203
204         // Here data is smaller than the 3DES block size (8 bytes)
205         public void Test_A1 () 
206         {
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);
211         }
212
213         // Here data is exactly one 3DES block size (8 bytes)
214         public void Test_A2 () 
215         {
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);
220         }
221
222         // Here data is more then one 3DES block size (8 bytes)
223         public void Test_A3 () 
224         {
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);
231         }
232
233         // Here data is a multiple of 3DES block size (8 bytes)
234         public void Test_A4 () 
235         {
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);
240         }
241 }
242
243 }