2 // MD4Test.cs - NUnit Test Cases for MD4 (RFC1320)
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 using NUnit
.Framework
;
13 using Mono
.Security
.Cryptography
;
16 namespace MonoTests
.Security
.Cryptography
{
19 // a. The MD4 Message-Digest Algorithm
20 // http://www.ietf.org/rfc/RFC1320.txt
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
{
28 // because most crypto stuff works with byte[] buffers
29 static public void AssertEquals (string msg
, byte[] array1
, byte[] array2
)
31 if ((array1
== null) && (array2
== null))
34 Assert
.Fail (msg
+ " -> First array is NULL");
36 Assert
.Fail (msg
+ " -> Second array is NULL");
38 bool a
= (array1
.Length
== array2
.Length
);
40 for (int i
= 0; i
< array1
.Length
; i
++) {
41 if (array1
[i
] != array2
[i
]) {
47 if (array1
.Length
> 0) {
48 msg
+= " -> Expected " + BitConverter
.ToString (array1
, 0);
49 msg
+= " is different than " + BitConverter
.ToString (array2
, 0);
51 Assert
.IsTrue (a
, msg
);
54 // MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
56 public void RFC1320_Test1 ()
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];
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);
70 // MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
72 public void RFC1320_Test2 ()
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");
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
);
86 // MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
88 public void RFC1320_Test3 ()
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");
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
);
102 // MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
104 public void RFC1320_Test4 ()
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");
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
);
118 // MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
120 public void RFC1320_Test5 ()
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");
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
);
134 // MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
135 // 043f8582f241db351ce627e153e7f0e4
137 public void RFC1320_Test6 ()
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");
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
);
151 // MD4 ("123456789012345678901234567890123456789012345678901234567890123456
152 // 78901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
154 public void RFC1320_Test7 ()
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");
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
);
168 public void RFC1320_a (string testName
, MD4 hash
, byte[] input
, byte[] result
)
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
177 public void RFC1320_b (string testName
, MD4 hash
, byte[] input
, byte[] result
)
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
186 public void RFC1320_c (string testName
, MD4 hash
, byte[] input
, byte[] result
)
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
196 public void RFC1320_d (string testName
, MD4 hash
, byte[] input
, byte[] result
)
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
205 public void RFC1320_e (string testName
, MD4 hash
, byte[] input
, byte[] result
)
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 Assert
.AreEqual (input
[input
.Length
- 1], output
[0], testName
+ ".e.1");
212 AssertEquals (testName
+ ".e.2", result
, hash
.Hash
);
213 // required or next operation will still return old hash
217 // none of those values changes for any implementation of MD4
218 public virtual void StaticInfo ()
220 string className
= hash
.ToString ();
221 Assert
.AreEqual (128, hash
.HashSize
, className
+ ".HashSize");
222 Assert
.AreEqual (1, hash
.InputBlockSize
, className
+ ".InputBlockSize");
223 Assert
.AreEqual (1, hash
.OutputBlockSize
, className
+ ".OutputBlockSize");