OSDN Git Service

2006-08-14 Mark Wielaard <mark@klomp.org>
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / javax / crypto / mode / CFB.java
1 /* CFB.java -- 
2    Copyright (C) 2002, 2006 Free Software Foundation, Inc.
3
4 This file is a part of GNU Classpath.
5
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.
10
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.
15
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
19 USA
20
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
24 combination.
25
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.  */
37
38
39 package gnu.javax.crypto.mode;
40
41 import gnu.java.security.Registry;
42 import gnu.javax.crypto.cipher.IBlockCipher;
43
44 /**
45  * The cipher feedback mode. CFB mode is a stream mode that operates on <i>s</i>
46  * bit blocks, where 1 &lt;= <i>s</i> &lt;= <i>b</i>, if <i>b</i> is the
47  * underlying cipher's block size. Encryption is:
48  * <pre>
49  *  I[1] = IV
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] &circ; MSB(s, O[j])         for j = 1,2...n
53  * </pre>
54  * <p>
55  * And decryption is:
56  * <pre>
57  *  I[1] = IV
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] &circ; MSB(s, O[j])         for j = 1,2...n
61  * </pre>
62  * <p>
63  * CFB mode requires an initialization vector, which need not be kept secret.
64  * <p>
65  * References:
66  * <ol>
67  * <li>Bruce Schneier, <i>Applied Cryptography: Protocols, Algorithms, and
68  * Source Code in C, Second Edition</i>. (1996 John Wiley and Sons) ISBN
69  * 0-471-11709-9.</li>
70  * <li><a
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>
74  * </ol>
75  */
76 public class CFB
77     extends BaseMode
78 {
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;
83
84   /**
85    * Package-private constructor for the factory class.
86    * 
87    * @param underlyingCipher The cipher implementation.
88    * @param cipherBlockSize The cipher's block size.
89    */
90   CFB(IBlockCipher underlyingCipher, int cipherBlockSize)
91   {
92     super(Registry.CFB_MODE, underlyingCipher, cipherBlockSize);
93   }
94
95   /**
96    * Cloneing constructor.
97    * 
98    * @param that The instance being cloned.
99    */
100   private CFB(CFB that)
101   {
102     this((IBlockCipher) that.cipher.clone(), that.cipherBlockSize);
103   }
104
105   public Object clone()
106   {
107     return new CFB(this);
108   }
109
110   public void setup()
111   {
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,
118                      shiftRegister, 0,
119                      Math.min(iv.length, cipherBlockSize));
120   }
121
122   public void teardown()
123   {
124     if (shiftRegister != null)
125       for (int i = 0; i < shiftRegister.length; i++)
126         shiftRegister[i] = 0;
127     shiftRegister = null;
128   }
129
130   public void encryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
131   {
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,
136                      shiftRegister, 0,
137                      cipherBlockSize - modeBlockSize);
138     System.arraycopy(out, outOffset,
139                      shiftRegister, cipherBlockSize - modeBlockSize,
140                      modeBlockSize);
141   }
142
143   public void decryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
144   {
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,
149                      shiftRegister, 0,
150                      cipherBlockSize - modeBlockSize);
151     System.arraycopy(in, inOffset,
152                      shiftRegister, cipherBlockSize - modeBlockSize,
153                      modeBlockSize);
154   }
155 }