Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ed25519] Use BouncyCastle instead of Chaos.NaCl #1448

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Ed25519 is now based on BouncyCastle instead of Chaos.NaCl
scott-xu committed Jul 20, 2024
commit e7b12b8c8d1904ddd4e09022183f19d50e9b77d2
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;

using Org.BouncyCastle.Math.EC.Rfc8032;

using Renci.SshNet.Common;
using Renci.SshNet.Security.Chaos.NaCl;

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

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

/// <summary>
14 changes: 5 additions & 9 deletions src/Renci.SshNet/Security/Cryptography/ED25519Key.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;

using Org.BouncyCastle.Math.EC.Rfc8032;

using Renci.SshNet.Common;
using Renci.SshNet.Security.Chaos.NaCl;
using Renci.SshNet.Security.Cryptography;

namespace Renci.SshNet.Security
@@ -49,7 +50,7 @@ public override int KeyLength
{
get
{
return PublicKey.Length * 8;
return Ed25519.PublicKeySize * 8;
}
}

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

PublicKey = publicKeyData.Keys[0].ToByteArray().Reverse().TrimLeadingZeros().Pad(Ed25519.PublicKeySizeInBytes);
PrivateKey = new byte[Ed25519.ExpandedPrivateKeySizeInBytes];
PublicKey = publicKeyData.Keys[0].ToByteArray().Reverse().TrimLeadingZeros().Pad(Ed25519.PublicKeySize);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just keep the PrivateKey not-null unless we enable nullability here

Suggested change
PublicKey = publicKeyData.Keys[0].ToByteArray().Reverse().TrimLeadingZeros().Pad(Ed25519.PublicKeySize);
PublicKey = publicKeyData.Keys[0].ToByteArray().Reverse().TrimLeadingZeros().Pad(Ed25519.PublicKeySize);
PrivateKey = new byte[Ed25519.SecretKeySize];

}

/// <summary>
@@ -103,11 +103,7 @@ public ED25519Key(SshKeyData publicKeyData)
/// </param>
public ED25519Key(byte[] privateKeyData)
{
var seed = new byte[Ed25519.PrivateKeySeedSizeInBytes];
Buffer.BlockCopy(privateKeyData, 0, seed, 0, seed.Length);
Ed25519.KeyPairFromSeed(out var publicKey, out var privateKey, seed);
PublicKey = publicKey;
PrivateKey = privateKey;
PrivateKey = privateKeyData.Take(Ed25519.SecretKeySize);
}

/// <summary>
5 changes: 3 additions & 2 deletions src/Renci.SshNet/Security/SshKeyData.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Collections.Generic;
using System.Text;

using Org.BouncyCastle.Math.EC.Rfc8032;

using Renci.SshNet.Common;
using Renci.SshNet.Security.Chaos.NaCl;

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

WriteBinaryString(keyData);