Search in sources :

Example 1 with ECNamedDomainParameters

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

the class PrivateKeyInfoFactory method createPrivateKeyInfo.

/**
 * Create a PrivateKeyInfo representation of a private key with attributes.
 *
 * @param privateKey the key to be encoded into the info object.
 * @param attributes the set of attributes to be included.
 * @return the appropriate PrivateKeyInfo
 * @throws IOException on an error encoding the key
 */
public static PrivateKeyInfo createPrivateKeyInfo(AsymmetricKeyParameter privateKey, ASN1Set attributes) throws IOException {
    if (privateKey instanceof RSAKeyParameters) {
        RSAPrivateCrtKeyParameters priv = (RSAPrivateCrtKeyParameters) privateKey;
        return new PrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPrivateKey(priv.getModulus(), priv.getPublicExponent(), priv.getExponent(), priv.getP(), priv.getQ(), priv.getDP(), priv.getDQ(), priv.getQInv()), attributes);
    } else if (privateKey instanceof DSAPrivateKeyParameters) {
        DSAPrivateKeyParameters priv = (DSAPrivateKeyParameters) privateKey;
        DSAParameters params = priv.getParameters();
        return new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(params.getP(), params.getQ(), params.getG())), new ASN1Integer(priv.getX()), attributes);
    } else if (privateKey instanceof ECPrivateKeyParameters) {
        ECPrivateKeyParameters priv = (ECPrivateKeyParameters) privateKey;
        ECDomainParameters domainParams = priv.getParameters();
        ASN1Encodable params;
        int orderBitLength;
        if (domainParams == null) {
            // Implicitly CA
            params = new X962Parameters(DERNull.INSTANCE);
            orderBitLength = priv.getD().bitLength();
        } else if (domainParams instanceof ECGOST3410Parameters) {
            GOST3410PublicKeyAlgParameters gostParams = new GOST3410PublicKeyAlgParameters(((ECGOST3410Parameters) domainParams).getPublicKeyParamSet(), ((ECGOST3410Parameters) domainParams).getDigestParamSet(), ((ECGOST3410Parameters) domainParams).getEncryptionParamSet());
            int size;
            ASN1ObjectIdentifier identifier;
            if (cryptoProOids.contains(gostParams.getPublicKeyParamSet())) {
                size = 32;
                identifier = CryptoProObjectIdentifiers.gostR3410_2001;
            } else {
                boolean is512 = priv.getD().bitLength() > 256;
                identifier = (is512) ? RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512 : RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
                size = (is512) ? 64 : 32;
            }
            byte[] encKey = new byte[size];
            extractBytes(encKey, size, 0, priv.getD());
            return new PrivateKeyInfo(new AlgorithmIdentifier(identifier, gostParams), new DEROctetString(encKey));
        } else if (domainParams instanceof ECNamedDomainParameters) {
            params = new X962Parameters(((ECNamedDomainParameters) domainParams).getName());
            orderBitLength = domainParams.getN().bitLength();
        } else {
            X9ECParameters ecP = new X9ECParameters(domainParams.getCurve(), new X9ECPoint(domainParams.getG(), false), domainParams.getN(), domainParams.getH(), domainParams.getSeed());
            params = new X962Parameters(ecP);
            orderBitLength = domainParams.getN().bitLength();
        }
        ECPoint q = new FixedPointCombMultiplier().multiply(domainParams.getG(), priv.getD());
        // TODO Support point compression
        DERBitString publicKey = new DERBitString(q.getEncoded(false));
        return new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), new ECPrivateKey(orderBitLength, priv.getD(), publicKey, params), attributes);
    } else if (privateKey instanceof X448PrivateKeyParameters) {
        X448PrivateKeyParameters key = (X448PrivateKeyParameters) privateKey;
        return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448), new DEROctetString(key.getEncoded()), attributes, key.generatePublicKey().getEncoded());
    } else if (privateKey instanceof X25519PrivateKeyParameters) {
        X25519PrivateKeyParameters key = (X25519PrivateKeyParameters) privateKey;
        return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), new DEROctetString(key.getEncoded()), attributes, key.generatePublicKey().getEncoded());
    } else if (privateKey instanceof Ed448PrivateKeyParameters) {
        Ed448PrivateKeyParameters key = (Ed448PrivateKeyParameters) privateKey;
        return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448), new DEROctetString(key.getEncoded()), attributes, key.generatePublicKey().getEncoded());
    } else if (privateKey instanceof Ed25519PrivateKeyParameters) {
        Ed25519PrivateKeyParameters key = (Ed25519PrivateKeyParameters) privateKey;
        return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519), new DEROctetString(key.getEncoded()), attributes, key.generatePublicKey().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) X25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.X25519PrivateKeyParameters) Ed448PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed448PrivateKeyParameters) Ed25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters) RSAKeyParameters(com.github.zhenwei.core.crypto.params.RSAKeyParameters) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) FixedPointCombMultiplier(com.github.zhenwei.core.math.ec.FixedPointCombMultiplier) DSAParameter(com.github.zhenwei.core.asn1.x509.DSAParameter) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) X448PrivateKeyParameters(com.github.zhenwei.core.crypto.params.X448PrivateKeyParameters) ECPrivateKey(com.github.zhenwei.core.asn1.sec.ECPrivateKey) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) DERBitString(com.github.zhenwei.core.asn1.DERBitString) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) IOException(java.io.IOException) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) DSAPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DSAPrivateKeyParameters) RSAPrivateKey(com.github.zhenwei.core.asn1.pkcs.RSAPrivateKey) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) RSAPrivateCrtKeyParameters(com.github.zhenwei.core.crypto.params.RSAPrivateCrtKeyParameters)

