java.security.spec.EllipticCurve Java Examples

The following examples show how to use java.security.spec.EllipticCurve. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ECOperations.java    From dragonwell8_jdk with GNU General Public License v2.0 7 votes vote down vote up
public static Optional<ECOperations> forParameters(ECParameterSpec params) {

        EllipticCurve curve = params.getCurve();
        if (!(curve.getField() instanceof ECFieldFp)) {
            return Optional.empty();
        }
        ECFieldFp primeField = (ECFieldFp) curve.getField();

        BigInteger three = BigInteger.valueOf(3);
        if (!primeField.getP().subtract(curve.getA()).equals(three)) {
            return Optional.empty();
        }
        IntegerFieldModuloP field = fields.get(primeField.getP());
        if (field == null) {
            return Optional.empty();
        }

        IntegerFieldModuloP orderField = orderFields.get(params.getOrder());
        if (orderField == null) {
            return Optional.empty();
        }

        ImmutableIntegerModuloP b = field.getElement(curve.getB());
        ECOperations ecOps = new ECOperations(b, orderField);
        return Optional.of(ecOps);
    }
 
Example #2
Source File: JCEECPrivateKey.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public JCEECPrivateKey(
    String              algorithm,
    org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec     spec)
{
    this.algorithm = algorithm;
    this.d = spec.getD();

    if (spec.getParams() != null) // can be null if implicitlyCA
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve;

        ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        this.ecSpec = null;
    }
}
 
Example #3
Source File: ECOperations.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static Optional<ECOperations> forParameters(ECParameterSpec params) {

        EllipticCurve curve = params.getCurve();
        if (!(curve.getField() instanceof ECFieldFp)) {
            return Optional.empty();
        }
        ECFieldFp primeField = (ECFieldFp) curve.getField();

        BigInteger three = BigInteger.valueOf(3);
        if (!primeField.getP().subtract(curve.getA()).equals(three)) {
            return Optional.empty();
        }
        IntegerFieldModuloP field = fields.get(primeField.getP());
        if (field == null) {
            return Optional.empty();
        }

        IntegerFieldModuloP orderField = orderFields.get(params.getOrder());
        if (orderField == null) {
            return Optional.empty();
        }

        ImmutableIntegerModuloP b = field.getElement(curve.getB());
        ECOperations ecOps = new ECOperations(b, orderField);
        return Optional.of(ecOps);
    }
 
Example #4
Source File: DOMKeyValue.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static ECPoint decodePoint(byte[] data, EllipticCurve curve)
        throws IOException {
    if ((data.length == 0) || (data[0] != 4)) {
        throw new IOException("Only uncompressed point format " +
                              "supported");
    }
    // Per ANSI X9.62, an encoded point is a 1 byte type followed by
    // ceiling(log base 2 field-size / 8) bytes of x and the same of y.
    int n = (data.length - 1) / 2;
    if (n != ((curve.getField().getFieldSize() + 7) >> 3)) {
        throw new IOException("Point does not match field size");
    }

    byte[] xb = Arrays.copyOfRange(data, 1, 1 + n);
    byte[] yb = Arrays.copyOfRange(data, n + 1, n + 1 + n);

    return new ECPoint(new BigInteger(1, xb), new BigInteger(1, yb));
}
 
Example #5
Source File: BCECPrivateKey.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
public BCECPrivateKey(
    String algorithm,
    org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec spec,
    ProviderConfiguration configuration)
{
    this.algorithm = algorithm;
    this.d = spec.getD();

    if (spec.getParams() != null) // can be null if implicitlyCA
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve;

        ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        this.ecSpec = null;
    }

    this.configuration = configuration;
}
 
Example #6
Source File: BCDSTU4145PrivateKey.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public BCDSTU4145PrivateKey(
    org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec spec)
{
    this.d = spec.getD();

    if (spec.getParams() != null) // can be null if implicitlyCA
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve;

        ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        this.ecSpec = null;
    }
}
 
