GenericSigner.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Parameters;
  4. using Org.BouncyCastle.Security;
  5. using Org.BouncyCastle.Utilities;
  6. namespace Org.BouncyCastle.Crypto.Signers
  7. {
  8. public class GenericSigner
  9. : ISigner
  10. {
  11. private readonly IAsymmetricBlockCipher engine;
  12. private readonly IDigest digest;
  13. private bool forSigning;
  14. public GenericSigner(
  15. IAsymmetricBlockCipher engine,
  16. IDigest digest)
  17. {
  18. this.engine = engine;
  19. this.digest = digest;
  20. }
  21. public virtual string AlgorithmName
  22. {
  23. get { return "Generic(" + engine.AlgorithmName + "/" + digest.AlgorithmName + ")"; }
  24. }
  25. /**
  26. * initialise the signer for signing or verification.
  27. *
  28. * @param forSigning
  29. * true if for signing, false otherwise
  30. * @param parameters
  31. * necessary parameters.
  32. */
  33. public virtual void Init(bool forSigning, 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. throw new InvalidKeyException("Signing requires private key.");
  47. if (!forSigning && k.IsPrivate)
  48. throw new InvalidKeyException("Verification requires public key.");
  49. Reset();
  50. engine.Init(forSigning, parameters);
  51. }
  52. /**
  53. * update the internal digest with the byte b
  54. */
  55. public virtual void Update(byte input)
  56. {
  57. digest.Update(input);
  58. }
  59. /**
  60. * update the internal digest with the byte array in
  61. */
  62. public virtual void BlockUpdate(byte[] input, int inOff, int length)
  63. {
  64. digest.BlockUpdate(input, inOff, length);
  65. }
  66. /**
  67. * Generate a signature for the message we've been loaded with using the key
  68. * we were initialised with.
  69. */
  70. public virtual byte[] GenerateSignature()
  71. {
  72. if (!forSigning)
  73. throw new InvalidOperationException("GenericSigner not initialised for signature generation.");
  74. byte[] hash = new byte[digest.GetDigestSize()];
  75. digest.DoFinal(hash, 0);
  76. return engine.ProcessBlock(hash, 0, hash.Length);
  77. }
  78. /**
  79. * return true if the internal state represents the signature described in
  80. * the passed in array.
  81. */
  82. public virtual bool VerifySignature(byte[] signature)
  83. {
  84. if (forSigning)
  85. throw new InvalidOperationException("GenericSigner not initialised for verification");
  86. byte[] hash = new byte[digest.GetDigestSize()];
  87. digest.DoFinal(hash, 0);
  88. try
  89. {
  90. byte[] sig = engine.ProcessBlock(signature, 0, signature.Length);
  91. // Extend with leading zeroes to match the digest size, if necessary.
  92. if (sig.Length < hash.Length)
  93. {
  94. byte[] tmp = new byte[hash.Length];
  95. Array.Copy(sig, 0, tmp, tmp.Length - sig.Length, sig.Length);
  96. sig = tmp;
  97. }
  98. return Arrays.ConstantTimeAreEqual(sig, hash);
  99. }
  100. catch (Exception)
  101. {
  102. return false;
  103. }
  104. }
  105. public virtual void Reset()
  106. {
  107. digest.Reset();
  108. }
  109. }
  110. }
  111. #endif