WhirlpoolDigest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Crypto.Digests
  6. {
  7. /**
  8. * Implementation of WhirlpoolDigest, based on Java source published by Barreto
  9. * and Rijmen.
  10. *
  11. */
  12. public sealed class WhirlpoolDigest
  13. : IDigest, IMemoable
  14. {
  15. private const int BYTE_LENGTH = 64;
  16. private const int DIGEST_LENGTH_BYTES = 512 / 8;
  17. private const int ROUNDS = 10;
  18. private const int REDUCTION_POLYNOMIAL = 0x011d; // 2^8 + 2^4 + 2^3 + 2 + 1;
  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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6e, 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 long[] C0 = new long[256];
  39. private static readonly long[] C1 = new long[256];
  40. private static readonly long[] C2 = new long[256];
  41. private static readonly long[] C3 = new long[256];
  42. private static readonly long[] C4 = new long[256];
  43. private static readonly long[] C5 = new long[256];
  44. private static readonly long[] C6 = new long[256];
  45. private static readonly long[] C7 = new long[256];
  46. private readonly long[] _rc = new long[ROUNDS + 1];
  47. /*
  48. * increment() can be implemented in this way using 2 arrays or
  49. * by having some temporary variables that are used to set the
  50. * value provided by EIGHT[i] and carry within the loop.
  51. *
  52. * not having done any timing, this seems likely to be faster
  53. * at the slight expense of 32*(sizeof short) bytes
  54. */
  55. private static readonly short[] EIGHT = new short[BITCOUNT_ARRAY_SIZE];
  56. static WhirlpoolDigest()
  57. {
  58. EIGHT[BITCOUNT_ARRAY_SIZE - 1] = 8;
  59. for (int i = 0; i < 256; i++)
  60. {
  61. int v1 = SBOX[i];
  62. int v2 = maskWithReductionPolynomial(v1 << 1);
  63. int v4 = maskWithReductionPolynomial(v2 << 1);
  64. int v5 = v4 ^ v1;
  65. int v8 = maskWithReductionPolynomial(v4 << 1);
  66. int v9 = v8 ^ v1;
  67. C0[i] = packIntoLong(v1, v1, v4, v1, v8, v5, v2, v9);
  68. C1[i] = packIntoLong(v9, v1, v1, v4, v1, v8, v5, v2);
  69. C2[i] = packIntoLong(v2, v9, v1, v1, v4, v1, v8, v5);
  70. C3[i] = packIntoLong(v5, v2, v9, v1, v1, v4, v1, v8);
  71. C4[i] = packIntoLong(v8, v5, v2, v9, v1, v1, v4, v1);
  72. C5[i] = packIntoLong(v1, v8, v5, v2, v9, v1, v1, v4);
  73. C6[i] = packIntoLong(v4, v1, v8, v5, v2, v9, v1, v1);
  74. C7[i] = packIntoLong(v1, v4, v1, v8, v5, v2, v9, v1);
  75. }
  76. }
  77. public WhirlpoolDigest()
  78. {
  79. _rc[0] = 0L;
  80. for (int r = 1; r <= ROUNDS; r++)
  81. {
  82. int i = 8 * (r - 1);
  83. _rc[r] = (long)((ulong)C0[i] & 0xff00000000000000L) ^
  84. (C1[i + 1] & (long) 0x00ff000000000000L) ^
  85. (C2[i + 2] & (long) 0x0000ff0000000000L) ^
  86. (C3[i + 3] & (long) 0x000000ff00000000L) ^
  87. (C4[i + 4] & (long) 0x00000000ff000000L) ^
  88. (C5[i + 5] & (long) 0x0000000000ff0000L) ^
  89. (C6[i + 6] & (long) 0x000000000000ff00L) ^
  90. (C7[i + 7] & (long) 0x00000000000000ffL);
  91. }
  92. }
  93. private static long packIntoLong(int b7, int b6, int b5, int b4, int b3, int b2, int b1, int b0)
  94. {
  95. return
  96. ((long)b7 << 56) ^
  97. ((long)b6 << 48) ^
  98. ((long)b5 << 40) ^
  99. ((long)b4 << 32) ^
  100. ((long)b3 << 24) ^
  101. ((long)b2 << 16) ^
  102. ((long)b1 << 8) ^
  103. b0;
  104. }
  105. /*
  106. * int's are used to prevent sign extension. The values that are really being used are
  107. * actually just 0..255
  108. */
  109. private static int maskWithReductionPolynomial(int input)
  110. {
  111. int rv = input;
  112. if (rv >= 0x100L) // high bit set
  113. {
  114. rv ^= REDUCTION_POLYNOMIAL; // reduced by the polynomial
  115. }
  116. return rv;
  117. }
  118. // --------------------------------------------------------------------------------------//
  119. // -- buffer information --
  120. private const int BITCOUNT_ARRAY_SIZE = 32;
  121. private byte[] _buffer = new byte[64];
  122. private int _bufferPos;
  123. private short[] _bitCount = new short[BITCOUNT_ARRAY_SIZE];
  124. // -- internal hash state --
  125. private long[] _hash = new long[8];
  126. private long[] _K = new long[8]; // the round key
  127. private long[] _L = new long[8];
  128. private long[] _block = new long[8]; // mu (buffer)
  129. private long[] _state = new long[8]; // the current "cipher" state
  130. /**
  131. * Copy constructor. This will copy the state of the provided message
  132. * digest.
  133. */
  134. public WhirlpoolDigest(WhirlpoolDigest originalDigest)
  135. {
  136. Reset(originalDigest);
  137. }
  138. public string AlgorithmName
  139. {
  140. get { return "Whirlpool"; }
  141. }
  142. public int GetDigestSize()
  143. {
  144. return DIGEST_LENGTH_BYTES;
  145. }
  146. public int DoFinal(byte[] output, int outOff)
  147. {
  148. // sets output[outOff] .. output[outOff+DIGEST_LENGTH_BYTES]
  149. finish();
  150. for (int i = 0; i < 8; i++)
  151. {
  152. convertLongToByteArray(_hash[i], output, outOff + (i * 8));
  153. }
  154. Reset();
  155. return GetDigestSize();
  156. }
  157. /**
  158. * Reset the chaining variables
  159. */
  160. public void Reset()
  161. {
  162. // set variables to null, blank, whatever
  163. _bufferPos = 0;
  164. Array.Clear(_bitCount, 0, _bitCount.Length);
  165. Array.Clear(_buffer, 0, _buffer.Length);
  166. Array.Clear(_hash, 0, _hash.Length);
  167. Array.Clear(_K, 0, _K.Length);
  168. Array.Clear(_L, 0, _L.Length);
  169. Array.Clear(_block, 0, _block.Length);
  170. Array.Clear(_state, 0, _state.Length);
  171. }
  172. // this takes a buffer of information and fills the block
  173. private void processFilledBuffer()
  174. {
  175. // copies into the block...
  176. for (int i = 0; i < _state.Length; i++)
  177. {
  178. _block[i] = bytesToLongFromBuffer(_buffer, i * 8);
  179. }
  180. processBlock();
  181. _bufferPos = 0;
  182. Array.Clear(_buffer, 0, _buffer.Length);
  183. }
  184. private static long bytesToLongFromBuffer(byte[] buffer, int startPos)
  185. {
  186. long rv = (((buffer[startPos + 0] & 0xffL) << 56) |
  187. ((buffer[startPos + 1] & 0xffL) << 48) |
  188. ((buffer[startPos + 2] & 0xffL) << 40) |
  189. ((buffer[startPos + 3] & 0xffL) << 32) |
  190. ((buffer[startPos + 4] & 0xffL) << 24) |
  191. ((buffer[startPos + 5] & 0xffL) << 16) |
  192. ((buffer[startPos + 6] & 0xffL) << 8) |
  193. ((buffer[startPos + 7]) & 0xffL));
  194. return rv;
  195. }
  196. private static void convertLongToByteArray(long inputLong, byte[] outputArray, int offSet)
  197. {
  198. for (int i = 0; i < 8; i++)
  199. {
  200. outputArray[offSet + i] = (byte)((inputLong >> (56 - (i * 8))) & 0xff);
  201. }
  202. }
  203. private void processBlock()
  204. {
  205. // buffer contents have been transferred to the _block[] array via
  206. // processFilledBuffer
  207. // compute and apply K^0
  208. for (int i = 0; i < 8; i++)
  209. {
  210. _state[i] = _block[i] ^ (_K[i] = _hash[i]);
  211. }
  212. // iterate over the rounds
  213. for (int round = 1; round <= ROUNDS; round++)
  214. {
  215. for (int i = 0; i < 8; i++)
  216. {
  217. _L[i] = 0;
  218. _L[i] ^= C0[(int)(_K[(i - 0) & 7] >> 56) & 0xff];
  219. _L[i] ^= C1[(int)(_K[(i - 1) & 7] >> 48) & 0xff];
  220. _L[i] ^= C2[(int)(_K[(i - 2) & 7] >> 40) & 0xff];
  221. _L[i] ^= C3[(int)(_K[(i - 3) & 7] >> 32) & 0xff];
  222. _L[i] ^= C4[(int)(_K[(i - 4) & 7] >> 24) & 0xff];
  223. _L[i] ^= C5[(int)(_K[(i - 5) & 7] >> 16) & 0xff];
  224. _L[i] ^= C6[(int)(_K[(i - 6) & 7] >> 8) & 0xff];
  225. _L[i] ^= C7[(int)(_K[(i - 7) & 7]) & 0xff];
  226. }
  227. Array.Copy(_L, 0, _K, 0, _K.Length);
  228. _K[0] ^= _rc[round];
  229. // apply the round transformation
  230. for (int i = 0; i < 8; i++)
  231. {
  232. _L[i] = _K[i];
  233. _L[i] ^= C0[(int)(_state[(i - 0) & 7] >> 56) & 0xff];
  234. _L[i] ^= C1[(int)(_state[(i - 1) & 7] >> 48) & 0xff];
  235. _L[i] ^= C2[(int)(_state[(i - 2) & 7] >> 40) & 0xff];
  236. _L[i] ^= C3[(int)(_state[(i - 3) & 7] >> 32) & 0xff];
  237. _L[i] ^= C4[(int)(_state[(i - 4) & 7] >> 24) & 0xff];
  238. _L[i] ^= C5[(int)(_state[(i - 5) & 7] >> 16) & 0xff];
  239. _L[i] ^= C6[(int)(_state[(i - 6) & 7] >> 8) & 0xff];
  240. _L[i] ^= C7[(int)(_state[(i - 7) & 7]) & 0xff];
  241. }
  242. // save the current state
  243. Array.Copy(_L, 0, _state, 0, _state.Length);
  244. }
  245. // apply Miuaguchi-Preneel compression
  246. for (int i = 0; i < 8; i++)
  247. {
  248. _hash[i] ^= _state[i] ^ _block[i];
  249. }
  250. }
  251. public void Update(byte input)
  252. {
  253. _buffer[_bufferPos] = input;
  254. //Console.WriteLine("adding to buffer = "+_buffer[_bufferPos]);
  255. ++_bufferPos;
  256. if (_bufferPos == _buffer.Length)
  257. {
  258. processFilledBuffer();
  259. }
  260. increment();
  261. }
  262. private void increment()
  263. {
  264. int carry = 0;
  265. for (int i = _bitCount.Length - 1; i >= 0; i--)
  266. {
  267. int sum = (_bitCount[i] & 0xff) + EIGHT[i] + carry;
  268. carry = sum >> 8;
  269. _bitCount[i] = (short)(sum & 0xff);
  270. }
  271. }
  272. public void BlockUpdate(byte[] input, int inOff, int length)
  273. {
  274. while (length > 0)
  275. {
  276. Update(input[inOff]);
  277. ++inOff;
  278. --length;
  279. }
  280. }
  281. private void finish()
  282. {
  283. /*
  284. * this makes a copy of the current bit length. at the expense of an
  285. * object creation of 32 bytes rather than providing a _stopCounting
  286. * boolean which was the alternative I could think of.
  287. */
  288. byte[] bitLength = copyBitLength();
  289. _buffer[_bufferPos++] |= 0x80;
  290. if (_bufferPos == _buffer.Length)
  291. {
  292. processFilledBuffer();
  293. }
  294. /*
  295. * Final block contains
  296. * [ ... data .... ][0][0][0][ length ]
  297. *
  298. * if [ length ] cannot fit. Need to create a new block.
  299. */
  300. if (_bufferPos > 32)
  301. {
  302. while (_bufferPos != 0)
  303. {
  304. Update((byte)0);
  305. }
  306. }
  307. while (_bufferPos <= 32)
  308. {
  309. Update((byte)0);
  310. }
  311. // copy the length information to the final 32 bytes of the
  312. // 64 byte block....
  313. Array.Copy(bitLength, 0, _buffer, 32, bitLength.Length);
  314. processFilledBuffer();
  315. }
  316. private byte[] copyBitLength()
  317. {
  318. byte[] rv = new byte[BITCOUNT_ARRAY_SIZE];
  319. for (int i = 0; i < rv.Length; i++)
  320. {
  321. rv[i] = (byte)(_bitCount[i] & 0xff);
  322. }
  323. return rv;
  324. }
  325. public int GetByteLength()
  326. {
  327. return BYTE_LENGTH;
  328. }
  329. public IMemoable Copy()
  330. {
  331. return new WhirlpoolDigest(this);
  332. }
  333. public void Reset(IMemoable other)
  334. {
  335. WhirlpoolDigest originalDigest = (WhirlpoolDigest)other;
  336. Array.Copy(originalDigest._rc, 0, _rc, 0, _rc.Length);
  337. Array.Copy(originalDigest._buffer, 0, _buffer, 0, _buffer.Length);
  338. this._bufferPos = originalDigest._bufferPos;
  339. Array.Copy(originalDigest._bitCount, 0, _bitCount, 0, _bitCount.Length);
  340. // -- internal hash state --
  341. Array.Copy(originalDigest._hash, 0, _hash, 0, _hash.Length);
  342. Array.Copy(originalDigest._K, 0, _K, 0, _K.Length);
  343. Array.Copy(originalDigest._L, 0, _L, 0, _L.Length);
  344. Array.Copy(originalDigest._block, 0, _block, 0, _block.Length);
  345. Array.Copy(originalDigest._state, 0, _state, 0, _state.Length);
  346. }
  347. }
  348. }
  349. #endif