java.security.spec.ECParameterSpec Java Examples

The following examples show how to use java.security.spec.ECParameterSpec. 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: JCEECPublicKey.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
public JCEECPublicKey(
    String                  algorithm,
    ECPublicKeyParameters   params,
    ECParameterSpec         spec)
{
    ECDomainParameters      dp = params.getParameters();

    this.algorithm = algorithm;
    this.q = params.getQ();

    if (spec == null)
    {
        EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed());

        this.ecSpec = createSpec(ellipticCurve, dp);
    }
    else
    {
        this.ecSpec = spec;
    }
}
 
Example #2
Source File: ECDHKeyExchange.java    From openjsse with GNU General Public License v2.0 6 votes vote down vote up
static ECDHECredentials valueOf(NamedGroup namedGroup,
    byte[] encodedPoint) throws IOException, GeneralSecurityException {

    if (namedGroup.type != NamedGroupType.NAMED_GROUP_ECDHE) {
        throw new RuntimeException(
            "Credentials decoding:  Not ECDHE named group");
    }

    if (encodedPoint == null || encodedPoint.length == 0) {
        return null;
    }

    ECParameterSpec parameters =
            JsseJce.getECParameterSpec(namedGroup.oid);
    if (parameters == null) {
        return null;
    }

    ECPoint point = JsseJce.decodePoint(
            encodedPoint, parameters.getCurve());
    KeyFactory factory = JsseJce.getKeyFactory("EC");
    ECPublicKey publicKey = (ECPublicKey)factory.generatePublic(
            new ECPublicKeySpec(point, parameters));
    return new ECDHECredentials(publicKey, namedGroup);
}
 
Example #3
Source File: BCDSTU4145PublicKey.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
public BCDSTU4145PublicKey(
    org.ripple.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.ripple.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
 
Example #4
Source File: P12Manager.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
public PublicKey getPublicKey()
        throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException,
                InvalidKeySpecException, NoSuchProviderException {
    ECPrivateKey privateKey = (ECPrivateKey) getPrivateKey();

    ECParameterSpec params = privateKey.getParams();

    org.bouncycastle.jce.spec.ECParameterSpec bcSpec =
            org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertSpec(params, false);
    org.bouncycastle.math.ec.ECPoint q = bcSpec.getG().multiply(privateKey.getS());
    org.bouncycastle.math.ec.ECPoint bcW = bcSpec.getCurve().decodePoint(q.getEncoded(false));
    ECPoint w =
            new ECPoint(
                    bcW.getAffineXCoord().toBigInteger(), bcW.getAffineYCoord().toBigInteger());
    ECPublicKeySpec keySpec = new ECPublicKeySpec(w, tryFindNamedCurveSpec(params));
    return (PublicKey)
            KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME)
                    .generatePublic(keySpec);
}
 
Example #5
Source File: ECKeyPairGenerator.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private KeyPair generateKeyPairNative(SecureRandom random)
    throws Exception {

    ECParameterSpec ecParams = (ECParameterSpec) params;
    byte[] encodedParams = ECUtil.encodeECParameterSpec(null, ecParams);

    // seed is twice the key size (in bytes) plus 1
    byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
    random.nextBytes(seed);
    Object[] keyBytes = generateECKeyPair(keySize, encodedParams, seed);

    // The 'params' object supplied above is equivalent to the native
    // one so there is no need to fetch it.
    // keyBytes[0] is the encoding of the native private key
    BigInteger s = new BigInteger(1, (byte[]) keyBytes[0]);

    PrivateKey privateKey = new ECPrivateKeyImpl(s, ecParams);

    // keyBytes[1] is the encoding of the native public key
    byte[] pubKey = (byte[]) keyBytes[1];
    ECPoint w = ECUtil.decodePoint(pubKey, ecParams.getCurve());
    PublicKey publicKey = new ECPublicKeyImpl(w, ecParams);

    return new KeyPair(publicKey, privateKey);
}
 
Example #6
Source File: ECTesterStandalone.java    From ECTester with MIT License 6 votes vote down vote up
/**
 *
 */
private void export() throws NoSuchAlgorithmException, IOException {
    ProviderECLibrary lib = cfg.selected;
    KeyPairGeneratorIdent ident = null;
    String algo = cli.getOptionValue("export.type", "EC");
    for (KeyPairGeneratorIdent kpIdent : lib.getKPGs()) {
        if (kpIdent.contains(algo)) {
            ident = kpIdent;
            break;
        }
    }
    if (ident == null) {
        throw new NoSuchAlgorithmException(algo);
    }
    KeyPairGenerator kpg = ident.getInstance(lib.getProvider());
    if (cli.hasOption("export.bits")) {
        int bits = Integer.parseInt(cli.getOptionValue("export.bits"));
        kpg.initialize(bits);
    }
    KeyPair kp = kpg.genKeyPair();
    ECPrivateKey privateKey = (ECPrivateKey) kp.getPrivate();
    ECParameterSpec params = privateKey.getParams();
    System.out.println(params);
    EC_Curve curve = EC_Curve.fromSpec(params);
    curve.writeCSV(System.out);
}
 
