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;
}
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);
}
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());
}
}
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));
}
}
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);
}
}
Aggregations