Class Cipher.OneShot
- java.lang.Object
-
- javacardx.crypto.Cipher
-
- javacardx.crypto.Cipher.OneShot
-
- Enclosing class:
- Cipher
public static final class Cipher.OneShot extends Cipher
TheOneShot
class is a specialization of theCipher
class intended to support efficient one-shot ciphering and deciphering operations that may avoid persistent memory writes entirely. TheOneShot
class uses a delegation model where calls are delegated to an instance of aCipher
-implementing class configured for one-shot use.Note:
- Instances of
OneShot
are JCRE owned temporary Entry Point Object instances and references to these temporary objects cannot be stored in class variables or instance variables or array components. See Runtime Environment Specification, Java Card Platform, Classic Edition, section 6.2.1 for details. - The platform must support at least one instance of
OneShot
. Support for severalOneShot
instances is platform dependent. To guarantee application code portability, acquiring/opening and then releasing/closingOneShot
instances should be performed within tighttry-catch-finally
blocks (as illustrated in the code sample below) in order to avoid unnecessarily keeping hold of instances and to prevent interleaving invocations - hence enforcing the One-Shot usage pattern. Additionally, any local variable holding a reference to aOneShot
instance should be set tonull
once the instance is closed in order to prevent further use attempts. - Upon return from any
Applet
entry point method, back to the JCRE, and on tear or card reset events anyOneShot
instances in use are released back to the JCRE. - The internal state associated with an instance of
OneShot
must be bound to the initial calling context (owner context) as to preclude use/calls on that instance from other contexts. - Unless otherwise specified, after an instance of
OneShot
is released back to the JCRE, calls to any of the instance methods of theOneShot
class results in anCryptoException
being thrown with reason codeCryptoException.ILLEGAL_USE
.
The following code shows a typical usage pattern for the
OneShot
class.... Cipher.OneShot enc = null; try { enc = Cipher.OneShot.open(Cipher.CIPHER_RSA, Cipher.PAD_PKCS1); enc.init(someRSAKey, Cipher.MODE_ENCRYPT); enc.doFinal(someInData, (short) 0, (short) someInData.length, encData, (short) 0); } catch (CryptoException ce) { // Handle exception } finally { if (enc != null) { enc.close(); enc = null; } } ...
- Since:
- 3.0.5
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class javacardx.crypto.Cipher
Cipher.OneShot
-
-
Field Summary
-
Fields inherited from class javacardx.crypto.Cipher
ALG_AES_BLOCK_128_CBC_NOPAD, ALG_AES_BLOCK_128_ECB_NOPAD, ALG_AES_BLOCK_192_CBC_NOPAD, ALG_AES_BLOCK_192_ECB_NOPAD, ALG_AES_BLOCK_256_CBC_NOPAD, ALG_AES_BLOCK_256_ECB_NOPAD, ALG_AES_CBC_ISO9797_M1, ALG_AES_CBC_ISO9797_M2, ALG_AES_CBC_PKCS5, ALG_AES_CFB, ALG_AES_CTR, ALG_AES_ECB_ISO9797_M1, ALG_AES_ECB_ISO9797_M2, ALG_AES_ECB_PKCS5, ALG_AES_XTS, 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_KOREAN_SEED_CBC_NOPAD, ALG_KOREAN_SEED_ECB_NOPAD, ALG_RSA_ISO14888, ALG_RSA_ISO9796, ALG_RSA_NOPAD, ALG_RSA_PKCS1, ALG_RSA_PKCS1_OAEP, CIPHER_AES_CBC, CIPHER_AES_CFB, CIPHER_AES_CTR, CIPHER_AES_ECB, CIPHER_AES_XTS, CIPHER_DES_CBC, CIPHER_DES_ECB, CIPHER_KOREAN_SEED_CBC, CIPHER_KOREAN_SEED_ECB, CIPHER_RSA, CIPHER_SM2, CIPHER_SM4_CBC, CIPHER_SM4_ECB, MODE_DECRYPT, MODE_ENCRYPT, PAD_ISO9796, PAD_ISO9796_MR, PAD_ISO9796_MR_SCHEME_2, PAD_ISO9796_MR_SCHEME_3, PAD_ISO9797_1_M1_ALG3, PAD_ISO9797_1_M2_ALG3, PAD_ISO9797_M1, PAD_ISO9797_M2, PAD_NOPAD, PAD_NULL, PAD_PKCS1, PAD_PKCS1_OAEP, PAD_PKCS1_OAEP_SHA224, PAD_PKCS1_OAEP_SHA256, PAD_PKCS1_OAEP_SHA3_224, PAD_PKCS1_OAEP_SHA3_256, PAD_PKCS1_OAEP_SHA3_384, PAD_PKCS1_OAEP_SHA3_512, PAD_PKCS1_OAEP_SHA384, PAD_PKCS1_OAEP_SHA512, PAD_PKCS1_PSS, PAD_PKCS5, PAD_RFC2409
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes and releases this JCRE owned temporary instance of theOneShot
object for reuse.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.byte
getCipherAlgorithm()
Gets the raw cipher algorithm.byte
getPaddingAlgorithm()
Gets the padding algorithm.void
init(Key theKey, byte theMode)
Initializes theCipher
object with the appropriateKey
.void
init(Key theKey, byte theMode, byte[] bArray, short bOff, short bLen)
Initializes theCipher
object with the appropriate Key and algorithm specific parameters.static Cipher.OneShot
open(byte cipherAlgorithm, byte paddingAlgorithm)
Opens/acquires a JCRE owned temporary Entry Point Object instance ofOneShot
with the selected cipher algorithm and padding algorithm.short
update(byte[] inBuff, short inOffset, short inLength, byte[] outBuff, short outOffset)
Always throws aCryptoException
.-
Methods inherited from class javacardx.crypto.Cipher
getInstance, getInstance
-
-
-
-
Method Detail
-
open
public static final Cipher.OneShot open(byte cipherAlgorithm, byte paddingAlgorithm) throws CryptoException
Opens/acquires a JCRE owned temporary Entry Point Object instance ofOneShot
with the selected cipher algorithm and padding algorithm.Note:
- When the padding algorithm is built into the cipher algorithm use
the
PAD_NULL
choice for the padding algorithm.
- Parameters:
cipherAlgorithm
- the desired cipher algorithm. Valid codes listed inCIPHER_*
constants in this class e.g.CIPHER_AES_CBC
.paddingAlgorithm
- the desired padding algorithm. Valid codes listed inPAD_*
constants in the Cipher class e.g.PAD_NULL
.- Returns:
- the
OneShot
object instance of the requested algorithm. - Throws:
CryptoException
- with the following reason codes:CryptoException.NO_SUCH_ALGORITHM
if the requested cipher algorithm or padding algorithm or their combination is not supported.
SystemException
- with the following reason codes:SystemException.NO_RESOURCE
if sufficient resources are not available.
- When the padding algorithm is built into the cipher algorithm use
the
-
close
public void close()
Closes and releases this JCRE owned temporary instance of theOneShot
object for reuse. If this method is called again this method does nothing.- Throws:
SecurityException
- if this JCRE owned temporary instance of theOneShot
object was opened in a context different from that of the caller.
-
update
public short update(byte[] inBuff, short inOffset, short inLength, byte[] outBuff, short outOffset) throws CryptoException
Always throws aCryptoException
. This method is not supported byOneShot
.- Specified by:
update
in classCipher
- Parameters:
inBuff
- the input buffer of data to be encrypted/decryptedinOffset
- the offset into the input buffer at which to begin encryption/decryptioninLength
- the byte length to be encrypted/decryptedoutBuff
- the output buffer, may be the same as the input bufferoutOffset
- 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.ILLEGAL_USE
always.
-
init
public void init(Key theKey, byte theMode) throws CryptoException
Initializes theCipher
object with the appropriateKey
. This method should be used for algorithms which do not need initialization parameters or use default parameter values.init()
must be used to update theCipher
object with a new key. If theKey
object is modified after invoking theinit()
method, the behavior of theupdate()
anddoFinal()
methods is unspecified.The
Key
is checked for consistency with theCipher
algorithm. For example, the key type must be matched. For elliptic curve algorithms, the key must represent a valid point on the curve's domain parameters. Additional key component/domain parameter strength checks are implementation specific.Note:
- AES, DES, triple DES and Korean SEED algorithms used in modes requiring an initial vector (like CBC, CFB, CTR, XTS modes) will use 0 for initial vector(IV) if this method is used.
- For optimal performance, when the
theKey
parameter is a transient key, the implementation should, whenever possible, use transient space for internal storage.
- Specified by:
init
in classCipher
- Parameters:
theKey
- the key object to use for encrypting or decryptingtheMode
- one ofMODE_DECRYPT
orMODE_ENCRYPT
- Throws:
SecurityException
- if this JCRE owned temporary instance of theOneShot
object was opened in a context different from that of the caller.CryptoException
- with the following reason codes:CryptoException.ILLEGAL_VALUE
iftheMode
option is an undefined value or if theKey
is inconsistent with theCipher
implementation.CryptoException.UNINITIALIZED_KEY
iftheKey
instance is uninitialized.
-
init
public void init(Key theKey, byte theMode, byte[] bArray, short bOff, short bLen) throws CryptoException
Initializes theCipher
object with the appropriate Key and algorithm specific parameters.init()
must be used to update theCipher
object with a new key. If theKey
object is modified after invoking theinit()
method, the behavior of theupdate()
anddoFinal()
methods is unspecified.The
Key
is checked for consistency with theCipher
algorithm. For example, the key type must be matched. For elliptic curve algorithms, the key must represent a valid point on the curve's domain parameters. Additional key component/domain parameter strength checks are implementation specific.Note:
- DES and triple DES algorithms in CBC mode expect an 8-byte parameter value for
the initial vector(IV) in
bArray
. - AES algorithms expect a 16-byte parameter value in
bArray
for the initial vector(IV) in CBC, CFB, CTR mode or for the value of the 128-bit tweak in XTS mode. - Korean SEED 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, Korean SEED algorithm in ECB mode,
RSA, DSA and SM2 algorithms throw
CryptoException.ILLEGAL_VALUE
. - For optimal performance, when the
theKey
parameter is a transient key, the implementation should, whenever possible, use transient space for internal storage.
- Specified by:
init
in classCipher
- Parameters:
theKey
- the key object to use for encrypting or decrypting.theMode
- one ofMODE_DECRYPT
orMODE_ENCRYPT
bArray
- byte array containing algorithm specific initialization infobOff
- offset within bArray where the algorithm specific data beginsbLen
- byte length of algorithm specific parameter data- Throws:
SecurityException
- if this JCRE owned temporary instance of theOneShot
object was opened in a context different from that of the caller.CryptoException
- with the following reason codes:CryptoException.ILLEGAL_VALUE
iftheMode
option is an undefined value or if a byte array parameter option is not supported by the algorithm or if thebLen
is an incorrect byte length for the algorithm specific data or if theKey
is inconsistent with theCipher
implementation.CryptoException.UNINITIALIZED_KEY
iftheKey
instance is uninitialized.
- DES and triple DES algorithms in CBC mode expect an 8-byte parameter value for
the initial vector(IV) in
-
getAlgorithm
public byte getAlgorithm()
Gets the Cipher algorithm.- Specified by:
getAlgorithm
in classCipher
- Returns:
- the algorithm code defined above; if the algorithm is not one of the pre-defined
algorithms,
0
is returned. - Throws:
SecurityException
- if this JCRE owned temporary instance of theOneShot
object was opened in a context different from that of the caller.- See Also:
Cipher.getInstance(byte, boolean)
-
getCipherAlgorithm
public byte getCipherAlgorithm()
Gets the raw cipher algorithm. Pre-defined codes listed inCIPHER_*
constants in this class e.g.CIPHER_AES_CBC
.- Specified by:
getCipherAlgorithm
in classCipher
- Returns:
- the raw cipher algorithm code defined above; if the algorithm is not
one of the pre-defined algorithms,
0
is returned. - Throws:
SecurityException
- if this JCRE owned temporary instance of theOneShot
object was opened in a context different from that of the caller.
-
getPaddingAlgorithm
public byte getPaddingAlgorithm()
Gets the padding algorithm. Pre-defined codes listed inPAD_*
constants in this class e.g.PAD_NULL
.- Specified by:
getPaddingAlgorithm
in classCipher
- Returns:
- the padding algorithm code defined in the
Cipher
class; if the algorithm is not one of the pre-defined algorithms,0
is returned. - Throws:
SecurityException
- if this JCRE owned temporary instance of theOneShot
object was opened in a context different from that of the caller.
-
doFinal
public short doFinal(byte[] inBuff, short inOffset, short inLength, byte[] outBuff, short outOffset) throws CryptoException
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 theupdate()
method as well as input data supplied in theinBuff
parameter.A call to this method also resets this
Cipher
object to the state it was in when previously initialized via a call toinit()
. That is, the object is reset and available to encrypt or decrypt (depending on the operation mode that was specified in the call toinit()
) more data. In addition, note that the initial vector(IV) used in AES, DES and Korean SEED 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
refer to the same array, or if any of these arguments refer to an array view sharing components with the other argument, then the output data area must not partially overlap the input data area such that the input data is modified before it is used.
Example: ifinBuff==outBuff
andinOffset < 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.
Example: ifinBuff==outBuff
andoutOffset < inOffset+inLength
, incorrect output may result. - AES, DES, triple DES and Korean SEED 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 thaninLength
or even 0. - On decryption operations resulting in an
ArrayIndexOutOfBoundsException
,outBuff
may be partially modified.
In addition to returning a
short
result, this method sets the result in an internal state which can be rechecked using assertion methods of theSensitiveResult
class, if supported by the platform.- Specified by:
doFinal
in classCipher
- Parameters:
inBuff
- the input buffer of data to be encrypted/decryptedinOffset
- the offset into the input buffer at which to begin encryption/decryptioninLength
- the byte length to be encrypted/decryptedoutBuff
- the output buffer, may be the same as the input bufferoutOffset
- the offset into the output buffer where the resulting output data begins- Returns:
- number of bytes output in
outBuff
- Throws:
SecurityException
- if this JCRE owned temporary instance of theOneShot
object was opened in a context different from that of the caller.CryptoException
- with the following reason codes:CryptoException.UNINITIALIZED_KEY
if key not initialized.CryptoException.INVALID_INIT
if thisCipher
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 ininBuff
or via theupdate()
method. - The input message length is not supported or the message value is greater than or equal to the modulus.
- The decrypted data is not bounded by appropriate padding bytes.
- This
- When using block-aligned data (multiple of block size),
if the input buffer,
-
-