src/java.base/share/classes/com/sun/crypto/provider/CipherBlockChaining.java
changeset 51052 080776992b29
parent 47216 71c04702a3d5
equal deleted inserted replaced
51051:4e98b465d706 51052:080776992b29
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    28 import java.security.InvalidKeyException;
    28 import java.security.InvalidKeyException;
    29 import java.security.ProviderException;
    29 import java.security.ProviderException;
    30 import java.util.Objects;
    30 import java.util.Objects;
    31 
    31 
    32 import jdk.internal.HotSpotIntrinsicCandidate;
    32 import jdk.internal.HotSpotIntrinsicCandidate;
       
    33 import sun.security.util.ArrayUtil;
    33 
    34 
    34 
    35 
    35 /**
    36 /**
    36  * This class represents ciphers in cipher block chaining (CBC) mode.
    37  * This class represents ciphers in cipher block chaining (CBC) mode.
    37  *
    38  *
   143     int encrypt(byte[] plain, int plainOffset, int plainLen,
   144     int encrypt(byte[] plain, int plainOffset, int plainLen,
   144                 byte[] cipher, int cipherOffset) {
   145                 byte[] cipher, int cipherOffset) {
   145         if (plainLen <= 0) {
   146         if (plainLen <= 0) {
   146             return plainLen;
   147             return plainLen;
   147         }
   148         }
   148         cryptBlockSizeCheck(plainLen);
   149         ArrayUtil.blockSizeCheck(plainLen, blockSize);
   149         cryptNullAndBoundsCheck(plain, plainOffset, plainLen);
   150         ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);
   150         cryptNullAndBoundsCheck(cipher, cipherOffset, plainLen);
   151         ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);
   151         return implEncrypt(plain, plainOffset, plainLen,
   152         return implEncrypt(plain, plainOffset, plainLen,
   152                            cipher, cipherOffset);
   153                            cipher, cipherOffset);
   153     }
   154     }
   154 
   155 
   155     @HotSpotIntrinsicCandidate
   156     @HotSpotIntrinsicCandidate
   194     int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
   195     int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
   195                 byte[] plain, int plainOffset) {
   196                 byte[] plain, int plainOffset) {
   196         if (cipherLen <= 0) {
   197         if (cipherLen <= 0) {
   197             return cipherLen;
   198             return cipherLen;
   198         }
   199         }
   199         cryptBlockSizeCheck(cipherLen);
   200         ArrayUtil.blockSizeCheck(cipherLen, blockSize);
   200         cryptNullAndBoundsCheck(cipher, cipherOffset, cipherLen);
   201         ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen);
   201         cryptNullAndBoundsCheck(plain, plainOffset, cipherLen);
   202         ArrayUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen);
   202         return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
   203         return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
   203     }
   204     }
   204 
   205 
   205     @HotSpotIntrinsicCandidate
   206     @HotSpotIntrinsicCandidate
   206     private int implDecrypt(byte[] cipher, int cipherOffset, int cipherLen,
   207     private int implDecrypt(byte[] cipher, int cipherOffset, int cipherLen,
   216             }
   217             }
   217             System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
   218             System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
   218         }
   219         }
   219         return cipherLen;
   220         return cipherLen;
   220     }
   221     }
   221 
       
   222     private void cryptBlockSizeCheck(int len) {
       
   223         if ((len % blockSize) != 0) {
       
   224             throw new ProviderException("Internal error in input buffering");
       
   225         }
       
   226     }
       
   227 
       
   228     private static void cryptNullAndBoundsCheck(byte[] array, int offset, int len) {
       
   229         Objects.requireNonNull(array);
       
   230 
       
   231         if (offset < 0 || offset >= array.length) {
       
   232             throw new ArrayIndexOutOfBoundsException(offset);
       
   233         }
       
   234 
       
   235         int endIndex = offset + len - 1;
       
   236         if (endIndex < 0 || endIndex >= array.length) {
       
   237             throw new ArrayIndexOutOfBoundsException(endIndex);
       
   238         }
       
   239     }
       
   240 }
   222 }