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

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

Introduction

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

Prototype

public ECNamedCurveSpec(String name, EllipticCurve curve, ECPoint g, BigInteger n) 

Source Link

Usage

From source file:com.facebook.delegatedrecovery.DelegatedRecoveryConfiguration.java

License:Open Source License

/**
 * Turn the JSON public key array from a configuration into a set of usable
 * public keys for ECDSA on secp256r1/*  w  w  w. jav  a  2  s.  c  o  m*/
 * 
 * @param array The JSON public key array
 * @return array of public keys decoded from the JSON array of base64 encoded
 *         strings
 */
protected static ECPublicKey[] keysFromJsonArray(final JsonArray array) {
    try {
        final ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("prime256v1");
        final KeyFactory kf = KeyFactory.getInstance("EC", new BouncyCastleProvider());
        final ECNamedCurveSpec params = new ECNamedCurveSpec("prime256v1", spec.getCurve(), spec.getG(),
                spec.getN());
        final ArrayList<ECPublicKey> pubKeys = new ArrayList<ECPublicKey>(array.size());

        for (int i = 0; i < array.size(); i++) {
            final String b64 = array.getString(i);
            final byte[] pubKeyAsn1 = Base64.getDecoder().decode(b64);
            final byte[] pubKey = new byte[pubKeyAsn1.length - PEM_ASN1_PREFIX.length]; // trim
            // PEM
            // ASN.1
            // prefix
            System.arraycopy(pubKeyAsn1, PEM_ASN1_PREFIX.length, pubKey, 0, pubKey.length);
            final ECPoint point = ECPointUtil.decodePoint(params.getCurve(), pubKey);
            final ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params);
            try {
                final ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec);
                pubKeys.add(pk);
            } catch (InvalidKeySpecException e) {
                System.err.println("InvalidKeySpecException while processing " + b64);
            }
        }
        return pubKeys.toArray(new ECPublicKey[pubKeys.size()]);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        System.err.println("Unable to initialize ECDSA key factor for prime256v1.  Cannot continue.");
        System.exit(1);
        return null; // unreachable but Eclipse complier wants me to return
                     // something. :P
    }
}

From source file:com.hierynomus.sshj.userauth.keyprovider.OpenSSHKeyV1KeyFile.java

License:Apache License

private PrivateKey createECDSAPrivateKey(KeyType kt, PlainBuffer buffer, String name)
        throws GeneralSecurityException, Buffer.BufferException {
    kt.readPubKeyFromBuffer(buffer); // Public key
    BigInteger s = new BigInteger(1, buffer.readBytes());
    X9ECParameters ecParams = NISTNamedCurves.getByName(name);
    ECNamedCurveSpec ecCurveSpec = new ECNamedCurveSpec(name, ecParams.getCurve(), ecParams.getG(),
            ecParams.getN());//from w  w  w  . ja  v a  2s.  c  o  m
    ECPrivateKeySpec pks = new ECPrivateKeySpec(s, ecCurveSpec);
    return SecurityUtils.getKeyFactory("ECDSA").generatePrivate(pks);

}

From source file:net.schmizz.sshj.common.ECDSAVariationsAdapter.java

License:Apache License

static PublicKey readPubKeyFromBuffer(Buffer<?> buf, String variation) throws GeneralSecurityException {
    String algorithm = BASE_ALGORITHM_NAME + variation;
    if (!SecurityUtils.isBouncyCastleRegistered()) {
        throw new GeneralSecurityException("BouncyCastle is required to read a key of type " + algorithm);
    }/*from w  ww.j  a  v a 2 s.c  om*/
    try {
        // final String algo = buf.readString(); it has been already read
        final String curveName = buf.readString();
        final int keyLen = buf.readUInt32AsInt();
        final byte x04 = buf.readByte(); // it must be 0x04, but don't think
        // we need that check
        final byte[] x = new byte[(keyLen - 1) / 2];
        final byte[] y = new byte[(keyLen - 1) / 2];
        buf.readRawBytes(x);
        buf.readRawBytes(y);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Key algo: %s, Key curve: %s, Key Len: %s, 0x04: %s\nx: %s\ny: %s",
                    algorithm, curveName, keyLen, x04, Arrays.toString(x), Arrays.toString(y)));
        }

        if (!SUPPORTED_CURVES.values().contains(curveName)) {
            throw new GeneralSecurityException(String.format("Unknown curve %s", curveName));
        }

        BigInteger bigX = new BigInteger(1, x);
        BigInteger bigY = new BigInteger(1, y);

        String name = NIST_CURVES_NAMES.get(variation);
        X9ECParameters ecParams = NISTNamedCurves.getByName(name);
        ECNamedCurveSpec ecCurveSpec = new ECNamedCurveSpec(name, ecParams.getCurve(), ecParams.getG(),
                ecParams.getN());
        ECPoint p = new ECPoint(bigX, bigY);
        ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(p, ecCurveSpec);

        KeyFactory keyFactory = KeyFactory.getInstance("ECDSA");
        return keyFactory.generatePublic(publicKeySpec);
    } catch (Exception ex) {
        throw new GeneralSecurityException(ex);
    }
}

