org.bouncycastle.jce.spec.ECNamedCurveSpec Java Examples

The following examples show how to use org.bouncycastle.jce.spec.ECNamedCurveSpec. 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: SoftKeymasterBlob.java    From keystore-decryptor with Apache License 2.0 7 votes vote down vote up
private static ECPrivateKey toJcaPrivateKey(org.bouncycastle.asn1.sec.ECPrivateKey ecPrivateKey)
        throws GeneralSecurityException {
    String curveName = null;
    ASN1ObjectIdentifier curveId = (ASN1ObjectIdentifier) ecPrivateKey.getParameters();
    if (curveId.equals(secp224r1_OID)) {
        curveName = "secp224r1";
    } else if (curveId.equals(prime256v1_OID)) {
        curveName = "prime256v1";
    } else if (curveId.equals(secp384r1_OID)) {
        curveName = "secp384r1";
    } else if (curveId.equals(secp521r1_OID)) {
        curveName = "secp521r1";
    } else {
        throw new IllegalStateException("Unknown curve OID: " + curveId);
    }

    ECNamedCurveParameterSpec sp = ECNamedCurveTable.getParameterSpec(curveName);
    ECParameterSpec params = new ECNamedCurveSpec(sp.getName(), sp.getCurve(), sp.getG(),
            sp.getN(), sp.getH());

    ECPrivateKeySpec pkSpec = new ECPrivateKeySpec(ecPrivateKey.getKey(), params);
    KeyFactory kf = KeyFactory.getInstance("EC");
    ECPrivateKey privateKey = (ECPrivateKey) kf.generatePrivate(pkSpec);

    return privateKey;
}
 
Example #2
Source File: EccUtil.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Determines the name of the domain parameters that were used for generating the key.
 *
 * @param key An EC key
 * @return The name of the domain parameters that were used for the EC key,
 *         or an empty string if curve is unknown.
 */
public static String getNamedCurve(Key key) {

	if (!(key instanceof ECKey)) {
		throw new InvalidParameterException("Not a EC private key.");
	}

	ECKey ecKey = (ECKey) key;
	ECParameterSpec params = ecKey.getParams();
	if (!(params instanceof ECNamedCurveSpec)) {
		return "";
	}

	ECNamedCurveSpec ecPrivateKeySpec = (ECNamedCurveSpec) params;
	String namedCurve = ecPrivateKeySpec.getName();
	return namedCurve;
}
 
