jdk/src/share/classes/com/sun/crypto/provider/AESWrapCipher.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3353 ddbd63234844
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.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
     * @exception SecurityException if this constructor fails to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * its own integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public AESWrapCipher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        SunJCE.ensureIntegrity(getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        cipher = new AESCrypt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Sets the mode of this cipher. Only "ECB" mode is accepted for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param mode the cipher mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @exception NoSuchAlgorithmException if the requested cipher mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * is not "ECB".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected void engineSetMode(String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (!mode.equalsIgnoreCase("ECB")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new NoSuchAlgorithmException(mode + " cannot be used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Sets the padding mechanism of this cipher. Only "NoPadding" schmem
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * is accepted for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param padding the padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @exception NoSuchPaddingException if the requested padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * is not "NoPadding".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected void engineSetPadding(String padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (!padding.equalsIgnoreCase("NoPadding")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new NoSuchPaddingException(padding + " cannot be used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Returns the block size (in bytes). i.e. 16 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @return the block size (in bytes), i.e. 16 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return blksize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Returns the length in bytes that an output buffer would need to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * given the input length <code>inputLen</code> (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <p>The actual output length of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <code>doFinal</code> call may be smaller than the length returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param inputLen the input length (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return the required output buffer size (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // can only return an upper-limit if not initialized yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (decrypting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            result = inputLen - 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            result = inputLen + 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return (result < 0? 0:result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Returns the initialization vector (IV) which is null for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return null for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Initializes this cipher with a key and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <p>The cipher only supports the following two operation modes:<b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Cipher.WRAP_MODE, and <b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Cipher.UNWRAP_MODE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <p>For modes other than the above two, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param opmode the operation mode of this cipher. Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param key the secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * initializing this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (opmode == Cipher.WRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            decrypting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } else if (opmode == Cipher.UNWRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            decrypting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new UnsupportedOperationException("This cipher can " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                "only be used for key wrapping and unwrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        cipher.init(decrypting, key.getAlgorithm(), key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Initializes this cipher with a key, a set of algorithm parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <p>The cipher only supports the following two operation modes:<b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Cipher.WRAP_MODE, and <b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Cipher.UNWRAP_MODE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <p>For modes other than the above two, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param opmode the operation mode of this cipher. Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param key the secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param params the algorithm parameters; must be null for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * parameters is not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                              AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new InvalidAlgorithmParameterException("This cipher " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                "does not accept any parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        engineInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Initializes this cipher with a key, a set of algorithm parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p>The cipher only supports the following two operation modes:<b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Cipher.WRAP_MODE, and <b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Cipher.UNWRAP_MODE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <p>For modes other than the above two, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param opmode the operation mode of this cipher. Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param key the secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param params the algorithm parameters; must be null for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @exception InvalidKeyException if the given key is inappropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * parameters is not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                              AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new InvalidAlgorithmParameterException("This cipher " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                "does not accept any parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        engineInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return n/a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    protected byte[] engineUpdate(byte[] in, int inOffset, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param out the buffer for the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param outOffset the offset in <code>out</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return n/a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    protected int engineUpdate(byte[] in, int inOffset, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                               byte[] out, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param in the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @return n/a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    protected byte[] engineDoFinal(byte[] input, int inputOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                   int inputLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * This operation is not supported by this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Since it's impossible to initialize this cipher given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * current Cipher.engineInit(...) implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * IllegalStateException will always be thrown upon invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param in the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param inOffset the offset in <code>in</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @param inLen the input length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param out the buffer for the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param outOffset the ofset in <code>out</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @return n/a.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @exception IllegalStateException upon invocation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    protected int engineDoFinal(byte[] in, int inOffset, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                byte[] out, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        throws IllegalBlockSizeException, ShortBufferException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
               BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        throw new IllegalStateException("Cipher has not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Returns the parameters used with this cipher which is always null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * for this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return null since this cipher does not use any parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Returns the key size of the given key object in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param key the key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @return the "effective" key size of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @exception InvalidKeyException if <code>key</code> is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (!AESCrypt.isKeySizeValid(encoded.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            throw new InvalidKeyException("Invalid key length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                          encoded.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return encoded.length * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Wrap a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param key the key to be wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @return the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @exception IllegalBlockSizeException if this cipher is a block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * cipher, no padding has been requested, and the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * encoding of the key to be wrapped is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * multiple of the block size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @exception InvalidKeyException if it is impossible or unsafe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * wrap the key with this cipher (e.g., a hardware protected key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * being passed to a software only cipher).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    protected byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        byte[] keyVal = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if ((keyVal == null) || (keyVal.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new InvalidKeyException("Cannot get an encoding of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                          "the key to be wrapped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        byte[] out = new byte[keyVal.length + 8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (keyVal.length == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            System.arraycopy(IV, 0, out, 0, IV.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            System.arraycopy(keyVal, 0, out, IV.length, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            cipher.encryptBlock(out, 0, out, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if (keyVal.length % 8 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                throw new IllegalBlockSizeException("length of the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    "to be wrapped key should be multiples of 8 bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            System.arraycopy(IV, 0, out, 0, IV.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            System.arraycopy(keyVal, 0, out, IV.length, keyVal.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            int N = keyVal.length/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            byte[] buffer = new byte[blksize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            for (int j = 0; j < 6; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                for (int i = 1; i <= N; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    int T = i + j*N;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    System.arraycopy(out, 0, buffer, 0, IV.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    System.arraycopy(out, i*8, buffer, IV.length, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    cipher.encryptBlock(buffer, 0, buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    for (int k = 1; T != 0; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        byte v = (byte) T;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        buffer[IV.length - k] ^= v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        T >>>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    System.arraycopy(buffer, 0, out, 0, IV.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    System.arraycopy(buffer, 8, out, 8*i, 8);
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
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Unwrap a previously wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param wrappedKey the key to be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param wrappedKeyType the type of the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * This is one of <code>Cipher.SECRET_KEY</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @return the unwrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @exception NoSuchAlgorithmException if no installed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * can create keys of type <code>wrappedKeyType</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @exception InvalidKeyException if <code>wrappedKey</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * represent a wrapped key of type <code>wrappedKeyType</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * the <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    protected Key engineUnwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                               String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                               int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        int wrappedKeyLen = wrappedKey.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        // ensure the wrappedKey length is multiples of 8 bytes and non-zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (wrappedKeyLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            throw new InvalidKeyException("The wrapped key is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (wrappedKeyLen % 8 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                ("The wrapped key has invalid key length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        byte[] out = new byte[wrappedKeyLen - 8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        byte[] buffer = new byte[blksize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (wrappedKeyLen == 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            cipher.decryptBlock(wrappedKey, 0, buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            for (int i = 0; i < IV.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                if (IV[i] != buffer[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    throw new InvalidKeyException("Integrity check failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            System.arraycopy(buffer, IV.length, out, 0, out.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            System.arraycopy(wrappedKey, 0, buffer, 0, IV.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            System.arraycopy(wrappedKey, IV.length, out, 0, out.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            int N = out.length/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            for (int j = 5; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                for (int i = N; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    int T = i + j*N;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    System.arraycopy(out, 8*(i-1), buffer, IV.length, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    for (int k = 1; T != 0; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        byte v = (byte) T;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        buffer[IV.length - k] ^= v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        T >>>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    cipher.decryptBlock(buffer, 0, buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    System.arraycopy(buffer, IV.length, out, 8*(i-1), 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            for (int i = 0; i < IV.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                if (IV[i] != buffer[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    throw new InvalidKeyException("Integrity check failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return ConstructKeys.constructKey(out, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                                          wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
}