use of java.security.spec.ECPoint in project minidns by MiniDNS.
the class ECGOSTSignatureVerifier method getPublicKey.
@Override
protected PublicKey getPublicKey(byte[] key) {
DataInput dis = new DataInputStream(new ByteArrayInputStream(key));
try {
byte[] xBytes = new byte[LENGTH];
dis.readFully(xBytes);
reverse(xBytes);
BigInteger x = new BigInteger(1, xBytes);
byte[] yBytes = new byte[LENGTH];
dis.readFully(yBytes);
reverse(yBytes);
BigInteger y = new BigInteger(1, yBytes);
return getKeyFactory().generatePublic(new ECPublicKeySpec(new ECPoint(x, y), SPEC));
} catch (IOException | InvalidKeySpecException e) {
throw new DNSSECValidationFailedException("Invalid public key!", e);
}
}
use of java.security.spec.ECPoint in project i2p.i2p by i2p.
the class SigUtil method fromJavaKey.
public static SigningPublicKey fromJavaKey(ECPublicKey pk, SigType type) throws GeneralSecurityException {
ECPoint w = pk.getW();
BigInteger x = w.getAffineX();
BigInteger y = w.getAffineY();
int len = type.getPubkeyLen();
byte[] b = combine(x, y, len);
return new SigningPublicKey(type, b);
}
use of java.security.spec.ECPoint in project i2p.i2p by i2p.
the class SigUtil method cvtToJavaECKey.
private static ECPublicKey cvtToJavaECKey(SigningPublicKey pk) throws GeneralSecurityException {
SigType type = pk.getType();
BigInteger[] xy = split(pk.getData());
ECPoint w = new ECPoint(xy[0], xy[1]);
// see ECConstants re: casting
ECPublicKeySpec ks = new ECPublicKeySpec(w, (ECParameterSpec) type.getParams());
KeyFactory kf = KeyFactory.getInstance("EC");
return (ECPublicKey) kf.generatePublic(ks);
}
use of java.security.spec.ECPoint in project i2p.i2p by i2p.
the class KeyGenerator method getSigningPublicKey.
/**
* Convert a SigningPrivateKey to a SigningPublicKey.
* As of 0.9.16, supports all key types.
*
* @param priv a SigningPrivateKey object
* @return a SigningPublicKey object
* @throws IllegalArgumentException on bad key or unknown type
*/
public static SigningPublicKey getSigningPublicKey(SigningPrivateKey priv) {
SigType type = priv.getType();
if (type == null)
throw new IllegalArgumentException("Unknown type");
try {
switch(type.getBaseAlgorithm()) {
case DSA:
BigInteger x = new NativeBigInteger(1, priv.toByteArray());
BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
SigningPublicKey pub = new SigningPublicKey();
pub.setData(SigUtil.rectify(y, SigningPublicKey.KEYSIZE_BYTES));
return pub;
case EC:
ECPrivateKey ecpriv = SigUtil.toJavaECKey(priv);
BigInteger s = ecpriv.getS();
ECParameterSpec spec = (ECParameterSpec) type.getParams();
EllipticCurve curve = spec.getCurve();
ECPoint g = spec.getGenerator();
ECPoint w = ECUtil.scalarMult(g, s, curve);
ECPublicKeySpec ecks = new ECPublicKeySpec(w, ecpriv.getParams());
KeyFactory eckf = KeyFactory.getInstance("EC");
ECPublicKey ecpub = (ECPublicKey) eckf.generatePublic(ecks);
return SigUtil.fromJavaKey(ecpub, type);
case RSA:
RSAPrivateKey rsapriv = SigUtil.toJavaRSAKey(priv);
BigInteger exp = ((RSAKeyGenParameterSpec) type.getParams()).getPublicExponent();
RSAPublicKeySpec rsaks = new RSAPublicKeySpec(rsapriv.getModulus(), exp);
KeyFactory rsakf = KeyFactory.getInstance("RSA");
RSAPublicKey rsapub = (RSAPublicKey) rsakf.generatePublic(rsaks);
return SigUtil.fromJavaKey(rsapub, type);
case EdDSA:
EdDSAPrivateKey epriv = SigUtil.toJavaEdDSAKey(priv);
EdDSAPublicKey epub = new EdDSAPublicKey(new EdDSAPublicKeySpec(epriv.getA(), epriv.getParams()));
return SigUtil.fromJavaKey(epub, type);
default:
throw new IllegalArgumentException("Unsupported algorithm");
}
} catch (GeneralSecurityException gse) {
throw new IllegalArgumentException("Conversion failed", gse);
}
}
use of java.security.spec.ECPoint in project jruby-openssl by jruby.
the class PKeyEC method encode.
private static byte[] encode(final int bitLength, final ECPoint point) {
if (point == ECPoint.POINT_INFINITY)
return new byte[1];
final int bytesLength = (bitLength + 7) / 8;
byte[] encoded = new byte[1 + bytesLength + bytesLength];
encoded[0] = 0x04;
addIntBytes(point.getAffineX(), bytesLength, encoded, 1);
addIntBytes(point.getAffineY(), bytesLength, encoded, 1 + bytesLength);
return encoded;
}
Aggregations