Search in sources :

Example 11 with ECPrivateKeySpec

use of java.security.spec.ECPrivateKeySpec in project web3sdk by FISCO-BCOS.

the class ECCDecrypt method createBCECPrivateKey.

/**
 * create BCECPrivateKey from privateKey
 *
 * @param privateKey
 * @return
 */
private BCECPrivateKey createBCECPrivateKey(BigInteger privateKey) {
    // Handle secret key
    ECPrivateKeySpec secretKeySpec = new ECPrivateKeySpec(privateKey, ECCParams.ecNamedCurveSpec);
    BCECPrivateKey bcecPrivateKey = new BCECPrivateKey("ECDSA", secretKeySpec, BouncyCastleProvider.CONFIGURATION);
    return bcecPrivateKey;
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) BCECPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)

Example 12 with ECPrivateKeySpec

use of java.security.spec.ECPrivateKeySpec in project i2p.i2p by i2p.

the class SigUtil method cvtToJavaECKey.

private static ECPrivateKey cvtToJavaECKey(SigningPrivateKey pk) throws GeneralSecurityException {
    SigType type = pk.getType();
    byte[] b = pk.getData();
    BigInteger s = new NativeBigInteger(1, b);
    // see ECConstants re: casting
    ECPrivateKeySpec ks = new ECPrivateKeySpec(s, (ECParameterSpec) type.getParams());
    KeyFactory kf = KeyFactory.getInstance("EC");
    return (ECPrivateKey) kf.generatePrivate(ks);
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) NativeBigInteger(net.i2p.util.NativeBigInteger) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) KeyFactory(java.security.KeyFactory)

Example 13 with ECPrivateKeySpec

use of java.security.spec.ECPrivateKeySpec in project jruby-openssl by jruby.

the class PKeyEC method set_private_key.

@JRubyMethod(name = "private_key=")
public IRubyObject set_private_key(final ThreadContext context, final IRubyObject arg) {
    final BigInteger s;
    if (arg instanceof BN) {
        s = ((BN) (arg)).getValue();
    } else {
        s = (BigInteger) arg;
    }
    ECPrivateKeySpec keySpec = new ECPrivateKeySpec(s, getParamSpec());
    try {
        this.privateKey = SecurityHelper.getKeyFactory("ECDSA").generatePrivate(keySpec);
        return arg;
    } catch (GeneralSecurityException ex) {
        throw newECError(context.runtime, ex.getMessage());
    }
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 14 with ECPrivateKeySpec

use of java.security.spec.ECPrivateKeySpec in project wycheproof by google.

the class EcdhTest method testLargePrivateKey.

/**
 * Tests for the problem detected by CVE-2017-10176.
 *
 * <p>Some libraries do not compute P + (-P) correctly and return 2 * P or throw exceptions. When
 * the library uses addition-subtraction chains for the point multiplication then such cases can
 * occur for example when the private key is close to the order of the curve.
 */
private void testLargePrivateKey(ECParameterSpec spec) throws Exception {
    BigInteger order = spec.getOrder();
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
    ECPublicKey pub;
    try {
        keyGen.initialize(spec);
        pub = (ECPublicKey) keyGen.generateKeyPair().getPublic();
    } catch (GeneralSecurityException ex) {
        // curve is not supported
        return;
    }
    KeyFactory kf = KeyFactory.getInstance("EC");
    KeyAgreement ka = KeyAgreement.getInstance("ECDH");
    for (int i = 1; i <= 64; i++) {
        BigInteger p1 = BigInteger.valueOf(i);
        ECPrivateKeySpec spec1 = new ECPrivateKeySpec(p1, spec);
        ECPrivateKeySpec spec2 = new ECPrivateKeySpec(order.subtract(p1), spec);
        ka.init(kf.generatePrivate(spec1));
        ka.doPhase(pub, true);
        byte[] shared1 = ka.generateSecret();
        ka.init(kf.generatePrivate(spec2));
        ka.doPhase(pub, true);
        byte[] shared2 = ka.generateSecret();
        // The private keys p1 and p2 are equivalent, since only the x-coordinate of the
        // shared point is used to generate the shared secret.
        assertEquals(TestUtil.bytesToHex(shared1), TestUtil.bytesToHex(shared2));
    }
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) ECPublicKey(java.security.interfaces.ECPublicKey) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) KeyPairGenerator(java.security.KeyPairGenerator) KeyAgreement(javax.crypto.KeyAgreement) KeyFactory(java.security.KeyFactory) ECPoint(java.security.spec.ECPoint)

Example 15 with ECPrivateKeySpec

use of java.security.spec.ECPrivateKeySpec in project thingsboard by thingsboard.

the class LwM2mRPkCredentials method generatePublicKeyRPK.

private void generatePublicKeyRPK(String publX, String publY, String privS) {
    try {
        /*Get Elliptic Curve Parameter spec for secp256r1 */
        AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
        algoParameters.init(new ECGenParameterSpec("secp256r1"));
        ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);
        if (publX != null && !publX.isEmpty() && publY != null && !publY.isEmpty()) {
            // Get point values
            byte[] publicX = Hex.decodeHex(publX.toCharArray());
            byte[] publicY = Hex.decodeHex(publY.toCharArray());
            /* Create key specs */
            KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)), parameterSpec);
            /* Get keys */
            this.serverPublicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec);
        }
        if (privS != null && !privS.isEmpty()) {
            /* Get point values */
            byte[] privateS = Hex.decodeHex(privS.toCharArray());
            /* Create key specs */
            KeySpec privateKeySpec = new ECPrivateKeySpec(new BigInteger(privateS), parameterSpec);
            /* Get keys */
            this.serverPrivateKey = KeyFactory.getInstance("EC").generatePrivate(privateKeySpec);
        }
    } catch (GeneralSecurityException | IllegalArgumentException e) {
        log.error("[{}] Failed generate Server KeyRPK", e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) KeySpec(java.security.spec.KeySpec) GeneralSecurityException(java.security.GeneralSecurityException) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

ECPrivateKeySpec (java.security.spec.ECPrivateKeySpec)15 BigInteger (java.math.BigInteger)10 ECParameterSpec (java.security.spec.ECParameterSpec)7 ECPoint (java.security.spec.ECPoint)7 KeyFactory (java.security.KeyFactory)6 ECPrivateKey (java.security.interfaces.ECPrivateKey)5 PrivateKey (java.security.PrivateKey)4 PublicKey (java.security.PublicKey)4 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)4 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 GeneralSecurityException (java.security.GeneralSecurityException)3 ECPublicKey (java.security.interfaces.ECPublicKey)3 KeyAgreement (javax.crypto.KeyAgreement)3 AlgorithmParameters (java.security.AlgorithmParameters)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ECFieldF2m (java.security.spec.ECFieldF2m)2 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)2 EllipticCurve (java.security.spec.EllipticCurve)2 KeySpec (java.security.spec.KeySpec)2