PKCS1MaskGenerationMethodTest.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // PKCS1MaskGenerationMethodTest.cs - NUnit Test Cases for PKCS1MaskGenerationMethod
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2004 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. using NUnit.Framework;
  30. using System;
  31. using System.Security.Cryptography;
  32. namespace MonoTests.System.Security.Cryptography {
  33. // References:
  34. // a. PKCS#1: RSA Cryptography Standard
  35. // http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/index.html
  36. [TestFixture]
  37. public class PKCS1MaskGenerationMethodTest {
  38. protected PKCS1MaskGenerationMethod pkcs1;
  39. [SetUp]
  40. public void SetUp ()
  41. {
  42. pkcs1 = new PKCS1MaskGenerationMethod ();
  43. }
  44. public void AssertEquals (string msg, byte[] array1, byte[] array2)
  45. {
  46. AllTests.AssertEquals (msg, array1, array2);
  47. }
  48. [Test]
  49. public void Properties ()
  50. {
  51. // default value
  52. Assert.AreEqual ("SHA1", pkcs1.HashName, "PKCS1MaskGenerationMethod HashName(default)");
  53. // return to default
  54. pkcs1.HashName = null;
  55. Assert.AreEqual ("SHA1", pkcs1.HashName, "PKCS1MaskGenerationMethod HashName(null)");
  56. // bad hash accepted
  57. pkcs1.HashName = "SHA2";
  58. Assert.AreEqual ("SHA2", pkcs1.HashName, "PKCS1MaskGenerationMethod HashName(bad)");
  59. // tostring
  60. Assert.AreEqual ("System.Security.Cryptography.PKCS1MaskGenerationMethod", pkcs1.ToString ());
  61. }
  62. [Test]
  63. public void EmptyMask ()
  64. {
  65. // pretty much useless but supported
  66. byte[] random = { 0x01 };
  67. byte[] mask = pkcs1.GenerateMask (random, 0);
  68. Assert.AreEqual (0, mask.Length, "PKCS1MaskGenerationMethod Empty Mask");
  69. }
  70. [Test]
  71. [ExpectedException (typeof (NullReferenceException))]
  72. public void NullSeed ()
  73. {
  74. byte[] mask = pkcs1.GenerateMask (null, 10);
  75. }
  76. [Test]
  77. [ExpectedException (typeof (OverflowException))]
  78. public void NegativeReturnRequest ()
  79. {
  80. byte[] random = { 0x01 };
  81. byte[] mask = pkcs1.GenerateMask (random, -1);
  82. }
  83. // This test will FAIL with MS framework 1.0 and 1.1 as their MGF1 implementation is buggy
  84. // see ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1-vec.zip for RSA tests vector
  85. [Test]
  86. #if ! NET_2_0
  87. [Category ("NotDotNet")] // Known to fail under MS runtime - both 1.0 and 1.1
  88. #endif
  89. public void PKCS1v21TestVector ()
  90. {
  91. pkcs1.HashName = "SHA1";
  92. // seed = random string of octets (well not random in the tests ;-)
  93. byte[] seed = { 0xaa, 0xfd, 0x12, 0xf6, 0x59, 0xca, 0xe6, 0x34, 0x89, 0xb4, 0x79, 0xe5, 0x07, 0x6d, 0xde, 0xc2, 0xf0, 0x6c, 0xb5, 0x8f };
  94. int LengthDB = 107;
  95. // dbMask = MGF(seed, length(DB))
  96. byte[] dbMask = pkcs1.GenerateMask (seed, LengthDB);
  97. byte[] expectedDBMask = { 0x06, 0xe1, 0xde, 0xb2, 0x36, 0x9a, 0xa5, 0xa5, 0xc7, 0x07, 0xd8, 0x2c, 0x8e, 0x4e, 0x93, 0x24,
  98. 0x8a, 0xc7, 0x83, 0xde, 0xe0, 0xb2, 0xc0, 0x46, 0x26, 0xf5, 0xaf, 0xf9, 0x3e, 0xdc, 0xfb, 0x25,
  99. 0xc9, 0xc2, 0xb3, 0xff, 0x8a, 0xe1, 0x0e, 0x83, 0x9a, 0x2d, 0xdb, 0x4c, 0xdc, 0xfe, 0x4f, 0xf4,
  100. 0x77, 0x28, 0xb4, 0xa1, 0xb7, 0xc1, 0x36, 0x2b, 0xaa, 0xd2, 0x9a, 0xb4, 0x8d, 0x28, 0x69, 0xd5,
  101. 0x02, 0x41, 0x21, 0x43, 0x58, 0x11, 0x59, 0x1b, 0xe3, 0x92, 0xf9, 0x82, 0xfb, 0x3e, 0x87, 0xd0,
  102. 0x95, 0xae, 0xb4, 0x04, 0x48, 0xdb, 0x97, 0x2f, 0x3a, 0xc1, 0x4e, 0xaf, 0xf4, 0x9c, 0x8c, 0x3b,
  103. 0x7c, 0xfc, 0x95, 0x1a, 0x51, 0xec, 0xd1, 0xdd, 0xe6, 0x12, 0x64 };
  104. AssertEquals ("PKCS1v21TestVector 1", expectedDBMask, dbMask);
  105. // maskedDB = DB xor dbMask
  106. byte[] DB = { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
  107. 0xaf, 0xd8, 0x07, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  111. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xd4, 0x36, 0xe9, 0x95, 0x69,
  112. 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
  113. byte[] maskedDB = new byte [dbMask.Length];
  114. for (int i = 0; i < dbMask.Length; i++)
  115. maskedDB [i] = Convert.ToByte (DB [i] ^ dbMask [i]);
  116. // seedMask = MGF(maskedDB, length(seed))
  117. byte[] seedMask = pkcs1.GenerateMask (maskedDB, seed.Length);
  118. byte[] expectedSeedMask = { 0x41, 0x87, 0x0b, 0x5a, 0xb0, 0x29, 0xe6, 0x57, 0xd9, 0x57, 0x50, 0xb5, 0x4c, 0x28, 0x3c, 0x08, 0x72, 0x5d, 0xbe, 0xa9 };
  119. AssertEquals ("PKCS1v21TestVector 2", expectedSeedMask, seedMask);
  120. }
  121. }
  122. }