Example usage for org.bouncycastle.math.ec ECCurve createPoint

List of usage examples for org.bouncycastle.math.ec ECCurve createPoint

Introduction

In this page you can find the example usage for org.bouncycastle.math.ec ECCurve createPoint.

Prototype

public ECPoint createPoint(BigInteger x, BigInteger y) 

Source Link

Usage

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

License:Open Source License

@Deprecated
public static ECPoint decompressFPPoint(ECCurve curve, BigInteger X) {
    // See Andrey Jivsov https://www.ietf.org/archive/id/draft-jivsov-ecc-compact-05.txt.
    ECFieldElement x = curve.fromBigInteger(X);
    ECFieldElement rhs = x.square().add(curve.getA()).multiply(x).add(curve.getB());

    // y' = sqrt( C(x) ), where y'>0
    ECFieldElement yTilde = rhs.sqrt();//from  w w w. j a  va 2 s .c  o  m

    if (yTilde == null) {
        throw new IllegalArgumentException("invalid point compression");
    }

    // y = min(y',p-y')
    BigInteger yT = yTilde.toBigInteger();
    BigInteger yTn = yTilde.negate().toBigInteger();
    BigInteger y = yT.compareTo(yTn) == -1 ? yT : yTn;

    // Q=(x,y) is the canonical representation of the point
    ECPoint Q = curve.createPoint(X, y);

    return Q;
}

From source file:com.github.horrorho.inflatabledonkey.crypto.eckey.ECAssistant.java

License:Open Source License

static ECPoint getQ(BigInteger x, BigInteger y, ECCurve curve) {
    return curve.createPoint(x, y);
}

From source file:com.github.horrorho.inflatabledonkey.crypto.eckey.ECAssistant.java

License:Open Source License

static Optional<ECPoint> getQ(Optional<BigInteger> x, Optional<BigInteger> y, ECCurve curve) {
    return x.isPresent() && y.isPresent() ? Optional.of(curve.createPoint(x.get(), y.get())) : Optional.empty();
}

From source file:de.rub.nds.tlsattacker.tls.protocol.handshake.ECDHClientKeyExchangeHandler.java

License:Apache License

