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