Ed25519Signer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // TODO Allow IAsymmetricCipherKeyPair to be an ICipherParameters?
  31. this.privateKey = (Ed25519PrivateKeyParameters)parameters;
  32. this.publicKey = privateKey.GeneratePublicKey();
  33. }
  34. else
  35. {
  36. this.privateKey = null;
  37. this.publicKey = (Ed25519PublicKeyParameters)parameters;
  38. }
  39. Reset();
  40. }
  41. public virtual void Update(byte b)
  42. {
  43. buffer.WriteByte(b);
  44. }
  45. public virtual void BlockUpdate(byte[] buf, int off, int len)
  46. {
  47. buffer.Write(buf, off, len);
  48. }
  49. public virtual byte[] GenerateSignature()
  50. {
  51. if (!forSigning || null == privateKey)
  52. throw new InvalidOperationException("Ed25519Signer not initialised for signature generation.");
  53. return buffer.GenerateSignature(privateKey, publicKey);
  54. }
  55. public virtual bool VerifySignature(byte[] signature)
  56. {
  57. if (forSigning || null == publicKey)
  58. throw new InvalidOperationException("Ed25519Signer not initialised for verification");
  59. return buffer.VerifySignature(publicKey, signature);
  60. }
  61. public virtual void Reset()
  62. {
  63. buffer.Reset();
  64. }
  65. private class Buffer : MemoryStream
  66. {
  67. internal byte[] GenerateSignature(Ed25519PrivateKeyParameters privateKey, Ed25519PublicKeyParameters publicKey)
  68. {
  69. lock (this)
  70. {
  71. #if PORTABLE || NETFX_CORE
  72. byte[] buf = ToArray();
  73. int count = buf.Length;
  74. #else
  75. byte[] buf = GetBuffer();
  76. int count = (int)Position;
  77. #endif
  78. byte[] signature = new byte[Ed25519PrivateKeyParameters.SignatureSize];
  79. privateKey.Sign(Ed25519.Algorithm.Ed25519, publicKey, null, buf, 0, count, signature, 0);
  80. Reset();
  81. return signature;
  82. }
  83. }
  84. internal bool VerifySignature(Ed25519PublicKeyParameters publicKey, byte[] signature)
  85. {
  86. lock (this)
  87. {
  88. #if PORTABLE || NETFX_CORE
  89. byte[] buf = ToArray();
  90. int count = buf.Length;
  91. #else
  92. byte[] buf = GetBuffer();
  93. int count = (int)Position;
  94. #endif
  95. byte[] pk = publicKey.GetEncoded();
  96. bool result = Ed25519.Verify(signature, 0, pk, 0, buf, 0, count);
  97. Reset();
  98. return result;
  99. }
  100. }
  101. internal void Reset()
  102. {
  103. lock (this)
  104. {
  105. long count = Position;
  106. #if PORTABLE || NETFX_CORE
  107. this.Position = 0L;
  108. Streams.WriteZeroes(this, count);
  109. #else
  110. Array.Clear(GetBuffer(), 0, (int)count);
  111. #endif
  112. this.Position = 0L;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. #pragma warning restore
  119. #endif