Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / mode / OFB.java
blob68065d10b9c5bd418f91e1c91d874191d7325eec
1 /* OFB.java --
2 Copyright (C) 2001, 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
19 USA
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.
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;
43 import gnu.javax.crypto.cipher.IBlockCipher;
45 /**
46 * <p>The Output Feedback (OFB) mode is a confidentiality mode that requires a
47 * unique <code>IV</code> for every message that is ever encrypted under the
48 * given key. The OFB mode is defined as follows:</p>
50 * <ul>
51 * <li>OFB Encryption:
52 * <ul>
53 * <li>I<sub>1</sub> = IV;</li>
54 * <li>I<sub>j</sub> = O<sub>j -1</sub> for j = 2...n;</li>
55 * <li>O<sub>j</sub> = CIPH<sub>K</sub>(I<sub>j</sub>) for j = 1, 2...n;</li>
56 * <li>C<sub>j</sub> = P<sub>j</sub> XOR O<sub>j</sub> for j = 1, 2...n.</li>
57 * </ul></li>
58 * <li>OFB Decryption:
59 * <ul>
60 * <li>I<sub>1</sub> = IV;</li>
61 * <li>I<sub>j</sub> = O<sub>j -1</sub> for j = 2...n;</li>
62 * <li>O<sub>j</sub> = CIPH<sub>K</sub>(I<sub>j</sub>) for j = 1, 2...n;</li>
63 * <li>P<sub>j</sub> = C<sub>j</sub> XOR O<sub>j</sub> for j = 1, 2...n.</li>
64 * </ul></li>
65 * </ul>
67 * <p>In OFB encryption, the <code>IV</code> is transformed by the forward
68 * cipher function to produce the first output block. The first output block is
69 * exclusive-ORed with the first plaintext block to produce the first ciphertext
70 * block. The first output block is then transformed by the forward cipher
71 * function to produce the second output block. The second output block is
72 * exclusive-ORed with the second plaintext block to produce the second
73 * ciphertext block, and the second output block is transformed by the forward
74 * cipher function to produce the third output block. Thus, the successive
75 * output blocks are produced from enciphering the previous output blocks, and
76 * the output blocks are exclusive-ORed with the corresponding plaintext blocks
77 * to produce the ciphertext blocks.</p>
79 * <p>In OFB decryption, the <code>IV</code> is transformed by the forward cipher
80 * function to produce the first output block. The first output block is
81 * exclusive-ORed with the first ciphertext block to recover the first plaintext
82 * block. The first output block is then transformed by the forward cipher
83 * function to produce the second output block. The second output block is
84 * exclusive-ORed with the second ciphertext block to produce the second
85 * plaintext block, and the second output block is also transformed by the
86 * forward cipher function to produce the third output block. Thus, the
87 * successive output blocks are produced from enciphering the previous output
88 * blocks, and the output blocks are exclusive-ORed with the corresponding
89 * ciphertext blocks to recover the plaintext blocks.</p>
91 * <p>In both OFB encryption and OFB decryption, each forward cipher function
92 * (except the first) depends on the results of the previous forward cipher
93 * function; therefore, multiple forward cipher functions cannot be performed
94 * in parallel. However, if the <code>IV</code> is known, the output blocks can
95 * be generated prior to the availability of the plaintext or ciphertext data.</p>
97 * <p>The OFB mode requires a unique <code>IV</code> for every message that is
98 * ever encrypted under the given key. If, contrary to this requirement, the
99 * same <code>IV</code> is used for the encryption of more than one message,
100 * then the confidentiality of those messages may be compromised. In particular,
101 * if a plaintext block of any of these messages is known, say, the j<sup>th</sup>
102 * plaintext block, then the j<sup>th</sup> output of the forward cipher
103 * function can be determined easily from the j<sup>th</sup> ciphertext block of
104 * the message. This information allows the j<sup>th</sup> plaintext block of
105 * any other message that is encrypted using the same <code>IV</code> to be
106 * easily recovered from the jth ciphertext block of that message.</p>
108 * <p>Confidentiality may similarly be compromised if any of the input blocks to
109 * the forward cipher function for the encryption of a message is used as the
110 * <code>IV</code> for the encryption of another message under the given key.</p>
112 * <p>References:</p>
114 * <ol>
115 * <li><a href="http://csrc.nist.gov/encryption/modes/Recommendation/Modes01.pdf">
116 * Recommendation for Block Cipher Modes of Operation Methods and Techniques</a>,
117 * Morris Dworkin.</li>
118 * </ol>
120 public class OFB extends BaseMode implements Cloneable
123 // Constants and variables
124 // -------------------------------------------------------------------------
126 private byte[] outputBlock;
128 // Constructor(s)
129 // -------------------------------------------------------------------------
132 * <p>Trivial package-private constructor for use by the Factory class.</p>
134 * @param underlyingCipher the underlying cipher implementation.
135 * @param cipherBlockSize the underlying cipher block size to use.
137 OFB(IBlockCipher underlyingCipher, int cipherBlockSize)
139 super(Registry.OFB_MODE, underlyingCipher, cipherBlockSize);
143 * <p>Private constructor for cloning purposes.</p>
145 * @param that the mode to clone.
147 private OFB(OFB that)
149 this((IBlockCipher) that.cipher.clone(), that.cipherBlockSize);
152 // Class methods
153 // -------------------------------------------------------------------------
155 // Instance methods
156 // -------------------------------------------------------------------------
158 // java.lang.Cloneable interface implementation ----------------------------
160 public Object clone()
162 return new OFB(this);
165 // Implementation of abstract methods in BaseMode --------------------------
167 public void setup()
169 if (modeBlockSize != cipherBlockSize)
171 throw new IllegalArgumentException(IMode.MODE_BLOCK_SIZE);
174 outputBlock = (byte[]) iv.clone();
177 public void teardown()
181 public void encryptBlock(byte[] in, int i, byte[] out, int o)
183 cipher.encryptBlock(outputBlock, 0, outputBlock, 0);
184 for (int j = 0; j < cipherBlockSize;)
186 out[o++] = (byte) (in[i++] ^ outputBlock[j++]);
190 public void decryptBlock(byte[] in, int i, byte[] out, int o)
192 this.encryptBlock(in, i, out, o);