2
|
1 |
/*
|
|
2 |
* Copyright 2003-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.*;
|
|
29 |
import java.security.spec.*;
|
|
30 |
|
|
31 |
import javax.crypto.*;
|
|
32 |
import javax.crypto.spec.RC2ParameterSpec;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* JCE CipherSpi for the RC2(tm) algorithm as described in RFC 2268.
|
|
36 |
* The real code is in CipherCore and RC2Crypt.
|
|
37 |
*
|
|
38 |
* @since 1.5
|
|
39 |
* @author Andreas Sterbenz
|
|
40 |
*/
|
|
41 |
public final class RC2Cipher extends CipherSpi {
|
|
42 |
|
|
43 |
// internal CipherCore & RC2Crypt objects which do the real work.
|
|
44 |
private final CipherCore core;
|
|
45 |
private final RC2Crypt embeddedCipher;
|
|
46 |
|
|
47 |
public RC2Cipher() {
|
|
48 |
SunJCE.ensureIntegrity(getClass());
|
|
49 |
embeddedCipher = new RC2Crypt();
|
|
50 |
core = new CipherCore(embeddedCipher, 8);
|
|
51 |
}
|
|
52 |
|
|
53 |
protected void engineSetMode(String mode)
|
|
54 |
throws NoSuchAlgorithmException {
|
|
55 |
core.setMode(mode);
|
|
56 |
}
|
|
57 |
|
|
58 |
protected void engineSetPadding(String paddingScheme)
|
|
59 |
throws NoSuchPaddingException {
|
|
60 |
core.setPadding(paddingScheme);
|
|
61 |
}
|
|
62 |
|
|
63 |
protected int engineGetBlockSize() {
|
|
64 |
return 8;
|
|
65 |
}
|
|
66 |
|
|
67 |
protected int engineGetOutputSize(int inputLen) {
|
|
68 |
return core.getOutputSize(inputLen);
|
|
69 |
}
|
|
70 |
|
|
71 |
protected byte[] engineGetIV() {
|
|
72 |
return core.getIV();
|
|
73 |
}
|
|
74 |
|
|
75 |
protected AlgorithmParameters engineGetParameters() {
|
|
76 |
return core.getParameters("RC2");
|
|
77 |
}
|
|
78 |
|
|
79 |
protected void engineInit(int opmode, Key key, SecureRandom random)
|
|
80 |
throws InvalidKeyException {
|
|
81 |
embeddedCipher.initEffectiveKeyBits(0);
|
|
82 |
core.init(opmode, key, random);
|
|
83 |
}
|
|
84 |
|
|
85 |
protected void engineInit(int opmode, Key key,
|
|
86 |
AlgorithmParameterSpec params, SecureRandom random)
|
|
87 |
throws InvalidKeyException, InvalidAlgorithmParameterException {
|
|
88 |
if (params != null && params instanceof RC2ParameterSpec) {
|
|
89 |
embeddedCipher.initEffectiveKeyBits
|
|
90 |
(((RC2ParameterSpec)params).getEffectiveKeyBits());
|
|
91 |
} else {
|
|
92 |
embeddedCipher.initEffectiveKeyBits(0);
|
|
93 |
}
|
|
94 |
core.init(opmode, key, params, random);
|
|
95 |
}
|
|
96 |
|
|
97 |
protected void engineInit(int opmode, Key key,
|
|
98 |
AlgorithmParameters params, SecureRandom random)
|
|
99 |
throws InvalidKeyException, InvalidAlgorithmParameterException {
|
|
100 |
if (params != null && params.getAlgorithm().equals("RC2")) {
|
|
101 |
try {
|
|
102 |
RC2ParameterSpec rc2Params = (RC2ParameterSpec)
|
|
103 |
params.getParameterSpec(RC2ParameterSpec.class);
|
|
104 |
engineInit(opmode, key, rc2Params, random);
|
|
105 |
} catch (InvalidParameterSpecException ipse) {
|
|
106 |
throw new InvalidAlgorithmParameterException
|
|
107 |
("Wrong parameter type: RC2 expected");
|
|
108 |
}
|
|
109 |
} else {
|
|
110 |
embeddedCipher.initEffectiveKeyBits(0);
|
|
111 |
core.init(opmode, key, params, random);
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
|
|
116 |
return core.update(in, inOfs, inLen);
|
|
117 |
}
|
|
118 |
|
|
119 |
protected int engineUpdate(byte[] in, int inOfs, int inLen,
|
|
120 |
byte[] out, int outOfs) throws ShortBufferException {
|
|
121 |
return core.update(in, inOfs, inLen, out, outOfs);
|
|
122 |
}
|
|
123 |
|
|
124 |
protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
|
|
125 |
throws IllegalBlockSizeException, BadPaddingException {
|
|
126 |
return core.doFinal(in, inOfs, inLen);
|
|
127 |
}
|
|
128 |
|
|
129 |
protected int engineDoFinal(byte[] in, int inOfs, int inLen,
|
|
130 |
byte[] out, int outOfs) throws IllegalBlockSizeException,
|
|
131 |
ShortBufferException, BadPaddingException {
|
|
132 |
return core.doFinal(in, inOfs, inLen, out, outOfs);
|
|
133 |
}
|
|
134 |
|
|
135 |
protected int engineGetKeySize(Key key) throws InvalidKeyException {
|
|
136 |
byte[] keyBytes = CipherCore.getKeyBytes(key);
|
|
137 |
RC2Crypt.checkKey(key.getAlgorithm(), keyBytes.length);
|
|
138 |
return keyBytes.length << 3;
|
|
139 |
}
|
|
140 |
|
|
141 |
protected byte[] engineWrap(Key key)
|
|
142 |
throws IllegalBlockSizeException, InvalidKeyException {
|
|
143 |
return core.wrap(key);
|
|
144 |
}
|
|
145 |
|
|
146 |
protected Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm,
|
|
147 |
int wrappedKeyType) throws InvalidKeyException,
|
|
148 |
NoSuchAlgorithmException {
|
|
149 |
return core.unwrap(wrappedKey, wrappedKeyAlgorithm, wrappedKeyType);
|
|
150 |
}
|
|
151 |
|
|
152 |
}
|