jdk/src/share/classes/com/sun/crypto/provider/AESWrapCipher.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 5506 202f599c92aa
child 13672 604588823b5a
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     2
 * Copyright (c) 2004, 2009, 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.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class implements the AES KeyWrap algorithm as defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * in <a href=http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * "XML Encryption Syntax and Processing" section 5.6.3 "AES Key Wrap".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Note: only <code>ECB</code> mode and <code>NoPadding</code> padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * 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 AESCipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public final class AESWrapCipher extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final byte[] IV = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, (byte) 0xA6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, (byte) 0xA6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int blksize = AESConstants.AES_BLOCK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * internal cipher object which does the real work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private AESCrypt cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * are we encrypting or decrypting?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private boolean decrypting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Creates an instance of AES KeyWrap cipher with default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * mode, i.e. "ECB" and padding scheme, i.e. "NoPadding".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public AESWrapCipher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        cipher = new AESCrypt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Sets the mode of this cipher. Only "ECB" mode is accepted for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param mode the cipher mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @exception NoSuchAlgorithmException if the requested cipher mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * is not "ECB".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected void engineSetMode(String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (!mode.equalsIgnoreCase("ECB")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new NoSuchAlgorithmException(mode + " cannot be used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Sets the padding mechanism of this cipher. Only "NoPadding" schmem
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * is accepted for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param padding the padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @exception NoSuchPaddingException if the requested padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * is not "NoPadding".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected void engineSetPadding(String padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (!padding.equalsIgnoreCase("NoPadding")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new NoSuchPaddingException(padding + " cannot be used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Returns the block size (in bytes). i.e. 16 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return the block size (in bytes), i.e. 16 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return blksize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Returns the length in bytes that an output buffer would need to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * given the input length <code>inputLen</code> (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>The actual output length of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <code>doFinal</code> call may be smaller than the length returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param inputLen the input length (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @return the required output buffer size (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // can only return an upper-limit if not initialized yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (decrypting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            result = inputLen - 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            result = inputLen + 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return (result < 0? 0:result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Returns the initialization vector (IV) which is null for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return null for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Initializes this cipher with a key and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>The cipher only supports the following two operation modes:<b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Cipher.WRAP_MODE, and <b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Cipher.UNWRAP_MODE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <p>For modes other than the above two, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param opmode the operation mode of this cipher. Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param key the secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * initializing this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (opmode == Cipher.WRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            decrypting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } else if (opmode == Cipher.UNWRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            decrypting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new UnsupportedOperationException("This cipher can " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                "only be used for key wrapping and unwrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        cipher.init(decrypting, key.getAlgorithm(), key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Initializes this cipher with a key, a set of algorithm parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p>The cipher only supports the following two operation modes:<b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Cipher.WRAP_MODE, and <b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Cipher.UNWRAP_MODE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <p>For modes other than the above two, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param opmode the operation mode of this cipher. Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param key the secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param params the algorithm parameters; must be null for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * parameters is not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                              AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new InvalidAlgorithmParameterException("This cipher " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                "does not accept any parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        engineInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Initializes this cipher with a key, a set of algorithm parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>The cipher only supports the following two operation modes:<b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Cipher.WRAP_MODE, and <b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Cipher.UNWRAP_MODE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p>For modes other than the above two, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param opmode the operation mode of this cipher. Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param key the secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param params the algorithm parameters; must be null for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @exception InvalidKeyException if the given key is inappropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * parameters is not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                              AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            throw new InvalidAlgorithmParameterException("This cipher " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                "does not accept any parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        engineInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return n/a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    protected byte[] engineUpdate(byte[] in, int inOffset, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param out the buffer for the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param outOffset the offset in <code>out</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return n/a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    protected int engineUpdate(byte[] in, int inOffset, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                               byte[] out, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param in the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return n/a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    protected byte[] engineDoFinal(byte[] input, int inputOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                   int inputLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param out the buffer for the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param outOffset the ofset in <code>out</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @return n/a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    protected int engineDoFinal(byte[] in, int inOffset, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                byte[] out, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        throws IllegalBlockSizeException, ShortBufferException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
               BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Returns the parameters used with this cipher which is always null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @return null since this cipher does not use any parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Returns the key size of the given key object in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param key the key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return the "effective" key size of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @exception InvalidKeyException if <code>key</code> is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (!AESCrypt.isKeySizeValid(encoded.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            throw new InvalidKeyException("Invalid key length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                          encoded.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return encoded.length * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Wrap a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param key the key to be wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @return the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @exception IllegalBlockSizeException if this cipher is a block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * cipher, no padding has been requested, and the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * encoding of the key to be wrapped is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * multiple of the block size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @exception InvalidKeyException if it is impossible or unsafe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * wrap the key with this cipher (e.g., a hardware protected key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * being passed to a software only cipher).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    protected byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        byte[] keyVal = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if ((keyVal == null) || (keyVal.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            throw new InvalidKeyException("Cannot get an encoding of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                          "the key to be wrapped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        byte[] out = new byte[keyVal.length + 8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (keyVal.length == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            System.arraycopy(IV, 0, out, 0, IV.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            System.arraycopy(keyVal, 0, out, IV.length, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            cipher.encryptBlock(out, 0, out, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (keyVal.length % 8 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                throw new IllegalBlockSizeException("length of the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    "to be wrapped key should be multiples of 8 bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            System.arraycopy(IV, 0, out, 0, IV.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            System.arraycopy(keyVal, 0, out, IV.length, keyVal.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            int N = keyVal.length/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            byte[] buffer = new byte[blksize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            for (int j = 0; j < 6; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                for (int i = 1; i <= N; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    int T = i + j*N;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    System.arraycopy(out, 0, buffer, 0, IV.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    System.arraycopy(out, i*8, buffer, IV.length, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    cipher.encryptBlock(buffer, 0, buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    for (int k = 1; T != 0; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        byte v = (byte) T;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        buffer[IV.length - k] ^= v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        T >>>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    System.arraycopy(buffer, 0, out, 0, IV.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    System.arraycopy(buffer, 8, out, 8*i, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Unwrap a previously wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param wrappedKey the key to be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param wrappedKeyType the type of the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * This is one of <code>Cipher.SECRET_KEY</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @return the unwrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @exception NoSuchAlgorithmException if no installed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * can create keys of type <code>wrappedKeyType</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @exception InvalidKeyException if <code>wrappedKey</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * represent a wrapped key of type <code>wrappedKeyType</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * the <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    protected Key engineUnwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                               String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                               int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        int wrappedKeyLen = wrappedKey.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        // ensure the wrappedKey length is multiples of 8 bytes and non-zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (wrappedKeyLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            throw new InvalidKeyException("The wrapped key is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (wrappedKeyLen % 8 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                ("The wrapped key has invalid key length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        byte[] out = new byte[wrappedKeyLen - 8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        byte[] buffer = new byte[blksize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (wrappedKeyLen == 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            cipher.decryptBlock(wrappedKey, 0, buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            for (int i = 0; i < IV.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (IV[i] != buffer[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    throw new InvalidKeyException("Integrity check failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            System.arraycopy(buffer, IV.length, out, 0, out.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            System.arraycopy(wrappedKey, 0, buffer, 0, IV.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            System.arraycopy(wrappedKey, IV.length, out, 0, out.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            int N = out.length/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            for (int j = 5; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                for (int i = N; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    int T = i + j*N;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    System.arraycopy(out, 8*(i-1), buffer, IV.length, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    for (int k = 1; T != 0; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        byte v = (byte) T;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        buffer[IV.length - k] ^= v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        T >>>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    cipher.decryptBlock(buffer, 0, buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    System.arraycopy(buffer, IV.length, out, 8*(i-1), 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            for (int i = 0; i < IV.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (IV[i] != buffer[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    throw new InvalidKeyException("Integrity check failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return ConstructKeys.constructKey(out, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                          wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
}