@Override
byte[] prepareKeyExchangeMessage() {
    if (tlsContext.getEcContext().getServerPublicKeyParameters() == null) {
        // we are probably handling a simple ECDH ciphersuite, we try to
        // establish server public key parameters from the server
        // certificate message
        Certificate x509Cert = tlsContext.getServerCertificate();

        SubjectPublicKeyInfo keyInfo = x509Cert.getSubjectPublicKeyInfo();
        ECPublicKeyParameters parameters;
        try {/*from   ww  w . j av  a  2  s  .  co  m*/
            parameters = (ECPublicKeyParameters) PublicKeyFactory.createKey(keyInfo);
            ECPublicKeyParameters publicKeyParameters = (ECPublicKeyParameters) parameters;
            tlsContext.getEcContext().setServerPublicKeyParameters(parameters);
            LOGGER.debug("Parsed the following EC domain parameters from the certificate: ");
            LOGGER.debug("  Curve order: {}", publicKeyParameters.getParameters().getCurve().getOrder());
            LOGGER.debug("  Parameter A: {}", publicKeyParameters.getParameters().getCurve().getA());
            LOGGER.debug("  Parameter B: {}", publicKeyParameters.getParameters().getCurve().getB());
            LOGGER.debug("  Base point: {} ", publicKeyParameters.getParameters().getG());
            LOGGER.debug("  Public key point Q: {} ", publicKeyParameters.getQ());
        } catch (NoSuchMethodError e) {
            LOGGER.debug("The method was not found. It is possible that it is because an older bouncy castle"
                    + " library was used. We try to proceed the workflow.", e);
        } catch (IOException e) {
            throw new WorkflowExecutionException("Problem in parsing public key parameters from certificate",
                    e);
        }
    }

    AsymmetricCipherKeyPair kp = TlsECCUtils.generateECKeyPair(new SecureRandom(),
            tlsContext.getEcContext().getServerPublicKeyParameters().getParameters());

    ECPublicKeyParameters ecPublicKey = (ECPublicKeyParameters) kp.getPublic();
    ECPrivateKeyParameters ecPrivateKey = (ECPrivateKeyParameters) kp.getPrivate();

    // do some ec point modification
    protocolMessage.setPublicKeyBaseX(ecPublicKey.getQ().getAffineXCoord().toBigInteger());
    protocolMessage.setPublicKeyBaseY(ecPublicKey.getQ().getAffineYCoord().toBigInteger());

    ECCurve curve = ecPublicKey.getParameters().getCurve();
    ECPoint point = curve.createPoint(protocolMessage.getPublicKeyBaseX().getValue(),
            protocolMessage.getPublicKeyBaseY().getValue());

    LOGGER.debug("Using the following point:");
    LOGGER.debug("X: " + protocolMessage.getPublicKeyBaseX().getValue().toString());
    LOGGER.debug("Y: " + protocolMessage.getPublicKeyBaseY().getValue().toString());

    // System.out.println("-----------------\nUsing the following point:");
    // System.out.println("X: " + point.getAffineXCoord());
    // System.out.println("Y: " + point.getAffineYCoord());
    // System.out.println("-----------------\n");
    ECPointFormat[] pointFormats = tlsContext.getEcContext().getServerPointFormats();

    try {
        byte[] serializedPoint = ECCUtilsBCWrapper.serializeECPoint(pointFormats, point);
        protocolMessage.setEcPointFormat(serializedPoint[0]);
        protocolMessage.setEcPointEncoded(Arrays.copyOfRange(serializedPoint, 1, serializedPoint.length));
        protocolMessage.setPublicKeyLength(serializedPoint.length);

        byte[] result = ArrayConverter.concatenate(
                new byte[] { protocolMessage.getPublicKeyLength().getValue().byteValue() },
                new byte[] { protocolMessage.getEcPointFormat().getValue() },
                protocolMessage.getEcPointEncoded().getValue());

        byte[] premasterSecret = TlsECCUtils.calculateECDHBasicAgreement(
                tlsContext.getEcContext().getServerPublicKeyParameters(), ecPrivateKey);
        byte[] random = tlsContext.getClientServerRandom();
        protocolMessage.setPremasterSecret(premasterSecret);
        LOGGER.debug("Computed PreMaster Secret: {}",
                ArrayConverter.bytesToHexString(protocolMessage.getPremasterSecret().getValue()));
        LOGGER.debug("Client Server Random: {}", ArrayConverter.bytesToHexString(random));

        PRFAlgorithm prfAlgorithm = AlgorithmResolver.getPRFAlgorithm(tlsContext.getProtocolVersion(),
                tlsContext.getSelectedCipherSuite());
        byte[] masterSecret = PseudoRandomFunction.compute(prfAlgorithm,
                protocolMessage.getPremasterSecret().getValue(), PseudoRandomFunction.MASTER_SECRET_LABEL,
                random, HandshakeByteLength.MASTER_SECRET);
        LOGGER.debug("Computed Master Secret: {}", ArrayConverter.bytesToHexString(masterSecret));

        protocolMessage.setMasterSecret(masterSecret);
        tlsContext.setMasterSecret(protocolMessage.getMasterSecret().getValue());

        return result;

    } catch (IOException ex) {
        throw new WorkflowExecutionException("EC point serialization failure", ex);
    }
}

From source file:eu.betaas.taas.securitymanager.common.ec.ECKeyPairGen.java

License:Apache License

/**
 * Generate random 192-bit EC Public Key given the Q/W parameters of EC Public
 * Key, i.e. the X and Y coordinate  /*from  ww w  .  ja v  a2 s .c o m*/
 * @param Wx: X coordinate of Q or W point representing the EC public key
 * @param Wy: Y coordinate of Q or W point representing the EC public key
 * @return
 * @throws Exception
 */
public static ECPublicKeyParameters generateECPublicKey192(BigInteger Wx, BigInteger Wy) throws Exception {
    // First, define an EC curve
    // ECCurve.Fp(p, a, b); p = prime; a,b = constants defined in equation E: y^2=x^3+ax+b (mod p)
    ECCurve curve = new ECCurve.Fp(new BigInteger(ECParams.P_192_R1, 16), // p 
            new BigInteger(ECParams.A_192_R1, 16), // a
            new BigInteger(ECParams.B_192_R1, 16)); // b

    byte[] seed = Hex.decode(ECParams.SEED_192_R1);

    org.bouncycastle.math.ec.ECPoint gPoint = curve.createPoint(Wx, Wy);

    return new ECPublicKeyParameters(gPoint,
            new ECDomainParameters(curve, curve.decodePoint(Hex.decode(ECParams.G_192_R1_NCOMP)), // G       
                    new BigInteger(ECParams.N_192_R1, 16), // n
                    new BigInteger(ECParams.H_192_R1, 16), // h 
                    seed));
}

From source file:eu.betaas.taas.securitymanager.common.ec.EllipticUtils.java

License:Apache License

/**
 * A method to calculate addition of 2 ECPoints: p3 = p1+p2, with 
 * the help of bouncy castle awesome library =)
 * @param p1 ECPoint 1//from   w  ww.  ja v a2  s.  c  o m
 * @param p2 ECPoint 2
 * @return p1 + p2
 */
