javacard.security.RSAPublicKey Java Examples

The following examples show how to use javacard.security.RSAPublicKey. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SigMsgRecApplet.java    From JCMathLib with MIT License 6 votes vote down vote up
/**
 * Only this class's install method should create the applet object.
 */
protected SigMsgRecApplet(byte[] bArray, short bOffset, byte bLength){       
    byte aidLen = bArray[bOffset];
    if (aidLen== (byte)0){
        //System.out.println("using dfault");
        register();
    } else {
        //System.out.println("using provided");
        register(bArray, (short)(bOffset+1), aidLen);
    }                    
    pubKey = (RSAPublicKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC,KeyBuilder.LENGTH_RSA_512,false);
    privKey = (RSAPrivateKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE,KeyBuilder.LENGTH_RSA_512,false);
    privKey.setExponent(RSA_PRIV_KEY_EXP,(short)0,(short)RSA_PRIV_KEY_EXP.length);
    privKey.setModulus(RSA_PUB_PRIV_KEY_MOD,(short)0,(short)RSA_PUB_PRIV_KEY_MOD.length);
    pubKey.setExponent(RSA_PUB_KEY_EXP,(short)0,(short)RSA_PUB_KEY_EXP.length);
    pubKey.setModulus(RSA_PUB_PRIV_KEY_MOD,(short)0,(short)RSA_PUB_PRIV_KEY_MOD.length);
    sigBuff = JCSystem.makeTransientByteArray((short)(SIG_LENGTH+2),JCSystem.CLEAR_ON_DESELECT);
    sig = (SignatureMessageRecovery)Signature.getInstance(Signature.ALG_RSA_SHA_ISO9796_MR,false);
    recState = (byte)0;
}
 
Example #2
Source File: IsoApplet.java    From IsoApplet with GNU General Public License v3.0 6 votes vote down vote up
/**
 * \brief Encode a 2048 bit RSAPublicKey according to ISO7816-8 table 3 and send it as a response,
 * using an extended APDU.
 *
 * \see ISO7816-8 table 3.
 *
 * \param apdu The apdu to answer. setOutgoing() must not be called already.
 *
 * \param key The RSAPublicKey to send.
 * 			Can be null for the secound part if there is no support for extended apdus.
 */
private void sendRSAPublicKey(APDU apdu, RSAPublicKey key) {
    short le = apdu.setOutgoing();
    short pos = 0;

    ram_buf[pos++] = (byte) 0x7F; // Interindustry template for nesting one set of public key data objects.
    ram_buf[pos++] = (byte) 0x49; // "
    ram_buf[pos++] = (byte) 0x82; // Length field: 3 Bytes.
    ram_buf[pos++] = (byte) 0x01; // Length : 265 Bytes.
    ram_buf[pos++] = (byte) 0x09; // "

    ram_buf[pos++] = (byte) 0x81; // RSA public key modulus tag.
    ram_buf[pos++] = (byte) 0x82; // Length field: 3 Bytes.
    ram_buf[pos++] = (byte) 0x01; // Length: 256 bytes.
    ram_buf[pos++] = (byte) 0x00; // "
    pos += key.getModulus(ram_buf, pos);
    ram_buf[pos++] = (byte) 0x82; // RSA public key exponent tag.
    ram_buf[pos++] = (byte) 0x03; // Length: 3 Bytes.
    pos += key.getExponent(ram_buf, pos);

    sendLargeData(apdu, (short)0, pos);
}
 
Example #3
Source File: GidsApplet.java    From GidsApplet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * \brief Encode a 2048 bit RSAPublicKey according to ISO7816-8 table 3 and send it as a response,
 * using an extended APDU.
 *
 * \see ISO7816-8 table 3.
 *
 * \param apdu The apdu to answer. setOutgoing() must not be called already.
 *
 * \param key The RSAPublicKey to send.
 * 			Can be null for the secound part if there is no support for extended apdus.
 */
