Ed448PrivateKeyParameters.cs 2.8 KB

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