WhirlpoolDigest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  7. {
  8. /**
  9. * Implementation of WhirlpoolDigest, based on Java source published by Barreto and Rijmen.
  10. */
  11. public sealed class WhirlpoolDigest
  12. : IDigest, IMemoable
  13. {
  14. private const int BITCOUNT_ARRAY_SIZE = 32;
  15. private const int BYTE_LENGTH = 64;
  16. private const int DIGEST_LENGTH_BYTES = 512 / 8;
  17. private const int REDUCTION_POLYNOMIAL = 0x011d; // 2^8 + 2^4 + 2^3 + 2 + 1;
  18. private const int ROUNDS = 10;
  19. private static readonly int[] SBOX =
  20. {
  21. 0x18, 0x23, 0xc6, 0xe8, 0x87, 0xb8, 0x01, 0x4f, 0x36, 0xa6, 0xd2, 0xf5, 0x79, 0x6f, 0x91, 0x52,
  22. 0x60, 0xbc, 0x9b, 0x8e, 0xa3, 0x0c, 0x7b, 0x35, 0x1d, 0xe0, 0xd7, 0xc2, 0x2e, 0x4b, 0xfe, 0x57,
  23. 0x15, 0x77, 0x37, 0xe5, 0x9f, 0xf0, 0x4a, 0xda, 0x58, 0xc9, 0x29, 0x0a, 0xb1, 0xa0, 0x6b, 0x85,
  24. 0xbd, 0x5d, 0x10, 0xf4, 0xcb, 0x3e, 0x05, 0x67, 0xe4, 0x27, 0x41, 0x8b, 0xa7, 0x7d, 0x95, 0xd8,
  25. 0xfb, 0xee, 0x7c, 0x66, 0xdd, 0x17, 0x47, 0x9e, 0xca, 0x2d, 0xbf, 0x07, 0xad, 0x5a, 0x83, 0x33,
  26. 0x63, 0x02, 0xaa, 0x71, 0xc8, 0x19, 0x49, 0xd9, 0xf2, 0xe3, 0x5b, 0x88, 0x9a, 0x26, 0x32, 0xb0,
  27. 0xe9, 0x0f, 0xd5, 0x80, 0xbe, 0xcd, 0x34, 0x48, 0xff, 0x7a, 0x90, 0x5f, 0x20, 0x68, 0x1a, 0xae,
  28. 0xb4, 0x54, 0x93, 0x22, 0x64, 0xf1, 0x73, 0x12, 0x40, 0x08, 0xc3, 0xec, 0xdb, 0xa1, 0x8d, 0x3d,
  29. 0x97, 0x00, 0xcf, 0x2b, 0x76, 0x82, 0xd6, 0x1b, 0xb5, 0xaf, 0x6a, 0x50, 0x45, 0xf3, 0x30, 0xef,
  30. 0x3f, 0x55, 0xa2, 0xea, 0x65, 0xba, 0x2f, 0xc0, 0xde, 0x1c, 0xfd, 0x4d, 0x92, 0x75, 0x06, 0x8a,
  31. 0xb2, 0xe6, 0x0e, 0x1f, 0x62, 0xd4, 0xa8, 0x96, 0xf9, 0xc5, 0x25, 0x59, 0x84, 0x72, 0x39, 0x4c,
  32. 0x5e, 0x78, 0x38, 0x8c, 0xd1, 0xa5, 0xe2, 0x61, 0xb3, 0x21, 0x9c, 0x1e, 0x43, 0xc7, 0xfc, 0x04,
  33. 0x51, 0x99, 0x6d, 0x0d, 0xfa, 0xdf, 0x7e, 0x24, 0x3b, 0xab, 0xce, 0x11, 0x8f, 0x4e, 0xb7, 0xeb,
  34. 0x3c, 0x81, 0x94, 0xf7, 0xb9, 0x13, 0x2c, 0xd3, 0xe7, 0x6e, 0xc4, 0x03, 0x56, 0x44, 0x7f, 0xa9,
  35. 0x2a, 0xbb, 0xc1, 0x53, 0xdc, 0x0b, 0x9d, 0x6c, 0x31, 0x74, 0xf6, 0x46, 0xac, 0x89, 0x14, 0xe1,
  36. 0x16, 0x3a, 0x69, 0x09, 0x70, 0xb6, 0xd0, 0xed, 0xcc, 0x42, 0x98, 0xa4, 0x28, 0x5c, 0xf8, 0x86
  37. };
  38. private static readonly ulong[] C0 = new ulong[256];
  39. private static readonly ulong[] C1 = new ulong[256];
  40. private static readonly ulong[] C2 = new ulong[256];
  41. private static readonly ulong[] C3 = new ulong[256];
  42. private static readonly ulong[] C4 = new ulong[256];
  43. private static readonly ulong[] C5 = new ulong[256];
  44. private static readonly ulong[] C6 = new ulong[256];
  45. private static readonly ulong[] C7 = new ulong[256];
  46. /*
  47. * increment() can be implemented in this way using 2 arrays or
  48. * by having some temporary variables that are used to set the
  49. * value provided by EIGHT[i] and carry within the loop.
  50. *
  51. * not having done any timing, this seems likely to be faster
  52. * at the slight expense of 32*(sizeof short) bytes
  53. */
  54. private static readonly short[] EIGHT = new short[BITCOUNT_ARRAY_SIZE];
  55. static WhirlpoolDigest()
  56. {
  57. EIGHT[BITCOUNT_ARRAY_SIZE - 1] = 8;
  58. for (int i = 0; i < 256; i++)
  59. {
  60. int v1 = SBOX[i];
  61. int v2 = MulX(v1);
  62. int v4 = MulX(v2);
  63. int v5 = v4 ^ v1;
  64. int v8 = MulX(v4);
  65. int v9 = v8 ^ v1;
  66. C0[i] = PackIntoUInt64(v1, v1, v4, v1, v8, v5, v2, v9);
  67. C1[i] = PackIntoUInt64(v9, v1, v1, v4, v1, v8, v5, v2);
  68. C2[i] = PackIntoUInt64(v2, v9, v1, v1, v4, v1, v8, v5);
  69. C3[i] = PackIntoUInt64(v5, v2, v9, v1, v1, v4, v1, v8);
  70. C4[i] = PackIntoUInt64(v8, v5, v2, v9, v1, v1, v4, v1);
  71. C5[i] = PackIntoUInt64(v1, v8, v5, v2, v9, v1, v1, v4);
  72. C6[i] = PackIntoUInt64(v4, v1, v8, v5, v2, v9, v1, v1);
  73. C7[i] = PackIntoUInt64(v1, v4, v1, v8, v5, v2, v9, v1);
  74. }
  75. }
  76. // int's are used to prevent sign extension. The values that are really being used are actually just 0..255
  77. private static int MulX(int input)
  78. {
  79. return (input << 1) ^ (-(input >> 7) & REDUCTION_POLYNOMIAL);
  80. }
  81. private static ulong PackIntoUInt64(int b7, int b6, int b5, int b4, int b3, int b2, int b1, int b0)
  82. {
  83. return ((ulong)b7 << 56) ^
  84. ((ulong)b6 << 48) ^
  85. ((ulong)b5 << 40) ^
  86. ((ulong)b4 << 32) ^
  87. ((ulong)b3 << 24) ^
  88. ((ulong)b2 << 16) ^
  89. ((ulong)b1 << 8) ^
  90. (ulong)b0;
  91. }
  92. private readonly ulong[] _rc = new ulong[ROUNDS + 1];
  93. public WhirlpoolDigest()
  94. {
  95. _rc[0] = 0UL;
  96. for (int r = 1; r <= ROUNDS; r++)
  97. {
  98. int i = 8 * (r - 1);
  99. _rc[r] =
  100. (C0[i ] & 0xff00000000000000UL) ^
  101. (C1[i + 1] & 0x00ff000000000000UL) ^
  102. (C2[i + 2] & 0x0000ff0000000000UL) ^
  103. (C3[i + 3] & 0x000000ff00000000UL) ^
  104. (C4[i + 4] & 0x00000000ff000000UL) ^
  105. (C5[i + 5] & 0x0000000000ff0000UL) ^
  106. (C6[i + 6] & 0x000000000000ff00UL) ^
  107. (C7[i + 7] & 0x00000000000000ffUL);
  108. }
  109. }
  110. // --------------------------------------------------------------------------------------//
  111. // -- buffer information --
  112. private byte[] _buffer = new byte[64];
  113. private int _bufferPos;
  114. private short[] _bitCount = new short[BITCOUNT_ARRAY_SIZE];
  115. // -- internal hash state --
  116. private ulong[] _hash = new ulong[8];
  117. private ulong[] _K = new ulong[8]; // the round key
  118. private ulong[] _L = new ulong[8];
  119. private ulong[] _block = new ulong[8]; // mu (buffer)
  120. private ulong[] _state = new ulong[8]; // the current "cipher" state
  121. /**
  122. * Copy constructor. This will copy the state of the provided message digest.
  123. */
  124. public WhirlpoolDigest(WhirlpoolDigest originalDigest)
  125. {
  126. Reset(originalDigest);
  127. }
  128. public string AlgorithmName
  129. {
  130. get { return "Whirlpool"; }
  131. }
  132. public int GetDigestSize()
  133. {
  134. return DIGEST_LENGTH_BYTES;
  135. }
  136. public int DoFinal(byte[] output, int outOff)
  137. {
  138. // sets output[outOff] .. output[outOff+DIGEST_LENGTH_BYTES]
  139. Finish();
  140. Pack.UInt64_To_BE(_hash, output, outOff);
  141. Reset();
  142. return GetDigestSize();
  143. }
  144. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  145. public int DoFinal(Span<byte> output)
  146. {
  147. // sets output[0..DIGEST_LENGTH_BYTES]
  148. Finish();
  149. Pack.UInt64_To_BE(_hash, output);
  150. Reset();
  151. return GetDigestSize();
  152. }
  153. #endif
  154. /**
  155. * Reset the chaining variables
  156. */
  157. public void Reset()
  158. {
  159. // set variables to null, blank, whatever
  160. _bufferPos = 0;
  161. Array.Clear(_bitCount, 0, _bitCount.Length);
  162. Array.Clear(_buffer, 0, _buffer.Length);
  163. Array.Clear(_hash, 0, _hash.Length);
  164. Array.Clear(_K, 0, _K.Length);
  165. Array.Clear(_L, 0, _L.Length);
  166. Array.Clear(_block, 0, _block.Length);
  167. Array.Clear(_state, 0, _state.Length);
  168. }
  169. // this takes a buffer of information and fills the block
  170. private void ProcessFilledBuffer()
  171. {
  172. // copies into the block...
  173. Pack.BE_To_UInt64(_buffer, 0, _block);
  174. ProcessBlock();
  175. _bufferPos = 0;
  176. Array.Clear(_buffer, 0, _buffer.Length);
  177. }
  178. private void ProcessBlock()
  179. {
  180. // buffer contents have been transferred to the _block[] array via ProcessFilledBuffer
  181. // compute and apply K^0
  182. for (int i = 0; i < 8; i++)
  183. {
  184. _state[i] = _block[i] ^ (_K[i] = _hash[i]);
  185. }
  186. // iterate over the rounds
  187. for (int round = 1; round <= ROUNDS; round++)
  188. {
  189. for (int i = 0; i < 8; i++)
  190. {
  191. _L[i] = C0[(int)(_K[(i - 0) & 7] >> 56) & 0xff];
  192. _L[i] ^= C1[(int)(_K[(i - 1) & 7] >> 48) & 0xff];
  193. _L[i] ^= C2[(int)(_K[(i - 2) & 7] >> 40) & 0xff];
  194. _L[i] ^= C3[(int)(_K[(i - 3) & 7] >> 32) & 0xff];
  195. _L[i] ^= C4[(int)(_K[(i - 4) & 7] >> 24) & 0xff];
  196. _L[i] ^= C5[(int)(_K[(i - 5) & 7] >> 16) & 0xff];
  197. _L[i] ^= C6[(int)(_K[(i - 6) & 7] >> 8) & 0xff];
  198. _L[i] ^= C7[(int)(_K[(i - 7) & 7]) & 0xff];
  199. }
  200. Array.Copy(_L, 0, _K, 0, _K.Length);
  201. _K[0] ^= _rc[round];
  202. // apply the round transformation
  203. for (int i = 0; i < 8; i++)
  204. {
  205. _L[i] = _K[i];
  206. _L[i] ^= C0[(int)(_state[(i - 0) & 7] >> 56) & 0xff];
  207. _L[i] ^= C1[(int)(_state[(i - 1) & 7] >> 48) & 0xff];
  208. _L[i] ^= C2[(int)(_state[(i - 2) & 7] >> 40) & 0xff];
  209. _L[i] ^= C3[(int)(_state[(i - 3) & 7] >> 32) & 0xff];
  210. _L[i] ^= C4[(int)(_state[(i - 4) & 7] >> 24) & 0xff];
  211. _L[i] ^= C5[(int)(_state[(i - 5) & 7] >> 16) & 0xff];
  212. _L[i] ^= C6[(int)(_state[(i - 6) & 7] >> 8) & 0xff];
  213. _L[i] ^= C7[(int)(_state[(i - 7) & 7]) & 0xff];
  214. }
  215. // save the current state
  216. Array.Copy(_L, 0, _state, 0, _state.Length);
  217. }
  218. // apply Miuaguchi-Preneel compression
  219. for (int i = 0; i < 8; i++)
  220. {
  221. _hash[i] ^= _state[i] ^ _block[i];
  222. }
  223. }
  224. public void Update(byte input)
  225. {
  226. _buffer[_bufferPos] = input;
  227. if (++_bufferPos == _buffer.Length)
  228. {
  229. ProcessFilledBuffer();
  230. }
  231. Increment();
  232. }
  233. private void Increment()
  234. {
  235. int carry = 0;
  236. for (int i = _bitCount.Length - 1; i >= 0; i--)
  237. {
  238. int sum = (_bitCount[i] & 0xff) + EIGHT[i] + carry;
  239. carry = sum >> 8;
  240. _bitCount[i] = (short)(sum & 0xff);
  241. }
  242. }
  243. public void BlockUpdate(byte[] input, int inOff, int length)
  244. {
  245. while (length > 0)
  246. {
  247. Update(input[inOff]);
  248. ++inOff;
  249. --length;
  250. }
  251. }
  252. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  253. public void BlockUpdate(ReadOnlySpan<byte> input)
  254. {
  255. for (int i = 0; i < input.Length; ++i)
  256. {
  257. Update(input[i]);
  258. }
  259. }
  260. #endif
  261. private void Finish()
  262. {
  263. /*
  264. * this makes a copy of the current bit length. at the expense of an
  265. * object creation of 32 bytes rather than providing a _stopCounting
  266. * boolean which was the alternative I could think of.
  267. */
  268. byte[] bitLength = CopyBitLength();
  269. _buffer[_bufferPos] |= 0x80;
  270. if (++_bufferPos == _buffer.Length)
  271. {
  272. ProcessFilledBuffer();
  273. }
  274. /*
  275. * Final block contains
  276. * [ ... data .... ][0][0][0][ length ]
  277. *
  278. * if [ length ] cannot fit. Need to create a new block.
  279. */
  280. if (_bufferPos > 32)
  281. {
  282. while (_bufferPos != 0)
  283. {
  284. Update((byte)0);
  285. }
  286. }
  287. while (_bufferPos <= 32)
  288. {
  289. Update((byte)0);
  290. }
  291. // copy the length information to the final 32 bytes of the 64 byte block....
  292. Array.Copy(bitLength, 0, _buffer, 32, bitLength.Length);
  293. ProcessFilledBuffer();
  294. }
  295. private byte[] CopyBitLength()
  296. {
  297. byte[] rv = new byte[BITCOUNT_ARRAY_SIZE];
  298. for (int i = 0; i < rv.Length; i++)
  299. {
  300. rv[i] = (byte)(_bitCount[i] & 0xff);
  301. }
  302. return rv;
  303. }
  304. public int GetByteLength()
  305. {
  306. return BYTE_LENGTH;
  307. }
  308. public IMemoable Copy()
  309. {
  310. return new WhirlpoolDigest(this);
  311. }
  312. public void Reset(IMemoable other)
  313. {
  314. WhirlpoolDigest originalDigest = (WhirlpoolDigest)other;
  315. Array.Copy(originalDigest._rc, 0, _rc, 0, _rc.Length);
  316. Array.Copy(originalDigest._buffer, 0, _buffer, 0, _buffer.Length);
  317. this._bufferPos = originalDigest._bufferPos;
  318. Array.Copy(originalDigest._bitCount, 0, _bitCount, 0, _bitCount.Length);
  319. // -- internal hash state --
  320. Array.Copy(originalDigest._hash, 0, _hash, 0, _hash.Length);
  321. Array.Copy(originalDigest._K, 0, _K, 0, _K.Length);
  322. Array.Copy(originalDigest._L, 0, _L, 0, _L.Length);
  323. Array.Copy(originalDigest._block, 0, _block, 0, _block.Length);
  324. Array.Copy(originalDigest._state, 0, _state, 0, _state.Length);
  325. }
  326. }
  327. }
  328. #pragma warning restore
  329. #endif