SHA512Test.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // SHA512Test.cs - NUnit Test Cases for SHA512
  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. // SHA512 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 SHA512Test : HashAlgorithmTest {
  21. protected override void SetUp ()
  22. {
  23. hash = SHA512.Create ();
  24. }
  25. protected override void TearDown () {}
  26. // test vectors from NIST FIPS 186-2
  27. private string input1 = "abc";
  28. private string input2 = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  29. public void FIPS186_Test1 (SHA512 hash)
  30. {
  31. string className = hash.ToString ();
  32. byte[] result = { 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
  33. 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
  34. 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
  35. 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
  36. 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
  37. 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
  38. 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
  39. 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f };
  40. byte[] input = Encoding.Default.GetBytes (input1);
  41. string testName = className + " 1";
  42. FIPS186_a (testName, hash, input, result);
  43. FIPS186_b (testName, hash, input, result);
  44. FIPS186_c (testName, hash, input, result);
  45. FIPS186_d (testName, hash, input, result);
  46. FIPS186_e (testName, hash, input, result);
  47. }
  48. public void FIPS186_Test2 (SHA512 hash)
  49. {
  50. string className = hash.ToString ();
  51. byte[] result = { 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda,
  52. 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
  53. 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
  54. 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
  55. 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4,
  56. 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
  57. 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
  58. 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09 };
  59. byte[] input = Encoding.Default.GetBytes (input2);
  60. string testName = className + " 2";
  61. FIPS186_a (testName, hash, input, result);
  62. FIPS186_b (testName, hash, input, result);
  63. FIPS186_c (testName, hash, input, result);
  64. FIPS186_d (testName, hash, input, result);
  65. FIPS186_e (testName, hash, input, result);
  66. }
  67. public void FIPS186_Test3 (SHA512 hash)
  68. {
  69. string className = hash.ToString ();
  70. byte[] result = { 0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64,
  71. 0x4e, 0x2e, 0x42, 0xc7, 0xbc, 0x15, 0xb4, 0x63,
  72. 0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20, 0x44, 0x28,
  73. 0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb,
  74. 0xde, 0x0f, 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a,
  75. 0x4c, 0xb0, 0x43, 0x2c, 0xe5, 0x77, 0xc3, 0x1b,
  76. 0xeb, 0x00, 0x9c, 0x5c, 0x2c, 0x49, 0xaa, 0x2e,
  77. 0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b };
  78. byte[] input = new byte [1000000];
  79. for (int i = 0; i < 1000000; i++)
  80. input[i] = 0x61; // a
  81. string testName = className + " 3";
  82. FIPS186_a (testName, hash, input, result);
  83. FIPS186_b (testName, hash, input, result);
  84. FIPS186_c (testName, hash, input, result);
  85. FIPS186_d (testName, hash, input, result);
  86. FIPS186_e (testName, hash, input, result);
  87. }
  88. public void FIPS186_a (string testName, SHA512 hash, byte[] input, byte[] result)
  89. {
  90. byte[] output = hash.ComputeHash (input);
  91. AssertEquals (testName + ".a.1", result, output);
  92. AssertEquals (testName + ".a.2", result, hash.Hash);
  93. // required or next operation will still return old hash
  94. hash.Initialize ();
  95. }
  96. public void FIPS186_b (string testName, SHA512 hash, byte[] input, byte[] result)
  97. {
  98. byte[] output = hash.ComputeHash (input, 0, input.Length);
  99. AssertEquals (testName + ".b.1", result, output);
  100. AssertEquals (testName + ".b.2", result, hash.Hash);
  101. // required or next operation will still return old hash
  102. hash.Initialize ();
  103. }
  104. public void FIPS186_c (string testName, SHA512 hash, byte[] input, byte[] result)
  105. {
  106. MemoryStream ms = new MemoryStream (input);
  107. byte[] output = hash.ComputeHash (ms);
  108. AssertEquals (testName + ".c.1", result, output);
  109. AssertEquals (testName + ".c.2", result, hash.Hash);
  110. // required or next operation will still return old hash
  111. hash.Initialize ();
  112. }
  113. public void FIPS186_d (string testName, SHA512 hash, byte[] input, byte[] result)
  114. {
  115. byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
  116. // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
  117. // AssertEquals( testName + ".d.1", result, output );
  118. AssertEquals (testName + ".d", result, hash.Hash);
  119. // required or next operation will still return old hash
  120. hash.Initialize ();
  121. }
  122. public void FIPS186_e (string testName, SHA512 hash, byte[] input, byte[] result)
  123. {
  124. byte[] copy = new byte [input.Length];
  125. for (int i=0; i < input.Length - 1; i++)
  126. hash.TransformBlock (input, i, 1, copy, i);
  127. byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
  128. // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
  129. // AssertEquals (testName + ".e.1", result, output);
  130. AssertEquals (testName + ".e", result, hash.Hash);
  131. // required or next operation will still return old hash
  132. hash.Initialize ();
  133. }
  134. public override void TestCreate ()
  135. {
  136. // Note: These tests will only be valid without a "machine.config" file
  137. // or a "machine.config" file that do not modify the default algorithm
  138. // configuration.
  139. const string defaultSHA512 = "System.Security.Cryptography.SHA512Managed";
  140. // try to build the default implementation
  141. SHA512 hash = SHA512.Create ();
  142. AssertEquals ("SHA512.Create()", hash.ToString (), defaultSHA512);
  143. // try to build, in every way, a SHA512 implementation
  144. hash = SHA512.Create ("SHA512");
  145. AssertEquals ("SHA512.Create('SHA512')", hash.ToString (), defaultSHA512);
  146. hash = SHA512.Create ("SHA-512");
  147. AssertEquals ("SHA512.Create('SHA-512')", hash.ToString (), defaultSHA512);
  148. // try to build an incorrect hash algorithms
  149. try {
  150. hash = SHA512.Create ("MD5");
  151. Fail ("SHA512.Create('MD5') should throw InvalidCastException");
  152. }
  153. catch (InvalidCastException) {
  154. // do nothing, this is what we expect
  155. }
  156. catch (Exception e) {
  157. Fail ("SHA512.Create('MD5') should throw InvalidCastException not " + e.ToString ());
  158. }
  159. // try to build invalid implementation
  160. hash = SHA512.Create ("InvalidHash");
  161. AssertNull ("SHA512.Create('InvalidHash')", hash);
  162. // try to build null implementation
  163. try {
  164. hash = SHA512.Create (null);
  165. Fail ("SHA512.Create(null) should throw ArgumentNullException");
  166. }
  167. catch (ArgumentNullException) {
  168. // do nothing, this is what we expect
  169. }
  170. catch (Exception e) {
  171. Fail ("SHA512.Create(null) should throw ArgumentNullException not " + e.ToString ());
  172. }
  173. }
  174. // none of those values changes for any implementation of defaultSHA512
  175. public virtual void TestStaticInfo ()
  176. {
  177. string className = hash.ToString ();
  178. AssertEquals (className + ".HashSize", 512, hash.HashSize);
  179. AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
  180. AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);
  181. }
  182. }
  183. }