GOST3410Signer.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  10. * Gost R 34.10-94 Signature Algorithm
  11. */
  12. public class Gost3410Signer
  13. : IDsaExt
  14. {
  15. private Gost3410KeyParameters key;
  16. private SecureRandom random;
  17. public virtual string AlgorithmName
  18. {
  19. get { return "GOST3410"; }
  20. }
  21. public virtual void Init(
  22. bool forSigning,
  23. ICipherParameters parameters)
  24. {
  25. if (forSigning)
  26. {
  27. if (parameters is ParametersWithRandom)
  28. {
  29. ParametersWithRandom rParam = (ParametersWithRandom)parameters;
  30. this.random = rParam.Random;
  31. parameters = rParam.Parameters;
  32. }
  33. else
  34. {
  35. this.random = new SecureRandom();
  36. }
  37. if (!(parameters is Gost3410PrivateKeyParameters))
  38. throw new InvalidKeyException("GOST3410 private key required for signing");
  39. this.key = (Gost3410PrivateKeyParameters) parameters;
  40. }
  41. else
  42. {
  43. if (!(parameters is Gost3410PublicKeyParameters))
  44. throw new InvalidKeyException("GOST3410 public key required for signing");
  45. this.key = (Gost3410PublicKeyParameters) parameters;
  46. }
  47. }
  48. public virtual BigInteger Order
  49. {
  50. get { return key.Parameters.Q; }
  51. }
  52. /**
  53. * generate a signature for the given message using the key we were
  54. * initialised with. For conventional Gost3410 the message should be a Gost3411
  55. * hash of the message of interest.
  56. *
  57. * @param message the message that will be verified later.
  58. */
  59. public virtual BigInteger[] GenerateSignature(
  60. byte[] message)
  61. {
  62. byte[] mRev = new byte[message.Length]; // conversion is little-endian
  63. for (int i = 0; i != mRev.Length; i++)
  64. {
  65. mRev[i] = message[mRev.Length - 1 - i];
  66. }
  67. BigInteger m = new BigInteger(1, mRev);
  68. Gost3410Parameters parameters = key.Parameters;
  69. BigInteger k;
  70. do
  71. {
  72. k = new BigInteger(parameters.Q.BitLength, random);
  73. }
  74. while (k.CompareTo(parameters.Q) >= 0);
  75. BigInteger r = parameters.A.ModPow(k, parameters.P).Mod(parameters.Q);
  76. BigInteger s = k.Multiply(m).
  77. Add(((Gost3410PrivateKeyParameters)key).X.Multiply(r)).
  78. Mod(parameters.Q);
  79. return new BigInteger[]{ r, s };
  80. }
  81. /**
  82. * return true if the value r and s represent a Gost3410 signature for
  83. * the passed in message for standard Gost3410 the message should be a
  84. * Gost3411 hash of the real message to be verified.
  85. */
  86. public virtual bool VerifySignature(
  87. byte[] message,
  88. BigInteger r,
  89. BigInteger s)
  90. {
  91. byte[] mRev = new byte[message.Length]; // conversion is little-endian
  92. for (int i = 0; i != mRev.Length; i++)
  93. {
  94. mRev[i] = message[mRev.Length - 1 - i];
  95. }
  96. BigInteger m = new BigInteger(1, mRev);
  97. Gost3410Parameters parameters = key.Parameters;
  98. if (r.SignValue < 0 || parameters.Q.CompareTo(r) <= 0)
  99. {
  100. return false;
  101. }
  102. if (s.SignValue < 0 || parameters.Q.CompareTo(s) <= 0)
  103. {
  104. return false;
  105. }
  106. BigInteger v = m.ModPow(parameters.Q.Subtract(BigInteger.Two), parameters.Q);
  107. BigInteger z1 = s.Multiply(v).Mod(parameters.Q);
  108. BigInteger z2 = (parameters.Q.Subtract(r)).Multiply(v).Mod(parameters.Q);
  109. z1 = parameters.A.ModPow(z1, parameters.P);
  110. z2 = ((Gost3410PublicKeyParameters)key).Y.ModPow(z2, parameters.P);
  111. BigInteger u = z1.Multiply(z2).Mod(parameters.P).Mod(parameters.Q);
  112. return u.Equals(r);
  113. }
  114. }
  115. }
  116. #pragma warning restore
  117. #endif