KDFDoublePipelineIterationBytesGenerator.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
  8. {
  9. public sealed class KdfDoublePipelineIterationBytesGenerator
  10. : IMacDerivationFunction
  11. {
  12. // fields set by the constructor
  13. private readonly IMac prf;
  14. private readonly int h;
  15. // fields set by init
  16. private byte[] fixedInputData;
  17. private int maxSizeExcl;
  18. // ios is i defined as an octet string (the binary representation)
  19. private byte[] ios;
  20. private bool useCounter;
  21. // operational
  22. private int generatedBytes;
  23. // k is used as buffer for all K(i) values
  24. private byte[] a;
  25. private byte[] k;
  26. public KdfDoublePipelineIterationBytesGenerator(IMac prf)
  27. {
  28. this.prf = prf;
  29. this.h = prf.GetMacSize();
  30. this.a = new byte[h];
  31. this.k = new byte[h];
  32. }
  33. public void Init(IDerivationParameters parameters)
  34. {
  35. if (!(parameters is KdfDoublePipelineIterationParameters dpiParams))
  36. throw new ArgumentException("Wrong type of arguments given");
  37. // --- init mac based PRF ---
  38. this.prf.Init(new KeyParameter(dpiParams.Ki));
  39. // --- set arguments ---
  40. this.fixedInputData = dpiParams.FixedInputData;
  41. int r = dpiParams.R;
  42. this.ios = new byte[r / 8];
  43. if (dpiParams.UseCounter)
  44. {
  45. // this is more conservative than the spec
  46. BigInteger maxSize = BigInteger.One.ShiftLeft(r).Multiply(BigInteger.ValueOf(h));
  47. this.maxSizeExcl = maxSize.BitLength > 31 ? int.MaxValue : maxSize.IntValueExact;
  48. }
  49. else
  50. {
  51. this.maxSizeExcl = int.MaxValue;
  52. }
  53. this.useCounter = dpiParams.UseCounter;
  54. // --- set operational state ---
  55. generatedBytes = 0;
  56. }
  57. private void GenerateNext()
  58. {
  59. if (generatedBytes == 0)
  60. {
  61. // --- step 4 ---
  62. prf.BlockUpdate(fixedInputData, 0, fixedInputData.Length);
  63. prf.DoFinal(a, 0);
  64. }
  65. else
  66. {
  67. // --- step 5a ---
  68. prf.BlockUpdate(a, 0, a.Length);
  69. prf.DoFinal(a, 0);
  70. }
  71. // --- step 5b ---
  72. prf.BlockUpdate(a, 0, a.Length);
  73. if (useCounter)
  74. {
  75. int i = generatedBytes / h + 1;
  76. // encode i into counter buffer
  77. switch (ios.Length)
  78. {
  79. case 4:
  80. ios[0] = (byte)(i >> 24);
  81. // fall through
  82. goto case 3;
  83. case 3:
  84. ios[ios.Length - 3] = (byte)(i >> 16);
  85. // fall through
  86. goto case 2;
  87. case 2:
  88. ios[ios.Length - 2] = (byte)(i >> 8);
  89. // fall through
  90. goto case 1;
  91. case 1:
  92. ios[ios.Length - 1] = (byte)i;
  93. break;
  94. default:
  95. throw new InvalidOperationException("Unsupported size of counter i");
  96. }
  97. prf.BlockUpdate(ios, 0, ios.Length);
  98. }
  99. prf.BlockUpdate(fixedInputData, 0, fixedInputData.Length);
  100. prf.DoFinal(k, 0);
  101. }
  102. public IDigest Digest
  103. {
  104. get { return (prf as HMac)?.GetUnderlyingDigest(); }
  105. }
  106. public int GenerateBytes(byte[] output, int outOff, int length)
  107. {
  108. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  109. return GenerateBytes(output.AsSpan(outOff, length));
  110. #else
  111. if (generatedBytes >= maxSizeExcl - length)
  112. throw new DataLengthException("Current KDFCTR may only be used for " + maxSizeExcl + " bytes");
  113. int toGenerate = length;
  114. int posInK = generatedBytes % h;
  115. if (posInK != 0)
  116. {
  117. // copy what is left in the currentT (1..hash
  118. int toCopy = System.Math.Min(h - posInK, toGenerate);
  119. Array.Copy(k, posInK, output, outOff, toCopy);
  120. generatedBytes += toCopy;
  121. toGenerate -= toCopy;
  122. outOff += toCopy;
  123. }
  124. while (toGenerate > 0)
  125. {
  126. GenerateNext();
  127. int toCopy = System.Math.Min(h, toGenerate);
  128. Array.Copy(k, 0, output, outOff, toCopy);
  129. generatedBytes += toCopy;
  130. toGenerate -= toCopy;
  131. outOff += toCopy;
  132. }
  133. return length;
  134. #endif
  135. }
  136. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  137. public int GenerateBytes(Span<byte> output)
  138. {
  139. int length = output.Length;
  140. if (generatedBytes >= maxSizeExcl - length)
  141. throw new DataLengthException("Current KDFCTR may only be used for " + maxSizeExcl + " bytes");
  142. int posInK = generatedBytes % h;
  143. if (posInK != 0)
  144. {
  145. // copy what is left in the currentT (1..hash
  146. GenerateNext();
  147. int toCopy = System.Math.Min(h - posInK, output.Length);
  148. k.AsSpan(posInK, toCopy).CopyTo(output);
  149. generatedBytes += toCopy;
  150. output = output[toCopy..];
  151. }
  152. while (!output.IsEmpty)
  153. {
  154. GenerateNext();
  155. int toCopy = System.Math.Min(h, output.Length);
  156. k.AsSpan(0, toCopy).CopyTo(output);
  157. generatedBytes += toCopy;
  158. output = output[toCopy..];
  159. }
  160. return length;
  161. }
  162. #endif
  163. public IMac Mac => prf;
  164. }
  165. }
  166. #pragma warning restore
  167. #endif