src/java.base/share/classes/com/sun/crypto/provider/PBES1Core.java
author vinnie
Wed, 05 Jul 2017 17:27:46 +0100
changeset 47417 5984d1c9d03d
parent 47407 2f79180e86e9
child 51504 c9a3e3cac9c7
permissions -rw-r--r--
8181692: Update storage implementations Reviewed-by: weijun, igerasim
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
47407
2f79180e86e9 8171252: Improve exception checking
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class represents password-based encryption as defined by the PKCS #5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see javax.crypto.Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    42
final class PBES1Core {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // the encapsulated DES cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private CipherCore cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private int blkSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private String algo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private byte[] salt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private int iCount = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Creates an instance of PBE Cipher using the specified CipherSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 10336
diff changeset
    57
    PBES1Core(String cipherAlg) throws NoSuchAlgorithmException,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        algo = cipherAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (algo.equals("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            cipher = new CipherCore(new DESCrypt(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                    DESConstants.DES_BLOCK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } else if (algo.equals("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            cipher = new CipherCore(new DESedeCrypt(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                    DESConstants.DES_BLOCK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new NoSuchAlgorithmException("No Cipher implementation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                               "for PBEWithMD5And" + algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        cipher.setMode("CBC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        cipher.setPadding("PKCS5Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // get instance of MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        md = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Sets the mode of this cipher. This algorithm can only be run in CBC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param mode the cipher mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @exception NoSuchAlgorithmException if the requested cipher mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    void setMode(String mode) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        cipher.setMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Sets the padding mechanism of this cipher. This algorithm only uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * PKCS #5 padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param padding the padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @exception NoSuchPaddingException if the requested padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    void setPadding(String paddingScheme) throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        cipher.setPadding(paddingScheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Returns the block size (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @return the block size (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    int getBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return DESConstants.DES_BLOCK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Returns the length in bytes that an output buffer would need to be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * order to hold the result of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <code>doFinal</code> operation, given the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <code>inputLen</code> (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>This call takes into account any unprocessed (buffered) data from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * previous <code>update</code> call, and padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>The actual output length of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <code>doFinal</code> call may be smaller than the length returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param inputLen the input length (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return the required output buffer size (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    int getOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return cipher.getOutputSize(inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Returns the initialization vector (IV) in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <p> This is useful in the case where a random IV has been created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * (see <a href = "#init">init</a>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * or in the context of password-based encryption or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * decryption, where the IV is derived from a user-supplied password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return the initialization vector in a new buffer, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * underlying algorithm does not use an IV, or if the IV has not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    byte[] getIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return cipher.getIV();
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 parameters used with this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <p>The returned parameters may be the same that were used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * this cipher, or may contain the default set of parameters or a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * randomly generated parameters used by the underlying cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * implementation (provided that the underlying cipher implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * uses a default set of parameters or creates new parameters if it needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * parameters but was not initialized with any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return the parameters used with this cipher, or null if this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * does not use any parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    AlgorithmParameters getParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        AlgorithmParameters params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (salt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            salt = new byte[8];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
   167
            SunJCE.getRandom().nextBytes(salt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            params = AlgorithmParameters.getInstance("PBEWithMD5And" +
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   172
                (algo.equalsIgnoreCase("DES")? "DES":"TripleDES"),
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   173
                SunJCE.getInstance());
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   174
            params.init(pbeSpec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw new RuntimeException("SunJCE called, but not configured");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new RuntimeException("PBEParameterSpec not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Initializes this cipher with a key, a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * algorithm parameters, and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * encryption, decryption, key wrapping or key unwrapping, depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * requires any random bytes, it will get them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param opmode the operation mode of this cipher (this is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * parameters are inappropriate for this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    void init(int opmode, Key key, AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (((opmode == Cipher.DECRYPT_MODE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
             (opmode == Cipher.UNWRAP_MODE)) && (params == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new InvalidAlgorithmParameterException("Parameters "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                                         + "missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if ((key == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            (key.getEncoded() == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            !(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new InvalidKeyException("Missing password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // create random salt and use default iteration count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            salt = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            random.nextBytes(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (!(params instanceof PBEParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    ("Wrong parameter type: PBE expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            salt = ((PBEParameterSpec) params).getSalt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            // salt must be 8 bytes long (by definition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (salt.length != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    ("Salt must be 8 bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            iCount = ((PBEParameterSpec) params).getIterationCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (iCount <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    ("IterationCount must be a positive number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        byte[] derivedKey = deriveCipherKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // use all but the last 8 bytes as the key value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        SecretKeySpec cipherKey = new SecretKeySpec(derivedKey, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                                    derivedKey.length-8, algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // use the last 8 bytes as the IV
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        IvParameterSpec ivSpec = new IvParameterSpec(derivedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                                     derivedKey.length-8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                                     8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // initialize the underlying cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        cipher.init(opmode, cipherKey, ivSpec, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private byte[] deriveCipherKey(Key key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        byte[] result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        byte[] passwdBytes = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (algo.equals("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            // P || S (password concatenated with salt)
47407
2f79180e86e9 8171252: Improve exception checking
valeriep
parents: 47216
diff changeset
   263
            byte[] concat = new byte[Math.addExact(passwdBytes.length, salt.length)];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            System.arraycopy(passwdBytes, 0, concat, 0, passwdBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            java.util.Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            System.arraycopy(salt, 0, concat, passwdBytes.length, salt.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // digest P || S with c iterations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            byte[] toBeHashed = concat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            for (int i = 0; i < iCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                md.update(toBeHashed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                toBeHashed = md.digest(); // this resets the digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            java.util.Arrays.fill(concat, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            result = toBeHashed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else if (algo.equals("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            // if the 2 salt halves are the same, invert one of them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            for (i=0; i<4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                if (salt[i] != salt[i+4])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (i==4) { // same, invert 1st half
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                for (i=0; i<2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    byte tmp = salt[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    salt[i] = salt[3-i];
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47407
diff changeset
   287
                    salt[3-i] = tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // Now digest each half (concatenated with password). For each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // half, go through the loop as many times as specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // iteration count parameter (inner for loop).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            // Concatenate the output from each digest round with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            // password, and use the result as the input to the next digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            // operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            byte[] kBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            IvParameterSpec iv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            byte[] toBeHashed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            result = new byte[DESedeKeySpec.DES_EDE_KEY_LEN +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                              DESConstants.DES_BLOCK_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            for (i = 0; i < 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                toBeHashed = new byte[salt.length/2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                System.arraycopy(salt, i*(salt.length/2), toBeHashed, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                 toBeHashed.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                for (int j=0; j < iCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    md.update(toBeHashed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    toBeHashed = md.digest(); // this resets the digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                System.arraycopy(toBeHashed, 0, result, i*16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                 toBeHashed.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    void init(int opmode, Key key, AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        PBEParameterSpec pbeSpec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   324
                pbeSpec = params.getParameterSpec(PBEParameterSpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                throw new InvalidAlgorithmParameterException("Wrong parameter "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                                             + "type: PBE "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                                             + "expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        init(opmode, key, pbeSpec, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * buffer, starting at <code>inputOffset</code>, are processed, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return the new buffer with the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    byte[] update(byte[] input, int inputOffset, int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return cipher.update(input, inputOffset, inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * buffer, starting at <code>inputOffset</code>, are processed, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <code>outputOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    int update(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
               byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return cipher.update(input, inputOffset, inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                             output, outputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Encrypts or decrypts data in a single-part operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * or finishes a multiple-part operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * The data is encrypted or decrypted, depending on how this cipher was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * buffer, starting at <code>inputOffset</code>, and any input bytes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * may have been buffered during a previous <code>update</code> operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * are processed, with padding (if requested) being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * The result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <p>The cipher is reset to its initial state (uninitialized) after this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @return the new buffer with the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * block size
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 16909
diff changeset
   411
     * @exception BadPaddingException if decrypting and padding is chosen,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * but the last input data does not have proper padding bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    byte[] doFinal(byte[] input, int inputOffset, int inputLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return cipher.doFinal(input, inputOffset, inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Encrypts or decrypts data in a single-part operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * or finishes a multiple-part operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * The data is encrypted or decrypted, depending on how this cipher was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * buffer, starting at <code>inputOffset</code>, and any input bytes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * may have been buffered during a previous <code>update</code> operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * are processed, with padding (if requested) being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * The result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * <code>outputOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <p>The cipher is reset to its initial state (uninitialized) after this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * to hold the result
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 16909
diff changeset
   451
     * @exception BadPaddingException if decrypting and padding is chosen,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * but the last input data does not have proper padding bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    int doFinal(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
               BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return cipher.doFinal(input, inputOffset, inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                    output, outputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Wrap a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param key the key to be wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @return the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @exception IllegalBlockSizeException if this cipher is a block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * cipher, no padding has been requested, and the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * encoding of the key to be wrapped is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * multiple of the block size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @exception InvalidKeyException if it is impossible or unsafe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * wrap the key with this cipher (e.g., a hardware protected key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * being passed to a software only cipher).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    byte[] wrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        byte[] result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            byte[] encodedKey = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if ((encodedKey == null) || (encodedKey.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                throw new InvalidKeyException("Cannot get an encoding of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                              "the key to be wrapped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            result = doFinal(encodedKey, 0, encodedKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            // Should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Unwrap a previously wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param wrappedKey the key to be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param wrappedKeyType the type of the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * This is one of <code>Cipher.SECRET_KEY</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @return the unwrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @exception NoSuchAlgorithmException if no installed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * can create keys of type <code>wrappedKeyType</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @exception InvalidKeyException if <code>wrappedKey</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * represent a wrapped key of type <code>wrappedKeyType</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * the <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    Key unwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
               String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
               int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        byte[] encodedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            encodedKey = doFinal(wrappedKey, 0, wrappedKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        } catch (BadPaddingException ePadding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            throw new InvalidKeyException("The wrapped key is not padded " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                          "correctly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        } catch (IllegalBlockSizeException eBlockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            throw new InvalidKeyException("The wrapped key does not have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                                          "the correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        return ConstructKeys.constructKey(encodedKey, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                                          wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
}