Example #7
Source File: DOMKeyValue.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static String getCurveOid(ECParameterSpec params) {
    // Check that the params represent one of the supported
    // curves. If there is a match, return the object identifier
    // of the curve.
    Curve match;
    if (matchCurve(params, SECP256R1)) {
        match = SECP256R1;
    } else if (matchCurve(params, SECP384R1)) {
        match = SECP384R1;
    } else if (matchCurve(params, SECP521R1)) {
        match = SECP521R1;
    } else {
        return null;
    }
    return match.getObjectId();
}
 
Example #8
Source File: PEMManager.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
public PublicKey getPublicKey()
        throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {
    ECPrivateKey privateKey = (ECPrivateKey) getPrivateKey();

    ECParameterSpec params = privateKey.getParams();

    org.bouncycastle.jce.spec.ECParameterSpec bcSpec =
            org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertSpec(params, false);
    org.bouncycastle.math.ec.ECPoint q = bcSpec.getG().multiply(privateKey.getS());
    org.bouncycastle.math.ec.ECPoint bcW = bcSpec.getCurve().decodePoint(q.getEncoded(false));
    ECPoint w =
            new ECPoint(
                    bcW.getAffineXCoord().toBigInteger(), bcW.getAffineYCoord().toBigInteger());
    ECPublicKeySpec keySpec = new ECPublicKeySpec(w, tryFindNamedCurveSpec(params));
    return (PublicKey)
            KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME)
                    .generatePublic(keySpec);
}
 
Example #9
Source File: EC5Util.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public static ECParameterSpec convertSpec(
    EllipticCurve ellipticCurve,
    org.ripple.bouncycastle.jce.spec.ECParameterSpec spec)
{
    if (spec instanceof ECNamedCurveParameterSpec)
    {
        return new ECNamedCurveSpec(
            ((ECNamedCurveParameterSpec)spec).getName(),
            ellipticCurve,
            new ECPoint(
                spec.getG().getAffineXCoord().toBigInteger(),
                spec.getG().getAffineYCoord().toBigInteger()),
            spec.getN(),
            spec.getH());
    }
    else
    {
        return new ECParameterSpec(
            ellipticCurve,
            new ECPoint(
                spec.getG().getAffineXCoord().toBigInteger(),
                spec.getG().getAffineYCoord().toBigInteger()),
            spec.getN(),
            spec.getH().intValue());
    }
}
 
Example #10
Source File: ECKeyPairGenerator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    if (params instanceof ECParameterSpec) {
        this.params = ECUtil.getECParameterSpec(null,
                                                (ECParameterSpec)params);
        if (this.params == null) {
            throw new InvalidAlgorithmParameterException(
                "Unsupported curve: " + params);
        }
    } else if (params instanceof ECGenParameterSpec) {
        String name = ((ECGenParameterSpec)params).getName();
        this.params = ECUtil.getECParameterSpec(null, name);
        if (this.params == null) {
            throw new InvalidAlgorithmParameterException(
                "Unknown curve name: " + name);
        }
    } else {
        throw new InvalidAlgorithmParameterException(
            "ECParameterSpec or ECGenParameterSpec required for EC");
    }
    this.keySize =
        ((ECParameterSpec)this.params).getCurve().getField().getFieldSize();
    this.random = random;
}
 
Example #11
Source File: ECKeyPairGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    if (params instanceof ECParameterSpec) {
        this.params = ECUtil.getECParameterSpec(null,
                                                (ECParameterSpec)params);
        if (this.params == null) {
            throw new InvalidAlgorithmParameterException(
                "Unsupported curve: " + params);
        }
    } else if (params instanceof ECGenParameterSpec) {
        String name = ((ECGenParameterSpec)params).getName();
        this.params = ECUtil.getECParameterSpec(null, name);
        if (this.params == null) {
            throw new InvalidAlgorithmParameterException(
                "Unknown curve name: " + name);
        }
    } else {
        throw new InvalidAlgorithmParameterException(
            "ECParameterSpec or ECGenParameterSpec required for EC");
    }
    this.keySize =
        ((ECParameterSpec)this.params).getCurve().getField().getFieldSize();
    this.random = random;
}
 
