public class HMACwithAESwrap extends AESKeyWrap
RFC 3537 specifies the HMACwithAESwrap key wrap algorithm for wrapping a HMAC message authentication code key with a AES key encryption key. This algorithm may be used for wrapping HMAC keys as required by the CMS (Cryptographic Message Syntax) AuthenticatedData type.
When calling an engineInit
method any parameters supplied are
ignored; this CMS HMACwithAESwrap key wrap cipher implementation itself takes
care for using the right IV for the right en/decryption step. When calling
method getIV
or getParameters
this class always
returns null
since a CMS HMACwithAESwrap key wrap cipher does
not include parameters in its algorithm id.
When creating a new CMS HMACwithAESwrap Cipher object you only may provide the name of the key wrap cipher ("AESwrapHMAC"). Any cipher mode (always uses CBC) or padding (does the padding itself) specification is ignored.
For example, wrapping a HMAC key using an AES key encryption key typically may be done as follows:
// the HMAC key to be wrapped: SecretKey hmacKey = ...; // the key encryption key to be used: SecretKey kek = ...; // get a HMACwithAESwrap cipher: Cipher c = Cipher.getInstance("AESwrapHMAC"); // init with the key encryption key c.init(Cipher.WRAP_MODE, kek); // wrap the HMAC key: byte[] wrappedHmacKey = c.wrap(hmacKey);For unwrapping the key init the Cipher in unwrap mode:
Cipher c = Cipher.getInstance("AESwrapHMAC"); // init with the key encryption key c.init(Cipher.UNWRAP_MODE, kek); // unwrap the wrapped HMAC key: Key unwrappedHmacKey = c.unwrap(wrappedHmacKey, "HmacSHA1", Cipher.SECRET_KEY);
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 |
---|
HMACwithAESwrap()
Creates a new instance of this
HMACwithAESwrap cipher. |
Modifier and Type | Method and Description |
---|---|
byte[] |
engineDoFinal(byte[] input,
int inputOffset,
int inputLen)
Performs the final step of a en/decryption (wrapping/unwrapping) 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 HMACwithAESwrap()
HMACwithAESwrap
cipher. Cipher.getInstance
factory methods instead, e.g.:
Cipher c = Cipher.getInstance("AESwrapHMAC");
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. In encryption/wrap mode the input data
(encoded HMAC key) is padded if it is not a multiple of 8 bytes); in
decryption/unwrap mode the padding is removed again.
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
- should not occur since the data is padded automaticallyjavax.crypto.BadPaddingException
- if the encrypted/wrappped data is incorrectly paddedCipherSpi.engineDoFinal(byte[], int, int)
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 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