Ed448Signer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers
  9. {
  10. public class Ed448Signer
  11. : ISigner
  12. {
  13. private readonly Buffer buffer = new Buffer();
  14. private readonly byte[] context;
  15. private bool forSigning;
  16. private Ed448PrivateKeyParameters privateKey;
  17. private Ed448PublicKeyParameters publicKey;
  18. public Ed448Signer(byte[] context)
  19. {
  20. this.context = Arrays.Clone(context);
  21. }
  22. public virtual string AlgorithmName
  23. {
  24. get { return "Ed448"; }
  25. }
  26. public virtual void Init(bool forSigning, ICipherParameters parameters)
  27. {
  28. this.forSigning = forSigning;
  29. if (forSigning)
  30. {
  31. this.privateKey = (Ed448PrivateKeyParameters)parameters;
  32. this.publicKey = null;
  33. }
  34. else
  35. {
  36. this.privateKey = null;
  37. this.publicKey = (Ed448PublicKeyParameters)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. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  50. public virtual void BlockUpdate(ReadOnlySpan<byte> input)
  51. {
  52. buffer.Write(input);
  53. }
  54. #endif
  55. public virtual byte[] GenerateSignature()
  56. {
  57. if (!forSigning || null == privateKey)
  58. throw new InvalidOperationException("Ed448Signer not initialised for signature generation.");
  59. return buffer.GenerateSignature(privateKey, context);
  60. }
  61. public virtual bool VerifySignature(byte[] signature)
  62. {
  63. if (forSigning || null == publicKey)
  64. throw new InvalidOperationException("Ed448Signer not initialised for verification");
  65. return buffer.VerifySignature(publicKey, context, signature);
  66. }
  67. public virtual void Reset()
  68. {
  69. buffer.Reset();
  70. }
  71. private class Buffer : MemoryStream
  72. {
  73. internal byte[] GenerateSignature(Ed448PrivateKeyParameters privateKey, byte[] ctx)
  74. {
  75. lock (this)
  76. {
  77. byte[] buf = GetBuffer();
  78. int count = Convert.ToInt32(Length);
  79. byte[] signature = new byte[Ed448PrivateKeyParameters.SignatureSize];
  80. privateKey.Sign(Ed448.Algorithm.Ed448, ctx, buf, 0, count, signature, 0);
  81. Reset();
  82. return signature;
  83. }
  84. }
  85. internal bool VerifySignature(Ed448PublicKeyParameters publicKey, byte[] ctx, byte[] signature)
  86. {
  87. if (Ed448.SignatureSize != signature.Length)
  88. {
  89. Reset();
  90. return false;
  91. }
  92. lock (this)
  93. {
  94. byte[] buf = GetBuffer();
  95. int count = Convert.ToInt32(Length);
  96. byte[] pk = publicKey.GetEncoded();
  97. bool result = Ed448.Verify(signature, 0, pk, 0, ctx, buf, 0, count);
  98. Reset();
  99. return result;
  100. }
  101. }
  102. internal void Reset()
  103. {
  104. lock (this)
  105. {
  106. int count = Convert.ToInt32(Length);
  107. Array.Clear(GetBuffer(), 0, count);
  108. SetLength(0);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. #pragma warning restore
  115. #endif