blob: f53338a0351a1839e60b8f2673295cd431652909 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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
26package com.sun.crypto.provider;
27
28import java.io.IOException;
29import java.security.AlgorithmParametersSpi;
30import java.security.spec.AlgorithmParameterSpec;
31import java.security.spec.InvalidParameterSpecException;
32import javax.crypto.spec.RC2ParameterSpec;
33import sun.misc.HexDumpEncoder;
34import 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
59public final class RC2Parameters extends AlgorithmParametersSpi {
60
61 // TABLE[EKB] from section 6 of RFC 2268, used to convert effective key
62 // size to/from encoded version number
63 private final static int[] EKB_TABLE = new int[] {
64 0xbd, 0x56, 0xea, 0xf2, 0xa2, 0xf1, 0xac, 0x2a,
65 0xb0, 0x93, 0xd1, 0x9c, 0x1b, 0x33, 0xfd, 0xd0,
66 0x30, 0x04, 0xb6, 0xdc, 0x7d, 0xdf, 0x32, 0x4b,
67 0xf7, 0xcb, 0x45, 0x9b, 0x31, 0xbb, 0x21, 0x5a,
68 0x41, 0x9f, 0xe1, 0xd9, 0x4a, 0x4d, 0x9e, 0xda,
69 0xa0, 0x68, 0x2c, 0xc3, 0x27, 0x5f, 0x80, 0x36,
70 0x3e, 0xee, 0xfb, 0x95, 0x1a, 0xfe, 0xce, 0xa8,
71 0x34, 0xa9, 0x13, 0xf0, 0xa6, 0x3f, 0xd8, 0x0c,
72 0x78, 0x24, 0xaf, 0x23, 0x52, 0xc1, 0x67, 0x17,
73 0xf5, 0x66, 0x90, 0xe7, 0xe8, 0x07, 0xb8, 0x60,
74 0x48, 0xe6, 0x1e, 0x53, 0xf3, 0x92, 0xa4, 0x72,
75 0x8c, 0x08, 0x15, 0x6e, 0x86, 0x00, 0x84, 0xfa,
76 0xf4, 0x7f, 0x8a, 0x42, 0x19, 0xf6, 0xdb, 0xcd,
77 0x14, 0x8d, 0x50, 0x12, 0xba, 0x3c, 0x06, 0x4e,
78 0xec, 0xb3, 0x35, 0x11, 0xa1, 0x88, 0x8e, 0x2b,
79 0x94, 0x99, 0xb7, 0x71, 0x74, 0xd3, 0xe4, 0xbf,
80 0x3a, 0xde, 0x96, 0x0e, 0xbc, 0x0a, 0xed, 0x77,
81 0xfc, 0x37, 0x6b, 0x03, 0x79, 0x89, 0x62, 0xc6,
82 0xd7, 0xc0, 0xd2, 0x7c, 0x6a, 0x8b, 0x22, 0xa3,
83 0x5b, 0x05, 0x5d, 0x02, 0x75, 0xd5, 0x61, 0xe3,
84 0x18, 0x8f, 0x55, 0x51, 0xad, 0x1f, 0x0b, 0x5e,
85 0x85, 0xe5, 0xc2, 0x57, 0x63, 0xca, 0x3d, 0x6c,
86 0xb4, 0xc5, 0xcc, 0x70, 0xb2, 0x91, 0x59, 0x0d,
87 0x47, 0x20, 0xc8, 0x4f, 0x58, 0xe0, 0x01, 0xe2,
88 0x16, 0x38, 0xc4, 0x6f, 0x3b, 0x0f, 0x65, 0x46,
89 0xbe, 0x7e, 0x2d, 0x7b, 0x82, 0xf9, 0x40, 0xb5,
90 0x1d, 0x73, 0xf8, 0xeb, 0x26, 0xc7, 0x87, 0x97,
91 0x25, 0x54, 0xb1, 0x28, 0xaa, 0x98, 0x9d, 0xa5,
92 0x64, 0x6d, 0x7a, 0xd4, 0x10, 0x81, 0x44, 0xef,
93 0x49, 0xd6, 0xae, 0x2e, 0xdd, 0x76, 0x5c, 0x2f,
94 0xa7, 0x1c, 0xc9, 0x09, 0x69, 0x9a, 0x83, 0xcf,
95 0x29, 0x39, 0xb9, 0xe9, 0x4c, 0xff, 0x43, 0xab
96 };
97
98 // the iv
99 private byte[] iv;
100
101 // the version number
102 private int version = 0;
103
104 // the effective key size
105 private int effectiveKeySize = 0;
106
107 public RC2Parameters() {}
108
109 protected void engineInit(AlgorithmParameterSpec paramSpec)
110 throws InvalidParameterSpecException {
111
112 if (!(paramSpec instanceof RC2ParameterSpec)) {
113 throw new InvalidParameterSpecException
114 ("Inappropriate parameter specification");
115 }
116 RC2ParameterSpec rps = (RC2ParameterSpec) paramSpec;
117
118 // check effective key size (a value of 0 means it is unspecified)
119 effectiveKeySize = rps.getEffectiveKeyBits();
120 if (effectiveKeySize != 0) {
121 if (effectiveKeySize < 1 || effectiveKeySize > 1024) {
122 throw new InvalidParameterSpecException("RC2 effective key " +
123 "size must be between 1 and 1024 bits");
124 }
125 if (effectiveKeySize < 256) {
126 version = EKB_TABLE[effectiveKeySize];
127 } else {
128 version = effectiveKeySize;
129 }
130 }
131 this.iv = rps.getIV();
132 }
133
134 protected void engineInit(byte[] encoded) throws IOException {
135 DerValue val = new DerValue(encoded);
136 // check if IV or params
137 if (val.tag == DerValue.tag_Sequence) {
138 val.data.reset();
139
140 version = val.data.getInteger();
141 if (version < 0 || version > 1024) {
142 throw new IOException("RC2 parameter parsing error: " +
143 "version number out of legal range (0-1024): " + version);
144 }
145 if (version > 255) {
146 effectiveKeySize = version;
147 } else {
148 // search table for index containing version
149 for (int i = 0; i < EKB_TABLE.length; i++) {
150 if (version == EKB_TABLE[i]) {
151 effectiveKeySize = i;
152 break;
153 }
154 }
155 }
156
157 iv = val.data.getOctetString();
158 } else {
159 val.data.reset();
160 iv = val.getOctetString();
161 version = 0;
162 effectiveKeySize = 0;
163 }
164
165 if (iv.length != 8) {
166 throw new IOException("RC2 parameter parsing error: iv length " +
167 "must be 8 bits, actual: " + iv.length);
168 }
169
170 if (val.data.available() != 0) {
171 throw new IOException("RC2 parameter parsing error: extra data");
172 }
173 }
174
175 protected void engineInit(byte[] encoded, String decodingMethod)
176 throws IOException {
177 engineInit(encoded);
178 }
179
180 protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
181 throws InvalidParameterSpecException {
182
183 if (RC2ParameterSpec.class.isAssignableFrom(paramSpec)) {
184 return (iv == null ?
185 new RC2ParameterSpec(effectiveKeySize) :
186 new RC2ParameterSpec(effectiveKeySize, iv));
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}