Pkcs5S1ParametersGenerator.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
  10. {
  11. /**
  12. * Generator for Pbe derived keys and ivs as defined by Pkcs 5 V2.0 Scheme 1.
  13. * Note this generator is limited to the size of the hash produced by the
  14. * digest used to drive it.
  15. * <p>
  16. * The document this implementation is based on can be found at
  17. * <a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/index.html">
  18. * RSA's Pkcs5 Page</a>
  19. * </p>
  20. */
  21. public class Pkcs5S1ParametersGenerator
  22. : PbeParametersGenerator
  23. {
  24. private readonly IDigest digest;
  25. /**
  26. * Construct a Pkcs 5 Scheme 1 Parameters generator.
  27. *
  28. * @param digest the digest to be used as the source of derived keys.
  29. */
  30. public Pkcs5S1ParametersGenerator(
  31. IDigest digest)
  32. {
  33. this.digest = digest;
  34. }
  35. /**
  36. * the derived key function, the ith hash of the mPassword and the mSalt.
  37. */
  38. private byte[] GenerateDerivedKey()
  39. {
  40. byte[] digestBytes = new byte[digest.GetDigestSize()];
  41. digest.BlockUpdate(mPassword, 0, mPassword.Length);
  42. digest.BlockUpdate(mSalt, 0, mSalt.Length);
  43. digest.DoFinal(digestBytes, 0);
  44. for (int i = 1; i < mIterationCount; i++)
  45. {
  46. digest.BlockUpdate(digestBytes, 0, digestBytes.Length);
  47. digest.DoFinal(digestBytes, 0);
  48. }
  49. return digestBytes;
  50. }
  51. /**
  52. * Generate a key parameter derived from the mPassword, mSalt, and iteration
  53. * count we are currently initialised with.
  54. *
  55. * @param keySize the size of the key we want (in bits)
  56. * @return a KeyParameter object.
  57. * @exception ArgumentException if the key length larger than the base hash size.
  58. */
  59. public override ICipherParameters GenerateDerivedParameters(
  60. int keySize)
  61. {
  62. return GenerateDerivedMacParameters(keySize);
  63. }
  64. public override ICipherParameters GenerateDerivedParameters(
  65. string algorithm,
  66. int keySize)
  67. {
  68. keySize /= 8;
  69. if (keySize > digest.GetDigestSize())
  70. {
  71. throw new ArgumentException(
  72. "Can't Generate a derived key " + keySize + " bytes long.");
  73. }
  74. byte[] dKey = GenerateDerivedKey();
  75. return ParameterUtilities.CreateKeyParameter(algorithm, dKey, 0, keySize);
  76. }
  77. /**
  78. * Generate a key with initialisation vector parameter derived from
  79. * the mPassword, mSalt, and iteration count we are currently initialised
  80. * with.
  81. *
  82. * @param keySize the size of the key we want (in bits)
  83. * @param ivSize the size of the iv we want (in bits)
  84. * @return a ParametersWithIV object.
  85. * @exception ArgumentException if keySize + ivSize is larger than the base hash size.
  86. */
  87. public override ICipherParameters GenerateDerivedParameters(
  88. int keySize,
  89. int ivSize)
  90. {
  91. keySize /= 8;
  92. ivSize /= 8;
  93. if ((keySize + ivSize) > digest.GetDigestSize())
  94. {
  95. throw new ArgumentException(
  96. "Can't Generate a derived key " + (keySize + ivSize) + " bytes long.");
  97. }
  98. byte[] dKey = GenerateDerivedKey();
  99. return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), dKey, keySize, ivSize);
  100. }
  101. public override ICipherParameters GenerateDerivedParameters(
  102. string algorithm,
  103. int keySize,
  104. int ivSize)
  105. {
  106. keySize /= 8;
  107. ivSize /= 8;
  108. if ((keySize + ivSize) > digest.GetDigestSize())
  109. {
  110. throw new ArgumentException(
  111. "Can't Generate a derived key " + (keySize + ivSize) + " bytes long.");
  112. }
  113. byte[] dKey = GenerateDerivedKey();
  114. KeyParameter key = ParameterUtilities.CreateKeyParameter(algorithm, dKey, 0, keySize);
  115. return new ParametersWithIV(key, dKey, keySize, ivSize);
  116. }
  117. /**
  118. * Generate a key parameter for use with a MAC derived from the mPassword,
  119. * mSalt, and iteration count we are currently initialised with.
  120. *
  121. * @param keySize the size of the key we want (in bits)
  122. * @return a KeyParameter object.
  123. * @exception ArgumentException if the key length larger than the base hash size.
  124. */
  125. public override ICipherParameters GenerateDerivedMacParameters(
  126. int keySize)
  127. {
  128. keySize /= 8;
  129. if (keySize > digest.GetDigestSize())
  130. {
  131. throw new ArgumentException(
  132. "Can't Generate a derived key " + keySize + " bytes long.");
  133. }
  134. byte[] dKey = GenerateDerivedKey();
  135. return new KeyParameter(dKey, 0, keySize);
  136. }
  137. }
  138. }
  139. #pragma warning restore
  140. #endif