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