Skip to content

Clean up more Ed25519 casting #346

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

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ else if ("EdDSA".equals(publicKey.getAlgorithm()))
}
else
{
Ed25519PrivateKey pk = (Ed25519PrivateKey) privateKey;
Ed25519PrivateKey pk = Ed25519Verify.convertPrivateKey(privateKey);
ed_sig_enc = Ed25519Verify.get().generateSignature(msg, pk, rnd);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public Key engineTranslateKey(Key key) throws InvalidKeyException {
if (key instanceof Ed25519PublicKey || key instanceof Ed25519PrivateKey) {
return key;
}

if (key instanceof PublicKey && key.getFormat().equals("X.509")) {
byte[] encoded = key.getEncoded();
try {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/trilead/ssh2/signature/Ed25519Verify.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static Ed25519Verify get() {

@Override
public byte[] encodePublicKey(PublicKey publicKey) throws IOException {
Ed25519PublicKey ed25519PublicKey = getEd25519PublicKey(publicKey);
Ed25519PublicKey ed25519PublicKey = convertPublicKey(publicKey);

TypesWriter tw = new TypesWriter();

Expand Down Expand Up @@ -104,15 +104,15 @@ public PublicKey decodePublicKey(byte[] encoded) throws IOException {

@Override
public byte[] generateSignature(byte[] msg, PrivateKey privateKey, SecureRandom secureRandom) throws IOException {
Ed25519PrivateKey ed25519PrivateKey = getEd25519PrivateKey(privateKey);
Ed25519PrivateKey ed25519PrivateKey = convertPrivateKey(privateKey);
try {
return encodeSSHEd25519Signature(new Ed25519Sign(ed25519PrivateKey.getSeed()).sign(msg));
} catch (GeneralSecurityException e) {
throw new IOException(e);
}
}

private static Ed25519PublicKey getEd25519PublicKey(PublicKey publicKey) throws IOException {
public static Ed25519PublicKey convertPublicKey(PublicKey publicKey) throws IOException {
Ed25519KeyFactory kf = new Ed25519KeyFactory();
try {
return (Ed25519PublicKey) kf.engineTranslateKey(publicKey);
Expand All @@ -121,7 +121,7 @@ private static Ed25519PublicKey getEd25519PublicKey(PublicKey publicKey) throws
}
}

private static Ed25519PrivateKey getEd25519PrivateKey(PrivateKey privateKey) throws IOException {
public static Ed25519PrivateKey convertPrivateKey(PrivateKey privateKey) throws IOException {
Ed25519KeyFactory kf = new Ed25519KeyFactory();
try {
return (Ed25519PrivateKey) kf.engineTranslateKey(privateKey);
Expand All @@ -132,7 +132,7 @@ private static Ed25519PrivateKey getEd25519PrivateKey(PrivateKey privateKey) thr

@Override
public boolean verifySignature(byte[] message, byte[] sshSig, PublicKey publicKey) throws IOException {
Ed25519PublicKey ed25519PublicKey = getEd25519PublicKey(publicKey);
Ed25519PublicKey ed25519PublicKey = convertPublicKey(publicKey);
byte[] javaSig = decodeSSHEd25519Signature(sshSig);
try {
new com.google.crypto.tink.subtle.Ed25519Verify(ed25519PublicKey.getAbyte()).verify(javaSig, message);
Expand Down