ConcatenationKdfGenerator.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement.Kdf
  7. {
  8. /**
  9. * Generator for Concatenation Key Derivation Function defined in NIST SP 800-56A, Sect 5.8.1
  10. */
  11. public class ConcatenationKdfGenerator
  12. : IDerivationFunction
  13. {
  14. private readonly IDigest mDigest;
  15. private byte[] mShared;
  16. private byte[] mOtherInfo;
  17. private int mHLen;
  18. /**
  19. * @param digest the digest to be used as the source of generated bytes
  20. */
  21. public ConcatenationKdfGenerator(IDigest digest)
  22. {
  23. this.mDigest = digest;
  24. this.mHLen = digest.GetDigestSize();
  25. }
  26. public virtual void Init(IDerivationParameters param)
  27. {
  28. if (!(param is KdfParameters))
  29. throw new ArgumentException("KDF parameters required for ConcatenationKdfGenerator");
  30. KdfParameters p = (KdfParameters)param;
  31. mShared = p.GetSharedSecret();
  32. mOtherInfo = p.GetIV();
  33. }
  34. /**
  35. * return the underlying digest.
  36. */
  37. public virtual IDigest Digest
  38. {
  39. get { return mDigest; }
  40. }
  41. /**
  42. * fill len bytes of the output buffer with bytes generated from
  43. * the derivation function.
  44. *
  45. * @throws DataLengthException if the out buffer is too small.
  46. */
  47. public virtual int GenerateBytes(byte[] outBytes, int outOff, int len)
  48. {
  49. if ((outBytes.Length - len) < outOff)
  50. throw new DataLengthException("output buffer too small");
  51. byte[] hashBuf = new byte[mHLen];
  52. byte[] C = new byte[4];
  53. uint counter = 1;
  54. int outputLen = 0;
  55. mDigest.Reset();
  56. if (len > mHLen)
  57. {
  58. do
  59. {
  60. Pack.UInt32_To_BE(counter, C);
  61. mDigest.BlockUpdate(C, 0, C.Length);
  62. mDigest.BlockUpdate(mShared, 0, mShared.Length);
  63. mDigest.BlockUpdate(mOtherInfo, 0, mOtherInfo.Length);
  64. mDigest.DoFinal(hashBuf, 0);
  65. Array.Copy(hashBuf, 0, outBytes, outOff + outputLen, mHLen);
  66. outputLen += mHLen;
  67. }
  68. while ((counter++) < (len / mHLen));
  69. }
  70. if (outputLen < len)
  71. {
  72. Pack.UInt32_To_BE(counter, C);
  73. mDigest.BlockUpdate(C, 0, C.Length);
  74. mDigest.BlockUpdate(mShared, 0, mShared.Length);
  75. mDigest.BlockUpdate(mOtherInfo, 0, mOtherInfo.Length);
  76. mDigest.DoFinal(hashBuf, 0);
  77. Array.Copy(hashBuf, 0, outBytes, outOff + outputLen, len - outputLen);
  78. }
  79. return len;
  80. }
  81. }
  82. }
  83. #pragma warning restore
  84. #endif