Skip to content

Commit 991ce84

Browse files
blperez01Valerie Peng
authored and
Valerie Peng
committedOct 6, 2023
4964430: (spec) missing IllegalStateException exception requirement for javax.crypto.Cipher.doFinal
Reviewed-by: mullan, valeriep
1 parent 8a30c2a commit 991ce84

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed
 

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

+45-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1815,8 +1815,8 @@ public final void init(int opmode, Certificate certificate,
18151815

18161816
/**
18171817
* Ensures that {@code Cipher} object is in a valid state for update() and
1818-
* doFinal() calls - should be initialized and in ENCRYPT_MODE or
1819-
* DECRYPT_MODE.
1818+
* doFinal() calls - should be initialized and in {@code ENCRYPT_MODE} or
1819+
* {@code DECRYPT_MODE}.
18201820
* @throws IllegalStateException if this {@code Cipher} object is not in
18211821
* valid state
18221822
*/
@@ -1851,7 +1851,8 @@ private void checkCipherState() {
18511851
* new block
18521852
*
18531853
* @throws IllegalStateException if this {@code Cipher} object is in a
1854-
* wrong state (e.g., has not been initialized)
1854+
* wrong state (e.g., has not been initialized, or is not
1855+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
18551856
*/
18561857
public final byte[] update(byte[] input) {
18571858
checkCipherState();
@@ -1890,7 +1891,8 @@ public final byte[] update(byte[] input) {
18901891
* new block.
18911892
*
18921893
* @throws IllegalStateException if this {@code Cipher} object
1893-
* is in a wrong state (e.g., has not been initialized)
1894+
* is in a wrong state (e.g., has not been initialized, or is not
1895+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
18941896
*/
18951897
public final byte[] update(byte[] input, int inputOffset, int inputLen) {
18961898
checkCipherState();
@@ -1940,7 +1942,8 @@ public final byte[] update(byte[] input, int inputOffset, int inputLen) {
19401942
* @return the number of bytes stored in {@code output}
19411943
*
19421944
* @throws IllegalStateException if this {@code Cipher} object
1943-
* is in a wrong state (e.g., has not been initialized)
1945+
* is in a wrong state (e.g., has not been initialized, or is not
1946+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
19441947
* @throws ShortBufferException if the given output buffer is too small
19451948
* to hold the result
19461949
*/
@@ -1998,7 +2001,8 @@ public final int update(byte[] input, int inputOffset, int inputLen,
19982001
* @return the number of bytes stored in {@code output}
19992002
*
20002003
* @throws IllegalStateException if this {@code Cipher} object
2001-
* is in a wrong state (e.g., has not been initialized)
2004+
* is in a wrong state (e.g., has not been initialized, or is not
2005+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
20022006
* @throws ShortBufferException if the given output buffer is too small
20032007
* to hold the result
20042008
*/
@@ -2052,7 +2056,8 @@ public final int update(byte[] input, int inputOffset, int inputLen,
20522056
* @return the number of bytes stored in {@code output}
20532057
*
20542058
* @throws IllegalStateException if this {@code Cipher} object
2055-
* is in a wrong state (e.g., has not been initialized)
2059+
* is in a wrong state (e.g., has not been initialized, or is not
2060+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
20562061
* @throws IllegalArgumentException if input and output are the
20572062
* same object
20582063
* @throws ReadOnlyBufferException if the output buffer is read-only
@@ -2104,7 +2109,8 @@ public final int update(ByteBuffer input, ByteBuffer output)
21042109
* @return the new buffer with the result
21052110
*
21062111
* @throws IllegalStateException if this {@code Cipher} object
2107-
* is in a wrong state (e.g., has not been initialized)
2112+
* is in a wrong state (e.g., has not been initialized, or is not
2113+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
21082114
* @throws IllegalBlockSizeException if this cipher is a block cipher,
21092115
* no padding has been requested (only in encryption mode), and the total
21102116
* input length of the data processed by this cipher is not a multiple of
@@ -2161,7 +2167,8 @@ public final byte[] doFinal()
21612167
* @return the number of bytes stored in {@code output}
21622168
*
21632169
* @throws IllegalStateException if this {@code Cipher} object
2164-
* is in a wrong state (e.g., has not been initialized)
2170+
* is in a wrong state (e.g., has not been initialized, or is not
2171+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
21652172
* @throws IllegalBlockSizeException if this cipher is a block cipher,
21662173
* no padding has been requested (only in encryption mode), and the total
21672174
* input length of the data processed by this cipher is not a multiple of
@@ -2218,7 +2225,8 @@ public final int doFinal(byte[] output, int outputOffset)
22182225
* @return the new buffer with the result
22192226
*
22202227
* @throws IllegalStateException if this {@code Cipher} object
2221-
* is in a wrong state (e.g., has not been initialized)
2228+
* is in a wrong state (e.g., has not been initialized, or is not
2229+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
22222230
* @throws IllegalBlockSizeException if this cipher is a block cipher,
22232231
* no padding has been requested (only in encryption mode), and the total
22242232
* input length of the data processed by this cipher is not a multiple of
@@ -2276,7 +2284,8 @@ public final byte[] doFinal(byte[] input)
22762284
* @return the new buffer with the result
22772285
*
22782286
* @throws IllegalStateException if this {@code Cipher} object
2279-
* is in a wrong state (e.g., has not been initialized)
2287+
* is in a wrong state (e.g., has not been initialized, or is not
2288+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
22802289
* @throws IllegalBlockSizeException if this cipher is a block cipher,
22812290
* no padding has been requested (only in encryption mode), and the total
22822291
* input length of the data processed by this cipher is not a multiple of
@@ -2347,7 +2356,8 @@ public final byte[] doFinal(byte[] input, int inputOffset, int inputLen)
23472356
* @return the number of bytes stored in {@code output}
23482357
*
23492358
* @throws IllegalStateException if this {@code Cipher} object
2350-
* is in a wrong state (e.g., has not been initialized)
2359+
* is in a wrong state (e.g., has not been initialized, or is not
2360+
* in or {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
23512361
* @throws IllegalBlockSizeException if this cipher is a block cipher,
23522362
* no padding has been requested (only in encryption mode), and the total
23532363
* input length of the data processed by this cipher is not a multiple of
@@ -2427,7 +2437,8 @@ public final int doFinal(byte[] input, int inputOffset, int inputLen,
24272437
* @return the number of bytes stored in {@code output}
24282438
*
24292439
* @throws IllegalStateException if this {@code Cipher} object
2430-
* is in a wrong state (e.g., has not been initialized)
2440+
* is in a wrong state (e.g., has not been initialized, or is not
2441+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
24312442
* @throws IllegalBlockSizeException if this cipher is a block cipher,
24322443
* no padding has been requested (only in encryption mode), and the total
24332444
* input length of the data processed by this cipher is not a multiple of
@@ -2503,7 +2514,8 @@ public final int doFinal(byte[] input, int inputOffset, int inputLen,
25032514
* @return the number of bytes stored in {@code output}
25042515
*
25052516
* @throws IllegalStateException if this {@code Cipher} object
2506-
* is in a wrong state (e.g., has not been initialized)
2517+
* is in a wrong state (e.g., has not been initialized, or is not
2518+
* in {@code ENCRYPT_MODE} or {@code DECRYPT_MODE})
25072519
* @throws IllegalArgumentException if input and output are the
25082520
* same object
25092521
* @throws ReadOnlyBufferException if the output buffer is read-only
@@ -2551,7 +2563,7 @@ public final int doFinal(ByteBuffer input, ByteBuffer output)
25512563
* @return the wrapped key
25522564
*
25532565
* @throws IllegalStateException if this {@code Cipher} object is in a wrong
2554-
* state (e.g., has not been initialized)
2566+
* state (e.g., has not been initialized, or is not in {@code WRAP_MODE})
25552567
*
25562568
* @throws IllegalBlockSizeException if this cipher is a block
25572569
* cipher, no padding has been requested, and the length of the
@@ -2595,8 +2607,8 @@ public final byte[] wrap(Key key)
25952607
*
25962608
* @return the unwrapped key
25972609
*
2598-
* @throws IllegalStateException if this {@code Cipher} object
2599-
* is in a wrong state (e.g., has not been initialized)
2610+
* @throws IllegalStateException if this {@code Cipher} object is in a wrong
2611+
* state (e.g., has not been initialized, or is not in {@code UNWRAP_MODE})
26002612
*
26012613
* @throws NoSuchAlgorithmException if no installed providers
26022614
* can create keys of type {@code wrappedKeyType} for the
@@ -2732,10 +2744,11 @@ public static final AlgorithmParameterSpec getMaxAllowedParameterSpec(
27322744
* @throws IllegalArgumentException if the {@code src}
27332745
* byte array is {@code null}
27342746
* @throws IllegalStateException if this {@code Cipher} object
2735-
* is in a wrong state (e.g., has not been initialized),
2736-
* does not accept AAD, or if operating in either GCM or CCM mode and
2737-
* one of the {@code update} methods has already been called for the active
2738-
* encryption/decryption operation
2747+
* is in a wrong state (e.g., has not been initialized, or is not in
2748+
* {@code ENCRYPT_MODE} or {@code DECRYPT_MODE}), does not accept AAD,
2749+
* or if operating in either GCM or CCM mode and one of the {@code update}
2750+
* methods has already been called for the active encryption/decryption
2751+
* operation
27392752
* @throws UnsupportedOperationException if the corresponding method
27402753
* in the {@code CipherSpi} has not been overridden by an
27412754
* implementation
@@ -2770,10 +2783,11 @@ public final void updateAAD(byte[] src) {
27702783
* {@code len} is greater than the length of the
27712784
* {@code src} byte array
27722785
* @throws IllegalStateException if this {@code Cipher} object
2773-
* is in a wrong state (e.g., has not been initialized),
2774-
* does not accept AAD, or if operating in either GCM or CCM mode and
2775-
* one of the {@code update} methods has already been called for the active
2776-
* encryption/decryption operation
2786+
* is in a wrong state (e.g., has not been initialized, or is not in
2787+
* {@code ENCRYPT_MODE} or {@code DECRYPT_MODE}), does not accept AAD,
2788+
* or if operating in either GCM or CCM mode and one of the {@code update}
2789+
* methods has already been called for the active encryption/decryption
2790+
* operation
27772791
* @throws UnsupportedOperationException if the corresponding method
27782792
* in the {@code CipherSpi} has not been overridden by an
27792793
* implementation
@@ -2816,10 +2830,11 @@ public final void updateAAD(byte[] src, int offset, int len) {
28162830
* @throws IllegalArgumentException if the {@code src ByteBuffer}
28172831
* is {@code null}
28182832
* @throws IllegalStateException if this {@code Cipher} object
2819-
* is in a wrong state (e.g., has not been initialized),
2820-
* does not accept AAD, or if operating in either GCM or CCM mode and
2821-
* one of the {@code update} methods has already been called for the active
2822-
* encryption/decryption operation
2833+
* is in a wrong state (e.g., has not been initialized, or is not in
2834+
* {@code ENCRYPT_MODE} or {@code DECRYPT_MODE}), does not accept AAD,
2835+
* or if operating in either GCM or CCM mode and one of the {@code update}
2836+
* methods has already been called for the active encryption/decryption
2837+
* operation
28232838
* @throws UnsupportedOperationException if the corresponding method
28242839
* in the {@code CipherSpi} has not been overridden by an
28252840
* implementation

‎src/java.base/share/classes/javax/crypto/NullCipher.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,7 +30,9 @@
3030
* "identity cipher" -- one that does not transform the plain text. As
3131
* a consequence, the ciphertext is identical to the plaintext. All
3232
* initialization methods do nothing, while the blocksize is set to 1
33-
* byte.
33+
* byte. Unlike other ciphers, the {@code NullCipher} has no state, and
34+
* will never throw an {@code IllegalStateException} when {@code Cipher}
35+
* methods are called.
3436
*
3537
* @author Li Gong
3638
* @since 1.4

0 commit comments

Comments
 (0)
Please sign in to comment.