SHA2HashBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using System.Security.Cryptography;
  2. namespace Renci.SshNet.Security.Cryptography
  3. {
  4. /// <summary>
  5. /// SHA256 algorithm implementation.
  6. /// </summary>
  7. public abstract class SHA2HashBase : HashAlgorithm
  8. {
  9. protected ulong H1, H2, H3, H4, H5, H6, H7, H8;
  10. private readonly ulong[] X = new ulong[80];
  11. private int _offset;
  12. private readonly byte[] _buffer;
  13. private int _bufferOffset;
  14. private long _byteCount1;
  15. private long _byteCount2;
  16. /// <summary>
  17. /// Gets a value indicating whether the current transform can be reused.
  18. /// </summary>
  19. /// <returns>Always true.</returns>
  20. public override bool CanReuseTransform
  21. {
  22. get
  23. {
  24. return true;
  25. }
  26. }
  27. /// <summary>
  28. /// Gets a value indicating whether multiple blocks can be transformed.
  29. /// </summary>
  30. /// <returns>true if multiple blocks can be transformed; otherwise, false.</returns>
  31. public override bool CanTransformMultipleBlocks
  32. {
  33. get
  34. {
  35. return true;
  36. }
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="SHA512Hash" /> class.
  40. /// </summary>
  41. protected SHA2HashBase()
  42. {
  43. this._buffer = new byte[8];
  44. this.Initialize();
  45. }
  46. protected override void HashCore(byte[] array, int ibStart, int cbSize)
  47. {
  48. // Fill the current word
  49. while ((this._bufferOffset != 0) && (cbSize > 0))
  50. {
  51. this.Update(array[ibStart]);
  52. ibStart++;
  53. cbSize--;
  54. }
  55. // Process whole words.
  56. while (cbSize > this._buffer.Length)
  57. {
  58. ProcessWord(array, ibStart);
  59. ibStart += this._buffer.Length;
  60. cbSize -= this._buffer.Length;
  61. this._byteCount1 += this._buffer.Length;
  62. }
  63. // Load in the remainder.
  64. while (cbSize > 0)
  65. {
  66. this.Update(array[ibStart]);
  67. ibStart++;
  68. cbSize--;
  69. }
  70. }
  71. public override void Initialize()
  72. {
  73. this._byteCount1 = 0;
  74. this._byteCount2 = 0;
  75. this._bufferOffset = 0;
  76. for (int i = 0; i < this._buffer.Length; i++)
  77. {
  78. this._buffer[i] = 0;
  79. }
  80. this._offset = 0;
  81. for (int i = 0; i < this.X.Length; i++)
  82. {
  83. this.X[i] = 0;
  84. }
  85. }
  86. protected void Finish()
  87. {
  88. this.AdjustByteCounts();
  89. long lowBitLength = this._byteCount1 << 3;
  90. long hiBitLength = this._byteCount2;
  91. //
  92. // add the pad bytes.
  93. //
  94. this.Update((byte)128);
  95. while (this._bufferOffset != 0)
  96. {
  97. this.Update((byte)0);
  98. }
  99. this.ProcessLength(lowBitLength, hiBitLength);
  100. this.ProcessBlock();
  101. }
  102. private void Update(byte input)
  103. {
  104. this._buffer[_bufferOffset++] = input;
  105. if (this._bufferOffset == this._buffer.Length)
  106. {
  107. this.ProcessWord(this._buffer, 0);
  108. this._bufferOffset = 0;
  109. }
  110. this._byteCount1++;
  111. }
  112. private void ProcessWord(byte[] input, int inOff)
  113. {
  114. this.X[_offset] = SHA512Hash.BE_To_UInt64(input, inOff);
  115. if (++_offset == 16)
  116. {
  117. ProcessBlock();
  118. }
  119. }
  120. internal void ProcessLength(long lowW, long hiW)
  121. {
  122. if (_offset > 14)
  123. {
  124. this.ProcessBlock();
  125. }
  126. this.X[14] = (ulong)hiW;
  127. this.X[15] = (ulong)lowW;
  128. }
  129. private void ProcessBlock()
  130. {
  131. this.AdjustByteCounts();
  132. //
  133. // expand 16 word block into 80 word blocks.
  134. //
  135. for (int ti = 16; ti <= 79; ++ti)
  136. {
  137. X[ti] = Sigma1(X[ti - 2]) + X[ti - 7] + Sigma0(X[ti - 15]) + X[ti - 16];
  138. }
  139. //
  140. // set up working variables.
  141. //
  142. ulong a = H1;
  143. ulong b = H2;
  144. ulong c = H3;
  145. ulong d = H4;
  146. ulong e = H5;
  147. ulong f = H6;
  148. ulong g = H7;
  149. ulong h = H8;
  150. int t = 0;
  151. for (int i = 0; i < 10; i++)
  152. {
  153. // t = 8 * i
  154. h += Sum1(e) + Ch(e, f, g) + K[t] + X[t++];
  155. d += h;
  156. h += Sum0(a) + Maj(a, b, c);
  157. // t = 8 * i + 1
  158. g += Sum1(d) + Ch(d, e, f) + K[t] + X[t++];
  159. c += g;
  160. g += Sum0(h) + Maj(h, a, b);
  161. // t = 8 * i + 2
  162. f += Sum1(c) + Ch(c, d, e) + K[t] + X[t++];
  163. b += f;
  164. f += Sum0(g) + Maj(g, h, a);
  165. // t = 8 * i + 3
  166. e += Sum1(b) + Ch(b, c, d) + K[t] + X[t++];
  167. a += e;
  168. e += Sum0(f) + Maj(f, g, h);
  169. // t = 8 * i + 4
  170. d += Sum1(a) + Ch(a, b, c) + K[t] + X[t++];
  171. h += d;
  172. d += Sum0(e) + Maj(e, f, g);
  173. // t = 8 * i + 5
  174. c += Sum1(h) + Ch(h, a, b) + K[t] + X[t++];
  175. g += c;
  176. c += Sum0(d) + Maj(d, e, f);
  177. // t = 8 * i + 6
  178. b += Sum1(g) + Ch(g, h, a) + K[t] + X[t++];
  179. f += b;
  180. b += Sum0(c) + Maj(c, d, e);
  181. // t = 8 * i + 7
  182. a += Sum1(f) + Ch(f, g, h) + K[t] + X[t++];
  183. e += a;
  184. a += Sum0(b) + Maj(b, c, d);
  185. }
  186. H1 += a;
  187. H2 += b;
  188. H3 += c;
  189. H4 += d;
  190. H5 += e;
  191. H6 += f;
  192. H7 += g;
  193. H8 += h;
  194. //
  195. // reset the offset and clean out the word buffer.
  196. //
  197. this._offset = 0;
  198. for (int i = 0; i < this.X.Length; i++)
  199. {
  200. this.X[i] = 0;
  201. }
  202. }
  203. /// <summary>
  204. /// Adjust the byte counts so that byteCount2 represents the upper long (less 3 bits) word of the byte count.
  205. /// </summary>
  206. private void AdjustByteCounts()
  207. {
  208. if (this._byteCount1 > 0x1fffffffffffffffL)
  209. {
  210. this._byteCount2 += (long)((ulong)this._byteCount1 >> 61);
  211. this._byteCount1 &= 0x1fffffffffffffffL;
  212. }
  213. }
  214. /* SHA-384 and SHA-512 functions (as for SHA-256 but for longs) */
  215. private static ulong Ch(ulong x, ulong y, ulong z)
  216. {
  217. return (x & y) ^ (~x & z);
  218. }
  219. private static ulong Maj(ulong x, ulong y, ulong z)
  220. {
  221. return (x & y) ^ (x & z) ^ (y & z);
  222. }
  223. private static ulong Sum0(ulong x)
  224. {
  225. return ((x << 36) | (x >> 28)) ^ ((x << 30) | (x >> 34)) ^ ((x << 25) | (x >> 39));
  226. }
  227. private static ulong Sum1(ulong x)
  228. {
  229. return ((x << 50) | (x >> 14)) ^ ((x << 46) | (x >> 18)) ^ ((x << 23) | (x >> 41));
  230. }
  231. private static ulong Sigma0(ulong x)
  232. {
  233. return ((x << 63) | (x >> 1)) ^ ((x << 56) | (x >> 8)) ^ (x >> 7);
  234. }
  235. private static ulong Sigma1(ulong x)
  236. {
  237. return ((x << 45) | (x >> 19)) ^ ((x << 3) | (x >> 61)) ^ (x >> 6);
  238. }
  239. /* SHA-384 and SHA-512 Constants
  240. * (represent the first 64 bits of the fractional parts of the
  241. * cube roots of the first sixty-four prime numbers)
  242. */
  243. private static readonly ulong[] K =
  244. {
  245. 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
  246. 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
  247. 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
  248. 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,
  249. 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
  250. 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
  251. 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
  252. 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,
  253. 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
  254. 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
  255. 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,
  256. 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
  257. 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
  258. 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
  259. 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
  260. 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
  261. 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
  262. 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
  263. 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
  264. 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
  265. };
  266. protected static void UInt32_To_BE(uint n, byte[] bs, int off)
  267. {
  268. bs[off] = (byte)(n >> 24);
  269. bs[++off] = (byte)(n >> 16);
  270. bs[++off] = (byte)(n >> 8);
  271. bs[++off] = (byte)(n);
  272. }
  273. protected static void UInt64_To_BE(ulong n, byte[] bs, int off)
  274. {
  275. UInt32_To_BE((uint)(n >> 32), bs, off);
  276. UInt32_To_BE((uint)(n), bs, off + 4);
  277. }
  278. protected static ulong BE_To_UInt64(byte[] bs)
  279. {
  280. uint hi = BE_To_UInt32(bs);
  281. uint lo = BE_To_UInt32(bs, 4);
  282. return ((ulong)hi << 32) | (ulong)lo;
  283. }
  284. protected static ulong BE_To_UInt64(byte[] bs, int off)
  285. {
  286. uint hi = BE_To_UInt32(bs, off);
  287. uint lo = BE_To_UInt32(bs, off + 4);
  288. return ((ulong)hi << 32) | (ulong)lo;
  289. }
  290. protected static uint BE_To_UInt32(byte[] bs, int off)
  291. {
  292. uint n = (uint)bs[off] << 24;
  293. n |= (uint)bs[++off] << 16;
  294. n |= (uint)bs[++off] << 8;
  295. n |= (uint)bs[++off];
  296. return n;
  297. }
  298. protected static uint BE_To_UInt32(byte[] bs)
  299. {
  300. uint n = (uint)bs[0] << 24;
  301. n |= (uint)bs[1] << 16;
  302. n |= (uint)bs[2] << 8;
  303. n |= (uint)bs[3];
  304. return n;
  305. }
  306. }
  307. }