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.io.UnsupportedEncodingException;
0N/Aimport java.security.*;
0N/Aimport java.security.spec.*;
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/A
0N/A/**
0N/A * This class implements a proprietary password-based encryption algorithm.
0N/A * It is based on password-based encryption as defined by the PKCS #5
0N/A * standard, except that is uses triple DES instead of DES.
0N/A *
0N/A * Here's how this algorithm works:
0N/A *
0N/A * 1. Create random salt and split it in two halves. If the two halves are
0N/A * identical, invert one of them.
0N/A * 2. Concatenate password with each of the halves.
0N/A * 3. Digest each concatenation with c iterations, where c is the
0N/A * iterationCount. Concatenate the output from each digest round with the
0N/A * password, and use the result as the input to the next digest operation.
0N/A * The digest algorithm is MD5.
0N/A * 4. After c iterations, use the 2 resulting digests as follows:
0N/A * The 16 bytes of the first digest and the 1st 8 bytes of the 2nd digest
0N/A * form the triple DES key, and the last 8 bytes of the 2nd digest form the
0N/A * IV.
0N/A *
0N/A * @author Jan Luehe
0N/A * @see javax.crypto.Cipher
0N/A */
0N/Apublic final class PBEWithMD5AndTripleDESCipher extends CipherSpi {
0N/A
0N/A private PBECipherCore core;
0N/A
0N/A /**
0N/A * Creates an instance of this cipher, and initializes its mode (CBC) and
0N/A * padding (PKCS5).
0N/A *
0N/A * @exception NoSuchAlgorithmException if the required cipher mode (CBC) is
0N/A * unavailable
0N/A * @exception NoSuchPaddingException if the required padding mechanism
0N/A * (PKCS5Padding) is unavailable
0N/A */
0N/A public PBEWithMD5AndTripleDESCipher()
0N/A throws NoSuchAlgorithmException, NoSuchPaddingException
0N/A {
0N/A // set the encapsulated cipher to do triple DES
0N/A core = new PBECipherCore("DESede");
0N/A }
0N/A
0N/A /**
0N/A * Sets the mode of this cipher. This algorithm can only be run in CBC
0N/A * mode.
0N/A *
0N/A * @param mode the cipher mode
0N/A *
0N/A * @exception NoSuchAlgorithmException if the requested cipher mode is
0N/A * invalid
0N/A */
0N/A protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
0N/A if ((mode != null) && (!mode.equalsIgnoreCase("CBC"))) {
0N/A throw new NoSuchAlgorithmException("Invalid cipher mode: " + mode);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the padding mechanism of this cipher. This algorithm only uses
0N/A * PKCS #5 padding.
0N/A *
0N/A * @param padding the padding mechanism
0N/A *
0N/A * @exception NoSuchPaddingException if the requested padding mechanism
0N/A * is invalid
0N/A */
0N/A protected void engineSetPadding(String paddingScheme)
0N/A throws NoSuchPaddingException
0N/A {
0N/A if ((paddingScheme != null) &&
0N/A (!paddingScheme.equalsIgnoreCase("PKCS5Padding"))) {
0N/A throw new NoSuchPaddingException("Invalid padding scheme: " +
0N/A paddingScheme);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the block size (in bytes).
0N/A *
0N/A * @return the block size (in bytes)
0N/A */
0N/A protected int engineGetBlockSize() {
0N/A return core.getBlockSize();
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 */
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();
0N/A }
0N/A
0N/A /**
0N/A * Initializes this cipher with a key and a source
0N/A * of randomness.
0N/A * 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 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 try {
0N/A core.init(opmode, key, (AlgorithmParameterSpec) null, random);
0N/A } catch (InvalidAlgorithmParameterException ie) {
0N/A InvalidKeyException ike =
0N/A new InvalidKeyException("requires PBE parameters");
0N/A ike.initCause(ie);
0N/A throw ike;
0N/A }
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 * The cipher is initialized for encryption or decryption, 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 either
0N/A * <code>ENCRYPT_MODE</code> or <code>DECRYPT_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 {
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 */
0N/A protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen)
0N/A {
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 {
0N/A return core.update(input, inputOffset, inputLen,
0N/A output, 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 decrypting and padding is choosen,
0N/A * but the last input data does not have proper padding bytes.
0N/A */
0N/A protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
0N/A throws IllegalBlockSizeException, BadPaddingException
0N/A {
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 decrypting and padding is choosen,
0N/A * but the last input data does not have proper padding bytes.
0N/A */
0N/A protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
0N/A byte[] output, int outputOffset)
0N/A throws ShortBufferException, IllegalBlockSizeException,
0N/A BadPaddingException
0N/A {
0N/A return core.doFinal(input, inputOffset, inputLen,
0N/A output, 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 168;
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}