GOST3410DigestSigner.cs 3.5 KB

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