Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/CipherBlockChaining.java
38922 views
/*1* Copyright (c) 1997, 2018, 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;3031/**32* This class represents ciphers in cipher block chaining (CBC) mode.33*34* <p>This mode is implemented independently of a particular cipher.35* Ciphers to which this mode should apply (e.g., DES) must be36* <i>plugged-in</i> using the constructor.37*38* <p>NOTE: This class does not deal with buffering or padding.39*40* @author Gigi Ankeny41*/4243class CipherBlockChaining extends FeedbackCipher {4445/*46* random bytes that are initialized with iv47*/48protected byte[] r;4950/*51* output buffer52*/53private byte[] k;5455// variables for save/restore calls56private byte[] rSave = null;5758CipherBlockChaining(SymmetricCipher embeddedCipher) {59super(embeddedCipher);60k = new byte[blockSize];61r = new byte[blockSize];62}6364/**65* Gets the name of this feedback mode.66*67* @return the string <code>CBC</code>68*/69String getFeedback() {70return "CBC";71}7273/**74* Initializes the cipher in the specified mode with the given key75* and iv.76*77* @param decrypting flag indicating encryption or decryption78* @param algorithm the algorithm name79* @param key the key80* @param iv the iv81*82* @exception InvalidKeyException if the given key is inappropriate for83* initializing this cipher84*/85void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)86throws InvalidKeyException {87if ((key == null) || (iv == null) || (iv.length != blockSize)) {88throw new InvalidKeyException("Internal error");89}90this.iv = iv;91reset();92embeddedCipher.init(decrypting, algorithm, key);93}9495/**96* Resets the iv to its original value.97* This is used when doFinal is called in the Cipher class, so that the98* cipher can be reused (with its original iv).99*/100void reset() {101System.arraycopy(iv, 0, r, 0, blockSize);102}103104/**105* Save the current content of this cipher.106*/107void save() {108if (rSave == null) {109rSave = new byte[blockSize];110}111System.arraycopy(r, 0, rSave, 0, blockSize);112}113114/**115* Restores the content of this cipher to the previous saved one.116*/117void restore() {118System.arraycopy(rSave, 0, r, 0, blockSize);119}120121/**122* Performs encryption operation.123*124* <p>The input plain text <code>plain</code>, starting at125* <code>plainOffset</code> and ending at126* <code>(plainOffset + plainLen - 1)</code>, is encrypted.127* The result is stored in <code>cipher</code>, starting at128* <code>cipherOffset</code>.129*130* @param plain the buffer with the input data to be encrypted131* @param plainOffset the offset in <code>plain</code>132* @param plainLen the length of the input data133* @param cipher the buffer for the result134* @param cipherOffset the offset in <code>cipher</code>135* @exception ProviderException if <code>len</code> is not136* a multiple of the block size137* @return the length of the encrypted data138*/139int encrypt(byte[] plain, int plainOffset, int plainLen,140byte[] cipher, int cipherOffset) {141if (plainLen <= 0) {142return plainLen;143}144RangeUtil.blockSizeCheck(plainLen, blockSize);145RangeUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);146RangeUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);147return implEncrypt(plain, plainOffset, plainLen,148cipher, cipherOffset);149}150151private int implEncrypt(byte[] plain, int plainOffset, int plainLen,152byte[] cipher, int cipherOffset)153{154int endIndex = plainOffset + plainLen;155156for (; plainOffset < endIndex;157plainOffset += blockSize, cipherOffset += blockSize) {158for (int i = 0; i < blockSize; i++) {159k[i] = (byte)(plain[i + plainOffset] ^ r[i]);160}161embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);162System.arraycopy(cipher, cipherOffset, r, 0, blockSize);163}164return plainLen;165}166167/**168* Performs decryption operation.169*170* <p>The input cipher text <code>cipher</code>, starting at171* <code>cipherOffset</code> and ending at172* <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.173* The result is stored in <code>plain</code>, starting at174* <code>plainOffset</code>.175*176* <p>It is also the application's responsibility to make sure that177* <code>init</code> has been called before this method is called.178* (This check is omitted here, to avoid double checking.)179*180* @param cipher the buffer with the input data to be decrypted181* @param cipherOffset the offset in <code>cipherOffset</code>182* @param cipherLen the length of the input data183* @param plain the buffer for the result184* @param plainOffset the offset in <code>plain</code>185* @exception ProviderException if <code>len</code> is not186* a multiple of the block size187* @return the length of the decrypted data188*/189int decrypt(byte[] cipher, int cipherOffset, int cipherLen,190byte[] plain, int plainOffset) {191if (cipherLen <= 0) {192return cipherLen;193}194RangeUtil.blockSizeCheck(cipherLen, blockSize);195RangeUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen);196RangeUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen);197return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);198}199200private int implDecrypt(byte[] cipher, int cipherOffset, int cipherLen,201byte[] plain, int plainOffset)202{203int endIndex = cipherOffset + cipherLen;204205for (; cipherOffset < endIndex;206cipherOffset += blockSize, plainOffset += blockSize) {207embeddedCipher.decryptBlock(cipher, cipherOffset, k, 0);208for (int i = 0; i < blockSize; i++) {209plain[i + plainOffset] = (byte)(k[i] ^ r[i]);210}211System.arraycopy(cipher, cipherOffset, r, 0, blockSize);212}213return cipherLen;214}215}216217218