IAIK PKCS#11 Provider Micro Edition
version 1.0

iaik.pkcs.pkcs11.me
Class Cipher

java.lang.Object
  extended byiaik.pkcs.pkcs11.me.Cipher

public class Cipher
extends java.lang.Object

This class encrypts and decrypts data using the associated token. The used cipher can be an asymmetric cipher as well as a symmetric cipher. In addition, it supports wrapping and unwrapping of symmetric keys; i.e. encrypting and decrypting symmetric keys with another key on the token. In this case, the key encryption key is usually an asymmetric key (e.g. RSA).

The application must initiate a cipher operation using the init(int, Key, byte[]) method. Thereafter, it feeds in the input data (i.e. the data to be encrypted, or the encrypted data) using update(byte[]). To finish the operation, the application calls doFinal(). The application should always finish each operation, otherwise resources may be bound until the operation gets finished.

For key-wrapping and key-unwrapping, the application calls init(int, Key, byte[]) and then wrap(Key) or unwrap(byte[], String, int, String, boolean) respectively, optionally followed by some more wrap(Key) or unwrap(byte[], String, int, String, boolean) calls. Remember that wrap(Key) and unwrap(byte[], String, int, String, boolean) rest the Cipher object into its last initialized state. Note, that there is no doFinal() call for key-wrapping and key-unwrapping.

A typical piece of code using this class may look like this.

  Token token = ... // get token from module
  long algorithm = ALGORITHM_RSA_PKCS1PADDING;
  if (!token.supportsAlgorithm(algorithm)) {
    ... // token does not support this cipher algorithm
  }
 
  // login user first to be able to see private key objects on the token
  char[] pin = ... // e.g. prompt the PIN from the user
  token.loginUser(pin);
 
  // get a key store view of the keys and certificates on the token
  KeyStore keyStore = token.getKeyStore();
  String alias = ... // select a key from the key store
  Key key = keyStore.getKey(alias);
  if (!key.canBeUsedFor(Key.USAGE_DECRYPTION)) {
    ... // this key cannot be used for decryption
  }
  
  // also get the DER encoded certificate for the key; 
  // e.g. for matching with the encryption certificate 
  byte[] certificate = keyStore.getCertificate(alias);
 
  Cipher cipher = token.getCipher(algorithm);
  cipher.init(Cipher.DECRYPT_MODE, key);
  byte[] dataToBeDecrypted = ... // assign encrypted data
  cipher.update(dataToBeDecrypted);
  byte[] decryptedData = cipher.doFinal();
 
The variable decryptedData will hold the decrypted data; e.g. a symmetric key value which was encrypted.

This class is not thread safe.

See Also:
Token, Token.getCipher(long), Token.supportsAlgorithm(long, int), Key.canBeUsedFor(long)

Field Summary
static long ALGORITHM_AES_CBC_PKCS5PADDING
          The algorithm code constant for the AES block cipher according to FIPS PUB 197 with PKCS#5 padding as specified in PKCS#5 version 1.5.
static long ALGORITHM_AES_ECB
          The algorithm code constant for the AES block cipher according to FIPS PUB 197 without any padding.
static long ALGORITHM_DES_CBC_PKCS5PADDING
          The algorithm code constant for the DES block cipher according to FIPS PUB 46-3 with PKCS#5 padding as specified in PKCS#5 version 1.5.
static long ALGORITHM_DES_ECB
          The algorithm code constant for the DES block cipher according to FIPS PUB 46-3 without any padding.
static long ALGORITHM_DESEDE_CBC_PKCS5PADDING
          The algorithm code constant for the Triple DES block cipher according to FIPS PUB 46-3 with PKCS#5 padding as specified in PKCS#5 version 1.5.
static long ALGORITHM_DESEDE_ECB
          The algorithm code constant for the Triple DES block cipher according to FIPS PUB 46-3 without any padding.