Example #3
Source File: KeyCodec.java    From UAF with Apache License 2.0 6 votes vote down vote up
/**
 * Decode based on X, Y 32 byte integers
 * 
 * @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;
}
 
Example #4
Source File: Account.java    From ontology-java-sdk with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Account(byte[] prikey, SignatureScheme scheme) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    signatureScheme = scheme;

    if (scheme == SignatureScheme.SM3WITHSM2) {
        this.keyType = KeyType.SM2;
        this.curveParams = new Object[]{Curve.SM2P256V1.toString()};
    } else if (scheme == SignatureScheme.SHA256WITHECDSA) {
        this.keyType = KeyType.ECDSA;
        this.curveParams = new Object[]{Curve.P256.toString()};
    }

    switch (scheme) {
        case SHA256WITHECDSA:
        case SM3WITHSM2:
            BigInteger d = new BigInteger(1, prikey);
            ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) this.curveParams[0]);
            ECParameterSpec paramSpec = new ECNamedCurveSpec(spec.getName(), spec.getCurve(), spec.getG(), spec.getN());
            ECPrivateKeySpec priSpec = new ECPrivateKeySpec(d, paramSpec);
            KeyFactory kf = KeyFactory.getInstance("EC", "BC");
            this.privateKey = kf.generatePrivate(priSpec);

            org.bouncycastle.math.ec.ECPoint Q = spec.getG().multiply(d).normalize();
            if (Q == null || Q.getAffineXCoord() == null || Q.getAffineYCoord() == null) {
                throw new SDKException(ErrorCode.OtherError("normalize error"));
            }
            ECPublicKeySpec pubSpec = new ECPublicKeySpec(
                    new ECPoint(Q.getAffineXCoord().toBigInteger(), Q.getAffineYCoord().toBigInteger()),
                    paramSpec);
            this.publicKey = kf.generatePublic(pubSpec);
            this.addressU160 = Address.addressFromPubKey(serializePublicKey());
            break;
        default:
            throw new Exception(ErrorCode.UnsupportedKeyType);
    }
}
 
Example #5
Source File: Crypto.java    From webauthndemo with Apache License 2.0 5 votes vote down vote up
public static PublicKey getECPublicKey(java.security.spec.ECPoint w, String stdCurveName)
    throws NoSuchAlgorithmException, InvalidKeySpecException {
  ECNamedCurveParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(stdCurveName);
  java.security.spec.ECParameterSpec params = new ECNamedCurveSpec(parameterSpec.getName(),
      parameterSpec.getCurve(), parameterSpec.getG(), parameterSpec.getN(), parameterSpec.getH(),
      parameterSpec.getSeed());
  KeySpec keySpec = new java.security.spec.ECPublicKeySpec(w, params);
  KeyFactory keyFactory = KeyFactory.getInstance("EC");
  return keyFactory.generatePublic(keySpec);
}
 
Example #6
Source File: KeyCodec.java    From UAF with Apache License 2.0 5 votes vote down vote up
/**
 * Decode based on d - 32 byte integer
 * 
 * @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);
}
 
Example #7
Source File: Main.java    From keystore-decryptor with Apache License 2.0 5 votes vote down vote up
private static void showJcaPrivateKey(PrivateKey pk) throws Exception {
    if (pk instanceof RSAPrivateKey) {
        RSAPrivateKey rsaPrivKey = (RSAPrivateKey) pk;
        PemObject rsaPem = new PemObject("RSA PRIVATE KEY", rsaPrivKey.getEncoded());
        StringWriter sw = new StringWriter();
        PemWriter pemWriter = new PemWriter(sw);
        try {
            pemWriter.writeObject(rsaPem);
        } finally {
            pemWriter.close();
        }
        System.out.println(sw.toString());
    } else if (pk instanceof java.security.interfaces.ECPrivateKey) {
        java.security.interfaces.ECPrivateKey ecPrivKey = (java.security.interfaces.ECPrivateKey) pk;
        System.out.printf("EC S: %s... (%d)\n",
                ecPrivKey.getS().toString(16).substring(0, 32),
                ecPrivKey.getS().bitLength());
        if (ecPrivKey.getParams() instanceof ECNamedCurveSpec) {
            ECNamedCurveSpec namedCurveSpec = (ECNamedCurveSpec) ecPrivKey.getParams();
            System.out.println("curve name: " + namedCurveSpec.getName());
        } else {
            System.out.println("EC params: " + ecPrivKey.getParams());
        }
    } else if (pk instanceof DSAPrivateKey) {
        DSAPrivateKey dsaPrivKey = (DSAPrivateKey) pk;
        System.out.printf("DSA X: %s... (%d)\n",
                dsaPrivKey.getX().toString(16).substring(0, 32), dsaPrivKey.getX()
                        .bitLength());
        System.out.println("DSA params: " + dsaPrivKey.getParams());
    } else {
        System.out.println("Unknown private key type: " + pk.getClass().getName());
    }
}
 
Example #8
Source File: JWKParser.java    From keycloak with Apache License 2.0 5 votes vote down vote up
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;
    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);
    }
}
 
Example #9
Source File: Account.java    From ontology-java-sdk with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void parsePublicKey(byte[] data) throws Exception {
        if (data == null) {
            throw new Exception(ErrorCode.NullInput);
        }
        if (data.length < 2) {
            throw new Exception(ErrorCode.InvalidData);
        }
        if(data.length == 33){
            this.keyType = KeyType.ECDSA;
        } else if(data.length == 35) {
            this.keyType = KeyType.fromLabel(data[0]);
        }
        this.privateKey = null;
        this.publicKey = null;
        switch (this.keyType) {
            case ECDSA:
			    this.keyType = KeyType.ECDSA;
                this.curveParams = new Object[]{Curve.P256.toString()};
                ECNamedCurveParameterSpec spec0 = ECNamedCurveTable.getParameterSpec(Curve.P256.toString());
                ECParameterSpec param0 = new ECNamedCurveSpec(spec0.getName(), spec0.getCurve(), spec0.getG(), spec0.getN());
                ECPublicKeySpec pubSpec0 = new ECPublicKeySpec(
                        ECPointUtil.decodePoint(
                                param0.getCurve(),
                                Arrays.copyOfRange(data, 0, data.length)),
                        param0);
                KeyFactory kf0 = KeyFactory.getInstance("EC", "BC");
                this.publicKey = kf0.generatePublic(pubSpec0);
                break;
            case SM2:
//                this.keyType = KeyType.fromLabel(data[0]);
                Curve c = Curve.fromLabel(data[1]);
                this.curveParams = new Object[]{c.toString()};
                ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(c.toString());
                ECParameterSpec param = new ECNamedCurveSpec(spec.getName(), spec.getCurve(), spec.getG(), spec.getN());
                ECPublicKeySpec pubSpec = new ECPublicKeySpec(
                        ECPointUtil.decodePoint(
                                param.getCurve(),
                                Arrays.copyOfRange(data, 2, data.length)),
                        param);
                KeyFactory kf = KeyFactory.getInstance("EC", "BC");
                this.publicKey = kf.generatePublic(pubSpec);
                break;
            default:
                throw new Exception(ErrorCode.UnknownKeyType);
        }
    }