2
|
1 |
/*
|
|
2 |
* Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.crypto.provider;
|
|
27 |
|
|
28 |
import java.security.InvalidKeyException;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* This class represents ciphers in counter (CTR) mode.
|
|
32 |
*
|
|
33 |
* <p>This mode is implemented independently of a particular cipher.
|
|
34 |
* Ciphers to which this mode should apply (e.g., DES) must be
|
|
35 |
* <i>plugged-in</i> using the constructor.
|
|
36 |
*
|
|
37 |
* <p>NOTE: This class does not deal with buffering or padding.
|
|
38 |
*
|
|
39 |
* @author Andreas Sterbenz
|
|
40 |
* @since 1.4.2
|
|
41 |
*/
|
|
42 |
final class CounterMode extends FeedbackCipher {
|
|
43 |
|
|
44 |
// current counter value
|
|
45 |
private final byte[] counter;
|
|
46 |
|
|
47 |
// encrypted bytes of the previous counter value
|
|
48 |
private final byte[] encryptedCounter;
|
|
49 |
|
|
50 |
// number of bytes in encryptedCounter already used up
|
|
51 |
private int used;
|
|
52 |
|
|
53 |
// variables for save/restore calls
|
|
54 |
private byte[] counterSave = null;
|
|
55 |
private byte[] encryptedCounterSave = null;
|
|
56 |
private int usedSave = 0;
|
|
57 |
|
|
58 |
CounterMode(SymmetricCipher embeddedCipher) {
|
|
59 |
super(embeddedCipher);
|
|
60 |
counter = new byte[blockSize];
|
|
61 |
encryptedCounter = new byte[blockSize];
|
|
62 |
}
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Gets the name of the feedback mechanism
|
|
66 |
*
|
|
67 |
* @return the name of the feedback mechanism
|
|
68 |
*/
|
|
69 |
String getFeedback() {
|
|
70 |
return "CTR";
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Resets the iv to its original value.
|
|
75 |
* This is used when doFinal is called in the Cipher class, so that the
|
|
76 |
* cipher can be reused (with its original iv).
|
|
77 |
*/
|
|
78 |
void reset() {
|
|
79 |
System.arraycopy(iv, 0, counter, 0, blockSize);
|
|
80 |
used = blockSize;
|
|
81 |
}
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Save the current content of this cipher.
|
|
85 |
*/
|
|
86 |
void save() {
|
|
87 |
if (counterSave == null) {
|
|
88 |
counterSave = new byte[blockSize];
|
|
89 |
encryptedCounterSave = new byte[blockSize];
|
|
90 |
}
|
|
91 |
System.arraycopy(counter, 0, counterSave, 0, blockSize);
|
|
92 |
System.arraycopy(encryptedCounter, 0, encryptedCounterSave, 0,
|
|
93 |
blockSize);
|
|
94 |
usedSave = used;
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Restores the content of this cipher to the previous saved one.
|
|
99 |
*/
|
|
100 |
void restore() {
|
|
101 |
System.arraycopy(counterSave, 0, counter, 0, blockSize);
|
|
102 |
System.arraycopy(encryptedCounterSave, 0, encryptedCounter, 0,
|
|
103 |
blockSize);
|
|
104 |
used = usedSave;
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Initializes the cipher in the specified mode with the given key
|
|
109 |
* and iv.
|
|
110 |
*
|
|
111 |
* @param decrypting flag indicating encryption or decryption
|
|
112 |
* @param algorithm the algorithm name
|
|
113 |
* @param key the key
|
|
114 |
* @param iv the iv
|
|
115 |
*
|
|
116 |
* @exception InvalidKeyException if the given key is inappropriate for
|
|
117 |
* initializing this cipher
|
|
118 |
*/
|
|
119 |
void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
|
|
120 |
throws InvalidKeyException {
|
|
121 |
if ((key == null) || (iv == null) || (iv.length != blockSize)) {
|
|
122 |
throw new InvalidKeyException("Internal error");
|
|
123 |
}
|
|
124 |
this.iv = iv;
|
|
125 |
reset();
|
|
126 |
// always encrypt mode for embedded cipher
|
|
127 |
embeddedCipher.init(false, algorithm, key);
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Performs encryption operation.
|
|
132 |
*
|
|
133 |
* <p>The input plain text <code>plain</code>, starting at
|
|
134 |
* <code>plainOffset</code> and ending at
|
|
135 |
* <code>(plainOffset + len - 1)</code>, is encrypted.
|
|
136 |
* The result is stored in <code>cipher</code>, starting at
|
|
137 |
* <code>cipherOffset</code>.
|
|
138 |
*
|
|
139 |
* <p>It is the application's responsibility to make sure that
|
|
140 |
* <code>plainLen</code> is a multiple of the embedded cipher's block size,
|
|
141 |
* as any excess bytes are ignored.
|
|
142 |
*
|
|
143 |
* <p>It is also the application's responsibility to make sure that
|
|
144 |
* <code>init</code> has been called before this method is called.
|
|
145 |
* (This check is omitted here, to avoid double checking.)
|
|
146 |
*
|
|
147 |
* @param in the buffer with the input data to be encrypted
|
|
148 |
* @param inOffset the offset in <code>plain</code>
|
|
149 |
* @param len the length of the input data
|
|
150 |
* @param out the buffer for the result
|
|
151 |
* @param outOff the offset in <code>cipher</code>
|
|
152 |
*/
|
|
153 |
void encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
|
|
154 |
crypt(in, inOff, len, out, outOff);
|
|
155 |
}
|
|
156 |
|
|
157 |
/**
|
|
158 |
* Performs decryption operation.
|
|
159 |
*
|
|
160 |
* <p>The input cipher text <code>cipher</code>, starting at
|
|
161 |
* <code>cipherOffset</code> and ending at
|
|
162 |
* <code>(cipherOffset + len - 1)</code>, is decrypted.
|
|
163 |
* The result is stored in <code>plain</code>, starting at
|
|
164 |
* <code>plainOffset</code>.
|
|
165 |
*
|
|
166 |
* <p>It is the application's responsibility to make sure that
|
|
167 |
* <code>cipherLen</code> is a multiple of the embedded cipher's block
|
|
168 |
* size, as any excess bytes are ignored.
|
|
169 |
*
|
|
170 |
* <p>It is also the application's responsibility to make sure that
|
|
171 |
* <code>init</code> has been called before this method is called.
|
|
172 |
* (This check is omitted here, to avoid double checking.)
|
|
173 |
*
|
|
174 |
* @param in the buffer with the input data to be decrypted
|
|
175 |
* @param inOff the offset in <code>cipherOffset</code>
|
|
176 |
* @param len the length of the input data
|
|
177 |
* @param out the buffer for the result
|
|
178 |
* @param outOff the offset in <code>plain</code>
|
|
179 |
*/
|
|
180 |
void decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
|
|
181 |
crypt(in, inOff, len, out, outOff);
|
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
|
185 |
* Increment the counter value.
|
|
186 |
*/
|
|
187 |
private static void increment(byte[] b) {
|
|
188 |
int n = b.length - 1;
|
|
189 |
while ((n >= 0) && (++b[n] == 0)) {
|
|
190 |
n--;
|
|
191 |
}
|
|
192 |
}
|
|
193 |
|
|
194 |
/**
|
|
195 |
* Do the actual encryption/decryption operation.
|
|
196 |
* Essentially we XOR the input plaintext/ciphertext stream with a
|
|
197 |
* keystream generated by encrypting the counter values. Counter values
|
|
198 |
* are encrypted on demand.
|
|
199 |
*/
|
|
200 |
private void crypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
|
|
201 |
while (len-- > 0) {
|
|
202 |
if (used >= blockSize) {
|
|
203 |
embeddedCipher.encryptBlock(counter, 0, encryptedCounter, 0);
|
|
204 |
increment(counter);
|
|
205 |
used = 0;
|
|
206 |
}
|
|
207 |
out[outOff++] = (byte)(in[inOff++] ^ encryptedCounter[used++]);
|
|
208 |
}
|
|
209 |
}
|
|
210 |
}
|