Save This Page
Home » crypto-143 » org.bouncycastle.crypto.agreement » [javadoc | source]
    1   package org.bouncycastle.crypto.agreement;
    2   
    3   import java.math.BigInteger;
    4   import java.security.SecureRandom;
    5   
    6   import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
    7   import org.bouncycastle.crypto.CipherParameters;
    8   import org.bouncycastle.crypto.generators.DHKeyPairGenerator;
    9   import org.bouncycastle.crypto.params.DHKeyGenerationParameters;
   10   import org.bouncycastle.crypto.params.DHParameters;
   11   import org.bouncycastle.crypto.params.DHPublicKeyParameters;
   12   import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
   13   import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
   14   import org.bouncycastle.crypto.params.ParametersWithRandom;
   15   
   16   /**
   17    * a Diffie-Hellman key exchange engine.
   18    * <p>
   19    * note: This uses MTI/A0 key agreement in order to make the key agreement
   20    * secure against passive attacks. If you're doing Diffie-Hellman and both
   21    * parties have long term public keys you should look at using this. For
   22    * further information have a look at RFC 2631.
   23    * <p>
   24    * It's possible to extend this to more than two parties as well, for the moment
   25    * that is left as an exercise for the reader.
   26    */
   27   public class DHAgreement
   28   {
   29       private DHPrivateKeyParameters  key;
   30       private DHParameters            dhParams;
   31       private BigInteger              privateValue;
   32       private SecureRandom            random;
   33   
   34       public void init(
   35           CipherParameters    param)
   36       {
   37           AsymmetricKeyParameter  kParam;
   38   
   39           if (param instanceof ParametersWithRandom)
   40           {
   41               ParametersWithRandom    rParam = (ParametersWithRandom)param;
   42   
   43               this.random = rParam.getRandom();
   44               kParam = (AsymmetricKeyParameter)rParam.getParameters();
   45           }
   46           else
   47           {
   48               this.random = new SecureRandom();
   49               kParam = (AsymmetricKeyParameter)param;
   50           }
   51   
   52           
   53           if (!(kParam instanceof DHPrivateKeyParameters))
   54           {
   55               throw new IllegalArgumentException("DHEngine expects DHPrivateKeyParameters");
   56           }
   57   
   58           this.key = (DHPrivateKeyParameters)kParam;
   59           this.dhParams = key.getParameters();
   60       }
   61   
   62       /**
   63        * calculate our initial message.
   64        */
   65       public BigInteger calculateMessage()
   66       {
   67           DHKeyPairGenerator dhGen = new DHKeyPairGenerator();
   68           dhGen.init(new DHKeyGenerationParameters(random, dhParams));
   69           AsymmetricCipherKeyPair dhPair = dhGen.generateKeyPair();
   70   
   71           this.privateValue = ((DHPrivateKeyParameters)dhPair.getPrivate()).getX();
   72   
   73           return ((DHPublicKeyParameters)dhPair.getPublic()).getY();
   74       }
   75   
   76       /**
   77        * given a message from a given party and the corresponding public key,
   78        * calculate the next message in the agreement sequence. In this case
   79        * this will represent the shared secret.
   80        */
   81       public BigInteger calculateAgreement(
   82           DHPublicKeyParameters   pub,
   83           BigInteger              message)
   84       {
   85           if (!pub.getParameters().equals(dhParams))
   86           {
   87               throw new IllegalArgumentException("Diffie-Hellman public key has wrong parameters.");
   88           }
   89   
   90           BigInteger p = dhParams.getP();
   91   
   92           return message.modPow(key.getX(), p).multiply(pub.getY().modPow(privateValue, p)).mod(p);
   93       }
   94   }

Save This Page
Home » crypto-143 » org.bouncycastle.crypto.agreement » [javadoc | source]