Example usage for org.bouncycastle.math.ec ECPoint normalize

List of usage examples for org.bouncycastle.math.ec ECPoint normalize

Introduction

In this page you can find the example usage for org.bouncycastle.math.ec ECPoint normalize.

Prototype

public ECPoint normalize() 

Source Link

Document

Normalization ensures that any projective coordinate is 1, and therefore that the x, y coordinates reflect those of the equivalent point in an affine coordinate system.

Usage

From source file:com.github.horrorho.inflatabledonkey.crypto.ec.ECCurvePoint.java

License:Open Source License

private ECCurvePoint(Object lock, ECPoint Q, String curveName, X9ECParameters x9ECParameters) {
    this.lock = Objects.requireNonNull(lock, "lock");
    this.Q = Objects.requireNonNull(Q.normalize(), "Q");
    this.curveName = Objects.requireNonNull(curveName, "curveName");
    this.x9ECParameters = Objects.requireNonNull(x9ECParameters, "x9ECParameters");
}

From source file:dorkbox.util.serialization.EccPrivateKeySerializer.java

License:Apache License

static void serializeECPoint(ECPoint point, Output output) throws KryoException {
    if (point.isInfinity()) {
        return;/*  w w  w. j av  a 2s . co m*/
    }

    ECPoint normed = point.normalize();

    byte[] X = normed.getXCoord().getEncoded();
    byte[] Y = normed.getYCoord().getEncoded();

    int length = 1 + X.length + Y.length;
    output.writeInt(length, true);

    output.write(0x04);
    output.write(X);
    output.write(Y);
}

From source file:eu.betaas.taas.securitymanager.authentication.service.impl.GWEcmqvExtService.java

License:Apache License

public EcmqvMessage initEcmqv(byte[] ephPubX, byte[] ephPubY, byte[] certByte) {
    // decode the certificate
    X509CertificateHolder cert = null;/*from   w w w  .  ja  v  a  2s  .c  om*/
    try {
        cert = new X509CertificateHolder(certByte);
    } catch (IOException e1) {
        log.error("Error in decoding the submitted certificate!!");
        e1.printStackTrace();
    }

    // validate the certificate
    boolean isCertValid = false;

    try {
        isCertValid = validateCert(cert);
    } catch (Exception e) {
        log.error("Error in verifying the submitted certificate: " + e.getMessage());
        e.printStackTrace();
    }

    if (!isCertValid) {
        log.error("The submitted certificate is not valid!!");
        return null;
    }
    log.debug("Passed the certificate validation!!");

    // decode the ephemeral public key
    try {
        ephPub = ECKeyPairGen.generateECPublicKey192(new BigInteger(ephPubX), new BigInteger(ephPubY));
    } catch (Exception e) {
        log.error("Error in decoding the submitted ephemeral public key: " + e.getMessage());
        e.printStackTrace();
    }

    // perform embedded public key validation
    boolean pubValid = ECMQVUtils.validateEmbedPubKey(ephPub);
    if (!pubValid) {
        log.error("The submitted ephemeral public key is not valid!!");
        return null;
    }
    log.debug("Passed the embedded ephemeral public key validation!!");

    // generates its own ephemeral key pairs, we assume that in this stage the 
    // ephemeral key pairs were not generated
    AsymmetricCipherKeyPair myEphKp = ECKeyPairGen.generateECKeyPair192();

    myEphPub = (ECPublicKeyParameters) myEphKp.getPublic();
    myEphPriv = (ECPrivateKeyParameters) myEphKp.getPrivate();

    // computes the implicit signature --> the static private key was obtained
    // when we validate the certificate (upon loading the KeyStore)
    BigInteger implSig = ECMQVUtils.computeImplicitSig(myEphPub, myEphPriv, statPriv);

    // calculates the shared key K
    ECPoint K = null;
    try {
        K = ECMQVUtils.calculateSharedKey(ephPub,
                (ECPublicKeyParameters) PublicKeyFactory.createKey(cert.getSubjectPublicKeyInfo()),
                ephPub.getParameters().getH(), implSig);
    } catch (IOException e) {
        log.error("Error in calculating the shared key K: " + e.getMessage());
        e.printStackTrace();
    }

    // derive 2 symmetric keys from the shared key K
    byte[] Kx = K.normalize().getXCoord().toBigInteger().toByteArray();
    int Lx = K.normalize().getXCoord().toBigInteger().bitLength();
    double x = Math.log(Lx) / Math.log(2.0);
    double L = Math.pow(2, 1 + Math.ceil(x));

    byte[] deriveK = ECMQVUtils.deriveKeyHKDF(Kx, (int) L / 8);

    // k1 and k2 split from newKey --> k1: to be MACed, k2: the session key
    k1 = new byte[deriveK.length / 2];
    k2 = new byte[deriveK.length / 2];
    int c = 0;
    for (byte b : deriveK) {
        if (c < deriveK.length / 2) {
            k1[c] = b;
        } else {
            k2[c - deriveK.length / 2] = b;
        }
        c++;
    }

    // retrieving my user friendly name from the SubjectAlternativeNames in my 
    // certificate
    Extensions myExs = myCert.getExtensions();
    if (myExs != null) {
        GeneralNames gns = GeneralNames.fromExtensions(myExs, Extension.subjectAlternativeName);
        for (int i = 0; i < gns.getNames().length; i++) {
            myUFN = gns.getNames()[i].getName().toString();
        }
    }

    // retrieving other GW user friendly name from the SubjectAlternativeNames 
    // in the submitted certificate
    Extensions oExs = cert.getExtensions();
    if (oExs != null) {
        GeneralNames gns = GeneralNames.fromExtensions(oExs, Extension.subjectAlternativeName);
        for (int i = 0; i < gns.getNames().length; i++) {
            ufn = gns.getNames()[i].getName().toString();
        }
    }

    // compute the MAC to be sent to the other gateway
    byte[] myMac = ECMQVUtils.computeMAC("2", myUFN, ufn, myEphPub.getQ().getEncoded(),
            ephPub.getQ().getEncoded(), k1);

    EcmqvMessage eMsg = new EcmqvMessage();
    eMsg.setMyMac(myMac);
    try {
        eMsg.setMyCertificate(myCert.getEncoded());
    } catch (IOException e) {
        log.error("Error in encoding the certificate: " + e.getMessage());
        e.printStackTrace();
    }

    eMsg.setEphemeralPublicX(myEphPub.getQ().normalize().getXCoord().toBigInteger().toByteArray());
    eMsg.setEphemeralPublicY(myEphPub.getQ().normalize().getXCoord().toBigInteger().toByteArray());

    return eMsg;
}

