KDFFeedbackBytesGenerator.cs 5.4 KB

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