Example usage for org.bouncycastle.jce.spec ECParameterSpec getG

List of usage examples for org.bouncycastle.jce.spec ECParameterSpec getG

Introduction

In this page you can find the example usage for org.bouncycastle.jce.spec ECParameterSpec getG.

Prototype

public ECPoint getG() 

Source Link

Document

return the base point we are using for these domain parameters.

Usage

From source file:com.yahoo.athenz.auth.util.Crypto.java

License:Apache License

public static PublicKey extractPublicKey(PrivateKey privateKey) throws CryptoException {

    // we only support RSA and ECDSA private keys

    PublicKey publicKey = null;//  w w  w  .  j av a  2s  . c  om
    switch (privateKey.getAlgorithm()) {
    case RSA:
        try {
            KeyFactory kf = KeyFactory.getInstance(RSA, BC_PROVIDER);
            RSAPrivateCrtKey rsaCrtKey = (RSAPrivateCrtKey) privateKey;
            RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaCrtKey.getModulus(),
                    rsaCrtKey.getPublicExponent());
            publicKey = kf.generatePublic(keySpec);
        } catch (NoSuchProviderException ex) {
            LOG.error("extractPublicKey: RSA - Caught NoSuchProviderException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        } catch (NoSuchAlgorithmException ex) {
            LOG.error("extractPublicKey: RSA - Caught NoSuchAlgorithmException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        } catch (InvalidKeySpecException ex) {
            LOG.error("extractPublicKey: RSA - Caught InvalidKeySpecException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        }
        break;

    case ECDSA:
        try {
            KeyFactory kf = KeyFactory.getInstance(ECDSA, BC_PROVIDER);
            BCECPrivateKey ecPrivKey = (BCECPrivateKey) privateKey;
            ECMultiplier ecMultiplier = new FixedPointCombMultiplier();
            ECParameterSpec ecParamSpec = (ECParameterSpec) ecPrivKey.getParameters();
            ECPoint ecPointQ = ecMultiplier.multiply(ecParamSpec.getG(), ecPrivKey.getD());
            ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPointQ, ecParamSpec);
            publicKey = kf.generatePublic(keySpec);
        } catch (NoSuchProviderException ex) {
            LOG.error("extractPublicKey: ECDSA - Caught NoSuchProviderException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        } catch (NoSuchAlgorithmException ex) {
            LOG.error(
                    "extractPublicKey: ECDSA - Caught NoSuchAlgorithmException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        } catch (InvalidKeySpecException ex) {
            LOG.error("extractPublicKey: ECDSA - Caught InvalidKeySpecException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        }
        break;

    default:
        String msg = "Unsupported Key Algorithm: " + privateKey.getAlgorithm();
        LOG.error("extractPublicKey: " + msg);
        throw new CryptoException(msg);
    }
    return publicKey;
}

From source file:com.yahoo.athenz.zts.store.DataStore.java

License:Apache License

String getCurveName(org.bouncycastle.jce.spec.ECParameterSpec ecParameterSpec) {

    String curveName = null;/*from ww w.  jav a  2s  . c  om*/
    for (Enumeration names = ECNamedCurveTable.getNames(); names.hasMoreElements();) {

        final String name = (String) names.nextElement();
        final X9ECParameters params = ECNamedCurveTable.getByName(name);

        if (params.getN().equals(ecParameterSpec.getN()) && params.getH().equals(ecParameterSpec.getH())
                && params.getCurve().equals(ecParameterSpec.getCurve())
                && params.getG().equals(ecParameterSpec.getG())) {
            curveName = name;
            break;
        }
    }
    return curveName;
}

From source file:com.yahoo.athenz.zts.store.DataStoreTest.java

License:Apache License

@Test
public void testGetInvalidCurveName() {
    ChangeLogStore clogStore = new MockZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", pkey, "0");
    DataStore store = new DataStore(clogStore, null);
    ECParameterSpec spec = Mockito.mock(ECParameterSpec.class);
    Mockito.when(spec.getCurve()).thenReturn(null);
    Mockito.when(spec.getG()).thenReturn(null);
    Mockito.when(spec.getH()).thenReturn(new BigInteger("100"));
    Mockito.when(spec.getN()).thenReturn(new BigInteger("100"));
    assertNull(store.getCurveName(spec));
}

From source file:common.crypto.bouncycastle.CEcdsaSignerBC.java

License:Open Source License

@Override
public void initialize(String szCurveName, CryptoTypes.ESHAMode eShaMode) {
    ECParameterSpec paramSpec = ECNamedCurveTable.getParameterSpec(szCurveName);
    m_eccParameter = new ECDomainParameters(paramSpec.getCurve(), paramSpec.getG(), paramSpec.getN());
    m_signer = new ECDSASigner();
    m_shaHash = new CCryptoSHABC();
    m_shaHash.initialize(eShaMode);//from  w w w  .  j a  va 2s .  co m
}

From source file:de.tsenger.animamea.pace.PaceECDH.java

License:Open Source License

public PaceECDH(ECParameterSpec ecParameterSpec) {

    pointG = ecParameterSpec.getG();
    curve = (org.bouncycastle.math.ec.ECCurve.Fp) ecParameterSpec.getCurve();
    Random rnd = new Random();
    randomGenerator.setSeed(rnd.nextLong());

}

From source file:dorkbox.util.crypto.CryptoECC.java

License:Apache License

public static AsymmetricCipherKeyPair generateKeyPair(ECParameterSpec eccSpec, SecureRandom secureRandom) {
    ECKeyGenerationParameters ecParams = new ECKeyGenerationParameters(
            new ECDomainParameters(eccSpec.getCurve(), eccSpec.getG(), eccSpec.getN()), secureRandom);

    ECKeyPairGenerator ecKeyGen = new ECKeyPairGenerator();
    ecKeyGen.init(ecParams);/*w w  w  . j a v  a  2  s  .  co  m*/

    return ecKeyGen.generateKeyPair();
}

From source file:org.jmrtd.app.DocumentEditFrame.java

License:Open Source License

private static PublicKey getECPublicKeyFromPrivateKey(PrivateKey privateKey) throws GeneralSecurityException {
    KeyFactory keyFactory = KeyFactory.getInstance("EC", BC_PROVIDER);
    org.bouncycastle.jce.provider.JCEECPrivateKey priv = (org.bouncycastle.jce.provider.JCEECPrivateKey) privateKey;
    org.bouncycastle.jce.spec.ECParameterSpec params = priv.getParameters();
    org.bouncycastle.jce.spec.ECPublicKeySpec pubKS = new org.bouncycastle.jce.spec.ECPublicKeySpec(
            params.getG().multiply(priv.getD()), params);
    PublicKey publicKey = keyFactory.generatePublic(pubKS);
    return publicKey;
}