Ed25519ctxSigner.cs 4.3 KB

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