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/A
0N/A/**
0N/A * This class represents ciphers in cipher-feedback (CFB) mode.
0N/A *
0N/A * <p>This mode is implemented independently of a particular cipher.
0N/A * Ciphers to which this mode should apply (e.g., DES) must be
0N/A * <i>plugged-in</i> using the constructor.
0N/A *
0N/A * <p>NOTE: This class does not deal with buffering or padding.
0N/A *
0N/A * @author Gigi Ankeny
0N/A */
0N/A
0N/Afinal class CipherFeedback extends FeedbackCipher {
0N/A
0N/A /*
0N/A * encrypt/decrypt output buffer
0N/A */
0N/A private final byte[] k;
0N/A
0N/A /*
0N/A * register value, initialized with iv
0N/A */
0N/A private final byte[] register;
0N/A
0N/A /*
0N/A * number of bytes for each stream unit, defaults to the blocksize
0N/A * of the embedded cipher
0N/A */
0N/A private int numBytes;
0N/A
0N/A // variables for save/restore calls
0N/A private byte[] registerSave = null;
0N/A
0N/A CipherFeedback(SymmetricCipher embeddedCipher, int numBytes) {
0N/A super(embeddedCipher);
0N/A if (numBytes > blockSize) {
0N/A numBytes = blockSize;
0N/A }
0N/A this.numBytes = numBytes;
0N/A k = new byte[blockSize];
0N/A register = new byte[blockSize];
0N/A }
0N/A
0N/A /**
0N/A * Gets the name of this feedback mode.
0N/A *
0N/A * @return the string <code>CFB</code>
0N/A */
0N/A String getFeedback() {
0N/A return "CFB";
0N/A }
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
0N/A * @param algorithm the algorithm name
0N/A * @param key the key
0N/A * @param iv the iv
0N/A *
0N/A * @exception InvalidKeyException if the given key is inappropriate for
0N/A * initializing this cipher
0N/A */
0N/A void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
0N/A throws InvalidKeyException {
0N/A if ((key == null) || (iv == null) || (iv.length != blockSize)) {
0N/A throw new InvalidKeyException("Internal error");
0N/A }
0N/A this.iv = iv;
0N/A reset();
0N/A // always encrypt mode for embedded cipher
0N/A embeddedCipher.init(false, algorithm, key);
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 void reset() {
0N/A System.arraycopy(iv, 0, register, 0, blockSize);
0N/A }
0N/A
0N/A /**
0N/A * Save the current content of this cipher.
0N/A */
0N/A void save() {
0N/A if (registerSave == null) {
0N/A registerSave = new byte[blockSize];
0N/A }
0N/A System.arraycopy(register, 0, registerSave, 0, blockSize);
0N/A }
0N/A
0N/A /**
0N/A * Restores the content of this cipher to the previous saved one.
0N/A */
0N/A void restore() {
0N/A System.arraycopy(registerSave, 0, register, 0, blockSize);
0N/A }
0N/A
0N/A /**
0N/A * Performs encryption operation.
0N/A *
0N/A * <p>The input plain text <code>plain</code>, starting at
0N/A * <code>plainOffset</code> and ending at
0N/A * <code>(plainOffset + len - 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>It is the application's responsibility to make sure that
0N/A * <code>plainLen</code> is a multiple of the stream unit size
0N/A * <code>numBytes</code>, as any excess bytes are ignored.
0N/A *
0N/A * <p>It is also the application's responsibility to make sure that
0N/A * <code>init</code> has been called before this method is called.
0N/A * (This check is omitted here, to avoid double checking.)
0N/A *
0N/A * @param plain the buffer with the input 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 result
0N/A * @param cipherOffset the offset in <code>cipher</code>
0N/A */
0N/A void encrypt(byte[] plain, int plainOffset, int plainLen,
0N/A byte[] cipher, int cipherOffset)
0N/A {
0N/A int i, len;
0N/A len = blockSize - numBytes;
0N/A int loopCount = plainLen / numBytes;
0N/A int oddBytes = plainLen % numBytes;
0N/A
0N/A if (len == 0) {
0N/A for (; loopCount > 0 ;
0N/A plainOffset += numBytes, cipherOffset += numBytes,
0N/A loopCount--) {
0N/A embeddedCipher.encryptBlock(register, 0, k, 0);
0N/A for (i = 0; i < blockSize; i++)
0N/A register[i] = cipher[i+cipherOffset] =
0N/A (byte)(k[i] ^ plain[i+plainOffset]);
0N/A }
0N/A if (oddBytes > 0) {
0N/A embeddedCipher.encryptBlock(register, 0, k, 0);
0N/A for (i=0; i<oddBytes; i++)
0N/A register[i] = cipher[i+cipherOffset] =
0N/A (byte)(k[i] ^ plain[i+plainOffset]);
0N/A }
0N/A } else {
0N/A for (; loopCount > 0 ;
0N/A plainOffset += numBytes, cipherOffset += numBytes,
0N/A loopCount--) {
0N/A embeddedCipher.encryptBlock(register, 0, k, 0);
0N/A System.arraycopy(register, numBytes, register, 0, len);
0N/A for (i=0; i<numBytes; i++)
0N/A register[i+len] = cipher[i+cipherOffset] =
0N/A (byte)(k[i] ^ plain[i+plainOffset]);
0N/A
0N/A }
0N/A if (oddBytes != 0) {
0N/A embeddedCipher.encryptBlock(register, 0, k, 0);
0N/A System.arraycopy(register, numBytes, register, 0, len);
0N/A for (i=0; i<oddBytes; i++) {
0N/A register[i+len] = cipher[i+cipherOffset] =
0N/A (byte)(k[i] ^ plain[i+plainOffset]);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Performs decryption operation.
0N/A *
0N/A * <p>The input cipher text <code>cipher</code>, starting at
0N/A * <code>cipherOffset</code> and ending at
0N/A * <code>(cipherOffset + len - 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>It is the application's responsibility to make sure that
0N/A * <code>cipherLen</code> is a multiple of the stream unit size
0N/A * <code>numBytes</code>, as any excess bytes are ignored.
0N/A *
0N/A * <p>It is also the application's responsibility to make sure that
0N/A * <code>init</code> has been called before this method is called.
0N/A * (This check is omitted here, to avoid double checking.)
0N/A *
0N/A * @param cipher the buffer with the input data to be decrypted
0N/A * @param cipherOffset the offset in <code>cipherOffset</code>
0N/A * @param cipherLen the length of the input data
0N/A * @param plain the buffer for the result
0N/A * @param plainOffset the offset in <code>plain</code>
0N/A */
0N/A void decrypt(byte[] cipher, int cipherOffset, int cipherLen,
0N/A byte[] plain, int plainOffset)
0N/A {
0N/A int i, len;
0N/A len = blockSize - numBytes;
0N/A int loopCount = cipherLen / numBytes;
0N/A int oddBytes = cipherLen % numBytes;
0N/A
0N/A if (len == 0) {
0N/A for (; loopCount > 0;
0N/A plainOffset += numBytes, cipherOffset += numBytes,
0N/A loopCount--) {
0N/A embeddedCipher.encryptBlock(register, 0, k, 0);
0N/A for (i = 0; i < blockSize; i++) {
0N/A register[i] = cipher[i+cipherOffset];
0N/A plain[i+plainOffset]
0N/A = (byte)(cipher[i+cipherOffset] ^ k[i]);
0N/A }
0N/A }
0N/A if (oddBytes > 0) {
0N/A embeddedCipher.encryptBlock(register, 0, k, 0);
0N/A for (i=0; i<oddBytes; i++) {
0N/A register[i] = cipher[i+cipherOffset];
0N/A plain[i+plainOffset]
0N/A = (byte)(cipher[i+cipherOffset] ^ k[i]);
0N/A }
0N/A }
0N/A } else {
0N/A for (; loopCount > 0;
0N/A plainOffset += numBytes, cipherOffset += numBytes,
0N/A loopCount--) {
0N/A embeddedCipher.encryptBlock(register, 0, k, 0);
0N/A System.arraycopy(register, numBytes, register, 0, len);
0N/A for (i=0; i<numBytes; i++) {
0N/A register[i+len] = cipher[i+cipherOffset];
0N/A plain[i+plainOffset]
0N/A = (byte)(cipher[i+cipherOffset] ^ k[i]);
0N/A }
0N/A }
0N/A if (oddBytes != 0) {
0N/A embeddedCipher.encryptBlock(register, 0, k, 0);
0N/A System.arraycopy(register, numBytes, register, 0, len);
0N/A for (i=0; i<oddBytes; i++) {
0N/A register[i+len] = cipher[i+cipherOffset];
0N/A plain[i+plainOffset]
0N/A = (byte)(cipher[i+cipherOffset] ^ k[i]);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}