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