Source for gnu.javax.crypto.key.srp6.SRP6User

   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:  * The implementation of the User in the SRP-6 protocol.
  56:  * <p>
  57:  * Reference:
  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
  64:     extends SRP6KeyAgreement
  65: {
  66:   /** The user's identity. */
  67:   private String I;
  68:   /** The user's cleartext password. */
  69:   private byte[] p;
  70:   /** The user's ephemeral key pair. */
  71:   private KeyPair userKeyPair;
  72: 
  73:   // default 0-arguments constructor
  74: 
  75:   protected void engineInit(final Map attributes) throws KeyAgreementException
  76:   {
  77:     rnd = (SecureRandom) attributes.get(SOURCE_OF_RANDOMNESS);
  78:     N = (BigInteger) attributes.get(SHARED_MODULUS);
  79:     if (N == null)
  80:       throw new KeyAgreementException("missing shared modulus");
  81:     g = (BigInteger) attributes.get(GENERATOR);
  82:     if (g == null)
  83:       throw new KeyAgreementException("missing generator");
  84:     final String md = (String) attributes.get(HASH_FUNCTION);
  85:     if (md == null || md.trim().length() == 0)
  86:       throw new KeyAgreementException("missing hash function");
  87:     srp = SRP.instance(md);
  88:     I = (String) attributes.get(USER_IDENTITY);
  89:     if (I == null)
  90:       throw new KeyAgreementException("missing user identity");
  91:     p = (byte[]) attributes.get(USER_PASSWORD);
  92:     if (p == null)
  93:       throw new KeyAgreementException("missing user password");
  94:   }
  95: 
  96:   protected OutgoingMessage engineProcessMessage(final IncomingMessage in)
  97:       throws KeyAgreementException
  98:   {
  99:     switch (step)
 100:       {
 101:       case 0:
 102:         return sendIdentity(in);
 103:       case 1:
 104:         return computeSharedSecret(in);
 105:       default:
 106:         throw new IllegalStateException("unexpected state");
 107:       }
 108:   }
 109: 
 110:   protected void engineReset()
 111:   {
 112:     I = null;
 113:     p = null;
 114:     userKeyPair = null;
 115:     super.engineReset();
 116:   }
 117: 
 118:   private OutgoingMessage sendIdentity(final IncomingMessage in)
 119:       throws KeyAgreementException
 120:   {
 121:     // generate an ephemeral keypair
 122:     final SRPKeyPairGenerator kpg = new SRPKeyPairGenerator();
 123:     final Map attributes = new HashMap();
 124:     if (rnd != null)
 125:       attributes.put(SRPKeyPairGenerator.SOURCE_OF_RANDOMNESS, rnd);
 126:     attributes.put(SRPKeyPairGenerator.SHARED_MODULUS, N);
 127:     attributes.put(SRPKeyPairGenerator.GENERATOR, g);
 128:     kpg.setup(attributes);
 129:     userKeyPair = kpg.generate();
 130:     final OutgoingMessage result = new OutgoingMessage();
 131:     result.writeString(I);
 132:     result.writeMPI(((SRPPublicKey) userKeyPair.getPublic()).getY());
 133:     return result;
 134:   }
 135: 
 136:   private OutgoingMessage computeSharedSecret(final IncomingMessage in)
 137:       throws KeyAgreementException
 138:   {
 139:     final BigInteger s = in.readMPI();
 140:     final BigInteger B = in.readMPI();
 141:     final BigInteger A = ((SRPPublicKey) userKeyPair.getPublic()).getY();
 142:     final BigInteger u = uValue(A, B); // u = H(A | B)
 143:     final BigInteger x;
 144:     try
 145:       {
 146:         x = new BigInteger(1, srp.computeX(Util.trim(s), I, p));
 147:       }
 148:     catch (Exception e)
 149:       {
 150:         throw new KeyAgreementException("computeSharedSecret()", e);
 151:       }
 152:     // compute S = (B - 3g^x) ^ (a + ux)
 153:     final BigInteger a = ((SRPPrivateKey) userKeyPair.getPrivate()).getX();
 154:     final BigInteger S = B.subtract(THREE.multiply(g.modPow(x, N)))
 155:                           .modPow(a.add(u.multiply(x)), N);
 156:     final byte[] sBytes = Util.trim(S);
 157:     final IMessageDigest hash = srp.newDigest();
 158:     hash.update(sBytes, 0, sBytes.length);
 159:     K = new BigInteger(1, hash.digest());
 160:     complete = true;
 161:     return null;
 162:   }
 163: }