GCMBlockCipher.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes.Gcm;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes
  10. {
  11. /// <summary>
  12. /// Implements the Galois/Counter mode (GCM) detailed in
  13. /// NIST Special Publication 800-38D.
  14. /// </summary>
  15. public sealed class GcmBlockCipher
  16. : IAeadBlockCipher
  17. {
  18. private const int BlockSize = 16;
  19. byte[] ctrBlock = new byte[BlockSize];
  20. private readonly IBlockCipher cipher;
  21. private readonly IGcmMultiplier multiplier;
  22. private IGcmExponentiator exp;
  23. // These fields are set by Init and not modified by processing
  24. private bool forEncryption;
  25. private bool initialised;
  26. private int macSize;
  27. private byte[] lastKey;
  28. private byte[] nonce;
  29. private byte[] initialAssociatedText;
  30. private byte[] H;
  31. private byte[] J0;
  32. // These fields are modified during processing
  33. private byte[] bufBlock;
  34. private byte[] macBlock;
  35. private byte[] S, S_at, S_atPre;
  36. private byte[] counter;
  37. private uint blocksRemaining;
  38. private int bufOff;
  39. private ulong totalLength;
  40. private byte[] atBlock;
  41. private int atBlockPos;
  42. private ulong atLength;
  43. private ulong atLengthPre;
  44. public GcmBlockCipher(
  45. IBlockCipher c)
  46. : this(c, null)
  47. {
  48. }
  49. public GcmBlockCipher(
  50. IBlockCipher c,
  51. IGcmMultiplier m)
  52. {
  53. if (c.GetBlockSize() != BlockSize)
  54. throw new ArgumentException("cipher required with a block size of " + BlockSize + ".");
  55. if (m == null)
  56. {
  57. // TODO Consider a static property specifying default multiplier
  58. m = new Tables8kGcmMultiplier();
  59. }
  60. this.cipher = c;
  61. this.multiplier = m;
  62. }
  63. public /*virtual*/ string AlgorithmName
  64. {
  65. get { return cipher.AlgorithmName + "/GCM"; }
  66. }
  67. public IBlockCipher GetUnderlyingCipher()
  68. {
  69. return cipher;
  70. }
  71. public /*virtual*/ int GetBlockSize()
  72. {
  73. return BlockSize;
  74. }
  75. /// <remarks>
  76. /// MAC sizes from 32 bits to 128 bits (must be a multiple of 8) are supported. The default is 128 bits.
  77. /// Sizes less than 96 are not recommended, but are supported for specialized applications.
  78. /// </remarks>
  79. public /*virtual*/ void Init(
  80. bool forEncryption,
  81. ICipherParameters parameters)
  82. {
  83. this.forEncryption = forEncryption;
  84. this.macBlock = null;
  85. this.initialised = true;
  86. KeyParameter keyParam;
  87. byte[] newNonce = null;
  88. if (parameters is AeadParameters)
  89. {
  90. AeadParameters param = (AeadParameters)parameters;
  91. newNonce = param.GetNonce();
  92. initialAssociatedText = param.GetAssociatedText();
  93. int macSizeBits = param.MacSize;
  94. if (macSizeBits < 32 || macSizeBits > 128 || macSizeBits % 8 != 0)
  95. {
  96. throw new ArgumentException("Invalid value for MAC size: " + macSizeBits);
  97. }
  98. macSize = macSizeBits / 8;
  99. keyParam = param.Key;
  100. }
  101. else if (parameters is ParametersWithIV)
  102. {
  103. ParametersWithIV param = (ParametersWithIV)parameters;
  104. newNonce = param.GetIV();
  105. initialAssociatedText = null;
  106. macSize = 16;
  107. keyParam = (KeyParameter)param.Parameters;
  108. }
  109. else
  110. {
  111. throw new ArgumentException("invalid parameters passed to GCM");
  112. }
  113. int bufLength = forEncryption ? BlockSize : (BlockSize + macSize);
  114. this.bufBlock = new byte[bufLength];
  115. if (newNonce == null || newNonce.Length < 1)
  116. {
  117. throw new ArgumentException("IV must be at least 1 byte");
  118. }
  119. if (forEncryption)
  120. {
  121. if (nonce != null && Arrays.AreEqual(nonce, newNonce))
  122. {
  123. if (keyParam == null)
  124. {
  125. throw new ArgumentException("cannot reuse nonce for GCM encryption");
  126. }
  127. if (lastKey != null && Arrays.AreEqual(lastKey, keyParam.GetKey()))
  128. {
  129. throw new ArgumentException("cannot reuse nonce for GCM encryption");
  130. }
  131. }
  132. }
  133. nonce = newNonce;
  134. if (keyParam != null)
  135. {
  136. lastKey = keyParam.GetKey();
  137. }
  138. // TODO Restrict macSize to 16 if nonce length not 12?
  139. // Cipher always used in forward mode
  140. // if keyParam is null we're reusing the last key.
  141. if (keyParam != null)
  142. {
  143. cipher.Init(true, keyParam);
  144. this.H = new byte[BlockSize];
  145. cipher.ProcessBlock(H, 0, H, 0);
  146. // if keyParam is null we're reusing the last key and the multiplier doesn't need re-init
  147. multiplier.Init(H);
  148. exp = null;
  149. }
  150. else if (this.H == null)
  151. {
  152. throw new ArgumentException("Key must be specified in initial init");
  153. }
  154. this.J0 = new byte[BlockSize];
  155. if (nonce.Length == 12)
  156. {
  157. Array.Copy(nonce, 0, J0, 0, nonce.Length);
  158. this.J0[BlockSize - 1] = 0x01;
  159. }
  160. else
  161. {
  162. gHASH(J0, nonce, nonce.Length);
  163. byte[] X = new byte[BlockSize];
  164. Pack.UInt64_To_BE((ulong)nonce.Length * 8UL, X, 8);
  165. gHASHBlock(J0, X);
  166. }
  167. this.S = new byte[BlockSize];
  168. this.S_at = new byte[BlockSize];
  169. this.S_atPre = new byte[BlockSize];
  170. this.atBlock = new byte[BlockSize];
  171. this.atBlockPos = 0;
  172. this.atLength = 0;
  173. this.atLengthPre = 0;
  174. this.counter = Arrays.Clone(J0);
  175. this.blocksRemaining = uint.MaxValue - 1; // page 8, len(P) <= 2^39 - 256, 1 block used by tag
  176. this.bufOff = 0;
  177. this.totalLength = 0;
  178. if (initialAssociatedText != null)
  179. {
  180. ProcessAadBytes(initialAssociatedText, 0, initialAssociatedText.Length);
  181. }
  182. }
  183. public /*virtual*/ byte[] GetMac()
  184. {
  185. return macBlock == null
  186. ? new byte[macSize]
  187. : Arrays.Clone(macBlock);
  188. }
  189. public /*virtual*/ int GetOutputSize(
  190. int len)
  191. {
  192. int totalData = len + bufOff;
  193. if (forEncryption)
  194. {
  195. return totalData + macSize;
  196. }
  197. return totalData < macSize ? 0 : totalData - macSize;
  198. }
  199. public /*virtual*/ int GetUpdateOutputSize(
  200. int len)
  201. {
  202. int totalData = len + bufOff;
  203. if (!forEncryption)
  204. {
  205. if (totalData < macSize)
  206. {
  207. return 0;
  208. }
  209. totalData -= macSize;
  210. }
  211. return totalData - totalData % BlockSize;
  212. }
  213. public /*virtual*/ void ProcessAadByte(byte input)
  214. {
  215. CheckStatus();
  216. atBlock[atBlockPos] = input;
  217. if (++atBlockPos == BlockSize)
  218. {
  219. // Hash each block as it fills
  220. gHASHBlock(S_at, atBlock);
  221. atBlockPos = 0;
  222. atLength += BlockSize;
  223. }
  224. }
  225. public /*virtual*/ void ProcessAadBytes(byte[] inBytes, int inOff, int len)
  226. {
  227. CheckStatus();
  228. for (int i = 0; i < len; ++i)
  229. {
  230. atBlock[atBlockPos] = inBytes[inOff + i];
  231. if (++atBlockPos == BlockSize)
  232. {
  233. // Hash each block as it fills
  234. gHASHBlock(S_at, atBlock);
  235. atBlockPos = 0;
  236. atLength += BlockSize;
  237. }
  238. }
  239. }
  240. private void InitCipher()
  241. {
  242. if (atLength > 0)
  243. {
  244. Array.Copy(S_at, 0, S_atPre, 0, BlockSize);
  245. atLengthPre = atLength;
  246. }
  247. // Finish hash for partial AAD block
  248. if (atBlockPos > 0)
  249. {
  250. gHASHPartial(S_atPre, atBlock, 0, atBlockPos);
  251. atLengthPre += (uint)atBlockPos;
  252. }
  253. if (atLengthPre > 0)
  254. {
  255. Array.Copy(S_atPre, 0, S, 0, BlockSize);
  256. }
  257. }
  258. public /*virtual*/ int ProcessByte(
  259. byte input,
  260. byte[] output,
  261. int outOff)
  262. {
  263. CheckStatus();
  264. bufBlock[bufOff] = input;
  265. if (++bufOff == bufBlock.Length)
  266. {
  267. ProcessBlock(bufBlock, 0, output, outOff);
  268. if (forEncryption)
  269. {
  270. bufOff = 0;
  271. }
  272. else
  273. {
  274. Array.Copy(bufBlock, BlockSize, bufBlock, 0, macSize);
  275. bufOff = macSize;
  276. }
  277. return BlockSize;
  278. }
  279. return 0;
  280. }
  281. public /*virtual*/ int ProcessBytes(
  282. byte[] input,
  283. int inOff,
  284. int len,
  285. byte[] output,
  286. int outOff)
  287. {
  288. CheckStatus();
  289. Check.DataLength(input, inOff, len, "input buffer too short");
  290. int resultLen = 0;
  291. if (forEncryption)
  292. {
  293. if (bufOff != 0)
  294. {
  295. while (len > 0)
  296. {
  297. --len;
  298. bufBlock[bufOff] = input[inOff++];
  299. if (++bufOff == BlockSize)
  300. {
  301. ProcessBlock(bufBlock, 0, output, outOff);
  302. bufOff = 0;
  303. resultLen += BlockSize;
  304. break;
  305. }
  306. }
  307. }
  308. while (len >= BlockSize)
  309. {
  310. ProcessBlock(input, inOff, output, outOff + resultLen);
  311. inOff += BlockSize;
  312. len -= BlockSize;
  313. resultLen += BlockSize;
  314. }
  315. if (len > 0)
  316. {
  317. Array.Copy(input, inOff, bufBlock, 0, len);
  318. bufOff = len;
  319. }
  320. }
  321. else
  322. {
  323. for (int i = 0; i < len; ++i)
  324. {
  325. bufBlock[bufOff] = input[inOff + i];
  326. if (++bufOff == bufBlock.Length)
  327. {
  328. ProcessBlock(bufBlock, 0, output, outOff + resultLen);
  329. Array.Copy(bufBlock, BlockSize, bufBlock, 0, macSize);
  330. bufOff = macSize;
  331. resultLen += BlockSize;
  332. }
  333. }
  334. }
  335. return resultLen;
  336. }
  337. public int DoFinal(byte[] output, int outOff)
  338. {
  339. CheckStatus();
  340. if (totalLength == 0)
  341. {
  342. InitCipher();
  343. }
  344. int extra = bufOff;
  345. if (forEncryption)
  346. {
  347. Check.OutputLength(output, outOff, extra + macSize, "Output buffer too short");
  348. }
  349. else
  350. {
  351. if (extra < macSize)
  352. throw new InvalidCipherTextException("data too short");
  353. extra -= macSize;
  354. Check.OutputLength(output, outOff, extra, "Output buffer too short");
  355. }
  356. if (extra > 0)
  357. {
  358. ProcessPartial(bufBlock, 0, extra, output, outOff);
  359. }
  360. atLength += (uint)atBlockPos;
  361. if (atLength > atLengthPre)
  362. {
  363. /*
  364. * Some AAD was sent after the cipher started. We determine the difference b/w the hash value
  365. * we actually used when the cipher started (S_atPre) and the final hash value calculated (S_at).
  366. * Then we carry this difference forward by multiplying by H^c, where c is the number of (full or
  367. * partial) cipher-text blocks produced, and adjust the current hash.
  368. */
  369. // Finish hash for partial AAD block
  370. if (atBlockPos > 0)
  371. {
  372. gHASHPartial(S_at, atBlock, 0, atBlockPos);
  373. }
  374. // Find the difference between the AAD hashes
  375. if (atLengthPre > 0)
  376. {
  377. GcmUtilities.Xor(S_at, S_atPre);
  378. }
  379. // Number of cipher-text blocks produced
  380. long c = (long)(((totalLength * 8) + 127) >> 7);
  381. // Calculate the adjustment factor
  382. byte[] H_c = new byte[16];
  383. if (exp == null)
  384. {
  385. exp = new Tables1kGcmExponentiator();
  386. exp.Init(H);
  387. }
  388. exp.ExponentiateX(c, H_c);
  389. // Carry the difference forward
  390. GcmUtilities.Multiply(S_at, H_c);
  391. // Adjust the current hash
  392. GcmUtilities.Xor(S, S_at);
  393. }
  394. // Final gHASH
  395. byte[] X = new byte[BlockSize];
  396. Pack.UInt64_To_BE(atLength * 8UL, X, 0);
  397. Pack.UInt64_To_BE(totalLength * 8UL, X, 8);
  398. gHASHBlock(S, X);
  399. // T = MSBt(GCTRk(J0,S))
  400. byte[] tag = new byte[BlockSize];
  401. cipher.ProcessBlock(J0, 0, tag, 0);
  402. GcmUtilities.Xor(tag, S);
  403. int resultLen = extra;
  404. // We place into macBlock our calculated value for T
  405. this.macBlock = new byte[macSize];
  406. Array.Copy(tag, 0, macBlock, 0, macSize);
  407. if (forEncryption)
  408. {
  409. // Append T to the message
  410. Array.Copy(macBlock, 0, output, outOff + bufOff, macSize);
  411. resultLen += macSize;
  412. }
  413. else
  414. {
  415. // Retrieve the T value from the message and compare to calculated one
  416. byte[] msgMac = new byte[macSize];
  417. Array.Copy(bufBlock, extra, msgMac, 0, macSize);
  418. if (!Arrays.ConstantTimeAreEqual(this.macBlock, msgMac))
  419. throw new InvalidCipherTextException("mac check in GCM failed");
  420. }
  421. Reset(false);
  422. return resultLen;
  423. }
  424. public /*virtual*/ void Reset()
  425. {
  426. Reset(true);
  427. }
  428. private void Reset(
  429. bool clearMac)
  430. {
  431. cipher.Reset();
  432. // note: we do not reset the nonce.
  433. S = new byte[BlockSize];
  434. S_at = new byte[BlockSize];
  435. S_atPre = new byte[BlockSize];
  436. atBlock = new byte[BlockSize];
  437. atBlockPos = 0;
  438. atLength = 0;
  439. atLengthPre = 0;
  440. counter = Arrays.Clone(J0);
  441. blocksRemaining = uint.MaxValue - 1;
  442. bufOff = 0;
  443. totalLength = 0;
  444. if (bufBlock != null)
  445. {
  446. Arrays.Fill(bufBlock, 0);
  447. }
  448. if (clearMac)
  449. {
  450. macBlock = null;
  451. }
  452. if (forEncryption)
  453. {
  454. initialised = false;
  455. }
  456. else
  457. {
  458. if (initialAssociatedText != null)
  459. {
  460. ProcessAadBytes(initialAssociatedText, 0, initialAssociatedText.Length);
  461. }
  462. }
  463. }
  464. private void ProcessBlock(byte[] buf, int bufOff, byte[] output, int outOff)
  465. {
  466. Check.OutputLength(output, outOff, BlockSize, "Output buffer too short");
  467. if (totalLength == 0)
  468. {
  469. InitCipher();
  470. }
  471. //byte[] ctrBlock = new byte[BlockSize];
  472. GetNextCtrBlock(ctrBlock);
  473. if (forEncryption)
  474. {
  475. GcmUtilities.Xor(ctrBlock, buf, bufOff);
  476. gHASHBlock(S, ctrBlock);
  477. Array.Copy(ctrBlock, 0, output, outOff, BlockSize);
  478. }
  479. else
  480. {
  481. gHASHBlock(S, buf, bufOff);
  482. GcmUtilities.Xor(ctrBlock, 0, buf, bufOff, output, outOff);
  483. }
  484. totalLength += BlockSize;
  485. }
  486. private void ProcessPartial(byte[] buf, int off, int len, byte[] output, int outOff)
  487. {
  488. //byte[] ctrBlock = new byte[BlockSize];
  489. GetNextCtrBlock(ctrBlock);
  490. if (forEncryption)
  491. {
  492. GcmUtilities.Xor(buf, off, ctrBlock, 0, len);
  493. gHASHPartial(S, buf, off, len);
  494. }
  495. else
  496. {
  497. gHASHPartial(S, buf, off, len);
  498. GcmUtilities.Xor(buf, off, ctrBlock, 0, len);
  499. }
  500. Array.Copy(buf, off, output, outOff, len);
  501. totalLength += (uint)len;
  502. }
  503. private void gHASH(byte[] Y, byte[] b, int len)
  504. {
  505. for (int pos = 0; pos < len; pos += BlockSize)
  506. {
  507. int num = System.Math.Min(len - pos, BlockSize);
  508. gHASHPartial(Y, b, pos, num);
  509. }
  510. }
  511. private void gHASHBlock(byte[] Y, byte[] b)
  512. {
  513. GcmUtilities.Xor(Y, b);
  514. multiplier.MultiplyH(Y);
  515. }
  516. private void gHASHBlock(byte[] Y, byte[] b, int off)
  517. {
  518. GcmUtilities.Xor(Y, b, off);
  519. multiplier.MultiplyH(Y);
  520. }
  521. private void gHASHPartial(byte[] Y, byte[] b, int off, int len)
  522. {
  523. GcmUtilities.Xor(Y, b, off, len);
  524. multiplier.MultiplyH(Y);
  525. }
  526. private void GetNextCtrBlock(byte[] block)
  527. {
  528. if (blocksRemaining == 0)
  529. throw new InvalidOperationException("Attempt to process too many blocks");
  530. blocksRemaining--;
  531. uint c = 1;
  532. c += counter[15]; counter[15] = (byte)c; c >>= 8;
  533. c += counter[14]; counter[14] = (byte)c; c >>= 8;
  534. c += counter[13]; counter[13] = (byte)c; c >>= 8;
  535. c += counter[12]; counter[12] = (byte)c;
  536. cipher.ProcessBlock(counter, 0, block, 0);
  537. }
  538. private void CheckStatus()
  539. {
  540. if (!initialised)
  541. {
  542. if (forEncryption)
  543. {
  544. throw new InvalidOperationException("GCM cipher cannot be reused for encryption");
  545. }
  546. throw new InvalidOperationException("GCM cipher needs to be initialised");
  547. }
  548. }
  549. }
  550. }
  551. #pragma warning restore
  552. #endif