Recovering an ECPublicKey from JavaCard to Java

477 views Asked by At

I am trying to implement ECDH between a terminal (simulated by my computer) and a smart card (Java Card).

I fixed the elliptic curve that I want to use, and on the card 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 create a KeyPair for ECDH and I send the public one to my terminal.

My problem is the following : I am not able to reconstruct an ECPublicKey given the response APDU I get...

I did not find any way to do this in Java (even using external library as Bouncy Castle).

Could someone help me ? Thank you in advance.

1

There are 1 answers

0
Chooch On BEST ANSWER

The public key returned in JavaCard is formatted as follows: 04 x y. In the terminal side, first you must extract 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(point, domainparameters);

ECPublicKey publickey = (ECPublicKey)kf.generatePublic(spec);