From source file:eu.betaas.taas.securitymanager.authentication.service.impl.GWEcmqvIntService.java

License:Apache License

public byte[] responseEcmqv(EcmqvMessage eMsg) throws Exception {
    // decode the certificate
    X509CertificateHolder cert = new X509CertificateHolder(eMsg.getMyCertificate());

    // decode the ECPublicKey
    ECPublicKeyParameters ephPub = ECKeyPairGen.generateECPublicKey192(
            new BigInteger(eMsg.getEphemeralPublicX()), new BigInteger(eMsg.getEphemeralPublicY()));
    // get the MAC 2
    byte[] mac2 = eMsg.getMyMac();

    // validate the certificate
    boolean isCertValid = false;
    isCertValid = validateCert(cert);// www .  ja  v  a2  s . c  o m

    if (!isCertValid) {
        log.error("The submitted certificate is not valid!!");
        return null;
    }
    log.debug("Passed the certificate validation!!");

    // perform embedded public key validation
    boolean pubValid = ECMQVUtils.validateEmbedPubKey(ephPub);
    if (!pubValid) {
        log.error("The submitted ephemeral public key is not valid!!");
        return null;
    }
    log.debug("Passed the embedded ephemeral public key validation!!");
    // set the ephPub with this received ephPub
    this.ephPub = ephPub;

    // now, no need to generate my own ephemeral key here, because it is done
    // compute the implicit signature
    BigInteger implSig = ECMQVUtils.computeImplicitSig(myEphPub, myEphPriv, statPriv);

    // calculates the shared key K
    ECPublicKeyParameters statPub = (ECPublicKeyParameters) PublicKeyFactory
            .createKey(cert.getSubjectPublicKeyInfo());
    org.bouncycastle.math.ec.ECPoint K = ECMQVUtils.calculateSharedKey(this.ephPub, statPub,
            this.ephPub.getParameters().getH(), implSig);

    // derive 2 symmetric keys from the shared key K
    byte[] Kx = K.normalize().getXCoord().toBigInteger().toByteArray();
    int Lx = K.normalize().getXCoord().toBigInteger().bitLength();
    double x = Math.log(Lx) / Math.log(2.0);
    double L = Math.pow(2, 1 + Math.ceil(x));

    byte[] deriveK = ECMQVUtils.deriveKeyHKDF(Kx, (int) L / 8);

    // k1 and k2 split from newKey --> k1: to be MACed, k2: the session key
    k1 = new byte[deriveK.length / 2];
    k2 = new byte[deriveK.length / 2];
    int c = 0;
    for (byte b : deriveK) {
        if (c < deriveK.length / 2) {
            k1[c] = b;
        } else {
            k2[c - deriveK.length / 2] = b;
        }
        c++;
    }

    // retrieving my user friendly name from the SubjectAlternativeNames in my 
    // certificate
    Extensions myExs = myCert.getExtensions();
    if (myExs != null) {
        GeneralNames gns = GeneralNames.fromExtensions(myExs, Extension.subjectAlternativeName);
        for (int i = 0; i < gns.getNames().length; i++) {
            myUFN = gns.getNames()[i].getName().toString();
        }
    }

    // retrieving other GW user friendly name from the SubjectAlternativeNames 
    // in the submitted certificate
    Extensions oExs = cert.getExtensions();
    if (oExs != null) {
        GeneralNames gns = GeneralNames.fromExtensions(oExs, Extension.subjectAlternativeName);
        for (int i = 0; i < gns.getNames().length; i++) {
            ufn = gns.getNames()[i].getName().toString();
        }
    }

    // validate MAC 2, which is received from other GW
    boolean isMac2Valid = verifyMac2(mac2, ufn, myUFN, this.ephPub, myEphPub, k1);

    // compute the MAC to be sent to the other gateway
    if (!isMac2Valid) {
        log.error("Fails to verify the received MAC (2)!!");
        return null;
    }
    log.debug("Successfully verifies the received MAC (2)!!");

    byte[] mac3 = ECMQVUtils.computeMAC("3", myUFN, ufn, myEphPub.getQ().getEncoded(),
            ephPub.getQ().getEncoded(), k1);

    return mac3;
}