1
/* SRPPrivateKey.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
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
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.srp6;
41
import gnu.java.security.Registry;
42
import gnu.java.security.key.IKeyPairCodec;
44
import java.math.BigInteger;
45
import java.security.PrivateKey;
48
* <p>A representation of an SRP ephemeral private key.</p>
52
* <li><a href="http://srp.stanford.edu/design.html">SRP Protocol Design</a><br>
56
public class SRPPrivateKey extends SRPKey implements PrivateKey
59
// Constants and variables
60
// -------------------------------------------------------------------------
63
* The private exponent for either the server or the client engaged in the
64
* SRP protocol exchange.
66
private final BigInteger X;
69
* The user's verifier (v) --for the server-- also computed at the client
70
* side as g.modPow(x, N), where x is the hashed output of the user name and
73
private final BigInteger v;
76
// -------------------------------------------------------------------------
79
* <p>Public constructor for use from outside this package.</p>
81
* @param N the public shared modulus.
82
* @param g the generator.
83
* @param x the private exponent of the ephemeral key.
85
public SRPPrivateKey(BigInteger N, BigInteger g, BigInteger x)
91
* <p>Public constructor for use from outside this package.</p>
93
* @param N the public shared modulus.
94
* @param g the generator.
95
* @param x the private exponent of the ephemeral key.
96
* @param v the user's verifier value (for the server side only).
98
public SRPPrivateKey(BigInteger N, BigInteger g, BigInteger x, BigInteger v)
102
SRPAlgorithm.checkParams(N, g);
108
* <p>Default constructor. Assumes N and g are already validated.</p>
110
* @param params an array of either 3 or 4 values representing N, g, and
111
* either v and X for the server, or just X for the client. Those values
112
* represent the following:
114
* <li>v (server side): the user's verifier.</li>
115
* <li>X (both sides): the server's or client's ephemeral private exponent.</li>
118
SRPPrivateKey(BigInteger[] params)
120
super(params[0], params[1]);
122
if (params.length == 3)
127
else if (params.length == 4)
134
throw new IllegalArgumentException("invalid number of SRP parameters");
139
// -------------------------------------------------------------------------
142
* <p>A class method that takes the output of the <code>encodePrivateKey()</code>
143
* method of an SRP keypair codec object (an instance implementing
144
* {@link IKeyPairCodec} for DSS keys, and re-constructs an instance of this
147
* @param k the contents of a previously encoded instance of this object.
148
* @throws ArrayIndexOutOfBoundsException if there is not enough bytes, in
149
* <code>k</code>, to represent a valid encoding of an instance of this object.
150
* @throws IllegalArgumentException if the byte sequence does not represent a
151
* valid encoding of an instance of this object.
153
public static SRPPrivateKey valueOf(byte[] k)
156
// we should parse here enough bytes to know which codec to use, and
157
// direct the byte array to the appropriate codec. since we only have one
158
// codec, we could have immediately tried it; nevertheless since testing
159
// one byte is cheaper than instatiating a codec that will fail we test
160
// the first byte before we carry on.
161
if (k[0] == Registry.MAGIC_RAW_SRP_PRIVATE_KEY[0])
163
// it's likely to be in raw format. get a raw codec and hand it over
164
IKeyPairCodec codec = new SRPKeyPairRawCodec();
165
return (SRPPrivateKey) codec.decodePrivateKey(k);
169
throw new IllegalArgumentException("magic");
174
// -------------------------------------------------------------------------
177
* <p>Returns the private exponent of the key as a {@link BigInteger}.</p>
179
* @return the private exponent of the key as a {@link BigInteger}.
181
public BigInteger getX()
187
* <p>Returns the user's verifier as a {@link BigInteger}.</p>
189
* @return the user's verifier as a {@link BigInteger} if this is an SRP
190
* private key of a Host, or <code>null</code> if this is a private SRP key
193
public BigInteger getV()
198
// Other instance methods --------------------------------------------------
201
* <p>Returns the encoded form of this private key according to the
202
* designated format.</p>
204
* @param format the desired format identifier of the resulting encoding.
205
* @return the byte sequence encoding this key according to the designated
207
* @throws IllegalArgumentException if the format is not supported.
209
public byte[] getEncoded(int format)
214
case IKeyPairCodec.RAW_FORMAT:
215
result = new SRPKeyPairRawCodec().encodePrivateKey(this);
218
throw new IllegalArgumentException("format");
224
* <p>Returns <code>true</code> if the designated object is an instance of
225
* <code>SRPPrivateKey</code> and has the same SRP parameter values as this
228
* @param obj the other non-null SRP key to compare to.
229
* @return <code>true</code> if the designated object is of the same type and
232
public boolean equals(Object obj)
238
if (!(obj instanceof SRPPrivateKey))
242
SRPPrivateKey that = (SRPPrivateKey) obj;
243
boolean result = super.equals(that) && X.equals(that.getX());
246
result = result && v.equals(that.getV());