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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.crypto.provider;
27
28 import java.security.*;
29 import java.security.spec.*;
30 import javax.crypto.*;
31 import javax.crypto.spec.*;
32
33 /**
34 * This class implements the CMS DESede KeyWrap algorithm as defined
35 * in <a href=http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap>
36 * "XML Encryption Syntax and Processing" section 5.6.2
37 * "CMS Triple DES Key Wrap".
38 * Note: only <code>CBC</code> mode and <code>NoPadding</code> padding
39 * scheme can be used for this algorithm.
40 *
41 * @author Valerie Peng
42 *
43 *
44 * @see DESedeCipher
45 */
46 public final class DESedeWrapCipher extends CipherSpi {
47
48 private static final byte[] IV2 = {
49 (byte) 0x4a, (byte) 0xdd, (byte) 0xa2, (byte) 0x2c,
50 (byte) 0x79, (byte) 0xe8, (byte) 0x21, (byte) 0x05
51 };
52
53 private static final int CHECKSUM_LEN = 8;
54 private static final int IV_LEN = 8;
55
142 } else {
143 result = inputLen + 16;
144 }
145 return (result < 0? 0:result);
146 }
147
148 /**
149 * Returns the initialization vector (IV) in a new buffer.
150 *
151 * @return the initialization vector, or null if the underlying
152 * algorithm does not use an IV, or if the IV has not yet
153 * been set.
154 */
155 protected byte[] engineGetIV() {
156 return (iv == null) ? null : iv.clone();
157 }
158
159 /**
160 * Initializes this cipher with a key and a source of randomness.
161 *
162 * <p>The cipher only supports the following two operation modes:<b>
163 * Cipher.WRAP_MODE, and <b>
164 * Cipher.UNWRAP_MODE.
165 * <p>For modes other than the above two, UnsupportedOperationException
166 * will be thrown.
167 * <p>If this cipher requires an initialization vector (IV), it will get
168 * it from <code>random</code>.
169 *
170 * @param opmode the operation mode of this cipher. Only
171 * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
172 * @param key the secret key.
173 * @param random the source of randomness.
174 *
175 * @exception InvalidKeyException if the given key is inappropriate
176 * or if parameters are required but not supplied.
177 */
178 protected void engineInit(int opmode, Key key, SecureRandom random)
179 throws InvalidKeyException {
180 try {
181 engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
182 } catch (InvalidAlgorithmParameterException iape) {
183 // should never happen
184 InvalidKeyException ike =
185 new InvalidKeyException("Parameters required");
186 ike.initCause(iape);
187 throw ike;
188 }
189 }
190
191 /**
192 * Initializes this cipher with a key, a set of algorithm parameters,
193 * and a source of randomness.
194 *
195 * <p>The cipher only supports the following two operation modes:<b>
196 * Cipher.WRAP_MODE, and <b>
197 * Cipher.UNWRAP_MODE.
198 * <p>For modes other than the above two, UnsupportedOperationException
199 * will be thrown.
200 * <p>If this cipher requires an initialization vector (IV), it will get
201 * it from <code>random</code>.
202 *
203 * @param opmode the operation mode of this cipher. Only
204 * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
205 * @param key the secret key.
206 * @param params the algorithm parameters.
207 * @param random the source of randomness.
208 *
209 * @exception InvalidKeyException if the given key is inappropriate.
210 * @exception InvalidAlgorithmParameterException if the given algorithm
211 * parameters are inappropriate for this cipher.
212 */
213 protected void engineInit(int opmode, Key key,
214 AlgorithmParameterSpec params,
215 SecureRandom random)
216 throws InvalidKeyException, InvalidAlgorithmParameterException {
217 byte[] currIv = null;
235 if (params != null) {
236 throw new InvalidAlgorithmParameterException
237 ("No parameter accepted for unwrapping keys");
238 }
239 iv = null;
240 decrypting = true;
241 currIv = IV2;
242 } else {
243 throw new UnsupportedOperationException("This cipher can " +
244 "only be used for key wrapping and unwrapping");
245 }
246 cipher.init(decrypting, key.getAlgorithm(), key.getEncoded(),
247 currIv);
248 cipherKey = key;
249 }
250
251 /**
252 * Initializes this cipher with a key, a set of algorithm parameters,
253 * and a source of randomness.
254 *
255 * <p>The cipher only supports the following two operation modes:<b>
256 * Cipher.WRAP_MODE, and <b>
257 * Cipher.UNWRAP_MODE.
258 * <p>For modes other than the above two, UnsupportedOperationException
259 * will be thrown.
260 * <p>If this cipher requires an initialization vector (IV), it will get
261 * it from <code>random</code>.
262 *
263 * @param opmode the operation mode of this cipher. Only
264 * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
265 * @param key the secret key.
266 * @param params the algorithm parameters.
267 * @param random the source of randomness.
268 *
269 * @exception InvalidKeyException if the given key is inappropriate.
270 * @exception InvalidAlgorithmParameterException if the given algorithm
271 * parameters are inappropriate for this cipher.
272 */
273 protected void engineInit(int opmode, Key key,
274 AlgorithmParameters params,
275 SecureRandom random)
276 throws InvalidKeyException, InvalidAlgorithmParameterException {
277 IvParameterSpec ivSpec = null;
343 * @param in the input buffer.
344 * @param inOffset the offset in <code>in</code> where the input
345 * starts.
346 * @param inLen the input length.
347 *
348 * @return the new buffer with the result.
349 *
350 * @exception IllegalStateException upon invocation of this method.
351 */
352 protected byte[] engineDoFinal(byte[] in, int inOffset, int inLen)
353 throws IllegalBlockSizeException, BadPaddingException {
354 throw new IllegalStateException("Cipher has not been initialized");
355 }
356
357 /**
358 * This operation is not supported by this cipher.
359 * Since it's impossible to initialize this cipher given the
360 * current Cipher.engineInit(...) implementation,
361 * IllegalStateException will always be thrown upon invocation.
362 *
363 * @param in the input buffer.
364 * @param inOffset the offset in <code>in</code> where the input
365 * starts.
366 * @param inLen the input length.
367 * @param out the buffer for the result.
368 * @param outOffset the ofset in <code>out</code> where the result
369 * is stored.
370 *
371 * @return the number of bytes stored in <code>out</code>.
372 *
373 * @exception IllegalStateException upon invocation of this method.
374 */
375 protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
376 byte[] output, int outputOffset)
377 throws IllegalBlockSizeException, ShortBufferException,
378 BadPaddingException {
379 throw new IllegalStateException("Cipher has not been initialized");
380 }
381
382 /**
383 * Returns the parameters used with this cipher.
384 * Note that null maybe returned if this cipher does not use any
385 * parameters or when it has not be set, e.g. initialized with
386 * UNWRAP_MODE but wrapped key data has not been given.
387 *
388 * @return the parameters used with this cipher; can be null.
389 */
390 protected AlgorithmParameters engineGetParameters() {
391 AlgorithmParameters params = null;
|
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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.crypto.provider;
27
28 import java.security.*;
29 import java.security.spec.*;
30 import javax.crypto.*;
31 import javax.crypto.spec.*;
32
33 /**
34 * This class implements the CMS DESede KeyWrap algorithm as defined
35 * in <a href=http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap></a>
36 * "XML Encryption Syntax and Processing" section 5.6.2
37 * "CMS Triple DES Key Wrap".
38 * Note: only <code>CBC</code> mode and <code>NoPadding</code> padding
39 * scheme can be used for this algorithm.
40 *
41 * @author Valerie Peng
42 *
43 *
44 * @see DESedeCipher
45 */
46 public final class DESedeWrapCipher extends CipherSpi {
47
48 private static final byte[] IV2 = {
49 (byte) 0x4a, (byte) 0xdd, (byte) 0xa2, (byte) 0x2c,
50 (byte) 0x79, (byte) 0xe8, (byte) 0x21, (byte) 0x05
51 };
52
53 private static final int CHECKSUM_LEN = 8;
54 private static final int IV_LEN = 8;
55
142 } else {
143 result = inputLen + 16;
144 }
145 return (result < 0? 0:result);
146 }
147
148 /**
149 * Returns the initialization vector (IV) in a new buffer.
150 *
151 * @return the initialization vector, or null if the underlying
152 * algorithm does not use an IV, or if the IV has not yet
153 * been set.
154 */
155 protected byte[] engineGetIV() {
156 return (iv == null) ? null : iv.clone();
157 }
158
159 /**
160 * Initializes this cipher with a key and a source of randomness.
161 *
162 * <p>The cipher only supports the following two operation modes:
163 * {@code Cipher.WRAP_MODE}, and {@code Cipher.UNWRAP_MODE}.
164 * <p>For modes other than the above two, UnsupportedOperationException
165 * will be thrown.
166 * <p>If this cipher requires an initialization vector (IV), it will get
167 * it from <code>random</code>.
168 *
169 * @param opmode the operation mode of this cipher. Only
170 * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
171 * @param key the secret key.
172 * @param random the source of randomness.
173 *
174 * @exception InvalidKeyException if the given key is inappropriate
175 * or if parameters are required but not supplied.
176 */
177 protected void engineInit(int opmode, Key key, SecureRandom random)
178 throws InvalidKeyException {
179 try {
180 engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
181 } catch (InvalidAlgorithmParameterException iape) {
182 // should never happen
183 InvalidKeyException ike =
184 new InvalidKeyException("Parameters required");
185 ike.initCause(iape);
186 throw ike;
187 }
188 }
189
190 /**
191 * Initializes this cipher with a key, a set of algorithm parameters,
192 * and a source of randomness.
193 *
194 * <p>The cipher only supports the following two operation modes:
195 * {@code Cipher.WRAP_MODE}, and {@code Cipher.UNWRAP_MODE}.
196 * <p>For modes other than the above two, UnsupportedOperationException
197 * will be thrown.
198 * <p>If this cipher requires an initialization vector (IV), it will get
199 * it from <code>random</code>.
200 *
201 * @param opmode the operation mode of this cipher. Only
202 * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
203 * @param key the secret key.
204 * @param params the algorithm parameters.
205 * @param random the source of randomness.
206 *
207 * @exception InvalidKeyException if the given key is inappropriate.
208 * @exception InvalidAlgorithmParameterException if the given algorithm
209 * parameters are inappropriate for this cipher.
210 */
211 protected void engineInit(int opmode, Key key,
212 AlgorithmParameterSpec params,
213 SecureRandom random)
214 throws InvalidKeyException, InvalidAlgorithmParameterException {
215 byte[] currIv = null;
233 if (params != null) {
234 throw new InvalidAlgorithmParameterException
235 ("No parameter accepted for unwrapping keys");
236 }
237 iv = null;
238 decrypting = true;
239 currIv = IV2;
240 } else {
241 throw new UnsupportedOperationException("This cipher can " +
242 "only be used for key wrapping and unwrapping");
243 }
244 cipher.init(decrypting, key.getAlgorithm(), key.getEncoded(),
245 currIv);
246 cipherKey = key;
247 }
248
249 /**
250 * Initializes this cipher with a key, a set of algorithm parameters,
251 * and a source of randomness.
252 *
253 * <p>The cipher only supports the following two operation modes:
254 * {@code Cipher.WRAP_MODE}, and {@code Cipher.UNWRAP_MODE}.
255 * <p>For modes other than the above two, UnsupportedOperationException
256 * will be thrown.
257 * <p>If this cipher requires an initialization vector (IV), it will get
258 * it from <code>random</code>.
259 *
260 * @param opmode the operation mode of this cipher. Only
261 * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted.
262 * @param key the secret key.
263 * @param params the algorithm parameters.
264 * @param random the source of randomness.
265 *
266 * @exception InvalidKeyException if the given key is inappropriate.
267 * @exception InvalidAlgorithmParameterException if the given algorithm
268 * parameters are inappropriate for this cipher.
269 */
270 protected void engineInit(int opmode, Key key,
271 AlgorithmParameters params,
272 SecureRandom random)
273 throws InvalidKeyException, InvalidAlgorithmParameterException {
274 IvParameterSpec ivSpec = null;
340 * @param in the input buffer.
341 * @param inOffset the offset in <code>in</code> where the input
342 * starts.
343 * @param inLen the input length.
344 *
345 * @return the new buffer with the result.
346 *
347 * @exception IllegalStateException upon invocation of this method.
348 */
349 protected byte[] engineDoFinal(byte[] in, int inOffset, int inLen)
350 throws IllegalBlockSizeException, BadPaddingException {
351 throw new IllegalStateException("Cipher has not been initialized");
352 }
353
354 /**
355 * This operation is not supported by this cipher.
356 * Since it's impossible to initialize this cipher given the
357 * current Cipher.engineInit(...) implementation,
358 * IllegalStateException will always be thrown upon invocation.
359 *
360 * @param input the input buffer.
361 * @param inputOffset the offset in {@code input} where the input
362 * starts.
363 * @param inputLen the input length.
364 * @param output the buffer for the result.
365 * @param outputOffset the ofset in {@code output} where the result
366 * is stored.
367 *
368 * @return the number of bytes stored in {@code out}.
369 *
370 * @exception IllegalStateException upon invocation of this method.
371 */
372 protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
373 byte[] output, int outputOffset)
374 throws IllegalBlockSizeException, ShortBufferException,
375 BadPaddingException {
376 throw new IllegalStateException("Cipher has not been initialized");
377 }
378
379 /**
380 * Returns the parameters used with this cipher.
381 * Note that null maybe returned if this cipher does not use any
382 * parameters or when it has not be set, e.g. initialized with
383 * UNWRAP_MODE but wrapped key data has not been given.
384 *
385 * @return the parameters used with this cipher; can be null.
386 */
387 protected AlgorithmParameters engineGetParameters() {
388 AlgorithmParameters params = null;
|