SHA256Test.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // SHA256Test.cs - NUnit Test Cases for SHA256
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.IO;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. namespace MonoTests.System.Security.Cryptography {
  15. // References:
  16. // a. FIPS PUB 180-2: Secure Hash Standard
  17. // http://csrc.nist.gov/publications/fips/fips180-2/fip180-2.txt
  18. // SHA256 is a abstract class - so most of the test included here wont be tested
  19. // on the abstract class but should be tested in ALL its descendants.
  20. public class SHA256Test : HashAlgorithmTest {
  21. protected override void SetUp ()
  22. {
  23. hash = SHA256.Create ();
  24. }
  25. protected override void TearDown () {}
  26. // test vectors from NIST FIPS 186-2
  27. private string input1 = "abc";
  28. private string input2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  29. public void FIPS186_Test1 (SHA256 hash)
  30. {
  31. string className = hash.ToString ();
  32. byte[] result = { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
  33. 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  34. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
  35. 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad };
  36. byte[] input = Encoding.Default.GetBytes (input1);
  37. string testName = className + " 1";
  38. FIPS186_a (testName, hash, input, result);
  39. FIPS186_b (testName, hash, input, result);
  40. FIPS186_c (testName, hash, input, result);
  41. FIPS186_d (testName, hash, input, result);
  42. FIPS186_e (testName, hash, input, result);
  43. }
  44. public void FIPS186_Test2 (SHA256 hash)
  45. {
  46. string className = hash.ToString ();
  47. byte[] result = { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
  48. 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
  49. 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
  50. 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 };
  51. byte[] input = Encoding.Default.GetBytes (input2);
  52. string testName = className + " 2";
  53. FIPS186_a (testName, hash, input, result);
  54. FIPS186_b (testName, hash, input, result);
  55. FIPS186_c (testName, hash, input, result);
  56. FIPS186_d (testName, hash, input, result);
  57. FIPS186_e (testName, hash, input, result);
  58. }
  59. public void FIPS186_Test3 (SHA256 hash)
  60. {
  61. string className = hash.ToString ();
  62. byte[] result = { 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92,
  63. 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,
  64. 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
  65. 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0 };
  66. byte[] input = new byte [1000000];
  67. for (int i = 0; i < 1000000; i++)
  68. input[i] = 0x61; // a
  69. string testName = className + " 3";
  70. FIPS186_a (testName, hash, input, result);
  71. FIPS186_b (testName, hash, input, result);
  72. FIPS186_c (testName, hash, input, result);
  73. FIPS186_d (testName, hash, input, result);
  74. FIPS186_e (testName, hash, input, result);
  75. }
  76. public void FIPS186_a (string testName, SHA256 hash, byte[] input, byte[] result)
  77. {
  78. byte[] output = hash.ComputeHash (input);
  79. AssertEquals (testName + ".a.1", result, output);
  80. AssertEquals (testName + ".a.2", result, hash.Hash);
  81. // required or next operation will still return old hash
  82. hash.Initialize ();
  83. }
  84. public void FIPS186_b (string testName, SHA256 hash, byte[] input, byte[] result)
  85. {
  86. byte[] output = hash.ComputeHash (input, 0, input.Length);
  87. AssertEquals (testName + ".b.1", result, output);
  88. AssertEquals (testName + ".b.2", result, hash.Hash);
  89. // required or next operation will still return old hash
  90. hash.Initialize ();
  91. }
  92. public void FIPS186_c (string testName, SHA256 hash, byte[] input, byte[] result)
  93. {
  94. MemoryStream ms = new MemoryStream (input);
  95. byte[] output = hash.ComputeHash (ms);
  96. AssertEquals (testName + ".c.1", result, output);
  97. AssertEquals (testName + ".c.2", result, hash.Hash);
  98. // required or next operation will still return old hash
  99. hash.Initialize ();
  100. }
  101. public void FIPS186_d (string testName, SHA256 hash, byte[] input, byte[] result)
  102. {
  103. byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
  104. // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
  105. // AssertEquals( testName + ".d.1", result, output );
  106. AssertEquals (testName + ".d", result, hash.Hash);
  107. // required or next operation will still return old hash
  108. hash.Initialize ();
  109. }
  110. public void FIPS186_e (string testName, SHA256 hash, byte[] input, byte[] result)
  111. {
  112. byte[] copy = new byte [input.Length];
  113. for (int i=0; i < input.Length - 1; i++)
  114. hash.TransformBlock (input, i, 1, copy, i);
  115. byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
  116. // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
  117. // AssertEquals (testName + ".e.1", result, output);
  118. AssertEquals (testName + ".e", result, hash.Hash);
  119. // required or next operation will still return old hash
  120. hash.Initialize ();
  121. }
  122. public override void TestCreate ()
  123. {
  124. // Note: These tests will only be valid without a "machine.config" file
  125. // or a "machine.config" file that do not modify the default algorithm
  126. // configuration.
  127. const string defaultSHA256 = "System.Security.Cryptography.SHA256Managed";
  128. // try to build the default implementation
  129. SHA256 hash = SHA256.Create ();
  130. AssertEquals ("SHA256.Create()", hash.ToString (), defaultSHA256);
  131. // try to build, in every way, a SHA256 implementation
  132. hash = SHA256.Create ("SHA256");
  133. AssertEquals ("SHA256.Create('SHA256')", hash.ToString (), defaultSHA256);
  134. hash = SHA256.Create ("SHA-256");
  135. AssertEquals ("SHA256.Create('SHA-256')", hash.ToString (), defaultSHA256);
  136. // try to build an incorrect hash algorithms
  137. try {
  138. hash = SHA256.Create ("MD5");
  139. Fail ("SHA256.Create('MD5') should throw InvalidCastException");
  140. }
  141. catch (InvalidCastException) {
  142. // do nothing, this is what we expect
  143. }
  144. catch (Exception e) {
  145. Fail ("SHA256.Create('MD5') should throw InvalidCastException not " + e.ToString ());
  146. }
  147. // try to build invalid implementation
  148. hash = SHA256.Create ("InvalidHash");
  149. AssertNull ("SHA256.Create('InvalidHash')", hash);
  150. // try to build null implementation
  151. try {
  152. hash = SHA256.Create (null);
  153. Fail ("SHA256.Create(null) should throw ArgumentNullException");
  154. }
  155. catch (ArgumentNullException) {
  156. // do nothing, this is what we expect
  157. }
  158. catch (Exception e) {
  159. Fail ("SHA256.Create(null) should throw ArgumentNullException not " + e.ToString ());
  160. }
  161. }
  162. // none of those values changes for any implementation of defaultSHA256
  163. public virtual void TestStaticInfo () {
  164. string className = hash.ToString ();
  165. AssertEquals (className + ".HashSize", 256, hash.HashSize);
  166. AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
  167. AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);
  168. }
  169. }
  170. }