/* CipherSpi.java -- The cipher service provider interface. Copyright (C) 2004, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package javax.crypto; import java.nio.ByteBuffer; import java.security.AlgorithmParameters; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.AlgorithmParameterSpec; /** *
This class represents the Service Provider Interface * (SPI) for cryptographic ciphers.
* *Providers of cryptographic ciphers must subclass this for every * cipher they implement, implementing the abstract methods as * appropriate, then provide an entry that points to the subclass in * their implementation of {@link java.security.Provider}.
* *CipherSpi objects are instantiated along with {@link Cipher}s when * the {@link Cipher#getInstance(java.lang.String)} methods are invoked. * Particular ciphers are referenced by a transformation, which * is a String consisting of the cipher's name or the ciper's name * followed by a mode and a padding. Transformations all follow the * general form:
* *Cipher names in the master {@link java.security.Provider} class * may be:
* *Cipher.algorithm
Cipher.algorithm/mode
Cipher.algorithm//padding
Cipher.algorithm/mode/padding
When any {@link Cipher#getInstance(java.lang.String)} method is * invoked, the following happens if the transformation is simply * algorithm:
* *CipherSpi
implementation
* for "algorithm", return it. Otherwise throw a {@link
* java.security.NoSuchAlgorithmException}.If the transformation is of the form * algorithm/mode/padding:
* *CipherSpi
subclass for
* "algorithm/mode/padding", return it. Otherwise
* go to step 2.CipherSpi
subclass for
* "algorithm/mode", instatiate it, call {@link
* #engineSetPadding(java.lang.String)} for the padding name, and return
* it. Otherwise go to step 3.CipherSpi
subclass for
* "algorithm//padding", instatiate it, call {@link
* #engineSetMode(java.lang.String)} for the mode name, and return
* it. Otherwise go to step 4.CipherSpi
subclass for
* "algorithm", instatiate it, call {@link
* #engineSetMode(java.lang.String)} for the mode name, call {@link
* #engineSetPadding(java.lang.String)} for the padding name, and return
* it. Otherwise throw a {@link java.security.NoSuchAlgorithmException}.Return the length of the given key in bits.
* *For compatibility this method is not declared
* abstract
, and the default implementation will throw an
* {@link java.lang.UnsupportedOperationException}. Concrete
* subclasses should override this method to return the correct
* value.
Returns the size, in bytes, an output buffer must be for a call * to {@link #engineUpdate(byte[],int,int,byte[],int)} or {@link * #engineDoFinal(byte[],int,int,byte[],int)} to succeed.
* *The actual output length may be smaller than the value returned * by this method, as it considers the padding length as well. The * length considered is the argument plus the length of any buffered, * unprocessed bytes.
* * @param inputLength The input length, in bytes. * @return The size an output buffer must be. */ protected abstract int engineGetOutputSize(int inputLength); /** * Returns the parameters that this cipher is using. This may be the * parameters used to initialize this cipher, or it may be parameters * that have been initialized with random values. * * @return This cipher's parameters, ornull
if this
* cipher does not use parameters.
*/
protected abstract AlgorithmParameters engineGetParameters();
/**
* Initializes this cipher with an operation mode, key, and source of
* randomness. If this cipher requires any other initializing data,
* for example an initialization vector, then it should generate it
* from the provided source of randomness.
*
* @param opmode The operation mode, one of {@link
* Cipher#DECRYPT_MODE}, {@link Cipher#ENCRYPT_MODE}, {@link
* Cipher#UNWRAP_MODE}, or {@link Cipher#WRAP_MODE}.
* @param key The key to initialize this cipher with.
* @param random The source of random bytes to use.
* @throws java.security.InvalidKeyException If the given key is not
* acceptable for this implementation.
*/
protected abstract void engineInit(int opmode, Key key, SecureRandom random)
throws InvalidKeyException;
/**
* Initializes this cipher with an operation mode, key, parameters,
* and source of randomness. If this cipher requires any other
* initializing data, for example an initialization vector, then it should
* generate it from the provided source of randomness.
*
* @param opmode The operation mode, one of {@link
* Cipher#DECRYPT_MODE}, {@link Cipher#ENCRYPT_MODE}, {@link
* Cipher#UNWRAP_MODE}, or {@link Cipher#WRAP_MODE}.
* @param key The key to initialize this cipher with.
* @param params The algorithm parameters to initialize with.
* @param random The source of random bytes to use.
* @throws java.security.InvalidAlgorithmParameterException If the
* given parameters are not appropriate for this
* implementation.
* @throws java.security.InvalidKeyException If the given key is not
* acceptable for this implementation.
*/
protected abstract void
engineInit(int opmode, Key key, AlgorithmParameters params,
SecureRandom random)
throws InvalidAlgorithmParameterException, InvalidKeyException;
/**
* Initializes this cipher with an operation mode, key, parameters,
* and source of randomness. If this cipher requires any other
* initializing data, for example an initialization vector, then it should
* generate it from the provided source of randomness.
*
* @param opmode The operation mode, one of {@link
* Cipher#DECRYPT_MODE}, {@link Cipher#ENCRYPT_MODE}, {@link
* Cipher#UNWRAP_MODE}, or {@link Cipher#WRAP_MODE}.
* @param key The key to initialize this cipher with.
* @param params The algorithm parameters to initialize with.
* @param random The source of random bytes to use.
* @throws java.security.InvalidAlgorithmParameterException If the
* given parameters are not appropriate for this
* implementation.
* @throws java.security.InvalidKeyException If the given key is not
* acceptable for this implementation.
*/
protected abstract void
engineInit(int opmode, Key key, AlgorithmParameterSpec params,
SecureRandom random)
throws InvalidAlgorithmParameterException, InvalidKeyException;
/**
* Set the mode in which this cipher is to run.
*
* @param mode The name of the mode to use.
* @throws java.security.NoSuchAlgorithmException If the mode is
* not supported by this cipher's provider.
*/
protected abstract void engineSetMode(String mode)
throws NoSuchAlgorithmException;
/**
* Set the method with which the input is to be padded.
*
* @param padding The name of the padding to use.
* @throws javax.crypto.NoSuchPaddingException If the padding is not
* supported by this cipher's provider.
*/
protected abstract void engineSetPadding(String padding)
throws NoSuchPaddingException;
/**
* Unwraps a previously-wrapped key.
* *For compatibility this method is not declared
* abstract
, and the default implementation will throw an
* {@link java.lang.UnsupportedOperationException}.
wrappedKeyType
is an
* inappropriate type for the unwrapped key.
* @throws java.security.NoSuchAlgorithmException If the
* wrappedKeyAlgorithm
is unknown.
*/
protected Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm,
int wrappedKeyType)
throws InvalidKeyException, NoSuchAlgorithmException
{
throw new UnsupportedOperationException();
}
/**
* Continue with a multi-part transformation, returning a new array of
* the transformed bytes.
*
* @param input The next input bytes.
* @param inputOffset The index in the input array from which to start.
* @param inputLength The number of bytes to input.
* @return The transformed bytes.
*/
protected abstract byte[]
engineUpdate(byte[] input, int inputOffset, int inputLength);
/**
* Continue with a multi-part transformation, storing the transformed
* bytes into the specified array.
*
* @param input The next input bytes.
* @param inputOffset The index in the input from which to start.
* @param inputLength The number of bytes to input.
* @param output The output buffer.
* @param outputOffset The index in the output array from which to start.
* @return The transformed bytes.
* @throws javax.crypto.ShortBufferException If there is not enough
* space in the output array to store the transformed bytes.
*/
protected abstract int
engineUpdate(byte[] input, int inputOffset, int inputLength,
byte[] output, int outputOffset)
throws ShortBufferException;
/**
* @since 1.5
*/
protected int engineUpdate (ByteBuffer input, ByteBuffer output)
throws ShortBufferException
{
int total = 0;
byte[] inbuf = new byte[256];
while (input.hasRemaining ())
{
int in = Math.min (inbuf.length, input.remaining ());
input.get (inbuf, 0, in);
byte[] outbuf = new byte[engineGetOutputSize (in)];
int out = engineUpdate (inbuf, 0, in, outbuf, 0);
output.put (outbuf, 0, out);
total += out;
}
return total;
}
/**
* Wrap a key.
* *For compatibility this method is not declared
* abstract
, and the default implementation will throw an
* {@link java.lang.UnsupportedOperationException}.