public static ECParameterSpec encodeECParameterSpec(EllipticCurveParameters params) { // Field final BigInteger pInt = new BigInteger(1, params.getP()); final ECField field = new ECFieldFp(pInt); final BigInteger aInt = new BigInteger(1, params.getA()); final BigInteger bInt = new BigInteger(1, params.getB()); final EllipticCurve curve = new EllipticCurve(field, aInt, bInt); // Fixed Point G final BigInteger xInt = new BigInteger(1, params.getX()); final BigInteger yInt = new BigInteger(1, params.getY()); final ECPoint g = new ECPoint(xInt, yInt); // Order N final BigInteger nInt = new BigInteger(1, params.getN()); return new ECParameterSpec(curve, g, nInt, params.getH()); }
private 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 { throw new IllegalStateException("not implemented yet!!!"); } }
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); } }
ECParameterSpec getECParameterSpec() { final String curveName = NativeCrypto.EC_GROUP_get_curve_name(groupCtx); final byte[][] curveParams = NativeCrypto.EC_GROUP_get_curve(groupCtx); final BigInteger p = new BigInteger(curveParams[0]); final BigInteger a = new BigInteger(curveParams[1]); final BigInteger b = new BigInteger(curveParams[2]); final ECField field = new ECFieldFp(p); final EllipticCurve curve = new EllipticCurve(field, a, b); final OpenSSLECPointContext generatorCtx = new OpenSSLECPointContext(this, new NativeRef.EC_POINT(NativeCrypto.EC_GROUP_get_generator(groupCtx))); final ECPoint generator = generatorCtx.getECPoint(); final BigInteger order = new BigInteger(NativeCrypto.EC_GROUP_get_order(groupCtx)); final BigInteger cofactor = new BigInteger(NativeCrypto.EC_GROUP_get_cofactor(groupCtx)); ECParameterSpec spec = new ECParameterSpec(curve, generator, order, cofactor.intValue()); Platform.setCurveName(spec, curveName); return spec; }
/** * 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); }
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 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); } }
/** * Test #5 for <code>EllipticCurve(ECField, BigInteger, BigInteger, byte[])</code> * constructor<br> * Assertion: array <code>seed</code> is copied to prevent subsequent modification<br> * Test preconditions: pass <code>seed</code> to the ctor then modify it<br> * Expected: getSeed() must return unmodified array */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies that byte array of EllipticCurve can't be modified", method = "EllipticCurve", args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class, byte[].class} ) public final void testEllipticCurveECFieldBigIntegerBigIntegerbyteArray05() { ECFieldF2m f = new ECFieldF2m(5); BigInteger a = BigInteger.valueOf(0L); BigInteger b = BigInteger.valueOf(19L); byte[] seed = new byte[24]; byte[] seedCopy = seed.clone(); EllipticCurve c = new EllipticCurve(f, a, b, seedCopy); // modify array passed seedCopy[0] = (byte) 1; // check that above modification did not changed // internal state of test object assertTrue(Arrays.equals(seed, c.getSeed())); }
private static Curve initializeCurve(String name, String oid, String sfield, String a, String b, String x, String y, String n, int h) { BigInteger p = bigInt(sfield); ECField field = new ECFieldFp(p); EllipticCurve curve = new EllipticCurve(field, bigInt(a), bigInt(b)); ECPoint g = new ECPoint(bigInt(x), bigInt(y)); return new Curve(name, oid, curve, g, bigInt(n), h); }
private static ECParameterSpec initECParams( String sfield, String a, String b, String gx, String gy, String n, int h) { ECField field = new ECFieldFp(bigInt(sfield)); EllipticCurve curve = new EllipticCurve(field, bigInt(a), bigInt(b)); ECPoint g = new ECPoint(bigInt(gx), bigInt(gy)); return new ECParameterSpec(curve, g, bigInt(n), h); }
private static ECCurve toBouncyCastleECCurve(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 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 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)); }
/** * 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"); } }
/** * Decompress a point on an elliptic curve. * * @param bytes The compressed point. Its representation is z || x where z is 2+lsb(y) and x is * using a unsigned fixed length big-endian representation. * @param ecParams the specification of the curve. Only Weierstrass curves over prime order fields * are implemented. */ public static ECPoint decompressPoint(byte[] bytes, 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(); int expectedLength = 1 + (p.bitLength() + 7) / 8; if (bytes.length != expectedLength) { throw new GeneralSecurityException("compressed point has wrong length"); } boolean lsb; switch (bytes[0]) { case 2: lsb = false; break; case 3: lsb = true; break; default: throw new GeneralSecurityException("Invalid format"); } BigInteger x = new BigInteger(1, Arrays.copyOfRange(bytes, 1, bytes.length)); 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 (lsb != y.testBit(0)) { y = p.subtract(y).mod(p); } return new ECPoint(x, y); }
private static EllipticCurve convertCurve( ECCurve curve, byte[] seed) { ECField field = convertField(curve.getField()); BigInteger a = curve.getA().toBigInteger(), b = curve.getB().toBigInteger(); return new EllipticCurve(field, a, b, seed); }
private static ECField convertField(FiniteField field) { if (ECAlgorithms.isFpField(field)) { return new ECFieldFp(field.getCharacteristic()); } else //if (ECAlgorithms.isF2mField(curveField)) { Polynomial poly = ((PolynomialExtensionField)field).getMinimalPolynomial(); int[] exponents = poly.getExponentsPresent(); int[] ks = Arrays.reverse(Arrays.copyOfRange(exponents, 1, exponents.length - 1)); return new ECFieldF2m(poly.getDegree(), ks); } }
private static EllipticCurve convertCurve( ECCurve curve) { ECField field = convertField(curve.getField()); BigInteger a = curve.getA().toBigInteger(), b = curve.getB().toBigInteger(); // TODO: the Sun EC implementation doesn't currently handle the seed properly // so at the moment it's set to null. Should probably look at making this configurable return new EllipticCurve(field, a, b, null); }
public static EllipticCurve convertCurve( ECCurve curve, byte[] seed) { ECField field = convertField(curve.getField()); BigInteger a = curve.getA().toBigInteger(), b = curve.getB().toBigInteger(); // TODO: the Sun EC implementation doesn't currently handle the seed properly // so at the moment it's set to null. Should probably look at making this configurable return new EllipticCurve(field, a, b, null); }
public static ECField convertField(FiniteField field) { if (ECAlgorithms.isFpField(field)) { return new ECFieldFp(field.getCharacteristic()); } else //if (ECAlgorithms.isF2mField(curveField)) { Polynomial poly = ((PolynomialExtensionField)field).getMinimalPolynomial(); int[] exponents = poly.getExponentsPresent(); int[] ks = Arrays.reverse(Arrays.copyOfRange(exponents, 1, exponents.length - 1)); return new ECFieldF2m(poly.getDegree(), ks); } }
/** * @tests java/security/spec/EllipticCurve#EllipticCurve(EcField,BigInteger,BigInteger) */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Regression test.", method = "EllipticCurve", args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class} ) public final void testEllipticCurveECFieldBigIntegerBigInteger05() { // Regression for Harmony-731 EllipticCurve ec = new EllipticCurve(new testECField(), BigInteger .valueOf(4L), BigInteger.ONE); assertEquals("incorrect a", ec.getA(), BigInteger.valueOf(4L)); assertEquals("incorrect b", ec.getB(), BigInteger.ONE); assertEquals("incorrect size", ec.getField().getFieldSize(), 2); }
public static void assertECFieldEquals(String message, ECField expected, ECField actual) { if (expected == actual) { return; } assertEquals(message + "[size]", expected.getFieldSize(), actual.getFieldSize()); }
public ECParameterSpec genSpec() { BigInteger pb = new NativeBigInteger(ps); BigInteger nb = new NativeBigInteger(ns); BigInteger sb = new NativeBigInteger(ss.replace(" ", ""), 16); BigInteger bb = new NativeBigInteger(bs.replace(" ", ""), 16); BigInteger gxb = new NativeBigInteger(gxs.replace(" ", ""), 16); BigInteger gyb = new NativeBigInteger(gys.replace(" ", ""), 16); BigInteger ab = new NativeBigInteger(A.mod(pb)); ECField field = new ECFieldFp(pb); EllipticCurve curve = new EllipticCurve(field, ab, bb, sb.toByteArray()); ECPoint g = new ECPoint(gxb, gyb); return new ECParameterSpec(curve, g, nb, H); }
private static void addParameterSpec(int namedCurveId, String p, String a, String b, String x, String y, String n, int h) { ECField field = new ECFieldFp(new BigInteger(p, 16)); EllipticCurve curve = new EllipticCurve(field, new BigInteger(a, 16), new BigInteger(b, 16)); ECPoint g = new ECPoint(new BigInteger(x, 16), new BigInteger(y, 16)); ECParameterSpec params = new ECParameterSpec(curve, g, new BigInteger(n, 16), h); NAMED_CURVE_PARAMETERS.put(namedCurveId, params); }
MyEllipticCurve(ECField f, BigInteger a, BigInteger b, byte[] seed) { super(f, a, b, seed); }