Ed448phSigner.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 Ed448phSigner
  11. : ISigner
  12. {
  13. private readonly IXof prehash = Ed448.CreatePrehash();
  14. private readonly byte[] context;
  15. private bool forSigning;
  16. private Ed448PrivateKeyParameters privateKey;
  17. private Ed448PublicKeyParameters publicKey;
  18. public Ed448phSigner(byte[] context)
  19. {
  20. this.context = Arrays.Clone(context);
  21. }
  22. public virtual string AlgorithmName
  23. {
  24. get { return "Ed448ph"; }
  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. prehash.Update(b);
  44. }
  45. public virtual void BlockUpdate(byte[] buf, int off, int len)
  46. {
  47. prehash.BlockUpdate(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. prehash.BlockUpdate(input);
  53. }
  54. #endif
  55. public virtual byte[] GenerateSignature()
  56. {
  57. if (!forSigning || null == privateKey)
  58. throw new InvalidOperationException("Ed448phSigner not initialised for signature generation.");
  59. byte[] msg = new byte[Ed448.PrehashSize];
  60. if (Ed448.PrehashSize != prehash.OutputFinal(msg, 0, Ed448.PrehashSize))
  61. throw new InvalidOperationException("Prehash digest failed");
  62. byte[] signature = new byte[Ed448PrivateKeyParameters.SignatureSize];
  63. privateKey.Sign(Ed448.Algorithm.Ed448ph, context, msg, 0, Ed448.PrehashSize, signature, 0);
  64. return signature;
  65. }
  66. public virtual bool VerifySignature(byte[] signature)
  67. {
  68. if (forSigning || null == publicKey)
  69. throw new InvalidOperationException("Ed448phSigner not initialised for verification");
  70. if (Ed448.SignatureSize != signature.Length)
  71. {
  72. prehash.Reset();
  73. return false;
  74. }
  75. byte[] pk = publicKey.GetEncoded();
  76. return Ed448.VerifyPrehash(signature, 0, pk, 0, context, prehash);
  77. }
  78. public void Reset()
  79. {
  80. prehash.Reset();
  81. }
  82. }
  83. }
  84. #pragma warning restore
  85. #endif