Mgf1BytesGenerator.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. //using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. #if UNITY_WSA && !UNITY_EDITOR && !ENABLE_IL2CPP
  6. using System.TypeFix;
  7. #endif
  8. //using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  11. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
  12. {
  13. /**
  14. * Generator for MGF1 as defined in Pkcs 1v2
  15. */
  16. public class Mgf1BytesGenerator : IDerivationFunction
  17. {
  18. private IDigest digest;
  19. private byte[] seed;
  20. private int hLen;
  21. /**
  22. * @param digest the digest to be used as the source of Generated bytes
  23. */
  24. public Mgf1BytesGenerator(
  25. IDigest digest)
  26. {
  27. this.digest = digest;
  28. this.hLen = digest.GetDigestSize();
  29. }
  30. public void Init(
  31. IDerivationParameters parameters)
  32. {
  33. if (!(typeof(MgfParameters).IsInstanceOfType(parameters)))
  34. {
  35. throw new ArgumentException("MGF parameters required for MGF1Generator");
  36. }
  37. MgfParameters p = (MgfParameters)parameters;
  38. seed = p.GetSeed();
  39. }
  40. /**
  41. * return the underlying digest.
  42. */
  43. public IDigest Digest
  44. {
  45. get
  46. {
  47. return digest;
  48. }
  49. }
  50. /**
  51. * int to octet string.
  52. */
  53. private void ItoOSP(
  54. int i,
  55. byte[] sp)
  56. {
  57. sp[0] = (byte)((uint) i >> 24);
  58. sp[1] = (byte)((uint) i >> 16);
  59. sp[2] = (byte)((uint) i >> 8);
  60. sp[3] = (byte)((uint) i >> 0);
  61. }
  62. /**
  63. * fill len bytes of the output buffer with bytes Generated from
  64. * the derivation function.
  65. *
  66. * @throws DataLengthException if the out buffer is too small.
  67. */
  68. public int GenerateBytes(
  69. byte[] output,
  70. int outOff,
  71. int length)
  72. {
  73. if ((output.Length - length) < outOff)
  74. {
  75. throw new DataLengthException("output buffer too small");
  76. }
  77. byte[] hashBuf = new byte[hLen];
  78. byte[] C = new byte[4];
  79. int counter = 0;
  80. digest.Reset();
  81. if (length > hLen)
  82. {
  83. do
  84. {
  85. ItoOSP(counter, C);
  86. digest.BlockUpdate(seed, 0, seed.Length);
  87. digest.BlockUpdate(C, 0, C.Length);
  88. digest.DoFinal(hashBuf, 0);
  89. Array.Copy(hashBuf, 0, output, outOff + counter * hLen, hLen);
  90. }
  91. while (++counter < (length / hLen));
  92. }
  93. if ((counter * hLen) < length)
  94. {
  95. ItoOSP(counter, C);
  96. digest.BlockUpdate(seed, 0, seed.Length);
  97. digest.BlockUpdate(C, 0, C.Length);
  98. digest.DoFinal(hashBuf, 0);
  99. Array.Copy(hashBuf, 0, output, outOff + counter * hLen, length - (counter * hLen));
  100. }
  101. return length;
  102. }
  103. }
  104. }
  105. #pragma warning restore
  106. #endif