FastPoly1305.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  9. namespace Best.HTTP.Shared.TLS.Crypto.Impl
  10. {
  11. /// <summary>
  12. /// Poly1305 message authentication code, designed by D. J. Bernstein.
  13. /// </summary>
  14. /// <remarks>
  15. /// Poly1305 computes a 128-bit (16 bytes) authenticator, using a 128 bit nonce and a 256 bit key
  16. /// consisting of a 128 bit key applied to an underlying cipher, and a 128 bit key (with 106
  17. /// effective key bits) used in the authenticator.
  18. ///
  19. /// The polynomial calculation in this implementation is adapted from the public domain <a
  20. /// href="https://github.com/floodyberry/poly1305-donna">poly1305-donna-unrolled</a> C implementation
  21. /// by Andrew M (@floodyberry).
  22. /// </remarks>
  23. /// <seealso cref="Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators.Poly1305KeyGenerator"/>
  24. [Best.HTTP.Shared.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
  25. public sealed class FastPoly1305 : IMac
  26. {
  27. private const int BlockSize = 16;
  28. private readonly IBlockCipher cipher;
  29. // Initialised state
  30. /** Polynomial key */
  31. private uint r0, r1, r2, r3, r4;
  32. /** Precomputed 5 * r[1..4] */
  33. private uint s1, s2, s3, s4;
  34. /** Encrypted nonce */
  35. private uint k0, k1, k2, k3;
  36. // Accumulating state
  37. /** Current block of buffered input */
  38. private byte[] currentBlock = new byte[BlockSize];
  39. /** Current offset in input buffer */
  40. private int currentBlockOffset = 0;
  41. /** Polynomial accumulator */
  42. private uint h0, h1, h2, h3, h4;
  43. /**
  44. * Constructs a Poly1305 MAC, where the key passed to init() will be used directly.
  45. */
  46. public FastPoly1305()
  47. {
  48. this.cipher = null;
  49. }
  50. /**
  51. * Constructs a Poly1305 MAC, using a 128 bit block cipher.
  52. */
  53. public FastPoly1305(IBlockCipher cipher)
  54. {
  55. if (cipher.GetBlockSize() != BlockSize)
  56. {
  57. throw new ArgumentException("Poly1305 requires a 128 bit block cipher.");
  58. }
  59. this.cipher = cipher;
  60. }
  61. /// <summary>
  62. /// Initialises the Poly1305 MAC.
  63. /// </summary>
  64. /// <param name="parameters">a {@link ParametersWithIV} containing a 128 bit nonce and a {@link KeyParameter} with
  65. /// a 256 bit key complying to the {@link Poly1305KeyGenerator Poly1305 key format}.</param>
  66. public void Init(ICipherParameters parameters)
  67. {
  68. byte[] nonce = null;
  69. if (cipher != null)
  70. {
  71. if (!(parameters is ParametersWithIV))
  72. throw new ArgumentException("Poly1305 requires an IV when used with a block cipher.", "parameters");
  73. ParametersWithIV ivParams = (ParametersWithIV)parameters;
  74. nonce = ivParams.GetIV();
  75. parameters = ivParams.Parameters;
  76. }
  77. if (!(parameters is KeyParameter))
  78. throw new ArgumentException("Poly1305 requires a key.");
  79. KeyParameter keyParams = (KeyParameter)parameters;
  80. SetKey(keyParams.GetKey(), nonce);
  81. Reset();
  82. }
  83. private void SetKey(byte[] key, byte[] nonce)
  84. {
  85. if (key.Length != 32)
  86. throw new ArgumentException("Poly1305 key must be 256 bits.");
  87. if (cipher != null && (nonce == null || nonce.Length != BlockSize))
  88. throw new ArgumentException("Poly1305 requires a 128 bit IV.");
  89. // Extract r portion of key (and "clamp" the values)
  90. uint t0 = Pack.LE_To_UInt32(key, 0);
  91. uint t1 = Pack.LE_To_UInt32(key, 4);
  92. uint t2 = Pack.LE_To_UInt32(key, 8);
  93. uint t3 = Pack.LE_To_UInt32(key, 12);
  94. // NOTE: The masks perform the key "clamping" implicitly
  95. r0 = t0 & 0x03FFFFFFU;
  96. r1 = ((t0 >> 26) | (t1 << 6)) & 0x03FFFF03U;
  97. r2 = ((t1 >> 20) | (t2 << 12)) & 0x03FFC0FFU;
  98. r3 = ((t2 >> 14) | (t3 << 18)) & 0x03F03FFFU;
  99. r4 = (t3 >> 8) & 0x000FFFFFU;
  100. // Precompute multipliers
  101. s1 = r1 * 5;
  102. s2 = r2 * 5;
  103. s3 = r3 * 5;
  104. s4 = r4 * 5;
  105. byte[] kBytes;
  106. int kOff;
  107. if (cipher == null)
  108. {
  109. kBytes = key;
  110. kOff = BlockSize;
  111. }
  112. else
  113. {
  114. // Compute encrypted nonce
  115. kBytes = new byte[BlockSize];
  116. kOff = 0;
  117. cipher.Init(true, new KeyParameter(key, BlockSize, BlockSize));
  118. cipher.ProcessBlock(nonce, 0, kBytes, 0);
  119. }
  120. k0 = Pack.LE_To_UInt32(kBytes, kOff + 0);
  121. k1 = Pack.LE_To_UInt32(kBytes, kOff + 4);
  122. k2 = Pack.LE_To_UInt32(kBytes, kOff + 8);
  123. k3 = Pack.LE_To_UInt32(kBytes, kOff + 12);
  124. }
  125. public string AlgorithmName
  126. {
  127. get { return cipher == null ? "Poly1305" : "Poly1305-" + cipher.AlgorithmName; }
  128. }
  129. public int GetMacSize()
  130. {
  131. return BlockSize;
  132. }
  133. public void Update(byte input)
  134. {
  135. currentBlock[currentBlockOffset++] = input;
  136. if (currentBlockOffset == BlockSize)
  137. {
  138. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  139. ProcessBlock(currentBlock);
  140. #else
  141. ProcessBlock(currentBlock, 0);
  142. #endif
  143. currentBlockOffset = 0;
  144. }
  145. }
  146. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  147. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  148. #endif
  149. public void BlockUpdate(byte[] input, int inOff, int len)
  150. {
  151. Check.DataLength(input, inOff, len, "input buffer too short");
  152. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  153. BlockUpdate(input.AsSpan(inOff, len));
  154. #else
  155. int available = BlockSize - currentBlockOffset;
  156. if (len < available)
  157. {
  158. Array.Copy(input, inOff, currentBlock, currentBlockOffset, len);
  159. currentBlockOffset += len;
  160. return;
  161. }
  162. int pos = 0;
  163. if (currentBlockOffset > 0)
  164. {
  165. Array.Copy(input, inOff, currentBlock, currentBlockOffset, available);
  166. pos = available;
  167. ProcessBlock(currentBlock, 0);
  168. }
  169. int remaining;
  170. while ((remaining = len - pos) >= BlockSize)
  171. {
  172. ProcessBlock(input, inOff + pos);
  173. pos += BlockSize;
  174. }
  175. Array.Copy(input, inOff + pos, currentBlock, 0, remaining);
  176. currentBlockOffset = remaining;
  177. #endif
  178. }
  179. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  180. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  181. #if !UNITY_ANDROID || UNITY_EDITOR
  182. unsafe
  183. #endif
  184. public void BlockUpdate(ReadOnlySpan<byte> input)
  185. {
  186. int available = BlockSize - currentBlockOffset;
  187. if (input.Length < available)
  188. {
  189. input.CopyTo(currentBlock.AsSpan(currentBlockOffset));
  190. currentBlockOffset += input.Length;
  191. return;
  192. }
  193. int pos = 0;
  194. if (currentBlockOffset > 0)
  195. {
  196. input[..available].CopyTo(currentBlock.AsSpan(currentBlockOffset));
  197. pos = available;
  198. ProcessBlock(currentBlock);
  199. }
  200. int remaining;
  201. while ((remaining = input.Length - pos) >= BlockSize)
  202. {
  203. #if UNITY_ANDROID && !UNITY_EDITOR
  204. uint t0 = System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(input[pos..]);
  205. uint t1 = System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(input[(pos + 4)..]);
  206. uint t2 = System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(input[(pos + 8)..]);
  207. uint t3 = System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(input[(pos + 12)..]);
  208. #else
  209. uint t0 = 0;
  210. uint t1 = 0;
  211. uint t2 = 0;
  212. uint t3 = 0;
  213. fixed (byte* pblock = &input[pos])
  214. {
  215. uint* publock = (uint*)pblock;
  216. t0 = publock[0];
  217. t1 = publock[1];
  218. t2 = publock[2];
  219. t3 = publock[3];
  220. }
  221. #endif
  222. h0 += t0 & 0x3ffffffU;
  223. h1 += ((t1 << 6) | (t0 >> 26)) & 0x3ffffffU;
  224. h2 += ((t2 << 12) | (t1 >> 20)) & 0x3ffffffU;
  225. h3 += ((t3 << 18) | (t2 >> 14)) & 0x3ffffffU;
  226. h4 += (1 << 24) | (t3 >> 8);
  227. ulong tp0 = (ulong)h0 * r0 + (ulong)h1 * s4 + (ulong)h2 * s3 + (ulong)h3 * s2 + (ulong)h4 * s1;
  228. ulong tp1 = (ulong)h0 * r1 + (ulong)h1 * r0 + (ulong)h2 * s4 + (ulong)h3 * s3 + (ulong)h4 * s2;
  229. ulong tp2 = (ulong)h0 * r2 + (ulong)h1 * r1 + (ulong)h2 * r0 + (ulong)h3 * s4 + (ulong)h4 * s3;
  230. ulong tp3 = (ulong)h0 * r3 + (ulong)h1 * r2 + (ulong)h2 * r1 + (ulong)h3 * r0 + (ulong)h4 * s4;
  231. ulong tp4 = (ulong)h0 * r4 + (ulong)h1 * r3 + (ulong)h2 * r2 + (ulong)h3 * r1 + (ulong)h4 * r0;
  232. h0 = (uint)tp0 & 0x3ffffff; tp1 += (tp0 >> 26);
  233. h1 = (uint)tp1 & 0x3ffffff; tp2 += (tp1 >> 26);
  234. h2 = (uint)tp2 & 0x3ffffff; tp3 += (tp2 >> 26);
  235. h3 = (uint)tp3 & 0x3ffffff; tp4 += (tp3 >> 26);
  236. h4 = (uint)tp4 & 0x3ffffff;
  237. h0 += (uint)(tp4 >> 26) * 5;
  238. h1 += h0 >> 26; h0 &= 0x3ffffff;
  239. pos += BlockSize;
  240. }
  241. input[pos..].CopyTo(currentBlock);
  242. currentBlockOffset = remaining;
  243. }
  244. #endif
  245. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  246. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  247. private void ProcessBlock(ReadOnlySpan<byte> block)
  248. {
  249. uint t0 = System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(block);
  250. uint t1 = System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(block[4..]);
  251. uint t2 = System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(block[8..]);
  252. uint t3 = System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(block[12..]);
  253. #else
  254. private void ProcessBlock(byte[] buf, int off)
  255. {
  256. uint t0 = Pack.LE_To_UInt32(buf, off + 0);
  257. uint t1 = Pack.LE_To_UInt32(buf, off + 4);
  258. uint t2 = Pack.LE_To_UInt32(buf, off + 8);
  259. uint t3 = Pack.LE_To_UInt32(buf, off + 12);
  260. #endif
  261. h0 += t0 & 0x3ffffffU;
  262. h1 += ((t1 << 6) | (t0 >> 26)) & 0x3ffffffU;
  263. h2 += ((t2 << 12) | (t1 >> 20)) & 0x3ffffffU;
  264. h3 += ((t3 << 18) | (t2 >> 14)) & 0x3ffffffU;
  265. h4 += (1 << 24) | (t3 >> 8);
  266. ulong tp0 = (ulong)h0 * r0 + (ulong)h1 * s4 + (ulong)h2 * s3 + (ulong)h3 * s2 + (ulong)h4 * s1;
  267. ulong tp1 = (ulong)h0 * r1 + (ulong)h1 * r0 + (ulong)h2 * s4 + (ulong)h3 * s3 + (ulong)h4 * s2;
  268. ulong tp2 = (ulong)h0 * r2 + (ulong)h1 * r1 + (ulong)h2 * r0 + (ulong)h3 * s4 + (ulong)h4 * s3;
  269. ulong tp3 = (ulong)h0 * r3 + (ulong)h1 * r2 + (ulong)h2 * r1 + (ulong)h3 * r0 + (ulong)h4 * s4;
  270. ulong tp4 = (ulong)h0 * r4 + (ulong)h1 * r3 + (ulong)h2 * r2 + (ulong)h3 * r1 + (ulong)h4 * r0;
  271. h0 = (uint)tp0 & 0x3ffffff; tp1 += (tp0 >> 26);
  272. h1 = (uint)tp1 & 0x3ffffff; tp2 += (tp1 >> 26);
  273. h2 = (uint)tp2 & 0x3ffffff; tp3 += (tp2 >> 26);
  274. h3 = (uint)tp3 & 0x3ffffff; tp4 += (tp3 >> 26);
  275. h4 = (uint)tp4 & 0x3ffffff;
  276. h0 += (uint)(tp4 >> 26) * 5;
  277. h1 += h0 >> 26; h0 &= 0x3ffffff;
  278. }
  279. public int DoFinal(byte[] output, int outOff)
  280. {
  281. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  282. return DoFinal(output.AsSpan(outOff));
  283. #else
  284. Check.OutputLength(output, outOff, BlockSize, "output buffer is too short.");
  285. if (currentBlockOffset > 0)
  286. {
  287. // Process padded block
  288. if (currentBlockOffset < BlockSize)
  289. {
  290. currentBlock[currentBlockOffset++] = 1;
  291. while (currentBlockOffset < BlockSize)
  292. {
  293. currentBlock[currentBlockOffset++] = 0;
  294. }
  295. h4 -= (1 << 24);
  296. }
  297. ProcessBlock(currentBlock, 0);
  298. }
  299. UnityEngine.Debug.Assert(h4 >> 26 == 0);
  300. //h0 += (h4 >> 26) * 5U + 5U; h4 &= 0x3ffffff;
  301. h0 += 5U;
  302. h1 += h0 >> 26; h0 &= 0x3ffffff;
  303. h2 += h1 >> 26; h1 &= 0x3ffffff;
  304. h3 += h2 >> 26; h2 &= 0x3ffffff;
  305. h4 += h3 >> 26; h3 &= 0x3ffffff;
  306. long c = ((int)(h4 >> 26) - 1) * 5;
  307. c += (long)k0 + ((h0) | (h1 << 26));
  308. Pack.UInt32_To_LE((uint)c, output, outOff); c >>= 32;
  309. c += (long)k1 + ((h1 >> 6) | (h2 << 20));
  310. Pack.UInt32_To_LE((uint)c, output, outOff + 4); c >>= 32;
  311. c += (long)k2 + ((h2 >> 12) | (h3 << 14));
  312. Pack.UInt32_To_LE((uint)c, output, outOff + 8); c >>= 32;
  313. c += (long)k3 + ((h3 >> 18) | (h4 << 8));
  314. Pack.UInt32_To_LE((uint)c, output, outOff + 12);
  315. Reset();
  316. return BlockSize;
  317. #endif
  318. }
  319. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  320. public int DoFinal(Span<byte> output)
  321. {
  322. Check.OutputLength(output, BlockSize, "output buffer is too short.");
  323. if (currentBlockOffset > 0)
  324. {
  325. // Process padded block
  326. if (currentBlockOffset < BlockSize)
  327. {
  328. currentBlock[currentBlockOffset++] = 1;
  329. while (currentBlockOffset < BlockSize)
  330. {
  331. currentBlock[currentBlockOffset++] = 0;
  332. }
  333. h4 -= (1 << 24);
  334. }
  335. ProcessBlock(currentBlock);
  336. }
  337. UnityEngine.Debug.Assert(h4 >> 26 == 0);
  338. //h0 += (h4 >> 26) * 5U + 5U; h4 &= 0x3ffffff;
  339. h0 += 5U;
  340. h1 += h0 >> 26; h0 &= 0x3ffffff;
  341. h2 += h1 >> 26; h1 &= 0x3ffffff;
  342. h3 += h2 >> 26; h2 &= 0x3ffffff;
  343. h4 += h3 >> 26; h3 &= 0x3ffffff;
  344. long c = ((int)(h4 >> 26) - 1) * 5;
  345. c += (long)k0 + ((h0) | (h1 << 26));
  346. Pack.UInt32_To_LE((uint)c, output); c >>= 32;
  347. c += (long)k1 + ((h1 >> 6) | (h2 << 20));
  348. Pack.UInt32_To_LE((uint)c, output[4..]); c >>= 32;
  349. c += (long)k2 + ((h2 >> 12) | (h3 << 14));
  350. Pack.UInt32_To_LE((uint)c, output[8..]); c >>= 32;
  351. c += (long)k3 + ((h3 >> 18) | (h4 << 8));
  352. Pack.UInt32_To_LE((uint)c, output[12..]);
  353. Reset();
  354. return BlockSize;
  355. }
  356. #endif
  357. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  358. public void Reset()
  359. {
  360. currentBlockOffset = 0;
  361. h0 = h1 = h2 = h3 = h4 = 0;
  362. }
  363. }
  364. }
  365. #pragma warning restore
  366. #endif