Example usage for org.bouncycastle.jce.spec ECPublicKeySpec ECPublicKeySpec

List of usage examples for org.bouncycastle.jce.spec ECPublicKeySpec ECPublicKeySpec

Introduction

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

Prototype

public ECPublicKeySpec(ECPoint q, ECParameterSpec spec) 

Source Link

Document

base constructor

Usage

From source file:ch.dissem.bitmessage.cryptography.bc.BouncyCryptography.java

License:Apache License

@Override
public boolean isSignatureValid(byte[] data, byte[] signature, Pubkey pubkey) {
    try {//  w  w  w. j  a  v  a  2s.  c  o  m
        ECParameterSpec spec = new ECParameterSpec(EC_CURVE_PARAMETERS.getCurve(), EC_CURVE_PARAMETERS.getG(),
                EC_CURVE_PARAMETERS.getN(), EC_CURVE_PARAMETERS.getH(), EC_CURVE_PARAMETERS.getSeed());

        ECPoint Q = keyToPoint(pubkey.getSigningKey());
        KeySpec keySpec = new ECPublicKeySpec(Q, spec);
        PublicKey publicKey = KeyFactory.getInstance(ALGORITHM_ECDSA, provider).generatePublic(keySpec);

        Signature sig = Signature.getInstance(ALGORITHM_ECDSA, provider);
        sig.initVerify(publicKey);
        sig.update(data);
        return sig.verify(signature);
    } catch (GeneralSecurityException e) {
        throw new ApplicationException(e);
    }
}

From source file:com.aelitis.azureus.core.security.CryptoECCUtils.java

License:Open Source License

public static PublicKey rawdataToPubkey(byte[] input)

        throws CryptoManagerException {
    ECPoint W = ECCparam.getCurve().decodePoint(input);

    KeySpec keyspec = new ECPublicKeySpec(W, (ECParameterSpec) ECCparam);

    try {// ww  w  .j  ava  2s.  c o m

        return KeyFactory.getInstance("ECDSA", "BC").generatePublic(keyspec);

    } catch (Throwable e) {

        throw (new CryptoManagerException("Failed to decode public key", e));
    }
}

From source file:com.cryptolib.CryptoObject.java

License:Open Source License

/**
* Open commitment and extract message to create shared secret.
*///from  w ww .j a v  a2s  .c o m
public void openCommitmentAndCreateSharedSecret(byte[] decommitment)
        throws CryptoSocketException, InvalidKeyException, NoSuchAlgorithmException {
    this.cc.open(decommitment);

    try {
        BCECPublicKey mypk = (BCECPublicKey) (this.encKeypair.getPublic());
        int publicKeySize = mypk.getQ().getEncoded(true).length - 1;
        byte[] message = this.cc.getOtherMessage();

        if (message.length != publicKeySize + this.OOB.length) {
            throw new CryptoSocketException("Message size is wrong!");
        }

        byte[] otherPK = new byte[publicKeySize + 1];

        //compressed encoding magic byte
        otherPK[0] = (byte) 0x02;
        byte[] otherOOB = new byte[this.OOB.length];
        System.arraycopy(message, 0, otherPK, 1, publicKeySize);
        System.arraycopy(message, publicKeySize, otherOOB, 0, otherOOB.length);
        X9ECParameters ecP = CustomNamedCurves.getByName(curve);
        org.bouncycastle.jce.spec.ECParameterSpec ecGenSpec = new org.bouncycastle.jce.spec.ECParameterSpec(
                ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH());
        //ECNamedCurveParameterSpec ecP = ECNamedCurveTable.getParameterSpec(this.curve);
        ECPublicKeySpec pubKey = new ECPublicKeySpec(ecP.getCurve().decodePoint(otherPK), ecGenSpec);
        KeyFactory kf = KeyFactory.getInstance(this.enc_algorithm, new BouncyCastleProvider());
        ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKey);
        createSharedEncKey(pk);
        mergeOOB(otherOOB);
    } catch (NoSuchAlgorithmException nsa) {
        throw new CryptoSocketException("Algorithm is not supported!");
    } catch (InvalidKeySpecException iks) {
        throw new CryptoSocketException("Wrong parameter for algorithm!");
    }
}

From source file:com.google.u2f.server.impl.BouncyCastleCrypto.java

License:Open Source License

@Override
public PublicKey decodePublicKey(byte[] encodedPublicKey) throws U2FException {
    try {/*from ww w  .j  a  va 2  s.c  o  m*/
        X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
        ECPoint point;
        try {
            point = curve.getCurve().decodePoint(encodedPublicKey);
        } catch (RuntimeException e) {
            throw new U2FException("Couldn't parse user public key", e);
        }

        return KeyFactory.getInstance("ECDSA").generatePublic(new ECPublicKeySpec(point,
                new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(), curve.getH())));
    } catch (InvalidKeySpecException e) {
        throw new U2FException("Error when decoding public key", e);
    } catch (NoSuchAlgorithmException e) {
        throw new U2FException("Error when decoding public key", e);
    }
}

From source file:com.google.u2f.TestUtils.java

License:Open Source License

