GOST3410DigestSigner.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Text;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  12. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers
  13. {
  14. public class Gost3410DigestSigner
  15. : ISigner
  16. {
  17. private readonly IDigest digest;
  18. private readonly IDsa dsaSigner;
  19. private readonly int size;
  20. private int halfSize;
  21. private bool forSigning;
  22. public Gost3410DigestSigner(
  23. IDsa signer,
  24. IDigest digest)
  25. {
  26. this.dsaSigner = signer;
  27. this.digest = digest;
  28. halfSize = digest.GetDigestSize();
  29. this.size = halfSize * 2;
  30. }
  31. public virtual string AlgorithmName
  32. {
  33. get { return digest.AlgorithmName + "with" + dsaSigner.AlgorithmName; }
  34. }
  35. public virtual void Init(
  36. bool forSigning,
  37. ICipherParameters parameters)
  38. {
  39. this.forSigning = forSigning;
  40. AsymmetricKeyParameter k;
  41. if (parameters is ParametersWithRandom)
  42. {
  43. k = (AsymmetricKeyParameter)((ParametersWithRandom)parameters).Parameters;
  44. }
  45. else
  46. {
  47. k = (AsymmetricKeyParameter)parameters;
  48. }
  49. if (forSigning && !k.IsPrivate)
  50. {
  51. throw new InvalidKeyException("Signing Requires Private Key.");
  52. }
  53. if (!forSigning && k.IsPrivate)
  54. {
  55. throw new InvalidKeyException("Verification Requires Public Key.");
  56. }
  57. Reset();
  58. dsaSigner.Init(forSigning, parameters);
  59. }
  60. /**
  61. * update the internal digest with the byte b
  62. */
  63. public virtual void Update(
  64. byte input)
  65. {
  66. digest.Update(input);
  67. }
  68. /**
  69. * update the internal digest with the byte array in
  70. */
  71. public virtual void BlockUpdate(
  72. byte[] input,
  73. int inOff,
  74. int length)
  75. {
  76. digest.BlockUpdate(input, inOff, length);
  77. }
  78. /**
  79. * Generate a signature for the message we've been loaded with using
  80. * the key we were initialised with.
  81. */
  82. public virtual byte[] GenerateSignature()
  83. {
  84. if (!forSigning)
  85. throw new InvalidOperationException("GOST3410DigestSigner not initialised for signature generation.");
  86. byte[] hash = new byte[digest.GetDigestSize()];
  87. digest.DoFinal(hash, 0);
  88. try
  89. {
  90. BigInteger[] sig = dsaSigner.GenerateSignature(hash);
  91. byte[] sigBytes = new byte[size];
  92. // TODO Add methods to allow writing BigInteger to existing byte array?
  93. byte[] r = sig[0].ToByteArrayUnsigned();
  94. byte[] s = sig[1].ToByteArrayUnsigned();
  95. s.CopyTo(sigBytes, halfSize - s.Length);
  96. r.CopyTo(sigBytes, size - r.Length);
  97. return sigBytes;
  98. }
  99. catch (Exception e)
  100. {
  101. throw new SignatureException(e.Message, e);
  102. }
  103. }
  104. /// <returns>true if the internal state represents the signature described in the passed in array.</returns>
  105. public virtual bool VerifySignature(
  106. byte[] signature)
  107. {
  108. if (forSigning)
  109. throw new InvalidOperationException("DSADigestSigner not initialised for verification");
  110. byte[] hash = new byte[digest.GetDigestSize()];
  111. digest.DoFinal(hash, 0);
  112. BigInteger R, S;
  113. try
  114. {
  115. R = new BigInteger(1, signature, halfSize, halfSize);
  116. S = new BigInteger(1, signature, 0, halfSize);
  117. }
  118. catch (Exception e)
  119. {
  120. throw new SignatureException("error decoding signature bytes.", e);
  121. }
  122. return dsaSigner.VerifySignature(hash, R, S);
  123. }
  124. /// <summary>Reset the internal state</summary>
  125. public virtual void Reset()
  126. {
  127. digest.Reset();
  128. }
  129. }
  130. }
  131. #pragma warning restore
  132. #endif