Ed25519PrivateKeyParameters.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.Math.EC.Rfc8032;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  9. namespace BestHTTP.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. public Ed25519PrivateKeyParameters(Stream input)
  33. : base(true)
  34. {
  35. if (KeySize != Streams.ReadFully(input, data))
  36. throw new EndOfStreamException("EOF encountered in middle of Ed25519 private key");
  37. }
  38. public void Encode(byte[] buf, int off)
  39. {
  40. Array.Copy(data, 0, buf, off, KeySize);
  41. }
  42. public byte[] GetEncoded()
  43. {
  44. return Arrays.Clone(data);
  45. }
  46. public Ed25519PublicKeyParameters GeneratePublicKey()
  47. {
  48. lock (data)
  49. {
  50. if (null == cachedPublicKey)
  51. {
  52. byte[] publicKey = new byte[Ed25519.PublicKeySize];
  53. Ed25519.GeneratePublicKey(data, 0, publicKey, 0);
  54. cachedPublicKey = new Ed25519PublicKeyParameters(publicKey, 0);
  55. }
  56. return cachedPublicKey;
  57. }
  58. }
  59. public void Sign(Ed25519.Algorithm algorithm, Ed25519PublicKeyParameters publicKey, byte[] ctx, byte[] msg, int msgOff, int msgLen,
  60. byte[] sig, int sigOff)
  61. {
  62. Sign(algorithm, ctx, msg, msgOff, msgLen, sig, sigOff);
  63. }
  64. public void Sign(Ed25519.Algorithm algorithm, byte[] ctx, byte[] msg, int msgOff, int msgLen,
  65. byte[] sig, int sigOff)
  66. {
  67. Ed25519PublicKeyParameters publicKey = GeneratePublicKey();
  68. byte[] pk = new byte[Ed25519.PublicKeySize];
  69. publicKey.Encode(pk, 0);
  70. switch (algorithm)
  71. {
  72. case Ed25519.Algorithm.Ed25519:
  73. {
  74. if (null != ctx)
  75. throw new ArgumentException("ctx");
  76. Ed25519.Sign(data, 0, pk, 0, msg, msgOff, msgLen, sig, sigOff);
  77. break;
  78. }
  79. case Ed25519.Algorithm.Ed25519ctx:
  80. {
  81. Ed25519.Sign(data, 0, pk, 0, ctx, msg, msgOff, msgLen, sig, sigOff);
  82. break;
  83. }
  84. case Ed25519.Algorithm.Ed25519ph:
  85. {
  86. if (Ed25519.PrehashSize != msgLen)
  87. throw new ArgumentException("msgLen");
  88. Ed25519.SignPrehash(data, 0, pk, 0, ctx, msg, msgOff, sig, sigOff);
  89. break;
  90. }
  91. default:
  92. {
  93. throw new ArgumentException("algorithm");
  94. }
  95. }
  96. }
  97. private static byte[] Validate(byte[] buf)
  98. {
  99. if (buf.Length != KeySize)
  100. throw new ArgumentException("must have length " + KeySize, "buf");
  101. return buf;
  102. }
  103. }
  104. }
  105. #pragma warning restore
  106. #endif