public class CamelliaKeyWrap
extends javax.crypto.CipherSpi
RFC 3657 (Use of the Camellia Encryption Algorithm in CMS) specifies the Camellia key wrap algorithm to maybe used for wrapping Camellia content encryption keys with Camellia key encryption keys.
When calling an engineInit method any parameters supplied are ignored;
this Camellia key wrap cipher implementation itself takes care for using the right
IV for the right wrap/unwrap step. When calling method
getIV or getParameters this class always returns null
since a Camellia key wrap cipher does not include parameters in its algorithm id.
When creating a new Camellia key wrap Cipher object you only may provide the name of the key wrap cipher ("CamelliaWrapCamellia"). Any cipher mode (always uses ECB) or padding (the input data has always be a multiple of 8) specification is ignored.
For example, wrapping a Camellia 128 bit content encryption key using a Camellia 128 bit key encryption key typically may be done as follows:
// the content encryption key to be wrapped:
SecretKey cek = ...;
// the key encryption key to be used:
SecretKey kek = ...;
// get a Camellia key wrap cipher:
Cipher c = Cipher.getInstance("CamelliaWrapCamellia", "IAIK");
// init with the key encryption key
c.init(Cipher.WRAP_MODE, kek);
// wrap the content encryption key:
byte[] wrappedCek = c.wrap(cek);
For unwrapping the key init the Cipher in unwrap mode:
Cipher c = Cipher.getInstance("CamelliaWrapCamellia", "IAIK");
// init with the key encryption key
c.init(Cipher.UNWRAP_MODE, kek);
// unwrap the wrapped content encryption key:
Key unwrappedCek = c.unwrap(wrappedCek, "Camellia", Cipher.SECRET_KEY);
For using a 192 or 256 bit key encryption key simply create and
init the cipher with a key encryption key of the desired bit length.Cipher,
SecretKey| Modifier and Type | Field and Description |
|---|---|
static byte[] |
NIST_KEY_WRAP_IV
The initial vector defined for the NIST symmetric key wrap algorithm.
|
| Constructor and Description |
|---|
CamelliaKeyWrap()
Creates a new instance of this
CamelliaKeyWrap cipher. |
| Modifier and Type | Method and Description |
|---|---|
byte[] |
engineDoFinal(byte[] input,
int inputOffset,
int inputLen)
Performs the final step of a en/decryption (wrapping/unwrappin) operation
by processing the given input data and any remaining buffered data.
|
int |
engineDoFinal(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
Performs the final step of a en/decryption (wrapping/unwrappin) operation
by processing the given input data and any remaining buffered data.
|
int |
engineGetBlockSize()
Returns the block size corresponding to this cipher.
|
byte[] |
engineGetIV()
Returns a byte array containing the initialization vector (IV).
|
protected int |
engineGetKeySize(java.security.Key key) |
int |
engineGetOutputSize(int inLen)
Returns the output buffer size necessary for capturing the data resulting
from the next
update or doFinal operation
including any data currently being buffered. |
java.security.AlgorithmParameters |
engineGetParameters()
Gets the algorithm parameters used/generated by this Cipher engine.
|
void |
engineInit(int opmode,
java.security.Key key,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom random)
Initializes this cipher object with proper key and algorithm parameter
values, and some random seed.
|
void |
engineInit(int opmode,
java.security.Key key,
java.security.AlgorithmParameters params,
java.security.SecureRandom random)
Initializes this cipher object with proper key and algorithm parameter
values, and some random seed.
|
void |
engineInit(int opmode,
java.security.Key key,
java.security.SecureRandom random)
Initializes this cipher object with a proper key and some random seed.
|
void |
engineSetMode(java.lang.String mode)
Sets the mode of this cipher.
|
void |
engineSetPadding(java.lang.String paddingName)
Sets the padding scheme of this cipher.
|
protected java.security.Key |
engineUnwrap(byte[] wrappedKey,
java.lang.String wrappedKeyAlgorithm,
int wrappedKeyType) |
byte[] |
engineUpdate(byte[] input,
int inputOffset,
int inputLen)
Updates this Cipher with the given data bytes.
|
int |
engineUpdate(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
Updates this Cipher with the given data bytes.
|
protected byte[] |
engineWrap(java.security.Key key) |
int |
getModeBlockSize()
Returns the block size corresponding to the actual cipher mode.
|
java.lang.String |
toString()
Returns a string representation of this Cipher.
|
public static final byte[] NIST_KEY_WRAP_IV
public CamelliaKeyWrap()
CamelliaKeyWrap cipher.
Cipher.getInstance factory
methods instead, e.g.:
Cipher CamelliaKeyWrap = Cipher.getInstance("CamelliaWrapCamellia");
Since the CamelliaKeyWrap cipher only runs in ECB mode and only is used for
wrapping/unwrapping (Camellia content encryption keys) purposes, any mode or padding
specification as part of the transformation string supplied when creating the
Cipher object is ignored.public void engineSetPadding(java.lang.String paddingName)
throws javax.crypto.NoSuchPaddingException
This method only overrides engineSetPadding for not allowing
an application to request a specific padding scheme (the input data always
has to be a multiple of 8).
paddingName - the name of the padding scheme; ignoredjavax.crypto.NoSuchPaddingException - if this padding scheme is not supportedCipherSpi.engineSetPadding(java.lang.String)public void engineSetMode(java.lang.String mode)
throws java.security.NoSuchAlgorithmException
This method only overrides engineSetMode for not allowing an
application to request a specific cipher mode (this key wrap cipher always
uses "ECB").
mode - the cipher mode; ignoredjava.security.NoSuchAlgorithmException - if this cipher mode is not supportedCipherSpi.engineSetMode(java.lang.String)public byte[] engineUpdate(byte[] input,
int inputOffset,
int inputLen)
wrap/ unwrap is called.input - the input data as byte arrayinputOffset - the start position in the input arrayinputLen - the number of bytes that should be processed, starting at
inputOffsetnull since no output is produced by this methodCipherSpi.engineUpdate(byte[], int, int)public int engineUpdate(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
throws javax.crypto.ShortBufferException
wrap/ unwrap is called.input - the input data as byte arrayinputOffset - the start position in the input arrayinputLen - the number of bytes that should be processed, starting at
inputOffsetoutput - the byte array to which to write the result; ignoredoutputOffset - the start position in the output array; ignoredjavax.crypto.ShortBufferException - never thrownCipherSpi.engineUpdate(byte[], int, int, byte[], int)public byte[] engineDoFinal(byte[] input,
int inputOffset,
int inputLen)
throws javax.crypto.IllegalBlockSizeException,
javax.crypto.BadPaddingException
The data to be processed is given in an input byte array. Beginning at
inputOffset, only the first inputLen bytes are
en/decrypted, including any buffered bytes of a previous
update operation. The total length of the input data has to be
a multiple of 8 (which is the case for content encryption keys to be
wrapped). The result is returned as a output byte array.
input - the byte array holding the data to be processedinputOffset - the offset indicating the start position within the input byte
arrayinputLen - the number of bytes to be processedjavax.crypto.ShortBufferException - if the given output buffer is too small for holding the resultjavax.crypto.IllegalBlockSizeException - if the total length of the processed data is not a multiple of
8javax.crypto.BadPaddingException - if a padding error occursCipherSpi.engineDoFinal(byte[], int, int)public int engineDoFinal(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
throws javax.crypto.ShortBufferException,
javax.crypto.IllegalBlockSizeException,
javax.crypto.BadPaddingException
The data to be processed is given in an input byte array. Beginning at
inputOffset, only the first inputLen bytes are
en/decrypted, including any buffered bytes of a previous
update operation. The result is stored in the given output
byte array, beginning at outputOffset. The number of bytes
stored in this byte array are returned.
input - the byte array holding the data to be processedinputOffset - the offset indicating the start position within the input byte
arrayinputLen - the number of bytes to be processedoutput - the byte array for holding the resultoutputOffset - the offset indicating the start position within the output byte
array to which the en/decrypted data is writtenjavax.crypto.ShortBufferException - if the given output buffer is too small for holding the resultjavax.crypto.IllegalBlockSizeException - if the total length of the processed data is not a multiple of
8javax.crypto.BadPaddingException - if a padding error occursCipherSpi.engineDoFinal(byte[], int, int, byte[], int)public void engineInit(int opmode,
java.security.Key key,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom random)
throws java.security.InvalidKeyException,
java.security.InvalidAlgorithmParameterException
Before a cipher object is ready for data processing, it has to be
initialized according to the desired cryptographic operation, which is
specified by the opmode parameter (either ENCRYPT_MODE or
DECCRYPT_MODE), e.g.:
cipher_obj.init(Cipher.ENCRYPT_MODE, key, alg_params, random_seed);
The Cipher init will call the proper CipherSpi
engineInit method.
engineInit in class javax.crypto.CipherSpiopmode - the operation mode for which this cipher is used (ENCRYPT_MODE or
DECRYPT_MODE)key - the keyparams - the algorithm parametersrandom - the random seedjava.security.InvalidKeyException - if the given key cannot be used for initializing this cipherjava.security.InvalidAlgorithmParameterException - if the given algorithm parameters don't match to this cipherCipher.init(int, java.security.Key),
CipherSpi.engineInit(int, java.security.Key, java.security.SecureRandom)public void engineInit(int opmode,
java.security.Key key,
java.security.SecureRandom random)
throws java.security.InvalidKeyException
Before a cipher object is ready for data processing, it has to be
initialized according to the desired cryptographic operation, which is
specified by the opmode parameter (either ENCRYPT_MODE or
DECCRYPT_MODE), e.g.:
cipher_obj.init(Cipher.ENCRYPT_MODE, key, random_seed);
The Cipher init will call the proper CipherSpi
engineInit method.
If this cipher (including its underlying feedback or padding scheme) requires any random bytes, it will get them from random.
engineInit in class javax.crypto.CipherSpiopmode - the operation mode for which this cipher is used (ENCRYPT_MODE or
DECRYPT_MODE)key - the keyrandom - the random seedjava.security.InvalidKeyException - if the given key cannot be used for initializing this cipherCipher.init(int, java.security.Key),
CipherSpi.engineInit(int, java.security.Key, java.security.SecureRandom)public void engineInit(int opmode,
java.security.Key key,
java.security.AlgorithmParameters params,
java.security.SecureRandom random)
throws java.security.InvalidKeyException,
java.security.InvalidAlgorithmParameterException
Before a cipher object is ready for data processing, it has to be
initialized according to the desired cryptographic operation, which is
specified by the opmode parameter (either ENCRYPT_MODE or
DECCRYPT_MODE), e.g.:
cipher_obj.init(Cipher.ENCRYPT_MODE, key, alg_params, random_seed);
The Cipher init will call the proper CipherSpi
engineInit method.
engineInit in class javax.crypto.CipherSpiopmode - the operation mode for which this cipher is used (ENCRYPT_MODE or
DECRYPT_MODE)key - the keyparams - the algorithm parametersrandom - the random seedjava.security.InvalidKeyException - if the given key cannot be used for initializing this cipherjava.security.InvalidAlgorithmParameterException - if the given algorithm parameters don't match to this cipherCipher.init(int, java.security.Key),
CipherSpi.engineInit(int, java.security.Key, java.security.SecureRandom)public java.security.AlgorithmParameters engineGetParameters()
engineGetParameters in class javax.crypto.CipherSpipublic int engineGetOutputSize(int inLen)
update or doFinal operation
including any data currently being buffered.engineGetOutputSize in class javax.crypto.CipherSpiinLen - the number of bytes to processCipher.getOutputSize(int),
CipherSpi.engineGetOutputSize(int)public byte[] engineGetIV()
null is returned.engineGetIV in class javax.crypto.CipherSpinull otherwise.Cipher.getIV(),
CipherSpi.engineGetIV()public int getModeBlockSize()
public int engineGetBlockSize()
engineGetBlockSize in class javax.crypto.CipherSpiCipher.getBlockSize(),
CipherSpi.engineGetBlockSize()public java.lang.String toString()
toString in class java.lang.Objectprotected int engineGetKeySize(java.security.Key key)
throws java.security.InvalidKeyException
engineGetKeySize in class javax.crypto.CipherSpijava.security.InvalidKeyExceptionprotected byte[] engineWrap(java.security.Key key)
throws javax.crypto.IllegalBlockSizeException,
java.security.InvalidKeyException
engineWrap in class javax.crypto.CipherSpijavax.crypto.IllegalBlockSizeExceptionjava.security.InvalidKeyExceptionprotected java.security.Key engineUnwrap(byte[] wrappedKey,
java.lang.String wrappedKeyAlgorithm,
int wrappedKeyType)
throws java.security.InvalidKeyException,
java.security.NoSuchAlgorithmException
engineUnwrap in class javax.crypto.CipherSpijava.security.InvalidKeyExceptionjava.security.NoSuchAlgorithmException