From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java

License:Apache License

/**
 * Decode based on X, Y 32 byte integers
 * //from w  w w  .  j a va  2 s . com
 * @param pubKey
 * @param curveName
 *            - Example secp256r1
 * @return
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
public static PublicKey getPubKeyFromCurve(byte[] pubKey, String curveName)
        throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {

    ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName);
    KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider());
    ECNamedCurveSpec params = new ECNamedCurveSpec(curveName, spec.getCurve(), spec.getG(), spec.getN());
    ECPoint point = ECPointUtil.decodePoint(params.getCurve(), pubKey);
    ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params);
    ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec);
    return pk;
}

From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java

License:Apache License

/**
 * Decode based on d - 32 byte integer//from  ww  w  .  j av  a2 s  .  c o m
 * 
 * @param privKey
 * @param curveName
 *            - Example secp256r1
 * @return
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
public static PrivateKey getPrivKeyFromCurve(byte[] privKey, String curveName)
        throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {

    ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName);
    KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider());
    ECNamedCurveSpec params = new ECNamedCurveSpec(curveName, spec.getCurve(), spec.getG(), spec.getN());
    ECPrivateKeySpec priKey = new ECPrivateKeySpec(new BigInteger(privKey), // d
            params);
    return kf.generatePrivate(priKey);
}

From source file:org.keycloak.jose.jwk.JWKParser.java

License:Apache License

private PublicKey createECPublicKey() {
    String crv = (String) jwk.getOtherClaims().get(ECPublicJWK.CRV);
    BigInteger x = new BigInteger(1, Base64Url.decode((String) jwk.getOtherClaims().get(ECPublicJWK.X)));
    BigInteger y = new BigInteger(1, Base64Url.decode((String) jwk.getOtherClaims().get(ECPublicJWK.Y)));

    String name;/*from   w w  w.ja v a2  s.  co m*/
    switch (crv) {
    case "P-256":
        name = "secp256r1";
        break;
    case "P-384":
        name = "secp384r1";
        break;
    case "P-521":
        name = "secp521r1";
        break;
    default:
        throw new RuntimeException("Unsupported curve");
    }

    try {
        ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(name);
        ECNamedCurveSpec params = new ECNamedCurveSpec("prime256v1", spec.getCurve(), spec.getG(), spec.getN());
        ECPoint point = new ECPoint(x, y);
        ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params);

        KeyFactory kf = KeyFactory.getInstance("ECDSA");
        return kf.generatePublic(pubKeySpec);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.springframework.security.jwt.crypto.sign.EllipticCurveKeyHelper.java

License:Apache License

static ECPublicKey createPublicKey(final BigInteger x, final BigInteger y, final String curve) {
    ECNamedCurveParameterSpec curveParameterSpec;
    if ((curveParameterSpec = ECNamedCurveTable.getParameterSpec(curve)) == null) {
        throw new IllegalArgumentException("Unsupported named curve: " + curve);
    }//from  w  w w .  j a v  a 2  s.co m

    ECParameterSpec parameterSpec = new ECNamedCurveSpec(curveParameterSpec.getName(),
            curveParameterSpec.getCurve(), curveParameterSpec.getG(), curveParameterSpec.getN());
    ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(x, y), parameterSpec);

    try {
        return (ECPublicKey) KeyFactory.getInstance("EC").generatePublic(publicKeySpec);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}