Java Code Examples for java.security.spec.ECParameterSpec#getCurve()
The following examples show how to use
java.security.spec.ECParameterSpec#getCurve() .
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 |
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: ECOperations.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
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 3
Source File: ECOperations.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
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: EcUtil.java From wycheproof with Apache License 2.0 | 6 votes |
public static void printParameters(ECParameterSpec spec) { System.out.println("cofactor:" + spec.getCofactor()); EllipticCurve curve = spec.getCurve(); System.out.println("A:" + curve.getA()); System.out.println("B:" + curve.getB()); ECField field = curve.getField(); System.out.println("field size:" + field.getFieldSize()); if (field instanceof ECFieldFp) { ECFieldFp fp = (ECFieldFp) field; System.out.println("P:" + fp.getP()); } ECPoint generator = spec.getGenerator(); System.out.println("Gx:" + generator.getAffineX()); System.out.println("Gy:" + generator.getAffineY()); System.out.println("order:" + spec.getOrder()); }
Example 5
Source File: EcUtil.java From wycheproof with Apache License 2.0 | 6 votes |
/** * 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 6
Source File: ECOperations.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
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 7
Source File: SM2NamedCurve.java From julongchain with Apache License 2.0 | 5 votes |
private static SM2NamedCurve genSM2NamedCurve() { ECParameterSpec sm2ParameterSpec = Util.getECParameterSpec(); return new SM2NamedCurve("sm2p256v1", "1.2.156.10197.1.301", sm2ParameterSpec.getCurve(), sm2ParameterSpec.getGenerator(), sm2ParameterSpec.getOrder(), sm2ParameterSpec.getCofactor()); }
Example 8
Source File: JsonWebKey.java From azure-keyvault-java with MIT License | 5 votes |
private static JsonWebKeyCurveName getCurveFromKeyPair(KeyPair keyPair, Provider provider) { try { ECPublicKey key = (ECPublicKey) keyPair.getPublic(); ECParameterSpec spec = key.getParams(); EllipticCurve crv = spec.getCurve(); List<JsonWebKeyCurveName> curveList = Arrays.asList(JsonWebKeyCurveName.P_256, JsonWebKeyCurveName.P_384, JsonWebKeyCurveName.P_521, JsonWebKeyCurveName.P_256K); for (JsonWebKeyCurveName curve : curveList) { ECGenParameterSpec gps = new ECGenParameterSpec(CURVE_TO_SPEC_NAME.get(curve)); KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", provider); kpg.initialize(gps); // Generate dummy keypair to get parameter spec. KeyPair apair = kpg.generateKeyPair(); ECPublicKey apub = (ECPublicKey) apair.getPublic(); ECParameterSpec aspec = apub.getParams(); EllipticCurve acurve = aspec.getCurve(); // Matches the parameter spec if (acurve.equals(crv)) { return curve; } } // Did not find a supported curve. throw new NoSuchAlgorithmException("Curve not supported."); } catch (GeneralSecurityException e) { throw new IllegalStateException(e); } }
Example 9
Source File: EcKey.java From azure-keyvault-java with MIT License | 5 votes |
private JsonWebKeyCurveName getCurveFromKeyPair(KeyPair keyPair) { try { ECPublicKey key = (ECPublicKey) keyPair.getPublic(); ECParameterSpec spec = key.getParams(); EllipticCurve crv = spec.getCurve(); List<JsonWebKeyCurveName> curveList = Arrays.asList(JsonWebKeyCurveName.P_256, JsonWebKeyCurveName.P_384, JsonWebKeyCurveName.P_521, JsonWebKeyCurveName.P_256K); for (JsonWebKeyCurveName curve : curveList) { ECGenParameterSpec gps = new ECGenParameterSpec(CURVE_TO_SPEC_NAME.get(curve)); KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", _provider); kpg.initialize(gps); // Generate dummy keypair to get parameter spec. KeyPair apair = kpg.generateKeyPair(); ECPublicKey apub = (ECPublicKey) apair.getPublic(); ECParameterSpec aspec = apub.getParams(); EllipticCurve acurve = aspec.getCurve(); //Matches the parameter spec if (acurve.equals(crv)) { return curve; } } //Did not find a supported curve. throw new IllegalArgumentException ("Curve not supported."); } catch (GeneralSecurityException e) { throw new IllegalStateException(e); } }
Example 10
Source File: EcUtil.java From wycheproof with Apache License 2.0 | 5 votes |
/** * 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); }
Example 11
Source File: EcUtil.java From wycheproof with Apache License 2.0 | 5 votes |
/** * Returns a weak public key of order 3 such that the public key point is on the curve specified * in ecParams. This method is used to check ECC implementations for missing step in the * verification of the public key. E.g. implementations of ECDH must verify that the public key * contains a point on the curve as well as public and secret key are using the same curve. * * @param ecParams the parameters of the key to attack. This must be a curve in Weierstrass form * over a prime order field. * @return a weak EC group with a genrator of order 3. */ public static ECPublicKeySpec getWeakPublicKey(ECParameterSpec ecParams) throws GeneralSecurityException { EllipticCurve curve = ecParams.getCurve(); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); keyGen.initialize(ecParams); BigInteger p = getModulus(curve); BigInteger three = new BigInteger("3"); while (true) { // Generate a point on the original curve KeyPair keyPair = keyGen.generateKeyPair(); ECPublicKey pub = (ECPublicKey) keyPair.getPublic(); ECPoint w = pub.getW(); BigInteger x = w.getAffineX(); BigInteger y = w.getAffineY(); // Find the curve parameters a,b such that 3*w = infinity. // This is the case if the following equations are satisfied: // 3x == l^2 (mod p) // l == (3x^2 + a) / 2*y (mod p) // y^2 == x^3 + ax + b (mod p) BigInteger l; try { l = modSqrt(x.multiply(three), p); } catch (GeneralSecurityException ex) { continue; } BigInteger xSqr = x.multiply(x).mod(p); BigInteger a = l.multiply(y.add(y)).subtract(xSqr.multiply(three)).mod(p); BigInteger b = y.multiply(y).subtract(x.multiply(xSqr.add(a))).mod(p); EllipticCurve newCurve = new EllipticCurve(curve.getField(), a, b); // Just a sanity check. checkPointOnCurve(w, newCurve); // Cofactor and order are of course wrong. ECParameterSpec spec = new ECParameterSpec(newCurve, w, p, 1); return new ECPublicKeySpec(w, spec); } }
Example 12
Source File: EllipticCurveJsonWebKey.java From Jose4j with Apache License 2.0 | 5 votes |
public EllipticCurveJsonWebKey(ECPublicKey publicKey) { super(publicKey); ECParameterSpec spec = publicKey.getParams(); EllipticCurve curve = spec.getCurve(); curveName = EllipticCurves.getName(curve); }
Example 13
Source File: EcdsaUsingShaAlgorithm.java From Jose4j with Apache License 2.0 | 5 votes |
private void validateKeySpec(ECKey ecKey) throws InvalidKeyException { ECParameterSpec spec = ecKey.getParams(); EllipticCurve curve = spec.getCurve(); String name = EllipticCurves.getName(curve); if (!getCurveName().equals(name)) { throw new InvalidKeyException(getAlgorithmIdentifier() + "/" + getJavaAlgorithm() + " expects a key using " + getCurveName() + " but was " + name); } }