< Summary

Information
Class: Renci.SshNet.Security.Cryptography.EcdsaDigitalSignature
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Cryptography\EcdsaDigitalSignature.cs
Line coverage
48%
Covered lines: 41
Uncovered lines: 44
Coverable lines: 85
Total lines: 187
Line coverage: 48.2%
Branch coverage
30%
Covered branches: 3
Total branches: 10
Branch coverage: 30%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)50%271.42%
Verify(...)0%20%
Sign(...)100%1100%
Dispose()100%10%
Dispose(...)50%444.44%
Finalize()100%1100%
get_Signature()100%10%
set_Signature(...)100%1100%
.ctor(...)100%1100%
.ctor(...)100%10%
LoadData()100%10%
SaveData()100%1100%
ReadBinary()0%20%
get_BufferCapacity()100%1100%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Cryptography\EcdsaDigitalSignature.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3#if NETFRAMEWORK
 4using System.Security.Cryptography;
 5#endif // NETFRAMEWORK
 6
 7using Renci.SshNet.Common;
 8
 9namespace Renci.SshNet.Security.Cryptography
 10{
 11    /// <summary>
 12    /// Implements ECDSA digital signature algorithm.
 13    /// </summary>
 14    public class EcdsaDigitalSignature : DigitalSignature, IDisposable
 15    {
 16        private readonly EcdsaKey _key;
 17        private bool _isDisposed;
 18
 19        /// <summary>
 20        /// Initializes a new instance of the <see cref="EcdsaDigitalSignature" /> class.
 21        /// </summary>
 22        /// <param name="key">The ECDSA key.</param>
 23        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
 3924        public EcdsaDigitalSignature(EcdsaKey key)
 3925        {
 3926            if (key is null)
 027            {
 028                throw new ArgumentNullException(nameof(key));
 29            }
 30
 3931            _key = key;
 3932        }
 33
 34        /// <summary>
 35        /// Verifies the signature.
 36        /// </summary>
 37        /// <param name="input">The input.</param>
 38        /// <param name="signature">The signature.</param>
 39        /// <returns>
 40        /// <see langword="true"/> if signature was successfully verified; otherwise <see langword="false"/>.
 41        /// </returns>
 42        public override bool Verify(byte[] input, byte[] signature)
 043        {
 44            // for 521 sig_size is 132
 045            var sig_size = _key.KeyLength == 521 ? 132 : _key.KeyLength / 4;
 046            var ssh_data = new SshDataSignature(signature, sig_size);
 47#if NETFRAMEWORK
 048            var ecdsa = (ECDsaCng)_key.Ecdsa;
 049            ecdsa.HashAlgorithm = _key.HashAlgorithm;
 050            return ecdsa.VerifyData(input, ssh_data.Signature);
 51#else
 052            return _key.Ecdsa.VerifyData(input, ssh_data.Signature, _key.HashAlgorithm);
 53#endif
 054        }
 55
 56        /// <summary>
 57        /// Creates the signature.
 58        /// </summary>
 59        /// <param name="input">The input.</param>
 60        /// <returns>
 61        /// Signed input data.
 62        /// </returns>
 63        public override byte[] Sign(byte[] input)
 364        {
 65#if NETFRAMEWORK
 066            var ecdsa = (ECDsaCng)_key.Ecdsa;
 067            ecdsa.HashAlgorithm = _key.HashAlgorithm;
 068            var signed = ecdsa.SignData(input);
 69#else
 370            var signed = _key.Ecdsa.SignData(input, _key.HashAlgorithm);
 71#endif
 372            var ssh_data = new SshDataSignature(signed.Length) { Signature = signed };
 373            return ssh_data.GetBytes();
 374        }
 75
 76        /// <summary>
 77        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 78        /// </summary>
 79        public void Dispose()
 080        {
 081            Dispose(disposing: true);
 082            GC.SuppressFinalize(this);
 083        }
 84
 85        /// <summary>
 86        /// Releases unmanaged and - optionally - managed resources.
 87        /// </summary>
 88        /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor
 89        protected virtual void Dispose(bool disposing)
 3990        {
 3991            if (_isDisposed)
 092            {
 093                return;
 94            }
 95
 3996            if (disposing)
 097            {
 098                _isDisposed = true;
 099            }
 39100        }
 101
 102        /// <summary>
 103        /// Finalizes an instance of the <see cref="EcdsaDigitalSignature"/> class.
 104        /// </summary>
 105        ~EcdsaDigitalSignature()
 78106        {
 39107            Dispose(disposing: false);
 78108        }
 109
 110        private sealed class SshDataSignature : SshData
 111        {
 112            private readonly int _signature_size;
 113
 114            private byte[] _signature_r;
 115            private byte[] _signature_s;
 116
 117            public byte[] Signature
 118            {
 119                get
 0120                {
 0121                    var signature = new byte[_signature_size];
 0122                    Buffer.BlockCopy(_signature_r, 0, signature, 0, _signature_r.Length);
 0123                    Buffer.BlockCopy(_signature_s, 0, signature, _signature_r.Length, _signature_s.Length);
 0124                    return signature;
 0125                }
 126                set
 3127                {
 3128                    var signed_r = new byte[_signature_size / 2];
 3129                    Buffer.BlockCopy(value, 0, signed_r, 0, signed_r.Length);
 3130                    _signature_r = signed_r.ToBigInteger2().ToByteArray().Reverse();
 131
 3132                    var signed_s = new byte[_signature_size / 2];
 3133                    Buffer.BlockCopy(value, signed_r.Length, signed_s, 0, signed_s.Length);
 3134                    _signature_s = signed_s.ToBigInteger2().ToByteArray().Reverse();
 3135                }
 136            }
 137
 3138            public SshDataSignature(int sig_size)
 3139            {
 3140                _signature_size = sig_size;
 3141            }
 142
 0143            public SshDataSignature(byte[] data, int sig_size)
 0144            {
 0145                _signature_size = sig_size;
 0146                Load(data);
 0147            }
 148
 149            protected override void LoadData()
 0150            {
 0151                _signature_r = ReadBinary().TrimLeadingZeros().Pad(_signature_size / 2);
 0152                _signature_s = ReadBinary().TrimLeadingZeros().Pad(_signature_size / 2);
 0153            }
 154
 155            protected override void SaveData()
 3156            {
 3157                WriteBinaryString(_signature_r.ToBigInteger2().ToByteArray().Reverse());
 3158                WriteBinaryString(_signature_s.ToBigInteger2().ToByteArray().Reverse());
 3159            }
 160
 161            public new byte[] ReadBinary()
 0162            {
 0163                var length = ReadUInt32();
 164
 0165                if (length > int.MaxValue)
 0166                {
 0167                    throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} i
 168                }
 169
 0170                return ReadBytes((int) length);
 0171            }
 172
 173            protected override int BufferCapacity
 174            {
 175                get
 3176                {
 3177                    var capacity = base.BufferCapacity;
 3178                    capacity += 4; // r length
 3179                    capacity += _signature_r.Length; // signature r
 3180                    capacity += 4; // s length
 3181                    capacity += _signature_s.Length; // signature s
 3182                    return capacity;
 3183                }
 184            }
 185        }
 186    }
 187}