SymmetricCipherImpl



com.licel.jcardsim.crypto
Class SymmetricCipherImpl

java.lang.Object
  extended by javacardx.crypto.Cipher
      extended by com.licel.jcardsim.crypto.SymmetricCipherImpl

public class SymmetricCipherImpl
extends Cipher

Implementation Cipher with symmetric keys based on BouncyCastle CryptoAPI

See Also:
Cipher

Field Summary
 
Fields inherited from class javacardx.crypto.Cipher
ALG_AES_BLOCK_128_CBC_NOPAD, ALG_AES_BLOCK_128_ECB_NOPAD, ALG_DES_CBC_ISO9797_M1, ALG_DES_CBC_ISO9797_M2, ALG_DES_CBC_NOPAD, ALG_DES_CBC_PKCS5, ALG_DES_ECB_ISO9797_M1, ALG_DES_ECB_ISO9797_M2, ALG_DES_ECB_NOPAD, ALG_DES_ECB_PKCS5, ALG_RSA_ISO14888, ALG_RSA_ISO9796, ALG_RSA_NOPAD, ALG_RSA_PKCS1, ALG_RSA_PKCS1_OAEP, MODE_DECRYPT, MODE_ENCRYPT
 
Constructor Summary
SymmetricCipherImpl(byte algorithm)
           
 
Method Summary
 short doFinal(byte[] inBuff, short inOffset, short inLength, byte[] outBuff, short outOffset)
          Generates encrypted/decrypted output from all/last input data.
 byte getAlgorithm()
          Gets the Cipher algorithm.
 void init(Key theKey, byte theMode)
          Initializes the Cipher object with the appropriate Key.
 void init(Key theKey, byte theMode, byte[] bArray, short bOff, short bLen)
          Initializes the Cipher object with the appropriate Key and algorithm specific parameters.
 short update(byte[] inBuff, short inOffset, short inLength, byte[] outBuff, short outOffset)
          Generates encrypted/decrypted output from input data.
 
Methods inherited from class javacardx.crypto.Cipher
getInstance
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SymmetricCipherImpl

public SymmetricCipherImpl(byte algorithm)
Method Detail

init

public void init(Key theKey,
                 byte theMode)
          throws CryptoException
Description copied from class: Cipher
Initializes the Cipher object with the appropriate Key. This method should be used for algorithms which do not need initialization parameters or use default parameter values.

init() must be used to update the Cipher object with a new key. If the Key object is modified after invoking the init() method, the behavior of the update() and doFinal() methods is unspecified.

Note:

  • AES, DES, and triple DES algorithms in CBC mode will use 0 for initial vector(IV) if this method is used.

Specified by:
init in class Cipher
Parameters:
theKey - the key object to use for encrypting or decrypting
theMode - one of MODE_DECRYPT or MODE_ENCRYPT
Throws:
CryptoException - with the following reason codes:
  • CryptoException.ILLEGAL_VALUE if theMode option is an undefined value or if the Key is inconsistent with the Cipher implementation.
  • CryptoException.UNINITIALIZED_KEY if theKey instance is uninitialized.

init

public void init(Key theKey,
                 byte theMode,
                 byte[] bArray,
                 short bOff,
                 short bLen)
          throws CryptoException
Description copied from class: Cipher
Initializes the Cipher object with the appropriate Key and algorithm specific parameters.

init() must be used to update the Cipher object with a new key. If the Key object is modified after invoking the init() method, the behavior of the update() and doFinal() methods is unspecified.

Note:

  • DES and triple DES algorithms in CBC mode expect an 8-byte parameter value for the initial vector(IV) in bArray.
  • AES algorithms in CBC mode expect a 16-byte parameter value for the initial vector(IV) in bArray.
  • AES algorithms in ECB mode, DES algorithms in ECB mode, RSA and DSA algorithms throw CryptoException.ILLEGAL_VALUE.

Specified by:
init in class Cipher
Parameters:
theKey - the key object to use for encrypting or decrypting.
theMode - one of MODE_DECRYPT or MODE_ENCRYPT
bArray - byte array containing algorithm specific initialization info
bOff - offset within bArray where the algorithm specific data begins
bLen - byte length of algorithm specific parameter data
Throws:
CryptoException - with the following reason codes:
  • CryptoException.ILLEGAL_VALUE if theMode option is an undefined value or if a byte array parameter option is not supported by the algorithm or if the bLen is an incorrect byte length for the algorithm specific data or if the Key is inconsistent with the Cipher implementation.
  • CryptoException.UNINITIALIZED_KEY if theKey instance is uninitialized.

getAlgorithm

public byte getAlgorithm()
Description copied from class: Cipher
Gets the Cipher algorithm.

Specified by:
getAlgorithm in class Cipher
Returns:
the algorithm code defined above

doFinal

