< Summary

Information
Class: Renci.SshNet.Security.KeyExchangeDiffieHellman
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\KeyExchangeDiffieHellman.cs
Line coverage
86%
Covered lines: 25
Uncovered lines: 4
Coverable lines: 29
Total lines: 140
Line coverage: 86.2%
Branch coverage
62%
Covered branches: 5
Total branches: 8
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
ValidateExchangeHash()100%1100%
Start(...)100%1100%
PopulateClientExchangeValue()62.5%873.33%
HandleServerDhReply(...)100%1100%

File(s)

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

#LineLine coverage
 1using System;
 2
 3using Renci.SshNet.Common;
 4using Renci.SshNet.Messages.Transport;
 5
 6namespace Renci.SshNet.Security
 7{
 8    /// <summary>
 9    /// Represents base class for Diffie Hellman key exchange algorithm.
 10    /// </summary>
 11    internal abstract class KeyExchangeDiffieHellman : KeyExchange
 12    {
 13#pragma warning disable SA1401 // Fields should be private
 14        /// <summary>
 15        /// Specifies key exchange group number.
 16        /// </summary>
 17        protected BigInteger _group;
 18
 19        /// <summary>
 20        /// Specifies key exchange prime number.
 21        /// </summary>
 22        protected BigInteger _prime;
 23
 24        /// <summary>
 25        /// Specifies client payload.
 26        /// </summary>
 27        protected byte[] _clientPayload;
 28
 29        /// <summary>
 30        /// Specifies server payload.
 31        /// </summary>
 32        protected byte[] _serverPayload;
 33
 34        /// <summary>
 35        /// Specifies client exchange number.
 36        /// </summary>
 37        protected byte[] _clientExchangeValue;
 38
 39        /// <summary>
 40        /// Specifies server exchange number.
 41        /// </summary>
 42        protected byte[] _serverExchangeValue;
 43
 44        /// <summary>
 45        /// Specifies random generated number.
 46        /// </summary>
 47        protected BigInteger _privateExponent;
 48
 49        /// <summary>
 50        /// Specifies host key data.
 51        /// </summary>
 52        protected byte[] _hostKey;
 53
 54        /// <summary>
 55        /// Specifies signature data.
 56        /// </summary>
 57        protected byte[] _signature;
 58#pragma warning restore SA1401 // Fields should be private
 59
 60        /// <summary>
 61        /// Gets the size, in bits, of the computed hash code.
 62        /// </summary>
 63        /// <value>
 64        /// The size, in bits, of the computed hash code.
 65        /// </value>
 66        protected abstract int HashSize { get; }
 67
 68        /// <summary>
 69        /// Validates the exchange hash.
 70        /// </summary>
 71        /// <returns>
 72        /// true if exchange hash is valid; otherwise false.
 73        /// </returns>
 74        protected override bool ValidateExchangeHash()
 1875        {
 1876            return ValidateExchangeHash(_hostKey, _signature);
 1877        }
 78
 79        /// <summary>
 80        /// Starts key exchange algorithm.
 81        /// </summary>
 82        /// <param name="session">The session.</param>
 83        /// <param name="message">Key exchange init message.</param>
 84        public override void Start(Session session, KeyExchangeInitMessage message)
 1885        {
 1886            base.Start(session, message);
 87
 1888            _serverPayload = message.GetBytes();
 1889            _clientPayload = Session.ClientInitMessage.GetBytes();
 1890        }
 91
 92        /// <summary>
 93        /// Populates the client exchange value.
 94        /// </summary>
 95        protected void PopulateClientExchangeValue()
 1896        {
 1897            if (_group.IsZero)
 098            {
 099                throw new ArgumentNullException("_group");
 100            }
 101
 18102            if (_prime.IsZero)
 0103            {
 0104                throw new ArgumentNullException("_prime");
 105            }
 106
 107            // generate private exponent that is twice the hash size (RFC 4419) with a minimum
 108            // of 1024 bits (whatever is less)
 18109            var privateExponentSize = Math.Max(HashSize * 2, 1024);
 110
 111            BigInteger clientExchangeValue;
 112
 113            do
 18114            {
 115                // Create private component
 18116                _privateExponent = BigInteger.Random(privateExponentSize);
 117
 118                // Generate public component
 18119                clientExchangeValue = BigInteger.ModPow(_group, _privateExponent, _prime);
 18120            }
 18121            while (clientExchangeValue < 1 || clientExchangeValue > (_prime - 1));
 122
 18123            _clientExchangeValue = clientExchangeValue.ToByteArray().Reverse();
 18124        }
 125
 126        /// <summary>
 127        /// Handles the server DH reply message.
 128        /// </summary>
 129        /// <param name="hostKey">The host key.</param>
 130        /// <param name="serverExchangeValue">The server exchange value.</param>
 131        /// <param name="signature">The signature.</param>
 132        protected virtual void HandleServerDhReply(byte[] hostKey, byte[] serverExchangeValue, byte[] signature)
 18133        {
 18134            _serverExchangeValue = serverExchangeValue;
 18135            _hostKey = hostKey;
 18136            SharedKey = BigInteger.ModPow(serverExchangeValue.ToBigInteger(), _privateExponent, _prime).ToByteArray().Re
 18137            _signature = signature;
 18138        }
 139    }
 140}