Source for gnu.javax.crypto.DiffieHellmanImpl

   1: /* DiffieHellmanImpl.java -- implementation of the Diffie-Hellman key agreement.
   2:    Copyright (C) 2005  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;
  40: 
  41: import gnu.java.security.provider.GnuDHPublicKey;
  42: 
  43: import java.math.BigInteger;
  44: 
  45: import java.security.Key;
  46: import java.security.InvalidKeyException;
  47: import java.security.PrivateKey;
  48: import java.security.PublicKey;
  49: import java.security.SecureRandom;
  50: import java.security.spec.AlgorithmParameterSpec;
  51: 
  52: import javax.crypto.KeyAgreementSpi;
  53: import javax.crypto.SecretKey;
  54: import javax.crypto.interfaces.DHPrivateKey;
  55: import javax.crypto.interfaces.DHPublicKey;
  56: import javax.crypto.spec.DHParameterSpec;
  57: import javax.crypto.spec.SecretKeySpec;
  58: 
  59: /**
  60:  * The Diffie-Hellman key agreement.
  61:  *
  62:  * @author Casey Marshall (csm@gnu.org)
  63:  */
  64: public final class DiffieHellmanImpl extends KeyAgreementSpi
  65: {
  66: 
  67:   /** The private key being used for this agreement. */
  68:   private DHPrivateKey key;
  69: 
  70:   /** The current result. */
  71:   private BigInteger result;
  72: 
  73:   /** True if the caller told us we are done. */
  74:   private boolean last_phase_done;
  75: 
  76:   /** Trivial default constructor. */
  77:   public DiffieHellmanImpl ()
  78:   {
  79:     key = null;
  80:     result = null;
  81:     last_phase_done = false;
  82:   }
  83: 
  84:   // KeyAgreementSpi methods.
  85: 
  86:   protected Key engineDoPhase (final Key incoming, final boolean lastPhase)
  87:     throws InvalidKeyException
  88:   {
  89:     if (key == null)
  90:       throw new IllegalStateException ("not initialized");
  91:     if (last_phase_done)
  92:       throw new IllegalStateException ("last phase already done");
  93: 
  94:     if (!(incoming instanceof DHPublicKey))
  95:       throw new InvalidKeyException ("expecting javax.crypto.interfaces.DHPublicKey");
  96:     DHPublicKey pub = (DHPublicKey) incoming;
  97:     DHParameterSpec s1 = key.getParams();
  98:     DHParameterSpec s2 = pub.getParams();
  99:     if (!s1.getG().equals (s2.getG())
 100:         || !s1.getP().equals (s2.getP())
 101:         || s1.getL() != s2.getL())
 102:       throw new InvalidKeyException ("supplied key is not compatible");
 103: 
 104:     result = pub.getY().modPow (key.getX(), s1.getP());
 105:     if (lastPhase)
 106:       {
 107:     last_phase_done = true;
 108:     return null;
 109:       }
 110: 
 111:     throw new IllegalArgumentException ("only supports two-party Diffie Hellman");
 112:   }
 113: 
 114:   protected byte[] engineGenerateSecret ()
 115:   {
 116:     if (result == null || !last_phase_done)
 117:       throw new IllegalStateException ("not finished");
 118: 
 119:     byte[] buf = result.toByteArray ();
 120:     if (buf[0] == 0x00)
 121:       {
 122:     byte[] buf2 = new byte[buf.length - 1];
 123:     System.arraycopy (buf, 1, buf2, 0, buf2.length);
 124:     buf = buf2;
 125:       }
 126:     return buf;
 127:   }
 128: 
 129:   protected int engineGenerateSecret (final byte[] secret, final int offset)
 130:   {
 131:     byte[] s = engineGenerateSecret();
 132:     System.arraycopy (s, 0, secret, offset, s.length);
 133:     return s.length;
 134:   }
 135: 
 136:   protected SecretKey engineGenerateSecret (final String algorithm)
 137:     throws InvalidKeyException
 138:   {
 139:     byte[] s = engineGenerateSecret();
 140:     return new SecretKeySpec (s, algorithm);
 141:   }
 142: 
 143:   protected void engineInit (final Key key, final SecureRandom random)
 144:     throws InvalidKeyException
 145:   {
 146:     if (!(key instanceof DHPrivateKey))
 147:       throw new InvalidKeyException ("not a javax.crypto.interfaces.DHPrivateKey");
 148:     this.key = (DHPrivateKey) key;
 149:     result = null;
 150:     last_phase_done = false;
 151:   }
 152: 
 153:   protected void engineInit (final Key key, final AlgorithmParameterSpec params,
 154:                              final SecureRandom random)
 155:     throws InvalidKeyException
 156:   {
 157:     engineInit (key, random);
 158:   }
 159: }