Search in sources :

Example 1 with Ed25519PublicKeyParameters

use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.

the class OpenSSHPublicKeyUtil method parsePublicKey.

/**
 * Parse a public key from an SSHBuffer instance.
 *
 * @param buffer containing the SSH public key.
 * @return A CipherParameters instance.
 */
public static AsymmetricKeyParameter parsePublicKey(SSHBuffer buffer) {
    AsymmetricKeyParameter result = null;
    String magic = buffer.readString();
    if (RSA.equals(magic)) {
        BigInteger e = buffer.readBigNumPositive();
        BigInteger n = buffer.readBigNumPositive();
        result = new RSAKeyParameters(false, n, e);
    } else if (DSS.equals(magic)) {
        BigInteger p = buffer.readBigNumPositive();
        BigInteger q = buffer.readBigNumPositive();
        BigInteger g = buffer.readBigNumPositive();
        BigInteger pubKey = buffer.readBigNumPositive();
        result = new DSAPublicKeyParameters(pubKey, new DSAParameters(p, q, g));
    } else if (magic.startsWith(ECDSA)) {
        String curveName = buffer.readString();
        ASN1ObjectIdentifier oid = SSHNamedCurves.getByName(curveName);
        X9ECParameters x9ECParameters = SSHNamedCurves.getParameters(oid);
        if (x9ECParameters == null) {
            throw new IllegalStateException("unable to find curve for " + magic + " using curve name " + curveName);
        }
        ECCurve curve = x9ECParameters.getCurve();
        byte[] pointRaw = buffer.readBlock();
        result = new ECPublicKeyParameters(curve.decodePoint(pointRaw), new ECNamedDomainParameters(oid, x9ECParameters));
    } else if (ED_25519.equals(magic)) {
        byte[] pubKeyBytes = buffer.readBlock();
        if (pubKeyBytes.length != Ed25519PublicKeyParameters.KEY_SIZE) {
            throw new IllegalStateException("public key value of wrong length");
        }
        result = new Ed25519PublicKeyParameters(pubKeyBytes, 0);
    }
    if (result == null) {
        throw new IllegalArgumentException("unable to parse key");
    }
    if (buffer.hasRemaining()) {
        throw new IllegalArgumentException("decoded key has trailing data");
    }
    return result;
}
Also used : DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) RSAKeyParameters(com.github.zhenwei.core.crypto.params.RSAKeyParameters) AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) BigInteger(java.math.BigInteger) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 2 with Ed25519PublicKeyParameters

use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.

the class Ed25519KeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    Ed25519PrivateKeyParameters privateKey = new Ed25519PrivateKeyParameters(random);
    Ed25519PublicKeyParameters publicKey = privateKey.generatePublicKey();
    return new AsymmetricCipherKeyPair(publicKey, privateKey);
}
Also used : Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) Ed25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 3 with Ed25519PublicKeyParameters

use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.

the class KeyFactorySpi method engineGeneratePublic.

protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
    if (keySpec instanceof X509EncodedKeySpec) {
        byte[] enc = ((X509EncodedKeySpec) keySpec).getEncoded();
        // optimise if we can
        if ((specificBase == 0 || specificBase == enc[8])) {
            // watch out for badly placed DER NULL - the default X509Cert will add these!
            if (enc[9] == 0x05 && enc[10] == 0x00) {
                SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo.getInstance(enc);
                keyInfo = new SubjectPublicKeyInfo(new AlgorithmIdentifier(keyInfo.getAlgorithm().getAlgorithm()), keyInfo.getPublicKeyData().getBytes());
                try {
                    enc = keyInfo.getEncoded(ASN1Encoding.DER);
                } catch (IOException e) {
                    throw new InvalidKeySpecException("attempt to reconstruct key failed: " + e.getMessage());
                }
            }
            switch(enc[8]) {
                case x448_type:
                    return new BCXDHPublicKey(x448Prefix, enc);
                case x25519_type:
                    return new BCXDHPublicKey(x25519Prefix, enc);
                case Ed448_type:
                    return new BCEdDSAPublicKey(Ed448Prefix, enc);
                case Ed25519_type:
                    return new BCEdDSAPublicKey(Ed25519Prefix, enc);
                default:
                    return super.engineGeneratePublic(keySpec);
            }
        }
    } else if (keySpec instanceof RawEncodedKeySpec) {
        byte[] enc = ((RawEncodedKeySpec) keySpec).getEncoded();
        switch(specificBase) {
            case x448_type:
                return new BCXDHPublicKey(new X448PublicKeyParameters(enc));
            case x25519_type:
                return new BCXDHPublicKey(new X25519PublicKeyParameters(enc));
            case Ed448_type:
                return new BCEdDSAPublicKey(new Ed448PublicKeyParameters(enc));
            case Ed25519_type:
                return new BCEdDSAPublicKey(new Ed25519PublicKeyParameters(enc));
            default:
                throw new InvalidKeySpecException("factory not a specific type, cannot recognise raw encoding");
        }
    } else if (keySpec instanceof OpenSSHPublicKeySpec) {
        CipherParameters parameters = OpenSSHPublicKeyUtil.parsePublicKey(((OpenSSHPublicKeySpec) keySpec).getEncoded());
        if (parameters instanceof Ed25519PublicKeyParameters) {
            return new BCEdDSAPublicKey(new byte[0], ((Ed25519PublicKeyParameters) parameters).getEncoded());
        }
        throw new IllegalStateException("openssh public key not Ed25519 public key");
    }
    return super.engineGeneratePublic(keySpec);
}
Also used : X448PublicKeyParameters(com.github.zhenwei.core.crypto.params.X448PublicKeyParameters) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) IOException(java.io.IOException) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) Ed448PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed448PublicKeyParameters) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) RawEncodedKeySpec(com.github.zhenwei.provider.jcajce.spec.RawEncodedKeySpec) OpenSSHPublicKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec) X25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.X25519PublicKeyParameters) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 4 with Ed25519PublicKeyParameters