private void sendRSAPublicKey(APDU apdu, RSAPublicKey key) {

    short pos = 0;
    short size = key.getSize();
    byte[] ram_buf = transmitManager.GetRamBuffer();
    transmitManager.ClearRamBuffer();

    ram_buf[pos++] = (byte) 0x7F; // Interindustry template for nesting one set of public key data objects.
    ram_buf[pos++] = (byte) 0x49; // "

    if (size < (short) 2048) {
        ram_buf[pos++] = (byte) 0x81; // Length field: 2 Bytes.
        ram_buf[pos++] = (byte) ((size / 8) + 8);
    } else {
        ram_buf[pos++] = (byte) 0x82; // Length field: 3 Bytes.
        Util.setShort(ram_buf, pos, (short)((size / 8) + 9));
        pos += 2;
    }

    ram_buf[pos++] = (byte) 0x81; // RSA public key modulus tag.
    if (size < (short) 2048) {
        ram_buf[pos++] = (byte) 0x81; // Length field: 2 Bytes.
        ram_buf[pos++] = (byte) (size / 8);
    } else {
        ram_buf[pos++] = (byte) 0x82; // Length field: 3 Bytes.
        Util.setShort(ram_buf, pos, (short)(size / 8));
        pos += 2;
    }
    pos += key.getModulus(ram_buf, pos);
    ram_buf[pos++] = (byte) 0x82; // RSA public key exponent tag.
    ram_buf[pos++] = (byte) 0x03; // Length: 3 Bytes.
    pos += key.getExponent(ram_buf, pos);

    transmitManager.sendDataFromRamBuffer(apdu, (short)0, pos);
}
 
Example #4
Source File: Gpg.java    From OpenPGP-Card with GNU General Public License v3.0 5 votes vote down vote up
private short addKeyPart(byte part, byte[] data, short offset, KeyPair key) {
  short size = Util.getShort(commandChainingBuffer, TEMP_PUT_KEY_EXPECTED_CHUNK_SIZE);
  short nextSize = RSA_KEY_HALF_LENGTH_BYTES;
  switch (part) {
    case KEY_PART_E:
      ((RSAPublicKey) key.getPublic()).setExponent(data, offset, size);
      break;
    case KEY_PART_PRIME_P:
      ((RSAPrivateCrtKey) key.getPrivate()).setP(data, offset, size);
      break;
    case KEY_PART_PRIME_Q:
      ((RSAPrivateCrtKey) key.getPrivate()).setQ(data, offset, size);
      break;
    case KEY_PART_PARAM_PQ:
      ((RSAPrivateCrtKey) key.getPrivate()).setPQ(data, offset, size);
      break;
    case KEY_PART_PARAM_DP1:
      ((RSAPrivateCrtKey) key.getPrivate()).setDP1(data, offset, size);
      break;
    case KEY_PART_PARAM_DQ1:
      ((RSAPrivateCrtKey) key.getPrivate()).setDQ1(data, offset, size);
      nextSize = RSA_KEY_LENGTH_BYTES;
      break;

    case KEY_PART_N:
      ((RSAPublicKey) key.getPublic()).setModulus(data, offset, RSA_KEY_LENGTH_BYTES);
      if (!key.getPrivate().isInitialized() ||
          !key.getPublic().isInitialized()) {
        ISOException.throwIt(ISO7816.SW_DATA_INVALID);
      }
      return (short) (offset + RSA_KEY_LENGTH_BYTES);
  }
  Util.setShort(commandChainingBuffer, TEMP_PUT_KEY_EXPECTED_CHUNK_SIZE, nextSize);
  return (short) (offset + size);
}
 
