Search in sources :

Example 1 with X25519PublicKeyParameters

use of com.github.zhenwei.core.crypto.params.X25519PublicKeyParameters 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 2 with X25519PublicKeyParameters

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

the class X25519KeyPairGenerator method generateKeyPair.

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

Example 3 with X25519PublicKeyParameters

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

the class SubjectPublicKeyInfoFactory method createSubjectPublicKeyInfo.

/**
 * Create a SubjectPublicKeyInfo public key.
 *
 * @param publicKey the key to be encoded into the info object.
 * @return a SubjectPublicKeyInfo representing the key.
 * @throws IOException on an error encoding the key
 */
public static SubjectPublicKeyInfo createSubjectPublicKeyInfo(AsymmetricKeyParameter publicKey) throws IOException {
    if (publicKey instanceof RSAKeyParameters) {
        RSAKeyParameters pub = (RSAKeyParameters) publicKey;
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(pub.getModulus(), pub.getExponent()));
    } else if (publicKey instanceof DSAPublicKeyParameters) {
        DSAPublicKeyParameters pub = (DSAPublicKeyParameters) publicKey;
        DSAParameter params = null;
        DSAParameters dsaParams = pub.getParameters();
        if (dsaParams != null) {
            params = new DSAParameter(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG());
        }
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, params), new ASN1Integer(pub.getY()));
    } else if (publicKey instanceof ECPublicKeyParameters) {
        ECPublicKeyParameters pub = (ECPublicKeyParameters) publicKey;
        ECDomainParameters domainParams = pub.getParameters();
        ASN1Encodable params;
        if (domainParams == null) {
            // Implicitly CA
            params = new X962Parameters(DERNull.INSTANCE);
        } else if (domainParams instanceof ECGOST3410Parameters) {
            ECGOST3410Parameters gostParams = (ECGOST3410Parameters) domainParams;
            BigInteger bX = pub.getQ().getAffineXCoord().toBigInteger();
            BigInteger bY = pub.getQ().getAffineYCoord().toBigInteger();
            params = new GOST3410PublicKeyAlgParameters(gostParams.getPublicKeyParamSet(), gostParams.getDigestParamSet());
            int encKeySize;
            int offset;
            ASN1ObjectIdentifier algIdentifier;
            if (cryptoProOids.contains(gostParams.getPublicKeyParamSet())) {
                encKeySize = 64;
                offset = 32;
                algIdentifier = CryptoProObjectIdentifiers.gostR3410_2001;
            } else {
                boolean is512 = (bX.bitLength() > 256);
                if (is512) {
                    encKeySize = 128;
                    offset = 64;
                    algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512;
                } else {
                    encKeySize = 64;
                    offset = 32;
                    algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
                }
            }
            byte[] encKey = new byte[encKeySize];
            extractBytes(encKey, encKeySize / 2, 0, bX);
            extractBytes(encKey, encKeySize / 2, offset, bY);
            try {
                return new SubjectPublicKeyInfo(new AlgorithmIdentifier(algIdentifier, params), new DEROctetString(encKey));
            } catch (IOException e) {
                return null;
            }
        } else if (domainParams instanceof ECNamedDomainParameters) {
            params = new X962Parameters(((ECNamedDomainParameters) domainParams).getName());
        } else {
            X9ECParameters ecP = new X9ECParameters(domainParams.getCurve(), // TODO Support point compression
            new X9ECPoint(domainParams.getG(), false), domainParams.getN(), domainParams.getH(), domainParams.getSeed());
            params = new X962Parameters(ecP);
        }
        // TODO Support point compression
        byte[] pubKeyOctets = pub.getQ().getEncoded(false);
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), pubKeyOctets);
    } else if (publicKey instanceof X448PublicKeyParameters) {
        X448PublicKeyParameters key = (X448PublicKeyParameters) publicKey;
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448), key.getEncoded());
    } else if (publicKey instanceof X25519PublicKeyParameters) {
        X25519PublicKeyParameters key = (X25519PublicKeyParameters) publicKey;
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), key.getEncoded());
    } else if (publicKey instanceof Ed448PublicKeyParameters) {
        Ed448PublicKeyParameters key = (Ed448PublicKeyParameters) publicKey;
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448), key.getEncoded());
    } else if (publicKey instanceof Ed25519PublicKeyParameters) {
        Ed25519PublicKeyParameters key = (Ed25519PublicKeyParameters) publicKey;
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519), key.getEncoded());
    } else {
        throw new IOException("key parameters not recognized");
    }
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ECGOST3410Parameters(com.github.zhenwei.core.crypto.params.ECGOST3410Parameters) GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) RSAKeyParameters(com.github.zhenwei.core.crypto.params.RSAKeyParameters) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) Ed448PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed448PublicKeyParameters) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) RSAPublicKey(com.github.zhenwei.core.asn1.pkcs.RSAPublicKey) X25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.X25519PublicKeyParameters) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) DSAParameter(com.github.zhenwei.core.asn1.x509.DSAParameter) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) X448PublicKeyParameters(com.github.zhenwei.core.crypto.params.X448PublicKeyParameters) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) IOException(java.io.IOException) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) BigInteger(java.math.BigInteger) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Aggregations

X25519PublicKeyParameters (com.github.zhenwei.core.crypto.params.X25519PublicKeyParameters)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)2 Ed25519PublicKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters)2 Ed448PublicKeyParameters (com.github.zhenwei.core.crypto.params.Ed448PublicKeyParameters)2 X448PublicKeyParameters (com.github.zhenwei.core.crypto.params.X448PublicKeyParameters)2 IOException (java.io.IOException)2 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)1 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)1 RSAPublicKey (com.github.zhenwei.core.asn1.pkcs.RSAPublicKey)1 DSAParameter (com.github.zhenwei.core.asn1.x509.DSAParameter)1 X962Parameters (com.github.zhenwei.core.asn1.x9.X962Parameters)1 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)1 X9ECPoint (com.github.zhenwei.core.asn1.x9.X9ECPoint)1 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)1 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)1 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)1