KDFCounterBytesGenerator.cs 4.8 KB

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