Example #7
Source File: ECOperations.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static Optional<ECOperations> forParameters(ECParameterSpec params) {

        EllipticCurve curve = params.getCurve();
        if (!(curve.getField() instanceof ECFieldFp)) {
            return Optional.empty();
        }
        ECFieldFp primeField = (ECFieldFp) curve.getField();

        BigInteger three = BigInteger.valueOf(3);
        if (!primeField.getP().subtract(curve.getA()).equals(three)) {
            return Optional.empty();
        }
        IntegerFieldModuloP field = fields.get(primeField.getP());
        if (field == null) {
            return Optional.empty();
        }

        IntegerFieldModuloP orderField = orderFields.get(params.getOrder());
        if (orderField == null) {
            return Optional.empty();
        }

        ImmutableIntegerModuloP b = field.getElement(curve.getB());
        ECOperations ecOps = new ECOperations(b, orderField);
        return Optional.of(ecOps);
    }
 
Example #8
Source File: TPMAuthenticator.java    From webauthn4j with Apache License 2.0 6 votes vote down vote up
private TPMTPublic createTPMTPublic(PublicKey credentialPublicKey) {
    TPMIAlgPublic type = null;
    TPMIAlgHash nameAlg = TPMIAlgHash.TPM_ALG_SHA256;
    TPMAObject objectAttributes = new TPMAObject(394354);
    byte[] authPolicy = Base64UrlUtil.decode("nf_L82w4OuaZ-5ho3G3LidcVOIS-KAOSLBJBWL-tIq4");
    TPMUPublicId unique = null;
    TPMUPublicParms parameters = null;
    if (credentialPublicKey instanceof ECPublicKey) {
        ECPublicKey ecPublicKey = (ECPublicKey) credentialPublicKey;
        EllipticCurve curve = ecPublicKey.getParams().getCurve();
        parameters = new TPMSECCParms(
                new byte[2],
                new byte[2],
                TPMEccCurve.create(curve),
                new byte[2]
        );
        type = TPMIAlgPublic.TPM_ALG_ECDSA;
        ECPoint ecPoint = ecPublicKey.getW();
        byte[] x = ecPoint.getAffineX().toByteArray();
        byte[] y = ecPoint.getAffineY().toByteArray();
        unique = new ECCUnique(x, y);
    }
    return new TPMTPublic(type, nameAlg, objectAttributes, authPolicy, parameters, unique);
}
 
Example #9
Source File: EC5Util.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public static ECCurve convertCurve(
    EllipticCurve ec)
{
    ECField field = ec.getField();
    BigInteger a = ec.getA();
    BigInteger b = ec.getB();

    if (field instanceof ECFieldFp)
    {
        ECCurve.Fp curve = new ECCurve.Fp(((ECFieldFp)field).getP(), a, b);

        if (customCurves.containsKey(curve))
        {
            return (ECCurve)customCurves.get(curve);
        }

        return curve;
    }
    else
    {
        ECFieldF2m fieldF2m = (ECFieldF2m)field;
        int m = fieldF2m.getM();
        int ks[] = ECUtil.convertMidTerms(fieldF2m.getMidTermsOfReductionPolynomial());
        return new ECCurve.F2m(m, ks[0], ks[1], ks[2], a, b); 
    }
}
 
Example #10
Source File: BCECPrivateKey.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public BCECPrivateKey(
    String algorithm,
    org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec spec,
    ProviderConfiguration configuration)
{
    this.algorithm = algorithm;
    this.d = spec.getD();

    if (spec.getParams() != null) // can be null if implicitlyCA
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve;

        ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        this.ecSpec = null;
    }

    this.configuration = configuration;
}
 
