0N/A/*
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.crypto.provider;
0N/A
0N/Aimport java.security.InvalidKeyException;
0N/Aimport javax.crypto.IllegalBlockSizeException;
0N/A
0N/A/**
0N/A * This class represents a block cipher in one of its modes. It wraps
0N/A * a SymmetricCipher maintaining the mode state and providing
0N/A * the capability to encrypt amounts of data larger than a single block.
0N/A *
0N/A * @author Jan Luehe
0N/A * @see ElectronicCodeBook
0N/A * @see CipherBlockChaining
0N/A * @see CipherFeedback
0N/A * @see OutputFeedback
0N/A * @see PCBC
0N/A */
0N/Aabstract class FeedbackCipher {
0N/A
0N/A // the embedded block cipher
0N/A final SymmetricCipher embeddedCipher;
0N/A
0N/A // the block size of the embedded block cipher
0N/A final int blockSize;
0N/A
0N/A // the initialization vector
0N/A byte[] iv;
0N/A
0N/A FeedbackCipher(SymmetricCipher embeddedCipher) {
0N/A this.embeddedCipher = embeddedCipher;
0N/A blockSize = embeddedCipher.getBlockSize();
0N/A }
0N/A
0N/A final SymmetricCipher getEmbeddedCipher() {
0N/A return embeddedCipher;
0N/A }
0N/A
0N/A /**
0N/A * Gets the block size of the embedded cipher.
0N/A *
0N/A * @return the block size of the embedded cipher
0N/A */
0N/A final int getBlockSize() {
0N/A return blockSize;
0N/A }
0N/A
0N/A /**
0N/A * Gets the name of the feedback mechanism
0N/A *
0N/A * @return the name of the feedback mechanism
0N/A */
0N/A abstract String getFeedback();
0N/A
0N/A /**
0N/A * Save the current content of this cipher.
0N/A */
0N/A abstract void save();
0N/A
0N/A /**
0N/A * Restores the content of this cipher to the previous saved one.
0N/A */
0N/A abstract void restore();
0N/A
0N/A /**
0N/A * Initializes the cipher in the specified mode with the given key
0N/A * and iv.
0N/A *
0N/A * @param decrypting flag indicating encryption or decryption mode
0N/A * @param algorithm the algorithm name (never null)
0N/A * @param key the key (never null)
0N/A * @param iv the iv (either null or blockSize bytes long)
0N/A *
0N/A * @exception InvalidKeyException if the given key is inappropriate for
0N/A * initializing this cipher
0N/A */
0N/A abstract void init(boolean decrypting, String algorithm, byte[] key,
0N/A byte[] iv) throws InvalidKeyException;
0N/A
0N/A /**
0N/A * Gets the initialization vector.
0N/A *
0N/A * @return the initialization vector
0N/A */
0N/A final byte[] getIV() {
0N/A return iv;
0N/A }
0N/A
0N/A /**
0N/A * Resets the iv to its original value.
0N/A * This is used when doFinal is called in the Cipher class, so that the
0N/A * cipher can be reused (with its original iv).
0N/A */
0N/A abstract void reset();
0N/A
0N/A /**
0N/A * Performs encryption operation.
0N/A *
0N/A * <p>The input <code>plain</code>, starting at <code>plainOffset</code>
0N/A * and ending at <code>(plainOffset+plainLen-1)</code>, is encrypted.
0N/A * The result is stored in <code>cipher</code>, starting at
0N/A * <code>cipherOffset</code>.
0N/A *
0N/A * <p>The subclass that implements Cipher should ensure that
0N/A * <code>init</code> has been called before this method is called.
0N/A *
0N/A * @param plain the input buffer with the data to be encrypted
0N/A * @param plainOffset the offset in <code>plain</code>
0N/A * @param plainLen the length of the input data
0N/A * @param cipher the buffer for the encryption result
0N/A * @param cipherOffset the offset in <code>cipher</code>
0N/A */
0N/A abstract void encrypt(byte[] plain, int plainOffset, int plainLen,
0N/A byte[] cipher, int cipherOffset);
0N/A /**
0N/A * Performs encryption operation for the last time.
0N/A *
0N/A * <p>NOTE: For cipher feedback modes which does not perform
0N/A * special handling for the last few blocks, this is essentially
0N/A * the same as <code>encrypt(...)</code>. Given most modes do
0N/A * not do special handling, the default impl for this method is
0N/A * to simply call <code>encrypt(...)</code>.
0N/A *
0N/A * @param plain the input buffer with the data to be encrypted
0N/A * @param plainOffset the offset in <code>plain</code>
0N/A * @param plainLen the length of the input data
0N/A * @param cipher the buffer for the encryption result
0N/A * @param cipherOffset the offset in <code>cipher</code>
0N/A */
0N/A void encryptFinal(byte[] plain, int plainOffset, int plainLen,
0N/A byte[] cipher, int cipherOffset)
0N/A throws IllegalBlockSizeException {
0N/A encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);
0N/A }
0N/A /**
0N/A * Performs decryption operation.
0N/A *
0N/A * <p>The input <code>cipher</code>, starting at <code>cipherOffset</code>
0N/A * and ending at <code>(cipherOffset+cipherLen-1)</code>, is decrypted.
0N/A * The result is stored in <code>plain</code>, starting at
0N/A * <code>plainOffset</code>.
0N/A *
0N/A * <p>The subclass that implements Cipher should ensure that
0N/A * <code>init</code> has been called before this method is called.
0N/A *
0N/A * @param cipher the input buffer with the data to be decrypted
0N/A * @param cipherOffset the offset in <code>cipher</code>
0N/A * @param cipherLen the length of the input data
0N/A * @param plain the buffer for the decryption result
0N/A * @param plainOffset the offset in <code>plain</code>
0N/A */
0N/A abstract void decrypt(byte[] cipher, int cipherOffset, int cipherLen,
0N/A byte[] plain, int plainOffset);
0N/A
0N/A /**
0N/A * Performs decryption operation for the last time.
0N/A *
0N/A * <p>NOTE: For cipher feedback modes which does not perform
0N/A * special handling for the last few blocks, this is essentially
0N/A * the same as <code>encrypt(...)</code>. Given most modes do
0N/A * not do special handling, the default impl for this method is
0N/A * to simply call <code>decrypt(...)</code>.
0N/A *
0N/A * @param cipher the input buffer with the data to be decrypted
0N/A * @param cipherOffset the offset in <code>cipher</code>
0N/A * @param cipherLen the length of the input data
0N/A * @param plain the buffer for the decryption result
0N/A * @param plainOffset the offset in <code>plain</code>
0N/A */
0N/A void decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,
0N/A byte[] plain, int plainOffset)
0N/A throws IllegalBlockSizeException {
0N/A decrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
0N/A }
0N/A}