SHA1CryptoServiceProvider.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. //
  2. // System.Security.Cryptography SHA1CryptoServiceProvider Class implementation
  3. //
  4. // Author:
  5. // Matthew S. Ford ([email protected])
  6. //
  7. // Copyright 2001 by Matthew S. Ford.
  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 SHA1CryptoServiceProvider : SHA1 {
  16. private const int BLOCK_SIZE_BYTES = 64;
  17. private const int HASH_SIZE_BYTES = 20;
  18. private const int HASH_SIZE_BITS = 160;
  19. protected uint[] _H; // these are my chaining variables
  20. protected uint count;
  21. private byte[] _ProcessingBuffer; // Used to start data when passed less than a block worth.
  22. private int _ProcessingBufferCount; // Counts how much data we have stored that still needs processed.
  23. /// <summary>
  24. /// Creates a new SHA1CryptoServiceProvider.
  25. /// </summary>
  26. public SHA1CryptoServiceProvider () {
  27. _H = new uint[5];
  28. HashSizeValue = HASH_SIZE_BITS;
  29. _ProcessingBuffer = new byte[BLOCK_SIZE_BYTES];
  30. Initialize();
  31. }
  32. /// <summary>
  33. /// Drives the hashing function.
  34. /// </summary>
  35. /// <param name="rgb">Byte array containing the data to hash.</param>
  36. /// <param name="start">Where in the input buffer to start.</param>
  37. /// <param name="size">Size in bytes of the data in the buffer to hash.</param>
  38. protected override void HashCore (byte[] rgb, int start, int size) {
  39. int i;
  40. State = 1;
  41. if (_ProcessingBufferCount != 0) {
  42. if (size < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) {
  43. System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, size);
  44. _ProcessingBufferCount += size;
  45. return;
  46. }
  47. else {
  48. i = (BLOCK_SIZE_BYTES - _ProcessingBufferCount);
  49. System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, i);
  50. ProcessBlock (_ProcessingBuffer, 0);
  51. _ProcessingBufferCount = 0;
  52. start += i;
  53. size -= i;
  54. }
  55. }
  56. for (i=0; i<size-size%BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) {
  57. ProcessBlock (rgb, start+i);
  58. }
  59. if (size%BLOCK_SIZE_BYTES != 0) {
  60. System.Buffer.BlockCopy (rgb, size-size%BLOCK_SIZE_BYTES+start, _ProcessingBuffer, 0, size%BLOCK_SIZE_BYTES);
  61. _ProcessingBufferCount = size%BLOCK_SIZE_BYTES;
  62. }
  63. }
  64. /// <summary>
  65. /// This finalizes the hash. Takes the data from the chaining variables and returns it.
  66. /// </summary>
  67. protected override byte[] HashFinal () {
  68. byte[] hash = new byte[20];
  69. int i, j;
  70. ProcessFinalBlock(_ProcessingBuffer, 0, _ProcessingBufferCount);
  71. for (i=0; i<5; i++) {
  72. for (j=0; j<4; j++) {
  73. hash[i*4+j] = (byte)(_H[i] >> (8*(3-j)));
  74. }
  75. }
  76. State = 0;
  77. return hash;
  78. }
  79. /// <summary>
  80. /// Resets the class after use. Called automatically after hashing is done.
  81. /// </summary>
  82. public override void Initialize () {
  83. count = 0;
  84. _ProcessingBufferCount = 0;
  85. _H[0] = 0x67452301;
  86. _H[1] = 0xefcdab89;
  87. _H[2] = 0x98badcfe;
  88. _H[3] = 0x10325476;
  89. _H[4] = 0xC3D2E1F0;
  90. }
  91. /// <summary>
  92. /// This is the meat of the hash function. It is what processes each block one at a time.
  93. /// </summary>
  94. /// <param name="inputBuffer">Byte array to process data from.</param>
  95. /// <param name="inputOffset">Where in the byte array to start processing.</param>
  96. private void ProcessBlock(byte[] inputBuffer, int inputOffset) {
  97. uint[] buff = new uint[80];
  98. uint a, b, c, d, e;
  99. int i;
  100. count += BLOCK_SIZE_BYTES;
  101. for (i=0; i<16; i++) {
  102. buff[i] = ((uint)(inputBuffer[inputOffset+4*i]) << 24)
  103. | ((uint)(inputBuffer[inputOffset+4*i+1]) << 16)
  104. | ((uint)(inputBuffer[inputOffset+4*i+2]) << 8)
  105. | ((uint)(inputBuffer[inputOffset+4*i+3]));
  106. }
  107. for (i=16; i<80; i++) {
  108. buff[i] = ((buff[i-3] ^ buff[i-8] ^ buff[i-14] ^ buff[i-16]) << 1)
  109. | ((buff[i-3] ^ buff[i-8] ^ buff[i-14] ^ buff[i-16]) >> 31);
  110. }
  111. a = _H[0];
  112. b = _H[1];
  113. c = _H[2];
  114. d = _H[3];
  115. e = _H[4];
  116. // This function was unrolled because it seems to be doubling our performance with current compiler/VM.
  117. // Possibly roll up if this changes.
  118. // ---- Round 1 --------
  119. e += ((a << 5) | (a >> 27)) + (((c ^ d) & b) ^ d) + 0x5A827999 + buff[0];
  120. b = (b << 30) | (b >> 2);
  121. d += ((e << 5) | (e >> 27)) + (((b ^ c) & a) ^ c) + 0x5A827999 + buff[1];
  122. a = (a << 30) | (a >> 2);
  123. c += ((d << 5) | (d >> 27)) + (((a ^ b) & e) ^ b) + 0x5A827999 + buff[2];
  124. e = (e << 30) | (e >> 2);
  125. b += ((c << 5) | (c >> 27)) + (((e ^ a) & d) ^ a) + 0x5A827999 + buff[3];
  126. d = (d << 30) | (d >> 2);
  127. a += ((b << 5) | (b >> 27)) + (((d ^ e) & c) ^ e) + 0x5A827999 + buff[4];
  128. c = (c << 30) | (c >> 2);
  129. e += ((a << 5) | (a >> 27)) + (((c ^ d) & b) ^ d) + 0x5A827999 + buff[5];
  130. b = (b << 30) | (b >> 2);
  131. d += ((e << 5) | (e >> 27)) + (((b ^ c) & a) ^ c) + 0x5A827999 + buff[6];
  132. a = (a << 30) | (a >> 2);
  133. c += ((d << 5) | (d >> 27)) + (((a ^ b) & e) ^ b) + 0x5A827999 + buff[7];
  134. e = (e << 30) | (e >> 2);
  135. b += ((c << 5) | (c >> 27)) + (((e ^ a) & d) ^ a) + 0x5A827999 + buff[8];
  136. d = (d << 30) | (d >> 2);
  137. a += ((b << 5) | (b >> 27)) + (((d ^ e) & c) ^ e) + 0x5A827999 + buff[9];
  138. c = (c << 30) | (c >> 2);
  139. e += ((a << 5) | (a >> 27)) + (((c ^ d) & b) ^ d) + 0x5A827999 + buff[10];
  140. b = (b << 30) | (b >> 2);
  141. d += ((e << 5) | (e >> 27)) + (((b ^ c) & a) ^ c) + 0x5A827999 + buff[11];
  142. a = (a << 30) | (a >> 2);
  143. c += ((d << 5) | (d >> 27)) + (((a ^ b) & e) ^ b) + 0x5A827999 + buff[12];
  144. e = (e << 30) | (e >> 2);
  145. b += ((c << 5) | (c >> 27)) + (((e ^ a) & d) ^ a) + 0x5A827999 + buff[13];
  146. d = (d << 30) | (d >> 2);
  147. a += ((b << 5) | (b >> 27)) + (((d ^ e) & c) ^ e) + 0x5A827999 + buff[14];
  148. c = (c << 30) | (c >> 2);
  149. e += ((a << 5) | (a >> 27)) + (((c ^ d) & b) ^ d) + 0x5A827999 + buff[15];
  150. b = (b << 30) | (b >> 2);
  151. d += ((e << 5) | (e >> 27)) + (((b ^ c) & a) ^ c) + 0x5A827999 + buff[16];
  152. a = (a << 30) | (a >> 2);
  153. c += ((d << 5) | (d >> 27)) + (((a ^ b) & e) ^ b) + 0x5A827999 + buff[17];
  154. e = (e << 30) | (e >> 2);
  155. b += ((c << 5) | (c >> 27)) + (((e ^ a) & d) ^ a) + 0x5A827999 + buff[18];
  156. d = (d << 30) | (d >> 2);
  157. a += ((b << 5) | (b >> 27)) + (((d ^ e) & c) ^ e) + 0x5A827999 + buff[19];
  158. c = (c << 30) | (c >> 2);
  159. // ---- Round 2 --------
  160. e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0x6ED9EBA1 + buff[20];
  161. b = (b << 30) | (b >> 2);
  162. d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0x6ED9EBA1 + buff[21];
  163. a = (a << 30) | (a >> 2);
  164. c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0x6ED9EBA1 + buff[22];
  165. e = (e << 30) | (e >> 2);
  166. b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0x6ED9EBA1 + buff[23];
  167. d = (d << 30) | (d >> 2);
  168. a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0x6ED9EBA1 + buff[24];
  169. c = (c << 30) | (c >> 2);
  170. e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0x6ED9EBA1 + buff[25];
  171. b = (b << 30) | (b >> 2);
  172. d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0x6ED9EBA1 + buff[26];
  173. a = (a << 30) | (a >> 2);
  174. c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0x6ED9EBA1 + buff[27];
  175. e = (e << 30) | (e >> 2);
  176. b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0x6ED9EBA1 + buff[28];
  177. d = (d << 30) | (d >> 2);
  178. a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0x6ED9EBA1 + buff[29];
  179. c = (c << 30) | (c >> 2);
  180. e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0x6ED9EBA1 + buff[30];
  181. b = (b << 30) | (b >> 2);
  182. d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0x6ED9EBA1 + buff[31];
  183. a = (a << 30) | (a >> 2);
  184. c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0x6ED9EBA1 + buff[32];
  185. e = (e << 30) | (e >> 2);
  186. b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0x6ED9EBA1 + buff[33];
  187. d = (d << 30) | (d >> 2);
  188. a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0x6ED9EBA1 + buff[34];
  189. c = (c << 30) | (c >> 2);
  190. e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0x6ED9EBA1 + buff[35];
  191. b = (b << 30) | (b >> 2);
  192. d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0x6ED9EBA1 + buff[36];
  193. a = (a << 30) | (a >> 2);
  194. c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0x6ED9EBA1 + buff[37];
  195. e = (e << 30) | (e >> 2);
  196. b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0x6ED9EBA1 + buff[38];
  197. d = (d << 30) | (d >> 2);
  198. a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0x6ED9EBA1 + buff[39];
  199. c = (c << 30) | (c >> 2);
  200. // ---- Round 3 --------
  201. e += ((a << 5) | (a >> 27)) + ((b&c) | (b&d) | (c&d)) + 0x8F1BBCDC + buff[40];
  202. b = (b << 30) | (b >> 2);
  203. d += ((e << 5) | (e >> 27)) + ((a&b) | (a&c) | (b&c)) + 0x8F1BBCDC + buff[41];
  204. a = (a << 30) | (a >> 2);
  205. c += ((d << 5) | (d >> 27)) + ((e&a) | (e&b) | (a&b)) + 0x8F1BBCDC + buff[42];
  206. e = (e << 30) | (e >> 2);
  207. b += ((c << 5) | (c >> 27)) + ((d&e) | (d&a) | (e&a)) + 0x8F1BBCDC + buff[43];
  208. d = (d << 30) | (d >> 2);
  209. a += ((b << 5) | (b >> 27)) + ((c&d) | (c&e) | (d&e)) + 0x8F1BBCDC + buff[44];
  210. c = (c << 30) | (c >> 2);
  211. e += ((a << 5) | (a >> 27)) + ((b&c) | (b&d) | (c&d)) + 0x8F1BBCDC + buff[45];
  212. b = (b << 30) | (b >> 2);
  213. d += ((e << 5) | (e >> 27)) + ((a&b) | (a&c) | (b&c)) + 0x8F1BBCDC + buff[46];
  214. a = (a << 30) | (a >> 2);
  215. c += ((d << 5) | (d >> 27)) + ((e&a) | (e&b) | (a&b)) + 0x8F1BBCDC + buff[47];
  216. e = (e << 30) | (e >> 2);
  217. b += ((c << 5) | (c >> 27)) + ((d&e) | (d&a) | (e&a)) + 0x8F1BBCDC + buff[48];
  218. d = (d << 30) | (d >> 2);
  219. a += ((b << 5) | (b >> 27)) + ((c&d) | (c&e) | (d&e)) + 0x8F1BBCDC + buff[49];
  220. c = (c << 30) | (c >> 2);
  221. e += ((a << 5) | (a >> 27)) + ((b&c) | (b&d) | (c&d)) + 0x8F1BBCDC + buff[50];
  222. b = (b << 30) | (b >> 2);
  223. d += ((e << 5) | (e >> 27)) + ((a&b) | (a&c) | (b&c)) + 0x8F1BBCDC + buff[51];
  224. a = (a << 30) | (a >> 2);
  225. c += ((d << 5) | (d >> 27)) + ((e&a) | (e&b) | (a&b)) + 0x8F1BBCDC + buff[52];
  226. e = (e << 30) | (e >> 2);
  227. b += ((c << 5) | (c >> 27)) + ((d&e) | (d&a) | (e&a)) + 0x8F1BBCDC + buff[53];
  228. d = (d << 30) | (d >> 2);
  229. a += ((b << 5) | (b >> 27)) + ((c&d) | (c&e) | (d&e)) + 0x8F1BBCDC + buff[54];
  230. c = (c << 30) | (c >> 2);
  231. e += ((a << 5) | (a >> 27)) + ((b&c) | (b&d) | (c&d)) + 0x8F1BBCDC + buff[55];
  232. b = (b << 30) | (b >> 2);
  233. d += ((e << 5) | (e >> 27)) + ((a&b) | (a&c) | (b&c)) + 0x8F1BBCDC + buff[56];
  234. a = (a << 30) | (a >> 2);
  235. c += ((d << 5) | (d >> 27)) + ((e&a) | (e&b) | (a&b)) + 0x8F1BBCDC + buff[57];
  236. e = (e << 30) | (e >> 2);
  237. b += ((c << 5) | (c >> 27)) + ((d&e) | (d&a) | (e&a)) + 0x8F1BBCDC + buff[58];
  238. d = (d << 30) | (d >> 2);
  239. a += ((b << 5) | (b >> 27)) + ((c&d) | (c&e) | (d&e)) + 0x8F1BBCDC + buff[59];
  240. c = (c << 30) | (c >> 2);
  241. // ---- Round 4 --------
  242. e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0xCA62C1D6 + buff[60];
  243. b = (b << 30) | (b >> 2);
  244. d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0xCA62C1D6 + buff[61];
  245. a = (a << 30) | (a >> 2);
  246. c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0xCA62C1D6 + buff[62];
  247. e = (e << 30) | (e >> 2);
  248. b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0xCA62C1D6 + buff[63];
  249. d = (d << 30) | (d >> 2);
  250. a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0xCA62C1D6 + buff[64];
  251. c = (c << 30) | (c >> 2);
  252. e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0xCA62C1D6 + buff[65];
  253. b = (b << 30) | (b >> 2);
  254. d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0xCA62C1D6 + buff[66];
  255. a = (a << 30) | (a >> 2);
  256. c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0xCA62C1D6 + buff[67];
  257. e = (e << 30) | (e >> 2);
  258. b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0xCA62C1D6 + buff[68];
  259. d = (d << 30) | (d >> 2);
  260. a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0xCA62C1D6 + buff[69];
  261. c = (c << 30) | (c >> 2);
  262. e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0xCA62C1D6 + buff[70];
  263. b = (b << 30) | (b >> 2);
  264. d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0xCA62C1D6 + buff[71];
  265. a = (a << 30) | (a >> 2);
  266. c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0xCA62C1D6 + buff[72];
  267. e = (e << 30) | (e >> 2);
  268. b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0xCA62C1D6 + buff[73];
  269. d = (d << 30) | (d >> 2);
  270. a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0xCA62C1D6 + buff[74];
  271. c = (c << 30) | (c >> 2);
  272. e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0xCA62C1D6 + buff[75];
  273. b = (b << 30) | (b >> 2);
  274. d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0xCA62C1D6 + buff[76];
  275. a = (a << 30) | (a >> 2);
  276. c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0xCA62C1D6 + buff[77];
  277. e = (e << 30) | (e >> 2);
  278. b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0xCA62C1D6 + buff[78];
  279. d = (d << 30) | (d >> 2);
  280. a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0xCA62C1D6 + buff[79];
  281. c = (c << 30) | (c >> 2);
  282. _H[0] += a;
  283. _H[1] += b;
  284. _H[2] += c;
  285. _H[3] += d;
  286. _H[4] += e;
  287. }
  288. /// <summary>
  289. /// Pads and then processes the final block.
  290. /// Non-standard.
  291. /// </summary>
  292. /// <param name="inputBuffer">Buffer to grab data from.</param>
  293. /// <param name="inputOffset">Position in buffer in bytes to get data from.</param>
  294. /// <param name="inputCount">How much data in bytes in the buffer to use.</param>
  295. private void ProcessFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) {
  296. byte[] fooBuffer;
  297. int paddingSize;
  298. int i;
  299. uint size;
  300. paddingSize = (int)(56 - (inputCount + count) % BLOCK_SIZE_BYTES);
  301. if (paddingSize < 1)
  302. paddingSize += BLOCK_SIZE_BYTES;
  303. fooBuffer = new byte[inputCount+paddingSize+8];
  304. for (i=0; i<inputCount; i++) {
  305. fooBuffer[i] = inputBuffer[i+inputOffset];
  306. }
  307. fooBuffer[inputCount] = 0x80;
  308. for (i=inputCount+1; i<inputCount+paddingSize; i++) {
  309. fooBuffer[i] = 0x00;
  310. }
  311. size = (uint)(count+inputCount);
  312. size *= 8; // I deal in bytes. They algorythm deals in bits.
  313. fooBuffer[inputCount+paddingSize] = 0x00;
  314. fooBuffer[inputCount+paddingSize+1] = 0x00;
  315. fooBuffer[inputCount+paddingSize+2] = 0x00;
  316. fooBuffer[inputCount+paddingSize+3] = 0x00;
  317. fooBuffer[inputCount+paddingSize+4] = (byte)((size) >> 24);
  318. fooBuffer[inputCount+paddingSize+5] = (byte)((size) >> 16);
  319. fooBuffer[inputCount+paddingSize+6] = (byte)((size) >> 8);
  320. fooBuffer[inputCount+paddingSize+7] = (byte)((size) >> 0);
  321. ProcessBlock(fooBuffer, 0);
  322. if (inputCount+paddingSize+8 == 128) {
  323. ProcessBlock(fooBuffer, 64);
  324. }
  325. }
  326. }
  327. }