public static PublicKey parsePublicKey(byte[] keyBytes) {
    try {//from ww  w  . j  ava  2 s.  c  o m
        X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
        ECParameterSpec curveSpec = new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(),
                curve.getH());
        ECPoint point = curve.getCurve().decodePoint(keyBytes);
        return KeyFactory.getInstance("ECDSA").generatePublic(new ECPublicKeySpec(point, curveSpec));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.jin.u2f.crypto.BouncyCastleCrypto.java

License:Open Source License

public PublicKey decodePublicKey(byte[] encodedPublicKey) throws U2fBadInputException {
    try {//from w  w w. j  a  va  2s  . c  om
        X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
        ECPoint point;
        try {
            point = curve.getCurve().decodePoint(encodedPublicKey);
        } catch (RuntimeException e) {
            throw new U2fBadInputException("Could not parse user public key", e);
        }

        return KeyFactory.getInstance("ECDSA").generatePublic(new ECPublicKeySpec(point,
                new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(), curve.getH())));
    } catch (GeneralSecurityException e) { // This should not happen
        throw new RuntimeException(e);
    }
}

From source file:com.sydefolk.crypto.zrtp.EC25SecretCalculator.java

License:Open Source License

@Override
public byte[] calculateKeyAgreement(KeyPair localKey, byte[] publicKeyBytes) {
    //    Log.w("EC25SecretCalculator", "Calculating EC25 Secret...");
    try {/* www. j  a  v  a 2  s.  c o  m*/
        byte[] x = new byte[32];
        byte[] y = new byte[32];

        System.arraycopy(publicKeyBytes, 0, x, 0, x.length);
        System.arraycopy(publicKeyBytes, x.length, y, 0, y.length);

        ECParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256r1");
        ECPoint point = params.getCurve().createPoint(Conversions.byteArrayToBigInteger(x),
                Conversions.byteArrayToBigInteger(y), false);

        ECPublicKeySpec keySpec = new ECPublicKeySpec(point, params);
        KeyFactory keyFactory = KeyFactory.getInstance("ECDH", "BC");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);

        KeyAgreement agreement = KeyAgreement.getInstance("ECDH", "BC");
        agreement.init(localKey.getPrivate());
        agreement.doPhase(publicKey, true);

        return agreement.generateSecret();
    } catch (NoSuchProviderException nspe) {
        throw new AssertionError(nspe);
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    } catch (InvalidKeyException e) {
        throw new IllegalArgumentException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalArgumentException(e);
    }
}

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

License:Apache License

public static PublicKey loadPublicKey(Reader r) throws CryptoException {
    try (org.bouncycastle.openssl.PEMParser pemReader = new org.bouncycastle.openssl.PEMParser(r)) {
        PublicKey pubKey = null;/*from w  w  w .  j  a  v a 2s . com*/
        Object pemObj = pemReader.readObject();
        JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter();
        SubjectPublicKeyInfo keyInfo = null;
        X9ECParameters ecParam = null;

        if (pemObj instanceof ASN1ObjectIdentifier) {

            // make sure this is EC Parameter we're handling. In which case
            // we'll store it and read the next object which should be our
            // EC Public Key

            ASN1ObjectIdentifier ecOID = (ASN1ObjectIdentifier) pemObj;
            ecParam = ECNamedCurveTable.getByOID(ecOID);
            if (ecParam == null) {
                throw new PEMException("Unable to find EC Parameter for the given curve oid: "
                        + ((ASN1ObjectIdentifier) pemObj).getId());
            }

            pemObj = pemReader.readObject();
        } else if (pemObj instanceof X9ECParameters) {
            ecParam = (X9ECParameters) pemObj;
            pemObj = pemReader.readObject();
        }

        if (pemObj instanceof org.bouncycastle.cert.X509CertificateHolder) {
            keyInfo = ((org.bouncycastle.cert.X509CertificateHolder) pemObj).getSubjectPublicKeyInfo();
        } else {
            keyInfo = (SubjectPublicKeyInfo) pemObj;
        }
        pubKey = pemConverter.getPublicKey(keyInfo);

        if (ecParam != null && ECDSA.equals(pubKey.getAlgorithm())) {
            ECParameterSpec ecSpec = new ECParameterSpec(ecParam.getCurve(), ecParam.getG(), ecParam.getN(),
                    ecParam.getH(), ecParam.getSeed());
            KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BC_PROVIDER);
            ECPublicKeySpec keySpec = new ECPublicKeySpec(((BCECPublicKey) pubKey).getQ(), ecSpec);
            pubKey = (PublicKey) keyFactory.generatePublic(keySpec);
        }
        return pubKey;
    } catch (PEMException e) {
        throw new CryptoException(e);
    } catch (NoSuchProviderException e) {
        LOG.error(
                "loadPublicKey: Caught NoSuchProviderException, check to make sure the provider is loaded correctly.");
        throw new CryptoException(e);
    } catch (NoSuchAlgorithmException e) {
        LOG.error(
                "loadPublicKey: Caught NoSuchAlgorithmException, check to make sure the algorithm is supported by the provider.");
        throw new CryptoException(e);
    } catch (InvalidKeySpecException e) {
        LOG.error("loadPublicKey: Caught InvalidKeySpecException, invalid key spec is being used.");
        throw new CryptoException("InvalidKeySpecException");
    } catch (IOException e) {
        throw new CryptoException(e);
    }
}

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;//from   w  w  w.  ja v a  2  s  .  c o  m
    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.yubico.u2f.crypto.BouncyCastleCrypto.java

License:Open Source License

@Override
public PublicKey decodePublicKey(byte[] encodedPublicKey) throws U2fBadInputException {
    try {//from www  . j  av a  2  s  .c  o  m
        X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
        ECPoint point;
        try {
            point = curve.getCurve().decodePoint(encodedPublicKey);
        } catch (RuntimeException e) {
            throw new U2fBadInputException("Could not parse user public key", e);
        }

        return KeyFactory.getInstance("ECDSA").generatePublic(new ECPublicKeySpec(point,
                new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(), curve.getH())));
    } catch (GeneralSecurityException e) { //This should not happen
        throw new RuntimeException(e);
    }
}