Skip to content

Commit 28301ab

Browse files
Ed25519 is now based on BouncyCastle instead of Chaos.NaCl (#1448)
* Ed25519 is now based on BouncyCastle instead of Chaos.NaCl * Generate PublicKey and fix NullReferenceException * Rectify variable name --------- Co-authored-by: Wojciech Nagórski <[email protected]>
1 parent 71e2909 commit 28301ab

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/Renci.SshNet/Security/Cryptography/ED25519DigitalSignature.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System;
22

3+
using Org.BouncyCastle.Math.EC.Rfc8032;
4+
35
using Renci.SshNet.Common;
4-
using Renci.SshNet.Security.Chaos.NaCl;
56

67
namespace Renci.SshNet.Security.Cryptography
78
{
@@ -39,7 +40,7 @@ public ED25519DigitalSignature(ED25519Key key)
3940
/// <exception cref="InvalidOperationException">Invalid signature.</exception>
4041
public override bool Verify(byte[] input, byte[] signature)
4142
{
42-
return Ed25519.Verify(signature, input, _key.PublicKey);
43+
return Ed25519.Verify(signature, 0, _key.PublicKey, 0, input, 0, input.Length);
4344
}
4445

4546
/// <summary>
@@ -52,7 +53,9 @@ public override bool Verify(byte[] input, byte[] signature)
5253
/// <exception cref="SshException">Invalid ED25519Key key.</exception>
5354
public override byte[] Sign(byte[] input)
5455
{
55-
return Ed25519.Sign(input, _key.PrivateKey);
56+
var signature = new byte[Ed25519.SignatureSize];
57+
Ed25519.Sign(_key.PrivateKey, 0, _key.PublicKey, 0, input, 0, input.Length, signature, 0);
58+
return signature;
5659
}
5760

5861
/// <summary>

src/Renci.SshNet/Security/Cryptography/ED25519Key.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System;
22

3+
using Org.BouncyCastle.Math.EC.Rfc8032;
4+
35
using Renci.SshNet.Common;
4-
using Renci.SshNet.Security.Chaos.NaCl;
56
using Renci.SshNet.Security.Cryptography;
67

78
namespace Renci.SshNet.Security
@@ -49,7 +50,7 @@ public override int KeyLength
4950
{
5051
get
5152
{
52-
return PublicKey.Length * 8;
53+
return Ed25519.PublicKeySize * 8;
5354
}
5455
}
5556

@@ -91,8 +92,7 @@ public ED25519Key(SshKeyData publicKeyData)
9192
throw new ArgumentException($"Invalid Ed25519 public key data ({publicKeyData.Name}, {publicKeyData.Keys.Length}).", nameof(publicKeyData));
9293
}
9394

94-
PublicKey = publicKeyData.Keys[0].ToByteArray().Reverse().TrimLeadingZeros().Pad(Ed25519.PublicKeySizeInBytes);
95-
PrivateKey = new byte[Ed25519.ExpandedPrivateKeySizeInBytes];
95+
PublicKey = publicKeyData.Keys[0].ToByteArray().Reverse().TrimLeadingZeros().Pad(Ed25519.PublicKeySize);
9696
}
9797

9898
/// <summary>
@@ -103,11 +103,10 @@ public ED25519Key(SshKeyData publicKeyData)
103103
/// </param>
104104
public ED25519Key(byte[] privateKeyData)
105105
{
106-
var seed = new byte[Ed25519.PrivateKeySeedSizeInBytes];
107-
Buffer.BlockCopy(privateKeyData, 0, seed, 0, seed.Length);
108-
Ed25519.KeyPairFromSeed(out var publicKey, out var privateKey, seed);
109-
PublicKey = publicKey;
110-
PrivateKey = privateKey;
106+
PrivateKey = new byte[Ed25519.SecretKeySize];
107+
PublicKey = new byte[Ed25519.PublicKeySize];
108+
Buffer.BlockCopy(privateKeyData, 0, PrivateKey, 0, Ed25519.SecretKeySize);
109+
Ed25519.GeneratePublicKey(privateKeyData, 0, PublicKey, 0);
111110
}
112111

113112
/// <summary>

src/Renci.SshNet/Security/SshKeyData.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System.Collections.Generic;
22
using System.Text;
33

4+
using Org.BouncyCastle.Math.EC.Rfc8032;
5+
46
using Renci.SshNet.Common;
5-
using Renci.SshNet.Security.Chaos.NaCl;
67

78
namespace Renci.SshNet.Security
89
{
@@ -88,7 +89,7 @@ protected override void SaveData()
8889
var keyData = key.ToByteArray().Reverse();
8990
if (Name == "ssh-ed25519")
9091
{
91-
keyData = keyData.TrimLeadingZeros().Pad(Ed25519.PublicKeySizeInBytes);
92+
keyData = keyData.TrimLeadingZeros().Pad(Ed25519.PublicKeySize);
9293
}
9394

9495
WriteBinaryString(keyData);

0 commit comments

Comments
 (0)