I am using java built-in SunEC 21 security provider to do X25519 test cases and all work but one.
I am using the same logic for every test just different inputs.
When I use the inputs:
Public key: e5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493
Private key: 4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d
the expected output is:
95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957
Instead I get:
d5f33573c9f6b8129483acce1e2534e95d3c41af6b00d0d30437b87cada57e4a
(This is test case two in the RFC 7748.)
I don't know why this is. Whenever I try it with the first or third input in the RFC 7748 it works fine. (Again, same logic just different inputs.)
byte[] publicKey;
publicKey = Hex.decode("e5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493");
byte[] privateKey;// input scalar
privateKey = Hex.decode("4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d");
// System.out.println("bc go crazy:" + Hex.toHexString(privateKey));
NamedParameterSpec paramSpec = new NamedParameterSpec("X25519");
BigInteger clientBigInteger = new BigInteger(1, Util.reverse(publicKey));
KeyFactory kf = KeyFactory.getInstance("X25519");
XECPublicKeySpec clientPublicKeySpec = new XECPublicKeySpec(paramSpec, clientBigInteger);
XECPublicKey clientPublicKey;
clientPublicKey = (XECPublicKey) kf.generatePublic(clientPublicKeySpec);
XECPrivateKeySpec privateKeySpec = new XECPrivateKeySpec(paramSpec, privateKey);
XECPrivateKey secretKey = (XECPrivateKey) kf.generatePrivate(privateKeySpec);
KeyAgreement ka = KeyAgreement.getInstance("X25519");
ka.init(secretKey);
ka.doPhase(clientPublicKey, true);
byte[] sharedSecret = ka.generateSecret();
System.err.println("---------- X25519 test cases ----------");
System.out.println("X25519-test-case-2-out:" + Hex.toHexString(sharedSecret));
System.out.println(
"X25519-test-case-2-exp:95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957");
This is the code that's not giving the right output. I have checked test vectors ten times, and copy pasted logic twenty. What's wrong with my code? Why does it work for some test vectors but not others?
Based off RFC 7748.