Page 1 of 1

Issue about generating 2048 bytes keys with RSA

Posted: Sun Aug 20, 2017 11:50 pm
by jbsoft
I need to generates a pair of 2048 bytes keys with the RSA alg in my applet. The APDU response is expected to be the 256 bytes of the public modulus and SW1-SW2 9000. But It returned 0x6F00. I don't know where I made mistake. Could anyone give me some clues and help me out? My code is shown below.

Code: Select all

import javacard.framework.*;
import javacard.security.*;
import javacardx.crypto.*;

public class RSA extends Applet {

public static final byte INS_GEN_KEYS = 0x46;
private static Key privateKey;
public static Key publicKey;
private static KeyPair keyPair;


     private RSA() {
     }

     public static void install(byte bArray[], short bOffset, byte bLength)
               throws ISOException {
          (new RSA()).register();
     }


     public void process(APDU apdu) throws ISOException {
          byte[] buffer = apdu.getBuffer();
         
          if (selectingApplet()) return ;

          switch(buffer[ISO7816.OFFSET_INS])
          {
               case INS_GEN_KEYS:
                    genKeys(apdu);
                    return;
          }

     }

     private void genKeys(APDU apdu) throws CryptoException {
          byte[] apduBuffer = apdu.getBuffer();
         
          privateKey = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE,KeyBuilder.LENGTH_RSA_2048, false);
          publicKey = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC,KeyBuilder.LENGTH_RSA_2048, false);
         
          keyPair = new KeyPair(KeyPair.ALG_RSA, (short)publicKey.getSize());
          keyPair.genKeyPair();
          publicKey = keyPair.getPublic();

          apduBuffer[0] = (byte)((RSAPublicKey)publicKey).getModulus(apduBuffer, (short)1);
         
          apdu.setOutgoing(); // set transmission to outgoing data
          apdu.setOutgoingLength((short) apduBuffer.length);
          apdu.sendBytesLong(apduBuffer, (short) 0, (short) apduBuffer.length);
     }         
}

Re: Issue about generating 2048 bytes keys with RSA

Posted: Mon Aug 21, 2017 5:13 am
by Crawford
A status word of 0x6F00 is an Unknown Error.

You could try inserting debug code to see what line of code is failing. Add the following line of code near the start and keep moving/redploying and see when you start getting 6F00 instead of 1234.

Code: Select all

ISOException.throwIt((short)0x1234);

Re: Issue about generating 2048 bytes keys with RSA

Posted: Mon Aug 21, 2017 6:33 am
by marjkbadboy
The problem may be caused by the following several lines of code. Donot use apduBuffer.length for the outgoing length. And If the card does not support extended APDUs, you can't return more than 256 bytes in the response.

Code: Select all

apduBuffer[0] = (byte)((RSAPublicKey)publicKey).getModulus(apduBuffer, (short)1);

apdu.setOutgoing(); // set transmission to outgoing data
apdu.setOutgoingLength((short) apduBuffer.length);
apdu.sendBytesLong(apduBuffer, (short) 0, (short) apduBuffer.length);