Example 2 with ECNamedDomainParameters

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

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

the class BcITSPublicEncryptionKey method getKey.

public AsymmetricKeyParameter getKey() {
    X9ECParameters params;
    BasePublicEncryptionKey baseKey = encryptionKey.getBasePublicEncryptionKey();
    ASN1ObjectIdentifier curveID;
    switch(baseKey.getChoice()) {
        case BasePublicEncryptionKey.eciesNistP256:
            curveID = SECObjectIdentifiers.secp256r1;
            params = NISTNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
            break;
        case BasePublicEncryptionKey.eciesBrainpoolP256r1:
            curveID = TeleTrusTObjectIdentifiers.brainpoolP256r1;
            params = TeleTrusTNamedCurves.getByOID(TeleTrusTObjectIdentifiers.brainpoolP256r1);
            break;
        default:
            throw new IllegalStateException("unknown key type");
    }
    ECCurve curve = params.getCurve();
    ASN1Encodable pviCurvePoint = encryptionKey.getBasePublicEncryptionKey().getValue();
    final EccCurvePoint itsPoint;
    if (pviCurvePoint instanceof EccCurvePoint) {
        itsPoint = (EccCurvePoint) baseKey.getValue();
    } else {
        throw new IllegalStateException("extension to public verification key not supported");
    }
    byte[] key;
    if (itsPoint instanceof EccP256CurvePoint) {
        key = itsPoint.getEncodedPoint();
    } else if (itsPoint instanceof EccP384CurvePoint) {
        key = itsPoint.getEncodedPoint();
    } else {
        throw new IllegalStateException("unknown key type");
    }
    ECPoint point = curve.decodePoint(key).normalize();
    return new ECPublicKeyParameters(point, new ECNamedDomainParameters(curveID, params));
}
Also used : X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) EccCurvePoint(com.github.zhenwei.pkix.util.oer.its.EccCurvePoint) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) BasePublicEncryptionKey(com.github.zhenwei.pkix.util.oer.its.BasePublicEncryptionKey) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) EccP256CurvePoint(com.github.zhenwei.pkix.util.oer.its.EccP256CurvePoint) EccP384CurvePoint(com.github.zhenwei.pkix.util.oer.its.EccP384CurvePoint) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 4 with ECNamedDomainParameters

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

the class BcITSPublicEncryptionKey method fromKeyParameters.

