24
24
*/
25
25
/*
26
26
* ===========================================================================
27
- * (c) Copyright IBM Corp. 2018, 2019 All Rights Reserved
27
+ * (c) Copyright IBM Corp. 2018, 2020 All Rights Reserved
28
28
* ===========================================================================
29
29
*/
30
30
package com .sun .crypto .provider ;
@@ -160,10 +160,11 @@ protected int engineGetBlockSize() {
160
160
* Get the output size based on an input length. In simple stream-cipher
161
161
* mode, the output size will equal the input size. For ChaCha20-Poly1305
162
162
* for encryption the output size will be the sum of the input length
163
- * and tag length. For decryption, the output size will be the input
164
- * length less the tag length or zero, whichever is larger.
163
+ * and tag length. For decryption, the output size will be the sum of
164
+ * 1. The input length less the tag length or zero, whichever is larger.
165
+ * 2. The unprocessed input length.
165
166
*
166
- * @param inputLen the length in bytes of the input
167
+ * @param inputLen the length in bytes of the input.
167
168
*
168
169
* @return the output length in bytes.
169
170
*/
@@ -174,9 +175,12 @@ protected int engineGetOutputSize(int inputLen) {
174
175
if (mode == MODE_NONE ) {
175
176
outLen = inputLen ;
176
177
} else if (mode == MODE_AEAD ) {
177
- outLen = (direction == Cipher .ENCRYPT_MODE ) ?
178
- Math .addExact (inputLen , TAG_LENGTH ) :
179
- Integer .max (inputLen - TAG_LENGTH , 0 );
178
+ if (direction == Cipher .ENCRYPT_MODE ) {
179
+ outLen = Math .addExact (inputLen , TAG_LENGTH );
180
+ } else {
181
+ outLen = Integer .max (inputLen , TAG_LENGTH ) - TAG_LENGTH ;
182
+ outLen = Math .addExact (outLen , engine .getCipherBufferLength ());
183
+ }
180
184
}
181
185
182
186
return outLen ;
@@ -849,6 +853,15 @@ int doUpdate(byte[] in, int inOff, int inLen, byte[] out, int outOff)
849
853
*/
850
854
int doFinal (byte [] in , int inOff , int inLen , byte [] out , int outOff )
851
855
throws ShortBufferException , AEADBadTagException , KeyException ;
856
+
857
+ /**
858
+ * Returns the length of the unprocessed input.
859
+ * Only used in EngineAEADDec since AEADDec does not process input in doUpdate().
860
+ * In other engines, the function should return zero.
861
+ *
862
+ * @return the number of unprocessed bytes left.
863
+ */
864
+ int getCipherBufferLength ();
852
865
}
853
866
854
867
private final class EngineStreamOnly implements ChaChaEngine {
@@ -891,6 +904,11 @@ public int doFinal(byte[] in, int inOff, int inLen, byte[] out,
891
904
return inLen ;
892
905
}
893
906
}
907
+
908
+ @ Override
909
+ public int getCipherBufferLength () {
910
+ return 0 ;
911
+ }
894
912
}
895
913
896
914
private final class EngineAEADEnc implements ChaChaEngine {
@@ -955,6 +973,11 @@ public synchronized int doFinal(byte[] in, int inOff, int inLen, byte[] out,
955
973
aadDone = false ;
956
974
return Math .addExact (inLen , TAG_LENGTH );
957
975
}
976
+
977
+ @ Override
978
+ public int getCipherBufferLength () {
979
+ return 0 ;
980
+ }
958
981
}
959
982
960
983
private final class EngineAEADDec implements ChaChaEngine {
@@ -1047,6 +1070,11 @@ public synchronized int doFinal(byte[] in, int inOff, int inLen, byte[] out,
1047
1070
1048
1071
return ctLen ;
1049
1072
}
1073
+
1074
+ @ Override
1075
+ public int getCipherBufferLength () {
1076
+ return cipherBuf .size ();
1077
+ }
1050
1078
}
1051
1079
1052
1080
public static final class ChaCha20Only extends NativeChaCha20Cipher {
0 commit comments