Example #12
Source File: TrailingSignatureAlgorithmTest.java    From aws-encryption-sdk-java with Apache License 2.0 6 votes vote down vote up
private void testSerialization(CryptoAlgorithm algorithm, String curveName, int[] x, int[] y, int[] expected) throws Exception {
    byte[] xBytes = TestUtils.unsignedBytesToSignedBytes(x);
    byte[] yBytes = TestUtils.unsignedBytesToSignedBytes(y);

    final AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
    parameters.init(new ECGenParameterSpec(curveName));
    ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);

    PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(
            new ECPublicKeySpec(new ECPoint(new BigInteger(1, xBytes), new BigInteger(1, yBytes)), ecParameterSpec));

    int[] result = TestUtils.signedBytesToUnsignedBytes(Utils.decodeBase64String(TrailingSignatureAlgorithm
            .forCryptoAlgorithm(algorithm)
            .serializePublicKey(publicKey)));

    assertArrayEquals(expected, result);
}
 
Example #13
Source File: DOMKeyValue.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
void getMethods() throws ClassNotFoundException, NoSuchMethodException {
    Class c  = Class.forName("sun.security.ec.ECParameters");
    Class[] params = new Class[] { ECPoint.class, EllipticCurve.class };
    encodePoint = c.getMethod("encodePoint", params);
    params = new Class[] { ECParameterSpec.class };
    getCurveName = c.getMethod("getCurveName", params);
    params = new Class[] { byte[].class, EllipticCurve.class };
    decodePoint = c.getMethod("decodePoint", params);
    c  = Class.forName("sun.security.ec.NamedCurve");
    params = new Class[] { String.class };
    getECParameterSpec = c.getMethod("getECParameterSpec", params);
}
 
Example #14
Source File: BCECGOST3410PublicKey.java    From ripple-lib-java with ISC License 5 votes vote down vote up
private ECParameterSpec createSpec(EllipticCurve ellipticCurve, ECDomainParameters dp)
{
    return new ECParameterSpec(
        ellipticCurve,
        new ECPoint(
            dp.getG().getAffineXCoord().toBigInteger(),
            dp.getG().getAffineYCoord().toBigInteger()),
        dp.getN(),
        dp.getH().intValue());
}
 
Example #15
Source File: Ssh2EcdsaSha2NistPublicKey.java    From j2ssh-maverick with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void init(byte[] blob, int start, int len) throws SshException {

		ByteArrayReader buf = new ByteArrayReader(blob, start, len);
		try {

			@SuppressWarnings("unused")
			String type = buf.readString();

			buf.readString();
			byte[] Q = buf.readBinaryString();

			ECParameterSpec ecspec = getCurveParams(curve);

			ECPoint p = ECUtils.fromByteArray(Q, ecspec.getCurve());
			KeyFactory keyFactory = JCEProvider
					.getProviderForAlgorithm(JCEAlgorithms.JCE_EC) == null ? KeyFactory
					.getInstance(JCEAlgorithms.JCE_EC) : KeyFactory
					.getInstance(JCEAlgorithms.JCE_EC, JCEProvider
							.getProviderForAlgorithm(JCEAlgorithms.JCE_EC));
			pub = (ECPublicKey) keyFactory.generatePublic(new ECPublicKeySpec(
					p, ecspec));
		} catch (Throwable t) {
			t.printStackTrace();
			throw new SshException("Failed to decode public key blob",
					SshException.INTERNAL_ERROR);
		} finally {
			try {
				buf.close();
			} catch (IOException e) {
			}
		}

	}
 
Example #16
Source File: JCEECPrivateKey.java    From ripple-lib-java with ISC License 5 votes vote down vote up
org.ripple.bouncycastle.jce.spec.ECParameterSpec engineGetSpec()
{
    if (ecSpec != null)
    {
        return EC5Util.convertSpec(ecSpec, withCompression);
    }

    return BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
}
 
Example #17
Source File: JCEECPrivateKey.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
org.ripple.bouncycastle.jce.spec.ECParameterSpec engineGetSpec()
{
    if (ecSpec != null)
    {
        return EC5Util.convertSpec(ecSpec, withCompression);
    }

    return BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
}
 
Example #18
Source File: GenerationTests.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #19
Source File: BCECPrivateKey.java    From ripple-lib-java with ISC License 5 votes vote down vote up
org.ripple.bouncycastle.jce.spec.ECParameterSpec engineGetSpec()
{
    if (ecSpec != null)
    {
        return EC5Util.convertSpec(ecSpec, withCompression);
    }

    return configuration.getEcImplicitlyCa();
}
 
