use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class BCECGOST3410PublicKey method populateFromPubKeyInfo.
private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
ASN1BitString bits = info.getPublicKeyData();
ASN1OctetString key;
this.algorithm = "ECGOST3410";
try {
key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
} catch (IOException ex) {
throw new IllegalArgumentException("error recovering public key");
}
byte[] keyEnc = key.getOctets();
byte[] x9Encoding = new byte[65];
x9Encoding[0] = 0x04;
for (int i = 1; i <= 32; ++i) {
x9Encoding[i] = keyEnc[32 - i];
x9Encoding[i + 32] = keyEnc[64 - i];
}
ASN1ObjectIdentifier paramOID;
if (info.getAlgorithm().getParameters() instanceof ASN1ObjectIdentifier) {
paramOID = ASN1ObjectIdentifier.getInstance(info.getAlgorithm().getParameters());
gostParams = paramOID;
} else {
GOST3410PublicKeyAlgParameters params = GOST3410PublicKeyAlgParameters.getInstance(info.getAlgorithm().getParameters());
gostParams = params;
paramOID = params.getPublicKeyParamSet();
}
ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(paramOID));
ECCurve curve = spec.getCurve();
EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
this.ecPublicKey = new ECPublicKeyParameters(curve.decodePoint(x9Encoding), ECUtil.getDomainParameters(null, spec));
this.ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(paramOID), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class BCECGOST3410_2012PublicKey method populateFromPubKeyInfo.
private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
ASN1ObjectIdentifier algOid = info.getAlgorithm().getAlgorithm();
ASN1BitString bits = info.getPublicKeyData();
ASN1OctetString key;
this.algorithm = "ECGOST3410-2012";
try {
key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
} catch (IOException ex) {
throw new IllegalArgumentException("error recovering public key");
}
byte[] keyEnc = key.getOctets();
int fieldSize = 32;
if (algOid.equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512)) {
fieldSize = 64;
}
int keySize = 2 * fieldSize;
byte[] x9Encoding = new byte[1 + keySize];
x9Encoding[0] = 0x04;
for (int i = 1; i <= fieldSize; ++i) {
x9Encoding[i] = keyEnc[fieldSize - i];
x9Encoding[i + fieldSize] = keyEnc[keySize - i];
}
this.gostParams = GOST3410PublicKeyAlgParameters.getInstance(info.getAlgorithm().getParameters());
ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
ECCurve curve = spec.getCurve();
EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
this.ecPublicKey = new ECPublicKeyParameters(curve.decodePoint(x9Encoding), ECUtil.getDomainParameters(null, spec));
this.ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class BCDSTU4145PublicKey method populateFromPubKeyInfo.
private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
ASN1BitString bits = info.getPublicKeyData();
ASN1OctetString key;
this.algorithm = "DSTU4145";
try {
key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
} catch (IOException ex) {
throw new IllegalArgumentException("error recovering public key");
}
byte[] keyEnc = key.getOctets();
if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le)) {
reverseBytes(keyEnc);
}
ASN1Sequence seq = ASN1Sequence.getInstance(info.getAlgorithm().getParameters());
com.github.zhenwei.provider.jce.spec.ECParameterSpec spec = null;
X9ECParameters x9Params = null;
if (seq.getObjectAt(0) instanceof ASN1Integer) {
x9Params = X9ECParameters.getInstance(seq);
spec = new com.github.zhenwei.provider.jce.spec.ECParameterSpec(x9Params.getCurve(), x9Params.getG(), x9Params.getN(), x9Params.getH(), x9Params.getSeed());
} else {
dstuParams = DSTU4145Params.getInstance(seq);
if (dstuParams.isNamedCurve()) {
ASN1ObjectIdentifier curveOid = dstuParams.getNamedCurve();
ECDomainParameters ecP = DSTU4145NamedCurves.getByOID(curveOid);
spec = new ECNamedCurveParameterSpec(curveOid.getId(), ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
} else {
DSTU4145ECBinary binary = dstuParams.getECBinary();
byte[] b_bytes = binary.getB();
if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le)) {
reverseBytes(b_bytes);
}
DSTU4145BinaryField field = binary.getField();
ECCurve curve = new ECCurve.F2m(field.getM(), field.getK1(), field.getK2(), field.getK3(), binary.getA(), new BigInteger(1, b_bytes));
byte[] g_bytes = binary.getG();
if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le)) {
reverseBytes(g_bytes);
}
spec = new com.github.zhenwei.provider.jce.spec.ECParameterSpec(curve, DSTU4145PointEncoder.decodePoint(curve, g_bytes), binary.getN());
}
}
ECCurve curve = spec.getCurve();
EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
if (dstuParams != null) {
ECPoint g = EC5Util.convertPoint(spec.getG());
if (dstuParams.isNamedCurve()) {
String name = dstuParams.getNamedCurve().getId();
ecSpec = new ECNamedCurveSpec(name, ellipticCurve, g, spec.getN(), spec.getH());
} else {
ecSpec = new ECParameterSpec(ellipticCurve, g, spec.getN(), spec.getH().intValue());
}
} else {
ecSpec = EC5Util.convertToSpec(x9Params);
}
// this.q = curve.createPoint(new BigInteger(1, x), new BigInteger(1, y), false);
this.ecPublicKey = new ECPublicKeyParameters(DSTU4145PointEncoder.decodePoint(curve, keyEnc), EC5Util.getDomainParameters(null, ecSpec));
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class BCECPublicKey method populateFromPubKeyInfo.
private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
X962Parameters params = X962Parameters.getInstance(info.getAlgorithm().getParameters());
ECCurve curve = EC5Util.getCurve(configuration, params);
ecSpec = EC5Util.convertToSpec(params, curve);
ASN1BitString bits = info.getPublicKeyData();
byte[] data = bits.getBytes();
ASN1OctetString key = new DEROctetString(data);
//
if (data[0] == 0x04 && data[1] == data.length - 2 && (data[2] == 0x02 || data[2] == 0x03)) {
int qLength = new X9IntegerConverter().getByteLength(curve);
if (qLength >= data.length - 3) {
try {
key = (ASN1OctetString) ASN1Primitive.fromByteArray(data);
} catch (IOException ex) {
throw new IllegalArgumentException("error recovering public key");
}
}
}
X9ECPoint derQ = new X9ECPoint(curve, key);
this.ecPublicKey = new ECPublicKeyParameters(derQ.getPoint(), ECUtil.getDomainParameters(configuration, params));
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class ECUtil method generatePublicKeyParameter.
public static AsymmetricKeyParameter generatePublicKeyParameter(PublicKey key) throws InvalidKeyException {
if (key instanceof ECPublicKey) {
ECPublicKey k = (ECPublicKey) key;
ECParameterSpec s = k.getParameters();
return new ECPublicKeyParameters(k.getQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
} else if (key instanceof java.security.interfaces.ECPublicKey) {
java.security.interfaces.ECPublicKey pubKey = (java.security.interfaces.ECPublicKey) key;
ECParameterSpec s = EC5Util.convertSpec(pubKey.getParams());
return new ECPublicKeyParameters(EC5Util.convertPoint(pubKey.getParams(), pubKey.getW()), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
} else {
// see if we can build a key from key.getEncoded()
try {
byte[] bytes = key.getEncoded();
if (bytes == null) {
throw new InvalidKeyException("no encoding for EC public key");
}
PublicKey publicKey = WeGooProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
if (publicKey instanceof java.security.interfaces.ECPublicKey) {
return ECUtil.generatePublicKeyParameter(publicKey);
}
} catch (Exception e) {
throw new InvalidKeyException("cannot identify EC public key: " + e.toString());
}
}
throw new InvalidKeyException("cannot identify EC public key.");
}
Aggregations