< prev index next >
src/share/classes/com/sun/crypto/provider/CipherCore.java
Print this page
rev 12526 : 8171252: Improve exception checking
8158517: Minor optimizations to ISO10126PADDING
Reviewed-by: ascarpino, mschoene
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -322,17 +322,18 @@
// estimate based on the maximum
return getOutputSizeByOperation(inputLen, true);
}
private int getOutputSizeByOperation(int inputLen, boolean isDoFinal) {
- int totalLen = buffered + inputLen + cipher.getBufferedLength();
+ int totalLen = Math.addExact(buffered, cipher.getBufferedLength());
+ totalLen = Math.addExact(totalLen, inputLen);
switch (cipherMode) {
case GCM_MODE:
if (isDoFinal) {
int tagLen = ((GaloisCounterMode) cipher).getTagLen();
if (!decrypting) {
- totalLen += tagLen;
+ totalLen = Math.addExact(totalLen, tagLen);
} else {
totalLen -= tagLen;
}
}
if (totalLen < 0) {
@@ -344,14 +345,14 @@
if (unitBytes != blockSize) {
if (totalLen < diffBlocksize) {
totalLen = diffBlocksize;
} else {
int residue = (totalLen - diffBlocksize) % blockSize;
- totalLen += (blockSize - residue);
+ totalLen = Math.addExact(totalLen, (blockSize - residue));
}
} else {
- totalLen += padding.padLength(totalLen);
+ totalLen = Math.addExact(totalLen, padding.padLength(totalLen));
}
}
break;
}
return totalLen;
@@ -709,11 +710,12 @@
throw new IllegalStateException
("Must use either different key or iv for GCM encryption");
}
// figure out how much can be sent to crypto function
- int len = buffered + inputLen - minBytes;
+ int len = Math.addExact(buffered, inputLen);
+ len -= minBytes;
if (padding != null && decrypting) {
// do not include the padding bytes when decrypting
len -= blockSize;
}
// do not count the trailing bytes which do not make up a unit
@@ -728,16 +730,16 @@
}
int outLen = 0;
if (len != 0) { // there is some work to do
if ((input == output)
- && (outputOffset < (inputOffset + inputLen))
- && (inputOffset < (outputOffset + buffer.length))) {
+ && (outputOffset - inputOffset < inputLen)
+ && (inputOffset - outputOffset < buffer.length)) {
// copy 'input' out to avoid its content being
// overwritten prematurely.
input = Arrays.copyOfRange(input, inputOffset,
- inputOffset + inputLen);
+ Math.addExact(inputOffset, inputLen));
inputOffset = 0;
}
if (len <= buffered) {
// all to-be-processed data are from 'buffer'
if (decrypting) {
@@ -755,25 +757,25 @@
if (buffered > 0) {
int bufferCapacity = buffer.length - buffered;
if (bufferCapacity != 0) {
temp = Math.min(bufferCapacity, inputConsumed);
if (unitBytes != blockSize) {
- temp -= ((buffered + temp) % unitBytes);
+ temp -= (Math.addExact(buffered, temp) % unitBytes);
}
System.arraycopy(input, inputOffset, buffer, buffered, temp);
- inputOffset += temp;
+ inputOffset = Math.addExact(inputOffset, temp);
inputConsumed -= temp;
inputLen -= temp;
- buffered += temp;
+ buffered = Math.addExact(buffered, temp);
}
// process 'buffer'
if (decrypting) {
outLen = cipher.decrypt(buffer, 0, buffered, output, outputOffset);
} else {
outLen = cipher.encrypt(buffer, 0, buffered, output, outputOffset);
}
- outputOffset += outLen;
+ outputOffset = Math.addExact(outputOffset, outLen);
buffered = 0;
}
if (inputConsumed > 0) { // still has input to process
if (decrypting) {
outLen += cipher.decrypt(input, inputOffset, inputConsumed,
@@ -800,11 +802,11 @@
}
// Store remaining input into 'buffer' again
if (inputLen > 0) {
System.arraycopy(input, inputOffset, buffer, buffered,
inputLen);
- buffered += inputLen;
+ buffered = Math.addExact(buffered, inputLen);
}
return outLen;
}
/**
@@ -910,14 +912,14 @@
throw new ShortBufferException("Output buffer must be "
+ "(at least) " + minOutSize + " bytes long");
}
// calculate total input length
- int len = buffered + inputLen;
+ int len = Math.addExact(buffered, inputLen);
// calculate padding length
- int totalLen = len + cipher.getBufferedLength();
+ int totalLen = Math.addExact(len, cipher.getBufferedLength());
int paddingLen = 0;
// will the total input length be a multiple of blockSize?
if (unitBytes != blockSize) {
if (totalLen < diffBlocksize) {
paddingLen = diffBlocksize - totalLen;
@@ -946,26 +948,26 @@
byte[] finalBuf = input;
int finalOffset = inputOffset;
int finalBufLen = inputLen;
if ((buffered != 0) || (!decrypting && padding != null) ||
((input == output)
- && (outputOffset < (inputOffset + inputLen))
- && (inputOffset < (outputOffset + buffer.length)))) {
+ && (outputOffset - inputOffset < inputLen)
+ && (inputOffset - outputOffset < buffer.length))) {
if (decrypting || padding == null) {
paddingLen = 0;
}
- finalBuf = new byte[len + paddingLen];
+ finalBuf = new byte[Math.addExact(len, paddingLen)];
finalOffset = 0;
if (buffered != 0) {
System.arraycopy(buffer, 0, finalBuf, 0, buffered);
}
if (inputLen != 0) {
System.arraycopy(input, inputOffset, finalBuf,
buffered, inputLen);
}
if (paddingLen != 0) {
- padding.padWithLen(finalBuf, (buffered+inputLen), paddingLen);
+ padding.padWithLen(finalBuf, Math.addExact(buffered, inputLen), paddingLen);
}
finalBufLen = finalBuf.length;
}
int outLen = 0;
if (decrypting) {
< prev index next >