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
.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
;
48 import java
.math
.BigInteger
;
49 import java
.security
.KeyPair
;
50 import java
.security
.SecureRandom
;
51 import java
.util
.HashMap
;
55 * <p>The implementation of the User in the SRP-6 protocol.</p>
59 * <li><a href="http://srp.stanford.edu/design.html">SRP Protocol Design</a><br>
63 public class SRP6User
extends SRP6KeyAgreement
66 // Constants and variables
67 // -------------------------------------------------------------------------
69 /** The user's identity. */
72 /** The user's cleartext password. */
75 /** The user's ephemeral key pair. */
76 private KeyPair userKeyPair
;
79 // -------------------------------------------------------------------------
81 // default 0-arguments constructor
84 // -------------------------------------------------------------------------
87 // -------------------------------------------------------------------------
89 // implementation of abstract methods in base class ------------------------
91 protected void engineInit(final Map attributes
) throws KeyAgreementException
93 rnd
= (SecureRandom
) attributes
.get(SOURCE_OF_RANDOMNESS
);
94 N
= (BigInteger
) attributes
.get(SHARED_MODULUS
);
97 throw new KeyAgreementException("missing shared modulus");
99 g
= (BigInteger
) attributes
.get(GENERATOR
);
102 throw new KeyAgreementException("missing generator");
105 final String md
= (String
) attributes
.get(HASH_FUNCTION
);
106 if (md
== null || "".equals(md
.trim()))
108 throw new KeyAgreementException("missing hash function");
110 srp
= SRP
.instance(md
);
112 I
= (String
) attributes
.get(USER_IDENTITY
);
115 throw new KeyAgreementException("missing user identity");
117 p
= (byte[]) attributes
.get(USER_PASSWORD
);
120 throw new KeyAgreementException("missing user password");
124 protected OutgoingMessage
engineProcessMessage(final IncomingMessage in
)
125 throws KeyAgreementException
130 return sendIdentity(in
);
132 return computeSharedSecret(in
);
134 throw new IllegalStateException("unexpected state");
138 protected void engineReset()
146 // own methods -------------------------------------------------------------
148 private OutgoingMessage
sendIdentity(final IncomingMessage in
)
149 throws KeyAgreementException
151 // generate an ephemeral keypair
152 final SRPKeyPairGenerator kpg
= new SRPKeyPairGenerator();
153 final Map attributes
= new HashMap();
156 attributes
.put(SRPKeyPairGenerator
.SOURCE_OF_RANDOMNESS
, rnd
);
158 attributes
.put(SRPKeyPairGenerator
.SHARED_MODULUS
, N
);
159 attributes
.put(SRPKeyPairGenerator
.GENERATOR
, g
);
160 kpg
.setup(attributes
);
161 userKeyPair
= kpg
.generate();
163 final OutgoingMessage result
= new OutgoingMessage();
164 result
.writeString(I
);
165 result
.writeMPI(((SRPPublicKey
) userKeyPair
.getPublic()).getY());
170 private OutgoingMessage
computeSharedSecret(final IncomingMessage in
)
171 throws KeyAgreementException
173 final BigInteger s
= in
.readMPI();
174 final BigInteger B
= in
.readMPI();
176 final BigInteger A
= ((SRPPublicKey
) userKeyPair
.getPublic()).getY();
177 final BigInteger u
= uValue(A
, B
); // u = H(A | B)
182 x
= new BigInteger(1, srp
.computeX(Util
.trim(s
), I
, p
));
186 throw new KeyAgreementException("computeSharedSecret()", e
);
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
)),
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());