jdk/src/share/classes/javax/crypto/Cipher.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8152 94e5966bdf22
child 9275 1df1f7dfab7f
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8152
diff changeset
     2
 * Copyright (c) 1997, 2011, 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: 1692
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: 1692
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: 1692
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1692
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1692
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 javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import static java.util.Locale.ENGLISH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.nio.ReadOnlyBufferException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This class provides the functionality of a cryptographic cipher for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * encryption and decryption. It forms the core of the Java Cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Extension (JCE) framework.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>In order to create a Cipher object, the application calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Cipher's <code>getInstance</code> method, and passes the name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * requested <i>transformation</i> to it. Optionally, the name of a provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * may be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>A <i>transformation</i> is a string that describes the operation (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * set of operations) to be performed on the given input, to produce some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * output. A transformation always includes the name of a cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * algorithm (e.g., <i>DES</i>), and may be followed by a feedback mode and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * padding scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p> A transformation is of the form:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <li>"<i>algorithm/mode/padding</i>" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <li>"<i>algorithm</i>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <P> (in the latter case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * provider-specific default values for the mode and padding scheme are used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * For example, the following is a valid transformation:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     Cipher c = Cipher.getInstance("<i>DES/CBC/PKCS5Padding</i>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    81
 * Using modes such as <code>CFB</code> and <code>OFB</code>, block
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * ciphers can encrypt data in units smaller than the cipher's actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * block size.  When requesting such a mode, you may optionally specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * the number of bits to be processed at a time by appending this number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * to the mode name as shown in the "<code>DES/CFB8/NoPadding</code>" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * "<code>DES/OFB32/PKCS5Padding</code>" transformations. If no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * number is specified, a provider-specific default is used. (For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * example, the SunJCE provider uses a default of 64 bits for DES.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Thus, block ciphers can be turned into byte-oriented stream ciphers by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * using an 8 bit mode such as CFB8 or OFB8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    92
 * <p> Every implementation of the Java platform is required to support
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    93
 * the following standard <code>Cipher</code> transformations with the keysizes
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    94
 * in parentheses:
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    95
 * <ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    96
 * <li><tt>AES/CBC/NoPadding</tt> (128)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    97
 * <li><tt>AES/CBC/PKCS5Padding</tt> (128)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    98
 * <li><tt>AES/ECB/NoPadding</tt> (128)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    99
 * <li><tt>AES/ECB/PKCS5Padding</tt> (128)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   100
 * <li><tt>DES/CBC/NoPadding</tt> (56)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   101
 * <li><tt>DES/CBC/PKCS5Padding</tt> (56)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   102
 * <li><tt>DES/ECB/NoPadding</tt> (56)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   103
 * <li><tt>DES/ECB/PKCS5Padding</tt> (56)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   104
 * <li><tt>DESede/CBC/NoPadding</tt> (168)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   105
 * <li><tt>DESede/CBC/PKCS5Padding</tt> (168)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   106
 * <li><tt>DESede/ECB/NoPadding</tt> (168)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   107
 * <li><tt>DESede/ECB/PKCS5Padding</tt> (168)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   108
 * <li><tt>RSA/ECB/PKCS1Padding</tt> (1024, 2048)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   109
 * <li><tt>RSA/ECB/OAEPWithSHA-1AndMGF1Padding</tt> (1024, 2048)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   110
 * <li><tt>RSA/ECB/OAEPWithSHA-256AndMGF1Padding</tt> (1024, 2048)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   111
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   112
 * These transformations are described in the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   113
 * <a href="{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   114
 * Cipher section</a> of the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   115
 * Java Cryptography Architecture Standard Algorithm Name Documentation.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   116
 * Consult the release documentation for your implementation to see if any
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   117
 * other transformations are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   118
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * @see KeyGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @see SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
public class Cipher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static final Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        Debug.getInstance("jca", "Cipher");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Constant used to initialize cipher to encryption mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static final int ENCRYPT_MODE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Constant used to initialize cipher to decryption mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static final int DECRYPT_MODE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Constant used to initialize cipher to key-wrapping mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static final int WRAP_MODE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Constant used to initialize cipher to key-unwrapping mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static final int UNWRAP_MODE = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Constant used to indicate the to-be-unwrapped key is a "public key".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static final int PUBLIC_KEY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Constant used to indicate the to-be-unwrapped key is a "private key".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public static final int PRIVATE_KEY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Constant used to indicate the to-be-unwrapped key is a "secret key".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static final int SECRET_KEY = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private CipherSpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // The transformation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private String transformation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // Crypto permission representing the maximum allowable cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // strength that this Cipher object can be used for. (The cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // strength is a function of the keysize and algorithm parameters encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // in the crypto permission.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private CryptoPermission cryptoPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // The exemption mechanism that needs to be enforced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private ExemptionMechanism exmech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    // Flag which indicates whether or not this cipher has been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // The operation mode - store the operation mode after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    // cipher has been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private int opmode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    // The OID for the KeyUsage extension in an X.509 v3 certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static final String KEY_USAGE_EXTENSION_OID = "2.5.29.15";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    // next SPI  to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private CipherSpi firstSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    // next service to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private Service firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    // remaining services to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private Iterator serviceIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    // list of transform Strings to lookup in the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private List transforms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private final Object lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Creates a Cipher object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param cipherSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param transformation the transformation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    protected Cipher(CipherSpi cipherSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                     Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                     String transformation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // See bug 4341369 & 4334690 for more info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // If the caller is trusted, then okey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // Otherwise throw a NullPointerException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (!JceSecurityManager.INSTANCE.isCallerTrusted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        this.spi = cipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        this.transformation = transformation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        this.cryptoPerm = CryptoAllPermission.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        this.lock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Creates a Cipher object. Called internally and by NullCipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param cipherSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param transformation the transformation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    Cipher(CipherSpi cipherSpi, String transformation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        this.spi = cipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        this.transformation = transformation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        this.cryptoPerm = CryptoAllPermission.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        this.lock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private Cipher(CipherSpi firstSpi, Service firstService,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Iterator serviceIterator, String transformation, List transforms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        this.firstSpi = firstSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        this.firstService = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        this.serviceIterator = serviceIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        this.transforms = transforms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        this.transformation = transformation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        this.lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private static String[] tokenizeTransformation(String transformation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (transformation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            throw new NoSuchAlgorithmException("No transformation given");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         * array containing the components of a Cipher transformation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         * index 0: algorithm component (e.g., DES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         * index 1: feedback component (e.g., CFB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         * index 2: padding component (e.g., PKCS5Padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        String[] parts = new String[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        StringTokenizer parser = new StringTokenizer(transformation, "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            while (parser.hasMoreTokens() && count < 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                parts[count++] = parser.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (count == 0 || count == 2 || parser.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                throw new NoSuchAlgorithmException("Invalid transformation"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                               + " format:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                               transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } catch (NoSuchElementException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new NoSuchAlgorithmException("Invalid transformation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                           "format:" + transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if ((parts[0] == null) || (parts[0].length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throw new NoSuchAlgorithmException("Invalid transformation:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                   "algorithm not specified-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                   + transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return parts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // Provider attribute name for supported chaining mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private final static String ATTR_MODE = "SupportedModes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    // Provider attribute name for supported padding names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private final static String ATTR_PAD  = "SupportedPaddings";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    // constants indicating whether the provider supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    // a given mode or padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private final static int S_NO    = 0;       // does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    private final static int S_MAYBE = 1;       // unable to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private final static int S_YES   = 2;       // does support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Nested class to deal with modes and paddings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    private static class Transform {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // transform string to lookup in the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        final String transform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        // the mode/padding suffix in upper case. for example, if the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // to lookup is "DES/CBC/PKCS5Padding" suffix is "/CBC/PKCS5PADDING"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // if loopup is "DES", suffix is the empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // needed because aliases prevent straight transform.equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        final String suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // value to pass to setMode() or null if no such call required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        final String mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // value to pass to setPadding() or null if no such call required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        final String pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        Transform(String alg, String suffix, String mode, String pad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            this.transform = alg + suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            this.suffix = suffix.toUpperCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            this.mode = mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            this.pad = pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // set mode and padding for the given SPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        void setModePadding(CipherSpi spi) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (mode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                spi.engineSetMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (pad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                spi.engineSetPadding(pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // check whether the given services supports the mode and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // padding described by this Transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int supportsModePadding(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            int smode = supportsMode(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (smode == S_NO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                return smode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            int spad = supportsPadding(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // our constants are defined so that Math.min() is a tri-valued AND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return Math.min(smode, spad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // separate methods for mode and padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // called directly by Cipher only to throw the correct exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        int supportsMode(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return supports(s, ATTR_MODE, mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        int supportsPadding(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return supports(s, ATTR_PAD, pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        private static int supports(Service s, String attrName, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                return S_YES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            String regexp = s.getAttribute(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (regexp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                return S_MAYBE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return matches(regexp, value) ? S_YES : S_NO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // Map<String,Pattern> for previously compiled patterns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // XXX use ConcurrentHashMap once available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        private final static Map patternCache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            Collections.synchronizedMap(new HashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        private static boolean matches(String regexp, String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            Pattern pattern = (Pattern)patternCache.get(regexp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (pattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                pattern = Pattern.compile(regexp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                patternCache.put(regexp, pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return pattern.matcher(str.toUpperCase(Locale.ENGLISH)).matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    private static List getTransforms(String transformation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        String[] parts = tokenizeTransformation(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        String alg = parts[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        String mode = parts[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        String pad = parts[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if ((mode != null) && (mode.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            mode = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if ((pad != null) && (pad.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            pad = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if ((mode == null) && (pad == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // DES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            Transform tr = new Transform(alg, "", null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return Collections.singletonList(tr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else { // if ((mode != null) && (pad != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            // DES/CBC/PKCS5Padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            List list = new ArrayList(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            list.add(new Transform(alg, "/" + mode + "/" + pad, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            list.add(new Transform(alg, "/" + mode, null, pad));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            list.add(new Transform(alg, "//" + pad, mode, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            list.add(new Transform(alg, "", mode, pad));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    // get the transform matching the specified service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    private static Transform getTransform(Service s, List transforms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        String alg = s.getAlgorithm().toUpperCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        for (Iterator t = transforms.iterator(); t.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            Transform tr = (Transform)t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (alg.endsWith(tr.suffix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                return tr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Returns a <code>Cipher</code> object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * A new Cipher object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * CipherSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @param transformation the name of the transformation, e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <i>DES/CBC/PKCS5Padding</i>.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   438
     * See the Cipher section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   439
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   440
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * for information about standard transformation names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return a cipher that implements the requested transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @exception NoSuchAlgorithmException if <code>transformation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *          is null, empty, in an invalid format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *          or if no Provider supports a CipherSpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @exception NoSuchPaddingException if <code>transformation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *          contains a padding scheme that is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public static final Cipher getInstance(String transformation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            throws NoSuchAlgorithmException, NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        List transforms = getTransforms(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        List cipherServices = new ArrayList(transforms.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        for (Iterator t = transforms.iterator(); t.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            Transform transform = (Transform)t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            cipherServices.add(new ServiceId("Cipher", transform.transform));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        List services = GetInstance.getServices(cipherServices);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // make sure there is at least one service from a signed provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // and that it can use the specified mode and padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        Iterator t = services.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        while (t.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            Service s = (Service)t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            Transform tr = getTransform(s, transforms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (tr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            int canuse = tr.supportsModePadding(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (canuse == S_NO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                // does not support mode or padding we need, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (canuse == S_YES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                return new Cipher(null, s, t, transformation, transforms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            } else { // S_MAYBE, try out if it works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    CipherSpi spi = (CipherSpi)s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    tr.setModePadding(spi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    return new Cipher(spi, s, t, transformation, transforms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            ("Cannot find any provider supporting " + transformation, failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Returns a <code>Cipher</code> object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * <p> A new Cipher object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * CipherSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param transformation the name of the transformation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * e.g., <i>DES/CBC/PKCS5Padding</i>.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   514
     * See the Cipher section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   515
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   516
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * for information about standard transformation names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @return a cipher that implements the requested transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @exception NoSuchAlgorithmException if <code>transformation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *          is null, empty, in an invalid format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *          or if a CipherSpi implementation for the specified algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *          is not available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @exception NoSuchPaddingException if <code>transformation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *          contains a padding scheme that is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *          is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    public static final Cipher getInstance(String transformation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                                           String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            throws NoSuchAlgorithmException, NoSuchProviderException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if ((provider == null) || (provider.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            throw new IllegalArgumentException("Missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        Provider p = Security.getProvider(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            throw new NoSuchProviderException("No such provider: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                                              provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return getInstance(transformation, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Returns a <code>Cipher</code> object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * <p> A new Cipher object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * CipherSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param transformation the name of the transformation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * e.g., <i>DES/CBC/PKCS5Padding</i>.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   566
     * See the Cipher section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   567
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   568
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * for information about standard transformation names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @return a cipher that implements the requested transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @exception NoSuchAlgorithmException if <code>transformation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *          is null, empty, in an invalid format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *          or if a CipherSpi implementation for the specified algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *          is not available from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @exception NoSuchPaddingException if <code>transformation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *          contains a padding scheme that is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    public static final Cipher getInstance(String transformation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                                           Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            throws NoSuchAlgorithmException, NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            throw new IllegalArgumentException("Missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        List transforms = getTransforms(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        boolean providerChecked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        String paddingError = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        for (Iterator t = transforms.iterator(); t.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            Transform tr = (Transform)t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            Service s = provider.getService("Cipher", tr.transform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            if (providerChecked == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                // for compatibility, first do the lookup and then verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                // the provider. this makes the difference between a NSAE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                // and a SecurityException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                // provider does not support the algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                Exception ve = JceSecurity.getVerificationResult(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                if (ve != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    String msg = "JCE cannot authenticate the provider "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        + provider.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    throw new SecurityException(msg, ve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                providerChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            if (tr.supportsMode(s) == S_NO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            if (tr.supportsPadding(s) == S_NO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                paddingError = tr.pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                CipherSpi spi = (CipherSpi)s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                tr.setModePadding(spi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                Cipher cipher = new Cipher(spi, transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                cipher.provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                cipher.initCryptoPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                return cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        // throw NoSuchPaddingException if the problem is with padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (failure instanceof NoSuchPaddingException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            throw (NoSuchPaddingException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if (paddingError != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            throw new NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                ("Padding not supported: " + paddingError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                ("No such algorithm: " + transformation, failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    // If the requested crypto service is export-controlled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    // determine the maximum allowable keysize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    private void initCryptoPermission() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if (JceSecurity.isRestricted() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            cryptoPerm = CryptoAllPermission.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            exmech = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        cryptoPerm = getConfiguredPermission(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        // Instantiate the exemption mechanism (if required)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        String exmechName = cryptoPerm.getExemptionMechanism();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        if (exmechName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            exmech = ExemptionMechanism.getInstance(exmechName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    // max number of debug warnings to print from chooseFirstProvider()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    private static int warnCount = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * Choose the Spi from the first provider available. Used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * delayed provider selection is not possible because init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * is not the first method called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    void chooseFirstProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                int w = --warnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                if (w >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    debug.println("Cipher.init() not first method "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        + "called, disabling delayed provider selection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    if (w == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        debug.println("Further warnings of this type will "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                            + "be suppressed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    new Exception("Call trace").printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                CipherSpi thisSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    thisSpi = firstSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    firstSpi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    s = (Service)serviceIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    thisSpi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                Transform tr = getTransform(s, transforms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                if (tr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                if (tr.supportsModePadding(s) == S_NO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    if (thisSpi == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        Object obj = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                        if (obj instanceof CipherSpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        thisSpi = (CipherSpi)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    tr.setModePadding(thisSpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    initCryptoPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    spi = thisSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    // not needed any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    transforms = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            ProviderException e = new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    ("Could not construct CipherSpi instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            if (lastException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                e.initCause(lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    private final static int I_KEY       = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    private final static int I_PARAMSPEC = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    private final static int I_PARAMS    = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    private final static int I_CERT      = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    private void implInit(CipherSpi thisSpi, int type, int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            AlgorithmParameterSpec paramSpec, AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            SecureRandom random) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        case I_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            checkCryptoPerm(thisSpi, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            thisSpi.engineInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        case I_PARAMSPEC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            checkCryptoPerm(thisSpi, key, paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            thisSpi.engineInit(opmode, key, paramSpec, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        case I_PARAMS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            checkCryptoPerm(thisSpi, key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            thisSpi.engineInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        case I_CERT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            checkCryptoPerm(thisSpi, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            thisSpi.engineInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            throw new AssertionError("Internal Cipher error: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    private void chooseProvider(int initType, int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            AlgorithmParameterSpec paramSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            AlgorithmParameters params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                implInit(spi, initType, opmode, key, paramSpec, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                CipherSpi thisSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    thisSpi = firstSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    firstSpi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    s = (Service)serviceIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    thisSpi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                // if provider says it does not support this key, ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                if (s.supportsParameter(key) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                Transform tr = getTransform(s, transforms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                if (tr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                if (tr.supportsModePadding(s) == S_NO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    if (thisSpi == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        thisSpi = (CipherSpi)s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    tr.setModePadding(thisSpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    initCryptoPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    implInit(thisSpi, initType, opmode, key, paramSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                                                        params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    this.spi = thisSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                    serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    transforms = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                    // NoSuchAlgorithmException from newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    // InvalidKeyException from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                    // RuntimeException (ProviderException) from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    // SecurityException from crypto permission check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    if (lastException == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            // no working provider found, fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            if (lastException instanceof InvalidKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                throw (InvalidKeyException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            if (lastException instanceof InvalidAlgorithmParameterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                throw (InvalidAlgorithmParameterException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            if (lastException instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                throw (RuntimeException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            String kName = (key != null) ? key.getClass().getName() : "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                ("No installed provider supports this key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                + kName, lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * Returns the provider of this <code>Cipher</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @return the provider of this <code>Cipher</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * Returns the algorithm name of this <code>Cipher</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * <p>This is the same name that was specified in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * <code>getInstance</code> calls that created this <code>Cipher</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * object..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * @return the algorithm name of this <code>Cipher</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        return this.transformation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Returns the block size (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @return the block size (in bytes), or 0 if the underlying algorithm is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * not a block cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    public final int getBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        return spi.engineGetBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Returns the length in bytes that an output buffer would need to be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * order to hold the result of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * <code>doFinal</code> operation, given the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * <code>inputLen</code> (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * <p>This call takes into account any unprocessed (buffered) data from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * previous <code>update</code> call, and padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * <p>The actual output length of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * <code>doFinal</code> call may be smaller than the length returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @param inputLen the input length (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @return the required output buffer size (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * (e.g., has not yet been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    public final int getOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        if (!initialized && !(this instanceof NullCipher)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            throw new IllegalStateException("Cipher not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        if (inputLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            throw new IllegalArgumentException("Input size must be equal " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                                               "to or greater than zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        return spi.engineGetOutputSize(inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * Returns the initialization vector (IV) in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * <p>This is useful in the case where a random IV was created,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * or in the context of password-based encryption or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * decryption, where the IV is derived from a user-supplied password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * @return the initialization vector in a new buffer, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * underlying algorithm does not use an IV, or if the IV has not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    public final byte[] getIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        return spi.engineGetIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * Returns the parameters used with this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * <p>The returned parameters may be the same that were used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * this cipher, or may contain a combination of default and random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * parameter values used by the underlying cipher implementation if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * cipher requires algorithm parameters but was not initialized with any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @return the parameters used with this cipher, or null if this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * does not use any parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    public final AlgorithmParameters getParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        return spi.engineGetParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * Returns the exemption mechanism object used with this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * @return the exemption mechanism object used with this cipher, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * null if this cipher does not use any exemption mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    public final ExemptionMechanism getExemptionMechanism() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        return exmech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    // Crypto permission check code below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    private void checkCryptoPerm(CipherSpi checkSpi, Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if (cryptoPerm == CryptoAllPermission.INSTANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        // Check if key size and default parameters are within legal limits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        AlgorithmParameterSpec params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            params = getAlgorithmParameterSpec(checkSpi.engineGetParameters());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                ("Unsupported default algorithm parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        if (!passCryptoPermCheck(checkSpi, key, params)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            throw new InvalidKeyException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                "Illegal key size or default parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    private void checkCryptoPerm(CipherSpi checkSpi, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            AlgorithmParameterSpec params) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (cryptoPerm == CryptoAllPermission.INSTANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        // Determine keysize and check if it is within legal limits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        if (!passCryptoPermCheck(checkSpi, key, null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            throw new InvalidKeyException("Illegal key size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        if ((params != null) && (!passCryptoPermCheck(checkSpi, key, params))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            throw new InvalidAlgorithmParameterException("Illegal parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    private void checkCryptoPerm(CipherSpi checkSpi, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            AlgorithmParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        if (cryptoPerm == CryptoAllPermission.INSTANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        // Convert the specified parameters into specs and then delegate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        AlgorithmParameterSpec pSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            pSpec = getAlgorithmParameterSpec(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                ("Failed to retrieve algorithm parameter specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        checkCryptoPerm(checkSpi, key, pSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    private boolean passCryptoPermCheck(CipherSpi checkSpi, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                                        AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        String em = cryptoPerm.getExemptionMechanism();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        int keySize = checkSpi.engineGetKeySize(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        // Use the "algorithm" component of the cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        // transformation so that the perm check would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        // work when the key has the "aliased" algo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        String algComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        int index = transformation.indexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            algComponent = transformation.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            algComponent = transformation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        CryptoPermission checkPerm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            new CryptoPermission(algComponent, keySize, params, em);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        if (!cryptoPerm.implies(checkPerm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                debug.println("Crypto Permission check failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                debug.println("granted: " + cryptoPerm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                debug.println("requesting: " + checkPerm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        if (exmech == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            if (!exmech.isCryptoAllowed(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                    debug.println(exmech.getName() + " isn't enforced");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        } catch (ExemptionMechanismException eme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                debug.println("Cannot determine whether "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                              exmech.getName() + " has been enforced");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                eme.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    // check if opmode is one of the defined constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    // throw InvalidParameterExeption if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    private static void checkOpmode(int opmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        if ((opmode < ENCRYPT_MODE) || (opmode > UNWRAP_MODE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            throw new InvalidParameterException("Invalid operation mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * Initializes this cipher with a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * encryption, decryption, key wrapping or key unwrapping, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * on the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * <p>If this cipher requires any algorithm parameters that cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * derived from the given <code>key</code>, the underlying cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * implementation is supposed to generate the required parameters itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * (using provider-specific default or random values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * initialized for encryption or key wrapping, and raise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * <code>InvalidKeyException</code> if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * initialized for decryption or key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * {@link #getParameters() getParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * {@link #getIV() getIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * them using the {@link SecureRandom <code>SecureRandom</code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * implementation of the highest-priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * installed provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * @param opmode the operation mode of this cipher (this is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * initializing this cipher, or if this cipher is being initialized for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * decryption and requires algorithm parameters that cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * determined from the given key, or if the given key has a keysize that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * exceeds the maximum allowable keysize (as determined from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * configured jurisdiction policy files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    public final void init(int opmode, Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        init(opmode, key, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * Initializes this cipher with a key and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * encryption, decryption, key wrapping or  key unwrapping, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * on the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * <p>If this cipher requires any algorithm parameters that cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * derived from the given <code>key</code>, the underlying cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * implementation is supposed to generate the required parameters itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * (using provider-specific default or random values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * initialized for encryption or key wrapping, and raise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * <code>InvalidKeyException</code> if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * initialized for decryption or key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * {@link #getParameters() getParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * {@link #getIV() getIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * @param opmode the operation mode of this cipher (this is one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * initializing this cipher, or if this cipher is being initialized for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * decryption and requires algorithm parameters that cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * determined from the given key, or if the given key has a keysize that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * exceeds the maximum allowable keysize (as determined from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * configured jurisdiction policy files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    public final void init(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        checkOpmode(opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            checkCryptoPerm(spi, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            spi.engineInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                chooseProvider(I_KEY, opmode, key, null, null, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                throw new InvalidKeyException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        this.opmode = opmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * Initializes this cipher with a key and a set of algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * encryption, decryption, key wrapping or  key unwrapping, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * on the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * <p>If this cipher requires any algorithm parameters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * <code>params</code> is null, the underlying cipher implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * supposed to generate the required parameters itself (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * provider-specific default or random values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * initialized for encryption or key wrapping, and raise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * <code>InvalidAlgorithmParameterException</code> if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * initialized for decryption or key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * {@link #getParameters() getParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * {@link #getIV() getIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * them using the {@link SecureRandom <code>SecureRandom</code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * implementation of the highest-priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * installed provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @param opmode the operation mode of this cipher (this is one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * initializing this cipher, or its keysize exceeds the maximum allowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * keysize (as determined from the configured jurisdiction policy files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * parameters are inappropriate for this cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * or this cipher is being initialized for decryption and requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * algorithm parameters and <code>params</code> is null, or the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * algorithm parameters imply a cryptographic strength that would exceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * the legal limits (as determined from the configured jurisdiction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * policy files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    public final void init(int opmode, Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            throws InvalidKeyException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        init(opmode, key, params, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * Initializes this cipher with a key, a set of algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * parameters, and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * encryption, decryption, key wrapping or  key unwrapping, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * on the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * <p>If this cipher requires any algorithm parameters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * <code>params</code> is null, the underlying cipher implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * supposed to generate the required parameters itself (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * provider-specific default or random values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * initialized for encryption or key wrapping, and raise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * <code>InvalidAlgorithmParameterException</code> if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * initialized for decryption or key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * {@link #getParameters() getParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * {@link #getIV() getIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * @param opmode the operation mode of this cipher (this is one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * initializing this cipher, or its keysize exceeds the maximum allowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * keysize (as determined from the configured jurisdiction policy files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * parameters are inappropriate for this cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * or this cipher is being initialized for decryption and requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * algorithm parameters and <code>params</code> is null, or the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * algorithm parameters imply a cryptographic strength that would exceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * the legal limits (as determined from the configured jurisdiction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * policy files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    public final void init(int opmode, Key key, AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                           SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            throws InvalidKeyException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        checkOpmode(opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            checkCryptoPerm(spi, key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            spi.engineInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            chooseProvider(I_PARAMSPEC, opmode, key, params, null, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        this.opmode = opmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * Initializes this cipher with a key and a set of algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * encryption, decryption, key wrapping or  key unwrapping, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * on the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * <p>If this cipher requires any algorithm parameters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * <code>params</code> is null, the underlying cipher implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * supposed to generate the required parameters itself (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * provider-specific default or random values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * initialized for encryption or key wrapping, and raise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * <code>InvalidAlgorithmParameterException</code> if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * initialized for decryption or key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * {@link #getParameters() getParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * {@link #getIV() getIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * them using the {@link SecureRandom <code>SecureRandom</code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * implementation of the highest-priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * installed provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * @param opmode the operation mode of this cipher (this is one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * following: <code>ENCRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * <code>DECRYPT_MODE</code>, <code>WRAP_MODE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * initializing this cipher, or its keysize exceeds the maximum allowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * keysize (as determined from the configured jurisdiction policy files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * parameters are inappropriate for this cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * or this cipher is being initialized for decryption and requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * algorithm parameters and <code>params</code> is null, or the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * algorithm parameters imply a cryptographic strength that would exceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * the legal limits (as determined from the configured jurisdiction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * policy files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    public final void init(int opmode, Key key, AlgorithmParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            throws InvalidKeyException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        init(opmode, key, params, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * Initializes this cipher with a key, a set of algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * parameters, and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * encryption, decryption, key wrapping or  key unwrapping, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * on the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * <p>If this cipher requires any algorithm parameters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * <code>params</code> is null, the underlying cipher implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * supposed to generate the required parameters itself (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * provider-specific default or random values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * initialized for encryption or key wrapping, and raise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * <code>InvalidAlgorithmParameterException</code> if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * initialized for decryption or key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * {@link #getParameters() getParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * {@link #getIV() getIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * @param opmode the operation mode of this cipher (this is one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * following: <code>ENCRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * <code>DECRYPT_MODE</code>, <code>WRAP_MODE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * initializing this cipher, or its keysize exceeds the maximum allowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * keysize (as determined from the configured jurisdiction policy files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * parameters are inappropriate for this cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * or this cipher is being initialized for decryption and requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * algorithm parameters and <code>params</code> is null, or the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * algorithm parameters imply a cryptographic strength that would exceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * the legal limits (as determined from the configured jurisdiction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * policy files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    public final void init(int opmode, Key key, AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                           SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            throws InvalidKeyException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        checkOpmode(opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            checkCryptoPerm(spi, key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            spi.engineInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            chooseProvider(I_PARAMS, opmode, key, null, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        this.opmode = opmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * Initializes this cipher with the public key from the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * <p> The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * encryption, decryption, key wrapping or  key unwrapping, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * on the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * <p>If the certificate is of type X.509 and has a <i>key usage</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * extension field marked as critical, and the value of the <i>key usage</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * extension field implies that the public key in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * the certificate and its corresponding private key are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * supposed to be used for the operation represented by the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * of <code>opmode</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * an <code>InvalidKeyException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * <p> If this cipher requires any algorithm parameters that cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * derived from the public key in the given certificate, the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * implementation is supposed to generate the required parameters itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * (using provider-specific default or ramdom values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * initialized for encryption or key wrapping, and raise an <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * InvalidKeyException</code> if it is being initialized for decryption or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * {@link #getParameters() getParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * {@link #getIV() getIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * them using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * <code>SecureRandom</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * implementation of the highest-priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * installed provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * @param opmode the operation mode of this cipher (this is one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * @param certificate the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * @exception InvalidKeyException if the public key in the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * certificate is inappropriate for initializing this cipher, or this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * cipher is being initialized for decryption or unwrapping keys and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * requires algorithm parameters that cannot be determined from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * public key in the given certificate, or the keysize of the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * in the given certificate has a keysize that exceeds the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * allowable keysize (as determined by the configured jurisdiction policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    public final void init(int opmode, Certificate certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        init(opmode, certificate, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * Initializes this cipher with the public key from the given certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * encryption, decryption, key wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * or key unwrapping, depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * <p>If the certificate is of type X.509 and has a <i>key usage</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * extension field marked as critical, and the value of the <i>key usage</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * extension field implies that the public key in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * the certificate and its corresponding private key are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * supposed to be used for the operation represented by the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * <code>opmode</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * an <code>InvalidKeyException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * <p>If this cipher requires any algorithm parameters that cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * derived from the public key in the given <code>certificate</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * the underlying cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * implementation is supposed to generate the required parameters itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * (using provider-specific default or random values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * initialized for encryption or key wrapping, and raise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * <code>InvalidKeyException</code> if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * initialized for decryption or key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * {@link #getParameters() getParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * {@link #getIV() getIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * @param opmode the operation mode of this cipher (this is one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * @param certificate the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * @exception InvalidKeyException if the public key in the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * certificate is inappropriate for initializing this cipher, or this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * cipher is being initialized for decryption or unwrapping keys and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * requires algorithm parameters that cannot be determined from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * public key in the given certificate, or the keysize of the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * in the given certificate has a keysize that exceeds the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * allowable keysize (as determined by the configured jurisdiction policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * files).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    public final void init(int opmode, Certificate certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                           SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        checkOpmode(opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        // Check key usage if the certificate is of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        // type X.509.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        if (certificate instanceof java.security.cert.X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            // Check whether the cert has a key usage extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            // marked as a critical extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            X509Certificate cert = (X509Certificate)certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            Set critSet = cert.getCriticalExtensionOIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            if (critSet != null && !critSet.isEmpty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                && critSet.contains(KEY_USAGE_EXTENSION_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                boolean[] keyUsageInfo = cert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                // keyUsageInfo[2] is for keyEncipherment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                // keyUsageInfo[3] is for dataEncipherment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                if ((keyUsageInfo != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                    (((opmode == Cipher.ENCRYPT_MODE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                      (keyUsageInfo.length > 3) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                      (keyUsageInfo[3] == false)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                     ((opmode == Cipher.WRAP_MODE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                      (keyUsageInfo.length > 2) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                      (keyUsageInfo[2] == false)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                    throw new InvalidKeyException("Wrong key usage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        PublicKey publicKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            (certificate==null? null:certificate.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            checkCryptoPerm(spi, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            spi.engineInit(opmode, publicKey, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                chooseProvider(I_CERT, opmode, publicKey, null, null, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                throw new InvalidKeyException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        this.opmode = opmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * Ensures that Cipher is in a valid state for update() and doFinal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * calls - should be initialized and in ENCRYPT_MODE or DECRYPT_MODE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * @throws IllegalStateException if Cipher object is not in valid state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    private void checkCipherState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        if (!(this instanceof NullCipher)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                throw new IllegalStateException("Cipher not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            if ((opmode != Cipher.ENCRYPT_MODE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                (opmode != Cipher.DECRYPT_MODE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                throw new IllegalStateException("Cipher not initialized " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                                                "for encryption/decryption");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * <p>The bytes in the <code>input</code> buffer are processed, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * <p>If <code>input</code> has a length of zero, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * @return the new buffer with the result, or null if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * cipher is a block cipher and the input data is too short to result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * new block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    public final byte[] update(byte[] input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        // Input sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        if (input == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            throw new IllegalArgumentException("Null input buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        if (input.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        return spi.engineUpdate(input, 0, input.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * buffer, starting at <code>inputOffset</code> inclusive, are processed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * and the result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * <p>If <code>inputLen</code> is zero, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * @return the new buffer with the result, or null if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * cipher is a block cipher and the input data is too short to result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * new block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    public final byte[] update(byte[] input, int inputOffset, int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        // Input sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        if (input == null || inputOffset < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            || inputLen > (input.length - inputOffset) || inputLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        if (inputLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        return spi.engineUpdate(input, inputOffset, inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * buffer, starting at <code>inputOffset</code> inclusive, are processed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * and the result is stored in the <code>output</code> buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * <p>If the <code>output</code> buffer is too small to hold the result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * a <code>ShortBufferException</code> is thrown. In this case, repeat this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * call with a larger output buffer. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * {@link #getOutputSize(int) getOutputSize} to determine how big
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * the output buffer should be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * <p>If <code>inputLen</code> is zero, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * a length of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * <p>Note: this method should be copy-safe, which means the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * <code>input</code> and <code>output</code> buffers can reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * the same byte array and no unprocessed input data is overwritten
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * when the result is copied into the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
    public final int update(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                            byte[] output)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        // Input sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        if (input == null || inputOffset < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            || inputLen > (input.length - inputOffset) || inputLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        if (inputLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        return spi.engineUpdate(input, inputOffset, inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                                      output, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * buffer, starting at <code>inputOffset</code> inclusive, are processed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * and the result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * <code>outputOffset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * <p>If the <code>output</code> buffer is too small to hold the result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * a <code>ShortBufferException</code> is thrown. In this case, repeat this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * call with a larger output buffer. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * {@link #getOutputSize(int) getOutputSize} to determine how big
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * the output buffer should be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * <p>If <code>inputLen</code> is zero, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * a length of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * <p>Note: this method should be copy-safe, which means the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * <code>input</code> and <code>output</code> buffers can reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * the same byte array and no unprocessed input data is overwritten
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * when the result is copied into the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    public final int update(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                            byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        // Input sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        if (input == null || inputOffset < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            || inputLen > (input.length - inputOffset) || inputLen < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            || outputOffset < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        if (inputLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        return spi.engineUpdate(input, inputOffset, inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                                      output, outputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * <p>All <code>input.remaining()</code> bytes starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * <code>input.position()</code> are processed. The result is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * in the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * Upon return, the input buffer's position will be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * to its limit; its limit will not have changed. The output buffer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * position will have advanced by n, where n is the value returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * by this method; the output buffer's limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * <p>If <code>output.remaining()</code> bytes are insufficient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * hold the result, a <code>ShortBufferException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * In this case, repeat this call with a larger output buffer. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * {@link #getOutputSize(int) getOutputSize} to determine how big
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * the output buffer should be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     * <p>Note: this method should be copy-safe, which means the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * <code>input</code> and <code>output</code> buffers can reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * the same block of memory and no unprocessed input data is overwritten
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     * when the result is copied into the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * @param input the input ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * @param output the output ByteByffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * @exception IllegalArgumentException if input and output are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     *   same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     * @exception ReadOnlyBufferException if the output buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     * @exception ShortBufferException if there is insufficient space in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    public final int update(ByteBuffer input, ByteBuffer output)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        if ((input == null) || (output == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
            throw new IllegalArgumentException("Buffers must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        if (input == output) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            throw new IllegalArgumentException("Input and output buffers must "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                + "not be the same object, consider using buffer.duplicate()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        if (output.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        return spi.engineUpdate(input, output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * Finishes a multiple-part encryption or decryption operation, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * on how this cipher was initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * <p>Input data that may have been buffered during a previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * <code>update</code> operation is processed, with padding (if requested)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * The result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * <p>Upon finishing, this method resets this cipher object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * it was in when previously initialized via a call to <code>init</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * That is, the object is reset and available to encrypt or decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * (depending on the operation mode that was specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * <code>init</code>) more data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * <p>Note: if any exception is thrown, this cipher object may need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * be reset before it can be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * @return the new buffer with the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * block size; or if this encryption algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * bounded by the appropriate padding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    public final byte[] doFinal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        return spi.engineDoFinal(null, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * Finishes a multiple-part encryption or decryption operation, depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * on how this cipher was initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * <p>Input data that may have been buffered during a previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * <code>update</code> operation is processed, with padding (if requested)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * The result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * <code>outputOffset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * <p>If the <code>output</code> buffer is too small to hold the result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * a <code>ShortBufferException</code> is thrown. In this case, repeat this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * call with a larger output buffer. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * {@link #getOutputSize(int) getOutputSize} to determine how big
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * the output buffer should be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * <p>Upon finishing, this method resets this cipher object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * it was in when previously initialized via a call to <code>init</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * That is, the object is reset and available to encrypt or decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     * (depending on the operation mode that was specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * <code>init</code>) more data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * <p>Note: if any exception is thrown, this cipher object may need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     * be reset before it can be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * block size; or if this encryption algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * bounded by the appropriate padding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    public final int doFinal(byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            throws IllegalBlockSizeException, ShortBufferException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
               BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        // Input sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        if ((output == null) || (outputOffset < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        return spi.engineDoFinal(null, 0, 0, output, outputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * Encrypts or decrypts data in a single-part operation, or finishes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * multiple-part operation. The data is encrypted or decrypted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * depending on how this cipher was initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     * <p>The bytes in the <code>input</code> buffer, and any input bytes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * may have been buffered during a previous <code>update</code> operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * are processed, with padding (if requested) being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * The result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     * <p>Upon finishing, this method resets this cipher object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     * it was in when previously initialized via a call to <code>init</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     * That is, the object is reset and available to encrypt or decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     * (depending on the operation mode that was specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * <code>init</code>) more data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * <p>Note: if any exception is thrown, this cipher object may need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     * be reset before it can be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * @return the new buffer with the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * block size; or if this encryption algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     * bounded by the appropriate padding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    public final byte[] doFinal(byte[] input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        // Input sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        if (input == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            throw new IllegalArgumentException("Null input buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        return spi.engineDoFinal(input, 0, input.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * Encrypts or decrypts data in a single-part operation, or finishes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * multiple-part operation. The data is encrypted or decrypted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     * depending on how this cipher was initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * buffer, starting at <code>inputOffset</code> inclusive, and any input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * bytes that may have been buffered during a previous <code>update</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * operation, are processed, with padding (if requested) being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * The result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * <p>Upon finishing, this method resets this cipher object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * it was in when previously initialized via a call to <code>init</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * That is, the object is reset and available to encrypt or decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * (depending on the operation mode that was specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * <code>init</code>) more data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     * <p>Note: if any exception is thrown, this cipher object may need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * be reset before it can be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * @return the new buffer with the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * block size; or if this encryption algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * bounded by the appropriate padding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
    public final byte[] doFinal(byte[] input, int inputOffset, int inputLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        // Input sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
        if (input == null || inputOffset < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
            || inputLen > (input.length - inputOffset) || inputLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        return spi.engineDoFinal(input, inputOffset, inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * Encrypts or decrypts data in a single-part operation, or finishes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * multiple-part operation. The data is encrypted or decrypted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * depending on how this cipher was initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * buffer, starting at <code>inputOffset</code> inclusive, and any input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * bytes that may have been buffered during a previous <code>update</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * operation, are processed, with padding (if requested) being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * The result is stored in the <code>output</code> buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * <p>If the <code>output</code> buffer is too small to hold the result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * a <code>ShortBufferException</code> is thrown. In this case, repeat this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * call with a larger output buffer. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * {@link #getOutputSize(int) getOutputSize} to determine how big
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * the output buffer should be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * <p>Upon finishing, this method resets this cipher object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     * it was in when previously initialized via a call to <code>init</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * That is, the object is reset and available to encrypt or decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     * (depending on the operation mode that was specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * <code>init</code>) more data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * <p>Note: if any exception is thrown, this cipher object may need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * be reset before it can be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * <p>Note: this method should be copy-safe, which means the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     * <code>input</code> and <code>output</code> buffers can reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     * the same byte array and no unprocessed input data is overwritten
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     * when the result is copied into the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * block size; or if this encryption algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * bounded by the appropriate padding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
    public final int doFinal(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                             byte[] output)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        // Input sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        if (input == null || inputOffset < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            || inputLen > (input.length - inputOffset) || inputLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        return spi.engineDoFinal(input, inputOffset, inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                                       output, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * Encrypts or decrypts data in a single-part operation, or finishes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * multiple-part operation. The data is encrypted or decrypted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * depending on how this cipher was initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * buffer, starting at <code>inputOffset</code> inclusive, and any input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     * bytes that may have been buffered during a previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     * <code>update</code> operation, are processed, with padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * (if requested) being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * The result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * <code>outputOffset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * <p>If the <code>output</code> buffer is too small to hold the result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     * a <code>ShortBufferException</code> is thrown. In this case, repeat this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * call with a larger output buffer. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * {@link #getOutputSize(int) getOutputSize} to determine how big
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * the output buffer should be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * <p>Upon finishing, this method resets this cipher object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     * it was in when previously initialized via a call to <code>init</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * That is, the object is reset and available to encrypt or decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * (depending on the operation mode that was specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * <code>init</code>) more data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * <p>Note: if any exception is thrown, this cipher object may need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * be reset before it can be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * <p>Note: this method should be copy-safe, which means the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     * <code>input</code> and <code>output</code> buffers can reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * the same byte array and no unprocessed input data is overwritten
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * when the result is copied into the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     * block size; or if this encryption algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     * bounded by the appropriate padding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
    public final int doFinal(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                             byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
            throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
            BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
        // Input sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        if (input == null || inputOffset < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
            || inputLen > (input.length - inputOffset) || inputLen < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
            || outputOffset < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
            throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        return spi.engineDoFinal(input, inputOffset, inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                                       output, outputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     * Encrypts or decrypts data in a single-part operation, or finishes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * multiple-part operation. The data is encrypted or decrypted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * depending on how this cipher was initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     * <p>All <code>input.remaining()</code> bytes starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * <code>input.position()</code> are processed. The result is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     * in the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * Upon return, the input buffer's position will be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     * to its limit; its limit will not have changed. The output buffer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * position will have advanced by n, where n is the value returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * by this method; the output buffer's limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     * <p>If <code>output.remaining()</code> bytes are insufficient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * hold the result, a <code>ShortBufferException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     * In this case, repeat this call with a larger output buffer. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     * {@link #getOutputSize(int) getOutputSize} to determine how big
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * the output buffer should be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * <p>Upon finishing, this method resets this cipher object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * it was in when previously initialized via a call to <code>init</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * That is, the object is reset and available to encrypt or decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     * (depending on the operation mode that was specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     * <code>init</code>) more data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     * <p>Note: if any exception is thrown, this cipher object may need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     * be reset before it can be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
     * <p>Note: this method should be copy-safe, which means the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     * <code>input</code> and <code>output</code> buffers can reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     * the same byte array and no unprocessed input data is overwritten
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     * when the result is copied into the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     * @param input the input ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     * @param output the output ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     * @exception IllegalArgumentException if input and output are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     *   same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     * @exception ReadOnlyBufferException if the output buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * block size; or if this encryption algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * @exception ShortBufferException if there is insufficient space in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     * output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * bounded by the appropriate padding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
    public final int doFinal(ByteBuffer input, ByteBuffer output)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        checkCipherState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
        if ((input == null) || (output == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
            throw new IllegalArgumentException("Buffers must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        if (input == output) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            throw new IllegalArgumentException("Input and output buffers must "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                + "not be the same object, consider using buffer.duplicate()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
        if (output.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        return spi.engineDoFinal(input, output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     * Wrap a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
     * @param key the key to be wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * @return the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     * @exception IllegalStateException if this cipher is in a wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     * state (e.g., has not been initialized).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     * @exception IllegalBlockSizeException if this cipher is a block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     * cipher, no padding has been requested, and the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     * encoding of the key to be wrapped is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     * multiple of the block size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * @exception InvalidKeyException if it is impossible or unsafe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     * wrap the key with this cipher (e.g., a hardware protected key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
     * being passed to a software-only cipher).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
    public final byte[] wrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        if (!(this instanceof NullCipher)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                throw new IllegalStateException("Cipher not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            if (opmode != Cipher.WRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                throw new IllegalStateException("Cipher not initialized " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                                                "for wrapping keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        return spi.engineWrap(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     * Unwrap a previously wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * @param wrappedKey the key to be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * @param wrappedKeyAlgorithm the algorithm associated with the wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     * @param wrappedKeyType the type of the wrapped key. This must be one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     * <code>SECRET_KEY</code>, <code>PRIVATE_KEY</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     * <code>PUBLIC_KEY</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * @return the unwrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     * (e.g., has not been initialized).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
     * @exception NoSuchAlgorithmException if no installed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
     * can create keys of type <code>wrappedKeyType</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     * <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * @exception InvalidKeyException if <code>wrappedKey</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * represent a wrapped key of type <code>wrappedKeyType</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * the <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
    public final Key unwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                            String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                            int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        if (!(this instanceof NullCipher)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
            if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                throw new IllegalStateException("Cipher not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            if (opmode != Cipher.UNWRAP_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                throw new IllegalStateException("Cipher not initialized " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                                                "for unwrapping keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        if ((wrappedKeyType != SECRET_KEY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            (wrappedKeyType != PRIVATE_KEY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            (wrappedKeyType != PUBLIC_KEY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            throw new InvalidParameterException("Invalid key type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        return spi.engineUnwrap(wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                                      wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                                      wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
    private AlgorithmParameterSpec getAlgorithmParameterSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                                      AlgorithmParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            throws InvalidParameterSpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
        String alg = params.getAlgorithm().toUpperCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        if (alg.equalsIgnoreCase("RC2")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            return params.getParameterSpec(RC2ParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        if (alg.equalsIgnoreCase("RC5")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            return params.getParameterSpec(RC5ParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        if (alg.startsWith("PBE")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            return params.getParameterSpec(PBEParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        if (alg.startsWith("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            return params.getParameterSpec(IvParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
    private static CryptoPermission getConfiguredPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            String transformation) throws NullPointerException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        if (transformation == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
        String[] parts = tokenizeTransformation(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        return JceSecurityManager.INSTANCE.getCryptoPermission(parts[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * Returns the maximum key length for the specified transformation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * according to the installed JCE jurisdiction policy files. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * JCE unlimited strength jurisdiction policy files are installed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * Integer.MAX_VALUE will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * For more information on default key size in JCE jurisdiction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * policy files, please see Appendix E in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     * <a href=
1692
92165aa9df3e 6778613: Update javax.crypto.Cipher.getMaxAllowedKeyLength to point to proper Appendix after doc reorg
wetmore
parents: 2
diff changeset
  2404
     *   "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppC">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * Java Cryptography Architecture Reference Guide</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     * @param transformation the cipher transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     * @return the maximum key length in bits or Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * @exception NullPointerException if <code>transformation</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * @exception NoSuchAlgorithmException if <code>transformation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     * is not a valid transformation, i.e. in the form of "algorithm" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     * "algorithm/mode/padding".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
    public static final int getMaxAllowedKeyLength(String transformation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        CryptoPermission cp = getConfiguredPermission(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
        return cp.getMaxKeySize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * Returns an AlgorithmParameterSpec object which contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * the maximum cipher parameter value according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     * jurisdiction policy file. If JCE unlimited strength jurisdiction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * policy files are installed or there is no maximum limit on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     * parameters for the specified transformation in the policy file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     * null will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * @param transformation the cipher transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * @return an AlgorithmParameterSpec which holds the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * value or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * @exception NullPointerException if <code>transformation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * @exception NoSuchAlgorithmException if <code>transformation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     * is not a valid transformation, i.e. in the form of "algorithm" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * "algorithm/mode/padding".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
    public static final AlgorithmParameterSpec getMaxAllowedParameterSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            String transformation) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        CryptoPermission cp = getConfiguredPermission(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        return cp.getAlgorithmParameterSpec();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
}