~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to gnu/javax/crypto/key/srp6/SRPPrivateKey.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* SRPPrivateKey.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.srp6;
 
40
 
 
41
import gnu.java.security.Registry;
 
42
import gnu.java.security.key.IKeyPairCodec;
 
43
 
 
44
import java.math.BigInteger;
 
45
import java.security.PrivateKey;
 
46
 
 
47
/**
 
48
 * <p>A representation of an SRP ephemeral private key.</p>
 
49
 *
 
50
 * <p>Reference:</p>
 
51
 * <ol>
 
52
 *    <li><a href="http://srp.stanford.edu/design.html">SRP Protocol Design</a><br>
 
53
 *    Thomas J. Wu.</li>
 
54
 * </ol>
 
55
 */
 
56
public class SRPPrivateKey extends SRPKey implements PrivateKey
 
57
{
 
58
 
 
59
  // Constants and variables
 
60
  // -------------------------------------------------------------------------
 
61
 
 
62
  /**
 
63
   * The private exponent for either the server or the client engaged in the
 
64
   * SRP protocol exchange.
 
65
   */
 
66
  private final BigInteger X;
 
67
 
 
68
  /**
 
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
 
71
   * password .
 
72
   */
 
73
  private final BigInteger v;
 
74
 
 
75
  // Constructor(s)
 
76
  // -------------------------------------------------------------------------
 
77
 
 
78
  /**
 
79
   * <p>Public constructor for use from outside this package.</p>
 
80
   *
 
81
   * @param N the public shared modulus.
 
82
   * @param g the generator.
 
83
   * @param x the private exponent of the ephemeral key.
 
84
   */
 
85
  public SRPPrivateKey(BigInteger N, BigInteger g, BigInteger x)
 
86
  {
 
87
    this(N, g, x, null);
 
88
  }
 
89
 
 
90
  /**
 
91
   * <p>Public constructor for use from outside this package.</p>
 
92
   *
 
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).
 
97
   */
 
98
  public SRPPrivateKey(BigInteger N, BigInteger g, BigInteger x, BigInteger v)
 
99
  {
 
100
    super(N, g);
 
101
 
 
102
    SRPAlgorithm.checkParams(N, g);
 
103
    this.X = x;
 
104
    this.v = v;
 
105
  }
 
106
 
 
107
  /**
 
108
   * <p>Default constructor. Assumes N and g are already validated.</p>
 
109
   *
 
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:
 
113
   * <ol>
 
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>
 
116
   * </ol>
 
117
   */
 
118
  SRPPrivateKey(BigInteger[] params)
 
119
  {
 
120
    super(params[0], params[1]);
 
121
 
 
122
    if (params.length == 3)
 
123
      {
 
124
        X = params[2];
 
125
        v = null;
 
126
      }
 
127
    else if (params.length == 4)
 
128
      {
 
129
        X = params[2];
 
130
        v = params[3];
 
131
      }
 
132
    else
 
133
      {
 
134
        throw new IllegalArgumentException("invalid number of SRP parameters");
 
135
      }
 
136
  }
 
137
 
 
138
  // Class methods
 
139
  // -------------------------------------------------------------------------
 
140
 
 
141
  /**
 
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
 
145
   * object.</p>
 
146
   *
 
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.
 
152
   */
 
153
  public static SRPPrivateKey valueOf(byte[] k)
 
154
  {
 
155
    // check magic...
 
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])
 
162
      {
 
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);
 
166
      }
 
167
    else
 
168
      {
 
169
        throw new IllegalArgumentException("magic");
 
170
      }
 
171
  }
 
172
 
 
173
  // Instance methods
 
174
  // -------------------------------------------------------------------------
 
175
 
 
176
  /**
 
177
   * <p>Returns the private exponent of the key as a {@link BigInteger}.</p>
 
178
   *
 
179
   * @return the private exponent of the key as a {@link BigInteger}.
 
180
   */
 
181
  public BigInteger getX()
 
182
  {
 
183
    return X;
 
184
  }
 
185
 
 
186
  /**
 
187
   * <p>Returns the user's verifier as a {@link BigInteger}.</p>
 
188
   *
 
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
 
191
   * for a User.
 
192
   */
 
193
  public BigInteger getV()
 
194
  {
 
195
    return v;
 
196
  }
 
197
 
 
198
  // Other instance methods --------------------------------------------------
 
199
 
 
200
  /**
 
201
   * <p>Returns the encoded form of this private key according to the
 
202
   * designated format.</p>
 
203
   *
 
204
   * @param format the desired format identifier of the resulting encoding.
 
205
   * @return the byte sequence encoding this key according to the designated
 
206
   * format.
 
207
   * @throws IllegalArgumentException if the format is not supported.
 
208
   */
 
209
  public byte[] getEncoded(int format)
 
210
  {
 
211
    byte[] result;
 
212
    switch (format)
 
213
      {
 
214
      case IKeyPairCodec.RAW_FORMAT:
 
215
        result = new SRPKeyPairRawCodec().encodePrivateKey(this);
 
216
        break;
 
217
      default:
 
218
        throw new IllegalArgumentException("format");
 
219
      }
 
220
    return result;
 
221
  }
 
222
 
 
223
  /**
 
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
 
226
   * one.</p>
 
227
   *
 
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
 
230
   * value as this one.
 
231
   */
 
232
  public boolean equals(Object obj)
 
233
  {
 
234
    if (obj == null)
 
235
      {
 
236
        return false;
 
237
      }
 
238
    if (!(obj instanceof SRPPrivateKey))
 
239
      {
 
240
        return false;
 
241
      }
 
242
    SRPPrivateKey that = (SRPPrivateKey) obj;
 
243
    boolean result = super.equals(that) && X.equals(that.getX());
 
244
    if (v != null)
 
245
      {
 
246
        result = result && v.equals(that.getV());
 
247
      }
 
248
    return result;
 
249
  }
 
250
}