DsaDigestSigner.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. namespace BestHTTP.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(
  17. IDsa dsa,
  18. IDigest digest)
  19. {
  20. this.dsa = dsa;
  21. this.digest = digest;
  22. this.encoding = StandardDsaEncoding.Instance;
  23. }
  24. public DsaDigestSigner(
  25. IDsaExt dsa,
  26. IDigest digest,
  27. IDsaEncoding encoding)
  28. {
  29. this.dsa = dsa;
  30. this.digest = digest;
  31. this.encoding = encoding;
  32. }
  33. public virtual string AlgorithmName
  34. {
  35. get { return digest.AlgorithmName + "with" + dsa.AlgorithmName; }
  36. }
  37. public virtual void Init(
  38. bool forSigning,
  39. ICipherParameters parameters)
  40. {
  41. this.forSigning = forSigning;
  42. AsymmetricKeyParameter k;
  43. if (parameters is ParametersWithRandom)
  44. {
  45. k = (AsymmetricKeyParameter)((ParametersWithRandom)parameters).Parameters;
  46. }
  47. else
  48. {
  49. k = (AsymmetricKeyParameter)parameters;
  50. }
  51. if (forSigning && !k.IsPrivate)
  52. throw new InvalidKeyException("Signing Requires Private Key.");
  53. if (!forSigning && k.IsPrivate)
  54. throw new InvalidKeyException("Verification Requires Public Key.");
  55. Reset();
  56. dsa.Init(forSigning, parameters);
  57. }
  58. /**
  59. * update the internal digest with the byte b
  60. */
  61. public virtual void Update(
  62. byte input)
  63. {
  64. digest.Update(input);
  65. }
  66. /**
  67. * update the internal digest with the byte array in
  68. */
  69. public virtual void BlockUpdate(
  70. byte[] input,
  71. int inOff,
  72. int length)
  73. {
  74. digest.BlockUpdate(input, inOff, length);
  75. }
  76. /**
  77. * Generate a signature for the message we've been loaded with using
  78. * the key we were initialised with.
  79. */
  80. public virtual byte[] GenerateSignature()
  81. {
  82. if (!forSigning)
  83. throw new InvalidOperationException("DSADigestSigner not initialised for signature generation.");
  84. byte[] hash = new byte[digest.GetDigestSize()];
  85. digest.DoFinal(hash, 0);
  86. BigInteger[] sig = dsa.GenerateSignature(hash);
  87. try
  88. {
  89. return encoding.Encode(GetOrder(), sig[0], sig[1]);
  90. }
  91. catch (Exception)
  92. {
  93. throw new InvalidOperationException("unable to encode signature");
  94. }
  95. }
  96. /// <returns>true if the internal state represents the signature described in the passed in array.</returns>
  97. public virtual bool VerifySignature(
  98. byte[] signature)
  99. {
  100. if (forSigning)
  101. throw new InvalidOperationException("DSADigestSigner not initialised for verification");
  102. byte[] hash = new byte[digest.GetDigestSize()];
  103. digest.DoFinal(hash, 0);
  104. try
  105. {
  106. BigInteger[] sig = encoding.Decode(GetOrder(), signature);
  107. return dsa.VerifySignature(hash, sig[0], sig[1]);
  108. }
  109. catch (Exception)
  110. {
  111. return false;
  112. }
  113. }
  114. /// <summary>Reset the internal state</summary>
  115. public virtual void Reset()
  116. {
  117. digest.Reset();
  118. }
  119. protected virtual BigInteger GetOrder()
  120. {
  121. return dsa is IDsaExt ? ((IDsaExt)dsa).Order : null;
  122. }
  123. }
  124. }
  125. #pragma warning restore
  126. #endif