jdk/src/share/classes/com/sun/crypto/provider/PBEWithMD5AndTripleDESCipher.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3353 ddbd63234844
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class implements a proprietary password-based encryption algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * It is based on password-based encryption as defined by the PKCS #5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * standard, except that is uses triple DES instead of DES.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Here's how this algorithm works:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * 1. Create random salt and split it in two halves. If the two halves are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *    identical, invert one of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * 2. Concatenate password with each of the halves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * 3. Digest each concatenation with c iterations, where c is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *    iterationCount. Concatenate the output from each digest round with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *    password, and use the result as the input to the next digest operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *    The digest algorithm is MD5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * 4. After c iterations, use the 2 resulting digests as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *    The 16 bytes of the first digest and the 1st 8 bytes of the 2nd digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *    form the triple DES key, and the last 8 bytes of the 2nd digest form the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *    IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see javax.crypto.Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public final class PBEWithMD5AndTripleDESCipher extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private PBECipherCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Creates an instance of this cipher, and initializes its mode (CBC) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * padding (PKCS5).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Verify the SunJCE provider in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @exception NoSuchAlgorithmException if the required cipher mode (CBC) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @exception NoSuchPaddingException if the required padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * (PKCS5Padding) is unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @exception SecurityException if fails to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * its own integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public PBEWithMD5AndTripleDESCipher()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        throws NoSuchAlgorithmException, NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (!SunJCE.verifySelfIntegrity(this.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new SecurityException("The SunJCE provider may have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                        "been tampered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // set the encapsulated cipher to do triple DES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        core = new PBECipherCore("DESede");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Sets the mode of this cipher. This algorithm can only be run in CBC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param mode the cipher mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @exception NoSuchAlgorithmException if the requested cipher mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if ((mode != null) && (!mode.equalsIgnoreCase("CBC"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new NoSuchAlgorithmException("Invalid cipher mode: " + mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Sets the padding mechanism of this cipher. This algorithm only uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * PKCS #5 padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param padding the padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @exception NoSuchPaddingException if the requested padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected void engineSetPadding(String paddingScheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        throws NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if ((paddingScheme != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            (!paddingScheme.equalsIgnoreCase("PKCS5Padding"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new NoSuchPaddingException("Invalid padding scheme: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                             paddingScheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Returns the block size (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @return the block size (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return core.getBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Returns the length in bytes that an output buffer would need to be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * order to hold the result of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>doFinal</code> operation, given the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <code>inputLen</code> (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>This call takes into account any unprocessed (buffered) data from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * previous <code>update</code> call, and padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <p>The actual output length of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <code>doFinal</code> call may be smaller than the length returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param inputLen the input length (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return the required output buffer size (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    protected int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return core.getOutputSize(inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Returns the initialization vector (IV) in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <p> This is useful in the case where a random IV has been created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * (see <a href = "#init">init</a>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * or in the context of password-based encryption or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * decryption, where the IV is derived from a user-supplied password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return the initialization vector in a new buffer, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * underlying algorithm does not use an IV, or if the IV has not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return core.getIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns the parameters used with this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p>The returned parameters may be the same that were used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * this cipher, or may contain the default set of parameters or a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * randomly generated parameters used by the underlying cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * implementation (provided that the underlying cipher implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * uses a default set of parameters or creates new parameters if it needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * parameters but was not initialized with any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return the parameters used with this cipher, or null if this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * does not use any parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return core.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Initializes this cipher with a key and a source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * encryption, decryption, key wrapping or key unwrapping, depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * requires any random bytes, it will get them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param opmode the operation mode of this cipher (this is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            core.init(opmode, key, (AlgorithmParameterSpec) null, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } catch (InvalidAlgorithmParameterException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            InvalidKeyException ike =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                new InvalidKeyException("requires PBE parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            ike.initCause(ie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Initializes this cipher with a key, a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * algorithm parameters, and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * The cipher is initialized for encryption or decryption, depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * requires any random bytes, it will get them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param opmode the operation mode of this cipher (this is either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <code>ENCRYPT_MODE</code> or <code>DECRYPT_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * parameters are inappropriate for this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                              AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        core.init(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                              AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        throws InvalidKeyException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        core.init(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * buffer, starting at <code>inputOffset</code>, are processed, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return the new buffer with the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return core.update(input, inputOffset, inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * buffer, starting at <code>inputOffset</code>, are processed, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <code>outputOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    protected int engineUpdate(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                               byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        throws ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return core.update(input, inputOffset, inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                           output, outputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Encrypts or decrypts data in a single-part operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * or finishes a multiple-part operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * The data is encrypted or decrypted, depending on how this cipher was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * buffer, starting at <code>inputOffset</code>, and any input bytes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * may have been buffered during a previous <code>update</code> operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * are processed, with padding (if requested) being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * The result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p>The cipher is reset to its initial state (uninitialized) after this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @return the new buffer with the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @exception BadPaddingException if decrypting and padding is choosen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * but the last input data does not have proper padding bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        throws IllegalBlockSizeException, BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return core.doFinal(input, inputOffset, inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Encrypts or decrypts data in a single-part operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * or finishes a multiple-part operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * The data is encrypted or decrypted, depending on how this cipher was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * buffer, starting at <code>inputOffset</code>, and any input bytes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * may have been buffered during a previous <code>update</code> operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * are processed, with padding (if requested) being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * The result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <code>outputOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <p>The cipher is reset to its initial state (uninitialized) after this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @exception BadPaddingException if decrypting and padding is choosen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * but the last input data does not have proper padding bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
               BadPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return core.doFinal(input, inputOffset, inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                            output, outputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *  Returns the key size of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param key the key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @return the key size of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @exception InvalidKeyException if <code>key</code> is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return 168;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Wrap a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param key the key to be wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @return the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @exception IllegalBlockSizeException if this cipher is a block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * cipher, no padding has been requested, and the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * encoding of the key to be wrapped is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * multiple of the block size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @exception InvalidKeyException if it is impossible or unsafe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * wrap the key with this cipher (e.g., a hardware protected key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * being passed to a software only cipher).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    protected byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return core.wrap(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Unwrap a previously wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @param wrappedKey the key to be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param wrappedKeyType the type of the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * This is one of <code>Cipher.SECRET_KEY</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @return the unwrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @exception NoSuchAlgorithmException if no installed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * can create keys of type <code>wrappedKeyType</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @exception InvalidKeyException if <code>wrappedKey</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * represent a wrapped key of type <code>wrappedKeyType</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * the <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    protected Key engineUnwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                                     String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                                     int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return core.unwrap(wrappedKey, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                           wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
}