Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / mode / CTR.java
blob49f4b9f3c2a14b5b77f249c8ee7816c9ad1cd0a8
1 /* CTR.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;
42 import gnu.java.security.util.Sequence;
44 import gnu.javax.crypto.cipher.IBlockCipher;
46 import java.math.BigInteger;
47 import java.util.Arrays;
48 import java.util.Iterator;
50 /**
51 * <p>The implementation of the Counter Mode.</p>
53 * <p>The algorithm steps are formally described as follows:</p>
55 * <pre>
56 * CTR Encryption: O[j] = E(K)(T[j]); for j = 1, 2...n;
57 * C[j] = P[j] ^ O[j]; for j = 1, 2...n.
58 * CTR Decryption: O[j] = E(K)(T[j]); for j = 1, 2...n;
59 * P[j] = C[j] ^ O[j]; for j = 1, 2...n.
60 * </pre>
62 * <p>where <code>P</code> is the plaintext, <code>C</code> is the ciphertext,
63 * <code>E(K)</code> is the underlying block cipher encryption function
64 * parametrised with the session key <code>K</code>, and <code>T</code> is the
65 * <i>Counter</i>.</p>
67 * <p>This implementation, uses a standard incrementing function with a step of
68 * 1, and an initial value similar to that described in the NIST document.</p>
70 * <p>References:</p>
72 * <ol>
73 * <li><a href="http://csrc.nist.gov/encryption/modes/Recommendation/Modes01.pdf">
74 * Recommendation for Block Cipher Modes of Operation Methods and Techniques</a>,
75 * Morris Dworkin.</li>
76 * </ol>
78 public class CTR extends BaseMode implements Cloneable
81 // Constants and variables
82 // -------------------------------------------------------------------------
84 /** The current counter. */
85 // private BigInteger T;
86 private int off;
88 private byte[] counter, enc;
90 // Constructor(s)
91 // -------------------------------------------------------------------------
93 /**
94 * <p>Trivial package-private constructor for use by the Factory class.</p>
96 * @param underlyingCipher the underlying cipher implementation.
97 * @param cipherBlockSize the underlying cipher block size to use.
99 CTR(IBlockCipher underlyingCipher, int cipherBlockSize)
101 super(Registry.CTR_MODE, underlyingCipher, cipherBlockSize);
105 * <p>Private constructor for cloning purposes.</p>
107 * @param that the instance to clone.
109 private CTR(CTR that)
111 this((IBlockCipher) that.cipher.clone(), that.cipherBlockSize);
114 // Class methods
115 // -------------------------------------------------------------------------
117 // Cloneable interface implementation
118 // -------------------------------------------------------------------------
120 public Object clone()
122 return new CTR(this);
125 // Implementation of abstract methods in BaseMode
126 // -------------------------------------------------------------------------
128 public void setup()
130 if (modeBlockSize > cipherBlockSize)
132 throw new IllegalArgumentException(
133 "mode size exceeds cipher block size");
135 off = 0;
136 counter = new byte[cipherBlockSize];
137 int i = cipherBlockSize - 1;
138 int j = iv.length - 1;
139 while (i >= 0 && j >= 0)
141 counter[i--] = iv[j--];
143 enc = new byte[cipherBlockSize];
144 cipher.encryptBlock(counter, 0, enc, 0);
145 // if (modeBlockSize != cipherBlockSize) {
146 // throw new IllegalArgumentException();
147 // }
149 // byte[] tBytes = new byte[modeBlockSize+1];
150 // tBytes[0] = (byte) 0x80;
151 // for (int i = 0; i < modeBlockSize; i++) {
152 // tBytes[i+1] = (byte)(256 - modeBlockSize + i);
153 // }
155 // T = new BigInteger(1, tBytes);
158 public void teardown()
160 if (counter != null)
162 Arrays.fill(counter, (byte) 0);
164 if (enc != null)
166 Arrays.fill(enc, (byte) 0);
168 // T = null;
171 public void encryptBlock(byte[] in, int i, byte[] out, int o)
173 ctr(in, i, out, o);
176 public void decryptBlock(byte[] in, int i, byte[] out, int o)
178 ctr(in, i, out, o);
181 public Iterator blockSizes()
183 return new Sequence(1, cipherBlockSize).iterator();
186 // own methods
187 // -------------------------------------------------------------------------
189 private void ctr(byte[] in, int inOffset, byte[] out, int outOffset)
191 // T = T.add(BigInteger.ONE);
192 // byte[] O = T.toByteArray();
193 // int ndx = O.length - modeBlockSize;
194 // cipher.encryptBlock(O, ndx, O, ndx);
195 // for (int i = 0; i < modeBlockSize; i++) {
196 // out[outOffset++] = (byte)(in[inOffset++] ^ O[ndx++]);
197 // }
198 for (int i = 0; i < modeBlockSize; i++)
200 out[outOffset++] = (byte) (in[inOffset++] ^ enc[off++]);
201 if (off == cipherBlockSize)
203 int j;
204 for (j = cipherBlockSize - 1; j >= 0; j--)
206 counter[j]++;
207 if ((counter[j] & 0xFF) != 0)
209 break;
212 if (j == 0)
214 counter[cipherBlockSize - 1]++;
216 off = 0;
217 cipher.encryptBlock(counter, 0, enc, 0);