Merge pull request #733 from amoiseev-softheme/bugfix/monofix
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / PasswordDeriveBytesTest.cs
1 //
2 // PasswordDeriveTest.cs - NUnit Test Cases for PasswordDerive
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004,2007 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Security.Cryptography;
33
34 namespace MonoTests.System.Security.Cryptography {
35
36 // References:
37 // a.   PKCS#5: Password-Based Cryptography Standard 
38 //      http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/index.html
39
40 [TestFixture]
41 public class PasswordDeriveBytesTest {
42
43         static byte[] salt = { 0xDE, 0xAD, 0xC0, 0xDE };
44         static string ssalt = "DE-AD-C0-DE";
45
46         // Constructors
47
48         [Test]
49 #if NET_2_0
50         [ExpectedException (typeof (ArgumentNullException))]
51 #endif
52         public void Ctor_PasswordNullSalt ()
53         {
54                 string pwd = null;
55                 PasswordDeriveBytes pdb = new PasswordDeriveBytes (pwd, salt);
56         }
57
58         [Test]
59         public void Ctor_PasswordSaltNull ()
60         {
61                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", null);
62                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
63                 Assert.AreEqual (100, pdb.IterationCount, "IterationCount");
64                 Assert.IsNull (pdb.Salt, "Salt");
65         }
66
67         [Test]
68         public void Ctor_PasswordSalt ()
69         {
70                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt);
71                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
72                 Assert.AreEqual (100, pdb.IterationCount, "IterationCount");
73                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
74         }
75
76         [Test]
77 #if NET_2_0
78         [ExpectedException (typeof (ArgumentNullException))]
79 #else
80         [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
81 #endif
82         public void Ctor_PasswordNullSaltCspParameters ()
83         {
84                 string pwd = null;
85                 PasswordDeriveBytes pdb = new PasswordDeriveBytes (pwd, salt, new CspParameters ());
86         }
87
88 #if !NET_2_1
89         [Test]
90         [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
91         public void Ctor_PasswordSaltNullCspParameters ()
92         {
93                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", null, new CspParameters ());
94                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
95                 Assert.AreEqual (100, pdb.IterationCount, "IterationCount");
96                 Assert.IsNull (pdb.Salt, "Salt");
97         }
98 #endif
99
100         [Test]
101         public void Ctor_PasswordSaltCspParametersNull ()
102         {
103                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, null);
104                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
105                 Assert.AreEqual (100, pdb.IterationCount, "IterationCount");
106                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
107         }
108
109 #if !NET_2_1
110         [Test]
111         [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
112         public void Ctor_PasswordSaltCspParameters ()
113         {
114                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, new CspParameters ());
115                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
116                 Assert.AreEqual (100, pdb.IterationCount, "IterationCount");
117                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
118         }
119 #endif
120
121         [Test]
122 #if NET_2_0
123         [ExpectedException (typeof (ArgumentNullException))]
124 #endif
125         public void Ctor_PasswordNullSaltHashIteration ()
126         {
127                 string pwd = null;
128                 PasswordDeriveBytes pdb = new PasswordDeriveBytes (pwd, salt, "SHA1", 1);
129         }
130
131         [Test]
132         public void Ctor_PasswordSaltNullHashIteration ()
133         {
134                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", null, "SHA1", 1);
135                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
136                 Assert.AreEqual (1, pdb.IterationCount, "IterationCount");
137                 Assert.IsNull (pdb.Salt, "Salt");
138         }
139
140         [Test]
141         [ExpectedException (typeof (ArgumentNullException))]
142         public void Ctor_PasswordSaltHashNullIteration ()
143         {
144                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, null, 1);
145         }
146
147         [Test]
148         [ExpectedException (typeof (ArgumentOutOfRangeException))]
149         public void Ctor_PasswordSaltHashIterationNegative ()
150         {
151                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", -1);
152         }
153
154         [Test]
155         [ExpectedException (typeof (ArgumentOutOfRangeException))]
156         public void Ctor_PasswordSaltHashIterationZero ()
157         {
158                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", 0);
159         }
160
161         [Test]
162         public void Ctor_PasswordSaltHashIterationMaxValue ()
163         {
164                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", Int32.MaxValue);
165                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
166                 Assert.AreEqual (Int32.MaxValue, pdb.IterationCount, "IterationCount");
167                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
168         }
169
170         [Test]
171         public void Ctor_PasswordSaltHashIteration ()
172         {
173                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", 1);
174                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
175                 Assert.AreEqual (1, pdb.IterationCount, "IterationCount");
176                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
177         }
178
179         [Test]
180 #if NET_2_0
181         [ExpectedException (typeof (ArgumentNullException))]
182 #else
183         [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
184 #endif
185         public void Ctor_PasswordNullSaltHashIterationCspParameters ()
186         {
187                 string pwd = null;
188                 PasswordDeriveBytes pdb = new PasswordDeriveBytes (pwd, salt, "SHA1", 1, new CspParameters ());
189         }
190
191 #if !NET_2_1
192         [Test]
193         [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
194         public void Ctor_PasswordSaltNullHashIterationCspParameters ()
195         {
196                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", null, "SHA1", 1, new CspParameters ());
197                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
198                 Assert.AreEqual (1, pdb.IterationCount, "IterationCount");
199                 Assert.IsNull (pdb.Salt, "Salt");
200         }
201 #endif
202
203         [Test]
204         [ExpectedException (typeof (ArgumentNullException))]
205         public void Ctor_PasswordSaltHashNullIterationCspParameters ()
206         {
207                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, null, 1, new CspParameters ());
208         }
209
210         [Test]
211         [ExpectedException (typeof (ArgumentOutOfRangeException))]
212         public void Ctor_PasswordSaltHashIterationNegativeCspParameters ()
213         {
214                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", -1, new CspParameters ());
215         }
216
217         [Test]
218         [ExpectedException (typeof (ArgumentOutOfRangeException))]
219         public void Ctor_PasswordSaltHashIterationZeroCspParameters ()
220         {
221                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", 0, new CspParameters ());
222         }
223                 
224 #if !NET_2_1
225         [Test]
226         [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
227         public void Ctor_PasswordSaltHashIterationMaxValueCspParameters ()
228         {
229                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", Int32.MaxValue, new CspParameters ());
230                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
231                 Assert.AreEqual (Int32.MaxValue, pdb.IterationCount, "IterationCount");
232                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
233         }
234 #endif
235
236         [Test]
237         public void Ctor_PasswordSaltHashIterationCspParametersNull ()
238         {
239                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", 1, null);
240                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
241                 Assert.AreEqual (1, pdb.IterationCount, "IterationCount");
242                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
243         }
244
245 #if !NET_2_1
246         [Test]
247         [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
248         public void Ctor_PasswordSaltHashIterationCspParameters ()
249         {
250                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", 1, new CspParameters ());
251                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
252                 Assert.AreEqual (1, pdb.IterationCount, "IterationCount");
253                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
254         }
255 #endif
256                 
257         // Properties
258
259         [Test]
260         [ExpectedException (typeof (ArgumentNullException))]
261         public void Property_HashName_Null ()
262         {
263                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", 1);
264                 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
265                 pdb.HashName = null;
266         }
267
268         [Test]
269 #if !NET_2_0
270         // Fixed in 2.0 beta 1
271         [ExpectedException (typeof (NullReferenceException))]
272 #endif
273         public void Property_Salt ()
274         {
275                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt);
276                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
277                 pdb.Salt = null;
278                 Assert.IsNull (pdb.Salt, "Salt");
279         }
280
281         [Test]
282         public void Property_Salt_Modify ()
283         {
284                 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt);
285                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
286                 pdb.Salt [0] = 0xFF;
287                 // modification rejected (the property returned a copy of the salt)
288                 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
289         }
290
291         // 1.0/1.1 compatibility
292
293 #if !NET_2_0
294         // 1.0/1.1 accepted a null password as valid - but throw the 
295         // ArgumentNullException when GetBytes is called
296         // byte stream from the null input. Check that we can do the same...
297         [Test]
298         [ExpectedException (typeof (ArgumentNullException))]
299         public void GetBytes_PasswordNull ()
300         {
301                 string pwd = null;
302                 PasswordDeriveBytes pdb = new PasswordDeriveBytes (pwd, salt);
303                 pdb.GetBytes (24);
304         }
305 #endif
306
307         // Old tests
308
309         static int ToInt32LE(byte [] bytes, int offset)
310         {
311                 return (bytes[offset + 3] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 1] << 8) | bytes[offset];
312         }
313
314         // generate the key up to HashSize and reset between operations
315         public void ShortRun(string msg, PasswordDeriveBytes pd, byte[] finalKey)
316         {
317                 for (int i=0; i < finalKey.Length; i++) {
318                         int j = 0;
319                         bool compare = true;
320                         byte[] key = pd.GetBytes (i+1);
321                         for (; j < i; j++) {
322                                 if (finalKey [j] != key[j]) {
323                                         compare = false;
324                                         break;
325                                 }
326                         }
327                         Assert.IsTrue (compare, msg + " #" + j);
328                         pd.Reset ();
329                 }
330         }
331
332         // generate a key at least 1000 bytes and don't reset between operations
333         public void LongRun(string msg, PasswordDeriveBytes pd, byte[] finalKey)
334         {
335                 int bloc = finalKey.Length;
336                 int iter = (int) ((1000 + bloc - 1) / bloc);
337                 byte[] pass = null;
338                 for (int i=0; i < iter; i++) {
339                         pass = pd.GetBytes (bloc);
340                 }
341                 Assert.AreEqual (pass, finalKey, msg);
342         }
343
344         public void Run (string password, byte[] salt, string hashname, int iterations, int getbytes, int lastFourBytes) 
345         {
346                 PasswordDeriveBytes pd = new PasswordDeriveBytes (password, salt, hashname, iterations);
347                 byte[] key = pd.GetBytes (getbytes);
348                 string msg = "[pwd=" + password;
349                 msg += ", salt=" + ((salt == null) ? "null" : salt.Length.ToString ());
350                 msg += ", hash=" + hashname;
351                 msg += ", iter=" + iterations;
352                 msg += ", get=" + getbytes + "]";
353                 Assert.AreEqual (lastFourBytes, ToInt32LE (key, key.Length - 4), msg);
354         }
355
356         [Test]
357         [ExpectedException (typeof (IndexOutOfRangeException))]
358         public void TooShort () 
359         {
360                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "SHA1", 1);
361                 byte[] key = pd.GetBytes (0);
362         }
363
364         public void TooLong (string hashName, int size, int lastFourBytes) 
365         {
366                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("toolong", null, hashName, 1);
367                 
368                 // this should work (we check the last four devired bytes to be sure)
369                 byte[] key = pd.GetBytes (size);
370                 Assert.AreEqual (lastFourBytes, ToInt32LE (key, size - 4), "Last 4 bytes");
371
372                 // but we can't get another byte from it!
373                 try {
374                         key = pd.GetBytes (1);
375                         Assert.Fail ("Expected CryptographicException but got none");
376                 }
377                 catch (CryptographicException) {
378                         // LAMESPEC: no limit is documented
379                 }
380                 catch (Exception e) {
381                         Assert.Fail ("Expected CryptographicException but got " + e.ToString ());
382                 }
383         }
384
385         [Test]
386         public void TooLong () 
387         {
388                 // 1000 times hash length is the maximum
389                 TooLong ("MD5",    16000, 1135777886);
390                 TooLong ("SHA1",   20000, -1167918035);
391                 TooLong ("SHA256", 32000, -358766048);
392                 TooLong ("SHA384", 48000, 1426370534);
393                 TooLong ("SHA512", 64000, -1763233543);
394         }
395
396         [Test]
397         public void OneIteration () 
398         {
399                 // (1) size of hash, (2) size of 2 hash
400                 Run ("password", salt, "MD5",    1, 16, 986357363);
401                 Run ("monomono", null, "MD5",    1, 32, -1092059875);
402                 Run ("password", salt, "SHA1",   1, 20, -1251929751);
403                 Run ("monomono", null, "SHA1",   1, 40, -1148594972);
404                 Run ("password", salt, "SHA256", 1, 32, -1106908309);
405                 Run ("monomono", null, "SHA256", 1, 64, 1243724695);
406                 Run ("password", salt, "SHA384", 1, 48, 1338639872);
407                 Run ("monomono", null, "SHA384", 1, 96, -1974067932);
408                 Run ("password", salt, "SHA512", 1, 64, 998927776);
409                 Run ("monomono", null, "SHA512", 1, 128, -1082987985);
410         }
411
412         [Test]
413         public void Salt () 
414         {
415                 Run ("password", salt, "MD5",  10, 10, -1174247292);
416                 Run ("monomono", salt, "SHA1", 20, 20, 622814236);
417                 Run ("password", salt, "MD5",  30, 30, 1491759020);
418                 Run ("monomono", salt, "SHA1", 40, 40, 1186751819);
419                 Run ("password", salt, "MD5",  50, 50, -1416348895);
420                 Run ("monomono", salt, "SHA1", 60, 60, -1167799882);
421                 Run ("password", salt, "MD5",  70, 70, -695745351);
422                 Run ("monomono", salt, "SHA1", 80, 80, 598766793);
423                 Run ("password", salt, "MD5",  90, 90, -906351079);
424                 Run ("monomono", salt, "SHA1", 100, 100, 1247157997);
425         }
426
427         [Test]
428         public void NoSalt () 
429         {
430                 Run ("password", null, "MD5",  10, 10, -385488886);
431                 Run ("password", null, "SHA1", 20, 20, -385953596);
432                 Run ("password", null, "MD5",  30, 30, -669295228);
433                 Run ("password", null, "SHA1", 40, 40, -1921654064);
434                 Run ("password", null, "MD5",  50, 50, -1664099354);
435                 Run ("monomono", null, "SHA1", 60, 60, -1988511363);
436                 Run ("monomono", null, "MD5",  70, 70, -1326415479);
437                 Run ("monomono", null, "SHA1", 80, 80, 158880373);
438                 Run ("monomono", null, "MD5",  90, 90,  532527918);
439                 Run ("monomono", null, "SHA1", 100, 100, 769250758);
440         }
441
442         [Test]
443         public void MD5 () 
444         {
445                 const string hashName = "MD5";
446                 // getbytes less than hash size
447                 Run ("password", null, hashName, 10, 10, -385488886);
448                 // getbytes equal to hash size
449                 Run ("password", salt, hashName, 20, 16, -470982134);
450                 // getbytes more than hash size
451                 Run ("password", null, hashName, 30, 30, -669295228);
452                 Run ("password", salt, hashName, 40, 40, 892279589);
453                 Run ("password", null, hashName, 50, 50, -1664099354);
454                 Run ("monomono", salt, hashName, 60, 60, -2050574033);
455                 Run ("monomono", null, hashName, 70, 70, -1326415479);
456                 Run ("monomono", salt, hashName, 80, 80, 2047895994);
457                 Run ("monomono", null, hashName, 90, 90, 532527918);
458                 Run ("monomono", salt, hashName, 100, 100, 1522243696);
459         }
460
461         [Test]
462         public void SHA1 () 
463         {
464                 const string hashName = "SHA1";
465                 // getbytes less than hash size
466                 Run ("password", null, hashName, 10, 10, -852142057);
467                 // getbytes equal to hash size
468                 Run ("password", salt, hashName, 20, 20, -1096621819);
469                 // getbytes more than hash size
470                 Run ("password", null, hashName, 30, 30, 1748347042);
471                 Run ("password", salt, hashName, 40, 40, 900690664);
472                 Run ("password", null, hashName, 50, 50, 2125027038);
473                 Run ("monomono", salt, hashName, 60, 60, -1167799882);
474                 Run ("monomono", null, hashName, 70, 70, -1967623713);
475                 Run ("monomono", salt, hashName, 80, 80, 598766793);
476                 Run ("monomono", null, hashName, 90, 90, -1754629926);
477                 Run ("monomono", salt, hashName, 100, 100, 1247157997);
478         }
479
480         [Test]
481         public void SHA256 () 
482         {
483                 const string hashName = "SHA256";
484                 // getbytes less than hash size
485                 Run ("password", null, hashName, 10, 10, -1636557322);
486                 Run ("password", salt, hashName, 20, 20, -1403130075);
487                 // getbytes equal to hash size
488                 Run ("password", null, hashName, 30, 32, -1013167039);
489                 // getbytes more than hash size
490                 Run ("password", salt, hashName, 40, 40, 379553148);
491                 Run ("password", null, hashName, 50, 50, 1031928292);
492                 Run ("monomono", salt, hashName, 60, 60, 1933836953);
493                 Run ("monomono", null, hashName, 70, 70, -956782587);
494                 Run ("monomono", salt, hashName, 80, 80, 1239391711);
495                 Run ("monomono", null, hashName, 90, 90, -872090432);
496                 Run ("monomono", salt, hashName, 100, 100, -591569127);
497         }
498
499         [Test]
500         public void SHA384 () 
501         {
502                 const string hashName = "SHA384";
503                 // getbytes less than hash size
504                 Run ("password", null, hashName, 10, 10, 323393534);
505                 Run ("password", salt, hashName, 20, 20, -2034683704);
506                 Run ("password", null, hashName, 30, 32, 167978389);
507                 Run ("password", salt, hashName, 40, 40, 2123410525);
508                 // getbytes equal to hash size
509                 Run ("password", null, hashName, 50, 48, -47538843);
510                 // getbytes more than hash size
511                 Run ("monomono", salt, hashName, 60, 60, -118610774);
512                 Run ("monomono", null, hashName, 70, 70, 772360425);
513                 Run ("monomono", salt, hashName, 80, 80, -1018881215);
514                 Run ("monomono", null, hashName, 90, 90, -1585583772);
515                 Run ("monomono", salt, hashName, 100, 100, -821501990);
516         }
517
518         [Test]
519         public void SHA512 () 
520         {
521                 const string hashName = "SHA512";
522                 // getbytes less than hash size
523                 Run ("password", null, hashName, 10, 10, 708870265);
524                 Run ("password", salt, hashName, 20, 20, 23889227);
525                 Run ("password", null, hashName, 30, 32, 1718904507);
526                 Run ("password", salt, hashName, 40, 40, 979228711);
527                 Run ("password", null, hashName, 50, 48, 1554003653);
528                 // getbytes equal to hash size
529                 Run ("monomono", salt, hashName, 60, 64, 1251099126);
530                 // getbytes more than hash size
531                 Run ("monomono", null, hashName, 70, 70, 1021441810);
532                 Run ("monomono", salt, hashName, 80, 80, 640059310);
533                 Run ("monomono", null, hashName, 90, 90, 1178147201);
534                 Run ("monomono", salt, hashName, 100, 100, 206423887);
535         }
536
537         // get one block after the other
538         [Test]
539         public void OneByOne () 
540         {
541                 byte[] key = { 0x91, 0xDA, 0xF9, 0x9D, 0x7C, 0xA9, 0xB4, 0x42, 0xB8, 0xD9, 0x45, 0xAB, 0x69, 0xEE, 0x12, 0xBC, 0x48, 0xDD, 0x38, 0x74 };
542                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", salt, "SHA1", 1);
543                 string msg = "PKCS#5-Long password salt SHA1 (1)";
544
545                 int bloc = key.Length;
546                 int iter = (int) ((1000 + bloc - 1) / bloc);
547                 byte[] pass = null;
548                 for (int i=0; i < iter; i++) {
549                         pass = pd.GetBytes (bloc);
550                 }
551                 Assert.AreEqual (pass, key, msg);
552         }
553
554         [Test]
555         public void SHA1SaltShortRun ()
556         {
557                 byte[] key = { 0x0B, 0x61, 0x93, 0x96, 0x3A, 0xFF, 0x0D, 0xFC, 0xF6, 0x3D, 0xA3, 0xDB, 0x34, 0xC2, 0x99, 0x71, 0x69, 0x11, 0x61, 0xB5 };
558                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", salt, "SHA1", 1);
559                 string msg = "PKCS#5 password salt SHA1 (1)";
560                 ShortRun (msg, pd, key);
561         }
562
563         [Test]
564         public void SHA1SaltLongRun () 
565         {
566                 byte[] key = { 0x91, 0xDA, 0xF9, 0x9D, 0x7C, 0xA9, 0xB4, 0x42, 0xB8, 0xD9, 0x45, 0xAB, 0x69, 0xEE, 0x12, 0xBC, 0x48, 0xDD, 0x38, 0x74 };
567                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", salt, "SHA1", 1);
568                 string msg = "PKCS#5-Long password salt SHA1 (1)";
569                 LongRun (msg, pd, key);
570         }
571
572         [Test]
573         public void SHA1NoSaltShortRun ()
574         {
575                 byte[] key = { 0x74, 0x61, 0x03, 0x6C, 0xA1, 0xFE, 0x85, 0x3E, 0xD9, 0x3F, 0x03, 0x06, 0x58, 0x45, 0xDE, 0x36, 0x52, 0xEF, 0x4B, 0x68 };
576                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("mono", null, "SHA1", 10);
577                 string msg = "PKCS#5 mono null SHA1 (10)";
578                 ShortRun (msg, pd, key);
579         }
580
581         [Test]
582         public void SHA1NoSaltLongRun () 
583         {
584                 byte[] key = { 0x3A, 0xF8, 0x33, 0x88, 0x39, 0x61, 0x29, 0x75, 0x5C, 0x17, 0xD2, 0x9E, 0x8A, 0x78, 0xEB, 0xBD, 0x89, 0x1E, 0x4C, 0x67 };
585                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("mono", null, "SHA1", 10);
586                 string msg = "PKCS#5-Long mono null SHA1 (10)";
587                 LongRun (msg, pd, key);
588         }
589
590         [Test]
591         public void MD5SaltShortRun ()
592         {
593                 byte[] key = { 0xA5, 0x4D, 0x4E, 0xDD, 0x3A, 0x59, 0xAC, 0x98, 0x08, 0xDA, 0xE7, 0xF2, 0x85, 0x2F, 0x7F, 0xF2 };
594                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("mono", salt, "MD5", 100);
595                 string msg = "PKCS#5 mono salt MD5 (100)";
596                 ShortRun (msg, pd, key);
597         }
598
599         [Test]
600         public void MD5SaltLongRun () 
601         {
602                 byte[] key = { 0x92, 0x51, 0x4D, 0x10, 0xE1, 0x5F, 0xA8, 0x44, 0xEF, 0xFC, 0x0F, 0x1F, 0x6F, 0x3E, 0x40, 0x36 };
603                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("mono", salt, "MD5", 100);
604                 string msg = "PKCS#5-Long mono salt MD5 (100)";
605                 LongRun (msg, pd, key);
606         }
607
608         [Test]
609         public void MD5NoSaltShortRun ()
610         {
611                 byte[] key = { 0x39, 0xEB, 0x82, 0x84, 0xCF, 0x1A, 0x3B, 0x3C, 0xA1, 0xF2, 0x68, 0xAF, 0xBF, 0xAC, 0x41, 0xA6 };
612                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "MD5", 1000);
613                 string msg = "PKCS#5 password null MD5 (1000)";
614                 ShortRun (msg, pd, key);
615         }
616
617         [Test]
618         public void MD5NoSaltLongRun () 
619         {
620                 byte[] key = { 0x49, 0x3C, 0x00, 0x69, 0xB4, 0x55, 0x21, 0xA4, 0xC9, 0x69, 0x2E, 0xFF, 0xAA, 0xED, 0x4C, 0x72 };
621                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "MD5", 1000);
622                 string msg = "PKCS#5-Long password null MD5 (1000)";
623                 LongRun (msg, pd, key);
624         }
625
626         [Test]
627         public void Properties () 
628         {
629                 // create object...
630                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "MD5", 1000);
631                 Assert.AreEqual ("MD5", pd.HashName, "HashName-MD5");
632                 Assert.AreEqual (1000, pd.IterationCount, "IterationCount-1000");
633                 // ...then change all its properties...
634                 pd.HashName = "SHA1";
635                 Assert.AreEqual ("SHA1", pd.HashName, "HashName-SHA1");
636                 pd.Salt = salt;
637                 Assert.AreEqual (ssalt, BitConverter.ToString (pd.Salt), "Salt");
638                 pd.IterationCount = 1;
639                 Assert.AreEqual (1, pd.IterationCount, "IterationCount-1");
640                 byte[] expectedKey = { 0x0b, 0x61, 0x93, 0x96 };
641                 // ... before using it
642                 Assert.AreEqual (expectedKey, pd.GetBytes (4), "PKCS#5 test properties");
643                 // it should work but if we try to set any properties after GetBytes
644                 // they should all throw an exception
645                 try {
646                         pd.HashName = "SHA256";
647                         Assert.Fail ("PKCS#5 can't set HashName after GetBytes - expected CryptographicException but got none");
648                 }
649                 catch (CryptographicException) {
650                         // do nothing, this is what we expect
651                 }
652                 catch (Exception e) {
653                         Assert.Fail ("PKCS#5 can't set HashName after GetBytes - expected CryptographicException but got " + e.ToString ());
654                 }
655                 try {
656                         pd.Salt = expectedKey;
657                         Assert.Fail ("PKCS#5 can't set Salt after GetBytes - expected CryptographicException but got none");
658                 }
659                 catch (CryptographicException) {
660                         // do nothing, this is what we expect
661                 }
662                 catch (Exception e) {
663                         Assert.Fail ("PKCS#5 can't set Salt after GetBytes - expected CryptographicException but got " + e.ToString ());
664                 }
665                 try {
666                         pd.IterationCount = 10;
667                         Assert.Fail ("PKCS#5 can't set IterationCount after GetBytes - expected CryptographicException but got none");
668                 }
669                 catch (CryptographicException) {
670                         // do nothing, this is what we expect
671                 }
672                 catch (Exception e) {
673                         Assert.Fail ("PKCS#5 can't set IterationCount after GetBytes - expected CryptographicException but got " + e.ToString ());
674                 }
675
676                 pd.Reset ();
677 #if NET_2_0
678                 // finally a useful reset :)
679                 pd.HashName = "SHA256";
680                 pd.Salt = expectedKey;
681                 pd.IterationCount = 10;
682 #else
683                 // same thing after Reset
684                 try {
685                         pd.HashName = "SHA256";
686                         Assert.Fail ("PKCS#5 can't set HashName after Reset - expected CryptographicException but got none");
687                 }
688                 catch (CryptographicException) {
689                         // do nothing, this is what we expect
690                 }
691                 catch (Exception e) {
692                         Assert.Fail ("PKCS#5 can't set HashName after Reset - expected CryptographicException but got " + e.ToString ());
693                 }
694                 try {
695                         pd.Salt = expectedKey;
696                         Assert.Fail ("PKCS#5 can't set Salt after Reset - expected CryptographicException but got none");
697                 }
698                 catch (CryptographicException) {
699                         // do nothing, this is what we expect
700                 }
701                 catch (Exception e) {
702                         Assert.Fail ("PKCS#5 can't set Salt after Reset - expected CryptographicException but got " + e.ToString ());
703                 }
704                 try {
705                         pd.IterationCount = 10;
706                         Assert.Fail ("PKCS#5 can't set IterationCount after Reset - expected CryptographicException but got none");
707                 }
708                 catch (CryptographicException) {
709                         // do nothing, this is what we expect
710                 }
711                 catch (Exception e) {
712                         Assert.Fail ("PKCS#5 can't set IterationCount after Reset - expected CryptographicException but got " + e.ToString ());
713                 }
714 #endif
715         }
716
717         // FIXME: should we treat this as a bug or as a feature ?
718         [Test]
719 #if ! NET_2_0
720         [ExpectedException (typeof (NullReferenceException))]
721 #endif
722         public void StrangeBehaviour ()
723         {
724                 // create object with a salt...
725                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", salt, "MD5", 1000);
726                 // ...then change the salt to null
727                 pd.Salt = null;
728         }
729
730         [Test]
731         [ExpectedException (typeof (CryptographicException))]
732         public void CryptDeriveKey_TooLongKey () 
733         {
734                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "MD5", 1000);
735                 pd.CryptDeriveKey ("AlgName", "MD5", 256, new byte [8]);
736         }
737                 
738 #if !NET_2_1
739         [Test]
740         [Category ("NotWorking")] // bug #79499
741         public void LongMultipleGetBytes ()
742         {
743                 // based on http://bugzilla.ximian.com/show_bug.cgi?id=79499
744                 PasswordDeriveBytes pd = new PasswordDeriveBytes ("mono", new byte[20]);
745                 string key = BitConverter.ToString (pd.GetBytes (32));
746                 Assert.AreEqual ("88-0A-AE-0A-41-61-02-78-FD-E2-70-9F-25-13-14-28-1F-C7-D9-72-9A-AE-CA-3F-BD-31-B4-F0-BD-8E-5B-98", key, "key");
747                 string iv = BitConverter.ToString (pd.GetBytes (16));
748                 Assert.AreEqual ("FD-E2-70-9F-25-13-14-28-4D-3F-9B-F8-EE-AA-95-ED", iv, "iv");
749                 pd.Reset ();
750                 // bytes from 32-40 are different from calling GetBytes separately
751                 Assert.AreEqual (key + "-F6-55-6C-3E-54-8B-F3-73-4D-3F-9B-F8-EE-AA-95-ED", BitConverter.ToString (pd.GetBytes (48)), "same");
752         }
753 #endif
754 }
755
756 }