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