org.bouncycastle.jce.spec.ECPublicKeySpec Java Examples

The following examples show how to use org.bouncycastle.jce.spec.ECPublicKeySpec. 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: KeycardTest.java    From status-keycard with Apache License 2.0 6 votes vote down vote up
private void verifySignResp(byte[] data, APDUResponse response) throws Exception {
  Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
  assertEquals(0x9000, response.getSw());
  byte[] sig = response.getData();
  byte[] keyData = extractPublicKeyFromSignature(sig);
  sig = extractSignature(sig);

  ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256k1");
  ECPublicKeySpec cardKeySpec = new ECPublicKeySpec(ecSpec.getCurve().decodePoint(keyData), ecSpec);
  ECPublicKey cardKey = (ECPublicKey) KeyFactory.getInstance("ECDSA", "BC").generatePublic(cardKeySpec);

  signature.initVerify(cardKey);
  assertEquals((SecureChannel.SC_KEY_LENGTH * 2 / 8) + 1, keyData.length);
  signature.update(data);
  assertTrue(signature.verify(sig));
  assertFalse(isMalleable(sig));
}
 
Example #2
Source File: BouncyCryptography.java    From Jabit with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isSignatureValid(byte[] data, byte[] signature, Pubkey pubkey) {
    try {
        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);
    }
}
 
Example #3
Source File: SHA256withECDSASignatureVerification.java    From oxAuth with MIT License 6 votes vote down vote up
@Override
 public PublicKey decodePublicKey(byte[] encodedPublicKey) throws SignatureException {
         X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
         ECPoint point = curve.getCurve().decodePoint(encodedPublicKey);

         try {
	return KeyFactory.getInstance("ECDSA").generatePublic(
	        new ECPublicKeySpec(point,
	                new ECParameterSpec(
	                        curve.getCurve(),
	                        curve.getG(),
	                        curve.getN(),
	                        curve.getH()
	                )
	        )
	);
} catch (GeneralSecurityException ex) {
	throw new SignatureException(ex);
}
 }
 
Example #4
Source File: ECKey.java    From javasdk with GNU Lesser General Public License v3.0 5 votes vote down vote up
public PublicKey GetPublickey() {
    PublicKey pub = null;
    try {
        pub = ECKeyFactory.getInstance(SpongyCastleProvider.getInstance()).generatePublic(new ECPublicKeySpec(this.pub, CURVE_SPEC));
    } catch (InvalidKeySpecException e) {
        logger.error(e);
    }
    return pub;
}
 
Example #5
Source File: SM2CertUtil.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
public static BCECPublicKey getBCECPublicKey(X509Certificate sm2Cert) {
    ECPublicKey pubKey = (ECPublicKey) sm2Cert.getPublicKey();
    ECPoint q = pubKey.getQ();
    ECParameterSpec parameterSpec = new ECParameterSpec(SM2Util.CURVE, SM2Util.G_POINT,
        SM2Util.SM2_ECC_N, SM2Util.SM2_ECC_H);
    ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(q, parameterSpec);
    return new BCECPublicKey(pubKey.getAlgorithm(), pubKeySpec,
        BouncyCastleProvider.CONFIGURATION);
}
 
Example #6
Source File: Sign.java    From blockchain with Apache License 2.0 5 votes vote down vote up
/**
 * 通过秘钥值(BigInteger)生成 PrivateKey 对象
 * @param privateKeyValue 秘钥值
 * @return
 */
public static PublicKey publicKeyFromPrivate(BigInteger privateKeyValue) throws Exception {

	ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(CryptoConstants.EC_PARAM_SPEC);
	ECPoint point = publicPointFromPrivate(privateKeyValue);
	ECPublicKeySpec keySpec = new ECPublicKeySpec(point, ecSpec);
	Security.addProvider(new BouncyCastleProvider());
	KeyFactory keyFactory = KeyFactory.getInstance(CryptoConstants.KEY_GEN_ALGORITHM, BouncyCastleProvider.PROVIDER_NAME);
	return keyFactory.generatePublic(keySpec);
}
 
Example #7
Source File: Utils.java    From org.openhab.ui.habot with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Load the public key from a URL-safe base64 encoded string. Takes into
 * account the different encodings, including point compression.
 *
 * @param encodedPublicKey
 */
public static PublicKey loadPublicKey(String encodedPublicKey)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] decodedPublicKey = base64Decode(encodedPublicKey);
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);
    ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
    ECCurve curve = parameterSpec.getCurve();
    ECPoint point = curve.decodePoint(decodedPublicKey);
    ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, parameterSpec);

    return keyFactory.generatePublic(pubSpec);
}
 
