Source for gnu.javax.crypto.jce.DiffieHellmanImpl

   1: /* DiffieHellmanImpl.java -- implementation of the Diffie-Hellman key agreement.
   2:    Copyright (C) 2005, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is 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, or (at your option)
   9: 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; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 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.jce;
  40: 
  41: import java.math.BigInteger;
  42: import java.security.InvalidKeyException;
  43: import java.security.Key;
  44: import java.security.SecureRandom;
  45: import java.security.spec.AlgorithmParameterSpec;
  46: 
  47: import javax.crypto.KeyAgreementSpi;
  48: import javax.crypto.SecretKey;
  49: import javax.crypto.ShortBufferException;
  50: import javax.crypto.interfaces.DHPrivateKey;
  51: import javax.crypto.interfaces.DHPublicKey;
  52: import javax.crypto.spec.DHParameterSpec;
  53: import javax.crypto.spec.SecretKeySpec;
  54: 
  55: /**
  56:  * The JCE implementation of a 2-party Diffie-Hellman key agreement.
  57:  * 
  58:  * @author Casey Marshall (csm@gnu.org)
  59:  */
  60: public final class DiffieHellmanImpl
  61:     extends KeyAgreementSpi
  62: {
  63:   /** The private key being used for this agreement. */
  64:   private DHPrivateKey key;
  65: 
  66:   /** The current result. */
  67:   private byte[] result;
  68: 
  69:   /** True if the caller told us we are done. */
  70:   private boolean last_phase_done;
  71: 
  72:   /** Trivial default constructor. */
  73:   public DiffieHellmanImpl()
  74:   {
  75:     super();
  76: 
  77:     key = null;
  78:     result = null;
  79:     last_phase_done = false;
  80:   }
  81: 
  82:   protected Key engineDoPhase(Key incoming, boolean lastPhase)
  83:       throws InvalidKeyException
  84:   {
  85:     if (key == null)
  86:       throw new IllegalStateException("Not initialized");
  87: 
  88:     if (last_phase_done)
  89:       throw new IllegalStateException("Last phase already done");
  90: 
  91:     if (! (incoming instanceof DHPublicKey))
  92:       throw new InvalidKeyException("Key MUST be a DHPublicKey");
  93: 
  94:     DHPublicKey pub = (DHPublicKey) incoming;
  95:     DHParameterSpec s1 = key.getParams();
  96:     DHParameterSpec s2 = pub.getParams();
  97:     if (! s1.getG().equals(s2.getG()) || ! s1.getP().equals(s2.getP())
  98:         || s1.getL() != s2.getL())
  99:       throw new InvalidKeyException("Incompatible key");
 100:     if (! lastPhase)
 101:       throw new IllegalArgumentException(
 102:           "This key-agreement MUST be concluded in one step only");
 103:     BigInteger resultBI = pub.getY().modPow(key.getX(), s1.getP());
 104:     result = resultBI.toByteArray();
 105:     if (result[0] == 0x00)
 106:       {
 107:         byte[] buf = new byte[result.length - 1];
 108:         System.arraycopy(result, 1, buf, 0, buf.length);
 109:         result = buf;
 110:       }
 111:     last_phase_done = true;
 112:     return null;
 113:   }
 114: 
 115:   protected byte[] engineGenerateSecret()
 116:   {
 117:     checkState();
 118:     byte[] res = (byte[]) result.clone();
 119:     reset();
 120:     return res;
 121:   }
 122: 
 123:   protected int engineGenerateSecret(byte[] secret, int offset)
 124:       throws ShortBufferException
 125:   {
 126:     checkState();
 127:     if (result.length > secret.length - offset)
 128:       throw new ShortBufferException();
 129:     System.arraycopy(result, 0, secret, offset, result.length);
 130:     int res = result.length;
 131:     reset();
 132:     return res;
 133:   }
 134: 
 135:   protected SecretKey engineGenerateSecret(String algorithm)
 136:       throws InvalidKeyException
 137:   {
 138:     checkState();
 139:     byte[] s = (byte[]) result.clone();
 140:     SecretKey res = new SecretKeySpec(s, algorithm);
 141:     reset();
 142:     return res;
 143:   }
 144: 
 145:   protected void engineInit(Key key, SecureRandom random)
 146:       throws InvalidKeyException
 147:   {
 148:     if (! (key instanceof DHPrivateKey))
 149:       throw new InvalidKeyException("Key MUST be a DHPrivateKey");
 150:     this.key = (DHPrivateKey) key;
 151:     reset();
 152:   }
 153: 
 154:   protected void engineInit(Key key, AlgorithmParameterSpec params,
 155:                             SecureRandom random)
 156:       throws InvalidKeyException
 157:   {
 158:     engineInit(key, random);
 159:   }
 160: 
 161:   private void reset()
 162:   {
 163:     result = null;
 164:     last_phase_done = false;
 165:   }
 166: 
 167:   private void checkState()
 168:   {
 169:     if (result == null || ! last_phase_done)
 170:       throw new IllegalStateException("Not finished");
 171:   }
 172: }