jdk/src/share/classes/javax/crypto/CipherSpi.java
author wetmore
Wed, 13 Apr 2011 11:59:18 -0700
changeset 9265 62d885310f4d
parent 5506 202f599c92aa
child 11837 6a7fa5f263ce
permissions -rw-r--r--
7031343: Provide API changes to support future GCM AEAD ciphers Reviewed-by: valeriep, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AlgorithmParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.ProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * for the <code>Cipher</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * All the abstract methods in this class must be implemented by each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * cryptographic service provider who wishes to supply the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * of a particular cipher algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>In order to create an instance of <code>Cipher</code>, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * encapsulates an instance of this <code>CipherSpi</code> class, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * application calls one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link Cipher#getInstance(java.lang.String) getInstance}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * factory methods of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@link Cipher Cipher} engine class and specifies the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <i>transformation</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Optionally, the application may also specify the name of a provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>A <i>transformation</i> is a string that describes the operation (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * set of operations) to be performed on the given input, to produce some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * output. A transformation always includes the name of a cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * algorithm (e.g., <i>DES</i>), and may be followed by a feedback mode and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * padding scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p> A transformation is of the form:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <li>"<i>algorithm/mode/padding</i>" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <li>"<i>algorithm</i>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <P> (in the latter case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * provider-specific default values for the mode and padding scheme are used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * For example, the following is a valid transformation:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     Cipher c = Cipher.getInstance("<i>DES/CBC/PKCS5Padding</i>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>A provider may supply a separate class for each combination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * of <i>algorithm/mode/padding</i>, or may decide to provide more generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * classes representing sub-transformations corresponding to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <i>algorithm</i> or <i>algorithm/mode</i> or <i>algorithm//padding</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * (note the double slashes),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * in which case the requested mode and/or padding are set automatically by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * the <code>getInstance</code> methods of <code>Cipher</code>, which invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * the {@link #engineSetMode(java.lang.String) engineSetMode} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * {@link #engineSetPadding(java.lang.String) engineSetPadding}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * methods of the provider's subclass of <code>CipherSpi</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p>A <code>Cipher</code> property in a provider master class may have one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * the following formats:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *     // provider's subclass of "CipherSpi" implements "algName" with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *     // pluggable mode and padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *     <code>Cipher.</code><i>algName</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *     // provider's subclass of "CipherSpi" implements "algName" in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *     // specified "mode", with pluggable padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *     <code>Cipher.</code><i>algName/mode</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *     // provider's subclass of "CipherSpi" implements "algName" with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *     // specified "padding", with pluggable mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *     <code>Cipher.</code><i>algName//padding</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *     // provider's subclass of "CipherSpi" implements "algName" with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *     // specified "mode" and "padding"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *     <code>Cipher.</code><i>algName/mode/padding</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <p>For example, a provider may supply a subclass of <code>CipherSpi</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * that implements <i>DES/ECB/PKCS5Padding</i>, one that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <i>DES/CBC/PKCS5Padding</i>, one that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <i>DES/CFB/PKCS5Padding</i>, and yet another one that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <i>DES/OFB/PKCS5Padding</i>. That provider would have the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <code>Cipher</code> properties in its master class:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *     <code>Cipher.</code><i>DES/ECB/PKCS5Padding</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *     <code>Cipher.</code><i>DES/CBC/PKCS5Padding</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *     <code>Cipher.</code><i>DES/CFB/PKCS5Padding</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *     <code>Cipher.</code><i>DES/OFB/PKCS5Padding</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * <p>Another provider may implement a class for each of the above modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * (i.e., one class for <i>ECB</i>, one for <i>CBC</i>, one for <i>CFB</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * and one for <i>OFB</i>), one class for <i>PKCS5Padding</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * and a generic <i>DES</i> class that subclasses from <code>CipherSpi</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * That provider would have the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <code>Cipher</code> properties in its master class:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *     <code>Cipher.</code><i>DES</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * <p>The <code>getInstance</code> factory method of the <code>Cipher</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * engine class follows these rules in order to instantiate a provider's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * implementation of <code>CipherSpi</code> for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * transformation of the form "<i>algorithm</i>":
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * Check if the provider has registered a subclass of <code>CipherSpi</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * for the specified "<i>algorithm</i>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * <p>If the answer is YES, instantiate this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * class, for whose mode and padding scheme default values (as supplied by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * the provider) are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * <p>If the answer is NO, throw a <code>NoSuchAlgorithmException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * <p>The <code>getInstance</code> factory method of the <code>Cipher</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * engine class follows these rules in order to instantiate a provider's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * implementation of <code>CipherSpi</code> for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * transformation of the form "<i>algorithm/mode/padding</i>":
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * Check if the provider has registered a subclass of <code>CipherSpi</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * for the specified "<i>algorithm/mode/padding</i>" transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * <p>If the answer is YES, instantiate it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * <p>If the answer is NO, go to the next step.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * Check if the provider has registered a subclass of <code>CipherSpi</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * for the sub-transformation "<i>algorithm/mode</i>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * <p>If the answer is YES, instantiate it, and call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * <code>engineSetPadding(<i>padding</i>)</code> on the new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * <p>If the answer is NO, go to the next step.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * Check if the provider has registered a subclass of <code>CipherSpi</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * for the sub-transformation "<i>algorithm//padding</i>" (note the double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * slashes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * <p>If the answer is YES, instantiate it, and call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * <code>engineSetMode(<i>mode</i>)</code> on the new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * <p>If the answer is NO, go to the next step.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * Check if the provider has registered a subclass of <code>CipherSpi</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * for the sub-transformation "<i>algorithm</i>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * <p>If the answer is YES, instantiate it, and call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * <code>engineSetMode(<i>mode</i>)</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * <code>engineSetPadding(<i>padding</i>)</code> on the new instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * <p>If the answer is NO, throw a <code>NoSuchAlgorithmException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * @see KeyGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * @see SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
public abstract class CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Sets the mode of this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param mode the cipher mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @exception NoSuchAlgorithmException if the requested cipher mode does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    protected abstract void engineSetMode(String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        throws NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Sets the padding mechanism of this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param padding the padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @exception NoSuchPaddingException if the requested padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    protected abstract void engineSetPadding(String padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        throws NoSuchPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns the block size (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return the block size (in bytes), or 0 if the underlying algorithm is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * not a block cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    protected abstract int engineGetBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Returns the length in bytes that an output buffer would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * need to be in order to hold the result of the next <code>update</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * or <code>doFinal</code> operation, given the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <code>inputLen</code> (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <p>This call takes into account any unprocessed (buffered) data from a
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   269
     * previous <code>update</code> call, padding, and AEAD tagging.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <p>The actual output length of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <code>doFinal</code> call may be smaller than the length returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param inputLen the input length (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return the required output buffer size (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    protected abstract int engineGetOutputSize(int inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Returns the initialization vector (IV) in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <p> This is useful in the context of password-based encryption or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * decryption, where the IV is derived from a user-provided passphrase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @return the initialization vector in a new buffer, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * underlying algorithm does not use an IV, or if the IV has not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    protected abstract byte[] engineGetIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Returns the parameters used with this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <p>The returned parameters may be the same that were used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * this cipher, or may contain a combination of default and random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * parameter values used by the underlying cipher implementation if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * cipher requires algorithm parameters but was not initialized with any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @return the parameters used with this cipher, or null if this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * does not use any parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    protected abstract AlgorithmParameters engineGetParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Initializes this cipher with a key and a source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * encryption, decryption, key wrapping or key unwrapping, depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <p>If this cipher requires any algorithm parameters that cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * derived from the given <code>key</code>, the underlying cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * implementation is supposed to generate the required parameters itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * (using provider-specific default or random values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * initialized for encryption or key wrapping, and raise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <code>InvalidKeyException</code> if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * initialized for decryption or key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * {@link #engineGetParameters() engineGetParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * {@link #engineGetIV() engineGetIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   325
     * <p>If this cipher requires algorithm parameters that cannot be
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   326
     * derived from the input parameters, and there are no reasonable
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   327
     * provider-specific default values, initialization will
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   328
     * necessarily fail.
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   329
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param opmode the operation mode of this cipher (this is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @exception InvalidKeyException if the given key is inappropriate for
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   347
     * initializing this cipher, or requires
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   348
     * algorithm parameters that cannot be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * determined from the given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    protected abstract void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                       SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        throws InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Initializes this cipher with a key, a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * algorithm parameters, and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * encryption, decryption, key wrapping or key unwrapping, depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <p>If this cipher requires any algorithm parameters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <code>params</code> is null, the underlying cipher implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * supposed to generate the required parameters itself (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * provider-specific default or random values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * initialized for encryption or key wrapping, and raise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <code>InvalidAlgorithmParameterException</code> if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * initialized for decryption or key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * {@link #engineGetParameters() engineGetParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * {@link #engineGetIV() engineGetIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   374
     * <p>If this cipher requires algorithm parameters that cannot be
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   375
     * derived from the input parameters, and there are no reasonable
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   376
     * provider-specific default values, initialization will
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   377
     * necessarily fail.
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   378
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @param opmode the operation mode of this cipher (this is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * parameters are inappropriate for this cipher,
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   400
     * or if this cipher requires
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * algorithm parameters and <code>params</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    protected abstract void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                       AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                       SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        throws InvalidKeyException, InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Initializes this cipher with a key, a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * algorithm parameters, and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * encryption, decryption, key wrapping or key unwrapping, depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <p>If this cipher requires any algorithm parameters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <code>params</code> is null, the underlying cipher implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * supposed to generate the required parameters itself (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * provider-specific default or random values) if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * initialized for encryption or key wrapping, and raise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * <code>InvalidAlgorithmParameterException</code> if it is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * initialized for decryption or key unwrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * The generated parameters can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * {@link #engineGetParameters() engineGetParameters} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * {@link #engineGetIV() engineGetIV} (if the parameter is an IV).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   427
     * <p>If this cipher requires algorithm parameters that cannot be
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   428
     * derived from the input parameters, and there are no reasonable
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   429
     * provider-specific default values, initialization will
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   430
     * necessarily fail.
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   431
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * requires any random bytes (e.g., for parameter generation), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <p>Note that when a Cipher object is initialized, it loses all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * previously-acquired state. In other words, initializing a Cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * equivalent to creating a new instance of that Cipher and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param opmode the operation mode of this cipher (this is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * parameters are inappropriate for this cipher,
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   453
     * or if this cipher requires
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * algorithm parameters and <code>params</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    protected abstract void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                       AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                       SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        throws InvalidKeyException, InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * buffer, starting at <code>inputOffset</code> inclusive, are processed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * and the result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @return the new buffer with the result, or null if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * cipher is a block cipher and the input data is too short to result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * new block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    protected abstract byte[] engineUpdate(byte[] input, int inputOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                           int inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * buffer, starting at <code>inputOffset</code> inclusive, are processed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * and the result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * <code>outputOffset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * <p>If the <code>output</code> buffer is too small to hold the result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * a <code>ShortBufferException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    protected abstract int engineUpdate(byte[] input, int inputOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                                        int inputLen, byte[] output,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                        int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        throws ShortBufferException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * <p>All <code>input.remaining()</code> bytes starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * <code>input.position()</code> are processed. The result is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * in the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Upon return, the input buffer's position will be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * to its limit; its limit will not have changed. The output buffer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * position will have advanced by n, where n is the value returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * by this method; the output buffer's limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * <p>If <code>output.remaining()</code> bytes are insufficient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * hold the result, a <code>ShortBufferException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * <p>Subclasses should consider overriding this method if they can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * process ByteBuffers more efficiently than byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @param input the input ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @param output the output ByteByffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @exception ShortBufferException if there is insufficient space in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @throws NullPointerException if either parameter is <CODE>null</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    protected int engineUpdate(ByteBuffer input, ByteBuffer output)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            return bufferCrypt(input, output, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        } catch (IllegalBlockSizeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            // never thrown for engineUpdate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            throw new ProviderException("Internal error in update()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            // never thrown for engineUpdate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            throw new ProviderException("Internal error in update()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Encrypts or decrypts data in a single-part operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * or finishes a multiple-part operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * The data is encrypted or decrypted, depending on how this cipher was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * buffer, starting at <code>inputOffset</code> inclusive, and any input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * bytes that may have been buffered during a previous <code>update</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * operation, are processed, with padding (if requested) being applied.
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   566
     * 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: 5506
diff changeset
   567
     * 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: 5506
diff changeset
   568
     * case of decryption.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * The result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * <p>Upon finishing, this method resets this cipher object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <code>engineInit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * That is, the object is reset and available to encrypt or decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * (depending on the operation mode that was specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <code>engineInit</code>) more data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <p>Note: if any exception is thrown, this cipher object may need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * be reset before it can be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @return the new buffer with the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * block size; or if this encryption algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * bounded by the appropriate padding bytes
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   596
     * @exception AEADBadTagException if this cipher is decrypting in an
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   597
     * AEAD mode (such as GCM/CCM), and the received authentication tag
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   598
     * does not match the calculated value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    protected abstract byte[] engineDoFinal(byte[] input, int inputOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                            int inputLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        throws IllegalBlockSizeException, BadPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * Encrypts or decrypts data in a single-part operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * or finishes a multiple-part operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * The data is encrypted or decrypted, depending on how this cipher was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * buffer, starting at <code>inputOffset</code> inclusive, and any input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * bytes that may have been buffered during a previous <code>update</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * operation, are processed, with padding (if requested) being applied.
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   614
     * 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: 5506
diff changeset
   615
     * 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: 5506
diff changeset
   616
     * case of decryption.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * The result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * <code>outputOffset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * <p>If the <code>output</code> buffer is too small to hold the result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * a <code>ShortBufferException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * <p>Upon finishing, this method resets this cipher object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <code>engineInit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * That is, the object is reset and available to encrypt or decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * (depending on the operation mode that was specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <code>engineInit</code>) more data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <p>Note: if any exception is thrown, this cipher object may need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * be reset before it can be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * block size; or if this encryption algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * bounded by the appropriate padding bytes
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   653
     * @exception AEADBadTagException if this cipher is decrypting in an
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   654
     * AEAD mode (such as GCM/CCM), and the received authentication tag
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   655
     * does not match the calculated value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    protected abstract int engineDoFinal(byte[] input, int inputOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                         int inputLen, byte[] output,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                         int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
               BadPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * Encrypts or decrypts data in a single-part operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * or finishes a multiple-part operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * The data is encrypted or decrypted, depending on how this cipher was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * <p>All <code>input.remaining()</code> bytes starting at
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   670
     * <code>input.position()</code> are processed.
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   671
     * 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: 5506
diff changeset
   672
     * 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: 5506
diff changeset
   673
     * case of decryption.
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   674
     * The result is stored in the output buffer.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Upon return, the input buffer's position will be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * to its limit; its limit will not have changed. The output buffer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * position will have advanced by n, where n is the value returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * by this method; the output buffer's limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <p>If <code>output.remaining()</code> bytes are insufficient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * hold the result, a <code>ShortBufferException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <p>Upon finishing, this method resets this cipher object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <code>engineInit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * That is, the object is reset and available to encrypt or decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * (depending on the operation mode that was specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * <code>engineInit</code>) more data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <p>Note: if any exception is thrown, this cipher object may need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * be reset before it can be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * <p>Subclasses should consider overriding this method if they can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * process ByteBuffers more efficiently than byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @param input the input ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @param output the output ByteByffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * block size; or if this encryption algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @exception ShortBufferException if there is insufficient space in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * bounded by the appropriate padding bytes
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   711
     * @exception AEADBadTagException if this cipher is decrypting in an
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   712
     * AEAD mode (such as GCM/CCM), and the received authentication tag
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   713
     * does not match the calculated value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @throws NullPointerException if either parameter is <CODE>null</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    protected int engineDoFinal(ByteBuffer input, ByteBuffer output)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        return bufferCrypt(input, output, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    // copied from sun.security.jca.JCAUtil
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    // will be changed to reference that method once that code has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    // integrated and promoted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    static int getTempArraySize(int totalSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        return Math.min(4096, totalSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * Implementation for encryption using ByteBuffers. Used for both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * engineUpdate() and engineDoFinal().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    private int bufferCrypt(ByteBuffer input, ByteBuffer output,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            boolean isUpdate) throws ShortBufferException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if ((input == null) || (output == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            throw new NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                ("Input and output buffers must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        int inPos = input.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        int inLimit = input.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        int inLen = inLimit - inPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (isUpdate && (inLen == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        int outLenNeeded = engineGetOutputSize(inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if (output.remaining() < outLenNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            throw new ShortBufferException("Need at least " + outLenNeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                + " bytes of space in output buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        boolean a1 = input.hasArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        boolean a2 = output.hasArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if (a1 && a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            byte[] inArray = input.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            int inOfs = input.arrayOffset() + inPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            byte[] outArray = output.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            int outPos = output.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            int outOfs = output.arrayOffset() + outPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (isUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                n = engineUpdate(inArray, inOfs, inLen, outArray, outOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                n = engineDoFinal(inArray, inOfs, inLen, outArray, outOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            input.position(inLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            output.position(outPos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        } else if (!a1 && a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            int outPos = output.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            byte[] outArray = output.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            int outOfs = output.arrayOffset() + outPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            byte[] inArray = new byte[getTempArraySize(inLen)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            while (inLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                int chunk = Math.min(inLen, inArray.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                input.get(inArray, 0, chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                if (isUpdate || (inLen != chunk)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    n = engineUpdate(inArray, 0, chunk, outArray, outOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    n = engineDoFinal(inArray, 0, chunk, outArray, outOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                total += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                outOfs += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                inLen -= chunk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            output.position(outPos + total);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        } else { // output is not backed by an accessible byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            byte[] inArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            int inOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            if (a1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                inArray = input.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                inOfs = input.arrayOffset() + inPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                inArray = new byte[getTempArraySize(inLen)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                inOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            byte[] outArray = new byte[getTempArraySize(outLenNeeded)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            int outSize = outArray.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            boolean resized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            while (inLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                int chunk = Math.min(inLen, outSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                if ((a1 == false) && (resized == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    input.get(inArray, 0, chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    inOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    if (isUpdate || (inLen != chunk)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                        n = engineUpdate(inArray, inOfs, chunk, outArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        n = engineDoFinal(inArray, inOfs, chunk, outArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    resized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    inOfs += chunk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    inLen -= chunk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    output.put(outArray, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    total += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                    if (resized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                        // we just resized the output buffer, but it still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                        // did not work. Bug in the provider, abort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                        throw (ProviderException)new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                            ("Could not determine buffer size").initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                    // output buffer is too small, realloc and try again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    resized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    int newOut = engineGetOutputSize(chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    outArray = new byte[newOut];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            input.position(inLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * Wrap a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * <p>This concrete method has been added to this previously-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * abstract class. (For backwards compatibility, it cannot be abstract.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * It may be overridden by a provider to wrap a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Such an override is expected to throw an IllegalBlockSizeException or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * InvalidKeyException (under the specified circumstances),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * if the given key cannot be wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * If this method is not overridden, it always throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @param key the key to be wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @return the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * no padding has been requested, and the length of the encoding of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * key to be wrapped is not a multiple of the block size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @exception InvalidKeyException if it is impossible or unsafe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * wrap the key with this cipher (e.g., a hardware protected key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * being passed to a software-only cipher).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    protected byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        throws IllegalBlockSizeException, InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Unwrap a previously wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * <p>This concrete method has been added to this previously-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * abstract class. (For backwards compatibility, it cannot be abstract.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * It may be overridden by a provider to unwrap a previously wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * Such an override is expected to throw an InvalidKeyException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * the given wrapped key cannot be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * If this method is not overridden, it always throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @param wrappedKey the key to be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @param wrappedKeyAlgorithm the algorithm associated with the wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @param wrappedKeyType the type of the wrapped key. This is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * <code>SECRET_KEY</code>, <code>PRIVATE_KEY</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * <code>PUBLIC_KEY</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @return the unwrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @exception NoSuchAlgorithmException if no installed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * can create keys of type <code>wrappedKeyType</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @exception InvalidKeyException if <code>wrappedKey</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * represent a wrapped key of type <code>wrappedKeyType</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * the <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    protected Key engineUnwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                               String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                               int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        throws InvalidKeyException, NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * Returns the key size of the given key object in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * <p>This concrete method has been added to this previously-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * abstract class. It throws an <code>UnsupportedOperationException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * if it is not overridden by the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @param key the key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @return the key size of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @exception InvalidKeyException if <code>key</code> is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    protected int engineGetKeySize(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
9265
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   928
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   929
    /**
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   930
     * Continues a multi-part update of the Additional Authentication
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   931
     * Data (AAD), using a subset of the provided buffer.
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   932
     * <p>
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   933
     * 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: 5506
diff changeset
   934
     * 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: 5506
diff changeset
   935
     * either GCM or CCM mode, all AAD must be supplied before beginning
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   936
     * operations on the ciphertext (via the {@code update} and {@code
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   937
     * doFinal} methods).
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   938
     *
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   939
     * @param src the buffer containing the AAD
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   940
     * @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: 5506
diff changeset
   941
     * @param len the number of AAD bytes
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   942
     *
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   943
     * @throws IllegalStateException if this cipher is in a wrong state
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   944
     * (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: 5506
diff changeset
   945
     * 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: 5506
diff changeset
   946
     * methods has already been called for the active
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   947
     * encryption/decryption operation
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   948
     * @throws UnsupportedOperationException if this method
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   949
     * has not been overridden by an implementation
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   950
     *
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   951
     * @since 1.7
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   952
     */
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   953
    protected void engineUpdateAAD(byte[] src, int offset, int len) {
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   954
        throw new UnsupportedOperationException(
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   955
            "The underlying Cipher implementation "
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   956
            +  "does not support this method");
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   957
    }
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   958
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   959
    /**
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   960
     * Continues a multi-part update of the Additional Authentication
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   961
     * Data (AAD).
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   962
     * <p>
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   963
     * 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: 5506
diff changeset
   964
     * 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: 5506
diff changeset
   965
     * either GCM or CCM mode, all AAD must be supplied before beginning
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   966
     * operations on the ciphertext (via the {@code update} and {@code
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   967
     * doFinal} methods).
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   968
     * <p>
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   969
     * All {@code src.remaining()} bytes starting at
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   970
     * {@code src.position()} are processed.
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   971
     * Upon return, the input buffer's position will be equal
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   972
     * to its limit; its limit will not have changed.
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   973
     *
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   974
     * @param src the buffer containing the AAD
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   975
     *
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   976
     * @throws IllegalStateException if this cipher is in a wrong state
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   977
     * (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: 5506
diff changeset
   978
     * 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: 5506
diff changeset
   979
     * methods has already been called for the active
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   980
     * encryption/decryption operation
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   981
     * @throws UnsupportedOperationException if this method
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   982
     * has not been overridden by an implementation
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   983
     *
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   984
     * @since 1.7
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   985
     */
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   986
    protected void engineUpdateAAD(ByteBuffer src) {
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   987
        throw new UnsupportedOperationException(
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   988
            "The underlying Cipher implementation "
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   989
            +  "does not support this method");
62d885310f4d 7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents: 5506
diff changeset
   990
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
}