use of org.gudy.bouncycastle.asn1.x9.X9ECParameters in project BiglyBT by BiglySoftware.
the class JCEECPublicKey method getEncoded.
@Override
public byte[] getEncoded() {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DEROutputStream dOut = new DEROutputStream(bOut);
X962Parameters params = null;
if (ecSpec instanceof ECNamedCurveParameterSpec) {
params = new X962Parameters(X962NamedCurves.getOID(((ECNamedCurveParameterSpec) ecSpec).getName()));
} else {
X9ECParameters ecP = new X9ECParameters(ecSpec.getCurve(), ecSpec.getG(), ecSpec.getN(), ecSpec.getH(), ecSpec.getSeed());
params = new X962Parameters(ecP);
}
ASN1OctetString p = (ASN1OctetString) (new X9ECPoint(this.getQ()).getDERObject());
SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params.getDERObject()), p.getOctets());
try {
dOut.writeObject(info);
dOut.close();
} catch (IOException e) {
throw new RuntimeException("Error encoding EC public key");
}
return bOut.toByteArray();
}
use of org.gudy.bouncycastle.asn1.x9.X9ECParameters in project xipki by xipki.
the class BaseX509Certprofile method checkEcSubjectPublicKeyInfo.
private static void checkEcSubjectPublicKeyInfo(ASN1ObjectIdentifier curveOid, byte[] encoded) throws BadCertTemplateException {
ParamUtil.requireNonNull("curveOid", curveOid);
ParamUtil.requireNonNull("encoded", encoded);
ParamUtil.requireMin("encoded.length", encoded.length, 1);
Integer expectedLength = ecCurveFieldSizes.get(curveOid);
if (expectedLength == null) {
X9ECParameters ecP = ECUtil.getNamedCurveByOid(curveOid);
ECCurve curve = ecP.getCurve();
expectedLength = (curve.getFieldSize() + 7) / 8;
ecCurveFieldSizes.put(curveOid, expectedLength);
}
switch(encoded[0]) {
// compressed
case 0x02:
case // compressed
0x03:
if (encoded.length != (expectedLength + 1)) {
throw new BadCertTemplateException("incorrect length for compressed encoding");
}
break;
// uncompressed
case 0x04:
// hybrid
case 0x06:
case // hybrid
0x07:
if (encoded.length != (2 * expectedLength + 1)) {
throw new BadCertTemplateException("incorrect length for uncompressed/hybrid encoding");
}
break;
default:
throw new BadCertTemplateException(String.format("invalid point encoding 0x%02x", encoded[0]));
}
}
use of org.gudy.bouncycastle.asn1.x9.X9ECParameters in project xipki by xipki.
the class GMUtil method getSM2Z.
// CHECKSTYLE:SKIP
public static byte[] getSM2Z(byte[] userID, ASN1ObjectIdentifier curveOid, BigInteger pubPointX, BigInteger pubPointY) {
SM3Digest digest = new SM3Digest();
addUserId(digest, userID);
X9ECParameters ecParams = GMNamedCurves.getByOID(curveOid);
addFieldElement(digest, ecParams.getCurve().getA());
addFieldElement(digest, ecParams.getCurve().getB());
addFieldElement(digest, ecParams.getG().getAffineXCoord());
addFieldElement(digest, ecParams.getG().getAffineYCoord());
int fieldSize = (ecParams.getCurve().getFieldSize() + 7) / 8;
byte[] bytes = BigIntegers.asUnsignedByteArray(fieldSize, pubPointX);
digest.update(bytes, 0, fieldSize);
bytes = BigIntegers.asUnsignedByteArray(fieldSize, pubPointY);
digest.update(bytes, 0, fieldSize);
byte[] result = new byte[digest.getDigestSize()];
digest.doFinal(result, 0);
return result;
}
use of org.gudy.bouncycastle.asn1.x9.X9ECParameters in project xipki by xipki.
the class PublicKeyChecker method checkECSubjectPublicKeyInfo.
// method checkPublicKey
// CHECKSTYLE:SKIP
private static void checkECSubjectPublicKeyInfo(ASN1ObjectIdentifier curveOid, byte[] encoded) throws BadCertTemplateException {
Integer expectedLength = EC_CURVEFIELD_SIZES.get(curveOid);
if (expectedLength == null) {
X9ECParameters ecP = ECUtil.getNamedCurveByOid(curveOid);
ECCurve curve = ecP.getCurve();
expectedLength = (curve.getFieldSize() + 7) / 8;
EC_CURVEFIELD_SIZES.put(curveOid, expectedLength);
}
switch(encoded[0]) {
// compressed
case 0x02:
case // compressed
0x03:
if (encoded.length != (expectedLength + 1)) {
throw new BadCertTemplateException("incorrect length for compressed encoding");
}
break;
// uncompressed
case 0x04:
// hybrid
case 0x06:
case // hybrid
0x07:
if (encoded.length != (2 * expectedLength + 1)) {
throw new BadCertTemplateException("incorrect length for uncompressed/hybrid encoding");
}
break;
default:
throw new BadCertTemplateException("invalid point encoding 0x" + Integer.toString(encoded[0], 16));
}
// end switch
}
use of org.gudy.bouncycastle.asn1.x9.X9ECParameters in project incubator-pulsar by apache.
the class MessageCrypto method loadPrivateKey.
private PrivateKey loadPrivateKey(byte[] keyBytes) throws Exception {
Reader keyReader = new StringReader(new String(keyBytes));
PrivateKey privateKey = null;
try (PEMParser pemReader = new PEMParser(keyReader)) {
X9ECParameters ecParam = null;
Object pemObj = pemReader.readObject();
if (pemObj instanceof ASN1ObjectIdentifier) {
// make sure this is EC Parameter we're handling. In which case
// we'll store it and read the next object which should be our
// EC Private Key
ASN1ObjectIdentifier ecOID = (ASN1ObjectIdentifier) pemObj;
ecParam = ECNamedCurveTable.getByOID(ecOID);
if (ecParam == null) {
throw new PEMException("Unable to find EC Parameter for the given curve oid: " + ecOID.getId());
}
pemObj = pemReader.readObject();
} else if (pemObj instanceof X9ECParameters) {
ecParam = (X9ECParameters) pemObj;
pemObj = pemReader.readObject();
}
if (pemObj instanceof PEMKeyPair) {
PrivateKeyInfo pKeyInfo = ((PEMKeyPair) pemObj).getPrivateKeyInfo();
JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter();
privateKey = pemConverter.getPrivateKey(pKeyInfo);
}
if (ecParam != null && ECDSA.equals(privateKey.getAlgorithm())) {
ECParameterSpec ecSpec = new ECParameterSpec(ecParam.getCurve(), ecParam.getG(), ecParam.getN(), ecParam.getH(), ecParam.getSeed());
KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BouncyCastleProvider.PROVIDER_NAME);
ECPrivateKeySpec keySpec = new ECPrivateKeySpec(((BCECPrivateKey) privateKey).getS(), ecSpec);
privateKey = (PrivateKey) keyFactory.generatePrivate(keySpec);
}
} catch (IOException e) {
throw new Exception(e);
}
return privateKey;
}
Aggregations