Ed25519Signer.cs 4.1 KB

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