Ed448Signer.cs 4.2 KB

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