Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ianjoneill committed Dec 22, 2024
1 parent cc10154 commit 615fab4
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.sshd.common.config.keys.PublicKeyEntryDecoder;
import org.apache.sshd.common.signature.Signature;
import org.apache.sshd.common.util.buffer.Buffer;
import org.apache.sshd.common.util.security.SecurityUtils;
import org.apache.sshd.common.util.security.eddsa.generic.EdDSASupport;

public class NetI2pCryptoEdDSASupport implements EdDSASupport<EdDSAPublicKey, EdDSAPrivateKey> {
Expand Down Expand Up @@ -126,4 +127,9 @@ public byte[] getPublicKeyData(EdDSAPublicKey publicKey) {
public byte[] getPrivateKeyData(EdDSAPrivateKey privateKey) throws IOException {
return privateKey.getSeed();
}

@Override
public String getKeyFactoryAlgorithm() {
return SecurityUtils.EDDSA;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.bouncycastle.jcajce.interfaces.EdDSAKey;
import org.bouncycastle.jcajce.interfaces.EdDSAPrivateKey;
import org.bouncycastle.jcajce.interfaces.EdDSAPublicKey;
import org.bouncycastle.jcajce.spec.OpenSSHPrivateKeySpec;
import org.bouncycastle.jcajce.spec.RawEncodedKeySpec;

public class BouncyCastleEdDSASupport implements EdDSASupport<EdDSAPublicKey, EdDSAPrivateKey> {
Expand Down Expand Up @@ -115,15 +114,15 @@ public EdDSAPublicKey recoverEDDSAPublicKey(PrivateKey key) throws GeneralSecuri
@Override
public EdDSAPublicKey generateEDDSAPublicKey(byte[] seed) throws GeneralSecurityException {
RawEncodedKeySpec keySpec = new RawEncodedKeySpec(seed);
KeyFactory factory = SecurityUtils.getKeyFactory(SecurityUtils.ED25519);
KeyFactory factory = SecurityUtils.getKeyFactory(getKeyFactoryAlgorithm());
return (EdDSAPublicKey) factory.generatePublic(keySpec);
}

@Override
public EdDSAPrivateKey generateEDDSAPrivateKey(byte[] seed) throws GeneralSecurityException, IOException {
Ed25519PrivateKeyParameters parameters = new Ed25519PrivateKeyParameters(seed);
PrivateKeyInfo info = PrivateKeyInfoFactory.createPrivateKeyInfo(parameters);
KeyFactory factory = SecurityUtils.getKeyFactory(SecurityUtils.ED25519);
KeyFactory factory = SecurityUtils.getKeyFactory(getKeyFactoryAlgorithm());
return (EdDSAPrivateKey) factory.generatePrivate(new PKCS8EncodedKeySpec(info.getEncoded()));
}

Expand All @@ -148,7 +147,7 @@ public KeySpec createPublicKeySpec(EdDSAPublicKey publicKey) {

@Override
public KeySpec createPrivateKeySpec(EdDSAPrivateKey privateKey) {
return new OpenSSHPrivateKeySpec(privateKey.getEncoded());
return new PKCS8EncodedKeySpec(privateKey.getEncoded());
}

@Override
Expand All @@ -162,4 +161,9 @@ public byte[] getPrivateKeyData(EdDSAPrivateKey privateKey) throws IOException {
= (Ed25519PrivateKeyParameters) PrivateKeyFactory.createKey(privateKey.getEncoded());
return parameters.getEncoded();
}

@Override
public String getKeyFactoryAlgorithm() {
return SecurityUtils.ED25519;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,9 @@ static PrivateKey decodeEdDSAPrivateKey(byte[] keyData) throws IOException, Gene
*/
byte[] getPrivateKeyData(PRV privateKey) throws IOException;

/**
* @return the algorithm name used by the provider's {@link java.security.KeyFactory}.
*/
String getKeyFactoryAlgorithm();

}
Loading

0 comments on commit 615fab4

Please sign in to comment.