Search in sources :

Example 1 with Ed448PrivateKeyParameters

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

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

the class Ed448KeyPairGenerator method generateKeyPair.

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

Example 3 with Ed448PrivateKeyParameters

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

the class SignatureSpi method engineInitSign.

protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    AsymmetricKeyParameter priv = getLwEdDSAKeyPrivate(privateKey);
    if (priv instanceof Ed25519PrivateKeyParameters) {
        signer = getSigner("Ed25519");
    } else if (priv instanceof Ed448PrivateKeyParameters) {
        signer = getSigner("Ed448");
    } else {
        throw new IllegalStateException("unsupported private key type");
    }
    signer.init(true, priv);
}
Also used : AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) Ed448PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed448PrivateKeyParameters) Ed25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters)

Example 4 with Ed448PrivateKeyParameters

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

the class PrivateKeyFactory method createKey.

/**
 * Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
 *
 * @param keyInfo the PrivateKeyInfo object containing the key material
 * @return a suitable private key parameter
 * @throws IOException on an error decoding the key
 */
public static AsymmetricKeyParameter createKey(PrivateKeyInfo keyInfo) throws IOException {
    AlgorithmIdentifier algId = keyInfo.getPrivateKeyAlgorithm();
    ASN1ObjectIdentifier algOID = algId.getAlgorithm();
    if (algOID.equals(PKCSObjectIdentifiers.rsaEncryption) || algOID.equals(PKCSObjectIdentifiers.id_RSASSA_PSS) || algOID.equals(X509ObjectIdentifiers.id_ea_rsa)) {
        RSAPrivateKey keyStructure = RSAPrivateKey.getInstance(keyInfo.parsePrivateKey());
        return new RSAPrivateCrtKeyParameters(keyStructure.getModulus(), keyStructure.getPublicExponent(), keyStructure.getPrivateExponent(), keyStructure.getPrime1(), keyStructure.getPrime2(), keyStructure.getExponent1(), keyStructure.getExponent2(), keyStructure.getCoefficient());
    } else // else if (algOID.equals(X9ObjectIdentifiers.dhpublicnumber))
    if (algOID.equals(PKCSObjectIdentifiers.dhKeyAgreement)) {
        DHParameter params = DHParameter.getInstance(algId.getParameters());
        ASN1Integer derX = (ASN1Integer) keyInfo.parsePrivateKey();
        BigInteger lVal = params.getL();
        int l = lVal == null ? 0 : lVal.intValue();
        DHParameters dhParams = new DHParameters(params.getP(), params.getG(), null, l);
        return new DHPrivateKeyParameters(derX.getValue(), dhParams);
    } else if (algOID.equals(OIWObjectIdentifiers.elGamalAlgorithm)) {
        ElGamalParameter params = ElGamalParameter.getInstance(algId.getParameters());
        ASN1Integer derX = (ASN1Integer) keyInfo.parsePrivateKey();
        return new ElGamalPrivateKeyParameters(derX.getValue(), new ElGamalParameters(params.getP(), params.getG()));
    } else if (algOID.equals(X9ObjectIdentifiers.id_dsa)) {
        ASN1Integer derX = (ASN1Integer) keyInfo.parsePrivateKey();
        ASN1Encodable algParameters = algId.getParameters();
        DSAParameters parameters = null;
        if (algParameters != null) {
            DSAParameter params = DSAParameter.getInstance(algParameters.toASN1Primitive());
            parameters = new DSAParameters(params.getP(), params.getQ(), params.getG());
        }
        return new DSAPrivateKeyParameters(derX.getValue(), parameters);
    } else if (algOID.equals(X9ObjectIdentifiers.id_ecPublicKey)) {
        X962Parameters params = X962Parameters.getInstance(algId.getParameters());
        X9ECParameters x9;
        ECDomainParameters dParams;
        if (params.isNamedCurve()) {
            ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) params.getParameters();
            x9 = CustomNamedCurves.getByOID(oid);
            if (x9 == null) {
                x9 = ECNamedCurveTable.getByOID(oid);
            }
            dParams = new ECNamedDomainParameters(oid, x9);
        } else {
            x9 = X9ECParameters.getInstance(params.getParameters());
            dParams = new ECDomainParameters(x9.getCurve(), x9.getG(), x9.getN(), x9.getH(), x9.getSeed());
        }
        ECPrivateKey ec = ECPrivateKey.getInstance(keyInfo.parsePrivateKey());
        BigInteger d = ec.getKey();
        return new ECPrivateKeyParameters(d, dParams);
    } else if (algOID.equals(EdECObjectIdentifiers.id_X25519)) {
        return new X25519PrivateKeyParameters(getRawKey(keyInfo));
    } else if (algOID.equals(EdECObjectIdentifiers.id_X448)) {
        return new X448PrivateKeyParameters(getRawKey(keyInfo));
    } else if (algOID.equals(EdECObjectIdentifiers.id_Ed25519)) {
        return new Ed25519PrivateKeyParameters(getRawKey(keyInfo));
    } else if (algOID.equals(EdECObjectIdentifiers.id_Ed448)) {
        return new Ed448PrivateKeyParameters(getRawKey(keyInfo));
    } else if (algOID.equals(CryptoProObjectIdentifiers.gostR3410_2001) || algOID.equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512) || algOID.equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256)) {
        ASN1Encodable algParameters = algId.getParameters();
        GOST3410PublicKeyAlgParameters gostParams = GOST3410PublicKeyAlgParameters.getInstance(algParameters);
        ECGOST3410Parameters ecSpec = null;
        BigInteger d = null;
        ASN1Primitive p = algParameters.toASN1Primitive();
        if (p instanceof ASN1Sequence && (ASN1Sequence.getInstance(p).size() == 2 || ASN1Sequence.getInstance(p).size() == 3)) {
            X9ECParameters ecP = ECGOST3410NamedCurves.getByOIDX9(gostParams.getPublicKeyParamSet());
            ecSpec = new ECGOST3410Parameters(new ECNamedDomainParameters(gostParams.getPublicKeyParamSet(), ecP), gostParams.getPublicKeyParamSet(), gostParams.getDigestParamSet(), gostParams.getEncryptionParamSet());
            ASN1OctetString privEnc = keyInfo.getPrivateKey();
            if (privEnc.getOctets().length == 32 || privEnc.getOctets().length == 64) {
                d = new BigInteger(1, Arrays.reverse(privEnc.getOctets()));
            } else {
                ASN1Encodable privKey = keyInfo.parsePrivateKey();
                if (privKey instanceof ASN1Integer) {
                    d = ASN1Integer.getInstance(privKey).getPositiveValue();
                } else {
                    byte[] dVal = Arrays.reverse(ASN1OctetString.getInstance(privKey).getOctets());
                    d = new BigInteger(1, dVal);
                }
            }
        } else {
            X962Parameters params = X962Parameters.getInstance(algId.getParameters());
            if (params.isNamedCurve()) {
                ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
                X9ECParameters ecP = ECNamedCurveTable.getByOID(oid);
                ecSpec = new ECGOST3410Parameters(new ECNamedDomainParameters(oid, ecP), gostParams.getPublicKeyParamSet(), gostParams.getDigestParamSet(), gostParams.getEncryptionParamSet());
            } else if (params.isImplicitlyCA()) {
                ecSpec = null;
            } else {
                X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
                ecSpec = new ECGOST3410Parameters(new ECNamedDomainParameters(algOID, ecP), gostParams.getPublicKeyParamSet(), gostParams.getDigestParamSet(), gostParams.getEncryptionParamSet());
            }
            ASN1Encodable privKey = keyInfo.parsePrivateKey();
            if (privKey instanceof ASN1Integer) {
                ASN1Integer derD = ASN1Integer.getInstance(privKey);
                d = derD.getValue();
            } else {
                ECPrivateKey ec = ECPrivateKey.getInstance(privKey);
                d = ec.getKey();
            }
        }
        return new ECPrivateKeyParameters(d, new ECGOST3410Parameters(ecSpec, gostParams.getPublicKeyParamSet(), gostParams.getDigestParamSet(), gostParams.getEncryptionParamSet()));
    } else {
        throw new RuntimeException("algorithm identifier in private key not recognised");
    }
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) DHPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) ECGOST3410Parameters(com.github.zhenwei.core.crypto.params.ECGOST3410Parameters) X25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.X25519PrivateKeyParameters) Ed448PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed448PrivateKeyParameters) Ed25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) DSAParameter(com.github.zhenwei.core.asn1.x509.DSAParameter) X448PrivateKeyParameters(com.github.zhenwei.core.crypto.params.X448PrivateKeyParameters) DHParameter(com.github.zhenwei.core.asn1.pkcs.DHParameter) ECPrivateKey(com.github.zhenwei.core.asn1.sec.ECPrivateKey) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) ElGamalPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) ElGamalParameter(com.github.zhenwei.core.asn1.oiw.ElGamalParameter) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) ElGamalParameters(com.github.zhenwei.core.crypto.params.ElGamalParameters) DSAPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DSAPrivateKeyParameters) BigInteger(java.math.BigInteger) RSAPrivateKey(com.github.zhenwei.core.asn1.pkcs.RSAPrivateKey) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) ASN1Primitive(com.github.zhenwei.core.asn1.ASN1Primitive) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) RSAPrivateCrtKeyParameters(com.github.zhenwei.core.crypto.params.RSAPrivateCrtKeyParameters)

Aggregations

Ed448PrivateKeyParameters (com.github.zhenwei.core.crypto.params.Ed448PrivateKeyParameters)4 Ed25519PrivateKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters)3 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)2 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)2 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)2 GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)2 RSAPrivateKey (com.github.zhenwei.core.asn1.pkcs.RSAPrivateKey)2 ECPrivateKey (com.github.zhenwei.core.asn1.sec.ECPrivateKey)2 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 DSAParameter (com.github.zhenwei.core.asn1.x509.DSAParameter)2 X962Parameters (com.github.zhenwei.core.asn1.x9.X962Parameters)2 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)2 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)2 DSAPrivateKeyParameters (com.github.zhenwei.core.crypto.params.DSAPrivateKeyParameters)2 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)2 ECGOST3410Parameters (com.github.zhenwei.core.crypto.params.ECGOST3410Parameters)2 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)2 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)2 RSAPrivateCrtKeyParameters (com.github.zhenwei.core.crypto.params.RSAPrivateCrtKeyParameters)2 X25519PrivateKeyParameters (com.github.zhenwei.core.crypto.params.X25519PrivateKeyParameters)2