Ed448PrivateKeyParameters.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 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. public Ed448PrivateKeyParameters(Stream input)
  33. : base(true)
  34. {
  35. if (KeySize != Streams.ReadFully(input, data))
  36. throw new EndOfStreamException("EOF encountered in middle of Ed448 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 Ed448PublicKeyParameters GeneratePublicKey()
  47. {
  48. lock (data)
  49. {
  50. if (null == cachedPublicKey)
  51. {
  52. byte[] publicKey = new byte[Ed448.PublicKeySize];
  53. Ed448.GeneratePublicKey(data, 0, publicKey, 0);
  54. cachedPublicKey = new Ed448PublicKeyParameters(publicKey, 0);
  55. }
  56. return cachedPublicKey;
  57. }
  58. }
  59. public void Sign(Ed448.Algorithm algorithm, Ed448PublicKeyParameters 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(Ed448.Algorithm algorithm, byte[] ctx, byte[] msg, int msgOff, int msgLen,
  65. byte[] sig, int sigOff)
  66. {
  67. Ed448PublicKeyParameters publicKey = GeneratePublicKey();
  68. byte[] pk = new byte[Ed448.PublicKeySize];
  69. publicKey.Encode(pk, 0);
  70. switch (algorithm)
  71. {
  72. case Ed448.Algorithm.Ed448:
  73. {
  74. Ed448.Sign(data, 0, pk, 0, ctx, msg, msgOff, msgLen, sig, sigOff);
  75. break;
  76. }
  77. case Ed448.Algorithm.Ed448ph:
  78. {
  79. if (Ed448.PrehashSize != msgLen)
  80. throw new ArgumentException("msgLen");
  81. Ed448.SignPrehash(data, 0, pk, 0, ctx, msg, msgOff, sig, sigOff);
  82. break;
  83. }
  84. default:
  85. {
  86. throw new ArgumentException("algorithm");
  87. }
  88. }
  89. }
  90. private static byte[] Validate(byte[] buf)
  91. {
  92. if (buf.Length != KeySize)
  93. throw new ArgumentException("must have length " + KeySize, "buf");
  94. return buf;
  95. }
  96. }
  97. }
  98. #pragma warning restore
  99. #endif