static long ALGORITHM_IDEA_CBC_PKCS5PADDING
          The algorithm code constant for the IDEA block cipher (see e.g.
static long ALGORITHM_IDEA_ECB
          The algorithm code constant for the IDEA block cipher (see e.g.
static long ALGORITHM_RC2_CBC_PKCS5PADDING
          The algorithm code constant for the RC2 block cipher according to RFC 2268 with PKCS#5 padding as specified in PKCS#5 version 1.5.
static long ALGORITHM_RC2_ECB
          The algorithm code constant for the RC2 block cipher according to RFC 2268 without any padding.
static long ALGORITHM_RC4
          The algorithm code constant for the RC4 stream cipher (see e.g.
static long ALGORITHM_RSA_OAEPPADDING
          The algorithm code constant for RSA OAEP asymmetric encryption according to PKCS#1 version 2.1.
static long ALGORITHM_RSA_PKCS1PADDING
          The algorithm code constant for RSA asymmetric encryption according to PKCS#1 version 1.5.
static int DECRYPT_MODE
          This constant is used to signal decryption.
static int ENCRYPT_MODE
          This constant is used to signal encryption.
static int UNWRAP_MODE
          This constant is used to signal key-unwrapping.
static int WRAP_MODE
          This constant is used to signal key-wrapping.
 
Method Summary
 byte[] doFinal()
          Finish the current encryption or decryption operation and return the final result; i.e. the enciphered data.
 void init(int mode, Key key, byte[] iv)
          Initialize a new encryption or decryption operation.
 Key unwrap(byte[] wrappedKey, java.lang.String wrappedKeyAlgorithm, int keyUsage, java.lang.String label, boolean tokenObject)
          Unwrap (decrypt) a wrapped secret key.
 byte[] update(byte[] data)
          Feed a piece of data into the current cipher operation.
 byte[] wrap(Key key)
          Wrap (encrypt) the specified symmetric key with the wrapping key which has been provided to the init(int, Key, byte[]) call.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALGORITHM_RSA_PKCS1PADDING

public static final long ALGORITHM_RSA_PKCS1PADDING
The algorithm code constant for RSA asymmetric encryption according to PKCS#1 version 1.5.
This corresponds to RSA/NONE/PKCS1Padding in the JCE.

See Also:
Constant Field Values

ALGORITHM_RSA_OAEPPADDING

public static final long ALGORITHM_RSA_OAEPPADDING
The algorithm code constant for RSA OAEP asymmetric encryption according to PKCS#1 version 2.1.
This corresponds to RSA/NONE/PKCS1Padding in the JCE.

See Also:
Constant Field Values

ALGORITHM_AES_ECB

public static final long ALGORITHM_AES_ECB
The algorithm code constant for the AES block cipher according to FIPS PUB 197 without any padding. Operates in ECB mode of operation. The input must be a multiple of the block size (i.e. 128 bit).
This corresponds to AES/ECB/NoPadding in the JCE.

See Also:
Constant Field Values

ALGORITHM_AES_CBC_PKCS5PADDING

public static final long ALGORITHM_AES_CBC_PKCS5PADDING
The algorithm code constant for the AES block cipher according to FIPS PUB 197 with PKCS#5 padding as specified in PKCS#5 version 1.5. Operates in CBC mode of operation.
This corresponds to AES/CBC/PKCS5Padding in the JCE.

See Also:
Constant Field Values

ALGORITHM_DES_ECB

public static final long ALGORITHM_DES_ECB
The algorithm code constant for the DES block cipher according to FIPS PUB 46-3 without any padding. Operates in ECB mode of operation. The input must be a multiple of the block size (i.e. 64 bit).
This corresponds to DES/ECB/NoPadding in the JCE.

See Also:
Constant Field Values

ALGORITHM_DES_CBC_PKCS5PADDING

public static final long ALGORITHM_DES_CBC_PKCS5PADDING
The algorithm code constant for the DES block cipher according to FIPS PUB 46-3 with PKCS#5 padding as specified in PKCS#5 version 1.5. Operates in CBC mode of operation.
This corresponds to DES/CBC/PKCS5Padding in the JCE.

See Also:
Constant Field Values

ALGORITHM_DESEDE_ECB

public static final long ALGORITHM_DESEDE_ECB
The algorithm code constant for the Triple DES block cipher according to FIPS PUB 46-3 without any padding. Operates in ECB mode of operation. The input must be a multiple of the block size (i.e. 64 bit).
This corresponds to DESede/ECB/NoPadding in the JCE.

See Also:
Constant Field Values

ALGORITHM_DESEDE_CBC_PKCS5PADDING

public static final long ALGORITHM_DESEDE_CBC_PKCS5PADDING
The algorithm code constant for the Triple DES block cipher according to FIPS PUB 46-3 with PKCS#5 padding as specified in PKCS#5 version 1.5. Operates in CBC mode of operation.
This corresponds to DESede/CBC/PKCS5Padding in the JCE.

See Also:
Constant Field Values

ALGORITHM_IDEA_ECB

public static final long ALGORITHM_IDEA_ECB
The algorithm code constant for the IDEA block cipher (see e.g. "Applied Cryptography", Bruce Schneier, ISBN 0-471-59756-2) without any padding. Operates in ECB mode of operation. The input must be a multiple of the block size (i.e. 64 bit).
This corresponds to IDEA/ECB/NoPadding in the JCE.

See Also:
Constant Field Values

ALGORITHM_IDEA_CBC_PKCS5PADDING

public static final long ALGORITHM_IDEA_CBC_PKCS5PADDING
The algorithm code constant for the IDEA block cipher (see e.g. "Applied Cryptography", Bruce Schneier, ISBN 0-471-59756-2) with PKCS#5 padding as specified in PKCS#5 version 1.5. Operates in CBC mode of operation.
This corresponds to IDEA/CBC/PKCS5Padding in the JCE.

See Also:
Constant Field Values

ALGORITHM_RC2_ECB

public static final long ALGORITHM_RC2_ECB
The algorithm code constant for the RC2 block cipher according to RFC 2268 without any padding. Operates in ECB mode of operation. The input must be a multiple of the block size (i.e. 64 bit).
This corresponds to RC2/ECB/NoPadding in the JCE.

See Also:
Constant Field Values

ALGORITHM_RC2_CBC_PKCS5PADDING

public static final long ALGORITHM_RC2_CBC_PKCS5PADDING
The algorithm code constant for the RC2 block cipher according to RFC 2268 with PKCS#5 padding as specified in PKCS#5 version 1.5. Operates in CBC mode of operation.
This corresponds to RC2/CBC/PKCS5Padding in the JCE.

See Also:
Constant Field Values

ALGORITHM_RC4

public static final long ALGORITHM_RC4
The algorithm code constant for the RC4 stream cipher (see e.g. "Applied Cryptography", Bruce Schneier, ISBN 0-471-59756-2). This cipher algorithm is usually not applicable for key wrapping.
This corresponds to RC4/NONE/NoPadding in the JCE.

See Also:
Constant Field Values

ENCRYPT_MODE

public static final int ENCRYPT_MODE
This constant is used to signal encryption.

See Also:
Constant Field Values

DECRYPT_MODE

public static final int DECRYPT_MODE
This constant is used to signal decryption.

See Also:
Constant Field Values

WRAP_MODE

public static final int WRAP_MODE
This constant is used to signal key-wrapping.

See Also:
Constant Field Values

UNWRAP_MODE

public static final int UNWRAP_MODE
This constant is used to signal key-unwrapping.

See Also:
Constant Field Values
Method Detail

init

public void init(int mode,
                 Key key,
                 byte[] iv)
          throws PKCS11RuntimeException
Initialize a new encryption or decryption operation. The specified key must be valid for encryption if mode is ENCRYPT_MODE; i.e. key.canBeUsedFor(Key.USAGE_ENCRYPTION) must return true. If mode is DECRYPT_MODE, the specified key must be valid for decryption; i.e. key.canBeUsedFor(Key.USAGE_DECRYPTION) must return true. If mode is WRAP_MODE, the specified key must be valid for key-wrapping; i.e. key.canBeUsedFor(Key.USAGE_WRAP) must return true. If mode is UNWRAP_MODE, the specified key must be valid for key-unwrapping; i.e. key.canBeUsedFor(Key.USAGE_UNWRAP) must return true.
Please note that only public and secret keys can be used for encryption and key-wrapping; and only private and secret keys can be used for decryption and key-unwrapping.

After this call, the application calls update(byte[]) one or more times to process encryption or decryption. To finish the encryption or decryption operation, the application calls doFinal().
For key-wrapping or key-unwrapping, the application calls wrap(iaik.pkcs.pkcs11.me.Key) or unwrap(byte[], java.lang.String, int, java.lang.String, boolean) after this initialization call.

Parameters:
mode - The mode; i.e. ENCRYPT_MODE for encryption, DECRYPT_MODE for decryption, WRAP_MODE for key-wrapping and UNWRAP_MODE for key-unwrapping.
key - The encryption key, decryption key, wrapping key or unwrapping key, depending on the mode.
iv - Initialization vector if the cipher is a block cipher and operates in a feedback mode (e.g. CBC).
Throws:
PKCS11RuntimeException - If initializing the cipher fails.
See Also:
update(byte[]), doFinal(), Key.canBeUsedFor(long)

update

public byte[] update(byte[] data)
              throws PKCS11RuntimeException
Feed a piece of data into the current cipher operation. i.e. data to be encrypted or enciphered data to be decrypted.

The underlying cipher will process as many of data as possible and will return any finished result data. However, the cipher may not have enough data to finish the processing of a block of data. Then, it may return null and return the result as return value of one of the next update(byte[]) calls or of the final doFinal() call. Anyway, the application must not ignore a result of any call to update(byte[]). Otherwise, it will end up with an incomplete result.
The application may call this method one or more times after calling init(int, Key, byte[]).
After providing the last input data, the application calls doFinal() to finish the current operation.

Parameters:
data - The piece of data; i.e. data to be encrypted or enciphered data to be decrypted.
Returns:
An part of the result (i.e. of the encrypted data or decrypted data) or null if the cipher had to delay the processing until the next call to update(byte[]) or doFinal().
Throws:
PKCS11RuntimeException - If the cipher operation fails.
See Also:
init(int, Key, byte[]), doFinal()

doFinal

public byte[] doFinal()
               throws PKCS11RuntimeException
Finish the current encryption or decryption operation and return the final result; i.e. the enciphered data.

After this call, this cipher object is reset to the state in which it was right after the call to init(int, Key, byte[]). The application can reuse it for another encryption or decryption operation; i.e. it can start with calling update(byte[]) without calling init(int, Key, byte[]) first.

The application may also initialize the object again for a new encryption or decryption operation.

Returns:
The final encryption or decryption result.
Throws:
PKCS11RuntimeException - If the cipher operation fails.
See Also:
init(int, Key, byte[]), update(byte[])

wrap

public byte[] wrap(Key key)
            throws PKCS11RuntimeException
Wrap (encrypt) the specified symmetric key with the wrapping key which has been provided to the init(int, Key, byte[]) call.

Note that the mode provided to the last init(int, Key, byte[]) call must have been WRAP_MODE.

After this call, the object is reset to the state in which it was just after the last call to init(int, Key, byte[]); i.e. the application can start a new key-wrapping operation with the same key using wrap(Key) without calling init(int, Key, byte[]) again. Do not call doFinal() after wrap(Key)!

Parameters:
key - The key to wrap.
Returns:
The wrapped (encrypted) key value.
Throws:
PKCS11RuntimeException - If wrapping the given key fails; e.g. if the specified key is too large.

unwrap

public Key unwrap(byte[] wrappedKey,
                  java.lang.String wrappedKeyAlgorithm,
                  int keyUsage,
                  java.lang.String label,
                  boolean tokenObject)
           throws PKCS11RuntimeException
Unwrap (decrypt) a wrapped secret key. After unwrapping the key on the token, the method will create a key object. This key object will have the specified attributes; e.g. label and key usage flags.

Note that the mode provided to the last init(int, Key, byte[]) call must have been unwrap(byte[], String, int, String, boolean).

After this call, the object is reset to the state in which it was just after the last call to init(int, Key, byte[]); i.e. the application can start a new key-unwrapping operation with the same key using unwrap(byte[], String, int, String, boolean) without calling init(int, Key, byte[]) again. Do not call doFinal() after unwrap(byte[], String, int, String, boolean)!

Parameters:
wrappedKey - The wrapped secret key.
wrappedKeyAlgorithm - The algorithm of the wrapped key; e.g. Key.ALGORITHM_AES or Key.ALGORITHM_DESEDE.
keyUsage - The usage of the new key object. It can be any sum of KeyGenerator.USAGE_CIPHER, KeyGenerator.USAGE_SIGNATURE and KeyGenerator.USAGE_WRAP. The use is the same as in KeyGenerator.init(int, int, String, boolean).
label - The label of the new key object. This can be used later as alias to access the key via the key store.
tokenObject - true to make the new secret key a permanent key on the token (i.e. a token object), or false to make is just a session key which will be deleted after the associated session is closed.
Returns:
The unwrapped secrete key.
Throws:
PKCS11RuntimeException - If unwrapping the key fails for some reason.

IAIK PKCS#11 Provider Micro Edition
version 1.0

IAIK JavaSecurity Website http://jce.iaik.tugraz.at/

IAIK at Graz University of Technology, Austria, Europe
Copyright 2001-2005, IAIK, Graz University of Technology, Inffeldgasse 16a, 8010 Graz, Austria. All Rights Reserved.
version 1.0