com.google.bitcoin.bouncycastle.crypto.agreement.DHBasicAgreement

Here are the examples of the java api class com.google.bitcoin.bouncycastle.crypto.agreement.DHBasicAgreement taken from open source projects.

1. TlsProtocolHandler#processDHEKeyExchange()

Project: bitcoin-android
File: TlsProtocolHandler.java
private void processDHEKeyExchange(ByteArrayInputStream is, Signer signer) throws IOException {
    InputStream sigIn = is;
    if (signer != null) {
        signer.init(false, this.serverPublicKey);
        signer.update(this.clientRandom, 0, this.clientRandom.length);
        signer.update(this.serverRandom, 0, this.serverRandom.length);
        sigIn = new SignerInputStream(is, signer);
    }
    /*
         * Parse the Structure
         */
    byte[] pByte = TlsUtils.readOpaque16(sigIn);
    byte[] gByte = TlsUtils.readOpaque16(sigIn);
    byte[] YsByte = TlsUtils.readOpaque16(sigIn);
    if (signer != null) {
        byte[] sigByte = TlsUtils.readOpaque16(is);
        /*
              * Verify the Signature.
              */
        if (!signer.verifySignature(sigByte)) {
            this.failWithError(AL_fatal, AP_bad_certificate);
        }
    }
    this.assertEmpty(is);
    /*
         * Do the DH calculation.
         */
    BigInteger p = new BigInteger(1, pByte);
    BigInteger g = new BigInteger(1, gByte);
    BigInteger Ys = new BigInteger(1, YsByte);
    /*
          * Check the DH parameter values
          */
    if (!p.isProbablePrime(10)) {
        this.failWithError(AL_fatal, AP_illegal_parameter);
    }
    if (g.compareTo(TWO) < 0 || g.compareTo(p.subtract(TWO)) > 0) {
        this.failWithError(AL_fatal, AP_illegal_parameter);
    }
    // TODO For static DH public values, see additional checks in RFC 2631 2.1.5 
    if (Ys.compareTo(TWO) < 0 || Ys.compareTo(p.subtract(ONE)) > 0) {
        this.failWithError(AL_fatal, AP_illegal_parameter);
    }
    /*
          * Diffie-Hellman basic key agreement
          */
    DHParameters dhParams = new DHParameters(p, g);
    // Generate a keypair
    DHBasicKeyPairGenerator dhGen = new DHBasicKeyPairGenerator();
    dhGen.init(new DHKeyGenerationParameters(random, dhParams));
    AsymmetricCipherKeyPair dhPair = dhGen.generateKeyPair();
    // Store the public value to send to server
    this.Yc = ((DHPublicKeyParameters) dhPair.getPublic()).getY();
    // Calculate the shared secret
    DHBasicAgreement dhAgree = new DHBasicAgreement();
    dhAgree.init(dhPair.getPrivate());
    BigInteger agreement = dhAgree.calculateAgreement(new DHPublicKeyParameters(Ys, dhParams));
    this.pms = BigIntegers.asUnsignedByteArray(agreement);
}