Example #11
Source File: Util.java    From julongchain with Apache License 2.0 6 votes vote down vote up
public static ECParameterSpec getECParameterSpec() {
    BigInteger n = new BigInteger(
            "FFFFFFFE" + "FFFFFFFF" + "FFFFFFFF" + "FFFFFFFF" + "7203DF6B" + "21C6052B" + "53BBF409" + "39D54123", 16);
    BigInteger p = new BigInteger(
            "FFFFFFFE" + "FFFFFFFF" + "FFFFFFFF" + "FFFFFFFF" + "FFFFFFFF" + "00000000" + "FFFFFFFF" + "FFFFFFFF", 16);
    BigInteger a = new BigInteger(
            "FFFFFFFE" + "FFFFFFFF" + "FFFFFFFF" + "FFFFFFFF" + "FFFFFFFF" + "00000000" + "FFFFFFFF" + "FFFFFFFC", 16);
    BigInteger b = new BigInteger(
            "28E9FA9E" + "9D9F5E34" + "4D5A9E4B" + "CF6509A7" + "F39789F5" + "15AB8F92" + "DDBCBD41" + "4D940E93", 16);
    BigInteger gx = new BigInteger(
            "32C4AE2C" + "1F198119" + "5F990446" + "6A39C994" + "8FE30BBF" + "F2660BE1" + "715A4589" + "334C74C7", 16);
    BigInteger gy = new BigInteger(
            "BC3736A2" + "F4F6779C" + "59BDCEE3" + "6B692153" + "D0A9877C" + "C62A4740" + "02DF32E5" + "2139F0A0", 16);

    EllipticCurve ellipticCurve = new EllipticCurve(new ECFieldFp(p), a, b);
    return new ECParameterSpec(ellipticCurve, new ECPoint(gx, gy), n, 1);
}
 
Example #12
Source File: BCECGOST3410PublicKey.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public BCECGOST3410PublicKey(
    org.ripple.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.ripple.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
 
Example #13
Source File: BCECGOST3410PrivateKey.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public BCECGOST3410PrivateKey(
    org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec spec)
{
    this.d = spec.getD();

    if (spec.getParams() != null) // can be null if implicitlyCA
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve;

        ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        this.ecSpec = null;
    }
}
 
Example #14
Source File: JCEECPrivateKey.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
public JCEECPrivateKey(
    String              algorithm,
    org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec     spec)
{
    this.algorithm = algorithm;
    this.d = spec.getD();

    if (spec.getParams() != null) // can be null if implicitlyCA
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve;

        ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        this.ecSpec = null;
    }
}
 
Example #15
Source File: DOMKeyValue.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static ECPoint decodePoint(byte[] data, EllipticCurve curve)
        throws IOException {
    if ((data.length == 0) || (data[0] != 4)) {
        throw new IOException("Only uncompressed point format " +
                              "supported");
    }
    // Per ANSI X9.62, an encoded point is a 1 byte type followed by
    // ceiling(log base 2 field-size / 8) bytes of x and the same of y.
    int n = (data.length - 1) / 2;
    if (n != ((curve.getField().getFieldSize() + 7) >> 3)) {
        throw new IOException("Point does not match field size");
    }

    byte[] xb = Arrays.copyOfRange(data, 1, 1 + n);
    byte[] yb = Arrays.copyOfRange(data, n + 1, n + 1 + n);

    return new ECPoint(new BigInteger(1, xb), new BigInteger(1, yb));
}
 
Example #16
Source File: JCEECPublicKey.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
public JCEECPublicKey(
    String              algorithm,
    org.ripple.bouncycastle.jce.spec.ECPublicKeySpec     spec)
{
    this.algorithm = algorithm;
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.ripple.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger(), false);
        }               
        this.ecSpec = null;
    }
}
 
Example #17
Source File: BCECGOST3410PublicKey.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
public BCECGOST3410PublicKey(
    String algorithm,
    ECPublicKeyParameters params,
    ECParameterSpec spec)
{
    ECDomainParameters dp = params.getParameters();

    this.algorithm = algorithm;
    this.q = params.getQ();

    if (spec == null)
    {
        EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed());

        this.ecSpec = createSpec(ellipticCurve, dp);
    }
    else
    {
        this.ecSpec = spec;
    }
}
 
