OpenSSLPBEParametersGenerator.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
  8. {
  9. /**
  10. * Generator for PBE derived keys and ivs as usd by OpenSSL.
  11. * <p>
  12. * The scheme is a simple extension of PKCS 5 V2.0 Scheme 1 using MD5 with an
  13. * iteration count of 1.
  14. * </p>
  15. */
  16. public class OpenSslPbeParametersGenerator
  17. : PbeParametersGenerator
  18. {
  19. private readonly IDigest digest = new MD5Digest();
  20. /**
  21. * Construct a OpenSSL Parameters generator.
  22. */
  23. public OpenSslPbeParametersGenerator()
  24. {
  25. }
  26. public override void Init(
  27. byte[] password,
  28. byte[] salt,
  29. int iterationCount)
  30. {
  31. // Ignore the provided iterationCount
  32. base.Init(password, salt, 1);
  33. }
  34. /**
  35. * Initialise - note the iteration count for this algorithm is fixed at 1.
  36. *
  37. * @param password password to use.
  38. * @param salt salt to use.
  39. */
  40. public virtual void Init(
  41. byte[] password,
  42. byte[] salt)
  43. {
  44. base.Init(password, salt, 1);
  45. }
  46. /**
  47. * the derived key function, the ith hash of the password and the salt.
  48. */
  49. private byte[] GenerateDerivedKey(
  50. int bytesNeeded)
  51. {
  52. byte[] buf = new byte[digest.GetDigestSize()];
  53. byte[] key = new byte[bytesNeeded];
  54. int offset = 0;
  55. for (;;)
  56. {
  57. digest.BlockUpdate(mPassword, 0, mPassword.Length);
  58. digest.BlockUpdate(mSalt, 0, mSalt.Length);
  59. digest.DoFinal(buf, 0);
  60. int len = (bytesNeeded > buf.Length) ? buf.Length : bytesNeeded;
  61. Array.Copy(buf, 0, key, offset, len);
  62. offset += len;
  63. // check if we need any more
  64. bytesNeeded -= len;
  65. if (bytesNeeded == 0)
  66. {
  67. break;
  68. }
  69. // do another round
  70. digest.Reset();
  71. digest.BlockUpdate(buf, 0, buf.Length);
  72. }
  73. return key;
  74. }
  75. /**
  76. * Generate a key parameter derived from the password, salt, and iteration
  77. * count we are currently initialised with.
  78. *
  79. * @param keySize the size of the key we want (in bits)
  80. * @return a KeyParameter object.
  81. * @exception ArgumentException if the key length larger than the base hash size.
  82. */
  83. [Obsolete("Use version with 'algorithm' parameter")]
  84. public override ICipherParameters GenerateDerivedParameters(
  85. int keySize)
  86. {
  87. return GenerateDerivedMacParameters(keySize);
  88. }
  89. public override ICipherParameters GenerateDerivedParameters(
  90. string algorithm,
  91. int keySize)
  92. {
  93. keySize /= 8;
  94. byte[] dKey = GenerateDerivedKey(keySize);
  95. return ParameterUtilities.CreateKeyParameter(algorithm, dKey, 0, keySize);
  96. }
  97. /**
  98. * Generate a key with initialisation vector parameter derived from
  99. * the password, salt, and iteration count we are currently initialised
  100. * with.
  101. *
  102. * @param keySize the size of the key we want (in bits)
  103. * @param ivSize the size of the iv we want (in bits)
  104. * @return a ParametersWithIV object.
  105. * @exception ArgumentException if keySize + ivSize is larger than the base hash size.
  106. */
  107. [Obsolete("Use version with 'algorithm' parameter")]
  108. public override ICipherParameters GenerateDerivedParameters(
  109. int keySize,
  110. int ivSize)
  111. {
  112. keySize = keySize / 8;
  113. ivSize = ivSize / 8;
  114. byte[] dKey = GenerateDerivedKey(keySize + ivSize);
  115. return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), dKey, keySize, ivSize);
  116. }
  117. public override ICipherParameters GenerateDerivedParameters(
  118. string algorithm,
  119. int keySize,
  120. int ivSize)
  121. {
  122. keySize /= 8;
  123. ivSize /= 8;
  124. byte[] dKey = GenerateDerivedKey(keySize + ivSize);
  125. KeyParameter key = ParameterUtilities.CreateKeyParameter(algorithm, dKey, 0, keySize);
  126. return new ParametersWithIV(key, dKey, keySize, ivSize);
  127. }
  128. /**
  129. * Generate a key parameter for use with a MAC derived from the password,
  130. * salt, and iteration count we are currently initialised with.
  131. *
  132. * @param keySize the size of the key we want (in bits)
  133. * @return a KeyParameter object.
  134. * @exception ArgumentException if the key length larger than the base hash size.
  135. */
  136. public override ICipherParameters GenerateDerivedMacParameters(
  137. int keySize)
  138. {
  139. keySize = keySize / 8;
  140. byte[] dKey = GenerateDerivedKey(keySize);
  141. return new KeyParameter(dKey, 0, keySize);
  142. }
  143. }
  144. }
  145. #pragma warning restore
  146. #endif