0N/A/*
2362N/A * Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.crypto.provider;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.spec.*;
0N/Aimport sun.security.util.*;
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/Aimport javax.crypto.BadPaddingException;
0N/A
0N/A/**
0N/A * This class implements the Blowfish algorithm in its various modes
0N/A * (<code>ECB</code>, <code>CFB</code>, <code>OFB</code>, <code>CBC</code>,
0N/A * <code>PCBC</code>) and padding schemes (<code>PKCS5Padding</code>,
0N/A * <code>NoPadding</code>, <code>ISO10126Padding</code>).
0N/A *
0N/A * <p> Blowfish is a 64-bit block cipher with a variable-length key.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A *
0N/A * @see BlowfishCrypt
0N/A * @see CipherBlockChaining
0N/A * @see ElectronicCodeBook
0N/A * @see CipherFeedback
0N/A * @see OutputFeedback
0N/A */
0N/A
0N/Apublic final class BlowfishCipher extends CipherSpi {
0N/A
0N/A /*
0N/A * internal CipherCore object which does the real work.
0N/A */
0N/A private CipherCore core = null;
0N/A
0N/A /**
0N/A * Creates an instance of Blowfish cipher with default ECB mode and
0N/A * PKCS5Padding.
0N/A */
0N/A public BlowfishCipher() {
0N/A core = new CipherCore(new BlowfishCrypt(),
0N/A BlowfishConstants.BLOWFISH_BLOCK_SIZE);
0N/A }
0N/A
0N/A /**
0N/A * Sets the mode of this cipher.
0N/A *
0N/A * @param mode the cipher mode
0N/A *
0N/A * @exception NoSuchAlgorithmException if the requested cipher mode does
0N/A * not exist
0N/A */
0N/A protected void engineSetMode(String mode)
0N/A throws NoSuchAlgorithmException {
0N/A core.setMode(mode);
0N/A }
0N/A
0N/A /**
0N/A * Sets the padding mechanism of this cipher.
0N/A *
0N/A * @param padding the padding mechanism
0N/A *
0N/A * @exception NoSuchPaddingException if the requested padding mechanism
0N/A * does not exist
0N/A */
0N/A protected void engineSetPadding(String paddingScheme)
0N/A throws NoSuchPaddingException {
0N/A core.setPadding(paddingScheme);
0N/A }
0N/A
0N/A /**
0N/A * Returns the block size (in bytes).
0N/A *
0N/A * @return the block size (in bytes), or 0 if the underlying algorithm is
0N/A * not a block cipher
0N/A */
0N/A protected int engineGetBlockSize() {
0N/A return BlowfishConstants.BLOWFISH_BLOCK_SIZE;
0N/A }
0N/A
0N/A /**
0N/A * Returns the length in bytes that an output buffer would need to be in
0N/A * order to hold the result of the next <code>update</code> or
0N/A * <code>doFinal</code> operation, given the input length
0N/A * <code>inputLen</code> (in bytes).
0N/A *
0N/A * <p>This call takes into account any unprocessed (buffered) data from a
0N/A * previous <code>update</code> call, and padding.
0N/A *
0N/A * <p>The actual output length of the next <code>update</code> or
0N/A * <code>doFinal</code> call may be smaller than the length returned by
0N/A * this method.
0N/A *
0N/A * @param inputLen the input length (in bytes)
0N/A *
0N/A * @return the required output buffer size (in bytes)
0N/A */
0N/A protected int engineGetOutputSize(int inputLen) {
0N/A return core.getOutputSize(inputLen);
0N/A }
0N/A
0N/A /**
0N/A * Returns the initialization vector (IV) in a new buffer.
0N/A *
0N/A * <p>This is useful in the case where a random IV has been created
0N/A * (see <a href = "#init">init</a>),
0N/A * or in the context of password-based encryption or
0N/A * decryption, where the IV is derived from a user-supplied password.
0N/A *
0N/A * @return the initialization vector in a new buffer, or null if the
0N/A * underlying algorithm does not use an IV, or if the IV has not yet
0N/A * been set.
0N/A */
0N/A protected byte[] engineGetIV() {
0N/A return core.getIV();
0N/A }
0N/A
0N/A /**
0N/A * Returns the parameters used with this cipher.
0N/A *
0N/A * <p>The returned parameters may be the same that were used to initialize
0N/A * this cipher, or may contain the default set of parameters or a set of
0N/A * randomly generated parameters used by the underlying cipher
0N/A * implementation (provided that the underlying cipher implementation
0N/A * uses a default set of parameters or creates new parameters if it needs
0N/A * parameters but was not initialized with any).
0N/A *
0N/A * @return the parameters used with this cipher, or null if this cipher
0N/A * does not use any parameters.
0N/A */
0N/A protected AlgorithmParameters engineGetParameters() {
0N/A return core.getParameters("Blowfish");
0N/A }
0N/A
0N/A /**
0N/A * Initializes this cipher with a key and a source of randomness.
0N/A *
0N/A * <p>The cipher is initialized for one of the following four operations:
0N/A * encryption, decryption, key wrapping or key unwrapping, depending on
0N/A * the value of <code>opmode</code>.
0N/A *
0N/A * <p>If this cipher requires an initialization vector (IV), it will get
0N/A * it from <code>random</code>.
0N/A * This behaviour should only be used in encryption or key wrapping
0N/A * mode, however.
0N/A * When initializing a cipher that requires an IV for decryption or
0N/A * key unwrapping, the IV
0N/A * (same IV that was used for encryption or key wrapping) must be provided
0N/A * explicitly as a
0N/A * parameter, in order to get the correct result.
0N/A *
0N/A * <p>This method also cleans existing buffer and other related state
0N/A * information.
0N/A *
0N/A * @param opmode the operation mode of this cipher (this is one of
0N/A * the following:
0N/A * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
0N/A * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
0N/A * @param key the secret key
0N/A * @param random the source of randomness
0N/A *
0N/A * @exception InvalidKeyException if the given key is inappropriate for
0N/A * initializing this cipher
0N/A */
0N/A protected void engineInit(int opmode, Key key, SecureRandom random)
0N/A throws InvalidKeyException {
0N/A core.init(opmode, key, random);
0N/A }
0N/A
0N/A /**
0N/A * Initializes this cipher with a key, a set of
0N/A * algorithm parameters, and a source of randomness.
0N/A *
0N/A * <p>The cipher is initialized for one of the following four operations:
0N/A * encryption, decryption, key wrapping or key unwrapping, depending on
0N/A * the value of <code>opmode</code>.
0N/A *
0N/A * <p>If this cipher (including its underlying feedback or padding scheme)
0N/A * requires any random bytes, it will get them from <code>random</code>.
0N/A *
0N/A * @param opmode the operation mode of this cipher (this is one of
0N/A * the following:
0N/A * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
0N/A * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
0N/A * @param key the encryption key
0N/A * @param params the algorithm parameters
0N/A * @param random the source of randomness
0N/A *
0N/A * @exception InvalidKeyException if the given key is inappropriate for
0N/A * initializing this cipher
0N/A * @exception InvalidAlgorithmParameterException if the given algorithm
0N/A * parameters are inappropriate for this cipher
0N/A */
0N/A protected void engineInit(int opmode, Key key,
0N/A AlgorithmParameterSpec params,
0N/A SecureRandom random)
0N/A throws InvalidKeyException, InvalidAlgorithmParameterException {
0N/A core.init(opmode, key, params, random);
0N/A }
0N/A
0N/A protected void engineInit(int opmode, Key key,
0N/A AlgorithmParameters params,
0N/A SecureRandom random)
0N/A throws InvalidKeyException, InvalidAlgorithmParameterException {
0N/A core.init(opmode, key, params, random);
0N/A }
0N/A
0N/A /**
0N/A * Continues a multiple-part encryption or decryption operation
0N/A * (depending on how this cipher was initialized), processing another data
0N/A * part.
0N/A *
0N/A * <p>The first <code>inputLen</code> bytes in the <code>input</code>
0N/A * buffer, starting at <code>inputOffset</code>, are processed, and the
0N/A * result is stored in a new buffer.
0N/A *
0N/A * @param input the input buffer
0N/A * @param inputOffset the offset in <code>input</code> where the input
0N/A * starts
0N/A * @param inputLen the input length
0N/A *
0N/A * @return the new buffer with the result
0N/A *
0N/A * @exception IllegalStateException if this cipher is in a wrong state
0N/A * (e.g., has not been initialized)
0N/A */
0N/A protected byte[] engineUpdate(byte[] input, int inputOffset,
0N/A int inputLen) {
0N/A return core.update(input, inputOffset, inputLen);
0N/A }
0N/A
0N/A /**
0N/A * Continues a multiple-part encryption or decryption operation
0N/A * (depending on how this cipher was initialized), processing another data
0N/A * part.
0N/A *
0N/A * <p>The first <code>inputLen</code> bytes in the <code>input</code>
0N/A * buffer, starting at <code>inputOffset</code>, are processed, and the
0N/A * result is stored in the <code>output</code> buffer, starting at
0N/A * <code>outputOffset</code>.
0N/A *
0N/A * @param input the input buffer
0N/A * @param inputOffset the offset in <code>input</code> where the input
0N/A * starts
0N/A * @param inputLen the input length
0N/A * @param output the buffer for the result
0N/A * @param outputOffset the offset in <code>output</code> where the result
0N/A * is stored
0N/A *
0N/A * @return the number of bytes stored in <code>output</code>
0N/A *
0N/A * @exception ShortBufferException if the given output buffer is too small
0N/A * to hold the result
0N/A */
0N/A protected int engineUpdate(byte[] input, int inputOffset, int inputLen,
0N/A byte[] output, int outputOffset)
0N/A throws ShortBufferException {
0N/A return core.update(input, inputOffset, inputLen, output,
0N/A outputOffset);
0N/A }
0N/A
0N/A /**
0N/A * Encrypts or decrypts data in a single-part operation,
0N/A * or finishes a multiple-part operation.
0N/A * The data is encrypted or decrypted, depending on how this cipher was
0N/A * initialized.
0N/A *
0N/A * <p>The first <code>inputLen</code> bytes in the <code>input</code>
0N/A * buffer, starting at <code>inputOffset</code>, and any input bytes that
0N/A * may have been buffered during a previous <code>update</code> operation,
0N/A * are processed, with padding (if requested) being applied.
0N/A * The result is stored in a new buffer.
0N/A *
0N/A * <p>The cipher is reset to its initial state (uninitialized) after this
0N/A * call.
0N/A *
0N/A * @param input the input buffer
0N/A * @param inputOffset the offset in <code>input</code> where the input
0N/A * starts
0N/A * @param inputLen the input length
0N/A *
0N/A * @return the new buffer with the result
0N/A *
0N/A * @exception IllegalBlockSizeException if this cipher is a block cipher,
0N/A * no padding has been requested (only in encryption mode), and the total
0N/A * input length of the data processed by this cipher is not a multiple of
0N/A * block size
0N/A * @exception BadPaddingException if this cipher is in decryption mode,
0N/A * and (un)padding has been requested, but the decrypted data is not
0N/A * bounded by the appropriate padding bytes
0N/A */
0N/A protected byte[] engineDoFinal(byte[] input, int inputOffset,
0N/A int inputLen)
0N/A throws IllegalBlockSizeException, BadPaddingException {
0N/A return core.doFinal(input, inputOffset, inputLen);
0N/A }
0N/A
0N/A /**
0N/A * Encrypts or decrypts data in a single-part operation,
0N/A * or finishes a multiple-part operation.
0N/A * The data is encrypted or decrypted, depending on how this cipher was
0N/A * initialized.
0N/A *
0N/A * <p>The first <code>inputLen</code> bytes in the <code>input</code>
0N/A * buffer, starting at <code>inputOffset</code>, and any input bytes that
0N/A * may have been buffered during a previous <code>update</code> operation,
0N/A * are processed, with padding (if requested) being applied.
0N/A * The result is stored in the <code>output</code> buffer, starting at
0N/A * <code>outputOffset</code>.
0N/A *
0N/A * <p>The cipher is reset to its initial state (uninitialized) after this
0N/A * call.
0N/A *
0N/A * @param input the input buffer
0N/A * @param inputOffset the offset in <code>input</code> where the input
0N/A * starts
0N/A * @param inputLen the input length
0N/A * @param output the buffer for the result
0N/A * @param outputOffset the offset in <code>output</code> where the result
0N/A * is stored
0N/A *
0N/A * @return the number of bytes stored in <code>output</code>
0N/A *
0N/A * @exception IllegalBlockSizeException if this cipher is a block cipher,
0N/A * no padding has been requested (only in encryption mode), and the total
0N/A * input length of the data processed by this cipher is not a multiple of
0N/A * block size
0N/A * @exception ShortBufferException if the given output buffer is too small
0N/A * to hold the result
0N/A * @exception BadPaddingException if this cipher is in decryption mode,
0N/A * and (un)padding has been requested, but the decrypted data is not
0N/A * bounded by the appropriate padding bytes
0N/A */
0N/A protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
0N/A byte[] output, int outputOffset)
0N/A throws IllegalBlockSizeException, ShortBufferException,
0N/A BadPaddingException {
0N/A return core.doFinal(input, inputOffset, inputLen, output,
0N/A outputOffset);
0N/A }
0N/A
0N/A /**
0N/A * Returns the key size of the given key object.
0N/A *
0N/A * @param key the key object.
0N/A *
0N/A * @return the key size of the given key object.
0N/A *
0N/A * @exception InvalidKeyException if <code>key</code> is invalid.
0N/A */
0N/A protected int engineGetKeySize(Key key) throws InvalidKeyException {
0N/A return (key.getEncoded().length * 8);
0N/A }
0N/A
0N/A /**
0N/A * Wrap a key.
0N/A *
0N/A * @param key the key to be wrapped.
0N/A *
0N/A * @return the wrapped key.
0N/A *
0N/A * @exception IllegalBlockSizeException if this cipher is a block
0N/A * cipher, no padding has been requested, and the length of the
0N/A * encoding of the key to be wrapped is not a
0N/A * multiple of the block size.
0N/A *
0N/A * @exception InvalidKeyException if it is impossible or unsafe to
0N/A * wrap the key with this cipher (e.g., a hardware protected key is
0N/A * being passed to a software only cipher).
0N/A */
0N/A protected byte[] engineWrap(Key key)
0N/A throws IllegalBlockSizeException, InvalidKeyException {
0N/A return core.wrap(key);
0N/A }
0N/A
0N/A /**
0N/A * Unwrap a previously wrapped key.
0N/A *
0N/A * @param wrappedKey the key to be unwrapped.
0N/A *
0N/A * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
0N/A *
0N/A * @param wrappedKeyType the type of the wrapped key.
0N/A * This is one of <code>Cipher.SECRET_KEY</code>,
0N/A * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
0N/A *
0N/A * @return the unwrapped key.
0N/A *
0N/A * @exception NoSuchAlgorithmException if no installed providers
0N/A * can create keys of type <code>wrappedKeyType</code> for the
0N/A * <code>wrappedKeyAlgorithm</code>.
0N/A *
0N/A * @exception InvalidKeyException if <code>wrappedKey</code> does not
0N/A * represent a wrapped key of type <code>wrappedKeyType</code> for
0N/A * the <code>wrappedKeyAlgorithm</code>.
0N/A */
0N/A protected Key engineUnwrap(byte[] wrappedKey,
0N/A String wrappedKeyAlgorithm,
0N/A int wrappedKeyType)
0N/A throws InvalidKeyException, NoSuchAlgorithmException {
0N/A return core.unwrap(wrappedKey, wrappedKeyAlgorithm,
0N/A wrappedKeyType);
0N/A }
0N/A}