0N/A/*
2362N/A * Copyright (c) 2004, 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 javax.crypto.IllegalBlockSizeException;
0N/Aimport javax.crypto.ShortBufferException;
0N/A
0N/A/**
0N/A * This class represents ciphers in cipher text stealing (CTS) mode.
0N/A * <br>CTS provides a way to allow block ciphers to operate on partial
0N/A * blocks without padding, and all bits of the message go through
0N/A * the encryption algorithm, rather than simply being XOR'd.
0N/A * <br>More details can be found in RFC 2040 section 8 "Description
0N/A * of RC5-CTS".
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#1: CTS requires the input data to be at least one block
0N/A * long. Thus, callers of this class has to buffer the input data
0N/A * to make sure the input data passed to encryptFinal()/decryptFinal()
0N/A * is not shorter than a block.
0N/A * <p>NOTE#2: This class does not deal with buffering or padding
0N/A * just like all other cipher mode implementations.
0N/A *
0N/A * @author Valerie Peng
0N/A */
0N/A
0N/Afinal class CipherTextStealing extends CipherBlockChaining {
0N/A
0N/A CipherTextStealing(SymmetricCipher embeddedCipher) {
0N/A super(embeddedCipher);
0N/A }
0N/A
0N/A /**
0N/A * Gets the name of this feedback mode.
0N/A *
0N/A * @return the string <code>CBC</code>
0N/A */
0N/A String getFeedback() {
0N/A return "CTS";
0N/A }
0N/A
0N/A /**
0N/A * Performs the last 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 embedded cipher's block size,
0N/A * as any excess bytes are ignored.
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 encryptFinal(byte[] plain, int plainOffset, int plainLen,
0N/A byte[] cipher, int cipherOffset)
0N/A throws IllegalBlockSizeException {
0N/A
0N/A if (plainLen < blockSize) {
0N/A throw new IllegalBlockSizeException("input is too short!");
0N/A } else if (plainLen == blockSize) {
0N/A encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);
0N/A } else {
0N/A // number of bytes in the last block
0N/A int nLeft = plainLen % blockSize;
0N/A if (nLeft == 0) {
0N/A encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);
0N/A // swap the last two blocks after encryption
0N/A int lastBlkIndex = cipherOffset + plainLen - blockSize;
0N/A int nextToLastBlkIndex = lastBlkIndex - blockSize;
0N/A byte[] tmp = new byte[blockSize];
0N/A System.arraycopy(cipher, lastBlkIndex, tmp, 0, blockSize);
0N/A System.arraycopy(cipher, nextToLastBlkIndex,
0N/A cipher, lastBlkIndex, blockSize);
0N/A System.arraycopy(tmp, 0, cipher, nextToLastBlkIndex,
0N/A blockSize);
0N/A } else {
0N/A int newPlainLen = plainLen - (blockSize + nLeft);
0N/A if (newPlainLen > 0) {
0N/A encrypt(plain, plainOffset, newPlainLen, cipher,
0N/A cipherOffset);
0N/A plainOffset += newPlainLen;
0N/A cipherOffset += newPlainLen;
0N/A }
0N/A
0N/A // Do final CTS step for last two blocks (the second of which
0N/A // may or may not be incomplete).
0N/A byte[] tmp = new byte[blockSize];
0N/A // now encrypt the next-to-last block
0N/A for (int i = 0; i < blockSize; i++) {
0N/A tmp[i] = (byte) (plain[plainOffset+i] ^ r[i]);
0N/A }
0N/A byte[] tmp2 = new byte[blockSize];
0N/A embeddedCipher.encryptBlock(tmp, 0, tmp2, 0);
0N/A System.arraycopy(tmp2, 0, cipher,
0N/A cipherOffset+blockSize, nLeft);
0N/A // encrypt the last block
0N/A for (int i=0; i<nLeft; i++) {
0N/A tmp2[i] = (byte)
0N/A (plain[plainOffset+blockSize+i] ^ tmp2[i]);
0N/A }
0N/A embeddedCipher.encryptBlock(tmp2, 0, cipher, cipherOffset);
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 embedded cipher's block
0N/A * size, 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 decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,
0N/A byte[] plain, int plainOffset)
0N/A throws IllegalBlockSizeException {
0N/A if (cipherLen < blockSize) {
0N/A throw new IllegalBlockSizeException("input is too short!");
0N/A } else if (cipherLen == blockSize) {
0N/A decrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
0N/A } else {
0N/A // number of bytes in the last block
0N/A int nLeft = cipherLen % blockSize;
0N/A if (nLeft == 0) {
0N/A // swap the last two blocks before decryption
0N/A int lastBlkIndex = cipherOffset + cipherLen - blockSize;
0N/A int nextToLastBlkIndex =
0N/A cipherOffset + cipherLen - 2*blockSize;
0N/A byte[] tmp = new byte[2*blockSize];
0N/A System.arraycopy(cipher, lastBlkIndex, tmp, 0, blockSize);
0N/A System.arraycopy(cipher, nextToLastBlkIndex,
0N/A tmp, blockSize, blockSize);
0N/A int cipherLen2 = cipherLen-2*blockSize;
0N/A decrypt(cipher, cipherOffset, cipherLen2, plain, plainOffset);
0N/A decrypt(tmp, 0, 2*blockSize, plain, plainOffset+cipherLen2);
0N/A } else {
0N/A int newCipherLen = cipherLen-(blockSize+nLeft);
0N/A if (newCipherLen > 0) {
0N/A decrypt(cipher, cipherOffset, newCipherLen, plain,
0N/A plainOffset);
0N/A cipherOffset += newCipherLen;
0N/A plainOffset += newCipherLen;
0N/A }
0N/A // Do final CTS step for last two blocks (the second of which
0N/A // may or may not be incomplete).
0N/A
0N/A // now decrypt the next-to-last block
0N/A byte[] tmp = new byte[blockSize];
0N/A embeddedCipher.decryptBlock(cipher, cipherOffset, tmp, 0);
0N/A for (int i = 0; i < nLeft; i++) {
0N/A plain[plainOffset+blockSize+i] =
0N/A (byte) (cipher[cipherOffset+blockSize+i] ^ tmp[i]);
0N/A }
0N/A
0N/A // decrypt the last block
0N/A System.arraycopy(cipher, cipherOffset+blockSize, tmp, 0,
0N/A nLeft);
0N/A embeddedCipher.decryptBlock(tmp, 0, plain, plainOffset);
0N/A //System.arraycopy(r, 0, tmp, 0, r.length);
0N/A for (int i=0; i<blockSize; i++) {
0N/A plain[plainOffset+i] = (byte)
0N/A (plain[plainOffset+i]^r[i]);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}