A wrapped key using a standard mode of operation is simply encryption of the encoded data of the key. As the encoded data of an AES key is identical to the raw data, the data of a 256 bit key is simply 32 bytes.
The main difference for these non-specialized modes such as GCM/CBC/ECB is how the key bytes are handled: they are directly used in a SecretKey
instance instead of being returned as bytes. This is of strong importance especially if the operation is performed in hardware (smart card, HSM, TPM) rather than software; the bytes of the wrapped keys can then be kept/protected within the specialized device.
GCM uses CTR mode underneath, which is a stream mode of operation. Stream mode of operation do not require padding of the plaintext, so the ciphertext will simply be 32 bytes as well. Java also includes the authentication tag (t) into the calculation. By default GCM uses the maximum authentication tag size, which is 16 bytes, so this is added to the ciphertext of the key itself, leaving you with 48 bytes. The tag size can be configured using the more specialized GCMParameterSpec
class rather than ivParameterSpec
; note that smaller tag sizes may introduce vulnerabilities for GCM mode.
However, remember that it is required to also be able to re-generate the IV/nonce for GCM mode encryption. So you need to store that as well if it cannot be regenerated from context. Note as well that GCM mode breaks in a horrible way if the nonce is ever reused for the same wrapping key. Most of the time using a fully random nonce and therefore storing it with the ciphertext is of high importance. For GCM it is strongly advisable to use a 12 byte nonce, expanding the ciphertext to 60 bytes.
Alternatively SIV mode or GCM-SIV mode could be used. These modes use the authentication tag as "synthetic" IV. This makes the encryption deterministic (identical plaintext leads to same ciphertext). As a key is supposed to be random by itself, they are very useful for these kind of modes, as they don't require usage of an RNG or storage of the IV. Unfortunately general purpose crypto libraries do often not contain implementations of these modes.