BigInteger p = ((ECFieldFp) this.field).getP(); if (this.a.signum() < 0 || this.a.compareTo(p) >= 0) { throw new IllegalArgumentException("the a is not in the field");
public static ECPoint scalarMult(ECPoint p, BigInteger kin, EllipticCurve curve) { ECPoint r = ECPoint.POINT_INFINITY; BigInteger prime = ((ECFieldFp) curve.getField()).getP(); BigInteger k = kin.mod(prime); int length = k.bitLength(); byte[] binarray = new byte[length]; for (int i = 0; i <= length-1; i++) { binarray[i] = k.mod(TWO).byteValue(); k = k.divide(TWO); } for (int i = length-1; i >= 0; i--) { // i should start at length-1 not -2 because the MSB of binarry may not be 1 r = doublePoint(r, curve); if (binarray[i] == 1) r = addPoint(r, p, curve); } return r; }
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; }
/** * Returns the modulus of the field used by the curve specified in ecParams. * * @param curve must be a prime order elliptic curve * @return the order of the finite field over which curve is defined. */ public static BigInteger getModulus(EllipticCurve curve) throws GeneralSecurityException { java.security.spec.ECField field = curve.getField(); if (field instanceof java.security.spec.ECFieldFp) { return ((java.security.spec.ECFieldFp) field).getP(); } else { throw new GeneralSecurityException("Only curves over prime order fields are supported"); } }
private static BigInteger getPrime(final ECParameterSpec params) { if (params == null) { throw new IllegalArgumentException( "Los parametros no pueden ser nulos" //$NON-NLS-1$ ); } final EllipticCurve curve = params.getCurve(); final ECField field = curve.getField(); if (!(field instanceof ECFieldFp)) { throw new IllegalStateException( "Solo se soporta 'ECFieldFp' y se proporciono " + field.getClass().getCanonicalName() //$NON-NLS-1$ ); } return ((ECFieldFp)field).getP(); }
private static ECCurve convertCurve( EllipticCurve ec, BigInteger order, int coFactor) { ECField field = ec.getField(); BigInteger a = ec.getA(); BigInteger b = ec.getB(); if (field instanceof ECFieldFp) { return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b, order, BigInteger.valueOf(coFactor)); } else { throw new IllegalStateException("not implemented yet!!!"); } }
private static ECCurve convertCurve( EllipticCurve ec, BigInteger order, int coFactor) { ECField field = ec.getField(); BigInteger a = ec.getA(); BigInteger b = ec.getB(); if (field instanceof ECFieldFp) { return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b, order, BigInteger.valueOf(coFactor)); } else { throw new IllegalStateException("not implemented yet!!!"); } }
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); }
private static ECCurve convertCurve(EllipticCurve ec, BigInteger order, int cofactor) { ECField field = ec.getField(); BigInteger a = ec.getA(); BigInteger b = ec.getB(); if (field instanceof ECFieldFp) { return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b, order, BigInteger.valueOf(cofactor)); } else { ECFieldF2m fieldF2m = (ECFieldF2m)field; int m = fieldF2m.getM(); int ks[] = convertMidTerms(fieldF2m.getMidTermsOfReductionPolynomial()); return new ECCurve.F2m(m, ks[0], ks[1], ks[2], a, b, order, BigInteger.valueOf(cofactor)); } }
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); } }
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); } }
public static OpenSSLECGroupContext getInstance(ECParameterSpec params) throws InvalidAlgorithmParameterException { final String curveName = params.getCurveName(); if (curveName != null) { return OpenSSLECGroupContext.getCurveByName(curveName); } final EllipticCurve curve = params.getCurve(); final ECField field = curve.getField(); final int type; final BigInteger p; if (field instanceof ECFieldFp) { type = NativeCrypto.EC_CURVE_GFP; p = ((ECFieldFp) field).getP(); } else if (field instanceof ECFieldF2m) { type = NativeCrypto.EC_CURVE_GF2M; p = ((ECFieldF2m) field).getReductionPolynomial(); } else { throw new InvalidParameterException("unhandled field class " + field.getClass().getName()); } final ECPoint generator = params.getGenerator(); return OpenSSLECGroupContext.getInstance(type, p, curve.getA(), curve.getB(), generator.getAffineX(), generator.getAffineY(), params.getOrder(), BigInteger.valueOf(params.getCofactor())); }
public static OpenSSLECGroupContext getInstance(ECParameterSpec params) throws InvalidAlgorithmParameterException { final String curveName = params.getCurveName(); if (curveName != null) { return OpenSSLECGroupContext.getCurveByName(curveName); } final EllipticCurve curve = params.getCurve(); final ECField field = curve.getField(); final int type; final BigInteger p; if (field instanceof ECFieldFp) { type = NativeCrypto.EC_CURVE_GFP; p = ((ECFieldFp) field).getP(); } else if (field instanceof ECFieldF2m) { type = NativeCrypto.EC_CURVE_GF2M; p = ((ECFieldF2m) field).getReductionPolynomial(); } else { throw new InvalidParameterException("unhandled field class " + field.getClass().getName()); } final ECPoint generator = params.getGenerator(); return OpenSSLECGroupContext.getInstance(type, p, curve.getA(), curve.getB(), generator.getAffineX(), generator.getAffineY(), params.getOrder(), BigInteger.valueOf(params.getCofactor())); }
public static ECCurve convertCurve( EllipticCurve ec) { ECField field = ec.getField(); BigInteger a = ec.getA(); BigInteger b = ec.getB(); if (field instanceof ECFieldFp) { return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b); } 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); } }
public static ECCurve convertCurve( EllipticCurve ec) { ECField field = ec.getField(); BigInteger a = ec.getA(); BigInteger b = ec.getB(); if (field instanceof ECFieldFp) { return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b); } 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); } }
public static ECCurve convertCurve( EllipticCurve ec) { ECField field = ec.getField(); BigInteger a = ec.getA(); BigInteger b = ec.getB(); if (field instanceof ECFieldFp) { return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b); } 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); } }
private static ECParameterSpec mapNonceGMWithECDH(final BigInteger nonceS, final ECPoint sharedSecretPointH, final ECParameterSpec params) { // D~ = (p, a, b, G~, n, h) where G~ = [s]G + H final ECPoint generator = params.getGenerator(); final EllipticCurve curve = params.getCurve(); final BigInteger a = curve.getA(); final BigInteger b = curve.getB(); final ECFieldFp field = (ECFieldFp)curve.getField(); final BigInteger p = field.getP(); final BigInteger order = params.getOrder(); final int cofactor = params.getCofactor(); final ECPoint ephemeralGenerator = add(multiply(nonceS, generator, params), sharedSecretPointH, params); if (!toSpongyCastleECPoint(ephemeralGenerator, params).isValid()) { LOGGER.warning("Se ha generado un punto invalido"); //$NON-NLS-1$ } return new ECParameterSpec(new EllipticCurve(new ECFieldFp(p), a, b), ephemeralGenerator, order, cofactor); }
public PublicKeyDataObject getPublicKeyDataObject(ASN1ObjectIdentifier usage, PublicKey publicKey) { if (publicKey instanceof java.security.interfaces.RSAPublicKey) { java.security.interfaces.RSAPublicKey pubKey = (java.security.interfaces.RSAPublicKey)publicKey; return new RSAPublicKey(usage, pubKey.getModulus(), pubKey.getPublicExponent()); } else { ECPublicKey pubKey = (ECPublicKey)publicKey; java.security.spec.ECParameterSpec params = pubKey.getParams(); return new ECDSAPublicKey( usage, ((ECFieldFp)params.getCurve().getField()).getP(), params.getCurve().getA(), params.getCurve().getB(), convertPoint(convertCurve(params.getCurve(), params.getOrder(), params.getCofactor()), params.getGenerator()).getEncoded(), params.getOrder(), convertPoint(convertCurve(params.getCurve(), params.getOrder(), params.getCofactor()), pubKey.getW()).getEncoded(), params.getCofactor()); } }
public PublicKeyDataObject getPublicKeyDataObject(ASN1ObjectIdentifier usage, PublicKey publicKey) { if (publicKey instanceof java.security.interfaces.RSAPublicKey) { java.security.interfaces.RSAPublicKey pubKey = (java.security.interfaces.RSAPublicKey)publicKey; return new RSAPublicKey(usage, pubKey.getModulus(), pubKey.getPublicExponent()); } else { ECPublicKey pubKey = (ECPublicKey)publicKey; java.security.spec.ECParameterSpec params = pubKey.getParams(); return new ECDSAPublicKey( usage, ((ECFieldFp)params.getCurve().getField()).getP(), params.getCurve().getA(), params.getCurve().getB(), convertPoint(convertCurve(params.getCurve(), params.getOrder(), params.getCofactor()), params.getGenerator()).getEncoded(), params.getOrder(), convertPoint(convertCurve(params.getCurve(), params.getOrder(), params.getCofactor()), pubKey.getW()).getEncoded(), params.getCofactor()); } }