1 |
769 |
jeremybenn |
/* GnuDHKey.java --
|
2 |
|
|
Copyright (C) 2003, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is a part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.javax.crypto.key.dh;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.Registry;
|
42 |
|
|
import gnu.java.security.action.GetPropertyAction;
|
43 |
|
|
import gnu.java.security.util.FormatUtil;
|
44 |
|
|
|
45 |
|
|
import java.math.BigInteger;
|
46 |
|
|
import java.security.AccessController;
|
47 |
|
|
import java.security.Key;
|
48 |
|
|
|
49 |
|
|
import javax.crypto.interfaces.DHKey;
|
50 |
|
|
import javax.crypto.spec.DHParameterSpec;
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* A base asbtract class for both public and private Diffie-Hellman keys. It
|
54 |
|
|
* encapsulates the two DH numbers: <code>p</code>, and <code>g</code>.
|
55 |
|
|
* <p>
|
56 |
|
|
* According to the JDK, cryptographic <i>Keys</i> all have a <i>format</i>.
|
57 |
|
|
* The format used in this implementation is called <i>Raw</i>, and basically
|
58 |
|
|
* consists of the raw byte sequences of algorithm parameters. The exact order
|
59 |
|
|
* of the byte sequences and the implementation details are given in each of the
|
60 |
|
|
* relevant <code>getEncoded()</code> methods of each of the private and
|
61 |
|
|
* public keys.
|
62 |
|
|
* <p>
|
63 |
|
|
* Reference:
|
64 |
|
|
* <ol>
|
65 |
|
|
* <li><a href="http://www.ietf.org/rfc/rfc2631.txt">Diffie-Hellman Key
|
66 |
|
|
* Agreement Method</a><br>
|
67 |
|
|
* Eric Rescorla.</li>
|
68 |
|
|
* </ol>
|
69 |
|
|
*/
|
70 |
|
|
public abstract class GnuDHKey
|
71 |
|
|
implements Key, DHKey
|
72 |
|
|
{
|
73 |
|
|
/** The public prime q. A prime divisor of p-1. */
|
74 |
|
|
protected BigInteger q;
|
75 |
|
|
/** The public prime p. */
|
76 |
|
|
protected BigInteger p;
|
77 |
|
|
/** The generator g. */
|
78 |
|
|
protected BigInteger g;
|
79 |
|
|
/**
|
80 |
|
|
* Identifier of the default encoding format to use when externalizing the key
|
81 |
|
|
* material.
|
82 |
|
|
*/
|
83 |
|
|
protected final int defaultFormat;
|
84 |
|
|
/** String representation of this key. Cached for speed. */
|
85 |
|
|
private transient String str;
|
86 |
|
|
|
87 |
|
|
/**
|
88 |
|
|
* Trivial protected constructor.
|
89 |
|
|
*
|
90 |
|
|
* @param defaultFormat the identifier of the encoding format to use by
|
91 |
|
|
* default when externalizing the key.
|
92 |
|
|
* @param q a prime divisor of p-1.
|
93 |
|
|
* @param p the public prime.
|
94 |
|
|
* @param g the generator of the group.
|
95 |
|
|
*/
|
96 |
|
|
protected GnuDHKey(int defaultFormat, BigInteger q, BigInteger p, BigInteger g)
|
97 |
|
|
{
|
98 |
|
|
super();
|
99 |
|
|
|
100 |
|
|
this.defaultFormat = defaultFormat <= 0 ? Registry.RAW_ENCODING_ID
|
101 |
|
|
: defaultFormat;
|
102 |
|
|
this.q = q;
|
103 |
|
|
this.p = p;
|
104 |
|
|
this.g = g;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
public DHParameterSpec getParams()
|
108 |
|
|
{
|
109 |
|
|
if (q == null)
|
110 |
|
|
return new DHParameterSpec(p, g);
|
111 |
|
|
return new DHParameterSpec(p, g, q.bitLength());
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
public String getAlgorithm()
|
115 |
|
|
{
|
116 |
|
|
return Registry.DH_KPG;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
/** @deprecated see getEncoded(int). */
|
120 |
|
|
public byte[] getEncoded()
|
121 |
|
|
{
|
122 |
|
|
return getEncoded(defaultFormat);
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
public String getFormat()
|
126 |
|
|
{
|
127 |
|
|
return FormatUtil.getEncodingShortName(defaultFormat);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
public BigInteger getQ()
|
131 |
|
|
{
|
132 |
|
|
return q;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
/**
|
136 |
|
|
* Returns <code>true</code> if the designated object is an instance of
|
137 |
|
|
* {@link DHKey} and has the same Diffie-Hellman parameter values as this one.
|
138 |
|
|
*
|
139 |
|
|
* @param obj the other non-null DH key to compare to.
|
140 |
|
|
* @return <code>true</code> if the designated object is of the same type
|
141 |
|
|
* and value as this one.
|
142 |
|
|
*/
|
143 |
|
|
public boolean equals(Object obj)
|
144 |
|
|
{
|
145 |
|
|
if (obj == null)
|
146 |
|
|
return false;
|
147 |
|
|
if (! (obj instanceof DHKey))
|
148 |
|
|
return false;
|
149 |
|
|
DHKey that = (DHKey) obj;
|
150 |
|
|
return p.equals(that.getParams().getP())
|
151 |
|
|
&& g.equals(that.getParams().getG());
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
public String toString()
|
155 |
|
|
{
|
156 |
|
|
if (str == null)
|
157 |
|
|
{
|
158 |
|
|
String ls = (String) AccessController.doPrivileged
|
159 |
|
|
(new GetPropertyAction("line.separator"));
|
160 |
|
|
StringBuilder sb = new StringBuilder(ls)
|
161 |
|
|
.append("defaultFormat=").append(defaultFormat).append(",").append(ls);
|
162 |
|
|
if (q == null)
|
163 |
|
|
sb.append("q=null,");
|
164 |
|
|
else
|
165 |
|
|
sb.append("q=0x").append(q.toString(16)).append(",");
|
166 |
|
|
sb.append(ls).append("p=0x").append(p.toString(16)).append(",").append(ls)
|
167 |
|
|
.append("g=0x").append(g.toString(16));
|
168 |
|
|
str = sb.toString();
|
169 |
|
|
}
|
170 |
|
|
return str;
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
public abstract byte[] getEncoded(int format);
|
174 |
|
|
}
|