Pkcs5S1ParametersGenerator.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  9. namespace Best.HTTP.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. public override ICipherParameters GenerateDerivedParameters(
  52. string algorithm,
  53. int keySize)
  54. {
  55. keySize /= 8;
  56. if (keySize > digest.GetDigestSize())
  57. {
  58. throw new ArgumentException(
  59. "Can't Generate a derived key " + keySize + " bytes long.");
  60. }
  61. byte[] dKey = GenerateDerivedKey();
  62. return ParameterUtilities.CreateKeyParameter(algorithm, dKey, 0, keySize);
  63. }
  64. public override ICipherParameters GenerateDerivedParameters(
  65. string algorithm,
  66. int keySize,
  67. int ivSize)
  68. {
  69. keySize /= 8;
  70. ivSize /= 8;
  71. if ((keySize + ivSize) > digest.GetDigestSize())
  72. {
  73. throw new ArgumentException(
  74. "Can't Generate a derived key " + (keySize + ivSize) + " bytes long.");
  75. }
  76. byte[] dKey = GenerateDerivedKey();
  77. KeyParameter key = ParameterUtilities.CreateKeyParameter(algorithm, dKey, 0, keySize);
  78. return new ParametersWithIV(key, dKey, keySize, ivSize);
  79. }
  80. /**
  81. * Generate a key parameter for use with a MAC derived from the mPassword,
  82. * mSalt, and iteration count we are currently initialised with.
  83. *
  84. * @param keySize the size of the key we want (in bits)
  85. * @return a KeyParameter object.
  86. * @exception ArgumentException if the key length larger than the base hash size.
  87. */
  88. public override ICipherParameters GenerateDerivedMacParameters(
  89. int keySize)
  90. {
  91. keySize /= 8;
  92. if (keySize > digest.GetDigestSize())
  93. {
  94. throw new ArgumentException(
  95. "Can't Generate a derived key " + keySize + " bytes long.");
  96. }
  97. byte[] dKey = GenerateDerivedKey();
  98. return new KeyParameter(dKey, 0, keySize);
  99. }
  100. }
  101. }
  102. #pragma warning restore
  103. #endif