Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/FeedbackCipher.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.nio.ByteBuffer;28import java.security.InvalidKeyException;29import java.security.InvalidAlgorithmParameterException;30import javax.crypto.*;3132/**33* This class represents a block cipher in one of its modes. It wraps34* a SymmetricCipher maintaining the mode state and providing35* the capability to encrypt amounts of data larger than a single block.36*37* @author Jan Luehe38* @see ElectronicCodeBook39* @see CipherBlockChaining40* @see CipherFeedback41* @see OutputFeedback42* @see PCBC43*/44abstract class FeedbackCipher {4546// the embedded block cipher47final SymmetricCipher embeddedCipher;4849// the block size of the embedded block cipher50final int blockSize;5152// the initialization vector53byte[] iv;5455FeedbackCipher(SymmetricCipher embeddedCipher) {56this.embeddedCipher = embeddedCipher;57blockSize = embeddedCipher.getBlockSize();58}5960final SymmetricCipher getEmbeddedCipher() {61return embeddedCipher;62}6364/**65* Gets the block size of the embedded cipher.66*67* @return the block size of the embedded cipher68*/69final int getBlockSize() {70return blockSize;71}7273/**74* Gets the name of the feedback mechanism75*76* @return the name of the feedback mechanism77*/78abstract String getFeedback();7980/**81* Save the current content of this cipher.82*/83abstract void save();8485/**86* Restores the content of this cipher to the previous saved one.87*/88abstract void restore();8990/**91* Initializes the cipher in the specified mode with the given key92* and iv.93*94* @param decrypting flag indicating encryption or decryption mode95* @param algorithm the algorithm name (never null)96* @param key the key (never null)97* @param iv the iv (either null or blockSize bytes long)98*99* @exception InvalidKeyException if the given key is inappropriate for100* initializing this cipher101*/102abstract void init(boolean decrypting, String algorithm, byte[] key,103byte[] iv) throws InvalidKeyException,104InvalidAlgorithmParameterException;105106/**107* Gets the initialization vector.108*109* @return the initialization vector110*/111final byte[] getIV() {112return iv;113}114115/**116* Resets the iv to its original value.117* This is used when doFinal is called in the Cipher class, so that the118* cipher can be reused (with its original iv).119*/120abstract void reset();121122/**123* Performs encryption operation.124*125* <p>The input <code>plain</code>, starting at <code>plainOffset</code>126* and ending at <code>(plainOffset+plainLen-1)</code>, is encrypted.127* The result is stored in <code>cipher</code>, starting at128* <code>cipherOffset</code>.129*130* <p>The subclass that implements Cipher should ensure that131* <code>init</code> has been called before this method is called.132*133* @param plain the input buffer with the data to be encrypted134* @param plainOffset the offset in <code>plain</code>135* @param plainLen the length of the input data136* @param cipher the buffer for the encryption result137* @param cipherOffset the offset in <code>cipher</code>138* @return the number of bytes placed into <code>cipher</code>139*/140abstract int encrypt(byte[] plain, int plainOffset, int plainLen,141byte[] cipher, int cipherOffset);142/**143* Performs encryption operation for the last time.144*145* <p>NOTE: For cipher feedback modes which does not perform146* special handling for the last few blocks, this is essentially147* the same as <code>encrypt(...)</code>. Given most modes do148* not do special handling, the default impl for this method is149* to simply call <code>encrypt(...)</code>.150*151* @param plain the input buffer with the data to be encrypted152* @param plainOffset the offset in <code>plain</code>153* @param plainLen the length of the input data154* @param cipher the buffer for the encryption result155* @param cipherOffset the offset in <code>cipher</code>156* @return the number of bytes placed into <code>cipher</code>157*/158int encryptFinal(byte[] plain, int plainOffset, int plainLen,159byte[] cipher, int cipherOffset)160throws IllegalBlockSizeException, ShortBufferException {161return encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);162}163/**164* Performs decryption operation.165*166* <p>The input <code>cipher</code>, starting at <code>cipherOffset</code>167* and ending at <code>(cipherOffset+cipherLen-1)</code>, is decrypted.168* The result is stored in <code>plain</code>, starting at169* <code>plainOffset</code>.170*171* <p>The subclass that implements Cipher should ensure that172* <code>init</code> has been called before this method is called.173*174* @param cipher the input buffer with the data to be decrypted175* @param cipherOffset the offset in <code>cipher</code>176* @param cipherLen the length of the input data177* @param plain the buffer for the decryption result178* @param plainOffset the offset in <code>plain</code>179* @return the number of bytes placed into <code>plain</code>180*/181abstract int decrypt(byte[] cipher, int cipherOffset, int cipherLen,182byte[] plain, int plainOffset);183184/**185* Performs decryption operation for the last time.186*187* <p>NOTE: For cipher feedback modes which does not perform188* special handling for the last few blocks, this is essentially189* the same as <code>encrypt(...)</code>. Given most modes do190* not do special handling, the default impl for this method is191* to simply call <code>decrypt(...)</code>.192*193* @param cipher the input buffer with the data to be decrypted194* @param cipherOffset the offset in <code>cipher</code>195* @param cipherLen the length of the input data196* @param plain the buffer for the decryption result197* @param plainOffset the offset in <code>plain</code>198* @return the number of bytes placed into <code>plain</code>199*/200int decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,201byte[] plain, int plainOffset)202throws IllegalBlockSizeException, AEADBadTagException,203ShortBufferException {204return decrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);205}206207/**208* Continues a multi-part update of the Additional Authentication209* Data (AAD), using a subset of the provided buffer. If this210* cipher is operating in either GCM or CCM mode, all AAD must be211* supplied before beginning operations on the ciphertext (via the212* {@code update} and {@code doFinal} methods).213* <p>214* NOTE: Given most modes do not accept AAD, default impl for this215* method throws IllegalStateException.216*217* @param src the buffer containing the AAD218* @param offset the offset in {@code src} where the AAD input starts219* @param len the number of AAD bytes220*221* @throws IllegalStateException if this cipher is in a wrong state222* (e.g., has not been initialized), does not accept AAD, or if223* operating in either GCM or CCM mode and one of the {@code update}224* methods has already been called for the active225* encryption/decryption operation226* @throws UnsupportedOperationException if this method227* has not been overridden by an implementation228*229* @since 1.8230*/231void updateAAD(byte[] src, int offset, int len) {232throw new IllegalStateException("No AAD accepted");233}234235/**236* @return the number of bytes that are buffered internally inside237* this FeedbackCipher instance.238* @since 1.8239*/240int getBufferedLength() {241// Currently only AEAD cipher impl, e.g. GCM, buffers data242// internally during decryption mode243return 0;244}245246/*247* ByteBuffer methods should not be accessed as CipherCore and AESCipher248* copy the data to byte arrays. These methods are to satisfy the compiler.249*/250int encrypt(ByteBuffer src, ByteBuffer dst) {251throw new UnsupportedOperationException("ByteBuffer not supported");252};253254int decrypt(ByteBuffer src, ByteBuffer dst) {255throw new UnsupportedOperationException("ByteBuffer not supported");256};257258int encryptFinal(ByteBuffer src, ByteBuffer dst)259throws IllegalBlockSizeException, ShortBufferException {260throw new UnsupportedOperationException("ByteBuffer not supported");261};262263int decryptFinal(ByteBuffer src, ByteBuffer dst)264throws IllegalBlockSizeException, AEADBadTagException,265ShortBufferException {266throw new UnsupportedOperationException("ByteBuffer not supported");267}268}269270271