Example #18
Source File: BCECGOST3410PrivateKey.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
public BCECGOST3410PrivateKey(
    org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec spec)
{
    this.d = spec.getD();

    if (spec.getParams() != null) // can be null if implicitlyCA
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve;

        ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        this.ecSpec = null;
    }
}
 
Example #19
Source File: EcdhTest.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
/**
 * Returns this key as ECPublicKeySpec or null if the key cannot be represented as
 * ECPublicKeySpec. The later happens for example if the order of cofactor are not positive.
 */
public ECPublicKeySpec getSpec() {
  try {
    ECFieldFp fp = new ECFieldFp(p);
    EllipticCurve curve = new EllipticCurve(fp, a, b);
    ECPoint g = new ECPoint(gx, gy);
    // ECParameterSpec requires that the cofactor h is specified.
    if (h == null) {
      return null;
    }
    ECParameterSpec params = new ECParameterSpec(curve, g, n, h);
    ECPoint pubPoint = new ECPoint(pubx, puby);
    ECPublicKeySpec pub = new ECPublicKeySpec(pubPoint, params);
    return pub;
  } catch (Exception ex) {
    System.out.println(comment + " throws " + ex.toString());
    return null;
  }
}
 
Example #20
Source File: EcUtil.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
public static ECParameterSpec getNistCurveSpec(
    String decimalP, String decimalN, String hexB, String hexGX, String hexGY) {
  final BigInteger p = new BigInteger(decimalP);
  final BigInteger n = new BigInteger(decimalN);
  final BigInteger three = new BigInteger("3");
  final BigInteger a = p.subtract(three);
  final BigInteger b = new BigInteger(hexB, 16);
  final BigInteger gx = new BigInteger(hexGX, 16);
  final BigInteger gy = new BigInteger(hexGY, 16);
  final int h = 1;
  ECFieldFp fp = new ECFieldFp(p);
  java.security.spec.EllipticCurve curveSpec = new java.security.spec.EllipticCurve(fp, a, b);
  ECPoint g = new ECPoint(gx, gy);
  ECParameterSpec ecSpec = new ECParameterSpec(curveSpec, g, n, h);
  return ecSpec;
}
 
Example #21
Source File: EcUtil.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
public static ECParameterSpec getBrainpoolP224r1Params() {
  // name = "brainpoolP224r1",
  // oid = '2b2403030208010105',
  // ref = "RFC 5639",
  BigInteger p = new BigInteger("D7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FF", 16);
  BigInteger a = new BigInteger("68A5E62CA9CE6C1C299803A6C1530B514E182AD8B0042A59CAD29F43", 16);
  BigInteger b = new BigInteger("2580F63CCFE44138870713B1A92369E33E2135D266DBB372386C400B", 16);
  BigInteger x = new BigInteger("0D9029AD2C7E5CF4340823B2A87DC68C9E4CE3174C1E6EFDEE12C07D", 16);
  BigInteger y = new BigInteger("58AA56F772C0726F24C6B89E4ECDAC24354B9E99CAA3F6D3761402CD", 16);
  BigInteger n = new BigInteger("D7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F", 16);
  final int h = 1;
  ECFieldFp fp = new ECFieldFp(p);
  EllipticCurve curve = new EllipticCurve(fp, a, b);
  ECPoint g = new ECPoint(x, y);
  return new ECParameterSpec(curve, g, n, h);
}
 
Example #22
Source File: EcUtil.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
public static ECParameterSpec getBrainpoolP256r1Params() {
  BigInteger p =
      new BigInteger("A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", 16);
  BigInteger a =
      new BigInteger("7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", 16);
  BigInteger b =
      new BigInteger("26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", 16);
  BigInteger x =
      new BigInteger("8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", 16);
  BigInteger y =
      new BigInteger("547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", 16);
  BigInteger n =
      new BigInteger("A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", 16);
  final int h = 1;
  ECFieldFp fp = new ECFieldFp(p);
  EllipticCurve curve = new EllipticCurve(fp, a, b);
  ECPoint g = new ECPoint(x, y);
  return new ECParameterSpec(curve, g, n, h);
}
 
