Ed25519PrivateKeyParameters.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 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. public Ed25519PrivateKeyParameters(SecureRandom random)
  18. : base(true)
  19. {
  20. Ed25519.GeneratePrivateKey(random, data);
  21. }
  22. public Ed25519PrivateKeyParameters(byte[] buf, int off)
  23. : base(true)
  24. {
  25. Array.Copy(buf, off, data, 0, KeySize);
  26. }
  27. public Ed25519PrivateKeyParameters(Stream input)
  28. : base(true)
  29. {
  30. if (KeySize != Streams.ReadFully(input, data))
  31. throw new EndOfStreamException("EOF encountered in middle of Ed25519 private key");
  32. }
  33. public void Encode(byte[] buf, int off)
  34. {
  35. Array.Copy(data, 0, buf, off, KeySize);
  36. }
  37. public byte[] GetEncoded()
  38. {
  39. return Arrays.Clone(data);
  40. }
  41. public Ed25519PublicKeyParameters GeneratePublicKey()
  42. {
  43. byte[] publicKey = new byte[Ed25519.PublicKeySize];
  44. Ed25519.GeneratePublicKey(data, 0, publicKey, 0);
  45. return new Ed25519PublicKeyParameters(publicKey, 0);
  46. }
  47. public void Sign(Ed25519.Algorithm algorithm, Ed25519PublicKeyParameters publicKey, byte[] ctx, byte[] msg, int msgOff, int msgLen,
  48. byte[] sig, int sigOff)
  49. {
  50. byte[] pk = new byte[Ed25519.PublicKeySize];
  51. if (null == publicKey)
  52. {
  53. Ed25519.GeneratePublicKey(data, 0, pk, 0);
  54. }
  55. else
  56. {
  57. publicKey.Encode(pk, 0);
  58. }
  59. switch (algorithm)
  60. {
  61. case Ed25519.Algorithm.Ed25519:
  62. {
  63. if (null != ctx)
  64. throw new ArgumentException("ctx");
  65. Ed25519.Sign(data, 0, pk, 0, msg, msgOff, msgLen, sig, sigOff);
  66. break;
  67. }
  68. case Ed25519.Algorithm.Ed25519ctx:
  69. {
  70. Ed25519.Sign(data, 0, pk, 0, ctx, msg, msgOff, msgLen, sig, sigOff);
  71. break;
  72. }
  73. case Ed25519.Algorithm.Ed25519ph:
  74. {
  75. if (Ed25519.PrehashSize != msgLen)
  76. throw new ArgumentException("msgLen");
  77. Ed25519.SignPrehash(data, 0, pk, 0, ctx, msg, msgOff, sig, sigOff);
  78. break;
  79. }
  80. default:
  81. {
  82. throw new ArgumentException("algorithm");
  83. }
  84. }
  85. }
  86. }
  87. }
  88. #pragma warning restore
  89. #endif