ECPublicKey recovery from JavaCard to Java
I am trying to implement ECDH between a terminal (simulated by my computer) and a smart card (Java Card).
I have fixed the elliptic curve I want to use and from the map side I have the following code to run the first part of the protocol:
ECPublicKey pubKey = (ECPublicKey) KeyBuilder.buildKey(
KeyBuilder.TYPE_EC_FP_PUBLIC, (short) 0x0100, false);
pubKey.setFieldFP(p, (short) 0x0001, (short) 0x0020);
pubKey.setA(a, (short) 0x0001, (short) 0x0020);
pubKey.setB(b, (short) 0x0000, (short) 0x0020);
pubKey.setR(r, (short) 0x0001, (short) 0x0020);
pubKey.setG(g, (short) 0x0000, (short) g.length);
ECPrivateKey privKey = (ECPrivateKey) KeyBuilder.buildKey(
KeyBuilder.TYPE_EC_FP_PRIVATE, (short) 0x0100, false);
KeyPair keypair = new KeyPair(pubKey, privKey);
keypair.genKeyPair();
pubKey.getW(apduBuffer, (short) 0x0000);
setOutgoingAndSend((short) 0x0000, (short) 0x0041);
So I am creating a KeyPair for ECDH and I am posting the public one to my terminal.
My problem is this: I cannot recover the ECPublicKey given the APDU response I received ...
I haven't found any way to do this in Java (even using an external library like Bouncy Castle).
Can anyone help me? Thank you in advance.
source to share
The public key is returned to the JavaCard, formatted as follows: 04 x y
. On the terminal side, you first need to extract the x and y coordinates. Then,
KeyFactory kf = KeyFactory.getInstance ("ECDSA", "BC");
ECPoint point = new ECPoint (x, y);
ECParameterSpec domainparameters = new ECParameterSpec (...); // initialize your domain parameters
ECPublicKeySpec spec = new ECPublicKeySpec (dots, domain parameters);
ECPublicKey publickey = (ECPublicKey) kf.generatePublic (spec);
source to share