WhirlpoolDigest.cs 12 KB

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