AES

This algorithm is an implementation of AES, which is a 64 bit block cipher with a variable length key 128, 192 or 256 bits long.

AES Cipher Initialization

This cipher supports both ECB and CBC modes, and may be used with NoPadding or PKCS5Padding.  To create an instance of this class use the Cipher.getInstance()method with “SAFENET” as the provider and one of the following strings as the transformation:

>AES

>AES/ECB/NoPadding

>AES/CBC/NoPadding

>AES/CBC/PKCS5Padding

Using the “AES” transformation, the Cipher will default to ECB and NoPadding.

If the NoPadding mode is selected, the input data must be a multiple of 8 bytes; otherwise, the encrypted or decrypted result will be truncated. In PKCS5Padding, arbitrary data lengths are accepted; the ciphertext will be padded to a multiple of 8 bytes, as described in PKCS#5. The decryption process will remove the padding from the data so that the correct plaintext is returned.

This Cipher will accept a javax.crypto.spec.SecretKeySpec or au.com.safenet.crypto.provider.CryptokiSecretKey as the key parameter during initialization.

When the Cipher is initialized in CBC mode, the Initialization Vector (IV) may be specified by passing a javax.crypto.spec.IvParameterSpec instance to the Cipher.init() method. When decrypting in this mode, a valid IV must be specified in the Cipher.init() method. For encryption, however, a random IV will be generated if none is specified (the IV may be retrieved using the Cipher.getIV() method).

The IV may be provided as a java.security.AlgorithmParameters or a javax.crypto.spec.IvParameterSpec instance. If the initialization is done using an AlgorithmParameters instance, it must be convertible to an IvParameterSpec using the AlgorithmParameters.getParameterSpec() method.

This Cipher does not support the Cipher.getParameters() method; this method will always return null. The only supported parameter for this class is the initialization vector, which may be determined using the Cipher.getIV() method.

AES Key

The AES Cipher requires either a SecretKeySpec or SafeNet ProtectToolkit-J provider AES Key during initialization. AES keys can be 128, 192, or 256 bits long.

To create an appropriate SecretKeySpec, pass a 16, 24 or 32 byte array and the algorithm name “AES” to the SecretKeySpec constructor.

For example:

byte[] keyBytes = { 0x41, 0x22, 0x35, 0x17,
                    0x39, 0xB6, 0xDC, 0x34,
                    0x11, 0x93, 0x55, 0x67,
                    0x39, 0xAC, 0xCD, 0xFF };
SecretKeySpec aesKey = new SecretKeySpec(keyBytes, “AES”);  

Alternatively, a random SafeNet ProtectToolkit-J AES key can be generated using the KeyGenerator as described in Key Generation, or a provider-independent form. The AES key may also be stored in the SafeNet ProtectToolkit-J KeyStore, as described in Key Storage.

The SafeNet ProtectToolkit-J AES key will return the string “AES” as its algorithm name, “RAW” as its encoding. However, since the key is stored within the hardware the actual key encoding may not be available.

The key value can only be extracted from a key if the associated Cryptoki key is not marked as Sensitive. The keys generated in SafeNet ProtectToolkit-J will always be marked as sensitive. It is possible, however, to access any Cryptoki keys stored on the device, and it is possible that the attributes of these keys have been modified.

AES KeyGenerator

The AES KeyGenerator is used to generate random AES keys. The generated key will be a hardware key that has the Cryptoki CKA_EXTRACTABLE and CKA_SENSITIVE attributes set. Since these keys are marked as sensitive their getEncoded() method will return null.

During initialization, the strength parameter may only be 128, 192, or 256 bits, with the default size being 128 bits. The random parameter is ignored as the hardware includes a cryptographically-secure random source.

Keys generated using the KeyGenerator are not thread-safe. That is, a SafeNet ProtectToolkit-J Key instance may only be used by a single Cipher instance (as well as a single MAC instance) at any given time. SeeKey Generation for information on threading and SafeNet ProtectToolkit-J keys.

AES SecretKeyFactory

The AES SecretKeyFactory is used to construct SafeNet ProtectToolkit-J keys from their provider-independent form. The provider-independent form of the AES key is the au.com.safenet.crypto.spec.AESKeySpec class.

Keys generated using the SecretKeyFactory are not thread-safe. That is, a SafeNet ProtectToolkit-J Key instance may only be used by a single Cipher instance (as well as a single MAC instance) at any given time. SeeKey Generation for information on threading and SafeNet ProtectToolkit-J keys.

For example, to create the provider-based key from its provider-independent form:

byte[] keyBytes = { 0x41, 0x22, 0x35, 0x17,
                    0x39, 0xDB, 0xDC, 0xEF
                    0x11, 0x93, 0x55, 0x67,
                    0x39, 0xAC, 0xCD, 0xFF };
AESKeySpec ideaKeySpec = new AESKeySpec(keyBytes);
SecretKeyFactory aesKeyFact =
           SecretKeyFactory.getInstance(“AES”, “SAFENET”);
SecretKey aesKey = aesKeyFact.generateSecret(aesKeySpec);  

AES Example Code

See DES for the simple DES example. To convert the example to use AES, use “AES” in place of “DES”.