< Summary

Information
Class: Renci.SshNet.Security.KeyExchangeECCurve25519
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\KeyExchangeECCurve25519.cs
Line coverage
97%
Covered lines: 35
Uncovered lines: 1
Coverable lines: 36
Total lines: 111
Line coverage: 97.2%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Name()100%1100%
get_HashSize()100%10%
Start(...)100%1100%
Finish()100%1100%
Hash(...)100%1100%
Session_KeyExchangeEcdhReplyMessageReceived(...)100%1100%
HandleServerEcdhReply(...)100%1100%

File(s)

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

#LineLine coverage
 1using Renci.SshNet.Abstractions;
 2using Renci.SshNet.Common;
 3using Renci.SshNet.Messages.Transport;
 4using Renci.SshNet.Security.Chaos.NaCl;
 5using Renci.SshNet.Security.Chaos.NaCl.Internal.Ed25519Ref10;
 6
 7namespace Renci.SshNet.Security
 8{
 9    internal sealed class KeyExchangeECCurve25519 : KeyExchangeEC
 10    {
 11        private byte[] _privateKey;
 12
 13        /// <summary>
 14        /// Gets algorithm name.
 15        /// </summary>
 16        public override string Name
 17        {
 351618            get { return "curve25519-sha256"; }
 19        }
 20
 21        /// <summary>
 22        /// Gets the size, in bits, of the computed hash code.
 23        /// </summary>
 24        /// <value>
 25        /// The size, in bits, of the computed hash code.
 26        /// </value>
 27        protected override int HashSize
 28        {
 029            get { return 256; }
 30        }
 31
 32        /// <summary>
 33        /// Starts key exchange algorithm.
 34        /// </summary>
 35        /// <param name="session">The session.</param>
 36        /// <param name="message">Key exchange init message.</param>
 37        public override void Start(Session session, KeyExchangeInitMessage message)
 117238        {
 117239            base.Start(session, message);
 40
 117241            Session.RegisterMessage("SSH_MSG_KEX_ECDH_REPLY");
 42
 117243            Session.KeyExchangeEcdhReplyMessageReceived += Session_KeyExchangeEcdhReplyMessageReceived;
 44
 117245            var basepoint = new byte[MontgomeryCurve25519.PublicKeySizeInBytes];
 117246            basepoint[0] = 9;
 47
 117248            _privateKey = CryptoAbstraction.GenerateRandom(MontgomeryCurve25519.PrivateKeySizeInBytes);
 49
 117250            _clientExchangeValue = new byte[MontgomeryCurve25519.PublicKeySizeInBytes];
 117251            MontgomeryOperations.scalarmult(_clientExchangeValue, 0, _privateKey, 0, basepoint, 0);
 52
 117253            SendMessage(new KeyExchangeEcdhInitMessage(_clientExchangeValue));
 117254        }
 55
 56        /// <summary>
 57        /// Finishes key exchange algorithm.
 58        /// </summary>
 59        public override void Finish()
 117260        {
 117261            base.Finish();
 62
 117163            Session.KeyExchangeEcdhReplyMessageReceived -= Session_KeyExchangeEcdhReplyMessageReceived;
 117164        }
 65
 66        /// <summary>
 67        /// Hashes the specified data bytes.
 68        /// </summary>
 69        /// <param name="hashData">The hash data.</param>
 70        /// <returns>
 71        /// The hash of the data.
 72        /// </returns>
 73        protected override byte[] Hash(byte[] hashData)
 937574        {
 937575            using (var sha256 = CryptoAbstraction.CreateSHA256())
 937576            {
 937577                return sha256.ComputeHash(hashData, 0, hashData.Length);
 78            }
 937579        }
 80
 81        private void Session_KeyExchangeEcdhReplyMessageReceived(object sender, MessageEventArgs<KeyExchangeEcdhReplyMes
 117282        {
 117283            var message = e.Message;
 84
 85            // Unregister message once received
 117286            Session.UnRegisterMessage("SSH_MSG_KEX_ECDH_REPLY");
 87
 117288            HandleServerEcdhReply(message.KS, message.QS, message.Signature);
 89
 90            // When SSH_MSG_KEXDH_REPLY received key exchange is completed
 117291            Finish();
 117192        }
 93
 94        /// <summary>
 95        /// Handles the server DH reply message.
 96        /// </summary>
 97        /// <param name="hostKey">The host key.</param>
 98        /// <param name="serverExchangeValue">The server exchange value.</param>
 99        /// <param name="signature">The signature.</param>
 100        private void HandleServerEcdhReply(byte[] hostKey, byte[] serverExchangeValue, byte[] signature)
 1172101        {
 1172102            _serverExchangeValue = serverExchangeValue;
 1172103            _hostKey = hostKey;
 1172104            _signature = signature;
 105
 1172106            var sharedKey = new byte[MontgomeryCurve25519.PublicKeySizeInBytes];
 1172107            MontgomeryOperations.scalarmult(sharedKey, 0, _privateKey, 0, serverExchangeValue, 0);
 1172108            SharedKey = sharedKey.ToBigInteger2().ToByteArray().Reverse();
 1172109        }
 110    }
 111}