Example #8
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 #9
Source File: KeycardTest.java    From status-keycard with Apache License 2.0 5 votes vote down vote up
private void verifyKeyDerivation(KeyPair keyPair, byte[] chainCode, int[] path) throws Exception {
  byte[] hash = sha256(new byte[8]);
  APDUResponse resp = cmdSet.sign(hash);
  assertEquals(0x9000, resp.getSw());
  byte[] sig = resp.getData();
  byte[] publicKey = extractPublicKeyFromSignature(sig);
  sig = extractSignature(sig);

  if (cmdSet.getApplicationInfo().hasKeyManagementCapability()) {
    DeterministicKey key = deriveKey(keyPair, chainCode, path);

    assertTrue(key.verify(hash, sig));
    assertArrayEquals(key.getPubKeyPoint().getEncoded(false), publicKey);
  } else {
    Signature signature = Signature.getInstance("SHA256withECDSA", "BC");

    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256k1");
    ECPublicKeySpec cardKeySpec = new ECPublicKeySpec(ecSpec.getCurve().decodePoint(publicKey), ecSpec);
    ECPublicKey cardKey = (ECPublicKey) KeyFactory.getInstance("ECDSA", "BC").generatePublic(cardKeySpec);

    signature.initVerify(cardKey);
    signature.update(new byte[8]);
    assertTrue(signature.verify(sig));
  }

  resp = cmdSet.getStatus(KeycardApplet.GET_STATUS_P1_KEY_PATH);
  assertEquals(0x9000, resp.getSw());
  byte[] rawPath = resp.getData();

  assertEquals(path.length * 4, rawPath.length);

  for (int i = 0; i < path.length; i++) {
    int k = path[i];
    int k1 = (rawPath[i * 4] << 24) | (rawPath[(i * 4) + 1] << 16) | (rawPath[(i * 4) + 2] << 8) | rawPath[(i * 4) + 3];
    assertEquals(k, k1);
  }
}
 
Example #10
Source File: ECDHExportTest.java    From Encryptor4j with MIT License 5 votes vote down vote up
/**
 * Loads and returns the elliptic-curve public key from the data byte array.
 * @param data
 * @return
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 * @throws InvalidKeySpecException
 */
public static ECPublicKey loadPublicKey(byte[] data) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException
{
	X9ECParameters params = CustomNamedCurves.getByName("curve25519");
	ECParameterSpec ecParameterSpec = new ECParameterSpec(params.getCurve(), params.getG(), params.getN(), params.getH(), params.getSeed());

	ECPublicKeySpec publicKey = new ECPublicKeySpec(ecParameterSpec.getCurve().decodePoint(data), ecParameterSpec);
	KeyFactory kf = KeyFactory.getInstance("ECDH", "BC");
	return (ECPublicKey) kf.generatePublic(publicKey);
}
 
Example #11
Source File: Utils.java    From webpush-java with MIT License 5 votes vote down vote up
/**
 * Load the public key from a byte array. 
 *
 * @param decodedPublicKey
 */
public static PublicKey loadPublicKey(byte[] decodedPublicKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);
    ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
    ECCurve curve = parameterSpec.getCurve();
    ECPoint point = curve.decodePoint(decodedPublicKey);
    ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, parameterSpec);

    return keyFactory.generatePublic(pubSpec);
}
 
Example #12
Source File: Utils.java    From webpush-java with MIT License 5 votes vote down vote up
/**
 * Load a public key from the private key.
 *
 * @param privateKey
 * @return
 */
public static ECPublicKey loadPublicKey(ECPrivateKey privateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(CURVE);
    ECPoint Q = ecSpec.getG().multiply(privateKey.getD());
    byte[] publicDerBytes = Q.getEncoded(false);
    ECPoint point = ecSpec.getCurve().decodePoint(publicDerBytes);
    ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, ecSpec);

    return (ECPublicKey) keyFactory.generatePublic(pubSpec);
}
 
Example #13
Source File: KeyUtils.java    From aerogear-unifiedpush-server with Apache License 2.0 5 votes vote down vote up
public static PublicKey loadPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] decodedPublicKey = Base64Encoder.decode(publicKey);
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER);
    ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
    ECCurve curve = parameterSpec.getCurve();
    ECPoint point = curve.decodePoint(decodedPublicKey);
    ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, parameterSpec);

    return keyFactory.generatePublic(pubSpec);

}
 
Example #14
Source File: KeyUtils.java    From aerogear-unifiedpush-server with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the base64 encoded public key as a PublicKey object
 */
public static PublicKey getUserPublicKey(WebPushRegistration registration) throws NoSuchAlgorithmException, InvalidKeySpecException {

    KeyFactory kf = KeyFactory.getInstance("ECDH", PROVIDER);
    ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1");
    ECPoint point = ecSpec.getCurve().decodePoint(registration.getKeyAsBytes());
    ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, ecSpec);

    return kf.generatePublic(pubSpec);
}
 
Example #15
Source File: ECCCryptor.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
@Override
protected Class<? extends KeySpec> getPublicKeySpecClass() {
	return ECPublicKeySpec.class;
}