Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/FeedbackCipher.java
67848 views
/*1* Copyright (c) 1997, 2021, 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.InvalidAlgorithmParameterException;29import javax.crypto.*;3031/**32* This class represents a block cipher in one of its modes. It wraps33* a SymmetricCipher maintaining the mode state and providing34* the capability to encrypt amounts of data larger than a single block.35*36* @author Jan Luehe37* @see ElectronicCodeBook38* @see CipherBlockChaining39* @see CipherFeedback40* @see OutputFeedback41* @see PCBC42*/43abstract class FeedbackCipher {4445// the embedded block cipher46final SymmetricCipher embeddedCipher;4748// the block size of the embedded block cipher49final int blockSize;5051// the initialization vector52byte[] iv;5354FeedbackCipher(SymmetricCipher embeddedCipher) {55this.embeddedCipher = embeddedCipher;56blockSize = embeddedCipher.getBlockSize();57}5859final SymmetricCipher getEmbeddedCipher() {60return embeddedCipher;61}6263/**64* Gets the block size of the embedded cipher.65*66* @return the block size of the embedded cipher67*/68final int getBlockSize() {69return blockSize;70}7172/**73* Gets the name of the feedback mechanism74*75* @return the name of the feedback mechanism76*/77abstract String getFeedback();7879/**80* Save the current content of this cipher.81*/82abstract void save();8384/**85* Restores the content of this cipher to the previous saved one.86*/87abstract void restore();8889/**90* Initializes the cipher in the specified mode with the given key91* and iv.92*93* @param decrypting flag indicating encryption or decryption mode94* @param algorithm the algorithm name (never null)95* @param key the key (never null)96* @param iv the iv (either null or blockSize bytes long)97*98* @exception InvalidKeyException if the given key is inappropriate for99* initializing this cipher100*/101abstract void init(boolean decrypting, String algorithm, byte[] key,102byte[] iv) throws InvalidKeyException,103InvalidAlgorithmParameterException;104105/**106* Gets the initialization vector.107*108* @return the initialization vector109*/110final byte[] getIV() {111return iv;112}113114/**115* Resets the iv to its original value.116* This is used when doFinal is called in the Cipher class, so that the117* cipher can be reused (with its original iv).118*/119abstract void reset();120121/**122* Performs encryption operation.123*124* <p>The input <code>plain</code>, starting at <code>plainOffset</code>125* and ending at <code>(plainOffset+plainLen-1)</code>, is encrypted.126* The result is stored in <code>cipher</code>, starting at127* <code>cipherOffset</code>.128*129* <p>The subclass that implements Cipher should ensure that130* <code>init</code> has been called before this method is called.131*132* @param plain the input buffer with the data to be encrypted133* @param plainOffset the offset in <code>plain</code>134* @param plainLen the length of the input data135* @param cipher the buffer for the encryption result136* @param cipherOffset the offset in <code>cipher</code>137* @return the number of bytes placed into <code>cipher</code>138*/139abstract int encrypt(byte[] plain, int plainOffset, int plainLen,140byte[] cipher, int cipherOffset);141/**142* Performs encryption operation for the last time.143*144* <p>NOTE: For cipher feedback modes which does not perform145* special handling for the last few blocks, this is essentially146* the same as <code>encrypt(...)</code>. Given most modes do147* not do special handling, the default impl for this method is148* to simply call <code>encrypt(...)</code>.149*150* @param plain the input buffer with the data to be encrypted151* @param plainOffset the offset in <code>plain</code>152* @param plainLen the length of the input data153* @param cipher the buffer for the encryption result154* @param cipherOffset the offset in <code>cipher</code>155* @return the number of bytes placed into <code>cipher</code>156*/157int encryptFinal(byte[] plain, int plainOffset, int plainLen,158byte[] cipher, int cipherOffset)159throws IllegalBlockSizeException, ShortBufferException {160return encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);161}162/**163* Performs decryption operation.164*165* <p>The input <code>cipher</code>, starting at <code>cipherOffset</code>166* and ending at <code>(cipherOffset+cipherLen-1)</code>, is decrypted.167* The result is stored in <code>plain</code>, starting at168* <code>plainOffset</code>.169*170* <p>The subclass that implements Cipher should ensure that171* <code>init</code> has been called before this method is called.172*173* @param cipher the input buffer with the data to be decrypted174* @param cipherOffset the offset in <code>cipher</code>175* @param cipherLen the length of the input data176* @param plain the buffer for the decryption result177* @param plainOffset the offset in <code>plain</code>178* @return the number of bytes placed into <code>plain</code>179*/180abstract int decrypt(byte[] cipher, int cipherOffset, int cipherLen,181byte[] plain, int plainOffset);182183/**184* Performs decryption operation for the last time.185*186* <p>NOTE: For cipher feedback modes which does not perform187* special handling for the last few blocks, this is essentially188* the same as <code>encrypt(...)</code>. Given most modes do189* not do special handling, the default impl for this method is190* to simply call <code>decrypt(...)</code>.191*192* @param cipher the input buffer with the data to be decrypted193* @param cipherOffset the offset in <code>cipher</code>194* @param cipherLen the length of the input data195* @param plain the buffer for the decryption result196* @param plainOffset the offset in <code>plain</code>197* @return the number of bytes placed into <code>plain</code>198*/199int decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,200byte[] plain, int plainOffset)201throws IllegalBlockSizeException, ShortBufferException {202return decrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);203}204}205206207