GOST3410Signer.cs 3.5 KB

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