Example usage for org.bouncycastle.math.ec ECCurve.Fp getB

List of usage examples for org.bouncycastle.math.ec ECCurve.Fp getB

Introduction

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

Prototype

public ECFieldElement getB() 

Source Link

Usage

From source file:eu.betaas.taas.securitymanager.common.mqv.ECMQVUtils.java

License:Apache License

/**
 * A method to perform embedded public key validation (BC style)
 * @param pub: the public key to be validated
 * @return true if valid/*from   w ww  .  j  av a2s .  co m*/
 */
public static boolean validateEmbedPubKey(AsymmetricKeyParameter pub) {

    ECPublicKeyParameters ecPub = (ECPublicKeyParameters) pub;
    ECCurve.Fp fpCurve = (Fp) ecPub.getParameters().getCurve();
    BigInteger p = fpCurve.getQ();
    BigInteger xq = ecPub.getQ().normalize().getXCoord().toBigInteger();
    BigInteger yq = ecPub.getQ().normalize().getYCoord().toBigInteger();

    // validate the xq and yq, they must be in the interval [0,q-1]
    if (xq.compareTo(BigInteger.ZERO) == -1 && xq.compareTo(p.subtract(BigInteger.ONE)) == 1
            && yq.compareTo(BigInteger.ZERO) == -1 && yq.compareTo(p.subtract(BigInteger.ONE)) == 1)
        return false;

    BigInteger a = fpCurve.getA().toBigInteger();
    BigInteger b = fpCurve.getB().toBigInteger();

    // test whether Q lies in the curve defined by y^2(mod p)=x^3+ax+b (mod p)
    BigInteger rightEq = xq.pow(3).add(a.multiply(xq)).add(b).mod(p);
    //      System.out.println("x^3+ax+b    = "+rightEq.toString());

    BigInteger y2 = yq.pow(2).mod(p);
    //      System.out.println("y^2       = "+y2.toString());

    if (y2.compareTo(rightEq) != 0)
        return false;

    return true;
}