2 Copyright (C) 2002, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.crypto.mode;
41 import gnu.java.security.Registry;
42 import gnu.javax.crypto.cipher.IBlockCipher;
45 * The cipher feedback mode. CFB mode is a stream mode that operates on <i>s</i>
46 * bit blocks, where 1 <= <i>s</i> <= <i>b</i>, if <i>b</i> is the
47 * underlying cipher's block size. Encryption is:
50 * I[j] = LSB(b-s, I[j-1]) | C[j-1] for j = 2...n
51 * O[j] = CIPH(K, I[j]) for j = 1,2...n
52 * C[j] = P[j] ˆ MSB(s, O[j]) for j = 1,2...n
58 * I[j] = LSB(b-s, I[j-1]) | C[j-1] for j = 2...n
59 * O[j] = CIPH(K, I[j]) for j = 1,2...n
60 * P[j] = C[j] ˆ MSB(s, O[j]) for j = 1,2...n
63 * CFB mode requires an initialization vector, which need not be kept secret.
67 * <li>Bruce Schneier, <i>Applied Cryptography: Protocols, Algorithms, and
68 * Source Code in C, Second Edition</i>. (1996 John Wiley and Sons) ISBN
71 * href="http://csrc.nist.gov/encryption/modes/Recommendation/Modes01.pdf">
72 * Recommendation for Block Cipher Modes of Operation Methods and Techniques</a>,
73 * Morris Dworkin.</li>
79 /** The shift register, the input block to the block cipher. */
80 private byte[] shiftRegister;
81 /** The output block from the block cipher. */
82 private byte[] scratch;
85 * Package-private constructor for the factory class.
87 * @param underlyingCipher The cipher implementation.
88 * @param cipherBlockSize The cipher's block size.
90 CFB(IBlockCipher underlyingCipher, int cipherBlockSize)
92 super(Registry.CFB_MODE, underlyingCipher, cipherBlockSize);
96 * Cloneing constructor.
98 * @param that The instance being cloned.
100 private CFB(CFB that)
102 this((IBlockCipher) that.cipher.clone(), that.cipherBlockSize);
105 public Object clone()
107 return new CFB(this);
112 if (modeBlockSize > cipherBlockSize)
113 throw new IllegalArgumentException(
114 "CFB block size cannot be larger than the cipher block size");
115 shiftRegister = new byte[cipherBlockSize];
116 scratch = new byte[cipherBlockSize];
117 System.arraycopy(iv, 0,
119 Math.min(iv.length, cipherBlockSize));
122 public void teardown()
124 if (shiftRegister != null)
125 for (int i = 0; i < shiftRegister.length; i++)
126 shiftRegister[i] = 0;
127 shiftRegister = null;
130 public void encryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
132 cipher.encryptBlock(shiftRegister, 0, scratch, 0);
133 for (int i = 0; i < modeBlockSize; i++)
134 out[outOffset + i] = (byte)(in[inOffset + i] ^ scratch[i]);
135 System.arraycopy(shiftRegister, modeBlockSize,
137 cipherBlockSize - modeBlockSize);
138 System.arraycopy(out, outOffset,
139 shiftRegister, cipherBlockSize - modeBlockSize,
143 public void decryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
145 cipher.encryptBlock(shiftRegister, 0, scratch, 0);
146 for (int i = 0; i < modeBlockSize; i++)
147 out[outOffset + i] = (byte)(in[inOffset + i] ^ scratch[i]);
148 System.arraycopy(shiftRegister, modeBlockSize,
150 cipherBlockSize - modeBlockSize);
151 System.arraycopy(in, inOffset,
152 shiftRegister, cipherBlockSize - modeBlockSize,