KDFDoublePipelineIterationBytesGenerator.cs 5.5 KB

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