Mgf1BytesGenerator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
  7. {
  8. /// <summary>Generator for MGF1 as defined in Pkcs 1v2</summary>
  9. public sealed class Mgf1BytesGenerator
  10. : IDerivationFunction
  11. {
  12. private readonly IDigest m_digest;
  13. private readonly int m_hLen;
  14. private byte[] m_buffer;
  15. /// <param name="digest">the digest to be used as the source of generated bytes</param>
  16. public Mgf1BytesGenerator(IDigest digest)
  17. {
  18. m_digest = digest;
  19. m_hLen = digest.GetDigestSize();
  20. }
  21. public void Init(IDerivationParameters parameters)
  22. {
  23. if (!(parameters is MgfParameters mgfParameters))
  24. throw new ArgumentException("MGF parameters required for MGF1Generator");
  25. m_buffer = new byte[mgfParameters.SeedLength + 4 + m_hLen];
  26. mgfParameters.GetSeed(m_buffer, 0);
  27. }
  28. /// <summary>the underlying digest.</summary>
  29. public IDigest Digest => m_digest;
  30. /// <summary>Fill <c>len</c> bytes of the output buffer with bytes generated from the derivation function.
  31. /// </summary>
  32. public int GenerateBytes(byte[] output, int outOff, int length)
  33. {
  34. Check.OutputLength(output, outOff, length, "output buffer too small");
  35. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  36. return GenerateBytes(output.AsSpan(outOff, length));
  37. #else
  38. int hashPos = m_buffer.Length - m_hLen;
  39. int counterPos = hashPos - 4;
  40. uint counter = 0;
  41. m_digest.Reset();
  42. int end = outOff + length;
  43. int limit = end - m_hLen;
  44. while (outOff <= limit)
  45. {
  46. Pack.UInt32_To_BE(counter++, m_buffer, counterPos);
  47. m_digest.BlockUpdate(m_buffer, 0, hashPos);
  48. m_digest.DoFinal(output, outOff);
  49. outOff += m_hLen;
  50. }
  51. if (outOff < end)
  52. {
  53. Pack.UInt32_To_BE(counter, m_buffer, counterPos);
  54. m_digest.BlockUpdate(m_buffer, 0, hashPos);
  55. m_digest.DoFinal(m_buffer, hashPos);
  56. Array.Copy(m_buffer, hashPos, output, outOff, end - outOff);
  57. }
  58. return length;
  59. #endif
  60. }
  61. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  62. public int GenerateBytes(Span<byte> output)
  63. {
  64. int hashPos = m_buffer.Length - m_hLen;
  65. int counterPos = hashPos - 4;
  66. uint counter = 0;
  67. m_digest.Reset();
  68. int pos = 0, length = output.Length, limit = length - m_hLen;
  69. while (pos <= limit)
  70. {
  71. Pack.UInt32_To_BE(counter++, m_buffer.AsSpan(counterPos));
  72. m_digest.BlockUpdate(m_buffer.AsSpan(0, hashPos));
  73. m_digest.DoFinal(output[pos..]);
  74. pos += m_hLen;
  75. }
  76. if (pos < length)
  77. {
  78. Pack.UInt32_To_BE(counter, m_buffer.AsSpan(counterPos));
  79. m_digest.BlockUpdate(m_buffer.AsSpan(0, hashPos));
  80. m_digest.DoFinal(m_buffer.AsSpan(hashPos));
  81. m_buffer.AsSpan(hashPos, length - pos).CopyTo(output[pos..]);
  82. }
  83. return length;
  84. }
  85. #endif
  86. }
  87. }
  88. #pragma warning restore
  89. #endif