Blake2bDigest.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. #if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER || UNITY_2021_2_OR_NEWER
  5. using System.Runtime.CompilerServices;
  6. #endif
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  10. {
  11. /* The BLAKE2 cryptographic hash function was designed by Jean-
  12. Philippe Aumasson, Samuel Neves, Zooko Wilcox-O'Hearn, and Christian
  13. Winnerlein.
  14. Reference Implementation and Description can be found at: https://blake2.net/
  15. Internet Draft: https://tools.ietf.org/html/draft-saarinen-blake2-02
  16. This implementation does not support the Tree Hashing Mode.
  17. For unkeyed hashing, developers adapting BLAKE2 to ASN.1 - based
  18. message formats SHOULD use the OID tree at x = 1.3.6.1.4.1.1722.12.2.
  19. Algorithm | Target | Collision | Hash | Hash ASN.1 |
  20. Identifier | Arch | Security | nn | OID Suffix |
  21. ---------------+--------+-----------+------+------------+
  22. id-blake2b160 | 64-bit | 2**80 | 20 | x.1.20 |
  23. id-blake2b256 | 64-bit | 2**128 | 32 | x.1.32 |
  24. id-blake2b384 | 64-bit | 2**192 | 48 | x.1.48 |
  25. id-blake2b512 | 64-bit | 2**256 | 64 | x.1.64 |
  26. ---------------+--------+-----------+------+------------+
  27. */
  28. /**
  29. * Implementation of the cryptographic hash function Blake2b.
  30. * <p/>
  31. * Blake2b offers a built-in keying mechanism to be used directly
  32. * for authentication ("Prefix-MAC") rather than a HMAC construction.
  33. * <p/>
  34. * Blake2b offers a built-in support for a salt for randomized hashing
  35. * and a personal string for defining a unique hash function for each application.
  36. * <p/>
  37. * BLAKE2b is optimized for 64-bit platforms and produces digests of any size
  38. * between 1 and 64 bytes.
  39. */
  40. public sealed class Blake2bDigest
  41. : IDigest
  42. {
  43. // Blake2b Initialization Vector:
  44. private static readonly ulong[] blake2b_IV =
  45. // Produced from the square root of primes 2, 3, 5, 7, 11, 13, 17, 19.
  46. // The same as SHA-512 IV.
  47. {
  48. 0x6a09e667f3bcc908UL, 0xbb67ae8584caa73bUL, 0x3c6ef372fe94f82bUL,
  49. 0xa54ff53a5f1d36f1UL, 0x510e527fade682d1UL, 0x9b05688c2b3e6c1fUL,
  50. 0x1f83d9abfb41bd6bUL, 0x5be0cd19137e2179UL
  51. };
  52. // Message word permutations:
  53. private static readonly byte[,] blake2b_sigma =
  54. {
  55. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
  56. { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
  57. { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
  58. { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
  59. { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
  60. { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
  61. { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
  62. { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
  63. { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
  64. { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
  65. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
  66. { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
  67. };
  68. private const int ROUNDS = 12; // to use for Catenas H'
  69. private const int BLOCK_LENGTH_BYTES = 128;// bytes
  70. // General parameters:
  71. private int digestLength = 64; // 1- 64 bytes
  72. private int keyLength = 0; // 0 - 64 bytes for keyed hashing for MAC
  73. private byte[] salt = null;// new byte[16];
  74. private byte[] personalization = null;// new byte[16];
  75. // the key
  76. private byte[] key = null;
  77. // Tree hashing parameters:
  78. // Because this class does not implement the Tree Hashing Mode,
  79. // these parameters can be treated as constants (see init() function)
  80. /*
  81. * private int fanout = 1; // 0-255 private int depth = 1; // 1 - 255
  82. * private int leafLength= 0; private long nodeOffset = 0L; private int
  83. * nodeDepth = 0; private int innerHashLength = 0;
  84. */
  85. // whenever this buffer overflows, it will be processed
  86. // in the Compress() function.
  87. // For performance issues, long messages will not use this buffer.
  88. private byte[] buffer = null;// new byte[BLOCK_LENGTH_BYTES];
  89. // Position of last inserted byte:
  90. private int bufferPos = 0;// a value from 0 up to 128
  91. private ulong[] internalState = new ulong[16]; // In the Blake2b paper it is
  92. // called: v
  93. private ulong[] chainValue = null; // state vector, in the Blake2b paper it
  94. // is called: h
  95. private ulong t0 = 0UL; // holds last significant bits, counter (counts bytes)
  96. private ulong t1 = 0UL; // counter: Length up to 2^128 are supported
  97. private ulong f0 = 0UL; // finalization flag, for last block: ~0L
  98. // For Tree Hashing Mode, not used here:
  99. // private long f1 = 0L; // finalization flag, for last node: ~0L
  100. public Blake2bDigest()
  101. : this(512)
  102. {
  103. }
  104. public Blake2bDigest(Blake2bDigest digest)
  105. {
  106. this.bufferPos = digest.bufferPos;
  107. this.buffer = Arrays.Clone(digest.buffer);
  108. this.keyLength = digest.keyLength;
  109. this.key = Arrays.Clone(digest.key);
  110. this.digestLength = digest.digestLength;
  111. this.chainValue = Arrays.Clone(digest.chainValue);
  112. this.personalization = Arrays.Clone(digest.personalization);
  113. this.salt = Arrays.Clone(digest.salt);
  114. this.t0 = digest.t0;
  115. this.t1 = digest.t1;
  116. this.f0 = digest.f0;
  117. }
  118. /**
  119. * Basic sized constructor - size in bits.
  120. *
  121. * @param digestSize size of the digest in bits
  122. */
  123. public Blake2bDigest(int digestSize)
  124. {
  125. if (digestSize < 8 || digestSize > 512 || digestSize % 8 != 0)
  126. throw new ArgumentException("BLAKE2b digest bit length must be a multiple of 8 and not greater than 512");
  127. buffer = new byte[BLOCK_LENGTH_BYTES];
  128. keyLength = 0;
  129. this.digestLength = digestSize / 8;
  130. Init();
  131. }
  132. /**
  133. * Blake2b for authentication ("Prefix-MAC mode").
  134. * After calling the doFinal() method, the key will
  135. * remain to be used for further computations of
  136. * this instance.
  137. * The key can be overwritten using the clearKey() method.
  138. *
  139. * @param key A key up to 64 bytes or null
  140. */
  141. public Blake2bDigest(byte[] key)
  142. {
  143. buffer = new byte[BLOCK_LENGTH_BYTES];
  144. if (key != null)
  145. {
  146. this.key = new byte[key.Length];
  147. Array.Copy(key, 0, this.key, 0, key.Length);
  148. if (key.Length > 64)
  149. throw new ArgumentException("Keys > 64 are not supported");
  150. keyLength = key.Length;
  151. Array.Copy(key, 0, buffer, 0, key.Length);
  152. bufferPos = BLOCK_LENGTH_BYTES; // zero padding
  153. }
  154. digestLength = 64;
  155. Init();
  156. }
  157. /**
  158. * Blake2b with key, required digest length (in bytes), salt and personalization.
  159. * After calling the doFinal() method, the key, the salt and the personal string
  160. * will remain and might be used for further computations with this instance.
  161. * The key can be overwritten using the clearKey() method, the salt (pepper)
  162. * can be overwritten using the clearSalt() method.
  163. *
  164. * @param key A key up to 64 bytes or null
  165. * @param digestLength from 1 up to 64 bytes
  166. * @param salt 16 bytes or null
  167. * @param personalization 16 bytes or null
  168. */
  169. public Blake2bDigest(byte[] key, int digestLength, byte[] salt, byte[] personalization)
  170. {
  171. if (digestLength < 1 || digestLength > 64)
  172. throw new ArgumentException("Invalid digest length (required: 1 - 64)");
  173. this.digestLength = digestLength;
  174. this.buffer = new byte[BLOCK_LENGTH_BYTES];
  175. if (salt != null)
  176. {
  177. if (salt.Length != 16)
  178. throw new ArgumentException("salt length must be exactly 16 bytes");
  179. this.salt = new byte[16];
  180. Array.Copy(salt, 0, this.salt, 0, salt.Length);
  181. }
  182. if (personalization != null)
  183. {
  184. if (personalization.Length != 16)
  185. throw new ArgumentException("personalization length must be exactly 16 bytes");
  186. this.personalization = new byte[16];
  187. Array.Copy(personalization, 0, this.personalization, 0, personalization.Length);
  188. }
  189. if (key != null)
  190. {
  191. if (key.Length > 64)
  192. throw new ArgumentException("Keys > 64 are not supported");
  193. this.key = new byte[key.Length];
  194. Array.Copy(key, 0, this.key, 0, key.Length);
  195. keyLength = key.Length;
  196. Array.Copy(key, 0, buffer, 0, key.Length);
  197. bufferPos = BLOCK_LENGTH_BYTES; // zero padding
  198. }
  199. Init();
  200. }
  201. // initialize chainValue
  202. private void Init()
  203. {
  204. if (chainValue == null)
  205. {
  206. chainValue = new ulong[8];
  207. chainValue[0] = blake2b_IV[0] ^ (ulong)(digestLength | (keyLength << 8) | 0x1010000);
  208. // 0x1010000 = ((fanout << 16) | (depth << 24) | (leafLength <<
  209. // 32));
  210. // with fanout = 1; depth = 0; leafLength = 0;
  211. chainValue[1] = blake2b_IV[1];// ^ nodeOffset; with nodeOffset = 0;
  212. chainValue[2] = blake2b_IV[2];// ^ ( nodeDepth | (innerHashLength << 8) );
  213. // with nodeDepth = 0; innerHashLength = 0;
  214. chainValue[3] = blake2b_IV[3];
  215. chainValue[4] = blake2b_IV[4];
  216. chainValue[5] = blake2b_IV[5];
  217. if (salt != null)
  218. {
  219. chainValue[4] ^= Pack.LE_To_UInt64(salt, 0);
  220. chainValue[5] ^= Pack.LE_To_UInt64(salt, 8);
  221. }
  222. chainValue[6] = blake2b_IV[6];
  223. chainValue[7] = blake2b_IV[7];
  224. if (personalization != null)
  225. {
  226. chainValue[6] ^= Pack.LE_To_UInt64(personalization, 0);
  227. chainValue[7] ^= Pack.LE_To_UInt64(personalization, 8);
  228. }
  229. }
  230. }
  231. private void InitializeInternalState()
  232. {
  233. // initialize v:
  234. Array.Copy(chainValue, 0, internalState, 0, chainValue.Length);
  235. Array.Copy(blake2b_IV, 0, internalState, chainValue.Length, 4);
  236. internalState[12] = t0 ^ blake2b_IV[4];
  237. internalState[13] = t1 ^ blake2b_IV[5];
  238. internalState[14] = f0 ^ blake2b_IV[6];
  239. internalState[15] = blake2b_IV[7];// ^ f1 with f1 = 0
  240. }
  241. /**
  242. * update the message digest with a single byte.
  243. *
  244. * @param b the input byte to be entered.
  245. */
  246. public void Update(byte b)
  247. {
  248. // process the buffer if full else add to buffer:
  249. int remainingLength = BLOCK_LENGTH_BYTES - bufferPos;
  250. if (remainingLength == 0)
  251. { // full buffer
  252. t0 += BLOCK_LENGTH_BYTES;
  253. if (t0 == 0)
  254. { // if message > 2^64
  255. t1++;
  256. }
  257. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  258. Compress(buffer);
  259. #else
  260. Compress(buffer, 0);
  261. #endif
  262. Array.Clear(buffer, 0, buffer.Length);// clear buffer
  263. buffer[0] = b;
  264. bufferPos = 1;
  265. }
  266. else
  267. {
  268. buffer[bufferPos] = b;
  269. bufferPos++;
  270. }
  271. }
  272. /**
  273. * update the message digest with a block of bytes.
  274. *
  275. * @param message the byte array containing the data.
  276. * @param offset the offset into the byte array where the data starts.
  277. * @param len the length of the data.
  278. */
  279. public void BlockUpdate(byte[] message, int offset, int len)
  280. {
  281. if (message == null || len == 0)
  282. return;
  283. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  284. BlockUpdate(message.AsSpan(offset, len));
  285. #else
  286. int remainingLength = 0; // left bytes of buffer
  287. if (bufferPos != 0)
  288. { // commenced, incomplete buffer
  289. // complete the buffer:
  290. remainingLength = BLOCK_LENGTH_BYTES - bufferPos;
  291. if (remainingLength < len)
  292. { // full buffer + at least 1 byte
  293. Array.Copy(message, offset, buffer, bufferPos, remainingLength);
  294. t0 += BLOCK_LENGTH_BYTES;
  295. if (t0 == 0)
  296. { // if message > 2^64
  297. t1++;
  298. }
  299. Compress(buffer, 0);
  300. bufferPos = 0;
  301. Array.Clear(buffer, 0, buffer.Length);// clear buffer
  302. }
  303. else
  304. {
  305. Array.Copy(message, offset, buffer, bufferPos, len);
  306. bufferPos += len;
  307. return;
  308. }
  309. }
  310. // process blocks except last block (also if last block is full)
  311. int messagePos;
  312. int blockWiseLastPos = offset + len - BLOCK_LENGTH_BYTES;
  313. for (messagePos = offset + remainingLength; messagePos < blockWiseLastPos; messagePos += BLOCK_LENGTH_BYTES)
  314. { // block wise 128 bytes
  315. // without buffer:
  316. t0 += BLOCK_LENGTH_BYTES;
  317. if (t0 == 0)
  318. {
  319. t1++;
  320. }
  321. Compress(message, messagePos);
  322. }
  323. // fill the buffer with left bytes, this might be a full block
  324. Array.Copy(message, messagePos, buffer, 0, offset + len - messagePos);
  325. bufferPos += offset + len - messagePos;
  326. #endif
  327. }
  328. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  329. public void BlockUpdate(ReadOnlySpan<byte> input)
  330. {
  331. if (input.IsEmpty)
  332. return;
  333. int remainingLength = 0; // left bytes of buffer
  334. if (bufferPos != 0)
  335. { // commenced, incomplete buffer
  336. // complete the buffer:
  337. remainingLength = BLOCK_LENGTH_BYTES - bufferPos;
  338. if (remainingLength < input.Length)
  339. { // full buffer + at least 1 byte
  340. input[..remainingLength].CopyTo(buffer.AsSpan(bufferPos));
  341. t0 += BLOCK_LENGTH_BYTES;
  342. if (t0 == 0)
  343. { // if message > 2^64
  344. t1++;
  345. }
  346. Compress(buffer);
  347. bufferPos = 0;
  348. Array.Clear(buffer, 0, buffer.Length);// clear buffer
  349. }
  350. else
  351. {
  352. input.CopyTo(buffer.AsSpan(bufferPos));
  353. bufferPos += input.Length;
  354. return;
  355. }
  356. }
  357. // process blocks except last block (also if last block is full)
  358. int messagePos;
  359. int blockWiseLastPos = input.Length - BLOCK_LENGTH_BYTES;
  360. for (messagePos = remainingLength; messagePos < blockWiseLastPos; messagePos += BLOCK_LENGTH_BYTES)
  361. { // block wise 128 bytes
  362. // without buffer:
  363. t0 += BLOCK_LENGTH_BYTES;
  364. if (t0 == 0)
  365. {
  366. t1++;
  367. }
  368. Compress(input[messagePos..]);
  369. }
  370. // fill the buffer with left bytes, this might be a full block
  371. input[messagePos..].CopyTo(buffer.AsSpan());
  372. bufferPos += input.Length - messagePos;
  373. }
  374. #endif
  375. /**
  376. * close the digest, producing the final digest value. The doFinal
  377. * call leaves the digest reset.
  378. * Key, salt and personal string remain.
  379. *
  380. * @param out the array the digest is to be copied into.
  381. * @param outOffset the offset into the out array the digest is to start at.
  382. */
  383. public int DoFinal(byte[] output, int outOffset)
  384. {
  385. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  386. return DoFinal(output.AsSpan(outOffset));
  387. #else
  388. f0 = 0xFFFFFFFFFFFFFFFFUL;
  389. t0 += (ulong)bufferPos;
  390. if (bufferPos > 0 && t0 == 0)
  391. {
  392. t1++;
  393. }
  394. Compress(buffer, 0);
  395. Array.Clear(buffer, 0, buffer.Length);// Holds eventually the key if input is null
  396. Array.Clear(internalState, 0, internalState.Length);
  397. int full = digestLength >> 3, partial = digestLength & 7;
  398. Pack.UInt64_To_LE(chainValue, 0, full, output, outOffset);
  399. if (partial > 0)
  400. {
  401. byte[] bytes = new byte[8];
  402. Pack.UInt64_To_LE(chainValue[full], bytes, 0);
  403. Array.Copy(bytes, 0, output, outOffset + digestLength - partial, partial);
  404. }
  405. Array.Clear(chainValue, 0, chainValue.Length);
  406. Reset();
  407. return digestLength;
  408. #endif
  409. }
  410. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  411. public int DoFinal(Span<byte> output)
  412. {
  413. f0 = 0xFFFFFFFFFFFFFFFFUL;
  414. t0 += (ulong)bufferPos;
  415. if (bufferPos > 0 && t0 == 0)
  416. {
  417. t1++;
  418. }
  419. Compress(buffer);
  420. Array.Clear(buffer, 0, buffer.Length);// Holds eventually the key if input is null
  421. Array.Clear(internalState, 0, internalState.Length);
  422. int full = digestLength >> 3, partial = digestLength & 7;
  423. Pack.UInt64_To_LE(chainValue.AsSpan(0, full), output);
  424. if (partial > 0)
  425. {
  426. Span<byte> bytes = stackalloc byte[8];
  427. Pack.UInt64_To_LE(chainValue[full], bytes);
  428. bytes[..partial].CopyTo(output[(digestLength - partial)..]);
  429. }
  430. Array.Clear(chainValue, 0, chainValue.Length);
  431. Reset();
  432. return digestLength;
  433. }
  434. #endif
  435. /**
  436. * Reset the digest back to it's initial state.
  437. * The key, the salt and the personal string will
  438. * remain for further computations.
  439. */
  440. public void Reset()
  441. {
  442. bufferPos = 0;
  443. f0 = 0L;
  444. t0 = 0L;
  445. t1 = 0L;
  446. chainValue = null;
  447. Array.Clear(buffer, 0, buffer.Length);
  448. if (key != null)
  449. {
  450. Array.Copy(key, 0, buffer, 0, key.Length);
  451. bufferPos = BLOCK_LENGTH_BYTES; // zero padding
  452. }
  453. Init();
  454. }
  455. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  456. private void Compress(ReadOnlySpan<byte> message)
  457. {
  458. InitializeInternalState();
  459. Span<ulong> m = stackalloc ulong[16];
  460. Pack.LE_To_UInt64(message, m);
  461. for (int round = 0; round < ROUNDS; round++)
  462. {
  463. // G apply to columns of internalState:m[blake2b_sigma[round][2 * blockPos]] /+1
  464. G(m[blake2b_sigma[round, 0]], m[blake2b_sigma[round, 1]], 0, 4, 8, 12);
  465. G(m[blake2b_sigma[round, 2]], m[blake2b_sigma[round, 3]], 1, 5, 9, 13);
  466. G(m[blake2b_sigma[round, 4]], m[blake2b_sigma[round, 5]], 2, 6, 10, 14);
  467. G(m[blake2b_sigma[round, 6]], m[blake2b_sigma[round, 7]], 3, 7, 11, 15);
  468. // G apply to diagonals of internalState:
  469. G(m[blake2b_sigma[round, 8]], m[blake2b_sigma[round, 9]], 0, 5, 10, 15);
  470. G(m[blake2b_sigma[round, 10]], m[blake2b_sigma[round, 11]], 1, 6, 11, 12);
  471. G(m[blake2b_sigma[round, 12]], m[blake2b_sigma[round, 13]], 2, 7, 8, 13);
  472. G(m[blake2b_sigma[round, 14]], m[blake2b_sigma[round, 15]], 3, 4, 9, 14);
  473. }
  474. // update chain values:
  475. for (int offset = 0; offset < chainValue.Length; offset++)
  476. {
  477. chainValue[offset] = chainValue[offset] ^ internalState[offset] ^ internalState[offset + 8];
  478. }
  479. }
  480. #else
  481. private void Compress(byte[] message, int messagePos)
  482. {
  483. InitializeInternalState();
  484. ulong[] m = new ulong[16];
  485. Pack.LE_To_UInt64(message, messagePos, m);
  486. for (int round = 0; round < ROUNDS; round++)
  487. {
  488. // G apply to columns of internalState:m[blake2b_sigma[round][2 * blockPos]] /+1
  489. G(m[blake2b_sigma[round,0]], m[blake2b_sigma[round,1]], 0, 4, 8, 12);
  490. G(m[blake2b_sigma[round,2]], m[blake2b_sigma[round,3]], 1, 5, 9, 13);
  491. G(m[blake2b_sigma[round,4]], m[blake2b_sigma[round,5]], 2, 6, 10, 14);
  492. G(m[blake2b_sigma[round,6]], m[blake2b_sigma[round,7]], 3, 7, 11, 15);
  493. // G apply to diagonals of internalState:
  494. G(m[blake2b_sigma[round,8]], m[blake2b_sigma[round,9]], 0, 5, 10, 15);
  495. G(m[blake2b_sigma[round,10]], m[blake2b_sigma[round,11]], 1, 6, 11, 12);
  496. G(m[blake2b_sigma[round,12]], m[blake2b_sigma[round,13]], 2, 7, 8, 13);
  497. G(m[blake2b_sigma[round,14]], m[blake2b_sigma[round,15]], 3, 4, 9, 14);
  498. }
  499. // update chain values:
  500. for (int offset = 0; offset < chainValue.Length; offset++)
  501. {
  502. chainValue[offset] = chainValue[offset] ^ internalState[offset] ^ internalState[offset + 8];
  503. }
  504. }
  505. #endif
  506. #if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER || UNITY_2021_2_OR_NEWER
  507. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  508. #endif
  509. private void G(ulong m1, ulong m2, int posA, int posB, int posC, int posD)
  510. {
  511. internalState[posA] = internalState[posA] + internalState[posB] + m1;
  512. internalState[posD] = Rotr64(internalState[posD] ^ internalState[posA], 32);
  513. internalState[posC] = internalState[posC] + internalState[posD];
  514. internalState[posB] = Rotr64(internalState[posB] ^ internalState[posC], 24); // replaces 25 of BLAKE
  515. internalState[posA] = internalState[posA] + internalState[posB] + m2;
  516. internalState[posD] = Rotr64(internalState[posD] ^ internalState[posA], 16);
  517. internalState[posC] = internalState[posC] + internalState[posD];
  518. internalState[posB] = Rotr64(internalState[posB] ^ internalState[posC], 63); // replaces 11 of BLAKE
  519. }
  520. private static ulong Rotr64(ulong x, int rot)
  521. {
  522. return x >> rot | x << -rot;
  523. }
  524. /**
  525. * return the algorithm name
  526. *
  527. * @return the algorithm name
  528. */
  529. public string AlgorithmName => "BLAKE2b";
  530. /**
  531. * return the size, in bytes, of the digest produced by this message digest.
  532. *
  533. * @return the size, in bytes, of the digest produced by this message digest.
  534. */
  535. public int GetDigestSize()
  536. {
  537. return digestLength;
  538. }
  539. /**
  540. * Return the size in bytes of the internal buffer the digest applies it's compression
  541. * function to.
  542. *
  543. * @return byte length of the digests internal buffer.
  544. */
  545. public int GetByteLength()
  546. {
  547. return BLOCK_LENGTH_BYTES;
  548. }
  549. /**
  550. * Overwrite the key
  551. * if it is no longer used (zeroization)
  552. */
  553. public void ClearKey()
  554. {
  555. if (key != null)
  556. {
  557. Array.Clear(key, 0, key.Length);
  558. Array.Clear(buffer, 0, buffer.Length);
  559. }
  560. }
  561. /**
  562. * Overwrite the salt (pepper) if it
  563. * is secret and no longer used (zeroization)
  564. */
  565. public void ClearSalt()
  566. {
  567. if (salt != null)
  568. {
  569. Array.Clear(salt, 0, salt.Length);
  570. }
  571. }
  572. }
  573. }
  574. #pragma warning restore
  575. #endif