public static boolean isSm2primev2Curve(EllipticCurve curve) { return curve.getB().equals(sm2primev2CurveA); }
/** * Computes the y coordinate of a point on an elliptic curve. This method can be used to * decompress elliptic curve points. * * @param x the x-coordinate of the point * @param lsb the least significant bit of the y-coordinate of the point. * @param curve this must be an elliptic curve over a prime field using Weierstrass * representation. * @return the y coordinate. * @throws GeneralSecurityException if there is no point with coordinate x on the curve, or if * curve is not supported. */ public static BigInteger getY(BigInteger x, boolean lsb, EllipticCurve curve) throws GeneralSecurityException { BigInteger p = getModulus(curve); BigInteger a = curve.getA(); BigInteger b = curve.getB(); BigInteger rhs = x.multiply(x).add(a).multiply(x).add(b).mod(p); BigInteger y = modSqrt(rhs, p); if (lsb != y.testBit(0)) { y = p.subtract(y).mod(p); } return y; }
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 toSpongyCastleECCurve(final ECParameterSpec params) { final EllipticCurve curve = params.getCurve(); final ECField field = curve.getField(); if (!(field instanceof ECFieldFp)) { throw new IllegalArgumentException( "Solo se soporta 'ECFieldFp' y se proporciono " + field.getClass().getCanonicalName() //$NON-NLS-1$ ); } final int coFactor = params.getCofactor(); final BigInteger order = params.getOrder(); final BigInteger a = curve.getA(); final BigInteger b = curve.getB(); final BigInteger p = getPrime(params); return new ECCurve.Fp(p, a, b, order, BigInteger.valueOf(coFactor)); }
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); } }
public static void assertCurveEquals(String message, EllipticCurve expected, EllipticCurve actual) { if (expected == actual) { return; } assertEquals(message + "[A]", expected.getA(), actual.getA()); assertEquals(message + "[B]", expected.getB(), actual.getB()); assertArrayEquals(message + "[seed]", expected.getSeed(), actual.getSeed()); assertECFieldEquals(message + "[field]", expected.getField(), actual.getField()); }
public static void assertCurveEquals(String message, EllipticCurve expected, EllipticCurve actual) { if (expected == actual) { return; } assertEquals(message + "[A]", expected.getA(), actual.getA()); assertEquals(message + "[B]", expected.getB(), actual.getB()); assertArrayEquals(message + "[seed]", expected.getSeed(), actual.getSeed()); assertECFieldEquals(message + "[field]", expected.getField(), actual.getField()); }
public static void assertCurveEquals(String message, EllipticCurve expected, EllipticCurve actual) { if (expected == actual) { return; } assertEquals(message + "[A]", expected.getA(), actual.getA()); assertEquals(message + "[B]", expected.getB(), actual.getB()); assertArrayEquals(message + "[seed]", expected.getSeed(), actual.getSeed()); assertECFieldEquals(message + "[field]", expected.getField(), actual.getField()); }
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()); } }