Ed25519Signer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Rfc8032;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers
  8. {
  9. public class Ed25519Signer
  10. : ISigner
  11. {
  12. private readonly Buffer buffer = new Buffer();
  13. private bool forSigning;
  14. private Ed25519PrivateKeyParameters privateKey;
  15. private Ed25519PublicKeyParameters publicKey;
  16. public Ed25519Signer()
  17. {
  18. }
  19. public virtual string AlgorithmName
  20. {
  21. get { return "Ed25519"; }
  22. }
  23. public virtual void Init(bool forSigning, ICipherParameters parameters)
  24. {
  25. this.forSigning = forSigning;
  26. if (forSigning)
  27. {
  28. this.privateKey = (Ed25519PrivateKeyParameters)parameters;
  29. this.publicKey = null;
  30. }
  31. else
  32. {
  33. this.privateKey = null;
  34. this.publicKey = (Ed25519PublicKeyParameters)parameters;
  35. }
  36. Reset();
  37. }
  38. public virtual void Update(byte b)
  39. {
  40. buffer.WriteByte(b);
  41. }
  42. public virtual void BlockUpdate(byte[] buf, int off, int len)
  43. {
  44. buffer.Write(buf, off, len);
  45. }
  46. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  47. public virtual void BlockUpdate(ReadOnlySpan<byte> input)
  48. {
  49. buffer.Write(input);
  50. }
  51. #endif
  52. public virtual byte[] GenerateSignature()
  53. {
  54. if (!forSigning || null == privateKey)
  55. throw new InvalidOperationException("Ed25519Signer not initialised for signature generation.");
  56. return buffer.GenerateSignature(privateKey);
  57. }
  58. public virtual bool VerifySignature(byte[] signature)
  59. {
  60. if (forSigning || null == publicKey)
  61. throw new InvalidOperationException("Ed25519Signer not initialised for verification");
  62. return buffer.VerifySignature(publicKey, signature);
  63. }
  64. public virtual void Reset()
  65. {
  66. buffer.Reset();
  67. }
  68. private class Buffer : MemoryStream
  69. {
  70. internal byte[] GenerateSignature(Ed25519PrivateKeyParameters privateKey)
  71. {
  72. lock (this)
  73. {
  74. byte[] buf = GetBuffer();
  75. int count = Convert.ToInt32(Length);
  76. byte[] signature = new byte[Ed25519PrivateKeyParameters.SignatureSize];
  77. privateKey.Sign(Ed25519.Algorithm.Ed25519, null, buf, 0, count, signature, 0);
  78. Reset();
  79. return signature;
  80. }
  81. }
  82. internal bool VerifySignature(Ed25519PublicKeyParameters publicKey, byte[] signature)
  83. {
  84. if (Ed25519.SignatureSize != signature.Length)
  85. {
  86. Reset();
  87. return false;
  88. }
  89. lock (this)
  90. {
  91. byte[] buf = GetBuffer();
  92. int count = Convert.ToInt32(Length);
  93. byte[] pk = publicKey.GetEncoded();
  94. bool result = Ed25519.Verify(signature, 0, pk, 0, buf, 0, count);
  95. Reset();
  96. return result;
  97. }
  98. }
  99. internal void Reset()
  100. {
  101. lock (this)
  102. {
  103. int count = Convert.ToInt32(Length);
  104. Array.Clear(GetBuffer(), 0, count);
  105. SetLength(0);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. #pragma warning restore
  112. #endif