use of java.security.spec.ECPoint in project tink by google.
the class TestUtil method generateEciesAeadHkdfPrivKey.
/**
* @return a freshly generated {@code EciesAeadHkdfPrivateKey} constructed with specified
* parameters.
*/
public static EciesAeadHkdfPrivateKey generateEciesAeadHkdfPrivKey(EllipticCurveType curve, HashType hashType, EcPointFormat pointFormat, KeyTemplate demKeyTemplate, byte[] salt) throws Exception {
ECParameterSpec ecParams;
switch(curve) {
case NIST_P256:
ecParams = EllipticCurves.getNistP256Params();
break;
case NIST_P384:
ecParams = EllipticCurves.getNistP384Params();
break;
case NIST_P521:
ecParams = EllipticCurves.getNistP521Params();
break;
default:
throw new NoSuchAlgorithmException("Curve not implemented:" + curve);
}
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(ecParams);
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privKey = (ECPrivateKey) keyPair.getPrivate();
ECPoint w = pubKey.getW();
EciesAeadHkdfPublicKey eciesPubKey = createEciesAeadHkdfPubKey(curve, hashType, pointFormat, demKeyTemplate, w.getAffineX().toByteArray(), w.getAffineY().toByteArray(), salt);
return createEciesAeadHkdfPrivKey(eciesPubKey, privKey.getS().toByteArray());
}
use of java.security.spec.ECPoint in project xipki by xipki.
the class P11SM3WithSM2SignatureSpi method engineInitSign.
@Override
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
if (!(privateKey instanceof P11PrivateKey)) {
throw new InvalidKeyException("privateKey is not instanceof " + P11PrivateKey.class.getName());
}
this.signingKey = (P11PrivateKey) privateKey;
if (!(signingKey.getPublicKey() instanceof ECPublicKey)) {
throw new InvalidKeyException("only EC key is allowed");
}
ECPublicKey pubKey = (ECPublicKey) signingKey.getPublicKey();
if (!GMUtil.isSm2primev2Curve(pubKey.getParams().getCurve())) {
throw new InvalidKeyException("only EC key of curve sm2primev2 is allowed");
}
String algo = privateKey.getAlgorithm();
if (!("EC".equals(algo) || "ECDSA".equals(algo))) {
throw new InvalidKeyException("privateKey is not an EC private key: " + algo);
}
byte[] userId = (paramSpec == null) ? "1234567812345678".getBytes() : paramSpec.getId();
if (signingKey.supportsMechanism(PKCS11Constants.CKM_VENDOR_SM2)) {
mechanism = PKCS11Constants.CKM_VENDOR_SM2;
outputStream = new DigestOutputStream(HashAlgo.SM3.createDigest());
p11Params = null;
ECPoint w = pubKey.getW();
sm2Z = GMUtil.getSM2Z(userId, GMObjectIdentifiers.sm2p256v1, w.getAffineX(), w.getAffineY());
try {
outputStream.write(sm2Z, 0, sm2Z.length);
} catch (IOException ex) {
throw new InvalidKeyException("could not compute Z of SM2");
}
} else if (signingKey.supportsMechanism(PKCS11Constants.CKM_VENDOR_SM2_SM3)) {
mechanism = PKCS11Constants.CKM_VENDOR_SM2_SM3;
outputStream = new ByteArrayOutputStream();
p11Params = new P11ByteArrayParams(userId);
} else {
throw new InvalidKeyException("privateKey and algorithm does not match");
}
this.signingKey = (P11PrivateKey) privateKey;
}
use of java.security.spec.ECPoint in project leshan by eclipse.
the class SecurityDeserializer method deserialize.
@Override
public SecurityInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
SecurityInfo info = null;
if (json.isJsonObject()) {
JsonObject object = (JsonObject) json;
String endpoint;
if (object.has("endpoint")) {
endpoint = object.get("endpoint").getAsString();
} else {
throw new JsonParseException("Missing endpoint");
}
JsonObject psk = (JsonObject) object.get("psk");
JsonObject rpk = (JsonObject) object.get("rpk");
JsonPrimitive x509 = object.getAsJsonPrimitive("x509");
if (psk != null) {
// PSK Deserialization
String identity;
if (psk.has("identity")) {
identity = psk.get("identity").getAsString();
} else {
throw new JsonParseException("Missing PSK identity");
}
byte[] key;
try {
key = Hex.decodeHex(psk.get("key").getAsString().toCharArray());
} catch (IllegalArgumentException e) {
throw new JsonParseException("key parameter must be a valid hex string", e);
}
info = SecurityInfo.newPreSharedKeyInfo(endpoint, identity, key);
} else if (rpk != null) {
PublicKey key;
try {
byte[] x = Hex.decodeHex(rpk.get("x").getAsString().toCharArray());
byte[] y = Hex.decodeHex(rpk.get("y").getAsString().toCharArray());
String params = rpk.get("params").getAsString();
AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
algoParameters.init(new ECGenParameterSpec(params));
ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);
KeySpec keySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(x), new BigInteger(y)), parameterSpec);
key = KeyFactory.getInstance("EC").generatePublic(keySpec);
} catch (IllegalArgumentException | InvalidKeySpecException | NoSuchAlgorithmException | InvalidParameterSpecException e) {
throw new JsonParseException("Invalid security info content", e);
}
info = SecurityInfo.newRawPublicKeyInfo(endpoint, key);
} else if (x509 != null && x509.getAsBoolean()) {
info = SecurityInfo.newX509CertInfo(endpoint);
} else {
throw new JsonParseException("Invalid security info content");
}
}
return info;
}
use of java.security.spec.ECPoint in project web3sdk by FISCO-BCOS.
the class PEMManager method getPublicKey.
public PublicKey getPublicKey() throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {
ECPrivateKey privateKey = (ECPrivateKey) getPrivateKey();
ECParameterSpec params = privateKey.getParams();
org.bouncycastle.jce.spec.ECParameterSpec bcSpec = org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertSpec(params, false);
org.bouncycastle.math.ec.ECPoint q = bcSpec.getG().multiply(privateKey.getS());
org.bouncycastle.math.ec.ECPoint bcW = bcSpec.getCurve().decodePoint(q.getEncoded(false));
ECPoint w = new ECPoint(bcW.getAffineXCoord().toBigInteger(), bcW.getAffineYCoord().toBigInteger());
ECPublicKeySpec keySpec = new ECPublicKeySpec(w, tryFindNamedCurveSpec(params));
return (PublicKey) KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME).generatePublic(keySpec);
}
use of java.security.spec.ECPoint in project web3sdk by FISCO-BCOS.
the class P12Manager method getPublicKey.
public PublicKey getPublicKey() throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
ECPrivateKey privateKey = (ECPrivateKey) getPrivateKey();
ECParameterSpec params = privateKey.getParams();
org.bouncycastle.jce.spec.ECParameterSpec bcSpec = org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertSpec(params, false);
org.bouncycastle.math.ec.ECPoint q = bcSpec.getG().multiply(privateKey.getS());
org.bouncycastle.math.ec.ECPoint bcW = bcSpec.getCurve().decodePoint(q.getEncoded(false));
ECPoint w = new ECPoint(bcW.getAffineXCoord().toBigInteger(), bcW.getAffineYCoord().toBigInteger());
ECPublicKeySpec keySpec = new ECPublicKeySpec(w, tryFindNamedCurveSpec(params));
return (PublicKey) KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME).generatePublic(keySpec);
}
Aggregations