Example #23
Source File: BCECPublicKey.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
public BCECPublicKey(
    String algorithm,
    ECPublicKeyParameters params,
    ECParameterSpec spec,
    ProviderConfiguration configuration)
{
    ECDomainParameters      dp = params.getParameters();

    this.algorithm = algorithm;
    this.q = params.getQ();

    if (spec == null)
    {
        EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed());

        this.ecSpec = createSpec(ellipticCurve, dp);
    }
    else
    {
        this.ecSpec = spec;
    }

    this.configuration = configuration;
}
 
Example #24
Source File: EcUtil.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
/**
 * Checks that a point is on a given elliptic curve. This method implements the partial public key
 * validation routine from Section 5.6.2.6 of NIST SP 800-56A
 * http://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf A partial
 * public key validation is sufficient for curves with cofactor 1. See Section B.3 of
 * http://www.nsa.gov/ia/_files/SuiteB_Implementer_G-113808.pdf The point validations above are
 * taken from recommendations for ECDH, because parameter checks in ECDH are much more important
 * than for the case of ECDSA. Performing this test for ECDSA keys is mainly a sanity check.
 *
 * @param point the point that needs verification
 * @param ec the elliptic curve. This must be a curve over a prime order field.
 * @throws GeneralSecurityException if the field is binary or if the point is not on the curve.
 */
public static void checkPointOnCurve(ECPoint point, EllipticCurve ec)
    throws GeneralSecurityException {
  BigInteger p = getModulus(ec);
  BigInteger x = point.getAffineX();
  BigInteger y = point.getAffineY();
  if (x == null || y == null) {
    throw new GeneralSecurityException("point is at infinity");
  }
  // Check 0 <= x < p and 0 <= y < p.
  if (x.signum() == -1 || x.compareTo(p) != -1) {
    throw new GeneralSecurityException("x is out of range");
  }
  if (y.signum() == -1 || y.compareTo(p) != -1) {
    throw new GeneralSecurityException("y is out of range");
  }
  // Check y^2 == x^3 + a x + b (mod p)
  BigInteger lhs = y.multiply(y).mod(p);
  BigInteger rhs = x.multiply(x).add(ec.getA()).multiply(x).add(ec.getB()).mod(p);
  if (!lhs.equals(rhs)) {
    throw new GeneralSecurityException("Point is not on curve");
  }
}
 
Example #25
Source File: EcUtil.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
/**
 * Decompress a point
 *
 * @param x The x-coordinate of the point
 * @param bit0 true if the least significant bit of y is set.
 * @param ecParams contains the curve of the point. This must be over a prime order field.
 */
public static ECPoint getPoint(BigInteger x, boolean bit0, ECParameterSpec ecParams)
    throws GeneralSecurityException {
  EllipticCurve ec = ecParams.getCurve();
  ECField field = ec.getField();
  if (!(field instanceof ECFieldFp)) {
    throw new GeneralSecurityException("Only curves over prime order fields are supported");
  }
  BigInteger p = ((java.security.spec.ECFieldFp) field).getP();
  if (x.compareTo(BigInteger.ZERO) == -1 || x.compareTo(p) != -1) {
    throw new GeneralSecurityException("x is out of range");
  }
  // Compute rhs == x^3 + a x + b (mod p)
  BigInteger rhs = x.multiply(x).add(ec.getA()).multiply(x).add(ec.getB()).mod(p);
  BigInteger y = modSqrt(rhs, p);
  if (bit0 != y.testBit(0)) {
    y = p.subtract(y).mod(p);
  }
  return new ECPoint(x, y);
}
 
