00001 // CbcBlockCipher - use a block cipher in CBC mode 00002 // 00003 // Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved. 00004 // 00005 // Redistribution and use in source and binary forms, with or without 00006 // modification, are permitted provided that the following conditions 00007 // are met: 00008 // 1. Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // 2. Redistributions in binary form must reproduce the above copyright 00011 // notice, this list of conditions and the following disclaimer in the 00012 // documentation and/or other materials provided with the distribution. 00013 // 00014 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 00015 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00017 // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00018 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00019 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00020 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00021 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00022 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00023 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00024 // SUCH DAMAGE. 00025 // 00026 // Visit the ACME Labs Java page for up-to-date versions of this and other 00027 // fine Java utilities: http://www.acme.com/java/ 00028 00029 package Acme.Crypto; 00030 00031 import java.io.*; 00032 00034 // <P> 00035 // A plain old block cipher, key and cleartext-block in, ciphertext-block 00036 // out, is said to be in Electronic Code Book (ECB) mode. A given block 00037 // of plaintext always encrypts to the same block of ciphertext. This 00038 // makes it somewhat vulnerable to known plaintext attacks, block replay 00039 // attacks, etc. 00040 // <P> 00041 // A fairly cheap alternative is to use it in Cipher Block Chaining (CBC) 00042 // mode. All this does is XOR each plaintext block with the previous 00043 // ciphertext block before encryption. For the first block, where there 00044 // is no previous ciphertext block, a caller-specified Initialization 00045 // Vector (IV) is used for the XOR. This makes each block's encryption 00046 // depend on all the previous blocks 00047 // <P> 00048 // This class lets you use any given block cipher in CBC mode. 00049 // <P> 00050 // <A HREF="/resources/classes/Acme/Crypto/CbcBlockCipher.java">Fetch the software.</A><BR> 00051 // <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A> 00052 // <P> 00053 // @see Cipher 00054 // @see BlockCipher 00055 // @see StreamCipher 00056 // @see EncryptedOutputStream 00057 // @see EncryptedInputStream 00058 00059 public class CbcBlockCipher extends BlockCipher 00060 { 00061 00062 private BlockCipher blockCipher; 00063 private byte[] iv; 00064 private byte[] temp; 00065 00067 public CbcBlockCipher( BlockCipher blockCipher ) 00068 { 00069 super( blockCipher.keySize(), blockCipher.blockSize() ); 00070 this.blockCipher = blockCipher; 00071 iv = new byte[blockSize()]; 00072 zeroBlock( iv ); 00073 temp = new byte[blockSize()]; 00074 } 00075 00076 00077 // Key routines. 00078 00079 // Set the key. 00080 public void setKey( byte[] key ) 00081 { 00082 blockCipher.setKey( key ); 00083 } 00084 00085 00086 // IV routines. 00087 00089 public void setIv( byte[] iv ) 00090 { 00091 copyBlock( iv, this.iv ); 00092 } 00093 00095 // In CBC mode, the IV does not have to be kept secret. 00096 // Typical usage is for the caller to set a random IV and then transmit 00097 // it as the first block of the message. 00098 public byte[] setRandomIv() 00099 { 00100 byte[] riv = new byte[blockSize()]; 00101 randomBlock( riv ); 00102 setIv( riv ); 00103 return riv; 00104 } 00105 00106 00107 // Block encryption routines. 00108 00110 public void encrypt( byte[] clearText, int clearOff, byte[] cipherText, int cipherOff ) 00111 { 00112 xorBlock( clearText, clearOff, iv, 0, temp, 0, blockSize ); 00113 blockCipher.encrypt( temp, 0, cipherText, cipherOff ); 00114 copyBlock( cipherText, cipherOff, iv, 0, blockSize ); 00115 } 00116 00118 public void decrypt( byte[] cipherText, int cipherOff, byte[] clearText, int clearOff ) 00119 { 00120 blockCipher.decrypt( cipherText, cipherOff, temp, 0 ); 00121 xorBlock( temp, 0, iv, 0, clearText, clearOff, blockSize ); 00122 copyBlock( cipherText, cipherOff, iv, 0, blockSize ); 00123 } 00124 00125 }