Example #5
Source File: Bignat_Helper.java    From JCMathLib with MIT License 4 votes vote down vote up
void initialize(short modRSAEngineMaxBits, short multRSAEngineMaxBits) {
    MODULO_RSA_ENGINE_MAX_LENGTH_BITS = modRSAEngineMaxBits;
    MULT_RSA_ENGINE_MAX_LENGTH_BITS = multRSAEngineMaxBits;
    
    fnc_deep_resize_tmp = rm.helper_BN_array1;
    fnc_mult_resultArray1 = rm.helper_BN_array1;
    fnc_mult_resultArray2 = rm.helper_BN_array2;

    fnc_same_value_array1 = rm.helper_BN_array1;
    fnc_same_value_hash = rm.helper_BN_array2;
    
    fnc_shift_bytes_right_tmp = rm.helper_BN_array1;
    
    // BN below are just reassigned allocated helper_BN_? so that same helper_BN_? is not used in parallel (checked by lock() unlock())
    fnc_mod_add_tmp = rm.helper_BN_A;

    fnc_mod_sub_tmpThis = rm.helper_BN_A;
    fnc_mod_sub_tmp = rm.helper_BN_B;
    fnc_mod_sub_tmpOther = rm.helper_BN_C;

    fnc_mult_mod_tmpThis = rm.helper_BN_A;
    fnc_mult_mod_tmp_mod = rm.helper_BN_B;
    fnc_mult_mod_tmp_x = rm.helper_BN_C;

    fnc_exponentiation_tmp = rm.helper_BN_A;
    fnc_exponentiation_i = rm.helper_BN_B;

    fnc_mod_minus_2 = rm.helper_BN_B;

    fnc_negate_tmp = rm.helper_BN_B;

    fnc_sqrt_S = rm.helper_BN_A;
    fnc_sqrt_exp = rm.helper_BN_A;
    fnc_sqrt_p_1 = rm.helper_BN_B;
    fnc_sqrt_Q = rm.helper_BN_C;
    fnc_sqrt_tmp = rm.helper_BN_D;
    fnc_sqrt_z = rm.helper_BN_E;

    fnc_mod_mult_tmpThis = rm.helper_BN_E; // mod_mult is called from  fnc_sqrt => requires helper_BN_E not being locked in fnc_sqrt when mod_mult is called

    fnc_divide_tmpThis = rm.helper_BN_E; // divide is called from  fnc_sqrt => requires helper_BN_E not being locked  in fnc_sqrt when divide is called

    fnc_mod_exp_modBN = rm.helper_BN_F;  // mod_exp is called from  fnc_sqrt => requires helper_BN_F not being locked  in fnc_sqrt when mod_exp is called

    fnc_int_add_tmpMag = rm.helper_BN_A;
    fnc_int_multiply_mod = rm.helper_BN_A;
    fnc_int_multiply_tmpThis = rm.helper_BN_B;
    fnc_int_divide_tmpThis = rm.helper_BN_A;        
    
    
    // Allocate BN constants always in EEPROM (only reading)
    ONE = new Bignat((short) 1, JCSystem.MEMORY_TYPE_PERSISTENT, this);
    ONE.one();
    TWO = new Bignat((short) 1, JCSystem.MEMORY_TYPE_PERSISTENT, this);
    TWO.two();
    THREE = new Bignat((short) 1, JCSystem.MEMORY_TYPE_PERSISTENT, this);
    THREE.three();

    tmp_array_short = rm.memAlloc.allocateByteArray((short) 2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET); // only 2b RAM for faster add(short)
    fnc_NmodE_cipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);
    fnc_NmodE_pubKey = (RSAPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, MODULO_RSA_ENGINE_MAX_LENGTH_BITS, false);

    // Speedup for fast multiplication
    fnc_mult_keypair = new KeyPair(KeyPair.ALG_RSA_CRT, MULT_RSA_ENGINE_MAX_LENGTH_BITS);
    fnc_mult_keypair.genKeyPair();
    fnc_mult_pubkey_pow2 = (RSAPublicKey) fnc_mult_keypair.getPublic();
    //mult_privkey_pow2 = (RSAPrivateCrtKey) mult_keypair.getPrivate();
    fnc_mult_pubkey_pow2.setExponent(CONST_TWO, (short) 0, (short) CONST_TWO.length);
    fnc_mult_cipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);

    hashEngine = rm.hashEngine;

    FLAG_FAST_MULT_VIA_RSA = false; // set true only if succesfully allocated and tested below
    try { // Subsequent code may fail on some real (e.g., Infineon CJTOP80K) cards - catch exception
        fnc_mult_cipher.init(fnc_mult_pubkey_pow2, Cipher.MODE_ENCRYPT);
        // Try operation - if doesn't work, exception SW_CANTALLOCATE_BIGNAT is emitted
        Util.arrayFillNonAtomic(fnc_mult_resultArray1, (short) 0, (short) fnc_mult_resultArray1.length, (byte) 6);
        fnc_mult_cipher.doFinal(fnc_mult_resultArray1, (short) 0, (short) fnc_mult_resultArray1.length, fnc_mult_resultArray1, (short) 0);
        FLAG_FAST_MULT_VIA_RSA = true;
    } catch (Exception ignored) {
    } // discard exception                
}
 
