EcdsaDigitalSignature
Implements ECDSA digital signature algorithm.
using Renci.SshNet.Common;
using System;
using System.Globalization;
namespace Renci.SshNet.Security.Cryptography
{
public class EcdsaDigitalSignature : DigitalSignature, IDisposable
{
private sealed class SshDataSignature : SshData
{
private readonly int _signature_size;
private byte[] _signature_r;
private byte[] _signature_s;
public byte[] Signature {
get {
byte[] array = new byte[_signature_size];
Buffer.BlockCopy(_signature_r, 0, array, 0, _signature_r.Length);
Buffer.BlockCopy(_signature_s, 0, array, _signature_r.Length, _signature_s.Length);
return array;
}
set {
byte[] array = new byte[_signature_size / 2];
Buffer.BlockCopy(value, 0, array, 0, array.Length);
BigInteger bigInteger = array.ToBigInteger2();
_signature_r = bigInteger.ToByteArray().Reverse();
byte[] array2 = new byte[_signature_size / 2];
Buffer.BlockCopy(value, array.Length, array2, 0, array2.Length);
bigInteger = array2.ToBigInteger2();
_signature_s = bigInteger.ToByteArray().Reverse();
}
}
protected override int BufferCapacity => base.BufferCapacity + 4 + _signature_r.Length + 4 + _signature_s.Length;
public SshDataSignature(int sig_size)
{
_signature_size = sig_size;
}
public SshDataSignature(byte[] data, int sig_size)
{
_signature_size = sig_size;
Load(data);
}
protected override void LoadData()
{
_signature_r = ReadBinary().TrimLeadingZeros().Pad(_signature_size / 2);
_signature_s = ReadBinary().TrimLeadingZeros().Pad(_signature_size / 2);
}
protected override void SaveData()
{
BigInteger bigInteger = _signature_r.ToBigInteger2();
WriteBinaryString(bigInteger.ToByteArray().Reverse());
bigInteger = _signature_s.ToBigInteger2();
WriteBinaryString(bigInteger.ToByteArray().Reverse());
}
public new byte[] ReadBinary()
{
uint num = ReadUInt32();
if (num > 2147483647)
throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is not supported.", 2147483647));
return ReadBytes((int)num);
}
}
private readonly EcdsaKey _key;
private bool _isDisposed;
public EcdsaDigitalSignature(EcdsaKey key)
{
if (key == null)
throw new ArgumentNullException("key");
_key = key;
}
public override bool Verify(byte[] input, byte[] signature)
{
int sig_size = (_key.KeyLength == 521) ? 132 : (_key.KeyLength / 4);
SshDataSignature sshDataSignature = new SshDataSignature(signature, sig_size);
return _key.Ecdsa.VerifyData(input, sshDataSignature.Signature, _key.HashAlgorithm);
}
public override byte[] Sign(byte[] input)
{
byte[] array = _key.Ecdsa.SignData(input, _key.HashAlgorithm);
return new SshDataSignature(array.Length) {
Signature = array
}.GetBytes();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_isDisposed && disposing)
_isDisposed = true;
}
~EcdsaDigitalSignature()
{
Dispose(false);
}
}
}