GOST3410DigestSigner.cs 3.3 KB

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