GCMBlockCipher.cs 17 KB

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