-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for ed25519 keys (Fixes #220)
- Loading branch information
1 parent
a73776a
commit db75bad
Showing
9 changed files
with
197 additions
and
11 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/com/hierynomus/sshj/signature/Ed25519PublicKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.hierynomus.sshj.signature; | ||
|
||
import net.i2p.crypto.eddsa.EdDSAPublicKey; | ||
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveSpec; | ||
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable; | ||
import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec; | ||
import net.schmizz.sshj.common.SSHRuntimeException; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Our own extension of the EdDSAPublicKey that comes from ECC-25519, as that class does not implement equality. | ||
* The code uses the equality of the keys as an indicator whether they're the same during host key verification. | ||
*/ | ||
public class Ed25519PublicKey extends EdDSAPublicKey { | ||
|
||
public Ed25519PublicKey(EdDSAPublicKeySpec spec) { | ||
super(spec); | ||
|
||
EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("ed25519-sha-512"); | ||
if (!spec.getParams().getCurve().equals(ed25519.getCurve())) { | ||
throw new SSHRuntimeException("Cannot create Ed25519 Public Key from wrong spec"); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (!(other instanceof Ed25519PublicKey)) { | ||
return false; | ||
} | ||
|
||
Ed25519PublicKey otherKey = (Ed25519PublicKey) other; | ||
return Arrays.equals(getAbyte(), otherKey.getAbyte()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return getA().hashCode(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/com/hierynomus/sshj/signature/SignatureEdDSA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.hierynomus.sshj.signature; | ||
|
||
import net.i2p.crypto.eddsa.EdDSAEngine; | ||
import net.schmizz.sshj.common.Buffer; | ||
import net.schmizz.sshj.common.KeyType; | ||
import net.schmizz.sshj.common.SSHRuntimeException; | ||
import net.schmizz.sshj.signature.Signature; | ||
|
||
import java.security.*; | ||
|
||
public class SignatureEdDSA implements Signature { | ||
public static class Factory implements net.schmizz.sshj.common.Factory.Named<Signature> { | ||
|
||
@Override | ||
public String getName() { | ||
return KeyType.ED25519.toString(); | ||
} | ||
|
||
@Override | ||
public Signature create() { | ||
return new SignatureEdDSA(); | ||
} | ||
} | ||
|
||
final EdDSAEngine engine; | ||
|
||
protected SignatureEdDSA() { | ||
try { | ||
engine = new EdDSAEngine(MessageDigest.getInstance("SHA-512")); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new SSHRuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void init(PublicKey pubkey, PrivateKey prvkey) { | ||
try { | ||
if (pubkey != null) { | ||
engine.initVerify(pubkey); | ||
} | ||
|
||
if (prvkey != null) { | ||
engine.initSign(prvkey); | ||
} | ||
} catch (InvalidKeyException e) { | ||
throw new SSHRuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void update(byte[] H) { | ||
update(H, 0, H.length); | ||
} | ||
|
||
@Override | ||
public void update(byte[] H, int off, int len) { | ||
try { | ||
engine.update(H, off, len); | ||
} catch (SignatureException e) { | ||
throw new SSHRuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] sign() { | ||
try { | ||
return engine.sign(); | ||
} catch (SignatureException e) { | ||
throw new SSHRuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] encode(byte[] signature) { | ||
return signature; | ||
} | ||
|
||
@Override | ||
public boolean verify(byte[] sig) { | ||
try { | ||
Buffer.PlainBuffer plainBuffer = new Buffer.PlainBuffer(sig); | ||
String algo = plainBuffer.readString(); | ||
if (!"ssh-ed25519".equals(algo)) { | ||
throw new SSHRuntimeException("Expected 'ssh-ed25519' key algorithm, but was: " + algo); | ||
} | ||
byte[] bytes = plainBuffer.readBytes(); | ||
return engine.verify(bytes); | ||
} catch (SignatureException e) { | ||
throw new SSHRuntimeException(e); | ||
} catch (Buffer.BufferException e) { | ||
throw new SSHRuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW | ||
QyNTUxOQAAACAwHSYkZJATPMgvLHkxKAJ9j38Gyyq5HGoWdMcT6FiAiQAAAJDimgR84poE | ||
fAAAAAtzc2gtZWQyNTUxOQAAACAwHSYkZJATPMgvLHkxKAJ9j38Gyyq5HGoWdMcT6FiAiQ | ||
AAAECmsckQycWnfGQK6XtQpaMGODbAkMQOdJNK6XJSipB7dDAdJiRkkBM8yC8seTEoAn2P | ||
fwbLKrkcahZ0xxPoWICJAAAACXJvb3RAc3NoagECAwQ= | ||
-----END OPENSSH PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDAdJiRkkBM8yC8seTEoAn2PfwbLKrkcahZ0xxPoWICJ root@sshj |