Skip to content

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed
 

‎closed/src/java.base/share/classes/com/sun/crypto/provider/NativeChaCha20Cipher.java

+35-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
/*
2626
* ===========================================================================
27-
* (c) Copyright IBM Corp. 2018, 2019 All Rights Reserved
27+
* (c) Copyright IBM Corp. 2018, 2020 All Rights Reserved
2828
* ===========================================================================
2929
*/
3030
package com.sun.crypto.provider;
@@ -160,10 +160,11 @@ protected int engineGetBlockSize() {
160160
* Get the output size based on an input length. In simple stream-cipher
161161
* mode, the output size will equal the input size. For ChaCha20-Poly1305
162162
* 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.
165166
*
166-
* @param inputLen the length in bytes of the input
167+
* @param inputLen the length in bytes of the input.
167168
*
168169
* @return the output length in bytes.
169170
*/
@@ -174,9 +175,12 @@ protected int engineGetOutputSize(int inputLen) {
174175
if (mode == MODE_NONE) {
175176
outLen = inputLen;
176177
} 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+
}
180184
}
181185

182186
return outLen;
@@ -849,6 +853,15 @@ int doUpdate(byte[] in, int inOff, int inLen, byte[] out, int outOff)
849853
*/
850854
int doFinal(byte[] in, int inOff, int inLen, byte[] out, int outOff)
851855
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();
852865
}
853866

854867
private final class EngineStreamOnly implements ChaChaEngine {
@@ -891,6 +904,11 @@ public int doFinal(byte[] in, int inOff, int inLen, byte[] out,
891904
return inLen;
892905
}
893906
}
907+
908+
@Override
909+
public int getCipherBufferLength() {
910+
return 0;
911+
}
894912
}
895913

896914
private final class EngineAEADEnc implements ChaChaEngine {
@@ -955,6 +973,11 @@ public synchronized int doFinal(byte[] in, int inOff, int inLen, byte[] out,
955973
aadDone = false;
956974
return Math.addExact(inLen, TAG_LENGTH);
957975
}
976+
977+
@Override
978+
public int getCipherBufferLength() {
979+
return 0;
980+
}
958981
}
959982

960983
private final class EngineAEADDec implements ChaChaEngine {
@@ -1047,6 +1070,11 @@ public synchronized int doFinal(byte[] in, int inOff, int inLen, byte[] out,
10471070

10481071
return ctLen;
10491072
}
1073+
1074+
@Override
1075+
public int getCipherBufferLength() {
1076+
return cipherBuf.size();
1077+
}
10501078
}
10511079

10521080
public static final class ChaCha20Only extends NativeChaCha20Cipher {

0 commit comments

Comments
 (0)
Please sign in to comment.