Page 1 of 1

How to set EC parameters of Keypair

Posted: Fri Jul 13, 2018 5:03 am
by DaHuFa
hello, everyone.
I have a problem with KeyPair. I want generate a EC keypair with KeyPair.genKeyPair(). And I hope the EC components parameters is my indicated parameters. But I not know how to set those parameters. Who can help me?

Re: How to set EC parameters of Keypair

Posted: Fri Jul 13, 2018 5:10 am
by happy123654
You can set the parameter via the API :setA; setB; setG; setR; setK;setFieldFP;setFieldF2M.
If you don't set the parameter into the card cos. the cos may use the default parameter.

Re: How to set EC parameters of Keypair

Posted: Fri Jul 13, 2018 5:22 am
by DaHuFa
You can set the parameter via the API :setA; setB; setG; setR; setK;setFieldFP;setFieldF2M.
If you don't set the parameter into the card cos. the cos may use the default parameter.


Thank for your answer very much. I try this way your mentioned. But the parameters is still not my expected.
My applet is:

Code: Select all

if((short)256 == keyLen){
         key.setA(SECP256R1_A, (short)0, (short)SECP256R1_A.length);
         key.setB(SECP256R1_B, (short)0, (short)SECP256R1_B.length);
         key.setFieldFP(SECP256R1_FP, (short)0, (short)SECP256R1_FP.length);
         key.setG(SECP256R1_G, (short)0, (short)SECP256R1_G.length);
         key.setR(SECP256R1_R, (short)0, (short)SECP256R1_R.length);
              }
             
short keyLen = Util.makeShort(buf[ISO7816.OFFSET_P1], buf[ISO7816.OFFSET_P2]);
         try{
            ecPair = new KeyPair(KeyPair.ALG_EC_FP, keyLen);
         }catch(CryptoException e)
         {
            ISOException.throwIt((short)(0x6900 + e.getReason()));
         }

Re: How to set EC parameters of Keypair

Posted: Sat Jul 14, 2018 9:36 pm
by scplatform
You should set ECC parameters before call genKeyPair(), as the description of jcapi v2.2.2, just need to set the public key parameters, and then, call genKeyPair(), e.g:

Code: Select all

ECPrivateKey priKey = new ECPrivateKeyImpl(KeyBuilder.ALG_TYPE_EC_FP_PRIVATE, KeyBuilder.LENGTH_EC_FP_256, JCSystem.MEMORY_TYPE_PERSISTENT);
      ECPublicKey pubKey = new ECPublicKeyImpl(KeyBuilder.ALG_TYPE_EC_FP_PUBLIC, KeyBuilder.LENGTH_EC_FP_256, JCSystem.MEMORY_TYPE_PERSISTENT);
      pubKey.setA(SECP256R1_A, (short)0, (short)SECP256R1_A.length);
      pubKey.setB(SECP256R1_B, (short)0, (short)SECP256R1_B.length);
      pubKey.setFieldFP(SECP256R1_FP, (short)0, (short)SECP256R1_FP.length);
      pubKey.setG(SECP256R1_G, (short)0, (short)SECP256R1_G.length);
      pubKey.setR(SECP256R1_R, (short)0, (short)SECP256R1_R.length);
      pubKey.setK(cofactor);
      KeyPair kp = new KeyPair(pubKey, priKey);
      kp.genKeyPair();//You can get the pubkey and private key

Re: How to set EC parameters of Keypair

Posted: Sun Jul 15, 2018 11:49 pm
by DaHuFa
It works! Thank you very much~

Re: How to set EC parameters of Keypair

Posted: Wed Oct 31, 2018 2:56 am
by BirdKing
If you call KeyPair() as KeyPair(KeyPair.ALG_EC_FP, keyLen), the encapsulated keys are uninitialized. Then genKeyPair() will initialize the key with default paramters.
If you call KeyPair() as KeyPair(pubKey, priKey), the encapsulated keys will be initialized with the parameters of pubKey.