Skip to content
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

Compatibility issue with curve25519-java #198

Open
c0deguard opened this issue Sep 13, 2019 · 6 comments
Open

Compatibility issue with curve25519-java #198

c0deguard opened this issue Sep 13, 2019 · 6 comments

Comments

@c0deguard
Copy link

Working on interop between java(https://github.com/signalapp/curve25519-java) and Nodejs (Elliptic Library)
Facing issue in generating a similar shared key.

Elliptic Keypair
Public key: '4a6916193cef6a39e0dafc65b5f3ed5e4b0a8390e21b66939171f99dad9b82d3'
Private key: '0c03ff1e715709daf08aabd039c1533909141f0d73689101f5961389a36b22f5'

curve25519-java Keypair
Public key: 'EA8B14FD04C8C62B1A150B999646464690E1667A2002D6C43AC8AA89F42E563B'
Private key: '18F320EFF4483556FFEE78EDBF8C6688E188DE7F94F571106A737D4D2A30A662'

Elliptic Shared Key
17e9bf834fb8348d481691dc38380db40a876d92de1662c887380f6aff1faf86
curve25519-java Shared Key
E26F02F571F701B4ECE3D6765C304F3305AEF9AC20B0DD69E85EE0A8DABB0A1E

Please help. Thanks

@fanatid
Copy link
Contributor

fanatid commented Sep 13, 2019

Please post code and steps for reproducing issue.

@c0deguard
Copy link
Author

c0deguard commented Sep 13, 2019

Node Js

 var ec = require('elliptic').ec('curve25519')
 ​
  const readline = require('readline').createInterface({​
      input: process.stdin,​
      output: process.stdout​
 })​
  key=ec.genKeyPair();​
  console.log('Public Key: '+key.getPublic(true,'hex'));​
  console.log('Private Key: '+key.getPrivate('hex'));​

 readline.question(`Enter the public key :  `, (publickey) => {​
      sharedkey=key.derive(ec.keyFromPublic(publickey).pub);​
      console.log("Shared Key :  "+sharedkey.toString('hex'))​
      readline.close()​
 })​

Java Code

import org.whispersystems.curve25519.*;
import java.lang.*;​
import java.util.Scanner; ​
​
public class test{​
​
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();​
​
public static String bytesToHex(byte[] bytes) {​
   char[] hexChars = new char[bytes.length * 2];​
   for (int j = 0; j < bytes.length; j++) {​
     int v = bytes[j] & 0xFF;​
     hexChars[j * 2] = HEX_ARRAY[v >>> 4];​
     hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];​
   }​
   return new String(hexChars);​
}​
​
public static byte[] hexToByte(String s) {​
   int len = s.length();​
   byte[] data = new byte[len / 2];​
   for (int i = 0; i < len; i += 2) {​
      data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)​
       + Character.digit(s.charAt(i+1), 16));​
   }​
   return data;​
}​
​
​
public static void main(String args[]){​
​
   Scanner sc = new Scanner(System.in);​
 ​
   Curve25519 cipher = Curve25519.getInstance(Curve25519.BEST);​
   Curve25519KeyPair keyPair1 = Curve25519.getInstance(Curve25519.BEST).generateKeyPair();​
   byte privatekey[]=keyPair1.getPrivateKey();​
   byte publickey[]=keyPair1.getPublicKey();​
   System.out.println("Private key of keyPair 1 : "+bytesToHex(privatekey));​
   System.out.println("Public key of keyPair 1 : "+bytesToHex(publickey));​
   System.out.println("Enter the public Key");​
   String public_key=sc.nextLine();​
   byte[] sharedSecret = cipher.calculateAgreement(hexToByte(public_key), privatekey);​
   System.out.println("shared key : "+bytesToHex(sharedSecret));​
 ​
}​
}

@c0deguard
Copy link
Author

@indutny please look into this

@fanatid
Copy link
Contributor

fanatid commented Sep 19, 2019

@c0deguard please respect anybody other time, nobody will not dive into your code if this require more than 10min or so.
If you rise issue about compatibility please provide code which show that result incorrect (and without readline). I'm not familiar with run java code, is it possible run anywhere online?
In your first message I see different keypairs, how shared keys can be same then?

@c0deguard
Copy link
Author

here is the online link to execute both the programs.

Keypair 1
Elliptic Private key: '0c03ff1e715709daf08aabd039c1533909141f0d73689101f5961389a36b22f5'
curve25519-java Public key: 'EA8B14FD04C8C62B1A150B999646464690E1667A2002D6C43AC8AA89F42E563B'

Elliptic Shared Key (Nodejs Test Link: https://repl.it/@MuthuSelvan/node-test)
789ab83aca04690a188512c245bdcbe7dfbe84428d595e9807422e4f140dc806

Keypair 2
curve25519-java Private key: '18F320EFF4483556FFEE78EDBF8C6688E188DE7F94F571106A737D4D2A30A662'
Elliptic Public key: '4a6916193cef6a39e0dafc65b5f3ed5e4b0a8390e21b66939171f99dad9b82d3'

curve25519-java Shared Key (Java Test Link: https://repl.it/@MuthuSelvan/java-test-program)
E26F02F571F701B4ECE3D6765C304F3305AEF9AC20B0DD69E85EE0A8DABB0A1E

@shamohai
Copy link

shamohai commented Dec 17, 2019

Hi, @c0deguard, Solve it ? C# has the same problem.
https://github.com/indutny/elliptic/issues/209

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants