OpenSSLPBEParametersGenerator.cs 4.8 KB

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