2 // PasswordDeriveTest.cs - NUnit Test Cases for PasswordDerive
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004,2007 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
32 using System.Security.Cryptography;
34 namespace MonoTests.System.Security.Cryptography {
37 // a. PKCS#5: Password-Based Cryptography Standard
38 // http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/index.html
41 public class PasswordDeriveBytesTest {
43 static byte[] salt = { 0xDE, 0xAD, 0xC0, 0xDE };
44 static string ssalt = "DE-AD-C0-DE";
50 [ExpectedException (typeof (ArgumentNullException))]
52 public void Ctor_PasswordNullSalt ()
55 PasswordDeriveBytes pdb = new PasswordDeriveBytes (pwd, salt);
59 public void Ctor_PasswordSaltNull ()
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");
68 public void Ctor_PasswordSalt ()
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");
78 [ExpectedException (typeof (ArgumentNullException))]
80 [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
82 public void Ctor_PasswordNullSaltCspParameters ()
85 PasswordDeriveBytes pdb = new PasswordDeriveBytes (pwd, salt, new CspParameters ());
90 [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
91 public void Ctor_PasswordSaltNullCspParameters ()
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");
101 public void Ctor_PasswordSaltCspParametersNull ()
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");
111 [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
112 public void Ctor_PasswordSaltCspParameters ()
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");
123 [ExpectedException (typeof (ArgumentNullException))]
125 public void Ctor_PasswordNullSaltHashIteration ()
128 PasswordDeriveBytes pdb = new PasswordDeriveBytes (pwd, salt, "SHA1", 1);
132 public void Ctor_PasswordSaltNullHashIteration ()
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");
141 [ExpectedException (typeof (ArgumentNullException))]
142 public void Ctor_PasswordSaltHashNullIteration ()
144 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, null, 1);
148 [ExpectedException (typeof (ArgumentOutOfRangeException))]
149 public void Ctor_PasswordSaltHashIterationNegative ()
151 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", -1);
155 [ExpectedException (typeof (ArgumentOutOfRangeException))]
156 public void Ctor_PasswordSaltHashIterationZero ()
158 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", 0);
162 public void Ctor_PasswordSaltHashIterationMaxValue ()
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");
171 public void Ctor_PasswordSaltHashIteration ()
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");
181 [ExpectedException (typeof (ArgumentNullException))]
183 [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
185 public void Ctor_PasswordNullSaltHashIterationCspParameters ()
188 PasswordDeriveBytes pdb = new PasswordDeriveBytes (pwd, salt, "SHA1", 1, new CspParameters ());
193 [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
194 public void Ctor_PasswordSaltNullHashIterationCspParameters ()
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");
204 [ExpectedException (typeof (ArgumentNullException))]
205 public void Ctor_PasswordSaltHashNullIterationCspParameters ()
207 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, null, 1, new CspParameters ());
211 [ExpectedException (typeof (ArgumentOutOfRangeException))]
212 public void Ctor_PasswordSaltHashIterationNegativeCspParameters ()
214 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", -1, new CspParameters ());
218 [ExpectedException (typeof (ArgumentOutOfRangeException))]
219 public void Ctor_PasswordSaltHashIterationZeroCspParameters ()
221 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", 0, new CspParameters ());
226 [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
227 public void Ctor_PasswordSaltHashIterationMaxValueCspParameters ()
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");
237 public void Ctor_PasswordSaltHashIterationCspParametersNull ()
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");
247 [Category ("NotWorking")] // CspParameters aren't supported by Mono (requires CryptoAPI)
248 public void Ctor_PasswordSaltHashIterationCspParameters ()
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");
260 [ExpectedException (typeof (ArgumentNullException))]
261 public void Property_HashName_Null ()
263 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt, "SHA1", 1);
264 Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
270 // Fixed in 2.0 beta 1
271 [ExpectedException (typeof (NullReferenceException))]
273 public void Property_Salt ()
275 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt);
276 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
278 Assert.IsNull (pdb.Salt, "Salt");
282 public void Property_Salt_Modify ()
284 PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt);
285 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
287 // modification rejected (the property returned a copy of the salt)
288 Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
291 // 1.0/1.1 compatibility
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...
298 [ExpectedException (typeof (ArgumentNullException))]
299 public void GetBytes_PasswordNull ()
302 PasswordDeriveBytes pdb = new PasswordDeriveBytes (pwd, salt);
309 static int ToInt32LE(byte [] bytes, int offset)
311 return (bytes[offset + 3] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 1] << 8) | bytes[offset];
314 // generate the key up to HashSize and reset between operations
315 public void ShortRun(string msg, PasswordDeriveBytes pd, byte[] finalKey)
317 for (int i=0; i < finalKey.Length; i++) {
320 byte[] key = pd.GetBytes (i+1);
322 if (finalKey [j] != key[j]) {
327 Assert.IsTrue (compare, msg + " #" + j);
332 // generate a key at least 1000 bytes and don't reset between operations
333 public void LongRun(string msg, PasswordDeriveBytes pd, byte[] finalKey)
335 int bloc = finalKey.Length;
336 int iter = (int) ((1000 + bloc - 1) / bloc);
338 for (int i=0; i < iter; i++) {
339 pass = pd.GetBytes (bloc);
341 Assert.AreEqual (pass, finalKey, msg);
344 public void Run (string password, byte[] salt, string hashname, int iterations, int getbytes, int lastFourBytes)
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);
357 [ExpectedException (typeof (IndexOutOfRangeException))]
358 public void TooShort ()
360 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "SHA1", 1);
361 byte[] key = pd.GetBytes (0);
364 public void TooLong (string hashName, int size, int lastFourBytes)
366 PasswordDeriveBytes pd = new PasswordDeriveBytes ("toolong", null, hashName, 1);
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");
372 // but we can't get another byte from it!
374 key = pd.GetBytes (1);
375 Assert.Fail ("Expected CryptographicException but got none");
377 catch (CryptographicException) {
378 // LAMESPEC: no limit is documented
380 catch (Exception e) {
381 Assert.Fail ("Expected CryptographicException but got " + e.ToString ());
386 public void TooLong ()
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);
397 public void OneIteration ()
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);
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);
428 public void NoSalt ()
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);
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);
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);
481 public void SHA256 ()
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);
500 public void SHA384 ()
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);
519 public void SHA512 ()
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);
537 // get one block after the other
539 public void OneByOne ()
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)";
545 int bloc = key.Length;
546 int iter = (int) ((1000 + bloc - 1) / bloc);
548 for (int i=0; i < iter; i++) {
549 pass = pd.GetBytes (bloc);
551 Assert.AreEqual (pass, key, msg);
555 public void SHA1SaltShortRun ()
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);
564 public void SHA1SaltLongRun ()
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);
573 public void SHA1NoSaltShortRun ()
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);
582 public void SHA1NoSaltLongRun ()
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);
591 public void MD5SaltShortRun ()
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);
600 public void MD5SaltLongRun ()
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);
609 public void MD5NoSaltShortRun ()
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);
618 public void MD5NoSaltLongRun ()
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);
627 public void Properties ()
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");
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
646 pd.HashName = "SHA256";
647 Assert.Fail ("PKCS#5 can't set HashName after GetBytes - expected CryptographicException but got none");
649 catch (CryptographicException) {
650 // do nothing, this is what we expect
652 catch (Exception e) {
653 Assert.Fail ("PKCS#5 can't set HashName after GetBytes - expected CryptographicException but got " + e.ToString ());
656 pd.Salt = expectedKey;
657 Assert.Fail ("PKCS#5 can't set Salt after GetBytes - expected CryptographicException but got none");
659 catch (CryptographicException) {
660 // do nothing, this is what we expect
662 catch (Exception e) {
663 Assert.Fail ("PKCS#5 can't set Salt after GetBytes - expected CryptographicException but got " + e.ToString ());
666 pd.IterationCount = 10;
667 Assert.Fail ("PKCS#5 can't set IterationCount after GetBytes - expected CryptographicException but got none");
669 catch (CryptographicException) {
670 // do nothing, this is what we expect
672 catch (Exception e) {
673 Assert.Fail ("PKCS#5 can't set IterationCount after GetBytes - expected CryptographicException but got " + e.ToString ());
678 // finally a useful reset :)
679 pd.HashName = "SHA256";
680 pd.Salt = expectedKey;
681 pd.IterationCount = 10;
683 // same thing after Reset
685 pd.HashName = "SHA256";
686 Assert.Fail ("PKCS#5 can't set HashName after Reset - expected CryptographicException but got none");
688 catch (CryptographicException) {
689 // do nothing, this is what we expect
691 catch (Exception e) {
692 Assert.Fail ("PKCS#5 can't set HashName after Reset - expected CryptographicException but got " + e.ToString ());
695 pd.Salt = expectedKey;
696 Assert.Fail ("PKCS#5 can't set Salt after Reset - expected CryptographicException but got none");
698 catch (CryptographicException) {
699 // do nothing, this is what we expect
701 catch (Exception e) {
702 Assert.Fail ("PKCS#5 can't set Salt after Reset - expected CryptographicException but got " + e.ToString ());
705 pd.IterationCount = 10;
706 Assert.Fail ("PKCS#5 can't set IterationCount after Reset - expected CryptographicException but got none");
708 catch (CryptographicException) {
709 // do nothing, this is what we expect
711 catch (Exception e) {
712 Assert.Fail ("PKCS#5 can't set IterationCount after Reset - expected CryptographicException but got " + e.ToString ());
717 // FIXME: should we treat this as a bug or as a feature ?
720 [ExpectedException (typeof (NullReferenceException))]
722 public void StrangeBehaviour ()
724 // create object with a salt...
725 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", salt, "MD5", 1000);
726 // ...then change the salt to null
731 [ExpectedException (typeof (CryptographicException))]
732 public void CryptDeriveKey_TooLongKey ()
734 PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "MD5", 1000);
735 pd.CryptDeriveKey ("AlgName", "MD5", 256, new byte [8]);
740 [Category ("NotWorking")] // bug #79499
741 public void LongMultipleGetBytes ()
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");
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");