Merge pull request #823 from DavidKarlas/master
[mono.git] / mcs / class / Mono.Security / Test / Mono.Security.Cryptography / MD4Test.cs
1 //
2 // MD4Test.cs - NUnit Test Cases for MD4 (RFC1320)
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
10
11 using System;
12 using System.IO;
13 using System.Security.Cryptography;
14 using System.Text;
15
16 using Mono.Security.Cryptography;
17 using NUnit.Framework;
18
19 namespace MonoTests.Mono.Security.Cryptography {
20
21         // References:
22         // a.   The MD4 Message-Digest Algorithm
23         //      http://www.ietf.org/rfc/RFC1320.txt
24
25         // MD4 is a abstract class - so ALL of the test included here wont be tested
26         // on the abstract class but should be tested in ALL its descendants.
27         public abstract class MD4Test {
28
29                 protected MD4 hash;
30
31                 static void AssertEquals (string msg, int expected, int actual)
32                 {
33                         Assert.AreEqual (expected, actual, msg);
34                 }
35
36                 // because most crypto stuff works with byte[] buffers
37                 static public void AssertEquals (string msg, byte[] array1, byte[] array2) 
38                 {
39                         if ((array1 == null) && (array2 == null))
40                                 return;
41                         if (array1 == null)
42                                 Assert.Fail (msg + " -> First array is NULL");
43                         if (array2 == null)
44                                 Assert.Fail (msg + " -> Second array is NULL");
45
46                         bool a = (array1.Length == array2.Length);
47                         if (a) {
48                                 for (int i = 0; i < array1.Length; i++) {
49                                         if (array1 [i] != array2 [i]) {
50                                                 a = false;
51                                                 break;
52                                         }
53                                 }
54                         }
55                         if (array1.Length > 0) {
56                                 msg += " -> Expected " + BitConverter.ToString (array1, 0);
57                                 msg += " is different than " + BitConverter.ToString (array2, 0);
58                         }
59                         Assert.IsTrue (a, msg);
60                 }
61
62                 // MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
63                 [Test]
64                 public void RFC1320_Test1 () 
65                 {
66                         string className = hash.ToString ();
67                         byte[] result = { 0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0 };
68                         byte[] input = new byte [0];
69
70                         string testName = className + " 1";
71                         RFC1320_a (testName, hash, input, result);
72                         RFC1320_b (testName, hash, input, result);
73                         RFC1320_c (testName, hash, input, result);
74                         RFC1320_d (testName, hash, input, result);
75                         // N/A RFC1320_e (testName, hash, input, result);
76                 }
77
78                 // MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
79                 [Test]
80                 public void RFC1320_Test2 () 
81                 {
82                         string className = hash.ToString ();
83                         byte[] result = { 0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46, 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24 };
84                         byte[] input = Encoding.Default.GetBytes ("a");
85
86                         string testName = className + " 2";
87                         RFC1320_a (testName, hash, input, result);
88                         RFC1320_b (testName, hash, input, result);
89                         RFC1320_c (testName, hash, input, result);
90                         RFC1320_d (testName, hash, input, result);
91                         RFC1320_e (testName, hash, input, result);
92                 }
93
94                 // MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
95                 [Test]
96                 public void RFC1320_Test3 () 
97                 {
98                         string className = hash.ToString ();
99                         byte[] result = { 0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d };
100                         byte[] input = Encoding.Default.GetBytes ("abc");
101
102                         string testName = className + " 3";
103                         RFC1320_a (testName, hash, input, result);
104                         RFC1320_b (testName, hash, input, result);
105                         RFC1320_c (testName, hash, input, result);
106                         RFC1320_d (testName, hash, input, result);
107                         RFC1320_e (testName, hash, input, result);
108                 }
109
110                 // MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
111                 [Test]
112                 public void RFC1320_Test4 () 
113                 {
114                         string className = hash.ToString ();
115                         byte[] result = { 0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b };
116                         byte[] input = Encoding.Default.GetBytes ("message digest");
117
118                         string testName = className + " 4";
119                         RFC1320_a (testName, hash, input, result);
120                         RFC1320_b (testName, hash, input, result);
121                         RFC1320_c (testName, hash, input, result);
122                         RFC1320_d (testName, hash, input, result);
123                         RFC1320_e (testName, hash, input, result);
124                 }
125
126                 // MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
127                 [Test]
128                 public void RFC1320_Test5 () 
129                 {
130                         string className = hash.ToString ();
131                         byte[] result = { 0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd, 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9 };
132                         byte[] input = Encoding.Default.GetBytes ("abcdefghijklmnopqrstuvwxyz");
133
134                         string testName = className + " 5";
135                         RFC1320_a (testName, hash, input, result);
136                         RFC1320_b (testName, hash, input, result);
137                         RFC1320_c (testName, hash, input, result);
138                         RFC1320_d (testName, hash, input, result);
139                         RFC1320_e (testName, hash, input, result);
140                 }
141
142                 // MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
143                 //      043f8582f241db351ce627e153e7f0e4
144                 [Test]
145                 public void RFC1320_Test6 () 
146                 {
147                         string className = hash.ToString ();
148                         byte[] result = { 0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35, 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4 };
149                         byte[] input = Encoding.Default.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
150
151                         string testName = className + " 6";
152                         RFC1320_a (testName, hash, input, result);
153                         RFC1320_b (testName, hash, input, result);
154                         RFC1320_c (testName, hash, input, result);
155                         RFC1320_d (testName, hash, input, result);
156                         RFC1320_e (testName, hash, input, result);
157                 }
158
159                 // MD4 ("123456789012345678901234567890123456789012345678901234567890123456
160                 //      78901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
161                 [Test]
162                 public void RFC1320_Test7 () 
163                 {
164                         string className = hash.ToString ();
165                         byte[] result = { 0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36 };
166                         byte[] input = Encoding.Default.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
167
168                         string testName = className + " 7";
169                         RFC1320_a (testName, hash, input, result);
170                         RFC1320_b (testName, hash, input, result);
171                         RFC1320_c (testName, hash, input, result);
172                         RFC1320_d (testName, hash, input, result);
173                         RFC1320_e (testName, hash, input, result);
174                 }
175
176                 public void RFC1320_a (string testName, MD4 hash, byte[] input, byte[] result) 
177                 {
178                         byte[] output = hash.ComputeHash (input); 
179                         AssertEquals (testName + ".a.1", result, output);
180                         AssertEquals (testName + ".a.2", result, hash.Hash);
181                         // required or next operation will still return old hash
182                         hash.Initialize ();
183                 }
184
185                 public void RFC1320_b (string testName, MD4 hash, byte[] input, byte[] result) 
186                 {
187                         byte[] output = hash.ComputeHash (input, 0, input.Length); 
188                         AssertEquals (testName + ".b.1", result, output);
189                         AssertEquals (testName + ".b.2", result, hash.Hash);
190                         // required or next operation will still return old hash
191                         hash.Initialize ();
192                 }
193
194                 public void RFC1320_c (string testName, MD4 hash, byte[] input, byte[] result) 
195                 {
196                         MemoryStream ms = new MemoryStream (input);
197                         byte[] output = hash.ComputeHash (ms); 
198                         AssertEquals (testName + ".c.1", result, output);
199                         AssertEquals (testName + ".c.2", result, hash.Hash);
200                         // required or next operation will still return old hash
201                         hash.Initialize ();
202                 }
203
204                 public void RFC1320_d (string testName, MD4 hash, byte[] input, byte[] result) 
205                 {
206                         byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
207                         AssertEquals (testName + ".d.1", input, output);
208                         AssertEquals (testName + ".d.2", result, hash.Hash);
209                         // required or next operation will still return old hash
210                         hash.Initialize ();
211                 }
212
213                 public void RFC1320_e (string testName, MD4 hash, byte[] input, byte[] result) 
214                 {
215                         byte[] copy = new byte [input.Length];
216                         for (int i=0; i < input.Length - 1; i++)
217                                 hash.TransformBlock (input, i, 1, copy, i);
218                         byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
219                         AssertEquals (testName + ".e.1", input [input.Length - 1], output [0]);
220                         AssertEquals (testName + ".e.2", result, hash.Hash);
221                         // required or next operation will still return old hash
222                         hash.Initialize ();
223                 }
224
225                 // none of those values changes for any implementation of MD4
226                 [Test]
227                 public virtual void StaticInfo () 
228                 {
229                         string className = hash.ToString ();
230                         AssertEquals (className + ".HashSize", 128, hash.HashSize);
231                         AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
232                         AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);
233                 }
234                 
235                 [Test]
236                 public virtual void Create () 
237                 {
238                         // create the default implementation
239                         HashAlgorithm h = MD4.Create ();
240                         Assert.IsTrue ((h is MD4Managed), "MD4Managed");
241                         // Note: will fail is default is changed in machine.config
242                 }
243         }
244 }