static PublicEncryptionKey fromKeyParameters(ECPublicKeyParameters pubKey) {
    ASN1ObjectIdentifier curveID = ((ECNamedDomainParameters) pubKey.getParameters()).getName();
    ECPoint q = pubKey.getQ();
    if (curveID.equals(SECObjectIdentifiers.secp256r1)) {
        return new PublicEncryptionKey(SymmAlgorithm.aes128Ccm, new BasePublicEncryptionKey.Builder().setChoice(BasePublicEncryptionKey.eciesNistP256).setValue(EccP256CurvePoint.builder().createUncompressedP256(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger())).createBasePublicEncryptionKey());
    } else if (curveID.equals(TeleTrusTObjectIdentifiers.brainpoolP256r1)) {
        return new PublicEncryptionKey(SymmAlgorithm.aes128Ccm, new BasePublicEncryptionKey.Builder().setChoice(BasePublicEncryptionKey.eciesBrainpoolP256r1).setValue(EccP256CurvePoint.builder().createUncompressedP256(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger())).createBasePublicEncryptionKey());
    } else {
        throw new IllegalArgumentException("unknown curve in public encryption key");
    }
}
Also used : BasePublicEncryptionKey(com.github.zhenwei.pkix.util.oer.its.BasePublicEncryptionKey) PublicEncryptionKey(com.github.zhenwei.pkix.util.oer.its.PublicEncryptionKey) ITSPublicEncryptionKey(com.github.zhenwei.pkix.its.ITSPublicEncryptionKey) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) BasePublicEncryptionKey(com.github.zhenwei.pkix.util.oer.its.BasePublicEncryptionKey) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 5 with ECNamedDomainParameters

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

the class BcITSPublicVerificationKey method getKey.

public AsymmetricKeyParameter getKey() {
    X9ECParameters params;
    ASN1ObjectIdentifier curveID;
    switch(verificationKey.getChoice()) {
        case PublicVerificationKey.ecdsaNistP256:
            curveID = SECObjectIdentifiers.secp256r1;
            params = NISTNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
            break;
        case PublicVerificationKey.ecdsaBrainpoolP256r1:
            curveID = TeleTrusTObjectIdentifiers.brainpoolP256r1;
            params = TeleTrusTNamedCurves.getByOID(TeleTrusTObjectIdentifiers.brainpoolP256r1);
            break;
        case PublicVerificationKey.ecdsaBrainpoolP384r1:
            curveID = TeleTrusTObjectIdentifiers.brainpoolP384r1;
            params = TeleTrusTNamedCurves.getByOID(TeleTrusTObjectIdentifiers.brainpoolP384r1);
            break;
        default:
            throw new IllegalStateException("unknown key type");
    }
    ECCurve curve = params.getCurve();
    ASN1Encodable pviCurvePoint = verificationKey.getCurvePoint();
    final EccCurvePoint itsPoint;
    if (pviCurvePoint instanceof EccCurvePoint) {
        itsPoint = (EccCurvePoint) verificationKey.getCurvePoint();
    } else {
        throw new IllegalStateException("extension to public verification key not supported");
    }
    byte[] key;
    if (itsPoint instanceof EccP256CurvePoint) {
        key = itsPoint.getEncodedPoint();
    } else if (itsPoint instanceof EccP384CurvePoint) {
        key = itsPoint.getEncodedPoint();
    } else {
        throw new IllegalStateException("unknown key type");
    }
    ECPoint point = curve.decodePoint(key).normalize();
    return new ECPublicKeyParameters(point, new ECNamedDomainParameters(curveID, params));
}
Also used : X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) EccCurvePoint(com.github.zhenwei.pkix.util.oer.its.EccCurvePoint) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) EccP256CurvePoint(com.github.zhenwei.pkix.util.oer.its.EccP256CurvePoint) EccP384CurvePoint(com.github.zhenwei.pkix.util.oer.its.EccP384CurvePoint) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Aggregations

ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)14 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)12 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)10 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)6 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)5 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)5 ECGOST3410Parameters (com.github.zhenwei.core.crypto.params.ECGOST3410Parameters)5 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)4 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)4 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)4 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)4 GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)3 RSAPrivateKey (com.github.zhenwei.core.asn1.pkcs.RSAPrivateKey)3 ECPrivateKey (com.github.zhenwei.core.asn1.sec.ECPrivateKey)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 DSAParameter (com.github.zhenwei.core.asn1.x509.DSAParameter)3 X962Parameters (com.github.zhenwei.core.asn1.x9.X962Parameters)3 DSAPrivateKeyParameters (com.github.zhenwei.core.crypto.params.DSAPrivateKeyParameters)3 Ed25519PrivateKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters)3 RSAPrivateCrtKeyParameters (com.github.zhenwei.core.crypto.params.RSAPrivateCrtKeyParameters)3