Ed25519PrivateKeyParameters.cs 4.5 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 Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Rfc8032;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  9. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  10. {
  11. public sealed class Ed25519PrivateKeyParameters
  12. : AsymmetricKeyParameter
  13. {
  14. public static readonly int KeySize = Ed25519.SecretKeySize;
  15. public static readonly int SignatureSize = Ed25519.SignatureSize;
  16. private readonly byte[] data = new byte[KeySize];
  17. private Ed25519PublicKeyParameters cachedPublicKey;
  18. public Ed25519PrivateKeyParameters(SecureRandom random)
  19. : base(true)
  20. {
  21. Ed25519.GeneratePrivateKey(random, data);
  22. }
  23. public Ed25519PrivateKeyParameters(byte[] buf)
  24. : this(Validate(buf), 0)
  25. {
  26. }
  27. public Ed25519PrivateKeyParameters(byte[] buf, int off)
  28. : base(true)
  29. {
  30. Array.Copy(buf, off, data, 0, KeySize);
  31. }
  32. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  33. public Ed25519PrivateKeyParameters(ReadOnlySpan<byte> buf)
  34. : base(true)
  35. {
  36. if (buf.Length != KeySize)
  37. throw new ArgumentException("must have length " + KeySize, nameof(buf));
  38. buf.CopyTo(data);
  39. }
  40. #endif
  41. public Ed25519PrivateKeyParameters(Stream input)
  42. : base(true)
  43. {
  44. if (KeySize != Streams.ReadFully(input, data))
  45. throw new EndOfStreamException("EOF encountered in middle of Ed25519 private key");
  46. }
  47. public void Encode(byte[] buf, int off)
  48. {
  49. Array.Copy(data, 0, buf, off, KeySize);
  50. }
  51. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  52. public void Encode(Span<byte> buf)
  53. {
  54. data.CopyTo(buf);
  55. }
  56. #endif
  57. public byte[] GetEncoded()
  58. {
  59. return Arrays.Clone(data);
  60. }
  61. public Ed25519PublicKeyParameters GeneratePublicKey()
  62. {
  63. lock (data)
  64. {
  65. if (null == cachedPublicKey)
  66. {
  67. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  68. Span<byte> publicKey = stackalloc byte[Ed25519.PublicKeySize];
  69. Ed25519.GeneratePublicKey(data, publicKey);
  70. cachedPublicKey = new Ed25519PublicKeyParameters(publicKey);
  71. #else
  72. byte[] publicKey = new byte[Ed25519.PublicKeySize];
  73. Ed25519.GeneratePublicKey(data, 0, publicKey, 0);
  74. cachedPublicKey = new Ed25519PublicKeyParameters(publicKey, 0);
  75. #endif
  76. }
  77. return cachedPublicKey;
  78. }
  79. }
  80. public void Sign(Ed25519.Algorithm algorithm, byte[] ctx, byte[] msg, int msgOff, int msgLen,
  81. byte[] sig, int sigOff)
  82. {
  83. Ed25519PublicKeyParameters publicKey = GeneratePublicKey();
  84. byte[] pk = new byte[Ed25519.PublicKeySize];
  85. publicKey.Encode(pk, 0);
  86. switch (algorithm)
  87. {
  88. case Ed25519.Algorithm.Ed25519:
  89. {
  90. if (null != ctx)
  91. throw new ArgumentException("ctx");
  92. Ed25519.Sign(data, 0, pk, 0, msg, msgOff, msgLen, sig, sigOff);
  93. break;
  94. }
  95. case Ed25519.Algorithm.Ed25519ctx:
  96. {
  97. Ed25519.Sign(data, 0, pk, 0, ctx, msg, msgOff, msgLen, sig, sigOff);
  98. break;
  99. }
  100. case Ed25519.Algorithm.Ed25519ph:
  101. {
  102. if (Ed25519.PrehashSize != msgLen)
  103. throw new ArgumentException("msgLen");
  104. Ed25519.SignPrehash(data, 0, pk, 0, ctx, msg, msgOff, sig, sigOff);
  105. break;
  106. }
  107. default:
  108. {
  109. throw new ArgumentException("algorithm");
  110. }
  111. }
  112. }
  113. private static byte[] Validate(byte[] buf)
  114. {
  115. if (buf.Length != KeySize)
  116. throw new ArgumentException("must have length " + KeySize, nameof(buf));
  117. return buf;
  118. }
  119. }
  120. }
  121. #pragma warning restore
  122. #endif