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