Example #6
Source File: GidsApplet.java    From GidsApplet with GNU General Public License v3.0 4 votes vote down vote up
private void sendPublicKey(APDU apdu, PublicKey publicKey) throws InvalidArgumentsException, NotEnoughSpaceException {

        if (publicKey instanceof RSAPublicKey) {
            sendRSAPublicKey(apdu, (RSAPublicKey) publicKey);
        }
    }
 
Example #7
Source File: Gpg.java    From OpenPGP-Card with GNU General Public License v3.0 4 votes vote down vote up
/**
 * GENERATE KEY APDU implementation.
 */
private void generateAsymetricKey(APDU apdu) {
  byte[] buffer = apdu.getBuffer();
  if (apdu.setIncomingAndReceive() != 2) {
    ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
  }
  KeyPair key = getKey(buffer[ISO7816.OFFSET_CDATA]);
  if (buffer[ISO7816.OFFSET_P1] == (byte) 0x81) {
    if (!(key.getPublic()).isInitialized()) {
      ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);
    }
  } else {
    if (!pins[PIN_INDEX_PW3].isValidated()) {
      ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
    }
    JCSystem.beginTransaction();
    key.genKeyPair();
    if (buffer[ISO7816.OFFSET_CDATA] == (byte)0xB6) {
      signatureCounter[0] = 0;
      signatureCounter[1] = 0;
      signatureCounter[2] = 0;
    }
    JCSystem.commitTransaction();
  }
  // Send the TLV data and public exponent using the APDU buffer.
  buffer[ISO7816.OFFSET_CDATA] = 0x7F;
  buffer[(short) (ISO7816.OFFSET_CDATA + 1)] = 0x49;
  buffer[(short) (ISO7816.OFFSET_CDATA + 2)] = (byte) 0x82;
  buffer[(short) (ISO7816.OFFSET_CDATA + 5)] = (byte) 0x82;
  short length = ((RSAPublicKey) key.getPublic()).getExponent(
      buffer, (short) (ISO7816.OFFSET_CDATA + 7));
  buffer[(short) (ISO7816.OFFSET_CDATA + 6)] = (byte) length;
  short pos = (short) (ISO7816.OFFSET_CDATA + 7 + length);
  buffer[pos] = (byte) 0x81;
  buffer[(short) (pos + 1)] = (byte) 0x82;
  Util.setShort(buffer, (short) (pos + 2), RSA_KEY_LENGTH_BYTES);
  Util.setShort(buffer, (short) (ISO7816.OFFSET_CDATA + 3),
                (short) (pos + RSA_KEY_LENGTH_BYTES - ISO7816.OFFSET_CDATA - 1));
  apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, (short) (length + 11));

  // And the modulus using get response.
  Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_LENGTH, RSA_KEY_LENGTH_BYTES);
  ((RSAPublicKey) key.getPublic()).getModulus(commandChainingBuffer, TEMP_GET_RESPONSE_DATA);
  // Skip leading zero byte.
  if (commandChainingBuffer[TEMP_GET_RESPONSE_DATA] == 0) {
    Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_OFFSET,
                  (short) (TEMP_GET_RESPONSE_DATA + 1));
  } else {
    Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_OFFSET, TEMP_GET_RESPONSE_DATA);
  }
  commandChainingBuffer[TEMP_INS] = buffer[ISO7816.OFFSET_INS];
  ISOException.throwIt(ISO7816.SW_BYTES_REMAINING_00);
}