Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / key / dh / DHKeyPairRawCodec.java
blobc0ff82bea523c7a066c36a20a7f9696790fb6969
1 /* DHKeyPairRawCodec.java --
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 USA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.crypto.key.dh;
41 import gnu.java.security.Registry;
42 import gnu.java.security.key.IKeyPairCodec;
44 import java.io.ByteArrayOutputStream;
45 import java.math.BigInteger;
46 import java.security.PrivateKey;
47 import java.security.PublicKey;
49 /**
50 * <p>An object that implements the {@link IKeyPairCodec} operations for the
51 * <i>Raw</i> format to use with Diffie-Hellman keypairs.</p>
53 public class DHKeyPairRawCodec implements IKeyPairCodec
56 // Constants and variables
57 // -------------------------------------------------------------------------
59 // Constructor(s)
60 // -------------------------------------------------------------------------
62 // implicit 0-arguments ctor
64 // Class methods
65 // -------------------------------------------------------------------------
67 // Instance methods
68 // -------------------------------------------------------------------------
70 // gnu.crypto.keys.IKeyPairCodec interface implementation -------------------
72 public int getFormatID()
74 return RAW_FORMAT;
77 /**
78 * <p>Returns the encoded form of the designated Diffie-Hellman public key
79 * according to the <i>Raw</i> format supported by this library.</p>
81 * <p>The <i>Raw</i> format for a DH public key, in this implementation, is
82 * a byte sequence consisting of the following:</p>
84 * <ol>
85 * <li>4-byte magic consisting of the value of the literal
86 * {@link Registry#MAGIC_RAW_DH_PUBLIC_KEY},<li>
87 * <li>1-byte version consisting of the constant: 0x01,</li>
88 * <li>4-byte count of following bytes representing the DH parameter
89 * <code>q</code> in internet order,</li>
90 * <li>n-bytes representation of a {@link BigInteger} obtained by invoking
91 * the <code>toByteArray()</code> method on the DH parameter <code>q</code>,</li>
92 * <li>4-byte count of following bytes representing the DH parameter
93 * <code>p</code> in internet order,</li>
94 * <li>n-bytes representation of a {@link BigInteger} obtained by invoking
95 * the <code>toByteArray()</code> method on the DH parameter <code>p</code>,</li>
96 * <li>4-byte count of following bytes representing the DH parameter
97 * <code>g</code>,</li>
98 * <li>n-bytes representation of a {@link BigInteger} obtained by invoking
99 * the <code>toByteArray()</code> method on the DH parameter <code>g</code>,</li>
100 * <li>4-byte count of following bytes representing the DH parameter
101 * <code>y</code>,</li>
102 * <li>n-bytes representation of a {@link BigInteger} obtained by invoking
103 * the <code>toByteArray()</code> method on the DH parameter <code>y</code>,</li>
104 * </ol>
106 * @param key the key to encode.
107 * @return the <i>Raw</i> format encoding of the designated key.
108 * @throws IllegalArgumentException if the designated key is not a DH one.
109 * @see Registry#MAGIC_RAW_DH_PUBLIC_KEY
111 public byte[] encodePublicKey(PublicKey key)
113 if (!(key instanceof GnuDHPublicKey))
115 throw new IllegalArgumentException("key");
118 GnuDHPublicKey dhKey = (GnuDHPublicKey) key;
119 ByteArrayOutputStream baos = new ByteArrayOutputStream();
121 // magic
122 baos.write(Registry.MAGIC_RAW_DH_PUBLIC_KEY[0]);
123 baos.write(Registry.MAGIC_RAW_DH_PUBLIC_KEY[1]);
124 baos.write(Registry.MAGIC_RAW_DH_PUBLIC_KEY[2]);
125 baos.write(Registry.MAGIC_RAW_DH_PUBLIC_KEY[3]);
127 // version
128 baos.write(0x01);
130 // q
131 byte[] buffer = dhKey.getQ().toByteArray();
132 int length = buffer.length;
133 baos.write(length >>> 24);
134 baos.write((length >>> 16) & 0xFF);
135 baos.write((length >>> 8) & 0xFF);
136 baos.write(length & 0xFF);
137 baos.write(buffer, 0, length);
139 // p
140 buffer = dhKey.getParams().getP().toByteArray();
141 length = buffer.length;
142 baos.write(length >>> 24);
143 baos.write((length >>> 16) & 0xFF);
144 baos.write((length >>> 8) & 0xFF);
145 baos.write(length & 0xFF);
146 baos.write(buffer, 0, length);
148 // g
149 buffer = dhKey.getParams().getG().toByteArray();
150 length = buffer.length;
151 baos.write(length >>> 24);
152 baos.write((length >>> 16) & 0xFF);
153 baos.write((length >>> 8) & 0xFF);
154 baos.write(length & 0xFF);
155 baos.write(buffer, 0, length);
157 // y
158 buffer = dhKey.getY().toByteArray();
159 length = buffer.length;
160 baos.write(length >>> 24);
161 baos.write((length >>> 16) & 0xFF);
162 baos.write((length >>> 8) & 0xFF);
163 baos.write(length & 0xFF);
164 baos.write(buffer, 0, length);
166 return baos.toByteArray();
169 public PublicKey decodePublicKey(byte[] k)
171 // magic
172 if (k[0] != Registry.MAGIC_RAW_DH_PUBLIC_KEY[0]
173 || k[1] != Registry.MAGIC_RAW_DH_PUBLIC_KEY[1]
174 || k[2] != Registry.MAGIC_RAW_DH_PUBLIC_KEY[2]
175 || k[3] != Registry.MAGIC_RAW_DH_PUBLIC_KEY[3])
177 throw new IllegalArgumentException("magic");
180 // version
181 if (k[4] != 0x01)
183 throw new IllegalArgumentException("version");
185 int i = 5;
186 int l;
187 byte[] buffer;
189 // q
190 l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8
191 | (k[i++] & 0xFF);
192 buffer = new byte[l];
193 System.arraycopy(k, i, buffer, 0, l);
194 i += l;
195 BigInteger q = new BigInteger(1, buffer);
197 // p
198 l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8
199 | (k[i++] & 0xFF);
200 buffer = new byte[l];
201 System.arraycopy(k, i, buffer, 0, l);
202 i += l;
203 BigInteger p = new BigInteger(1, buffer);
205 // g
206 l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8
207 | (k[i++] & 0xFF);
208 buffer = new byte[l];
209 System.arraycopy(k, i, buffer, 0, l);
210 i += l;
211 BigInteger g = new BigInteger(1, buffer);
213 // y
214 l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8
215 | (k[i++] & 0xFF);
216 buffer = new byte[l];
217 System.arraycopy(k, i, buffer, 0, l);
218 i += l;
219 BigInteger y = new BigInteger(1, buffer);
221 return new GnuDHPublicKey(q, p, g, y);
225 * <p>Returns the encoded form of the designated Diffie-Hellman private key
226 * according to the <i>Raw</i> format supported by this library.</p>
228 * <p>The <i>Raw</i> format for a DH private key, in this implementation, is
229 * a byte sequence consisting of the following:</p>
231 * <ol>
232 * <li>4-byte magic consisting of the value of the literal
233 * {@link Registry#MAGIC_RAW_DH_PRIVATE_KEY},<li>
234 * <li>1-byte version consisting of the constant: 0x01,</li>
235 * <li>4-byte count of following bytes representing the DH parameter
236 * <code>q</code>,</li>
237 * <li>n-bytes representation of a {@link BigInteger} obtained by invoking
238 * the <code>toByteArray()</code> method on the DH parameter <code>q</code>,</li>
239 * <li>4-byte count of following bytes representing the DH parameter
240 * <code>p</code> in internet order,</li>
241 * <li>n-bytes representation of a {@link BigInteger} obtained by invoking
242 * the <code>toByteArray()</code> method on the DH parameter <code>p</code>,</li>
243 * <li>4-byte count of following bytes representing the DH parameter
244 * <code>g</code>,</li>
245 * <li>n-bytes representation of a {@link BigInteger} obtained by invoking
246 * the <code>toByteArray()</code> method on the DH parameter <code>g</code>,</li>
247 * <li>4-byte count of following bytes representing the DH parameter
248 * <code>x</code>,</li>
249 * <li>n-bytes representation of a {@link BigInteger} obtained by invoking
250 * the <code>toByteArray()</code> method on the DH parameter <code>x</code>,</li>
251 * </ol>
253 * @param key the key to encode.
254 * @return the <i>Raw</i> format encoding of the designated key.
255 * @throws IllegalArgumentException if the designated key is not a DH one.
256 * @see Registry#MAGIC_RAW_DH_PRIVATE_KEY
258 public byte[] encodePrivateKey(PrivateKey key)
260 if (!(key instanceof GnuDHPrivateKey))
262 throw new IllegalArgumentException("key");
265 GnuDHPrivateKey dhKey = (GnuDHPrivateKey) key;
266 ByteArrayOutputStream baos = new ByteArrayOutputStream();
268 // magic
269 baos.write(Registry.MAGIC_RAW_DH_PRIVATE_KEY[0]);
270 baos.write(Registry.MAGIC_RAW_DH_PRIVATE_KEY[1]);
271 baos.write(Registry.MAGIC_RAW_DH_PRIVATE_KEY[2]);
272 baos.write(Registry.MAGIC_RAW_DH_PRIVATE_KEY[3]);
274 // version
275 baos.write(0x01);
277 // q
278 byte[] buffer = dhKey.getQ().toByteArray();
279 int length = buffer.length;
280 baos.write(length >>> 24);
281 baos.write((length >>> 16) & 0xFF);
282 baos.write((length >>> 8) & 0xFF);
283 baos.write(length & 0xFF);
284 baos.write(buffer, 0, length);
286 // p
287 buffer = dhKey.getParams().getP().toByteArray();
288 length = buffer.length;
289 baos.write(length >>> 24);
290 baos.write((length >>> 16) & 0xFF);
291 baos.write((length >>> 8) & 0xFF);
292 baos.write(length & 0xFF);
293 baos.write(buffer, 0, length);
295 // g
296 buffer = dhKey.getParams().getG().toByteArray();
297 length = buffer.length;
298 baos.write(length >>> 24);
299 baos.write((length >>> 16) & 0xFF);
300 baos.write((length >>> 8) & 0xFF);
301 baos.write(length & 0xFF);
302 baos.write(buffer, 0, length);
304 // x
305 buffer = dhKey.getX().toByteArray();
306 length = buffer.length;
307 baos.write(length >>> 24);
308 baos.write((length >>> 16) & 0xFF);
309 baos.write((length >>> 8) & 0xFF);
310 baos.write(length & 0xFF);
311 baos.write(buffer, 0, length);
313 return baos.toByteArray();
316 public PrivateKey decodePrivateKey(byte[] k)
318 // magic
319 if (k[0] != Registry.MAGIC_RAW_DH_PRIVATE_KEY[0]
320 || k[1] != Registry.MAGIC_RAW_DH_PRIVATE_KEY[1]
321 || k[2] != Registry.MAGIC_RAW_DH_PRIVATE_KEY[2]
322 || k[3] != Registry.MAGIC_RAW_DH_PRIVATE_KEY[3])
324 throw new IllegalArgumentException("magic");
327 // version
328 if (k[4] != 0x01)
330 throw new IllegalArgumentException("version");
332 int i = 5;
333 int l;
334 byte[] buffer;
336 // q
337 l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8
338 | (k[i++] & 0xFF);
339 buffer = new byte[l];
340 System.arraycopy(k, i, buffer, 0, l);
341 i += l;
342 BigInteger q = new BigInteger(1, buffer);
344 // p
345 l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8
346 | (k[i++] & 0xFF);
347 buffer = new byte[l];
348 System.arraycopy(k, i, buffer, 0, l);
349 i += l;
350 BigInteger p = new BigInteger(1, buffer);
352 // g
353 l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8
354 | (k[i++] & 0xFF);
355 buffer = new byte[l];
356 System.arraycopy(k, i, buffer, 0, l);
357 i += l;
358 BigInteger g = new BigInteger(1, buffer);
360 // x
361 l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8
362 | (k[i++] & 0xFF);
363 buffer = new byte[l];
364 System.arraycopy(k, i, buffer, 0, l);
365 i += l;
366 BigInteger x = new BigInteger(1, buffer);
368 return new GnuDHPrivateKey(q, p, g, x);