public static ECPoint pointAdditionPrime(EllipticCurve ec, ECPoint p1, ECPoint p2) {
    // get the curve parameters
    ECFieldFp field = (ECFieldFp) ec.getField();
    BigInteger p = field.getP();
    BigInteger a = ec.getA();
    BigInteger b = ec.getB();

    org.bouncycastle.math.ec.ECCurve curve = new org.bouncycastle.math.ec.ECCurve.Fp(p, a, b);

    org.bouncycastle.math.ec.ECPoint pb1 = curve.createPoint(p1.getAffineX(), p1.getAffineY());

    org.bouncycastle.math.ec.ECPoint pb2 = curve.createPoint(p2.getAffineX(), p2.getAffineY());

    org.bouncycastle.math.ec.ECPoint pb3 = pb1.add(pb2);

    ECPoint p3 = new ECPoint(pb3.normalize().getXCoord().toBigInteger(),
            pb3.normalize().getYCoord().toBigInteger());

    return p3;
}

From source file:eu.betaas.taas.securitymanager.common.ec.EllipticUtils.java

License:Apache License

/**
 * A method to calculate the doubling of an ECPoint p, i.e. q = 2p, with the
 * help of bouncy castle awesome library =)
 * @param p1 the ECPoint to be doubled//  w  w  w.  j  a  v  a 2s .com
 * @param a the constant a from the elliptic curve
 * @return 2p
 */
public static ECPoint pointDoublingPrime(EllipticCurve ec, ECPoint p1) {
    // get the curve parameters
    ECFieldFp field = (ECFieldFp) ec.getField();
    BigInteger p = field.getP();
    BigInteger a = ec.getA();
    BigInteger b = ec.getB();

    org.bouncycastle.math.ec.ECCurve curve = new org.bouncycastle.math.ec.ECCurve.Fp(p, a, b);

    org.bouncycastle.math.ec.ECPoint pb1 = curve.createPoint(p1.getAffineX(), p1.getAffineY());

    org.bouncycastle.math.ec.ECPoint pb3 = pb1.twice();

    ECPoint p3 = new ECPoint(pb3.normalize().getXCoord().toBigInteger(),
            pb3.normalize().getYCoord().toBigInteger());

    return p3;
}

From source file:eu.betaas.taas.securitymanager.common.ec.EllipticUtils.java

License:Apache License

/**
 * A method to get the negative of an ECPoint, i.e. p (xp,yp); -p(xp,-yp), 
 * with the help of bouncy castle awesome library
 * @param p1 an ECPoint//from w  w  w. j a v  a2  s  .c om
 * @return -p
 */
public static ECPoint pointNegativePrime(EllipticCurve ec, ECPoint p1) {
    // get the curve parameters
    ECFieldFp field = (ECFieldFp) ec.getField();
    BigInteger p = field.getP();
    BigInteger a = ec.getA();
    BigInteger b = ec.getB();

    org.bouncycastle.math.ec.ECCurve curve = new org.bouncycastle.math.ec.ECCurve.Fp(p, a, b);

    org.bouncycastle.math.ec.ECPoint pb1 = curve.createPoint(p1.getAffineX(), p1.getAffineY());

    org.bouncycastle.math.ec.ECPoint pb2 = pb1.negate();

    ECPoint p2 = new ECPoint(p1.getAffineX(), pb2.normalize().getYCoord().toBigInteger());

    return p2;
}

From source file:eu.betaas.taas.securitymanager.common.ec.EllipticUtils.java

License:Apache License

/**
 * A method to multiply an ECPoint with a scalar number
 * @param p the ECPoint//from  w  w w  .  jav a 2s.c  o m
 * @param k the scalar number 
 * @param a the constant a of the elliptic curve
 * @return k*p
 */
public static ECPoint pointMultiplication(EllipticCurve ec, ECPoint p1, BigInteger k) {
    // get the curve parameters
    ECFieldFp field = (ECFieldFp) ec.getField();
    BigInteger p = field.getP();
    BigInteger a = ec.getA();
    BigInteger b = ec.getB();

    org.bouncycastle.math.ec.ECCurve curve = new org.bouncycastle.math.ec.ECCurve.Fp(p, a, b);

    org.bouncycastle.math.ec.ECPoint pb1 = curve.createPoint(p1.getAffineX(), p1.getAffineY());

    org.bouncycastle.math.ec.ECPoint pb2 = pb1.multiply(k);

    ECPoint R = new ECPoint(pb2.normalize().getXCoord().toBigInteger(),
            pb2.normalize().getYCoord().toBigInteger());

    return R;
}