public class AESKeyWrapWithPadding extends AESKeyWrap
The AES key wrap algorithm specified by RFC 3394 maybe used for wrapping AES content encryption keys with AES key encryption keys. However, RFC 3394 requires that the size of the to-be-wrapped key is a multiple of 64 bits. As an extension to RFC 3394 a padding mechanism for the AES key wrap algorithm is specified in RFC 5649 allowing to also wrap keys with a size that is not a multiple of 64 bits.
When calling an engineInit
method any parameters supplied are ignored;
this AES key wrap cipher implementation itself takes care for using the right
initialization vector for the right wrap/unwrap step. When calling method
getIV
or getParameters
this class always returns null
since a AES key wrap cipher does not include parameters in its algorithm id.
When creating a new AES key wrap Cipher object you only may provide the name of the key wrap cipher ("AESWrapWithPadding"). Any cipher mode (always uses ECB) or padding (uses RFC 5649 padding) specification is ignored.
For example, wrapping some key using a AES 128 bit key encryption key typically may be done as follows:
// the key to be wrapped: String keyAlg = ...; Key keyToBeWrapped = ...; // the key encryption key to be used: SecretKey kek = ...; // get a AES key wrap cipher: Cipher c = Cipher.getInstance("AESWrapWithPadding", "IAIK"); // init with the key encryption key c.init(Cipher.WRAP_MODE, kek); // wrap the key: byte[] wrappedKey = c.wrap(keyToBeWrapped);For unwrapping the key init the Cipher in unwrap mode:
Cipher c = Cipher.getInstance("AESWrapWithPadding", "IAIK"); // init with the key encryption key c.init(Cipher.UNWRAP_MODE, kek); // unwrap the wrapped key: Key unwrappedKey = c.unwrap(wrappedKey, keyAlg, 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.
The maximum size of keys that can be wrapped using the algorithm specified by RFC 5649 is 232 octets. This is more than a byte array can hold, thus this implementation does not check for upper key size limits. However, an application using this algorithm may impose upper key size bounds that are much less than 232 octets (see RFC 5649).
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 |
---|
AESKeyWrapWithPadding()
Creates a new instance of this
AESKeyWrapWithPadding 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.
|
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 void |
engineUpdateAAD(byte[] src,
int offset,
int len) |
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.
|
engineSetPadding
public static final byte[] NIST_KEY_WRAP_IV
public AESKeyWrapWithPadding()
AESKeyWrapWithPadding
cipher.
Cipher.getInstance
factory
methods instead, e.g.:
Cipher cipher = Cipher.getInstance("AESKeyWrapWithPadding");Since the AESKeyWrapWithPadding cipher only runs in ECB mode and uses its inherent padding mechanism, any mode or padding specification as part of the transformation string supplied when creating the Cipher object is ignored.
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
inputOffset
null
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
inputOffset
output
- 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.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.CipherSpi
opmode
- 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.CipherSpi
opmode
- 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.CipherSpi
opmode
- 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.CipherSpi
protected void engineUpdateAAD(byte[] src, int offset, int len)
engineUpdateAAD
in class javax.crypto.CipherSpi
public int engineGetOutputSize(int inLen)
update
or doFinal
operation
including any data currently being buffered.engineGetOutputSize
in class javax.crypto.CipherSpi
inLen
- the number of bytes to processCipher.getOutputSize(int)
,
CipherSpi.engineGetOutputSize(int)
public byte[] engineGetIV()
null
is returned.engineGetIV
in class javax.crypto.CipherSpi
null
otherwise.Cipher.getIV()
,
CipherSpi.engineGetIV()
public int getModeBlockSize()
public int engineGetBlockSize()
engineGetBlockSize
in class javax.crypto.CipherSpi
Cipher.getBlockSize()
,
CipherSpi.engineGetBlockSize()
public java.lang.String toString()
toString
in class java.lang.Object
protected int engineGetKeySize(java.security.Key key) throws java.security.InvalidKeyException
engineGetKeySize
in class javax.crypto.CipherSpi
java.security.InvalidKeyException
protected byte[] engineWrap(java.security.Key key) throws javax.crypto.IllegalBlockSizeException, java.security.InvalidKeyException
engineWrap
in class javax.crypto.CipherSpi
javax.crypto.IllegalBlockSizeException
java.security.InvalidKeyException
protected java.security.Key engineUnwrap(byte[] wrappedKey, java.lang.String wrappedKeyAlgorithm, int wrappedKeyType) throws java.security.InvalidKeyException, java.security.NoSuchAlgorithmException
engineUnwrap
in class javax.crypto.CipherSpi
java.security.InvalidKeyException
java.security.NoSuchAlgorithmException