FileDocCategorySizeDatePackage
JCEDHKeyAgreement.javaAPI DocAndroid 1.5 API4962Wed May 06 22:41:06 BST 2009org.bouncycastle.jce.provider

JCEDHKeyAgreement

public class JCEDHKeyAgreement extends KeyAgreementSpi
Diffie-Hellman key agreement. There's actually a better way of doing this if you are using long term public keys, see the light-weight version for details.

Fields Summary
private BigInteger
x
private BigInteger
p
private BigInteger
g
private BigInteger
result
private SecureRandom
random
Constructors Summary
Methods Summary
private byte[]bigIntToBytes(java.math.BigInteger r)

        byte[]    tmp = r.toByteArray();
        
        if (tmp[0] == 0)
        {
            byte[]    ntmp = new byte[tmp.length - 1];
            
            System.arraycopy(tmp, 1, ntmp, 0, ntmp.length);
            return ntmp;
        }
        
        return tmp;
    
protected java.security.KeyengineDoPhase(java.security.Key key, boolean lastPhase)

        if (x == null)
        {
            throw new IllegalStateException("Diffie-Hellman not initialised.");
        }

        if (!(key instanceof DHPublicKey))
        {
            throw new InvalidKeyException("DHKeyAgreement doPhase requires DHPublicKey");
        }
        DHPublicKey pubKey = (DHPublicKey)key;

        if (!pubKey.getParams().getG().equals(g) || !pubKey.getParams().getP().equals(p))
        {
            throw new InvalidKeyException("DHPublicKey not for this KeyAgreement!");
        }

        if (lastPhase)
        {
            result = ((DHPublicKey)key).getY().modPow(x, p);
            return null;
        }
        else
        {
            result = ((DHPublicKey)key).getY().modPow(x, p);
        }

        return new JCEDHPublicKey(result, pubKey.getParams());
    
protected byte[]engineGenerateSecret()

        if (x == null)
        {
            throw new IllegalStateException("Diffie-Hellman not initialised.");
        }

        return bigIntToBytes(result);
    
protected intengineGenerateSecret(byte[] sharedSecret, int offset)

        if (x == null)
        {
            throw new IllegalStateException("Diffie-Hellman not initialised.");
        }

        byte[]  secret = bigIntToBytes(result);

        if (sharedSecret.length - offset < secret.length)
        {
            throw new ShortBufferException("DHKeyAgreement - buffer too short");
        }

        System.arraycopy(secret, 0, sharedSecret, offset, secret.length);

        return secret.length;
    
protected javax.crypto.SecretKeyengineGenerateSecret(java.lang.String algorithm)

        if (x == null)
        {
            throw new IllegalStateException("Diffie-Hellman not initialised.");
        }
        
        return new SecretKeySpec(bigIntToBytes(result), algorithm);
    
protected voidengineInit(java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random)

        if (!(key instanceof DHPrivateKey))
        {
            throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
        }
        DHPrivateKey    privKey = (DHPrivateKey)key;

        this.random = random;

        if (params != null)
        {
            if (!(params instanceof DHParameterSpec))
            {
                throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
            }
            DHParameterSpec p = (DHParameterSpec)params;

            this.p = p.getP();
            this.g = p.getG();
        }
        else
        {
            this.p = privKey.getParams().getP();
            this.g = privKey.getParams().getG();
        }

        this.x = this.result = privKey.getX();
    
protected voidengineInit(java.security.Key key, java.security.SecureRandom random)

        if (!(key instanceof DHPrivateKey))
        {
            throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey");
        }

        DHPrivateKey    privKey = (DHPrivateKey)key;

        this.random = random;
        this.p = privKey.getParams().getP();
        this.g = privKey.getParams().getG();
        this.x = this.result = privKey.getX();