2
|
1 |
/*
|
|
2 |
* Copyright 1998-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.io.UnsupportedEncodingException;
|
|
29 |
import java.security.*;
|
|
30 |
import java.security.spec.*;
|
|
31 |
import javax.crypto.*;
|
|
32 |
import javax.crypto.spec.*;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* This class implements a proprietary password-based encryption algorithm.
|
|
36 |
* It is based on password-based encryption as defined by the PKCS #5
|
|
37 |
* standard, except that is uses triple DES instead of DES.
|
|
38 |
*
|
|
39 |
* Here's how this algorithm works:
|
|
40 |
*
|
|
41 |
* 1. Create random salt and split it in two halves. If the two halves are
|
|
42 |
* identical, invert one of them.
|
|
43 |
* 2. Concatenate password with each of the halves.
|
|
44 |
* 3. Digest each concatenation with c iterations, where c is the
|
|
45 |
* iterationCount. Concatenate the output from each digest round with the
|
|
46 |
* password, and use the result as the input to the next digest operation.
|
|
47 |
* The digest algorithm is MD5.
|
|
48 |
* 4. After c iterations, use the 2 resulting digests as follows:
|
|
49 |
* The 16 bytes of the first digest and the 1st 8 bytes of the 2nd digest
|
|
50 |
* form the triple DES key, and the last 8 bytes of the 2nd digest form the
|
|
51 |
* IV.
|
|
52 |
*
|
|
53 |
* @author Jan Luehe
|
|
54 |
* @see javax.crypto.Cipher
|
|
55 |
*/
|
|
56 |
public final class PBEWithMD5AndTripleDESCipher extends CipherSpi {
|
|
57 |
|
|
58 |
private PBECipherCore core;
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Creates an instance of this cipher, and initializes its mode (CBC) and
|
|
62 |
* padding (PKCS5).
|
|
63 |
*
|
|
64 |
* Verify the SunJCE provider in the constructor.
|
|
65 |
*
|
|
66 |
* @exception NoSuchAlgorithmException if the required cipher mode (CBC) is
|
|
67 |
* unavailable
|
|
68 |
* @exception NoSuchPaddingException if the required padding mechanism
|
|
69 |
* (PKCS5Padding) is unavailable
|
|
70 |
* @exception SecurityException if fails to verify
|
|
71 |
* its own integrity
|
|
72 |
*/
|
|
73 |
public PBEWithMD5AndTripleDESCipher()
|
|
74 |
throws NoSuchAlgorithmException, NoSuchPaddingException
|
|
75 |
{
|
|
76 |
if (!SunJCE.verifySelfIntegrity(this.getClass())) {
|
|
77 |
throw new SecurityException("The SunJCE provider may have " +
|
|
78 |
"been tampered.");
|
|
79 |
}
|
|
80 |
|
|
81 |
// set the encapsulated cipher to do triple DES
|
|
82 |
core = new PBECipherCore("DESede");
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Sets the mode of this cipher. This algorithm can only be run in CBC
|
|
87 |
* mode.
|
|
88 |
*
|
|
89 |
* @param mode the cipher mode
|
|
90 |
*
|
|
91 |
* @exception NoSuchAlgorithmException if the requested cipher mode is
|
|
92 |
* invalid
|
|
93 |
*/
|
|
94 |
protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
|
|
95 |
if ((mode != null) && (!mode.equalsIgnoreCase("CBC"))) {
|
|
96 |
throw new NoSuchAlgorithmException("Invalid cipher mode: " + mode);
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Sets the padding mechanism of this cipher. This algorithm only uses
|
|
102 |
* PKCS #5 padding.
|
|
103 |
*
|
|
104 |
* @param padding the padding mechanism
|
|
105 |
*
|
|
106 |
* @exception NoSuchPaddingException if the requested padding mechanism
|
|
107 |
* is invalid
|
|
108 |
*/
|
|
109 |
protected void engineSetPadding(String paddingScheme)
|
|
110 |
throws NoSuchPaddingException
|
|
111 |
{
|
|
112 |
if ((paddingScheme != null) &&
|
|
113 |
(!paddingScheme.equalsIgnoreCase("PKCS5Padding"))) {
|
|
114 |
throw new NoSuchPaddingException("Invalid padding scheme: " +
|
|
115 |
paddingScheme);
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Returns the block size (in bytes).
|
|
121 |
*
|
|
122 |
* @return the block size (in bytes)
|
|
123 |
*/
|
|
124 |
protected int engineGetBlockSize() {
|
|
125 |
return core.getBlockSize();
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Returns the length in bytes that an output buffer would need to be in
|
|
130 |
* order to hold the result of the next <code>update</code> or
|
|
131 |
* <code>doFinal</code> operation, given the input length
|
|
132 |
* <code>inputLen</code> (in bytes).
|
|
133 |
*
|
|
134 |
* <p>This call takes into account any unprocessed (buffered) data from a
|
|
135 |
* previous <code>update</code> call, and padding.
|
|
136 |
*
|
|
137 |
* <p>The actual output length of the next <code>update</code> or
|
|
138 |
* <code>doFinal</code> call may be smaller than the length returned by
|
|
139 |
* this method.
|
|
140 |
*
|
|
141 |
* @param inputLen the input length (in bytes)
|
|
142 |
*
|
|
143 |
* @return the required output buffer size (in bytes)
|
|
144 |
*
|
|
145 |
*/
|
|
146 |
protected int engineGetOutputSize(int inputLen) {
|
|
147 |
return core.getOutputSize(inputLen);
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Returns the initialization vector (IV) in a new buffer.
|
|
152 |
*
|
|
153 |
* <p> This is useful in the case where a random IV has been created
|
|
154 |
* (see <a href = "#init">init</a>),
|
|
155 |
* or in the context of password-based encryption or
|
|
156 |
* decryption, where the IV is derived from a user-supplied password.
|
|
157 |
*
|
|
158 |
* @return the initialization vector in a new buffer, or null if the
|
|
159 |
* underlying algorithm does not use an IV, or if the IV has not yet
|
|
160 |
* been set.
|
|
161 |
*/
|
|
162 |
protected byte[] engineGetIV() {
|
|
163 |
return core.getIV();
|
|
164 |
}
|
|
165 |
|
|
166 |
/**
|
|
167 |
* Returns the parameters used with this cipher.
|
|
168 |
*
|
|
169 |
* <p>The returned parameters may be the same that were used to initialize
|
|
170 |
* this cipher, or may contain the default set of parameters or a set of
|
|
171 |
* randomly generated parameters used by the underlying cipher
|
|
172 |
* implementation (provided that the underlying cipher implementation
|
|
173 |
* uses a default set of parameters or creates new parameters if it needs
|
|
174 |
* parameters but was not initialized with any).
|
|
175 |
*
|
|
176 |
* @return the parameters used with this cipher, or null if this cipher
|
|
177 |
* does not use any parameters.
|
|
178 |
*/
|
|
179 |
protected AlgorithmParameters engineGetParameters() {
|
|
180 |
return core.getParameters();
|
|
181 |
}
|
|
182 |
|
|
183 |
/**
|
|
184 |
* Initializes this cipher with a key and a source
|
|
185 |
* of randomness.
|
|
186 |
* The cipher is initialized for one of the following four operations:
|
|
187 |
* encryption, decryption, key wrapping or key unwrapping, depending on
|
|
188 |
* the value of <code>opmode</code>.
|
|
189 |
*
|
|
190 |
* <p>If this cipher (including its underlying feedback or padding scheme)
|
|
191 |
* requires any random bytes, it will get them from <code>random</code>.
|
|
192 |
*
|
|
193 |
* @param opmode the operation mode of this cipher (this is one of
|
|
194 |
* the following:
|
|
195 |
* <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>),
|
|
196 |
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
|
|
197 |
* @param key the encryption key
|
|
198 |
* @param random the source of randomness
|
|
199 |
*
|
|
200 |
* @exception InvalidKeyException if the given key is inappropriate for
|
|
201 |
* initializing this cipher
|
|
202 |
*/
|
|
203 |
protected void engineInit(int opmode, Key key, SecureRandom random)
|
|
204 |
throws InvalidKeyException {
|
|
205 |
try {
|
|
206 |
core.init(opmode, key, (AlgorithmParameterSpec) null, random);
|
|
207 |
} catch (InvalidAlgorithmParameterException ie) {
|
|
208 |
InvalidKeyException ike =
|
|
209 |
new InvalidKeyException("requires PBE parameters");
|
|
210 |
ike.initCause(ie);
|
|
211 |
throw ike;
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
/**
|
|
216 |
* Initializes this cipher with a key, a set of
|
|
217 |
* algorithm parameters, and a source of randomness.
|
|
218 |
* The cipher is initialized for encryption or decryption, depending on
|
|
219 |
* the value of <code>opmode</code>.
|
|
220 |
*
|
|
221 |
* <p>If this cipher (including its underlying feedback or padding scheme)
|
|
222 |
* requires any random bytes, it will get them from <code>random</code>.
|
|
223 |
*
|
|
224 |
* @param opmode the operation mode of this cipher (this is either
|
|
225 |
* <code>ENCRYPT_MODE</code> or <code>DECRYPT_MODE</code>)
|
|
226 |
* @param key the encryption key
|
|
227 |
* @param params the algorithm parameters
|
|
228 |
* @param random the source of randomness
|
|
229 |
*
|
|
230 |
* @exception InvalidKeyException if the given key is inappropriate for
|
|
231 |
* initializing this cipher
|
|
232 |
* @exception InvalidAlgorithmParameterException if the given algorithm
|
|
233 |
* parameters are inappropriate for this cipher
|
|
234 |
*/
|
|
235 |
protected void engineInit(int opmode, Key key,
|
|
236 |
AlgorithmParameterSpec params,
|
|
237 |
SecureRandom random)
|
|
238 |
throws InvalidKeyException, InvalidAlgorithmParameterException {
|
|
239 |
core.init(opmode, key, params, random);
|
|
240 |
}
|
|
241 |
|
|
242 |
protected void engineInit(int opmode, Key key,
|
|
243 |
AlgorithmParameters params,
|
|
244 |
SecureRandom random)
|
|
245 |
throws InvalidKeyException, InvalidAlgorithmParameterException
|
|
246 |
{
|
|
247 |
core.init(opmode, key, params, random);
|
|
248 |
}
|
|
249 |
|
|
250 |
/**
|
|
251 |
* Continues a multiple-part encryption or decryption operation
|
|
252 |
* (depending on how this cipher was initialized), processing another data
|
|
253 |
* part.
|
|
254 |
*
|
|
255 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
|
|
256 |
* buffer, starting at <code>inputOffset</code>, are processed, and the
|
|
257 |
* result is stored in a new buffer.
|
|
258 |
*
|
|
259 |
* @param input the input buffer
|
|
260 |
* @param inputOffset the offset in <code>input</code> where the input
|
|
261 |
* starts
|
|
262 |
* @param inputLen the input length
|
|
263 |
*
|
|
264 |
* @return the new buffer with the result
|
|
265 |
*
|
|
266 |
*/
|
|
267 |
protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen)
|
|
268 |
{
|
|
269 |
return core.update(input, inputOffset, inputLen);
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Continues a multiple-part encryption or decryption operation
|
|
274 |
* (depending on how this cipher was initialized), processing another data
|
|
275 |
* part.
|
|
276 |
*
|
|
277 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
|
|
278 |
* buffer, starting at <code>inputOffset</code>, are processed, and the
|
|
279 |
* result is stored in the <code>output</code> buffer, starting at
|
|
280 |
* <code>outputOffset</code>.
|
|
281 |
*
|
|
282 |
* @param input the input buffer
|
|
283 |
* @param inputOffset the offset in <code>input</code> where the input
|
|
284 |
* starts
|
|
285 |
* @param inputLen the input length
|
|
286 |
* @param output the buffer for the result
|
|
287 |
* @param outputOffset the offset in <code>output</code> where the result
|
|
288 |
* is stored
|
|
289 |
*
|
|
290 |
* @return the number of bytes stored in <code>output</code>
|
|
291 |
*
|
|
292 |
* @exception ShortBufferException if the given output buffer is too small
|
|
293 |
* to hold the result
|
|
294 |
*/
|
|
295 |
protected int engineUpdate(byte[] input, int inputOffset, int inputLen,
|
|
296 |
byte[] output, int outputOffset)
|
|
297 |
throws ShortBufferException
|
|
298 |
{
|
|
299 |
return core.update(input, inputOffset, inputLen,
|
|
300 |
output, outputOffset);
|
|
301 |
}
|
|
302 |
|
|
303 |
/**
|
|
304 |
* Encrypts or decrypts data in a single-part operation,
|
|
305 |
* or finishes a multiple-part operation.
|
|
306 |
* The data is encrypted or decrypted, depending on how this cipher was
|
|
307 |
* initialized.
|
|
308 |
*
|
|
309 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
|
|
310 |
* buffer, starting at <code>inputOffset</code>, and any input bytes that
|
|
311 |
* may have been buffered during a previous <code>update</code> operation,
|
|
312 |
* are processed, with padding (if requested) being applied.
|
|
313 |
* The result is stored in a new buffer.
|
|
314 |
*
|
|
315 |
* <p>The cipher is reset to its initial state (uninitialized) after this
|
|
316 |
* call.
|
|
317 |
*
|
|
318 |
* @param input the input buffer
|
|
319 |
* @param inputOffset the offset in <code>input</code> where the input
|
|
320 |
* starts
|
|
321 |
* @param inputLen the input length
|
|
322 |
*
|
|
323 |
* @return the new buffer with the result
|
|
324 |
*
|
|
325 |
* @exception IllegalBlockSizeException if this cipher is a block cipher,
|
|
326 |
* no padding has been requested (only in encryption mode), and the total
|
|
327 |
* input length of the data processed by this cipher is not a multiple of
|
|
328 |
* block size
|
|
329 |
* @exception BadPaddingException if decrypting and padding is choosen,
|
|
330 |
* but the last input data does not have proper padding bytes.
|
|
331 |
*/
|
|
332 |
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
|
|
333 |
throws IllegalBlockSizeException, BadPaddingException
|
|
334 |
{
|
|
335 |
return core.doFinal(input, inputOffset, inputLen);
|
|
336 |
}
|
|
337 |
|
|
338 |
/**
|
|
339 |
* Encrypts or decrypts data in a single-part operation,
|
|
340 |
* or finishes a multiple-part operation.
|
|
341 |
* The data is encrypted or decrypted, depending on how this cipher was
|
|
342 |
* initialized.
|
|
343 |
*
|
|
344 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
|
|
345 |
* buffer, starting at <code>inputOffset</code>, and any input bytes that
|
|
346 |
* may have been buffered during a previous <code>update</code> operation,
|
|
347 |
* are processed, with padding (if requested) being applied.
|
|
348 |
* The result is stored in the <code>output</code> buffer, starting at
|
|
349 |
* <code>outputOffset</code>.
|
|
350 |
*
|
|
351 |
* <p>The cipher is reset to its initial state (uninitialized) after this
|
|
352 |
* call.
|
|
353 |
*
|
|
354 |
* @param input the input buffer
|
|
355 |
* @param inputOffset the offset in <code>input</code> where the input
|
|
356 |
* starts
|
|
357 |
* @param inputLen the input length
|
|
358 |
* @param output the buffer for the result
|
|
359 |
* @param outputOffset the offset in <code>output</code> where the result
|
|
360 |
* is stored
|
|
361 |
*
|
|
362 |
* @return the number of bytes stored in <code>output</code>
|
|
363 |
*
|
|
364 |
* @exception IllegalBlockSizeException if this cipher is a block cipher,
|
|
365 |
* no padding has been requested (only in encryption mode), and the total
|
|
366 |
* input length of the data processed by this cipher is not a multiple of
|
|
367 |
* block size
|
|
368 |
* @exception ShortBufferException if the given output buffer is too small
|
|
369 |
* to hold the result
|
|
370 |
* @exception BadPaddingException if decrypting and padding is choosen,
|
|
371 |
* but the last input data does not have proper padding bytes.
|
|
372 |
*/
|
|
373 |
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
|
|
374 |
byte[] output, int outputOffset)
|
|
375 |
throws ShortBufferException, IllegalBlockSizeException,
|
|
376 |
BadPaddingException
|
|
377 |
{
|
|
378 |
return core.doFinal(input, inputOffset, inputLen,
|
|
379 |
output, outputOffset);
|
|
380 |
}
|
|
381 |
|
|
382 |
/**
|
|
383 |
* Returns the key size of the given key object.
|
|
384 |
*
|
|
385 |
* @param key the key object.
|
|
386 |
*
|
|
387 |
* @return the key size of the given key object.
|
|
388 |
*
|
|
389 |
* @exception InvalidKeyException if <code>key</code> is invalid.
|
|
390 |
*/
|
|
391 |
protected int engineGetKeySize(Key key) throws InvalidKeyException {
|
|
392 |
return 168;
|
|
393 |
}
|
|
394 |
|
|
395 |
/**
|
|
396 |
* Wrap a key.
|
|
397 |
*
|
|
398 |
* @param key the key to be wrapped.
|
|
399 |
*
|
|
400 |
* @return the wrapped key.
|
|
401 |
*
|
|
402 |
* @exception IllegalBlockSizeException if this cipher is a block
|
|
403 |
* cipher, no padding has been requested, and the length of the
|
|
404 |
* encoding of the key to be wrapped is not a
|
|
405 |
* multiple of the block size.
|
|
406 |
*
|
|
407 |
* @exception InvalidKeyException if it is impossible or unsafe to
|
|
408 |
* wrap the key with this cipher (e.g., a hardware protected key is
|
|
409 |
* being passed to a software only cipher).
|
|
410 |
*/
|
|
411 |
protected byte[] engineWrap(Key key)
|
|
412 |
throws IllegalBlockSizeException, InvalidKeyException {
|
|
413 |
return core.wrap(key);
|
|
414 |
}
|
|
415 |
|
|
416 |
/**
|
|
417 |
* Unwrap a previously wrapped key.
|
|
418 |
*
|
|
419 |
* @param wrappedKey the key to be unwrapped.
|
|
420 |
*
|
|
421 |
* @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
|
|
422 |
*
|
|
423 |
* @param wrappedKeyType the type of the wrapped key.
|
|
424 |
* This is one of <code>Cipher.SECRET_KEY</code>,
|
|
425 |
* <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
|
|
426 |
*
|
|
427 |
* @return the unwrapped key.
|
|
428 |
*
|
|
429 |
* @exception NoSuchAlgorithmException if no installed providers
|
|
430 |
* can create keys of type <code>wrappedKeyType</code> for the
|
|
431 |
* <code>wrappedKeyAlgorithm</code>.
|
|
432 |
*
|
|
433 |
* @exception InvalidKeyException if <code>wrappedKey</code> does not
|
|
434 |
* represent a wrapped key of type <code>wrappedKeyType</code> for
|
|
435 |
* the <code>wrappedKeyAlgorithm</code>.
|
|
436 |
*/
|
|
437 |
protected Key engineUnwrap(byte[] wrappedKey,
|
|
438 |
String wrappedKeyAlgorithm,
|
|
439 |
int wrappedKeyType)
|
|
440 |
throws InvalidKeyException, NoSuchAlgorithmException {
|
|
441 |
return core.unwrap(wrappedKey, wrappedKeyAlgorithm,
|
|
442 |
wrappedKeyType);
|
|
443 |
}
|
|
444 |
}
|