How does elliptic curve Diffie Hellman use JavaCard with Java?

I'm trying to use elliptic curve Diffie Hellman to handle JavaCard (version 2.2.1)

On JavaCard, I now have the following code:

byte temp[] = new byte[100];
byte secret[] = new byte[100];
byte size = buf[ISO7816.OFFSET_LC];

Util.arrayCopy(buf,ISO7816.OFFSET_CDATA,temp,(byte) 0,size);

// the public key is in temp
short len = dh.generateSecret(temp,size,secret,(byte) 0);

Util.arrayCopy(temp,buf,size);
//Util.arrayCopy(secret,len);
apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA,size);

My initialization DH is as follows:

keyPair = new KeyPair(KeyPair.ALG_EC_FP,KeyBuilder.LENGTH_EC_F2M_163);
keyPair.genKeyPair();
dh = KeyAgreement.getInstance(KeyAgreement.ALG_EC_SVDP_DH,false);
dh.init(keyPair.getPrivate());

Except DH All this seems to work except for the generatesecret call, where the applet just seems to crash This works well if I exit the call and return other data I import the data sent by the terminal At the terminal, I have the following:

// generate an ecdh keypair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(163);
KeyPair keyPair = keyGen.generateKeyPair();

// initialize DH
KeyAgreement dh = KeyAgreement.getInstance("ECDH");
dh.init(keyPair.getPrivate());

//byte encKey[] = keyPair.getPublic().getEncoded();

// X9.62 encoding,no compression
int qLength = (163+7)/8;
byte[] xArr = ((EcpublicKey) keyPair.getPublic()).getW().getAffineX().toByteArray();
byte[] yArr = ((EcpublicKey) keyPair.getPublic()).getW().getAffineY().toByteArray();
byte[] enc2 = new byte[1+2*qLength];
enc2[0] = (byte) 0x04;
System.arraycopy(xArr,enc2,qLength - xArr.length,xArr.length);
System.arraycopy(yArr,2* qLength - yArr.length,yArr.length);

byte res[] =send((byte) 0x00,enc2).getData();

I've tried a few things Now, the code that sends the public key attempts to use x9. 0 as specified in the JavaCard document 62 encode it (uncompressed) However, I also tried the default encoding method, which gave exactly the same result

I can't seem to get any errors from JavaCard Who knows what's wrong? Or does anyone have a working example of how to exchange keys on a JavaCard?

Solution

As Vojta has pointed out:

keyPair = new KeyPair(KeyPair.ALG_EC_FP,KeyBuilder.LENGTH_EC_F2M_163);

Just trying to generate a key pair may work to some extent But FP curve is different from F2m curve. As far as I know, there is no 163 bit FP curve (as far as I know)

This means that you never really install domain parameters unless you generate your own domain parameters, which I will call unlikely

Use FP curves with known key lengths and set parameters, at least for public keys (for jcop cards, you may also have to set them for private keys) You usually use a 224 bit key or higher to ensure security

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>