src/java.base/share/classes/com/sun/crypto/provider/CipherBlockChaining.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8076112 Cdiff src/java.base/share/classes/com/sun/crypto/provider/CipherBlockChaining.java

src/java.base/share/classes/com/sun/crypto/provider/CipherBlockChaining.java

Print this page

        

*** 25,34 **** --- 25,37 ---- package com.sun.crypto.provider; import java.security.InvalidKeyException; import java.security.ProviderException; + import java.util.Objects; + + import jdk.internal.HotSpotIntrinsicCandidate; /** * This class represents ciphers in cipher block chaining (CBC) mode. *
*** 136,154 **** * @exception ProviderException if <code>len</code> is not * a multiple of the block size * @return the length of the encrypted data */ int encrypt(byte[] plain, int plainOffset, int plainLen, byte[] cipher, int cipherOffset) { - if ((plainLen % blockSize) != 0) { - throw new ProviderException("Internal error in input buffering"); - } int endIndex = plainOffset + plainLen; for (; plainOffset < endIndex; ! plainOffset+=blockSize, cipherOffset += blockSize) { for (int i = 0; i < blockSize; i++) { k[i] = (byte)(plain[i + plainOffset] ^ r[i]); } embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset); System.arraycopy(cipher, cipherOffset, r, 0, blockSize); --- 139,164 ---- * @exception ProviderException if <code>len</code> is not * a multiple of the block size * @return the length of the encrypted data */ int encrypt(byte[] plain, int plainOffset, int plainLen, + byte[] cipher, int cipherOffset) { + cryptBlockSizeCheck(plainLen); + cryptNullAndBoundsCheck(plain, plainOffset, plainLen); + cryptNullAndBoundsCheck(cipher, cipherOffset, plainLen); + return implEncrypt(plain, plainOffset, plainLen, + cipher, cipherOffset); + } + + @HotSpotIntrinsicCandidate + private int implEncrypt(byte[] plain, int plainOffset, int plainLen, byte[] cipher, int cipherOffset) { int endIndex = plainOffset + plainLen; for (; plainOffset < endIndex; ! plainOffset += blockSize, cipherOffset += blockSize) { for (int i = 0; i < blockSize; i++) { k[i] = (byte)(plain[i + plainOffset] ^ r[i]); } embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset); System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
*** 177,191 **** * @exception ProviderException if <code>len</code> is not * a multiple of the block size * @return the length of the decrypted data */ int decrypt(byte[] cipher, int cipherOffset, int cipherLen, byte[] plain, int plainOffset) { - if ((cipherLen % blockSize) != 0) { - throw new ProviderException("Internal error in input buffering"); - } int endIndex = cipherOffset + cipherLen; for (; cipherOffset < endIndex; cipherOffset += blockSize, plainOffset += blockSize) { embeddedCipher.decryptBlock(cipher, cipherOffset, k, 0); --- 187,207 ---- * @exception ProviderException if <code>len</code> is not * a multiple of the block size * @return the length of the decrypted data */ int decrypt(byte[] cipher, int cipherOffset, int cipherLen, + byte[] plain, int plainOffset) { + cryptBlockSizeCheck(cipherLen); + cryptNullAndBoundsCheck(cipher, cipherOffset, cipherLen); + cryptNullAndBoundsCheck(plain, plainOffset, cipherLen); + return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset); + } + + @HotSpotIntrinsicCandidate + private int implDecrypt(byte[] cipher, int cipherOffset, int cipherLen, byte[] plain, int plainOffset) { int endIndex = cipherOffset + cipherLen; for (; cipherOffset < endIndex; cipherOffset += blockSize, plainOffset += blockSize) { embeddedCipher.decryptBlock(cipher, cipherOffset, k, 0);
*** 194,199 **** --- 210,238 ---- } System.arraycopy(cipher, cipherOffset, r, 0, blockSize); } return cipherLen; } + + private void cryptBlockSizeCheck(int len) { + if ((len % blockSize) != 0) { + throw new ProviderException("Internal error in input buffering"); + } + } + + private static void cryptNullAndBoundsCheck(byte[] array, int offset, int len) { + if (len <= 0) { + return; // not an error because cryptImpl/decryptImpl won't execute if len <= 0 + } + + Objects.requireNonNull(array); + + if (offset < 0 || offset >= array.length) { + throw new ArrayIndexOutOfBoundsException(offset); + } + + int endIndex = offset + len - 1; + if (endIndex < 0 || endIndex >= array.length) { + throw new ArrayIndexOutOfBoundsException(endIndex); + } + } }
src/java.base/share/classes/com/sun/crypto/provider/CipherBlockChaining.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File