Example #26
Source File: DOMKeyValue.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static ECPoint decodePoint(byte[] data, EllipticCurve curve)
        throws IOException {
    if ((data.length == 0) || (data[0] != 4)) {
        throw new IOException("Only uncompressed point format " +
                              "supported");
    }
    // Per ANSI X9.62, an encoded point is a 1 byte type followed by
    // ceiling(log base 2 field-size / 8) bytes of x and the same of y.
    int n = (data.length - 1) / 2;
    if (n != ((curve.getField().getFieldSize() + 7) >> 3)) {
        throw new IOException("Point does not match field size");
    }

    byte[] xb = Arrays.copyOfRange(data, 1, 1 + n);
    byte[] yb = Arrays.copyOfRange(data, n + 1, n + 1 + n);

    return new ECPoint(new BigInteger(1, xb), new BigInteger(1, yb));
}
 
Example #27
Source File: ECOperations.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static Optional<ECOperations> forParameters(ECParameterSpec params) {

        EllipticCurve curve = params.getCurve();
        if (!(curve.getField() instanceof ECFieldFp)) {
            return Optional.empty();
        }
        ECFieldFp primeField = (ECFieldFp) curve.getField();

        BigInteger three = BigInteger.valueOf(3);
        if (!primeField.getP().subtract(curve.getA()).equals(three)) {
            return Optional.empty();
        }
        IntegerFieldModuloP field = fields.get(primeField.getP());
        if (field == null) {
            return Optional.empty();
        }

        IntegerFieldModuloP orderField = orderFields.get(params.getOrder());
        if (orderField == null) {
            return Optional.empty();
        }

        ImmutableIntegerModuloP b = field.getElement(curve.getB());
        ECOperations ecOps = new ECOperations(b, orderField);
        return Optional.of(ecOps);
    }
 
Example #28
Source File: BCDSTU4145PublicKey.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public BCDSTU4145PublicKey(
    org.ripple.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.ripple.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
 
Example #29
Source File: ECPointUtil.java    From ripple-lib-java with ISC License 5 votes vote down vote up
/**
 * Decode a point on this curve which has been encoded using point
 * compression (X9.62 s 4.2.1 and 4.2.2) or regular encoding.
 * 
 * @param curve
 *            The elliptic curve.
 * @param encoded
 *            The encoded point.
 * @return the decoded point.
 */
public static ECPoint decodePoint(
   EllipticCurve curve, 
   byte[] encoded)
{
    ECCurve c = null;
    
    if (curve.getField() instanceof ECFieldFp)
    {
        c = new ECCurve.Fp(
                ((ECFieldFp)curve.getField()).getP(), curve.getA(), curve.getB());
    }
    else
    {
        int k[] = ((ECFieldF2m)curve.getField()).getMidTermsOfReductionPolynomial();
        
        if (k.length == 3)
        {
            c = new ECCurve.F2m(
                    ((ECFieldF2m)curve.getField()).getM(), k[2], k[1], k[0], curve.getA(), curve.getB());
        }
        else
        {
            c = new ECCurve.F2m(
                    ((ECFieldF2m)curve.getField()).getM(), k[0], curve.getA(), curve.getB());
        }
    }
    
    org.ripple.bouncycastle.math.ec.ECPoint p = c.decodePoint(encoded);

    return new ECPoint(p.getAffineXCoord().toBigInteger(), p.getAffineYCoord().toBigInteger());
}
 
Example #30
Source File: BCECGOST3410PrivateKey.java    From ripple-lib-java with ISC License 5 votes vote down vote up
public BCECGOST3410PrivateKey(
    String algorithm,
    ECPrivateKeyParameters params,
    BCECGOST3410PublicKey pubKey,
    ECParameterSpec spec)
{
    ECDomainParameters dp = params.getParameters();

    this.algorithm = algorithm;
    this.d = params.getD();

    if (spec == null)
    {
        EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed());

        this.ecSpec = new ECParameterSpec(
            ellipticCurve,
            new ECPoint(
                dp.getG().getAffineXCoord().toBigInteger(),
                dp.getG().getAffineYCoord().toBigInteger()),
            dp.getN(),
            dp.getH().intValue());
    }
    else
    {
        this.ecSpec = spec;
    }

    this.gostParams = pubKey.getGostParams();

    publicKey = getPublicKeyDetails(pubKey);
}