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

« back to all changes in this revision

Viewing changes to gnu/javax/crypto/key/srp6/SRP6User.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
/* SRP6User.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.hash.IMessageDigest;
 
42
import gnu.java.security.util.Util;
 
43
import gnu.javax.crypto.key.KeyAgreementException;
 
44
import gnu.javax.crypto.key.IncomingMessage;
 
45
import gnu.javax.crypto.key.OutgoingMessage;
 
46
import gnu.javax.crypto.sasl.srp.SRP;
 
47
 
 
48
import java.math.BigInteger;
 
49
import java.security.KeyPair;
 
50
import java.security.SecureRandom;
 
51
import java.util.HashMap;
 
52
import java.util.Map;
 
53
 
 
54
/**
 
55
 * <p>The implementation of the User in the SRP-6 protocol.</p>
 
56
 *
 
57
 * <p>Reference:</p>
 
58
 * <ol>
 
59
 *    <li><a href="http://srp.stanford.edu/design.html">SRP Protocol Design</a><br>
 
60
 *    Thomas J. Wu.</li>
 
61
 * </ol>
 
62
 */
 
63
public class SRP6User extends SRP6KeyAgreement
 
64
{
 
65
 
 
66
  // Constants and variables
 
67
  // -------------------------------------------------------------------------
 
68
 
 
69
  /** The user's identity. */
 
70
  private String I;
 
71
 
 
72
  /** The user's cleartext password. */
 
73
  private byte[] p;
 
74
 
 
75
  /** The user's ephemeral key pair. */
 
76
  private KeyPair userKeyPair;
 
77
 
 
78
  // Constructor(s)
 
79
  // -------------------------------------------------------------------------
 
80
 
 
81
  // default 0-arguments constructor
 
82
 
 
83
  // Class methods
 
84
  // -------------------------------------------------------------------------
 
85
 
 
86
  // Instance methods
 
87
  // -------------------------------------------------------------------------
 
88
 
 
89
  // implementation of abstract methods in base class ------------------------
 
90
 
 
91
  protected void engineInit(final Map attributes) throws KeyAgreementException
 
92
  {
 
93
    rnd = (SecureRandom) attributes.get(SOURCE_OF_RANDOMNESS);
 
94
    N = (BigInteger) attributes.get(SHARED_MODULUS);
 
95
    if (N == null)
 
96
      {
 
97
        throw new KeyAgreementException("missing shared modulus");
 
98
      }
 
99
    g = (BigInteger) attributes.get(GENERATOR);
 
100
    if (g == null)
 
101
      {
 
102
        throw new KeyAgreementException("missing generator");
 
103
      }
 
104
 
 
105
    final String md = (String) attributes.get(HASH_FUNCTION);
 
106
    if (md == null || "".equals(md.trim()))
 
107
      {
 
108
        throw new KeyAgreementException("missing hash function");
 
109
      }
 
110
    srp = SRP.instance(md);
 
111
 
 
112
    I = (String) attributes.get(USER_IDENTITY);
 
113
    if (I == null)
 
114
      {
 
115
        throw new KeyAgreementException("missing user identity");
 
116
      }
 
117
    p = (byte[]) attributes.get(USER_PASSWORD);
 
118
    if (p == null)
 
119
      {
 
120
        throw new KeyAgreementException("missing user password");
 
121
      }
 
122
  }
 
123
 
 
124
  protected OutgoingMessage engineProcessMessage(final IncomingMessage in)
 
125
      throws KeyAgreementException
 
126
  {
 
127
    switch (step)
 
128
      {
 
129
      case 0:
 
130
        return sendIdentity(in);
 
131
      case 1:
 
132
        return computeSharedSecret(in);
 
133
      default:
 
134
        throw new IllegalStateException("unexpected state");
 
135
      }
 
136
  }
 
137
 
 
138
  protected void engineReset()
 
139
  {
 
140
    I = null;
 
141
    p = null;
 
142
    userKeyPair = null;
 
143
    super.engineReset();
 
144
  }
 
145
 
 
146
  // own methods -------------------------------------------------------------
 
147
 
 
148
  private OutgoingMessage sendIdentity(final IncomingMessage in)
 
149
      throws KeyAgreementException
 
150
  {
 
151
    // generate an ephemeral keypair
 
152
    final SRPKeyPairGenerator kpg = new SRPKeyPairGenerator();
 
153
    final Map attributes = new HashMap();
 
154
    if (rnd != null)
 
155
      {
 
156
        attributes.put(SRPKeyPairGenerator.SOURCE_OF_RANDOMNESS, rnd);
 
157
      }
 
158
    attributes.put(SRPKeyPairGenerator.SHARED_MODULUS, N);
 
159
    attributes.put(SRPKeyPairGenerator.GENERATOR, g);
 
160
    kpg.setup(attributes);
 
161
    userKeyPair = kpg.generate();
 
162
 
 
163
    final OutgoingMessage result = new OutgoingMessage();
 
164
    result.writeString(I);
 
165
    result.writeMPI(((SRPPublicKey) userKeyPair.getPublic()).getY());
 
166
 
 
167
    return result;
 
168
  }
 
169
 
 
170
  private OutgoingMessage computeSharedSecret(final IncomingMessage in)
 
171
      throws KeyAgreementException
 
172
  {
 
173
    final BigInteger s = in.readMPI();
 
174
    final BigInteger B = in.readMPI();
 
175
 
 
176
    final BigInteger A = ((SRPPublicKey) userKeyPair.getPublic()).getY();
 
177
    final BigInteger u = uValue(A, B); // u = H(A | B)
 
178
 
 
179
    final BigInteger x;
 
180
    try
 
181
      {
 
182
        x = new BigInteger(1, srp.computeX(Util.trim(s), I, p));
 
183
      }
 
184
    catch (Exception e)
 
185
      {
 
186
        throw new KeyAgreementException("computeSharedSecret()", e);
 
187
      }
 
188
 
 
189
    // compute S = (B - 3g^x) ^ (a + ux)
 
190
    final BigInteger a = ((SRPPrivateKey) userKeyPair.getPrivate()).getX();
 
191
    final BigInteger S = B.subtract(THREE.multiply(g.modPow(x, N))).modPow(
 
192
                                                                           a.add(u.multiply(x)),
 
193
                                                                           N);
 
194
 
 
195
    final byte[] sBytes = Util.trim(S);
 
196
    final IMessageDigest hash = srp.newDigest();
 
197
    hash.update(sBytes, 0, sBytes.length);
 
198
    K = new BigInteger(1, hash.digest());
 
199
 
 
200
    complete = true;
 
201
    return null;
 
202
  }
 
203
}