public short doFinal(byte[] inBuff,
                     short inOffset,
                     short inLength,
                     byte[] outBuff,
                     short outOffset)
              throws CryptoException
Description copied from class: Cipher
Generates encrypted/decrypted output from all/last input data. This method must be invoked to complete a cipher operation. This method processes any remaining input data buffered by one or more calls to the update() method as well as input data supplied in the inBuff parameter.

A call to this method also resets this Cipher object to the state it was in when previously initialized via a call to init(). That is, the object is reset and available to encrypt or decrypt (depending on the operation mode that was specified in the call to init()) more data. In addition, note that the initial vector(IV) used in AES and DES algorithms will be reset to 0.

Notes:

  • When using block-aligned data (multiple of block size), if the input buffer, inBuff and the output buffer, outBuff are the same array, then the output data area must not partially overlap the input data area such that the input data is modified before it is used; if inBuff==outBuff and
    inOffset < outOffset < inOffset+inLength, incorrect output may result.
  • When non-block aligned data is presented as input data, no amount of input and output buffer data overlap is allowed; if inBuff==outBuff and
    outOffset < inOffset+inLength, incorrect output may result.
  • AES, DES, and triple DES algorithms in CBC mode reset the initial vector(IV) to 0. The initial vector(IV) can be re-initialized using the init(Key, byte, byte[], short, short) method.
  • On decryption operations (except when ISO 9797 method 1 padding is used), the padding bytes are not written to outBuff.
  • On encryption and decryption operations, the number of bytes output into outBuff may be larger or smaller than inLength or even 0.
  • On decryption operations resulting in an ArrayIndexOutOfBoundException, outBuff may be partially modified.

Specified by:
doFinal in class Cipher
Parameters:
inBuff - the input buffer of data to be encrypted/decrypted
inOffset - the offset into the input buffer at which to begin encryption/decryption
inLength - the byte length to be encrypted/decrypted
outBuff - the output buffer, may be the same as the input buffer
outOffset - the offset into the output buffer where the resulting output data begins
Returns:
number of bytes output in outBuff
Throws:
CryptoException - with the following reason codes:
  • CryptoException.UNINITIALIZED_KEY if key not initialized.
  • CryptoException.INVALID_INIT if this Cipher object is not initialized.
  • CryptoException.ILLEGAL_USE if one of the following conditions is met:
    • This Cipher algorithm does not pad the message and the message is not block aligned.
    • This Cipher algorithm does not pad the message and no input data has been provided in inBuff or via the update() method.
    • The input message length is not supported.
    • The decrypted data is not bounded by appropriate padding bytes.

update

public short update(byte[] inBuff,
                    short inOffset,
                    short inLength,
                    byte[] outBuff,
                    short outOffset)
             throws CryptoException
Description copied from class: Cipher
Generates encrypted/decrypted output from input data. This method is intended for multiple-part encryption/decryption operations.

This method requires temporary storage of intermediate results. In addition, if the input data length is not block aligned (multiple of block size) then additional internal storage may be allocated at this time to store a partial input data block. This may result in additional resource consumption and/or slow performance.

This method should only be used if all the input data required for the cipher is not available in one byte array. If all the input data required for the cipher is located in a single byte array, use of the doFinal() method to process all of the input data is recommended. The doFinal() method must be invoked to complete processing of any remaining input data buffered by one or more calls to the update() method.

Notes:

  • When using block-aligned data (multiple of block size), if the input buffer, inBuff and the output buffer, outBuff are the same array, then the output data area must not partially overlap the input data area such that the input data is modified before it is used; if inBuff==outBuff and
    inOffset < outOffset < inOffset+inLength, incorrect output may result.
  • When non-block aligned data is presented as input data, no amount of input and output buffer data overlap is allowed; if inBuff==outBuff and
    outOffset < inOffset+inLength, incorrect output may result.
  • On decryption operations(except when ISO 9797 method 1 padding is used), the padding bytes are not written to outBuff.
  • On encryption and decryption operations, block alignment considerations may require that the number of bytes output into outBuff be larger or smaller than inLength or even 0.
  • If inLength is 0 this method does nothing.

Specified by:
update in class Cipher
Parameters:
inBuff - the input buffer of data to be encrypted/decrypted
inOffset - the offset into the input buffer at which to begin encryption/decryption
inLength - the byte length to be encrypted/decryptedv
outBuff - the output buffer, may be the same as the input buffer
outOffset - the offset into the output buffer where the resulting ciphertext/plaintext begins
Returns:
number of bytes output in outBuff
Throws:
CryptoException - with the following reason codes:
  • CryptoException.UNINITIALIZED_KEY if key not initialized.
  • CryptoException.INVALID_INIT if this Cipher object is not initialized.
  • CryptoException.ILLEGAL_USE if the input message length is not supported.