< Summary

Information
Class: Renci.SshNet.Security.Chaos.NaCl.Ed25519
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Chaos.NaCl\Ed25519.cs
Line coverage
31%
Covered lines: 35
Uncovered lines: 75
Coverable lines: 110
Total lines: 147
Line coverage: 31.8%
Branch coverage
23%
Covered branches: 12
Total branches: 52
Branch coverage: 23%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.cctor()100%1100%
Verify(...)0%40%
Verify(...)50%1061.53%
Sign(...)50%1061.53%
Sign(...)100%1100%
PublicKeyFromSeed(...)100%10%
ExpandedPrivateKeyFromSeed(...)100%10%
KeyPairFromSeed(...)50%481.81%
KeyPairFromSeed(...)0%120%
KeyExchange(...)100%10%
KeyExchange(...)0%120%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Chaos.NaCl\Ed25519.cs

#LineLine coverage
 1using System;
 2using Renci.SshNet.Security.Chaos.NaCl.Internal.Ed25519Ref10;
 3
 4namespace Renci.SshNet.Security.Chaos.NaCl
 5{
 6    internal static class Ed25519
 7    {
 48        public static readonly int PublicKeySizeInBytes = 32;
 49        public static readonly int SignatureSizeInBytes = 64;
 410        public static readonly int ExpandedPrivateKeySizeInBytes = 32 * 2;
 411        public static readonly int PrivateKeySeedSizeInBytes = 32;
 412        public static readonly int SharedKeySizeInBytes = 32;
 13
 14        public static bool Verify(ArraySegment<byte> signature, ArraySegment<byte> message, ArraySegment<byte> publicKey
 015        {
 016            if (signature.Count != SignatureSizeInBytes)
 017                throw new ArgumentException(string.Format("Signature size must be {0}", SignatureSizeInBytes), "signatur
 018            if (publicKey.Count != PublicKeySizeInBytes)
 019                throw new ArgumentException(string.Format("Public key size must be {0}", PublicKeySizeInBytes), "publicK
 020            return Ed25519Operations.crypto_sign_verify(signature.Array, signature.Offset, message.Array, message.Offset
 021        }
 22
 23        public static bool Verify(byte[] signature, byte[] message, byte[] publicKey)
 324        {
 325            if (signature == null)
 026                throw new ArgumentNullException("signature");
 327            if (message == null)
 028                throw new ArgumentNullException("message");
 329            if (publicKey == null)
 030                throw new ArgumentNullException("publicKey");
 331            if (signature.Length != SignatureSizeInBytes)
 032                throw new ArgumentException(string.Format("Signature size must be {0}", SignatureSizeInBytes), "signatur
 333            if (publicKey.Length != PublicKeySizeInBytes)
 034                throw new ArgumentException(string.Format("Public key size must be {0}", PublicKeySizeInBytes), "publicK
 335            return Ed25519Operations.crypto_sign_verify(signature, 0, message, 0, message.Length, publicKey, 0);
 336        }
 37
 38        public static void Sign(ArraySegment<byte> signature, ArraySegment<byte> message, ArraySegment<byte> expandedPri
 139        {
 140            if (signature.Array == null)
 041                throw new ArgumentNullException("signature.Array");
 142            if (signature.Count != SignatureSizeInBytes)
 043                throw new ArgumentException("signature.Count");
 144            if (expandedPrivateKey.Array == null)
 045                throw new ArgumentNullException("expandedPrivateKey.Array");
 146            if (expandedPrivateKey.Count != ExpandedPrivateKeySizeInBytes)
 047                throw new ArgumentException("expandedPrivateKey.Count");
 148            if (message.Array == null)
 049                throw new ArgumentNullException("message.Array");
 150            Ed25519Operations.crypto_sign2(signature.Array, signature.Offset, message.Array, message.Offset, message.Cou
 151        }
 52
 53        public static byte[] Sign(byte[] message, byte[] expandedPrivateKey)
 154        {
 155            var signature = new byte[SignatureSizeInBytes];
 156            Sign(new ArraySegment<byte>(signature), new ArraySegment<byte>(message), new ArraySegment<byte>(expandedPriv
 157            return signature;
 158        }
 59
 60        public static byte[] PublicKeyFromSeed(byte[] privateKeySeed)
 061        {
 62            byte[] privateKey;
 63            byte[] publicKey;
 064            KeyPairFromSeed(out publicKey, out privateKey, privateKeySeed);
 065            CryptoBytes.Wipe(privateKey);
 066            return publicKey;
 067        }
 68
 69        public static byte[] ExpandedPrivateKeyFromSeed(byte[] privateKeySeed)
 070        {
 71            byte[] privateKey;
 72            byte[] publicKey;
 073            KeyPairFromSeed(out publicKey, out privateKey, privateKeySeed);
 074            CryptoBytes.Wipe(publicKey);
 075            return privateKey;
 076        }
 77
 78        public static void KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPrivateKey, byte[] privateKeySeed)
 779        {
 780            if (privateKeySeed == null)
 081                throw new ArgumentNullException("privateKeySeed");
 782            if (privateKeySeed.Length != PrivateKeySeedSizeInBytes)
 083                throw new ArgumentException("privateKeySeed");
 784            var pk = new byte[PublicKeySizeInBytes];
 785            var sk = new byte[ExpandedPrivateKeySizeInBytes];
 786            Ed25519Operations.crypto_sign_keypair(pk, 0, sk, 0, privateKeySeed, 0);
 787            publicKey = pk;
 788            expandedPrivateKey = sk;
 789        }
 90
 91        public static void KeyPairFromSeed(ArraySegment<byte> publicKey, ArraySegment<byte> expandedPrivateKey, ArraySeg
 092        {
 093            if (publicKey.Array == null)
 094                throw new ArgumentNullException("publicKey.Array");
 095            if (expandedPrivateKey.Array == null)
 096                throw new ArgumentNullException("expandedPrivateKey.Array");
 097            if (privateKeySeed.Array == null)
 098                throw new ArgumentNullException("privateKeySeed.Array");
 099            if (publicKey.Count != PublicKeySizeInBytes)
 0100                throw new ArgumentException("publicKey.Count");
 0101            if (expandedPrivateKey.Count != ExpandedPrivateKeySizeInBytes)
 0102                throw new ArgumentException("expandedPrivateKey.Count");
 0103            if (privateKeySeed.Count != PrivateKeySeedSizeInBytes)
 0104                throw new ArgumentException("privateKeySeed.Count");
 0105            Ed25519Operations.crypto_sign_keypair(
 0106                publicKey.Array, publicKey.Offset,
 0107                expandedPrivateKey.Array, expandedPrivateKey.Offset,
 0108                privateKeySeed.Array, privateKeySeed.Offset);
 0109        }
 110
 111        [Obsolete("Needs more testing")]
 112        public static byte[] KeyExchange(byte[] publicKey, byte[] privateKey)
 0113        {
 0114            var sharedKey = new byte[SharedKeySizeInBytes];
 0115            KeyExchange(new ArraySegment<byte>(sharedKey), new ArraySegment<byte>(publicKey), new ArraySegment<byte>(pri
 0116            return sharedKey;
 0117        }
 118
 119        [Obsolete("Needs more testing")]
 120        public static void KeyExchange(ArraySegment<byte> sharedKey, ArraySegment<byte> publicKey, ArraySegment<byte> pr
 0121        {
 0122            if (sharedKey.Array == null)
 0123                throw new ArgumentNullException("sharedKey.Array");
 0124            if (publicKey.Array == null)
 0125                throw new ArgumentNullException("publicKey.Array");
 0126            if (privateKey.Array == null)
 0127                throw new ArgumentNullException("privateKey");
 0128            if (sharedKey.Count != 32)
 0129                throw new ArgumentException("sharedKey.Count != 32");
 0130            if (publicKey.Count != 32)
 0131                throw new ArgumentException("publicKey.Count != 32");
 0132            if (privateKey.Count != 64)
 0133                throw new ArgumentException("privateKey.Count != 64");
 134
 135            FieldElement montgomeryX, edwardsY, edwardsZ, sharedMontgomeryX;
 0136            FieldOperations.fe_frombytes(out edwardsY, publicKey.Array, publicKey.Offset);
 0137            FieldOperations.fe_1(out edwardsZ);
 0138            MontgomeryCurve25519.EdwardsToMontgomeryX(out montgomeryX, ref edwardsY, ref edwardsZ);
 0139            byte[] h = Sha512.Hash(privateKey.Array, privateKey.Offset, 32);//ToDo: Remove alloc
 0140            ScalarOperations.sc_clamp(h, 0);
 0141            MontgomeryOperations.scalarmult(out sharedMontgomeryX, h, 0, ref montgomeryX);
 0142            CryptoBytes.Wipe(h);
 0143            FieldOperations.fe_tobytes(sharedKey.Array, sharedKey.Offset, ref sharedMontgomeryX);
 0144            MontgomeryCurve25519.KeyExchangeOutputHashNaCl(sharedKey.Array, sharedKey.Offset);
 0145        }
 146    }
 147}