/** * Creates a new {@code ECPublicKey} with the specified public elliptic * curve point and parameter specification. * * @param w * the public elliptic curve point {@code W}. * @param params * the domain parameter specification. * @throws IllegalArgumentException * if the specified point {@code W} is at infinity. */ public ECPublicKeySpec(ECPoint w, ECParameterSpec params) { this.w = w; this.params = params; // throw NullPointerException if w or params is null if (this.w == null) { throw new NullPointerException("w == null"); } if (this.params == null) { throw new NullPointerException("params == null"); } // throw IllegalArgumentException if w is point at infinity if (this.w.equals(ECPoint.POINT_INFINITY)) { throw new IllegalArgumentException("the w parameter is point at infinity"); } }
private static ECPoint doublePoint(ECPoint r, EllipticCurve curve) { if (r.equals(ECPoint.POINT_INFINITY)) return r; BigInteger slope = (r.getAffineX().pow(2)).multiply(THREE); slope = slope.add(curve.getA()); BigInteger prime = ((ECFieldFp) curve.getField()).getP(); // use NBI modInverse(); BigInteger tmp = r.getAffineY().multiply(TWO); tmp = new NativeBigInteger(tmp); slope = slope.multiply(tmp.modInverse(prime)); BigInteger xOut = slope.pow(2).subtract(r.getAffineX().multiply(TWO)).mod(prime); BigInteger yOut = (r.getAffineY().negate()).add(slope.multiply(r.getAffineX().subtract(xOut))).mod(prime); ECPoint out = new ECPoint(xOut, yOut); return out; }
private static ECPoint addPoint(ECPoint r, ECPoint s, EllipticCurve curve) { if (r.equals(s)) return doublePoint(r, curve); else if (r.equals(ECPoint.POINT_INFINITY)) return s; else if (s.equals(ECPoint.POINT_INFINITY)) return r; BigInteger prime = ((ECFieldFp) curve.getField()).getP(); // use NBI modInverse(); BigInteger tmp = r.getAffineX().subtract(s.getAffineX()); tmp = new NativeBigInteger(tmp); BigInteger slope = (r.getAffineY().subtract(s.getAffineY())).multiply(tmp.modInverse(prime)).mod(prime); slope = new NativeBigInteger(slope); BigInteger xOut = (slope.modPow(TWO, prime).subtract(r.getAffineX())).subtract(s.getAffineX()).mod(prime); BigInteger yOut = s.getAffineY().negate().mod(prime); yOut = yOut.add(slope.multiply(s.getAffineX().subtract(xOut))).mod(prime); ECPoint out = new ECPoint(xOut, yOut); return out; }
/** * Creates a new {@code ECPublicKey} with the specified public elliptic * curve point and parameter specification. * * @param w * the public elliptic curve point {@code W}. * @param params * the domain parameter specification. * @throws IllegalArgumentException * if the specified point {@code W} is at infinity. */ public ECPublicKeySpec(ECPoint w, ECParameterSpec params) { this.w = w; this.params = params; // throw NullPointerException if w or params is null if (this.w == null) { throw new NullPointerException("w == null"); } if (this.params == null) { throw new NullPointerException("params == null"); } // throw IllegalArgumentException if w is point at infinity if (this.w.equals(ECPoint.POINT_INFINITY)) { throw new IllegalArgumentException("the w parameter is point at infinity"); } }
/** * Creates a new {@code ECPublicKey} with the specified public elliptic * curve point and parameter specification. * * @param w * the public elliptic curve point {@code W}. * @param params * the domain parameter specification. * @throws IllegalArgumentException * if the specified point {@code W} is at infinity. */ public ECPublicKeySpec(ECPoint w, ECParameterSpec params) { this.w = w; this.params = params; // throw NullPointerException if w or params is null if (this.w == null) { throw new NullPointerException("w == null"); } if (this.params == null) { throw new NullPointerException("params == null"); } // throw IllegalArgumentException if w is point at infinity if (this.w.equals(ECPoint.POINT_INFINITY)) { throw new IllegalArgumentException("the w parameter is point at infinity"); } }
/** * Creates a new {@code ECPublicKey} with the specified public elliptic * curve point and parameter specification. * * @param w * the public elliptic curve point {@code W}. * @param params * the domain parameter specification. * @throws IllegalArgumentException * if the specified point {@code W} is at infinity. */ public ECPublicKeySpec(ECPoint w, ECParameterSpec params) { this.w = w; this.params = params; // throw NullPointerException if w or params is null if (this.w == null) { throw new NullPointerException("w == null"); } if (this.params == null) { throw new NullPointerException("params == null"); } // throw IllegalArgumentException if w is point at infinity if (this.w.equals(ECPoint.POINT_INFINITY)) { throw new IllegalArgumentException("the w parameter is point at infinity"); } }
/** * Creates a new {@code ECPublicKey} with the specified public elliptic * curve point and parameter specification. * * @param w * the public elliptic curve point {@code W}. * @param params * the domain parameter specification. * @throws IllegalArgumentException * if the specified point {@code W} is at infinity. */ public ECPublicKeySpec(ECPoint w, ECParameterSpec params) { this.w = w; this.params = params; // throw NullPointerException if w or params is null if (this.w == null) { throw new NullPointerException("w == null"); } if (this.params == null) { throw new NullPointerException("params == null"); } // throw IllegalArgumentException if w is point at infinity if (this.w.equals(ECPoint.POINT_INFINITY)) { throw new IllegalArgumentException("the w parameter is point at infinity"); } }
/** * Creates a new {@code ECPublicKey} with the specified public elliptic * curve point and parameter specification. * * @param w * the public elliptic curve point {@code W}. * @param params * the domain parameter specification. * @throws IllegalArgumentException * if the specified point {@code W} is at infinity. */ public ECPublicKeySpec(ECPoint w, ECParameterSpec params) { this.w = w; this.params = params; // throw NullPointerException if w or params is null if (this.w == null) { throw new NullPointerException("w == null"); } if (this.params == null) { throw new NullPointerException("params == null"); } // throw IllegalArgumentException if w is point at infinity if (this.w.equals(ECPoint.POINT_INFINITY)) { throw new IllegalArgumentException("the w parameter is point at infinity"); } }
public boolean validate(byte[] r, byte[] s) throws Exception { BigInteger x = new BigInteger(1, r); BigInteger y = new BigInteger(1, s); ECPoint w = new ECPoint(x, y); if (w.equals(ECPoint.POINT_INFINITY)) { return false; } ECParameterSpec params = publicKey.getParams(); EllipticCurve curve = params.getCurve(); BigInteger p = ((ECFieldFp) curve.getField()).getP(); BigInteger p_sub1 = p.subtract(BigInteger.ONE); if (!(x.compareTo(p_sub1) <= 0 && y.compareTo(p_sub1) <= 0)) { return false; } BigInteger tmp = x.multiply(curve.getA()). add(curve.getB()). add(x.modPow(three, p)). mod(p); BigInteger y_2 = y.modPow(two, p); return y_2.equals(tmp); }
/** Checks that the public key's params spec is the same as the private key's params spec. */ static void validatePublicKeySpec(ECPublicKey publicKey, ECPrivateKey privateKey) throws GeneralSecurityException { try { ECParameterSpec publicKeySpec = publicKey.getParams(); ECParameterSpec privateKeySpec = privateKey.getParams(); if (!publicKeySpec.getCurve().equals(privateKeySpec.getCurve()) || !publicKeySpec.getGenerator().equals(privateKeySpec.getGenerator()) || !publicKeySpec.getOrder().equals(privateKeySpec.getOrder()) || publicKeySpec.getCofactor() != privateKeySpec.getCofactor()) { throw new GeneralSecurityException("invalid public key spec"); } } catch (IllegalArgumentException | NullPointerException ex) { // The Java security providers on Android K and Android L might throw these unchecked // exceptions, converting them to a checked one to not crash the JVM. throw new GeneralSecurityException(ex.toString()); } }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPublicKey)) { return false; } final ECPublicKey other = (ECPublicKey) o; if (!getPublicKey().equals(other.getW())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPublicKey) { OpenSSLECPublicKey other = (OpenSSLECPublicKey) o; return key.equals(other.key); } if (!(o instanceof ECPublicKey)) { return false; } final ECPublicKey other = (ECPublicKey) o; if (!getPublicKey().equals(other.getW())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPublicKey)) { return false; } final ECPublicKey other = (ECPublicKey) o; if (!getPublicKey().equals(other.getW())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPublicKey) { OpenSSLECPublicKey other = (OpenSSLECPublicKey) o; return key.equals(other.key); } if (!(o instanceof ECPublicKey)) { return false; } final ECPublicKey other = (ECPublicKey) o; if (!getPublicKey().equals(other.getW())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPrivateKey)) { return false; } final ECPrivateKey other = (ECPrivateKey) o; if (!getPrivateKey().equals(other.getS())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPrivateKey)) { return false; } final ECPrivateKey other = (ECPrivateKey) o; if (!getPrivateKey().equals(other.getS())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
private static boolean matchCurve(ECParameterSpec params, Curve curve) { int fieldSize = params.getCurve().getField().getFieldSize(); if (curve.getCurve().getField().getFieldSize() == fieldSize && curve.getCurve().equals(params.getCurve()) && curve.getGenerator().equals(params.getGenerator()) && curve.getOrder().equals(params.getOrder()) && curve.getCofactor() == params.getCofactor()) { return true; } else { return false; } }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPrivateKey)) { return false; } final ECPrivateKey other = (ECPrivateKey) o; if (!getPrivateKey().equals(other.getS())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPrivateKey)) { return false; } final ECPrivateKey other = (ECPrivateKey) o; if (!getPrivateKey().equals(other.getS())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
private static void validateEcCurves(ECPublicKey publicKey, ECPrivateKey privateKey) throws JOSEException { final ECParameterSpec pubParams = publicKey.getParams(); final ECParameterSpec privParams = privateKey.getParams(); if (!pubParams.getCurve().equals(privParams.getCurve())) { throw new JOSEException("Public/private EC key curve mismatch: " + publicKey); } if (pubParams.getCofactor() != privParams.getCofactor()) { throw new JOSEException("Public/private EC key cofactor mismatch: " + publicKey); } if (!pubParams.getGenerator().equals(privParams.getGenerator())) { throw new JOSEException("Public/private EC key generator mismatch: " + publicKey); } if (!pubParams.getOrder().equals(privParams.getOrder())) { throw new JOSEException("Public/private EC key order mismatch: " + publicKey); } }