author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 10336 | 0bb1999251f8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.crypto.provider; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.security.AlgorithmParametersSpi; |
|
30 |
import java.security.spec.AlgorithmParameterSpec; |
|
31 |
import java.security.spec.InvalidParameterSpecException; |
|
32 |
import javax.crypto.spec.RC2ParameterSpec; |
|
33 |
import sun.misc.HexDumpEncoder; |
|
34 |
import sun.security.util.*; |
|
35 |
||
36 |
/** |
|
37 |
* This class implements the parameter set used with RC2 |
|
38 |
* encryption, which is defined in RFC2268 as follows: |
|
39 |
* |
|
40 |
* <pre> |
|
41 |
* RC2-CBCParameter ::= CHOICE { |
|
42 |
* iv IV, |
|
43 |
* params SEQUENCE { |
|
44 |
* version RC2Version, |
|
45 |
* iv IV |
|
46 |
* } |
|
47 |
* } |
|
48 |
* |
|
49 |
* where |
|
50 |
* |
|
51 |
* IV ::= OCTET STRING -- 8 octets |
|
52 |
* RC2Version ::= INTEGER -- 1-1024 |
|
53 |
* </pre> |
|
54 |
* |
|
55 |
* @author Sean Mullan |
|
56 |
* @since 1.5 |
|
57 |
*/ |
|
58 |
public final class RC2Parameters extends AlgorithmParametersSpi { |
|
59 |
||
60 |
// TABLE[EKB] from section 6 of RFC 2268, used to convert effective key |
|
61 |
// size to/from encoded version number |
|
62 |
private final static int[] EKB_TABLE = new int[] { |
|
63 |
0xbd, 0x56, 0xea, 0xf2, 0xa2, 0xf1, 0xac, 0x2a, |
|
64 |
0xb0, 0x93, 0xd1, 0x9c, 0x1b, 0x33, 0xfd, 0xd0, |
|
65 |
0x30, 0x04, 0xb6, 0xdc, 0x7d, 0xdf, 0x32, 0x4b, |
|
66 |
0xf7, 0xcb, 0x45, 0x9b, 0x31, 0xbb, 0x21, 0x5a, |
|
67 |
0x41, 0x9f, 0xe1, 0xd9, 0x4a, 0x4d, 0x9e, 0xda, |
|
68 |
0xa0, 0x68, 0x2c, 0xc3, 0x27, 0x5f, 0x80, 0x36, |
|
69 |
0x3e, 0xee, 0xfb, 0x95, 0x1a, 0xfe, 0xce, 0xa8, |
|
70 |
0x34, 0xa9, 0x13, 0xf0, 0xa6, 0x3f, 0xd8, 0x0c, |
|
71 |
0x78, 0x24, 0xaf, 0x23, 0x52, 0xc1, 0x67, 0x17, |
|
72 |
0xf5, 0x66, 0x90, 0xe7, 0xe8, 0x07, 0xb8, 0x60, |
|
73 |
0x48, 0xe6, 0x1e, 0x53, 0xf3, 0x92, 0xa4, 0x72, |
|
74 |
0x8c, 0x08, 0x15, 0x6e, 0x86, 0x00, 0x84, 0xfa, |
|
75 |
0xf4, 0x7f, 0x8a, 0x42, 0x19, 0xf6, 0xdb, 0xcd, |
|
76 |
0x14, 0x8d, 0x50, 0x12, 0xba, 0x3c, 0x06, 0x4e, |
|
77 |
0xec, 0xb3, 0x35, 0x11, 0xa1, 0x88, 0x8e, 0x2b, |
|
78 |
0x94, 0x99, 0xb7, 0x71, 0x74, 0xd3, 0xe4, 0xbf, |
|
79 |
0x3a, 0xde, 0x96, 0x0e, 0xbc, 0x0a, 0xed, 0x77, |
|
80 |
0xfc, 0x37, 0x6b, 0x03, 0x79, 0x89, 0x62, 0xc6, |
|
81 |
0xd7, 0xc0, 0xd2, 0x7c, 0x6a, 0x8b, 0x22, 0xa3, |
|
82 |
0x5b, 0x05, 0x5d, 0x02, 0x75, 0xd5, 0x61, 0xe3, |
|
83 |
0x18, 0x8f, 0x55, 0x51, 0xad, 0x1f, 0x0b, 0x5e, |
|
84 |
0x85, 0xe5, 0xc2, 0x57, 0x63, 0xca, 0x3d, 0x6c, |
|
85 |
0xb4, 0xc5, 0xcc, 0x70, 0xb2, 0x91, 0x59, 0x0d, |
|
86 |
0x47, 0x20, 0xc8, 0x4f, 0x58, 0xe0, 0x01, 0xe2, |
|
87 |
0x16, 0x38, 0xc4, 0x6f, 0x3b, 0x0f, 0x65, 0x46, |
|
88 |
0xbe, 0x7e, 0x2d, 0x7b, 0x82, 0xf9, 0x40, 0xb5, |
|
89 |
0x1d, 0x73, 0xf8, 0xeb, 0x26, 0xc7, 0x87, 0x97, |
|
90 |
0x25, 0x54, 0xb1, 0x28, 0xaa, 0x98, 0x9d, 0xa5, |
|
91 |
0x64, 0x6d, 0x7a, 0xd4, 0x10, 0x81, 0x44, 0xef, |
|
92 |
0x49, 0xd6, 0xae, 0x2e, 0xdd, 0x76, 0x5c, 0x2f, |
|
93 |
0xa7, 0x1c, 0xc9, 0x09, 0x69, 0x9a, 0x83, 0xcf, |
|
94 |
0x29, 0x39, 0xb9, 0xe9, 0x4c, 0xff, 0x43, 0xab |
|
95 |
}; |
|
96 |
||
97 |
// the iv |
|
98 |
private byte[] iv; |
|
99 |
||
100 |
// the version number |
|
101 |
private int version = 0; |
|
102 |
||
103 |
// the effective key size |
|
104 |
private int effectiveKeySize = 0; |
|
105 |
||
106 |
public RC2Parameters() {} |
|
107 |
||
108 |
protected void engineInit(AlgorithmParameterSpec paramSpec) |
|
109 |
throws InvalidParameterSpecException { |
|
110 |
||
111 |
if (!(paramSpec instanceof RC2ParameterSpec)) { |
|
112 |
throw new InvalidParameterSpecException |
|
113 |
("Inappropriate parameter specification"); |
|
114 |
} |
|
115 |
RC2ParameterSpec rps = (RC2ParameterSpec) paramSpec; |
|
116 |
||
117 |
// check effective key size (a value of 0 means it is unspecified) |
|
118 |
effectiveKeySize = rps.getEffectiveKeyBits(); |
|
119 |
if (effectiveKeySize != 0) { |
|
120 |
if (effectiveKeySize < 1 || effectiveKeySize > 1024) { |
|
121 |
throw new InvalidParameterSpecException("RC2 effective key " + |
|
122 |
"size must be between 1 and 1024 bits"); |
|
123 |
} |
|
124 |
if (effectiveKeySize < 256) { |
|
125 |
version = EKB_TABLE[effectiveKeySize]; |
|
126 |
} else { |
|
127 |
version = effectiveKeySize; |
|
128 |
} |
|
129 |
} |
|
130 |
this.iv = rps.getIV(); |
|
131 |
} |
|
132 |
||
133 |
protected void engineInit(byte[] encoded) throws IOException { |
|
134 |
DerValue val = new DerValue(encoded); |
|
135 |
// check if IV or params |
|
136 |
if (val.tag == DerValue.tag_Sequence) { |
|
137 |
val.data.reset(); |
|
138 |
||
139 |
version = val.data.getInteger(); |
|
140 |
if (version < 0 || version > 1024) { |
|
141 |
throw new IOException("RC2 parameter parsing error: " + |
|
142 |
"version number out of legal range (0-1024): " + version); |
|
143 |
} |
|
144 |
if (version > 255) { |
|
145 |
effectiveKeySize = version; |
|
146 |
} else { |
|
147 |
// search table for index containing version |
|
148 |
for (int i = 0; i < EKB_TABLE.length; i++) { |
|
149 |
if (version == EKB_TABLE[i]) { |
|
150 |
effectiveKeySize = i; |
|
151 |
break; |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
iv = val.data.getOctetString(); |
|
157 |
} else { |
|
158 |
val.data.reset(); |
|
159 |
iv = val.getOctetString(); |
|
160 |
version = 0; |
|
161 |
effectiveKeySize = 0; |
|
162 |
} |
|
163 |
||
164 |
if (iv.length != 8) { |
|
165 |
throw new IOException("RC2 parameter parsing error: iv length " + |
|
166 |
"must be 8 bits, actual: " + iv.length); |
|
167 |
} |
|
168 |
||
169 |
if (val.data.available() != 0) { |
|
170 |
throw new IOException("RC2 parameter parsing error: extra data"); |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
protected void engineInit(byte[] encoded, String decodingMethod) |
|
175 |
throws IOException { |
|
176 |
engineInit(encoded); |
|
177 |
} |
|
178 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
179 |
protected <T extends AlgorithmParameterSpec> |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
180 |
T engineGetParameterSpec(Class<T> paramSpec) |
2 | 181 |
throws InvalidParameterSpecException { |
182 |
||
183 |
if (RC2ParameterSpec.class.isAssignableFrom(paramSpec)) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
184 |
return paramSpec.cast((iv == null ? |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
185 |
new RC2ParameterSpec(effectiveKeySize) : |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
186 |
new RC2ParameterSpec(effectiveKeySize, iv))); |
2 | 187 |
} else { |
188 |
throw new InvalidParameterSpecException |
|
189 |
("Inappropriate parameter specification"); |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
protected byte[] engineGetEncoded() throws IOException { |
|
194 |
DerOutputStream out = new DerOutputStream(); |
|
195 |
DerOutputStream bytes = new DerOutputStream(); |
|
196 |
||
197 |
if (this.effectiveKeySize != 0) { |
|
198 |
bytes.putInteger(version); |
|
199 |
bytes.putOctetString(iv); |
|
200 |
out.write(DerValue.tag_Sequence, bytes); |
|
201 |
} else { |
|
202 |
out.putOctetString(iv); |
|
203 |
} |
|
204 |
||
205 |
return out.toByteArray(); |
|
206 |
} |
|
207 |
||
208 |
protected byte[] engineGetEncoded(String encodingMethod) |
|
209 |
throws IOException { |
|
210 |
return engineGetEncoded(); |
|
211 |
} |
|
212 |
||
213 |
/* |
|
214 |
* Returns a formatted string describing the parameters. |
|
215 |
*/ |
|
216 |
protected String engineToString() { |
|
217 |
String LINE_SEP = System.getProperty("line.separator"); |
|
218 |
HexDumpEncoder encoder = new HexDumpEncoder(); |
|
219 |
StringBuilder sb |
|
220 |
= new StringBuilder(LINE_SEP + " iv:" + LINE_SEP + "[" |
|
221 |
+ encoder.encodeBuffer(iv) + "]"); |
|
222 |
||
223 |
if (version != 0) { |
|
224 |
sb.append(LINE_SEP + "version:" + LINE_SEP |
|
225 |
+ version + LINE_SEP); |
|
226 |
} |
|
227 |
return sb.toString(); |
|
228 |
} |
|
229 |
} |