EcdsaDigitalSignature.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using Renci.SshNet.Common;
  3. namespace Renci.SshNet.Security.Cryptography
  4. {
  5. /// <summary>
  6. /// Implements ECDSA digital signature algorithm.
  7. /// </summary>
  8. public class EcdsaDigitalSignature : DigitalSignature, IDisposable
  9. {
  10. private readonly EcdsaKey _key;
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="EcdsaDigitalSignature" /> class.
  13. /// </summary>
  14. /// <param name="key">The ECDSA key.</param>
  15. /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
  16. public EcdsaDigitalSignature(EcdsaKey key)
  17. {
  18. if (key is null)
  19. {
  20. throw new ArgumentNullException(nameof(key));
  21. }
  22. _key = key;
  23. }
  24. /// <summary>
  25. /// Verifies the signature.
  26. /// </summary>
  27. /// <param name="input">The input.</param>
  28. /// <param name="signature">The signature.</param>
  29. /// <returns>
  30. /// <see langword="true"/> if signature was successfully verified; otherwise <see langword="false"/>.
  31. /// </returns>
  32. public override bool Verify(byte[] input, byte[] signature)
  33. {
  34. // for 521 sig_size is 132
  35. var sig_size = _key.KeyLength == 521 ? 132 : _key.KeyLength / 4;
  36. var ssh_data = new SshDataSignature(signature, sig_size);
  37. return _key._impl.Verify(input, ssh_data.Signature);
  38. }
  39. /// <summary>
  40. /// Creates the signature.
  41. /// </summary>
  42. /// <param name="input">The input.</param>
  43. /// <returns>
  44. /// Signed input data.
  45. /// </returns>
  46. public override byte[] Sign(byte[] input)
  47. {
  48. var signed = _key._impl.Sign(input);
  49. var ssh_data = new SshDataSignature(signed.Length) { Signature = signed };
  50. return ssh_data.GetBytes();
  51. }
  52. /// <summary>
  53. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  54. /// </summary>
  55. public void Dispose()
  56. {
  57. Dispose(disposing: true);
  58. GC.SuppressFinalize(this);
  59. }
  60. /// <summary>
  61. /// Releases unmanaged and - optionally - managed resources.
  62. /// </summary>
  63. /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
  64. protected virtual void Dispose(bool disposing)
  65. {
  66. }
  67. private sealed class SshDataSignature : SshData
  68. {
  69. private readonly int _signature_size;
  70. private byte[] _signature_r;
  71. private byte[] _signature_s;
  72. public byte[] Signature
  73. {
  74. get
  75. {
  76. var signature = new byte[_signature_size];
  77. Buffer.BlockCopy(_signature_r, 0, signature, 0, _signature_r.Length);
  78. Buffer.BlockCopy(_signature_s, 0, signature, _signature_r.Length, _signature_s.Length);
  79. return signature;
  80. }
  81. set
  82. {
  83. var signed_r = new byte[_signature_size / 2];
  84. Buffer.BlockCopy(value, 0, signed_r, 0, signed_r.Length);
  85. _signature_r = signed_r.ToBigInteger2().ToByteArray(isBigEndian: true);
  86. var signed_s = new byte[_signature_size / 2];
  87. Buffer.BlockCopy(value, signed_r.Length, signed_s, 0, signed_s.Length);
  88. _signature_s = signed_s.ToBigInteger2().ToByteArray(isBigEndian: true);
  89. }
  90. }
  91. public SshDataSignature(int sig_size)
  92. {
  93. _signature_size = sig_size;
  94. }
  95. public SshDataSignature(byte[] data, int sig_size)
  96. {
  97. _signature_size = sig_size;
  98. Load(data);
  99. }
  100. protected override void LoadData()
  101. {
  102. _signature_r = ReadBinary().TrimLeadingZeros().Pad(_signature_size / 2);
  103. _signature_s = ReadBinary().TrimLeadingZeros().Pad(_signature_size / 2);
  104. }
  105. protected override void SaveData()
  106. {
  107. WriteBinaryString(_signature_r.ToBigInteger2().ToByteArray(isBigEndian: true));
  108. WriteBinaryString(_signature_s.ToBigInteger2().ToByteArray(isBigEndian: true));
  109. }
  110. protected override int BufferCapacity
  111. {
  112. get
  113. {
  114. var capacity = base.BufferCapacity;
  115. capacity += 4; // r length
  116. capacity += _signature_r.Length; // signature r
  117. capacity += 4; // s length
  118. capacity += _signature_s.Length; // signature s
  119. return capacity;
  120. }
  121. }
  122. }
  123. }
  124. }