Search in sources :

Example 81 with ECPoint

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);
    }
}
Also used : DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) DNSSECValidationFailedException(org.minidns.dnssec.DNSSECValidationFailedException) BigInteger(java.math.BigInteger) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DataInputStream(java.io.DataInputStream) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec)

Example 82 with ECPoint

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);
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) ECPoint(java.security.spec.ECPoint) ECPoint(java.security.spec.ECPoint)

Example 83 with ECPoint

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);
}
Also used : ECPublicKey(java.security.interfaces.ECPublicKey) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 84 with ECPoint

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);
    }
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) NativeBigInteger(net.i2p.util.NativeBigInteger) GeneralSecurityException(java.security.GeneralSecurityException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) ECPublicKey(java.security.interfaces.ECPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) EllipticCurve(java.security.spec.EllipticCurve) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 85 with ECPoint

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;
}
Also used : ECPoint(java.security.spec.ECPoint)

Aggregations

ECPoint (java.security.spec.ECPoint)111 ECParameterSpec (java.security.spec.ECParameterSpec)56 BigInteger (java.math.BigInteger)54 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)36 ECPublicKey (java.security.interfaces.ECPublicKey)31 EllipticCurve (java.security.spec.EllipticCurve)31 KeyPair (java.security.KeyPair)20 ECPrivateKey (java.security.interfaces.ECPrivateKey)20 PublicKey (java.security.PublicKey)17 ECFieldFp (java.security.spec.ECFieldFp)17 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)16 Test (org.junit.Test)16 KeyFactory (java.security.KeyFactory)15 KeyPairGenerator (java.security.KeyPairGenerator)14 AlgorithmParameters (java.security.AlgorithmParameters)13 GeneralSecurityException (java.security.GeneralSecurityException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 KeySpec (java.security.spec.KeySpec)11 IOException (java.io.IOException)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9