jdk/src/share/classes/com/sun/crypto/provider/DESedeWrapCipher.java
author valeriep
Tue, 19 Nov 2013 15:29:56 -0800
changeset 21837 0c41fa97176a
parent 16909 78a1749a43e2
permissions -rw-r--r--
8026943: SQE test jce/Global/Cipher/SameBuffer failed Summary: Always use different input/output buffers when calling FeedbackCipher objects Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 10336
diff changeset
     2
 * Copyright (c) 2004, 2013, 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: 3353
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: 3353
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: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
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 implements the CMS DESede KeyWrap algorithm as defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * in <a href=http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * "XML Encryption Syntax and Processing" section 5.6.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * "CMS Triple DES Key Wrap".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Note: only <code>CBC</code> mode and <code>NoPadding</code> padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * scheme can be used for this algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see DESedeCipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public final class DESedeWrapCipher extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final byte[] IV2 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        (byte) 0x4a, (byte) 0xdd, (byte) 0xa2, (byte) 0x2c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        (byte) 0x79, (byte) 0xe8, (byte) 0x21, (byte) 0x05
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
    53
    private static final int CHECKSUM_LEN = 8;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
    54
    private static final int IV_LEN = 8;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
    55
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * internal cipher object which does the real work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private FeedbackCipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * iv for (re-)initializing the internal cipher object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private byte[] iv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * key for re-initializing the internal cipher object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private Key cipherKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * are we encrypting or decrypting?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private boolean decrypting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Creates an instance of CMS DESede KeyWrap cipher with default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * mode, i.e. "CBC" and padding scheme, i.e. "NoPadding".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public DESedeWrapCipher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        cipher = new CipherBlockChaining(new DESedeCrypt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Sets the mode of this cipher. Only "CBC" mode is accepted for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param mode the cipher mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @exception NoSuchAlgorithmException if the requested cipher mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * is not "CBC".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected void engineSetMode(String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (!mode.equalsIgnoreCase("CBC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new NoSuchAlgorithmException(mode + " cannot be used");
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. Only "NoPadding" schmem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * is accepted for this cipher.
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 not "NoPadding".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected void engineSetPadding(String padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (!padding.equalsIgnoreCase("NoPadding")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new NoSuchPaddingException(padding + " cannot be used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Returns the block size (in bytes), i.e. 8 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return the block size (in bytes), i.e. 8 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return DESConstants.DES_BLOCK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Returns the length in bytes that an output buffer would need to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * given the input length <code>inputLen</code> (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <p>The actual output length of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <code>doFinal</code> call may be smaller than the length returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param inputLen the input length (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return the required output buffer size (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    protected int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // can only return an upper-limit if not initialized yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (decrypting) {
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   141
            result = inputLen - 16; // CHECKSUM_LEN + IV_LEN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            result = inputLen + 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return (result < 0? 0:result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Returns the initialization vector (IV) in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return the initialization vector, or null if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * algorithm does not use an IV, or if the IV has not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected byte[] engineGetIV() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   156
        return (iv == null) ? null : iv.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Initializes this cipher with a key and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <p>The cipher only supports the following two operation modes:<b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Cipher.WRAP_MODE, and <b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Cipher.UNWRAP_MODE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <p>For modes other than the above two, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>If this cipher requires an initialization vector (IV), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * it from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param opmode the operation mode of this cipher. Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param key the secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @exception InvalidKeyException if the given key is inappropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * or if parameters are required but not supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            InvalidKeyException ike =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                new InvalidKeyException("Parameters required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            ike.initCause(iape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Initializes this cipher with a key, a set of algorithm parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <p>The cipher only supports the following two operation modes:<b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Cipher.WRAP_MODE, and <b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Cipher.UNWRAP_MODE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <p>For modes other than the above two, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p>If this cipher requires an initialization vector (IV), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * it from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param opmode the operation mode of this cipher. Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param key the secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param params the algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @exception InvalidKeyException if the given key is inappropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * parameters are inappropriate for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                              AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        byte[] currIv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (opmode == Cipher.WRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            decrypting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (params == null) {
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   221
                iv = new byte[IV_LEN];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                if (random == null) {
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 10336
diff changeset
   223
                    random = SunJCE.getRandom();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                random.nextBytes(iv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            else if (params instanceof IvParameterSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                iv = ((IvParameterSpec) params).getIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    ("Wrong parameter type: IV expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            currIv = iv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } else if (opmode == Cipher.UNWRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    ("No parameter accepted for unwrapping keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            iv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            decrypting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            currIv = IV2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throw new UnsupportedOperationException("This cipher can " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                "only be used for key wrapping and unwrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        cipher.init(decrypting, key.getAlgorithm(), key.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    currIv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        cipherKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Initializes this cipher with a key, a set of algorithm parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p>The cipher only supports the following two operation modes:<b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Cipher.WRAP_MODE, and <b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Cipher.UNWRAP_MODE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <p>For modes other than the above two, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <p>If this cipher requires an initialization vector (IV), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * it from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param opmode the operation mode of this cipher. Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param key the secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param params the algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @exception InvalidKeyException if the given key is inappropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * parameters are inappropriate for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                              AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        IvParameterSpec ivSpec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                DESedeParameters paramsEng = new DESedeParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                paramsEng.engineInit(params.getEncoded());
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   282
                ivSpec = paramsEng.engineGetParameterSpec(IvParameterSpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                InvalidAlgorithmParameterException iape =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        ("Wrong parameter type: IV expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                iape.initCause(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                throw iape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        engineInit(opmode, key, ivSpec, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return n/a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    protected byte[] engineUpdate(byte[] in, int inOffset, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @param out the buffer for the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param outOffset the offset in <code>out</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return n/a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    protected int engineUpdate(byte[] in, int inOffset, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                               byte[] out, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param inLen 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
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    protected byte[] engineDoFinal(byte[] in, int inOffset, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param out the buffer for the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param outOffset the ofset in <code>out</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @return the number of bytes stored in <code>out</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        throws IllegalBlockSizeException, ShortBufferException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
               BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        throw new IllegalStateException("Cipher has not been initialized");
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 parameters used with this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Note that null maybe returned if this cipher does not use any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * parameters or when it has not be set, e.g. initialized with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * UNWRAP_MODE but wrapped key data has not been given.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @return the parameters used with this cipher; can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        AlgorithmParameters params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (iv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            String algo = cipherKey.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            try {
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   395
                params = AlgorithmParameters.getInstance(algo,
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   396
                    SunJCE.getInstance());
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   397
                params.init(new IvParameterSpec(iv));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                throw new RuntimeException("Cannot find " + algo +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    " AlgorithmParameters implementation in SunJCE provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                throw new RuntimeException("IvParameterSpec not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Returns the key size of the given key object in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * This cipher always return the same key size as the DESede ciphers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param key the key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @return the "effective" key size of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @exception InvalidKeyException if <code>key</code> is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (encoded.length != 24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throw new InvalidKeyException("Invalid key length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                encoded.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        // Return the effective key length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return 112;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Wrap a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param key the key to be wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @return the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @exception IllegalBlockSizeException if this cipher is a block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * cipher, no padding has been requested, and the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * encoding of the key to be wrapped is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * multiple of the block size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @exception InvalidKeyException if it is impossible or unsafe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * wrap the key with this cipher (e.g., a hardware protected key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * being passed to a software only cipher).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    protected byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        byte[] keyVal = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if ((keyVal == null) || (keyVal.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            throw new InvalidKeyException("Cannot get an encoding of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                                          "the key to be wrapped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        byte[] cks = getChecksum(keyVal);
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   455
        byte[] in = new byte[keyVal.length + CHECKSUM_LEN];
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   456
        System.arraycopy(keyVal, 0, in, 0, keyVal.length);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   457
        System.arraycopy(cks, 0, in, keyVal.length, CHECKSUM_LEN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   459
        byte[] out = new byte[iv.length + in.length];
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   460
        System.arraycopy(iv, 0, out, 0, iv.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   462
        cipher.encrypt(in, 0, in.length, out, iv.length);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   463
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        // reverse the array content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        for (int i = 0; i < out.length/2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            byte temp = out[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            out[i] = out[out.length-1-i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            out[out.length-1-i] = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            cipher.init(false, cipherKey.getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        cipherKey.getEncoded(), IV2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        } catch (InvalidKeyException ike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            throw new RuntimeException("Internal cipher key is corrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   477
        byte[] out2 = new byte[out.length];
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   478
        cipher.encrypt(out, 0, out.length, out2, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        // restore cipher state to prior to this call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            cipher.init(decrypting, cipherKey.getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                        cipherKey.getEncoded(), iv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        } catch (InvalidKeyException ike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            throw new RuntimeException("Internal cipher key is corrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   488
        return out2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Unwrap a previously wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @param wrappedKey the key to be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @param wrappedKeyType the type of the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * This is one of <code>Cipher.SECRET_KEY</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @return the unwrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @exception NoSuchAlgorithmException if no installed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * can create keys of type <code>wrappedKeyType</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @exception InvalidKeyException if <code>wrappedKey</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * represent a wrapped key of type <code>wrappedKeyType</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * the <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    protected Key engineUnwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                               String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                               int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (wrappedKey.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            throw new InvalidKeyException("The wrapped key is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        byte[] buffer = new byte[wrappedKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        cipher.decrypt(wrappedKey, 0, wrappedKey.length, buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        // reverse array content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        for (int i = 0; i < buffer.length/2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            byte temp = buffer[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            buffer[i] = buffer[buffer.length-1-i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            buffer[buffer.length-1-i] = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   528
        iv = new byte[IV_LEN];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        System.arraycopy(buffer, 0, iv, 0, iv.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        cipher.init(true, cipherKey.getAlgorithm(), cipherKey.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    iv);
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   532
        byte[] buffer2 = new byte[buffer.length - iv.length];
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   533
        cipher.decrypt(buffer, iv.length, buffer2.length,
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   534
                       buffer2, 0);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   535
        int keyValLen = buffer2.length - CHECKSUM_LEN;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   536
        byte[] cks = getChecksum(buffer2, 0, keyValLen);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   537
        int offset = keyValLen;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   538
        for (int i = 0; i < CHECKSUM_LEN; i++) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   539
            if (buffer2[offset + i] != cks[i]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                throw new InvalidKeyException("Checksum comparison failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        // restore cipher state to prior to this call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        cipher.init(decrypting, cipherKey.getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    cipherKey.getEncoded(), IV2);
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   546
        byte[] out = new byte[keyValLen];
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   547
        System.arraycopy(buffer2, 0, out, 0, keyValLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return ConstructKeys.constructKey(out, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                          wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    private static final byte[] getChecksum(byte[] in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return getChecksum(in, 0, in.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    private static final byte[] getChecksum(byte[] in, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        MessageDigest md = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            md = MessageDigest.getInstance("SHA1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            throw new RuntimeException("SHA1 message digest not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        md.update(in, offset, len);
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 16909
diff changeset
   563
        byte[] cks = new byte[CHECKSUM_LEN];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        System.arraycopy(md.digest(), 0, cks, 0, cks.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return cks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
}