Ed448PrivateKeyParameters.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 Ed448PrivateKeyParameters
  12. : AsymmetricKeyParameter
  13. {
  14. public static readonly int KeySize = Ed448.SecretKeySize;
  15. public static readonly int SignatureSize = Ed448.SignatureSize;
  16. private readonly byte[] data = new byte[KeySize];
  17. private Ed448PublicKeyParameters cachedPublicKey;
  18. public Ed448PrivateKeyParameters(SecureRandom random)
  19. : base(true)
  20. {
  21. Ed448.GeneratePrivateKey(random, data);
  22. }
  23. public Ed448PrivateKeyParameters(byte[] buf)
  24. : this(Validate(buf), 0)
  25. {
  26. }
  27. public Ed448PrivateKeyParameters(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 Ed448PrivateKeyParameters(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 Ed448PrivateKeyParameters(Stream input)
  42. : base(true)
  43. {
  44. if (KeySize != Streams.ReadFully(input, data))
  45. throw new EndOfStreamException("EOF encountered in middle of Ed448 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 Ed448PublicKeyParameters 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[Ed448.PublicKeySize];
  69. Ed448.GeneratePublicKey(data, publicKey);
  70. cachedPublicKey = new Ed448PublicKeyParameters(publicKey);
  71. #else
  72. byte[] publicKey = new byte[Ed448.PublicKeySize];
  73. Ed448.GeneratePublicKey(data, 0, publicKey, 0);
  74. cachedPublicKey = new Ed448PublicKeyParameters(publicKey, 0);
  75. #endif
  76. }
  77. return cachedPublicKey;
  78. }
  79. }
  80. public void Sign(Ed448.Algorithm algorithm, byte[] ctx, byte[] msg, int msgOff, int msgLen,
  81. byte[] sig, int sigOff)
  82. {
  83. Ed448PublicKeyParameters publicKey = GeneratePublicKey();
  84. byte[] pk = new byte[Ed448.PublicKeySize];
  85. publicKey.Encode(pk, 0);
  86. switch (algorithm)
  87. {
  88. case Ed448.Algorithm.Ed448:
  89. {
  90. Ed448.Sign(data, 0, pk, 0, ctx, msg, msgOff, msgLen, sig, sigOff);
  91. break;
  92. }
  93. case Ed448.Algorithm.Ed448ph:
  94. {
  95. if (Ed448.PrehashSize != msgLen)
  96. throw new ArgumentException("msgLen");
  97. Ed448.SignPrehash(data, 0, pk, 0, ctx, msg, msgOff, sig, sigOff);
  98. break;
  99. }
  100. default:
  101. {
  102. throw new ArgumentException("algorithm");
  103. }
  104. }
  105. }
  106. private static byte[] Validate(byte[] buf)
  107. {
  108. if (buf.Length != KeySize)
  109. throw new ArgumentException("must have length " + KeySize, nameof(buf));
  110. return buf;
  111. }
  112. }
  113. }
  114. #pragma warning restore
  115. #endif