This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 [TestFixture]
18 public class MACTripleDESTest : Assertion {
19
20         protected MACTripleDES algo;
21
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 };
25
26         public void AssertEquals (string msg, byte[] array1, byte[] array2) 
27         {
28                 AllTests.AssertEquals (msg, array1, array2);
29         }
30
31         protected byte[] CombineKeys (byte[] key1, byte[] key2, byte[] key3) 
32         {
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);
40                 return key;
41         }
42
43         [Test]
44         public void ConstructorEmpty () 
45         {
46                 algo = new MACTripleDES ();
47                 AssertNotNull ("MACTripleDES ()", algo);
48         }
49
50         [Test]
51         public void ConstructorKey () 
52         {
53                 byte[] key = CombineKeys (key1, key2, key3);
54                 algo = new MACTripleDES (key);
55                 AssertNotNull ("MACTripleDES (key)", algo);
56         }
57
58         [Test]
59         [ExpectedException (typeof (ArgumentNullException))]
60         public void ConstructorKeyNull () 
61         {
62                 algo = new MACTripleDES (null);
63         }
64
65         [Test]
66         public void ConstructorNameKey () 
67         {
68                 byte[] key = CombineKeys (key1, key2, key3);
69                 algo = new MACTripleDES ("TripleDES", key);
70                 AssertNotNull ("MACTripleDES ('TripleDES',key)", algo);
71         }
72
73         [Test]
74         // LAMESPEC: [ExpectedException (typeof (ArgumentNullException))]
75         public void ConstructorNameNullKey () 
76         {
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);
81         }
82
83         [Test]
84         [ExpectedException (typeof (ArgumentNullException))]
85         public void ConstructorNameNullKeyNull () 
86         {
87                 algo = new MACTripleDES (null, null);
88         }
89
90         [Test]
91         public void Invariants () 
92         {
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);
101         }
102
103         [Test]
104         [ExpectedException (typeof (InvalidCastException))]
105         public void InvalidAlgorithmName () 
106         {
107                 byte[] key = CombineKeys (key1, key2, key3);
108                 algo = new MACTripleDES ("DES", key);
109         }
110
111         [Test]
112         [ExpectedException (typeof (ObjectDisposedException))]
113         public void ObjectDisposed () 
114         {
115                 byte[] key = CombineKeys (key1, key2, key3);
116                 algo = new MACTripleDES (key);
117                 algo.Clear ();
118                 algo.ComputeHash (new byte[1]);
119         }
120
121         public void Check (string testName, byte[] key, byte[] data, byte[] result) 
122         {
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);
129         }
130
131         // - Test constructor #1 ()
132         // - Test ComputeHash (byte[]);
133         public void CheckA (string testName, byte[] key, byte[] data, byte[] result) 
134         {
135                 algo = new MACTripleDES ();
136                 algo.Key = key;
137                 byte[] hmac = algo.ComputeHash (data);
138                 AssertEquals (testName + "a1", result, hmac);
139                 AssertEquals (testName + "a2", result, algo.Hash);
140         }
141
142         // - Test constructor #2 (byte[])
143         // - Test ComputeHash (byte[], int, int);
144         public void CheckB (string testName, byte[] key, byte[] data, byte[] result) 
145         {
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);
150         }
151         
152         // - Test constructor #3 (string, byte[])
153         // - Test ComputeHash (stream);
154         public void CheckC (string testName, byte[] key, byte[] data, byte[] result) 
155         {
156                 algo = new MACTripleDES ("TripleDES", key);
157                 algo.Key = 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);
162         }
163
164         // - Test TransformFinalBlock - alone;
165         public void CheckD (string testName, byte[] key, byte[] data, byte[] result) 
166         {
167                 algo = new MACTripleDES ();
168                 algo.Key = key;
169                 // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
170                 algo.TransformFinalBlock (data, 0, data.Length);
171                 AssertEquals (testName + "d", result, algo.Hash);
172         }
173
174         // - Test TransformBlock/TransformFinalBlock
175         public void CheckE (string testName, byte[] key, byte[] data, byte[] result) 
176         {
177                 algo = new MACTripleDES ();
178                 algo.Key = key;
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);
185         }
186
187         // Here data is smaller than the 3DES block size (8 bytes)
188         [Test]
189         public void SmallerThanOneBlockSize () 
190         {
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);
195         }
196
197         // Here data is exactly one 3DES block size (8 bytes)
198         [Test]
199         public void ExactlyOneBlockSize () 
200         {
201                 byte[] key = CombineKeys (key1, key2, key3);
202 #if NET_1_0
203                 // Believe it or not, MACTripleDES returns different values in 1.1
204                 byte[] expected = { 0x23, 0xD6, 0x92, 0xA0, 0x80, 0x6E, 0xC9, 0x30 };
205 #else
206                 byte[] expected = { 0x86, 0xE9, 0x65, 0xBD, 0x1E, 0xC4, 0x44, 0x61 };
207 #endif
208                 byte[] data = new byte [8];
209                 Check ("3DESMAC-A2", key, data, expected);
210         }
211
212         // Here data is more then one 3DES block size (8 bytes)
213         [Test]
214         public void MoreThanOneBlockSize () 
215         {
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);
222         }
223
224         // Here data is a multiple of 3DES block size (8 bytes)
225         [Test]
226         public void ExactMultipleBlockSize () 
227         {
228                 byte[] key = CombineKeys (key1, key2, key3);
229 #if NET_1_0
230                 // Believe it or not, MACTripleDES returns different values in 1.1
231                 byte[] expected = { 0xD6, 0x6D, 0x75, 0xD4, 0x75, 0xF1, 0x01, 0x71 };
232 #else
233                 byte[] expected = { 0xA3, 0x0E, 0x34, 0x26, 0x8B, 0x49, 0xEF, 0x49 };
234 #endif
235                 byte[] data = new byte [48];
236                 Check ("3DESMAC-A4", key, data, expected);
237         }
238 }
239
240 }