< 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 **** /* ! * Copyright (c) 2002, 2016, 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 --- 1,7 ---- /* ! * 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,338 **** // estimate based on the maximum return getOutputSizeByOperation(inputLen, true); } private int getOutputSizeByOperation(int inputLen, boolean isDoFinal) { ! int totalLen = buffered + inputLen + cipher.getBufferedLength(); switch (cipherMode) { case GCM_MODE: if (isDoFinal) { int tagLen = ((GaloisCounterMode) cipher).getTagLen(); if (!decrypting) { ! totalLen += tagLen; } else { totalLen -= tagLen; } } if (totalLen < 0) { --- 322,339 ---- // estimate based on the maximum return getOutputSizeByOperation(inputLen, true); } private int getOutputSizeByOperation(int inputLen, boolean isDoFinal) { ! 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 = Math.addExact(totalLen, tagLen); } else { totalLen -= tagLen; } } if (totalLen < 0) {
*** 344,357 **** if (unitBytes != blockSize) { if (totalLen < diffBlocksize) { totalLen = diffBlocksize; } else { int residue = (totalLen - diffBlocksize) % blockSize; ! totalLen += (blockSize - residue); } } else { ! totalLen += padding.padLength(totalLen); } } break; } return totalLen; --- 345,358 ---- if (unitBytes != blockSize) { if (totalLen < diffBlocksize) { totalLen = diffBlocksize; } else { int residue = (totalLen - diffBlocksize) % blockSize; ! totalLen = Math.addExact(totalLen, (blockSize - residue)); } } else { ! totalLen = Math.addExact(totalLen, padding.padLength(totalLen)); } } break; } return totalLen;
*** 709,719 **** 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; 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 --- 710,721 ---- 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 = 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,743 **** } int outLen = 0; if (len != 0) { // there is some work to do if ((input == output) ! && (outputOffset < (inputOffset + inputLen)) ! && (inputOffset < (outputOffset + buffer.length))) { // copy 'input' out to avoid its content being // overwritten prematurely. input = Arrays.copyOfRange(input, inputOffset, ! inputOffset + inputLen); inputOffset = 0; } if (len <= buffered) { // all to-be-processed data are from 'buffer' if (decrypting) { --- 730,745 ---- } int outLen = 0; if (len != 0) { // there is some work to do if ((input == output) ! && (outputOffset - inputOffset < inputLen) ! && (inputOffset - outputOffset < buffer.length)) { // copy 'input' out to avoid its content being // overwritten prematurely. input = Arrays.copyOfRange(input, inputOffset, ! Math.addExact(inputOffset, inputLen)); inputOffset = 0; } if (len <= buffered) { // all to-be-processed data are from 'buffer' if (decrypting) {
*** 755,779 **** if (buffered > 0) { int bufferCapacity = buffer.length - buffered; if (bufferCapacity != 0) { temp = Math.min(bufferCapacity, inputConsumed); if (unitBytes != blockSize) { ! temp -= ((buffered + temp) % unitBytes); } System.arraycopy(input, inputOffset, buffer, buffered, temp); ! inputOffset += temp; inputConsumed -= temp; inputLen -= temp; ! 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; buffered = 0; } if (inputConsumed > 0) { // still has input to process if (decrypting) { outLen += cipher.decrypt(input, inputOffset, inputConsumed, --- 757,781 ---- if (buffered > 0) { int bufferCapacity = buffer.length - buffered; if (bufferCapacity != 0) { temp = Math.min(bufferCapacity, inputConsumed); if (unitBytes != blockSize) { ! temp -= (Math.addExact(buffered, temp) % unitBytes); } System.arraycopy(input, inputOffset, buffer, buffered, temp); ! inputOffset = Math.addExact(inputOffset, temp); inputConsumed -= temp; inputLen -= 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 = Math.addExact(outputOffset, outLen); buffered = 0; } if (inputConsumed > 0) { // still has input to process if (decrypting) { outLen += cipher.decrypt(input, inputOffset, inputConsumed,
*** 800,810 **** } // Store remaining input into 'buffer' again if (inputLen > 0) { System.arraycopy(input, inputOffset, buffer, buffered, inputLen); ! buffered += inputLen; } return outLen; } /** --- 802,812 ---- } // Store remaining input into 'buffer' again if (inputLen > 0) { System.arraycopy(input, inputOffset, buffer, buffered, inputLen); ! buffered = Math.addExact(buffered, inputLen); } return outLen; } /**
*** 910,923 **** throw new ShortBufferException("Output buffer must be " + "(at least) " + minOutSize + " bytes long"); } // calculate total input length ! int len = buffered + inputLen; // calculate padding length ! int totalLen = 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; --- 912,925 ---- throw new ShortBufferException("Output buffer must be " + "(at least) " + minOutSize + " bytes long"); } // calculate total input length ! int len = Math.addExact(buffered, inputLen); // calculate padding length ! 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,971 **** byte[] finalBuf = input; int finalOffset = inputOffset; int finalBufLen = inputLen; if ((buffered != 0) || (!decrypting && padding != null) || ((input == output) ! && (outputOffset < (inputOffset + inputLen)) ! && (inputOffset < (outputOffset + buffer.length)))) { if (decrypting || padding == null) { paddingLen = 0; } ! finalBuf = new byte[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); } finalBufLen = finalBuf.length; } int outLen = 0; if (decrypting) { --- 948,973 ---- byte[] finalBuf = input; int finalOffset = inputOffset; int finalBufLen = inputLen; if ((buffered != 0) || (!decrypting && padding != null) || ((input == output) ! && (outputOffset - inputOffset < inputLen) ! && (inputOffset - outputOffset < buffer.length))) { if (decrypting || padding == null) { paddingLen = 0; } ! 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, Math.addExact(buffered, inputLen), paddingLen); } finalBufLen = finalBuf.length; } int outLen = 0; if (decrypting) {
< prev index next >