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
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
.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
;
51 * <p>The implementation of the Counter Mode.</p>
53 * <p>The algorithm steps are formally described as follows:</p>
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.
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
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>
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>
78 public class CTR
extends BaseMode
implements Cloneable
81 // Constants and variables
82 // -------------------------------------------------------------------------
84 /** The current counter. */
85 // private BigInteger T;
88 private byte[] counter
, enc
;
91 // -------------------------------------------------------------------------
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
);
115 // -------------------------------------------------------------------------
117 // Cloneable interface implementation
118 // -------------------------------------------------------------------------
120 public Object
clone()
122 return new CTR(this);
125 // Implementation of abstract methods in BaseMode
126 // -------------------------------------------------------------------------
130 if (modeBlockSize
> cipherBlockSize
)
132 throw new IllegalArgumentException(
133 "mode size exceeds cipher block size");
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();
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);
155 // T = new BigInteger(1, tBytes);
158 public void teardown()
162 Arrays
.fill(counter
, (byte) 0);
166 Arrays
.fill(enc
, (byte) 0);
171 public void encryptBlock(byte[] in
, int i
, byte[] out
, int o
)
176 public void decryptBlock(byte[] in
, int i
, byte[] out
, int o
)
181 public Iterator
blockSizes()
183 return new Sequence(1, cipherBlockSize
).iterator();
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++]);
198 for (int i
= 0; i
< modeBlockSize
; i
++)
200 out
[outOffset
++] = (byte) (in
[inOffset
++] ^ enc
[off
++]);
201 if (off
== cipherBlockSize
)
204 for (j
= cipherBlockSize
- 1; j
>= 0; j
--)
207 if ((counter
[j
] & 0xFF) != 0)
214 counter
[cipherBlockSize
- 1]++;
217 cipher
.encryptBlock(counter
, 0, enc
, 0);