OpenSSLPBEParametersGenerator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.Digests;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
  8. {
  9. /// <description>
  10. /// Generator for PBE derived keys and IVs as usd by OpenSSL. Originally this scheme was a simple extension of
  11. /// PKCS 5 V2.0 Scheme 1 using MD5 with an iteration count of 1. The default digest was changed to SHA-256 with
  12. /// OpenSSL 1.1.0. This implementation still defaults to MD5, but the digest can now be set.
  13. /// </description>
  14. public class OpenSslPbeParametersGenerator
  15. : PbeParametersGenerator
  16. {
  17. private readonly IDigest digest;
  18. ///
  19. /// <description>
  20. /// Construct a OpenSSL Parameters generator - digest the original MD5.
  21. /// </description>
  22. ///
  23. public OpenSslPbeParametersGenerator() : this(new MD5Digest())
  24. {
  25. }
  26. ///
  27. /// <description>
  28. /// Construct a OpenSSL Parameters generator - digest as specified.
  29. /// </description>
  30. /// <param name="digest">the digest to use as the PRF.</param>
  31. ///
  32. public OpenSslPbeParametersGenerator(IDigest digest)
  33. {
  34. this.digest = digest;
  35. }
  36. public override void Init(
  37. byte[] password,
  38. byte[] salt,
  39. int iterationCount)
  40. {
  41. // Ignore the provided iterationCount
  42. base.Init(password, salt, 1);
  43. }
  44. /**
  45. * Initialise - note the iteration count for this algorithm is fixed at 1.
  46. *
  47. * @param password password to use.
  48. * @param salt salt to use.
  49. */
  50. public virtual void Init(
  51. byte[] password,
  52. byte[] salt)
  53. {
  54. base.Init(password, salt, 1);
  55. }
  56. /**
  57. * the derived key function, the ith hash of the password and the salt.
  58. */
  59. private byte[] GenerateDerivedKey(
  60. int bytesNeeded)
  61. {
  62. byte[] buf = new byte[digest.GetDigestSize()];
  63. byte[] key = new byte[bytesNeeded];
  64. int offset = 0;
  65. for (;;)
  66. {
  67. digest.BlockUpdate(mPassword, 0, mPassword.Length);
  68. digest.BlockUpdate(mSalt, 0, mSalt.Length);
  69. digest.DoFinal(buf, 0);
  70. int len = (bytesNeeded > buf.Length) ? buf.Length : bytesNeeded;
  71. Array.Copy(buf, 0, key, offset, len);
  72. offset += len;
  73. // check if we need any more
  74. bytesNeeded -= len;
  75. if (bytesNeeded == 0)
  76. {
  77. break;
  78. }
  79. // do another round
  80. digest.Reset();
  81. digest.BlockUpdate(buf, 0, buf.Length);
  82. }
  83. return key;
  84. }
  85. public override ICipherParameters GenerateDerivedParameters(
  86. string algorithm,
  87. int keySize)
  88. {
  89. keySize /= 8;
  90. byte[] dKey = GenerateDerivedKey(keySize);
  91. return ParameterUtilities.CreateKeyParameter(algorithm, dKey, 0, keySize);
  92. }
  93. public override ICipherParameters GenerateDerivedParameters(
  94. string algorithm,
  95. int keySize,
  96. int ivSize)
  97. {
  98. keySize /= 8;
  99. ivSize /= 8;
  100. byte[] dKey = GenerateDerivedKey(keySize + ivSize);
  101. KeyParameter key = ParameterUtilities.CreateKeyParameter(algorithm, dKey, 0, keySize);
  102. return new ParametersWithIV(key, dKey, keySize, ivSize);
  103. }
  104. /**
  105. * Generate a key parameter for use with a MAC derived from the password,
  106. * salt, and iteration count we are currently initialised with.
  107. *
  108. * @param keySize the size of the key we want (in bits)
  109. * @return a KeyParameter object.
  110. * @exception ArgumentException if the key length larger than the base hash size.
  111. */
  112. public override ICipherParameters GenerateDerivedMacParameters(
  113. int keySize)
  114. {
  115. keySize = keySize / 8;
  116. byte[] dKey = GenerateDerivedKey(keySize);
  117. return new KeyParameter(dKey, 0, keySize);
  118. }
  119. }
  120. }
  121. #pragma warning restore
  122. #endif