use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.

the class KeyFactorySpi method engineGetKeySpec.

protected KeySpec engineGetKeySpec(Key key, Class spec) throws InvalidKeySpecException {
    if (spec.isAssignableFrom(OpenSSHPrivateKeySpec.class) && key instanceof BCEdDSAPrivateKey) {
        try {
            // 
            // The DEROctetString at element 2 is an encoded DEROctetString with the private key value
            // within it.
            // 
            ASN1Sequence seq = ASN1Sequence.getInstance(key.getEncoded());
            ASN1OctetString val = ASN1OctetString.getInstance(seq.getObjectAt(2));
            byte[] encoding = ASN1OctetString.getInstance(ASN1Primitive.fromByteArray(val.getOctets())).getOctets();
            return new OpenSSHPrivateKeySpec(OpenSSHPrivateKeyUtil.encodePrivateKey(new Ed25519PrivateKeyParameters(encoding)));
        } catch (IOException ex) {
            throw new InvalidKeySpecException(ex.getMessage(), ex.getCause());
        }
    } else if (spec.isAssignableFrom(OpenSSHPublicKeySpec.class) && key instanceof BCEdDSAPublicKey) {
        try {
            byte[] encoding = key.getEncoded();
            if (!Arrays.areEqual(Ed25519Prefix, 0, Ed25519Prefix.length, encoding, 0, encoding.length - Ed25519PublicKeyParameters.KEY_SIZE)) {
                throw new InvalidKeySpecException("Invalid Ed25519 public key encoding");
            }
            Ed25519PublicKeyParameters publicKey = new Ed25519PublicKeyParameters(encoding, Ed25519Prefix.length);
            return new OpenSSHPublicKeySpec(OpenSSHPublicKeyUtil.encodePublicKey(publicKey));
        } catch (IOException ex) {
            throw new InvalidKeySpecException(ex.getMessage(), ex.getCause());
        }
    } else if (spec.isAssignableFrom(RawEncodedKeySpec.class)) {
        if (key instanceof XDHPublicKey) {
            return new RawEncodedKeySpec(((XDHPublicKey) key).getUEncoding());
        }
        if (key instanceof EdDSAPublicKey) {
            return new RawEncodedKeySpec(((EdDSAPublicKey) key).getPointEncoding());
        }
    }
    return super.engineGetKeySpec(key, spec);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) EdDSAPublicKey(com.github.zhenwei.provider.jcajce.interfaces.EdDSAPublicKey) OpenSSHPrivateKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPrivateKeySpec) IOException(java.io.IOException) Ed25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters) XDHPublicKey(com.github.zhenwei.provider.jcajce.interfaces.XDHPublicKey) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) RawEncodedKeySpec(com.github.zhenwei.provider.jcajce.spec.RawEncodedKeySpec) OpenSSHPublicKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 5 with Ed25519PublicKeyParameters

use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.

the class SignatureSpi method engineInitVerify.

protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    AsymmetricKeyParameter pub = getLwEdDSAKeyPublic(publicKey);
    if (pub instanceof Ed25519PublicKeyParameters) {
        signer = getSigner("Ed25519");
    } else if (pub instanceof Ed448PublicKeyParameters) {
        signer = getSigner("Ed448");
    } else {
        throw new IllegalStateException("unsupported public key type");
    }
    signer.init(false, pub);
}
Also used : AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) Ed448PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed448PublicKeyParameters)

Aggregations

Ed25519PublicKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters)8 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)4 IOException (java.io.IOException)4 DSAPublicKeyParameters (com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters)3 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)3 Ed25519PrivateKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters)3 Ed448PublicKeyParameters (com.github.zhenwei.core.crypto.params.Ed448PublicKeyParameters)3 RSAKeyParameters (com.github.zhenwei.core.crypto.params.RSAKeyParameters)3 BigInteger (java.math.BigInteger)3 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)2 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)2 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)2 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)2 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)2 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)2 X25519PublicKeyParameters (com.github.zhenwei.core.crypto.params.X25519PublicKeyParameters)2 X448PublicKeyParameters (com.github.zhenwei.core.crypto.params.X448PublicKeyParameters)2 OpenSSHPublicKeySpec (com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec)2 RawEncodedKeySpec (com.github.zhenwei.provider.jcajce.spec.RawEncodedKeySpec)2