Example #20
Source File: BCECPrivateKey.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
org.ripple.bouncycastle.jce.spec.ECParameterSpec engineGetSpec()
{
    if (ecSpec != null)
    {
        return EC5Util.convertSpec(ecSpec, withCompression);
    }

    return configuration.getEcImplicitlyCa();
}
 
Example #21
Source File: BCDSTU4145PublicKey.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
private ECParameterSpec createSpec(EllipticCurve ellipticCurve, ECDomainParameters dp)
{
    return new ECParameterSpec(
        ellipticCurve,
        new ECPoint(
            dp.getG().getAffineXCoord().toBigInteger(),
            dp.getG().getAffineYCoord().toBigInteger()),
        dp.getN(),
        dp.getH().intValue());
}
 
Example #22
Source File: SM2NamedCurve.java    From julongchain with Apache License 2.0 5 votes vote down vote up
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 #23
Source File: BCECPublicKey.java    From ripple-lib-java with ISC License 5 votes vote down vote up
org.ripple.bouncycastle.jce.spec.ECParameterSpec engineGetSpec()
{
    if (ecSpec != null)
    {
        return EC5Util.convertSpec(ecSpec, withCompression);
    }

    return configuration.getEcImplicitlyCa();
}
 
Example #24
Source File: DOMKeyValue.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
void getMethods() throws ClassNotFoundException, NoSuchMethodException {
    Class c  = Class.forName("sun.security.ec.ECParameters");
    Class[] params = new Class[] { ECPoint.class, EllipticCurve.class };
    encodePoint = c.getMethod("encodePoint", params);
    params = new Class[] { ECParameterSpec.class };
    getCurveName = c.getMethod("getCurveName", params);
    params = new Class[] { byte[].class, EllipticCurve.class };
    decodePoint = c.getMethod("decodePoint", params);
    c  = Class.forName("sun.security.ec.NamedCurve");
    params = new Class[] { String.class };
    getECParameterSpec = c.getMethod("getECParameterSpec", params);
}
 
Example #25
Source File: DOMKeyValue.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void getMethods() throws ClassNotFoundException, NoSuchMethodException {
    Class c  = Class.forName("sun.security.ec.ECParameters");
    Class[] params = new Class[] { ECPoint.class, EllipticCurve.class };
    encodePoint = c.getMethod("encodePoint", params);
    params = new Class[] { ECParameterSpec.class };
    getCurveName = c.getMethod("getCurveName", params);
    params = new Class[] { byte[].class, EllipticCurve.class };
    decodePoint = c.getMethod("decodePoint", params);
    c  = Class.forName("sun.security.ec.NamedCurve");
    params = new Class[] { String.class };
    getECParameterSpec = c.getMethod("getECParameterSpec", params);
}
 
Example #26
Source File: PKCS11Test.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
boolean checkSupport(Vector<ECParameterSpec> supportedEC,
        ECParameterSpec curve) {
    boolean found = false;
    for (ECParameterSpec ec: supportedEC) {
        if (ec.equals(curve)) {
            return true;
        }
    }
    return false;
}
 
Example #27
Source File: DOMKeyValue.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
void getMethods() throws ClassNotFoundException, NoSuchMethodException {
    Class c  = Class.forName("sun.security.ec.ECParameters");
    Class[] params = new Class[] { ECPoint.class, EllipticCurve.class };
    encodePoint = c.getMethod("encodePoint", params);
    params = new Class[] { ECParameterSpec.class };
    getCurveName = c.getMethod("getCurveName", params);
    params = new Class[] { byte[].class, EllipticCurve.class };
    decodePoint = c.getMethod("decodePoint", params);
    c  = Class.forName("sun.security.ec.NamedCurve");
    params = new Class[] { String.class };
    getECParameterSpec = c.getMethod("getECParameterSpec", params);
}
 
Example #28
Source File: SupportedEllipticCurvesExtension.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static int getCurveIndex(ECParameterSpec params) {
    String oid = JsseJce.getNamedCurveOid(params);
    if (oid == null) {
        return -1;
    }
    Integer n = curveIndices.get(oid);
    return (n == null) ? -1 : n;
}
 
Example #29
Source File: EC5Util.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public static org.ripple.bouncycastle.math.ec.ECPoint convertPoint(
    ECParameterSpec ecSpec,
    ECPoint point,
    boolean withCompression)
{
    return convertPoint(convertCurve(ecSpec.getCurve()), point, withCompression);
}
 
Example #30
Source File: Main.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private String fullDisplayAlgName(Key key) {
    String result = key.getAlgorithm();
    if (key instanceof ECKey) {
        ECParameterSpec paramSpec = ((ECKey) key).getParams();
        if (paramSpec instanceof NamedCurve) {
            result += " (" + paramSpec.toString().split(" ")[0] + ")";
        }
    }
    return result;
}