DsaDigestSigner.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.Math;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers
  8. {
  9. public class DsaDigestSigner
  10. : ISigner
  11. {
  12. private readonly IDsa dsa;
  13. private readonly IDigest digest;
  14. private readonly IDsaEncoding encoding;
  15. private bool forSigning;
  16. public DsaDigestSigner(IDsa dsa, IDigest digest)
  17. : this(dsa, digest, StandardDsaEncoding.Instance)
  18. {
  19. }
  20. public DsaDigestSigner(IDsa dsa, IDigest digest, IDsaEncoding encoding)
  21. {
  22. this.dsa = dsa;
  23. this.digest = digest;
  24. this.encoding = encoding;
  25. }
  26. public virtual string AlgorithmName
  27. {
  28. get { return digest.AlgorithmName + "with" + dsa.AlgorithmName; }
  29. }
  30. public virtual void Init(bool forSigning, ICipherParameters parameters)
  31. {
  32. this.forSigning = forSigning;
  33. AsymmetricKeyParameter k;
  34. if (parameters is ParametersWithRandom withRandom)
  35. {
  36. k = (AsymmetricKeyParameter)withRandom.Parameters;
  37. }
  38. else
  39. {
  40. k = (AsymmetricKeyParameter)parameters;
  41. }
  42. if (forSigning && !k.IsPrivate)
  43. throw new InvalidKeyException("Signing Requires Private Key.");
  44. if (!forSigning && k.IsPrivate)
  45. throw new InvalidKeyException("Verification Requires Public Key.");
  46. Reset();
  47. dsa.Init(forSigning, parameters);
  48. }
  49. public virtual void Update(byte input)
  50. {
  51. digest.Update(input);
  52. }
  53. public virtual void BlockUpdate(byte[] input, int inOff, int inLen)
  54. {
  55. digest.BlockUpdate(input, inOff, inLen);
  56. }
  57. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  58. public virtual void BlockUpdate(ReadOnlySpan<byte> input)
  59. {
  60. digest.BlockUpdate(input);
  61. }
  62. #endif
  63. public virtual byte[] GenerateSignature()
  64. {
  65. if (!forSigning)
  66. throw new InvalidOperationException("DSADigestSigner not initialised for signature generation.");
  67. byte[] hash = new byte[digest.GetDigestSize()];
  68. digest.DoFinal(hash, 0);
  69. BigInteger[] sig = dsa.GenerateSignature(hash);
  70. try
  71. {
  72. return encoding.Encode(GetOrder(), sig[0], sig[1]);
  73. }
  74. catch (Exception)
  75. {
  76. throw new InvalidOperationException("unable to encode signature");
  77. }
  78. }
  79. public virtual bool VerifySignature(byte[] signature)
  80. {
  81. if (forSigning)
  82. throw new InvalidOperationException("DSADigestSigner not initialised for verification");
  83. byte[] hash = new byte[digest.GetDigestSize()];
  84. digest.DoFinal(hash, 0);
  85. try
  86. {
  87. BigInteger[] sig = encoding.Decode(GetOrder(), signature);
  88. return dsa.VerifySignature(hash, sig[0], sig[1]);
  89. }
  90. catch (Exception)
  91. {
  92. return false;
  93. }
  94. }
  95. public virtual void Reset()
  96. {
  97. digest.Reset();
  98. }
  99. protected virtual BigInteger GetOrder()
  100. {
  101. return dsa.Order;
  102. }
  103. }
  104. }
  105. #pragma warning restore
  106. #endif