Skip to content

Commit 48ba9d4

Browse files
koushikthirupatturAnthony Scarpino
authored andcommitted
8349946: Cipher javadoc could describe AEAD reuse better
Reviewed-by: ascarpino
1 parent 9dd93c6 commit 48ba9d4

File tree

1 file changed

+19
-106
lines changed

1 file changed

+19
-106
lines changed

src/java.base/share/classes/javax/crypto/Cipher.java

Lines changed: 19 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -96,49 +96,30 @@
9696
* provide authenticity assurances for both confidential data and
9797
* Additional Associated Data (AAD) that is not encrypted. (Please see
9898
* <a href="http://www.ietf.org/rfc/rfc5116.txt"> RFC 5116 </a> for more
99-
* information on AEAD and AAD algorithms such as GCM/CCM.) Both
99+
* information on AEAD and AAD algorithms.) Both
100100
* confidential and AAD data can be used when calculating the
101101
* authentication tag (similar to a {@link Mac}). This tag is appended
102102
* to the ciphertext during encryption, and is verified on decryption.
103103
* <p>
104-
* AEAD modes such as GCM/CCM perform all AAD authenticity calculations
104+
* AEAD modes perform all AAD authenticity calculations
105105
* before starting the ciphertext authenticity calculations. To avoid
106106
* implementations having to internally buffer ciphertext, all AAD data
107-
* must be supplied to GCM/CCM implementations (via the {@code updateAAD}
107+
* must be supplied to their implementations (via the {@code updateAAD}
108108
* methods) <b>before</b> the ciphertext is processed (via
109109
* the {@code update} and {@code doFinal} methods).
110110
* <p>
111-
* Note that GCM mode has a uniqueness requirement on IVs used in
112-
* encryption with a given key. When IVs are repeated for GCM
113-
* encryption, such usages are subject to forgery attacks. Thus, after
114-
* each encryption operation using GCM mode, callers should re-initialize
115-
* the {@code Cipher} objects with GCM parameters which have a different IV
116-
* value.
117-
* <pre>
118-
* GCMParameterSpec s = ...;
119-
* cipher.init(..., s);
120-
*
121-
* // If the GCM parameters were generated by the provider, it can
122-
* // be retrieved by:
123-
* // cipher.getParameters().getParameterSpec(GCMParameterSpec.class);
124-
*
125-
* cipher.updateAAD(...); // AAD
126-
* cipher.update(...); // Multi-part update
127-
* cipher.doFinal(...); // conclusion of operation
111+
* When a {@code doFinal} method completes the operation, the {@code Cipher} object will attempt
112+
* to reset the state to the most recent call to {@code init}, allowing for additional
113+
* operations. A successful reset depends on the mode ({@code ENCRYPT_MODE} or
114+
* {@code DECRYPT_MODE}) and the algorithm. AEAD algorithms may not reset, in order to prevent
115+
* forgery attacks due to Key and IV uniqueness requirements.
116+
* <p> An {@link IllegalStateException} will be thrown when calling {@code update}
117+
* or {@code doFinal} methods if a reset did not occur. A call to {@code init} will
118+
* re-initialize the {@code Cipher} object with new parameters.
128119
*
129-
* // Use a different IV value for every encryption
130-
* byte[] newIv = ...;
131-
* s = new GCMParameterSpec(s.getTLen(), newIv);
132-
* cipher.init(..., s);
133-
* ...
120+
* @see javax.crypto.Cipher
121+
* @see javax.crypto.spec.GCMParameterSpec
134122
*
135-
* </pre>
136-
* The ChaCha20 and ChaCha20-Poly1305 algorithms have a similar requirement
137-
* for unique nonces with a given key. After each encryption or decryption
138-
* operation, callers should re-initialize their ChaCha20 or ChaCha20-Poly1305
139-
* ciphers with parameters that specify a different nonce value. Please
140-
* see <a href="https://tools.ietf.org/html/rfc7539">RFC 7539</a> for more
141-
* information on the ChaCha20 and ChaCha20-Poly1305 algorithms.
142123
* <p>
143124
* Every implementation of the Java platform is required to support
144125
* the following standard {@code Cipher} object transformations with
@@ -167,8 +148,6 @@
167148
*
168149
* @spec https://www.rfc-editor.org/info/rfc5116
169150
* RFC 5116: An Interface and Algorithms for Authenticated Encryption
170-
* @spec https://www.rfc-editor.org/info/rfc7539
171-
* RFC 7539: ChaCha20 and Poly1305 for IETF Protocols
172151
* @spec security/standard-names.html Java Security Standard Algorithm Names
173152
* @author Jan Luehe
174153
* @see KeyGenerator
@@ -2110,21 +2089,10 @@ public final int update(ByteBuffer input, ByteBuffer output)
21102089
* case of decryption.
21112090
* The result is stored in a new buffer.
21122091
*
2113-
* <p>Upon finishing, this method resets this {@code Cipher} object
2114-
* to the state it was in when previously initialized via a call to
2115-
* {@code init}.
2116-
* That is, the object is reset and available to encrypt or decrypt
2117-
* (depending on the operation mode that was specified in the call to
2118-
* {@code init}) more data.
2119-
*
2120-
* <p>Note: if any exception is thrown, this {@code Cipher} object
2121-
* may need to be reset before it can be used again.
2122-
*
21232092
* @return the new buffer with the result
21242093
*
21252094
* @throws IllegalStateException if this {@code Cipher} object
2126-
* is in a wrong state (e.g., has not been initialized, or is not
2127-
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
2095+
* is in an incorrect mode or cannot be reset.
21282096
* @throws IllegalBlockSizeException if this cipher is a block cipher,
21292097
* no padding has been requested (only in encryption mode), and the total
21302098
* input length of the data processed by this cipher is not a multiple of
@@ -2224,23 +2192,12 @@ public final int doFinal(byte[] output, int outputOffset)
22242192
* case of decryption.
22252193
* The result is stored in a new buffer.
22262194
*
2227-
* <p>Upon finishing, this method resets this {@code Cipher} object
2228-
* to the state it was in when previously initialized via a call to
2229-
* {@code init}.
2230-
* That is, the object is reset and available to encrypt or decrypt
2231-
* (depending on the operation mode that was specified in the call to
2232-
* {@code init}) more data.
2233-
*
2234-
* <p>Note: if any exception is thrown, this {@code Cipher} object
2235-
* may need to be reset before it can be used again.
2236-
*
22372195
* @param input the input buffer
22382196
*
22392197
* @return the new buffer with the result
22402198
*
22412199
* @throws IllegalStateException if this {@code Cipher} object
2242-
* is in a wrong state (e.g., has not been initialized, or is not
2243-
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
2200+
* is in an incorrect mode or cannot be reset.
22442201
* @throws IllegalBlockSizeException if this cipher is a block cipher,
22452202
* no padding has been requested (only in encryption mode), and the total
22462203
* input length of the data processed by this cipher is not a multiple of
@@ -2280,16 +2237,6 @@ public final byte[] doFinal(byte[] input)
22802237
* case of decryption.
22812238
* The result is stored in a new buffer.
22822239
*
2283-
* <p>Upon finishing, this method resets this {@code Cipher} object
2284-
* to the state it was in when previously initialized via a call to
2285-
* {@code init}.
2286-
* That is, the object is reset and available to encrypt or decrypt
2287-
* (depending on the operation mode that was specified in the call to
2288-
* {@code init}) more data.
2289-
*
2290-
* <p>Note: if any exception is thrown, this {@code Cipher} object
2291-
* may need to be reset before it can be used again.
2292-
*
22932240
* @param input the input buffer
22942241
* @param inputOffset the offset in {@code input} where the input
22952242
* starts
@@ -2298,8 +2245,7 @@ public final byte[] doFinal(byte[] input)
22982245
* @return the new buffer with the result
22992246
*
23002247
* @throws IllegalStateException if this {@code Cipher} object
2301-
* is in a wrong state (e.g., has not been initialized, or is not
2302-
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
2248+
* is in an incorrect mode or cannot be reset.
23032249
* @throws IllegalBlockSizeException if this cipher is a block cipher,
23042250
* no padding has been requested (only in encryption mode), and the total
23052251
* input length of the data processed by this cipher is not a multiple of
@@ -2346,16 +2292,6 @@ public final byte[] doFinal(byte[] input, int inputOffset, int inputLen)
23462292
* {@link #getOutputSize(int) getOutputSize} to determine how big
23472293
* the output buffer should be.
23482294
*
2349-
* <p>Upon finishing, this method resets this {@code Cipher} object
2350-
* to the state it was in when previously initialized via a call to
2351-
* {@code init}.
2352-
* That is, the object is reset and available to encrypt or decrypt
2353-
* (depending on the operation mode that was specified in the call to
2354-
* {@code init}) more data.
2355-
*
2356-
* <p>Note: if any exception is thrown, this {@code Cipher} object
2357-
* may need to be reset before it can be used again.
2358-
*
23592295
* <p>Note: this method should be copy-safe, which means the
23602296
* {@code input} and {@code output} buffers can reference
23612297
* the same byte array and no unprocessed input data is overwritten
@@ -2370,8 +2306,7 @@ public final byte[] doFinal(byte[] input, int inputOffset, int inputLen)
23702306
* @return the number of bytes stored in {@code output}
23712307
*
23722308
* @throws IllegalStateException if this {@code Cipher} object
2373-
* is in a wrong state (e.g., has not been initialized, or is not
2374-
* in or {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
2309+
* is in an incorrect mode or cannot be reset.
23752310
* @throws IllegalBlockSizeException if this cipher is a block cipher,
23762311
* no padding has been requested (only in encryption mode), and the total
23772312
* input length of the data processed by this cipher is not a multiple of
@@ -2425,16 +2360,6 @@ public final int doFinal(byte[] input, int inputOffset, int inputLen,
24252360
* {@link #getOutputSize(int) getOutputSize} to determine how big
24262361
* the output buffer should be.
24272362
*
2428-
* <p>Upon finishing, this method resets this {@code Cipher} object
2429-
* to the state it was in when previously initialized via a call to
2430-
* {@code init}.
2431-
* That is, the object is reset and available to encrypt or decrypt
2432-
* (depending on the operation mode that was specified in the call to
2433-
* {@code init}) more data.
2434-
*
2435-
* <p>Note: if any exception is thrown, this {@code Cipher} object
2436-
* may need to be reset before it can be used again.
2437-
*
24382363
* <p>Note: this method should be copy-safe, which means the
24392364
* {@code input} and {@code output} buffers can reference
24402365
* the same byte array and no unprocessed input data is overwritten
@@ -2451,8 +2376,7 @@ public final int doFinal(byte[] input, int inputOffset, int inputLen,
24512376
* @return the number of bytes stored in {@code output}
24522377
*
24532378
* @throws IllegalStateException if this {@code Cipher} object
2454-
* is in a wrong state (e.g., has not been initialized, or is not
2455-
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
2379+
* is in an incorrect mode or cannot be reset.
24562380
* @throws IllegalBlockSizeException if this cipher is a block cipher,
24572381
* no padding has been requested (only in encryption mode), and the total
24582382
* input length of the data processed by this cipher is not a multiple of
@@ -2507,16 +2431,6 @@ public final int doFinal(byte[] input, int inputOffset, int inputLen,
25072431
* {@link #getOutputSize(int) getOutputSize} to determine how big
25082432
* the output buffer should be.
25092433
*
2510-
* <p>Upon finishing, this method resets this {@code Cipher} object
2511-
* to the state it was in when previously initialized via a call to
2512-
* {@code init}.
2513-
* That is, the object is reset and available to encrypt or decrypt
2514-
* (depending on the operation mode that was specified in the call to
2515-
* {@code init}) more data.
2516-
*
2517-
* <p>Note: if any exception is thrown, this {@code Cipher} object
2518-
* may need to be reset before it can be used again.
2519-
*
25202434
* <p>Note: this method should be copy-safe, which means the
25212435
* {@code input} and {@code output} buffers can reference
25222436
* the same byte array and no unprocessed input data is overwritten
@@ -2528,8 +2442,7 @@ public final int doFinal(byte[] input, int inputOffset, int inputLen,
25282442
* @return the number of bytes stored in {@code output}
25292443
*
25302444
* @throws IllegalStateException if this {@code Cipher} object
2531-
* is in a wrong state (e.g., has not been initialized, or is not
2532-
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
2445+
* is in an incorrect mode or cannot be reset.
25332446
* @throws IllegalArgumentException if input and output are the
25342447
* same object
25352448
* @throws ReadOnlyBufferException if the output buffer is read-only

0 commit comments

Comments
 (0)