In Java 11 a curve25519 built-in implementation was introduced. As I had no idea of this, and only discovered it recently, I was using a library from Signal. This was my code before I switched to Java 11's implementation:
private final Curve25519 CURVE_25519 = Curve25519.getInstance(Curve25519.JAVA);
public Curve25519KeyPair calculateRandomKeyPair() {
return CURVE_25519.generateKeyPair();
}
public byte[] calculateSharedSecret(byte[] publicKey, byte[] privateKey) {
return CURVE_25519.calculateAgreement(publicKey, privateKey);
}
And this is my code now:
private final String XDH = "XDH";
private final String CURVE = "X25519";
@SneakyThrows
public KeyPair calculateRandomKeyPair() {
return KeyPairGenerator.getInstance(CURVE).generateKeyPair();
}
@SneakyThrows
public byte[] calculateSharedSecret(byte[] publicKeyBytes, XECPrivateKey privateKey) {
var paramSpec = new NamedParameterSpec(CURVE);
var keyFactory = KeyFactory.getInstance(XDH);
var publicKeySpec = new XECPublicKeySpec(paramSpec, new BigInteger(publicKeyBytes));
var publicKey = keyFactory.generatePublic(publicKeySpec);
var keyAgreement = KeyAgreement.getInstance(XDH);
keyAgreement.init(privateKey);
keyAgreement.doPhase(publicKey, true);
return keyAgreement.generateSecret();
}
Obviously, the second implementation doesn't work while the first one does. Initially, I thought I was doing something wrong so I read the documentation and checked similar answers, though, as I didn't find anything helpful, I decided to dig further and tried to check if both Signal's library and Java generate the same public key given the private one. To do this I wrote this snippet:
import org.whispersystems.curve25519.Curve25519;
import sun.security.ec.XECOperations;
import sun.security.ec.XECParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.NamedParameterSpec;
import java.util.Arrays;
private static boolean generateJava11KeyPair() throws InvalidAlgorithmParameterException {
var signalKeyPair = Curve25519.getInstance(Curve25519.JAVA).generateKeyPair();
var signalPublicKey = signalKeyPair.getPublicKey();
var params = XECParameters.get(InvalidAlgorithmParameterException::new, NamedParameterSpec.X25519);
var ops = new XECOperations(params);
var javaPublicKey = ops.computePublic(signalKeyPair.getPrivateKey().clone()).toByteArray();
return Arrays.equals(signalPublicKey, javaPublicKey);
}
(the code used to calculate the public key following Java's implementation was extracted from sun.security.ec.XDHKeyPairGenerator)
This method prints false, which means that the two implementations actually don't behave in the same way. At this point, I'm wondering if this is a Java bug or if I'm missing something. Thanks in advance.