提交 af862438 编写于 作者: I igerasim

8171252: Improve exception checking

8158517: Minor optimizations to ISO10126PADDING
Reviewed-by: ascarpino, mschoene
上级 83890cdc
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -156,7 +156,7 @@ abstract class AESCipher extends CipherSpi { ...@@ -156,7 +156,7 @@ abstract class AESCipher extends CipherSpi {
throw new InvalidKeyException("Key encoding must not be null"); throw new InvalidKeyException("Key encoding must not be null");
} else if (value.length != fixedKeySize) { } else if (value.length != fixedKeySize) {
throw new InvalidKeyException("The key must be " + throw new InvalidKeyException("The key must be " +
fixedKeySize*8 + " bits"); fixedKeySize + " bytes");
} }
} }
} }
...@@ -509,7 +509,7 @@ abstract class AESCipher extends CipherSpi { ...@@ -509,7 +509,7 @@ abstract class AESCipher extends CipherSpi {
throw new InvalidKeyException("Invalid AES key length: " + throw new InvalidKeyException("Invalid AES key length: " +
encoded.length + " bytes"); encoded.length + " bytes");
} }
return encoded.length * 8; return Math.multiplyExact(encoded.length, 8);
} }
/** /**
...@@ -628,9 +628,9 @@ abstract class AESCipher extends CipherSpi { ...@@ -628,9 +628,9 @@ abstract class AESCipher extends CipherSpi {
} }
if (src != null) { if (src != null) {
int aadLen = src.limit() - src.position(); int aadLen = src.limit() - src.position();
if (aadLen != 0) { if (aadLen > 0) {
if (src.hasArray()) { if (src.hasArray()) {
int aadOfs = src.arrayOffset() + src.position(); int aadOfs = Math.addExact(src.arrayOffset(), src.position());
core.updateAAD(src.array(), aadOfs, aadLen); core.updateAAD(src.array(), aadOfs, aadLen);
src.position(src.limit()); src.position(src.limit());
} else { } else {
......
/* /*
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -156,7 +156,7 @@ abstract class AESWrapCipher extends CipherSpi { ...@@ -156,7 +156,7 @@ abstract class AESWrapCipher extends CipherSpi {
if (decrypting) { if (decrypting) {
result = inputLen - 8; result = inputLen - 8;
} else { } else {
result = inputLen + 8; result = Math.addExact(inputLen, 8);
} }
return (result < 0? 0:result); return (result < 0? 0:result);
} }
...@@ -378,7 +378,7 @@ abstract class AESWrapCipher extends CipherSpi { ...@@ -378,7 +378,7 @@ abstract class AESWrapCipher extends CipherSpi {
throw new InvalidKeyException("Invalid key length: " + throw new InvalidKeyException("Invalid key length: " +
encoded.length + " bytes"); encoded.length + " bytes");
} }
return encoded.length * 8; return Math.multiplyExact(encoded.length, 8);
} }
/** /**
...@@ -404,7 +404,7 @@ abstract class AESWrapCipher extends CipherSpi { ...@@ -404,7 +404,7 @@ abstract class AESWrapCipher extends CipherSpi {
throw new InvalidKeyException("Cannot get an encoding of " + throw new InvalidKeyException("Cannot get an encoding of " +
"the key to be wrapped"); "the key to be wrapped");
} }
byte[] out = new byte[keyVal.length + 8]; byte[] out = new byte[Math.addExact(keyVal.length, 8)];
if (keyVal.length == 8) { if (keyVal.length == 8) {
System.arraycopy(IV, 0, out, 0, IV.length); System.arraycopy(IV, 0, out, 0, IV.length);
......
/* /*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -257,7 +257,7 @@ public final class ARCFOURCipher extends CipherSpi { ...@@ -257,7 +257,7 @@ public final class ARCFOURCipher extends CipherSpi {
// see JCE spec // see JCE spec
protected int engineGetKeySize(Key key) throws InvalidKeyException { protected int engineGetKeySize(Key key) throws InvalidKeyException {
byte[] encodedKey = getEncodedKey(key); byte[] encodedKey = getEncodedKey(key);
return encodedKey.length << 3; return Math.multiplyExact(encodedKey.length, 8);
} }
} }
/* /*
* Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -373,7 +373,7 @@ public final class BlowfishCipher extends CipherSpi { ...@@ -373,7 +373,7 @@ public final class BlowfishCipher extends CipherSpi {
* @exception InvalidKeyException if <code>key</code> is invalid. * @exception InvalidKeyException if <code>key</code> is invalid.
*/ */
protected int engineGetKeySize(Key key) throws InvalidKeyException { protected int engineGetKeySize(Key key) throws InvalidKeyException {
return (key.getEncoded().length * 8); return Math.multiplyExact(key.getEncoded().length, 8);
} }
/** /**
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -324,13 +324,14 @@ final class CipherCore { ...@@ -324,13 +324,14 @@ final class CipherCore {
} }
private int getOutputSizeByOperation(int inputLen, boolean isDoFinal) { 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) { switch (cipherMode) {
case GCM_MODE: case GCM_MODE:
if (isDoFinal) { if (isDoFinal) {
int tagLen = ((GaloisCounterMode) cipher).getTagLen(); int tagLen = ((GaloisCounterMode) cipher).getTagLen();
if (!decrypting) { if (!decrypting) {
totalLen += tagLen; totalLen = Math.addExact(totalLen, tagLen);
} else { } else {
totalLen -= tagLen; totalLen -= tagLen;
} }
...@@ -346,10 +347,10 @@ final class CipherCore { ...@@ -346,10 +347,10 @@ final class CipherCore {
totalLen = diffBlocksize; totalLen = diffBlocksize;
} else { } else {
int residue = (totalLen - diffBlocksize) % blockSize; int residue = (totalLen - diffBlocksize) % blockSize;
totalLen += (blockSize - residue); totalLen = Math.addExact(totalLen, (blockSize - residue));
} }
} else { } else {
totalLen += padding.padLength(totalLen); totalLen = Math.addExact(totalLen, padding.padLength(totalLen));
} }
} }
break; break;
...@@ -711,7 +712,8 @@ final class CipherCore { ...@@ -711,7 +712,8 @@ final class CipherCore {
} }
// figure out how much can be sent to crypto function // 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) { if (padding != null && decrypting) {
// do not include the padding bytes when decrypting // do not include the padding bytes when decrypting
len -= blockSize; len -= blockSize;
...@@ -730,12 +732,12 @@ final class CipherCore { ...@@ -730,12 +732,12 @@ final class CipherCore {
int outLen = 0; int outLen = 0;
if (len != 0) { // there is some work to do if (len != 0) { // there is some work to do
if ((input == output) if ((input == output)
&& (outputOffset < (inputOffset + inputLen)) && (outputOffset - inputOffset < inputLen)
&& (inputOffset < (outputOffset + buffer.length))) { && (inputOffset - outputOffset < buffer.length)) {
// copy 'input' out to avoid its content being // copy 'input' out to avoid its content being
// overwritten prematurely. // overwritten prematurely.
input = Arrays.copyOfRange(input, inputOffset, input = Arrays.copyOfRange(input, inputOffset,
inputOffset + inputLen); Math.addExact(inputOffset, inputLen));
inputOffset = 0; inputOffset = 0;
} }
if (len <= buffered) { if (len <= buffered) {
...@@ -757,13 +759,13 @@ final class CipherCore { ...@@ -757,13 +759,13 @@ final class CipherCore {
if (bufferCapacity != 0) { if (bufferCapacity != 0) {
temp = Math.min(bufferCapacity, inputConsumed); temp = Math.min(bufferCapacity, inputConsumed);
if (unitBytes != blockSize) { if (unitBytes != blockSize) {
temp -= ((buffered + temp) % unitBytes); temp -= (Math.addExact(buffered, temp) % unitBytes);
} }
System.arraycopy(input, inputOffset, buffer, buffered, temp); System.arraycopy(input, inputOffset, buffer, buffered, temp);
inputOffset += temp; inputOffset = Math.addExact(inputOffset, temp);
inputConsumed -= temp; inputConsumed -= temp;
inputLen -= temp; inputLen -= temp;
buffered += temp; buffered = Math.addExact(buffered, temp);
} }
// process 'buffer' // process 'buffer'
if (decrypting) { if (decrypting) {
...@@ -771,7 +773,7 @@ final class CipherCore { ...@@ -771,7 +773,7 @@ final class CipherCore {
} else { } else {
outLen = cipher.encrypt(buffer, 0, buffered, output, outputOffset); outLen = cipher.encrypt(buffer, 0, buffered, output, outputOffset);
} }
outputOffset += outLen; outputOffset = Math.addExact(outputOffset, outLen);
buffered = 0; buffered = 0;
} }
if (inputConsumed > 0) { // still has input to process if (inputConsumed > 0) { // still has input to process
...@@ -802,7 +804,7 @@ final class CipherCore { ...@@ -802,7 +804,7 @@ final class CipherCore {
if (inputLen > 0) { if (inputLen > 0) {
System.arraycopy(input, inputOffset, buffer, buffered, System.arraycopy(input, inputOffset, buffer, buffered,
inputLen); inputLen);
buffered += inputLen; buffered = Math.addExact(buffered, inputLen);
} }
return outLen; return outLen;
} }
...@@ -912,10 +914,10 @@ final class CipherCore { ...@@ -912,10 +914,10 @@ final class CipherCore {
} }
// calculate total input length // calculate total input length
int len = buffered + inputLen; int len = Math.addExact(buffered, inputLen);
// calculate padding length // calculate padding length
int totalLen = len + cipher.getBufferedLength(); int totalLen = Math.addExact(len, cipher.getBufferedLength());
int paddingLen = 0; int paddingLen = 0;
// will the total input length be a multiple of blockSize? // will the total input length be a multiple of blockSize?
if (unitBytes != blockSize) { if (unitBytes != blockSize) {
...@@ -948,12 +950,12 @@ final class CipherCore { ...@@ -948,12 +950,12 @@ final class CipherCore {
int finalBufLen = inputLen; int finalBufLen = inputLen;
if ((buffered != 0) || (!decrypting && padding != null) || if ((buffered != 0) || (!decrypting && padding != null) ||
((input == output) ((input == output)
&& (outputOffset < (inputOffset + inputLen)) && (outputOffset - inputOffset < inputLen)
&& (inputOffset < (outputOffset + buffer.length)))) { && (inputOffset - outputOffset < buffer.length))) {
if (decrypting || padding == null) { if (decrypting || padding == null) {
paddingLen = 0; paddingLen = 0;
} }
finalBuf = new byte[len + paddingLen]; finalBuf = new byte[Math.addExact(len, paddingLen)];
finalOffset = 0; finalOffset = 0;
if (buffered != 0) { if (buffered != 0) {
System.arraycopy(buffer, 0, finalBuf, 0, buffered); System.arraycopy(buffer, 0, finalBuf, 0, buffered);
...@@ -963,7 +965,7 @@ final class CipherCore { ...@@ -963,7 +965,7 @@ final class CipherCore {
buffered, inputLen); buffered, inputLen);
} }
if (paddingLen != 0) { if (paddingLen != 0) {
padding.padWithLen(finalBuf, (buffered+inputLen), paddingLen); padding.padWithLen(finalBuf, Math.addExact(buffered, inputLen), paddingLen);
} }
finalBufLen = finalBuf.length; finalBufLen = finalBuf.length;
} }
......
/* /*
* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -140,7 +140,7 @@ public final class DESedeWrapCipher extends CipherSpi { ...@@ -140,7 +140,7 @@ public final class DESedeWrapCipher extends CipherSpi {
if (decrypting) { if (decrypting) {
result = inputLen - 16; // CHECKSUM_LEN + IV_LEN; result = inputLen - 16; // CHECKSUM_LEN + IV_LEN;
} else { } else {
result = inputLen + 16; result = Math.addExact(inputLen, 16);
} }
return (result < 0? 0:result); return (result < 0? 0:result);
} }
...@@ -452,11 +452,11 @@ public final class DESedeWrapCipher extends CipherSpi { ...@@ -452,11 +452,11 @@ public final class DESedeWrapCipher extends CipherSpi {
} }
byte[] cks = getChecksum(keyVal); byte[] cks = getChecksum(keyVal);
byte[] in = new byte[keyVal.length + CHECKSUM_LEN]; byte[] in = new byte[Math.addExact(keyVal.length, CHECKSUM_LEN)];
System.arraycopy(keyVal, 0, in, 0, keyVal.length); System.arraycopy(keyVal, 0, in, 0, keyVal.length);
System.arraycopy(cks, 0, in, keyVal.length, CHECKSUM_LEN); System.arraycopy(cks, 0, in, keyVal.length, CHECKSUM_LEN);
byte[] out = new byte[iv.length + in.length]; byte[] out = new byte[Math.addExact(iv.length, in.length)];
System.arraycopy(iv, 0, out, 0, iv.length); System.arraycopy(iv, 0, out, 0, iv.length);
cipher.encrypt(in, 0, in.length, out, iv.length); cipher.encrypt(in, 0, in.length, out, iv.length);
......
/* /*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -63,15 +63,16 @@ final class ISO10126Padding implements Padding { ...@@ -63,15 +63,16 @@ final class ISO10126Padding implements Padding {
if (in == null) if (in == null)
return; return;
if ((off + len) > in.length) { int idx = Math.addExact(off, len);
if (idx > in.length) {
throw new ShortBufferException("Buffer too small to hold padding"); throw new ShortBufferException("Buffer too small to hold padding");
} }
byte paddingOctet = (byte) (len & 0xff); byte paddingOctet = (byte) (len & 0xff);
byte[] padding = new byte[len]; byte[] padding = new byte[len - 1];
SunJCE.getRandom().nextBytes(padding); SunJCE.getRandom().nextBytes(padding);
padding[len-1] = paddingOctet; System.arraycopy(padding, 0, in, off, len - 1);
System.arraycopy(padding, 0, in, off, len); in[idx - 1] = paddingOctet;
return; return;
} }
...@@ -94,14 +95,15 @@ final class ISO10126Padding implements Padding { ...@@ -94,14 +95,15 @@ final class ISO10126Padding implements Padding {
return 0; return 0;
} }
byte lastByte = in[off + len - 1]; int idx = Math.addExact(off, len);
byte lastByte = in[idx - 1];
int padValue = (int)lastByte & 0x0ff; int padValue = (int)lastByte & 0x0ff;
if ((padValue < 0x01) if ((padValue < 0x01)
|| (padValue > blockSize)) { || (padValue > blockSize)) {
return -1; return -1;
} }
int start = off + len - ((int)lastByte & 0x0ff); int start = idx - padValue;
if (start < off) { if (start < off) {
return -1; return -1;
} }
......
/* /*
* Copyright (c) 2002, 2013, 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -260,7 +260,7 @@ final class PBES1Core { ...@@ -260,7 +260,7 @@ final class PBES1Core {
if (algo.equals("DES")) { if (algo.equals("DES")) {
// P || S (password concatenated with salt) // P || S (password concatenated with salt)
byte[] concat = new byte[passwdBytes.length + salt.length]; byte[] concat = new byte[Math.addExact(passwdBytes.length, salt.length)];
System.arraycopy(passwdBytes, 0, concat, 0, passwdBytes.length); System.arraycopy(passwdBytes, 0, concat, 0, passwdBytes.length);
java.util.Arrays.fill(passwdBytes, (byte)0x00); java.util.Arrays.fill(passwdBytes, (byte)0x00);
System.arraycopy(salt, 0, concat, passwdBytes.length, salt.length); System.arraycopy(salt, 0, concat, passwdBytes.length, salt.length);
......
/* /*
* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
package com.sun.crypto.provider; package com.sun.crypto.provider;
import javax.crypto.ShortBufferException; import javax.crypto.ShortBufferException;
import java.util.Arrays;
/** /**
* This class implements padding as specified in the PKCS#5 standard. * This class implements padding as specified in the PKCS#5 standard.
...@@ -63,14 +64,13 @@ final class PKCS5Padding implements Padding { ...@@ -63,14 +64,13 @@ final class PKCS5Padding implements Padding {
if (in == null) if (in == null)
return; return;
if ((off + len) > in.length) { int idx = Math.addExact(off, len);
if (idx > in.length) {
throw new ShortBufferException("Buffer too small to hold padding"); throw new ShortBufferException("Buffer too small to hold padding");
} }
byte paddingOctet = (byte) (len & 0xff); byte paddingOctet = (byte) (len & 0xff);
for (int i = 0; i < len; i++) { Arrays.fill(in, off, idx, paddingOctet);
in[i + off] = paddingOctet;
}
return; return;
} }
...@@ -92,25 +92,24 @@ final class PKCS5Padding implements Padding { ...@@ -92,25 +92,24 @@ final class PKCS5Padding implements Padding {
(len == 0)) { // this can happen if input is really a padded buffer (len == 0)) { // this can happen if input is really a padded buffer
return 0; return 0;
} }
int idx = Math.addExact(off, len);
byte lastByte = in[off + len - 1]; byte lastByte = in[idx - 1];
int padValue = (int)lastByte & 0x0ff; int padValue = (int)lastByte & 0x0ff;
if ((padValue < 0x01) if ((padValue < 0x01)
|| (padValue > blockSize)) { || (padValue > blockSize)) {
return -1; return -1;
} }
int start = off + len - ((int)lastByte & 0x0ff); int start = idx - padValue;
if (start < off) { if (start < off) {
return -1; return -1;
} }
for (int i = 0; i < ((int)lastByte & 0x0ff); i++) { for (int i = start; i < idx; i++) {
if (in[start+i] != lastByte) { if (in[i] != lastByte) {
return -1; return -1;
} }
} }
return start; return start;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册