Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/CipherBlockChaining.java
41161 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.crypto.provider;2627import java.security.InvalidKeyException;28import java.security.ProviderException;29import java.util.Objects;3031import jdk.internal.vm.annotation.IntrinsicCandidate;32import sun.security.util.ArrayUtil;333435/**36* This class represents ciphers in cipher block chaining (CBC) mode.37*38* <p>This mode is implemented independently of a particular cipher.39* Ciphers to which this mode should apply (e.g., DES) must be40* <i>plugged-in</i> using the constructor.41*42* <p>NOTE: This class does not deal with buffering or padding.43*44* @author Gigi Ankeny45*/4647class CipherBlockChaining extends FeedbackCipher {4849/*50* random bytes that are initialized with iv51*/52protected byte[] r;5354/*55* output buffer56*/57private byte[] k;5859// variables for save/restore calls60private byte[] rSave = null;6162CipherBlockChaining(SymmetricCipher embeddedCipher) {63super(embeddedCipher);64k = new byte[blockSize];65r = new byte[blockSize];66}6768/**69* Gets the name of this feedback mode.70*71* @return the string <code>CBC</code>72*/73String getFeedback() {74return "CBC";75}7677/**78* Initializes the cipher in the specified mode with the given key79* and iv.80*81* @param decrypting flag indicating encryption or decryption82* @param algorithm the algorithm name83* @param key the key84* @param iv the iv85*86* @exception InvalidKeyException if the given key is inappropriate for87* initializing this cipher88*/89void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)90throws InvalidKeyException {91if ((key == null) || (iv == null) || (iv.length != blockSize)) {92throw new InvalidKeyException("Internal error");93}94this.iv = iv;95reset();96embeddedCipher.init(decrypting, algorithm, key);97}9899/**100* Resets the iv to its original value.101* This is used when doFinal is called in the Cipher class, so that the102* cipher can be reused (with its original iv).103*/104void reset() {105System.arraycopy(iv, 0, r, 0, blockSize);106}107108/**109* Save the current content of this cipher.110*/111void save() {112if (rSave == null) {113rSave = new byte[blockSize];114}115System.arraycopy(r, 0, rSave, 0, blockSize);116}117118/**119* Restores the content of this cipher to the previous saved one.120*/121void restore() {122System.arraycopy(rSave, 0, r, 0, blockSize);123}124125/**126* Performs encryption operation.127*128* <p>The input plain text <code>plain</code>, starting at129* <code>plainOffset</code> and ending at130* <code>(plainOffset + plainLen - 1)</code>, is encrypted.131* The result is stored in <code>cipher</code>, starting at132* <code>cipherOffset</code>.133*134* @param plain the buffer with the input data to be encrypted135* @param plainOffset the offset in <code>plain</code>136* @param plainLen the length of the input data137* @param cipher the buffer for the result138* @param cipherOffset the offset in <code>cipher</code>139* @exception ProviderException if <code>len</code> is not140* a multiple of the block size141* @return the length of the encrypted data142*/143int encrypt(byte[] plain, int plainOffset, int plainLen,144byte[] cipher, int cipherOffset) {145if (plainLen <= 0) {146return plainLen;147}148ArrayUtil.blockSizeCheck(plainLen, blockSize);149ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);150ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);151return implEncrypt(plain, plainOffset, plainLen,152cipher, cipherOffset);153}154155@IntrinsicCandidate156private int implEncrypt(byte[] plain, int plainOffset, int plainLen,157byte[] cipher, int cipherOffset)158{159int endIndex = plainOffset + plainLen;160161for (; plainOffset < endIndex;162plainOffset += blockSize, cipherOffset += blockSize) {163for (int i = 0; i < blockSize; i++) {164k[i] = (byte)(plain[i + plainOffset] ^ r[i]);165}166embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);167System.arraycopy(cipher, cipherOffset, r, 0, blockSize);168}169return plainLen;170}171172/**173* Performs decryption operation.174*175* <p>The input cipher text <code>cipher</code>, starting at176* <code>cipherOffset</code> and ending at177* <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.178* The result is stored in <code>plain</code>, starting at179* <code>plainOffset</code>.180*181* <p>It is also the application's responsibility to make sure that182* <code>init</code> has been called before this method is called.183* (This check is omitted here, to avoid double checking.)184*185* @param cipher the buffer with the input data to be decrypted186* @param cipherOffset the offset in <code>cipherOffset</code>187* @param cipherLen the length of the input data188* @param plain the buffer for the result189* @param plainOffset the offset in <code>plain</code>190* @exception ProviderException if <code>len</code> is not191* a multiple of the block size192* @return the length of the decrypted data193*/194int decrypt(byte[] cipher, int cipherOffset, int cipherLen,195byte[] plain, int plainOffset) {196if (cipherLen <= 0) {197return cipherLen;198}199ArrayUtil.blockSizeCheck(cipherLen, blockSize);200ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen);201ArrayUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen);202return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);203}204205@IntrinsicCandidate206private int implDecrypt(byte[] cipher, int cipherOffset, int cipherLen,207byte[] plain, int plainOffset)208{209int endIndex = cipherOffset + cipherLen;210211for (; cipherOffset < endIndex;212cipherOffset += blockSize, plainOffset += blockSize) {213embeddedCipher.decryptBlock(cipher, cipherOffset, k, 0);214for (int i = 0; i < blockSize; i++) {215plain[i + plainOffset] = (byte)(k[i] ^ r[i]);216}217System.arraycopy(cipher, cipherOffset, r, 0, blockSize);218}219return cipherLen;220}221}222223224