SHA256Managed.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // System.Security.Cryptography SHA256Managed Class implementation
  3. //
  4. // Author:
  5. // Matthew S. Ford ([email protected])
  6. //
  7. // (C) 2001
  8. //
  9. using System.Security.Cryptography;
  10. namespace System.Security.Cryptography {
  11. /// <summary>
  12. /// C# implementation of the SHA1 cryptographic hash function.
  13. /// LAMESPEC?: Basically the same thing as SHA1Managed except for how its implemented.
  14. /// </summary>
  15. public class SHA256Managed : SHA256 {
  16. private const int BLOCK_SIZE_BYTES = 64;
  17. private const int HASH_SIZE_BYTES = 32;
  18. private const int HASH_SIZE_BITS = 256;
  19. protected uint[] _H;
  20. private uint[] K;
  21. protected uint count;
  22. private byte[] _ProcessingBuffer; // Used to start data when passed less than a block worth.
  23. private int _ProcessingBufferCount; // Counts how much data we have stored that still needs processed.
  24. /// <summary>
  25. /// Creates a new SHA256Managed class.
  26. /// </summary>
  27. public SHA256Managed () {
  28. _H = new uint[8];
  29. HashSizeValue = HASH_SIZE_BITS;
  30. _ProcessingBuffer = new byte[BLOCK_SIZE_BYTES];
  31. K = new uint[64];
  32. K[0] = 0x428A2F98; K[1] = 0x71374491; K[2] = 0xB5C0FBCF; K[3] = 0xE9B5DBA5;
  33. K[4] = 0x3956C25B; K[5] = 0x59F111F1; K[6] = 0x923F82A4; K[7] = 0xAB1C5ED5;
  34. K[8] = 0xD807AA98; K[9] = 0x12835B01; K[10] = 0x243185BE; K[11] = 0x550C7DC3;
  35. K[12] = 0x72BE5D74; K[13] = 0x80DEB1FE; K[14] = 0x9BDC06A7; K[15] = 0xC19BF174;
  36. K[16] = 0xE49B69C1; K[17] = 0xEFBE4786; K[18] = 0x0FC19DC6; K[19] = 0x240CA1CC;
  37. K[20] = 0x2DE92C6F; K[21] = 0x4A7484AA; K[22] = 0x5CB0A9DC; K[23] = 0x76F988DA;
  38. K[24] = 0x983E5152; K[25] = 0xA831C66D; K[26] = 0xB00327C8; K[27] = 0xBF597FC7;
  39. K[28] = 0xC6E00BF3; K[29] = 0xD5A79147; K[30] = 0x06CA6351; K[31] = 0x14292967;
  40. K[32] = 0x27B70A85; K[33] = 0x2E1B2138; K[34] = 0x4D2C6DFC; K[35] = 0x53380D13;
  41. K[36] = 0x650A7354; K[37] = 0x766A0ABB; K[38] = 0x81C2C92E; K[39] = 0x92722C85;
  42. K[40] = 0xA2BFE8A1; K[41] = 0xA81A664B; K[42] = 0xC24B8B70; K[43] = 0xC76C51A3;
  43. K[44] = 0xD192E819; K[45] = 0xD6990624; K[46] = 0xF40E3585; K[47] = 0x106AA070;
  44. K[48] = 0x19A4C116; K[49] = 0x1E376C08; K[50] = 0x2748774C; K[51] = 0x34B0BCB5;
  45. K[52] = 0x391C0CB3; K[53] = 0x4ED8AA4A; K[54] = 0x5B9CCA4F; K[55] = 0x682E6FF3;
  46. K[56] = 0x748F82EE; K[57] = 0x78A5636F; K[58] = 0x84C87814; K[59] = 0x8CC70208;
  47. K[60] = 0x90BEFFFA; K[61] = 0xA4506CEB; K[62] = 0xBEF9A3F7; K[63] = 0xC67178F2;
  48. Initialize();
  49. }
  50. /// <summary>
  51. /// Internal function handling a subset of the algorithm.
  52. /// </summary>
  53. private uint Ch (uint u, uint v, uint w) {
  54. return (u&v) ^ (~u&w);
  55. }
  56. /// <summary>
  57. /// Internal function handling a subset of the algorithm.
  58. /// </summary>
  59. private uint Maj (uint u, uint v, uint w) {
  60. return (u&v) ^ (u&w) ^ (v&w);
  61. }
  62. /// <summary>
  63. /// Internal function handling a subset of the algorithm.
  64. /// </summary>
  65. private uint Ro0 (uint x) {
  66. return ((x >> 7) | (x << 25))
  67. ^ ((x >> 18) | (x << 14))
  68. ^ (x >> 3);
  69. }
  70. /// <summary>
  71. /// Internal function handling a subset of the algorithm.
  72. /// </summary>
  73. private uint Ro1 (uint x) {
  74. return ((x >> 17) | (x << 15))
  75. ^ ((x >> 19) | (x << 13))
  76. ^ (x >> 10);
  77. }
  78. /// <summary>
  79. /// Internal function handling a subset of the algorithm.
  80. /// </summary>
  81. private uint Sig0 (uint x) {
  82. return ((x >> 2) | (x << 30))
  83. ^ ((x >> 13) | (x << 19))
  84. ^ ((x >> 22) | (x << 10));
  85. }
  86. /// <summary>
  87. /// Internal function handling a subset of the algorithm.
  88. /// </summary>
  89. private uint Sig1 (uint x) {
  90. return ((x >> 6) | (x << 26))
  91. ^ ((x >> 11) | (x << 21))
  92. ^ ((x >> 25) | (x << 7));
  93. }
  94. /// <summary>
  95. /// Drives the hashing function.
  96. /// </summary>
  97. /// <param name="rgb">Byte array containing the data to hash.</param>
  98. /// <param name="start">Where in the input buffer to start.</param>
  99. /// <param name="size">Size in bytes of the data in the buffer to hash.</param>
  100. protected override void HashCore (byte[] rgb, int start, int size) {
  101. int i;
  102. State = 1;
  103. if (_ProcessingBufferCount != 0) {
  104. if (size < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) {
  105. System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, size);
  106. _ProcessingBufferCount += size;
  107. return;
  108. }
  109. else {
  110. i = (BLOCK_SIZE_BYTES - _ProcessingBufferCount);
  111. System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, i);
  112. ProcessBlock (_ProcessingBuffer, 0);
  113. _ProcessingBufferCount = 0;
  114. start += i;
  115. size -= i;
  116. }
  117. }
  118. for (i=0; i<size-size%BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) {
  119. ProcessBlock (rgb, start+i);
  120. }
  121. if (size%BLOCK_SIZE_BYTES != 0) {
  122. System.Buffer.BlockCopy (rgb, size-size%BLOCK_SIZE_BYTES+start, _ProcessingBuffer, 0, size%BLOCK_SIZE_BYTES);
  123. _ProcessingBufferCount = size%BLOCK_SIZE_BYTES;
  124. }
  125. }
  126. /// <summary>
  127. /// This finalizes the hash. Takes the data from the chaining variables and returns it.
  128. /// </summary>
  129. protected override byte[] HashFinal () {
  130. byte[] hash = new byte[32];
  131. int i, j;
  132. ProcessFinalBlock(_ProcessingBuffer, 0, _ProcessingBufferCount);
  133. for (i=0; i<8; i++) {
  134. for (j=0; j<4; j++) {
  135. hash[i*4+j] = (byte)(_H[i] >> (24-j*8));
  136. }
  137. }
  138. State = 0;
  139. return hash;
  140. }
  141. /// <summary>
  142. /// Resets the class after use. Called automatically after hashing is done.
  143. /// </summary>
  144. public override void Initialize () {
  145. count = 0;
  146. _ProcessingBufferCount = 0;
  147. _H[0] = 0x6A09E667;
  148. _H[1] = 0xBB67AE85;
  149. _H[2] = 0x3C6EF372;
  150. _H[3] = 0xA54FF53A;
  151. _H[4] = 0x510E527F;
  152. _H[5] = 0x9B05688C;
  153. _H[6] = 0x1F83D9AB;
  154. _H[7] = 0x5BE0CD19;
  155. }
  156. /// <summary>
  157. /// This is the meat of the hash function. It is what processes each block one at a time.
  158. /// </summary>
  159. /// <param name="inputBuffer">Byte array to process data from.</param>
  160. /// <param name="inputOffset">Where in the byte array to start processing.</param>
  161. public void ProcessBlock(byte[] inputBuffer, int inputOffset) {
  162. uint a, b, c, d, e, f, g, h;
  163. uint t1, t2;
  164. int i;
  165. uint[] buff;
  166. count += BLOCK_SIZE_BYTES;
  167. buff = new uint[64];
  168. for (i=0; i<16; i++) {
  169. buff[i] = ((uint)(inputBuffer[inputOffset+4*i]) << 24)
  170. | ((uint)(inputBuffer[inputOffset+4*i+1]) << 16)
  171. | ((uint)(inputBuffer[inputOffset+4*i+2]) << 8)
  172. | ((uint)(inputBuffer[inputOffset+4*i+3]));
  173. }
  174. for (i=16; i<64; i++) {
  175. buff[i] = Ro1(buff[i-2]) + buff[i-7] + Ro0(buff[i-15]) + buff[i-16];
  176. }
  177. a = _H[0];
  178. b = _H[1];
  179. c = _H[2];
  180. d = _H[3];
  181. e = _H[4];
  182. f = _H[5];
  183. g = _H[6];
  184. h = _H[7];
  185. for (i=0; i<64; i++) {
  186. t1 = h + Sig1(e) + Ch(e,f,g) + K[i] + buff[i];
  187. t2 = Sig0(a) + Maj(a,b,c);
  188. h = g;
  189. g = f;
  190. f = e;
  191. e = d + t1;
  192. d = c;
  193. c = b;
  194. b = a;
  195. a = t1 + t2;
  196. }
  197. _H[0] += a;
  198. _H[1] += b;
  199. _H[2] += c;
  200. _H[3] += d;
  201. _H[4] += e;
  202. _H[5] += f;
  203. _H[6] += g;
  204. _H[7] += h;
  205. }
  206. /// <summary>
  207. /// Pads and then processes the final block.
  208. /// Non-standard.
  209. /// </summary>
  210. /// <param name="inputBuffer">Buffer to grab data from.</param>
  211. /// <param name="inputOffset">Position in buffer in bytes to get data from.</param>
  212. /// <param name="inputCount">How much data in bytes in the buffer to use.</param>
  213. public void ProcessFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) {
  214. byte[] fooBuffer;
  215. int paddingSize;
  216. int i;
  217. uint size;
  218. paddingSize = (int)(56 - (inputCount + count) % BLOCK_SIZE_BYTES);
  219. if (paddingSize < 1)
  220. paddingSize += BLOCK_SIZE_BYTES;
  221. fooBuffer = new byte[inputCount+paddingSize+8];
  222. for (i=0; i<inputCount; i++) {
  223. fooBuffer[i] = inputBuffer[i+inputOffset];
  224. }
  225. fooBuffer[inputCount] = 0x80;
  226. for (i=inputCount+1; i<inputCount+paddingSize; i++) {
  227. fooBuffer[i] = 0x00;
  228. }
  229. size = (uint)(count+inputCount);
  230. size *= 8;
  231. fooBuffer[inputCount+paddingSize] = 0x00;
  232. fooBuffer[inputCount+paddingSize+1] = 0x00;
  233. fooBuffer[inputCount+paddingSize+2] = 0x00;
  234. fooBuffer[inputCount+paddingSize+3] = 0x00;
  235. fooBuffer[inputCount+paddingSize+4] = (byte)((size) >> 24);
  236. fooBuffer[inputCount+paddingSize+5] = (byte)((size) >> 16);
  237. fooBuffer[inputCount+paddingSize+6] = (byte)((size) >> 8);
  238. fooBuffer[inputCount+paddingSize+7] = (byte)((size) >> 0);
  239. ProcessBlock(fooBuffer, 0);
  240. if (inputCount+paddingSize+8 == 128) {
  241. ProcessBlock(fooBuffer, 64);
  242. }
  243. }
  244. }
  245. }