Search in sources :

Example 1 with ElGamalPrivateKeyParameters

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

the class ElGamalEngine method processBlock.

/**
 * Process a single block using the basic ElGamal algorithm.
 *
 * @param in    the input array.
 * @param inOff the offset into the input buffer where the data starts.
 * @param inLen the length of the data to be processed.
 * @return the result of the ElGamal process.
 * @throws DataLengthException the input block is too large.
 */
public byte[] processBlock(byte[] in, int inOff, int inLen) {
    if (key == null) {
        throw new IllegalStateException("ElGamal engine not initialised");
    }
    int maxLength = forEncryption ? (bitSize - 1 + 7) / 8 : getInputBlockSize();
    if (inLen > maxLength) {
        throw new DataLengthException("input too large for ElGamal cipher.\n");
    }
    BigInteger p = key.getParameters().getP();
    if (// decryption
    key instanceof ElGamalPrivateKeyParameters) {
        byte[] in1 = new byte[inLen / 2];
        byte[] in2 = new byte[inLen / 2];
        System.arraycopy(in, inOff, in1, 0, in1.length);
        System.arraycopy(in, inOff + in1.length, in2, 0, in2.length);
        BigInteger gamma = new BigInteger(1, in1);
        BigInteger phi = new BigInteger(1, in2);
        ElGamalPrivateKeyParameters priv = (ElGamalPrivateKeyParameters) key;
        // a shortcut, which generally relies on p being prime amongst other things.
        // if a problem with this shows up, check the p and g values!
        BigInteger m = gamma.modPow(p.subtract(ONE).subtract(priv.getX()), p).multiply(phi).mod(p);
        return BigIntegers.asUnsignedByteArray(m);
    } else // encryption
    {
        byte[] block;
        if (inOff != 0 || inLen != in.length) {
            block = new byte[inLen];
            System.arraycopy(in, inOff, block, 0, inLen);
        } else {
            block = in;
        }
        BigInteger input = new BigInteger(1, block);
        if (input.compareTo(p) >= 0) {
            throw new DataLengthException("input too large for ElGamal cipher.\n");
        }
        ElGamalPublicKeyParameters pub = (ElGamalPublicKeyParameters) key;
        int pBitLength = p.bitLength();
        BigInteger k = BigIntegers.createRandomBigInteger(pBitLength, random);
        while (k.equals(ZERO) || (k.compareTo(p.subtract(TWO)) > 0)) {
            k = BigIntegers.createRandomBigInteger(pBitLength, random);
        }
        BigInteger g = key.getParameters().getG();
        BigInteger gamma = g.modPow(k, p);
        BigInteger phi = input.multiply(pub.getY().modPow(k, p)).mod(p);
        byte[] out1 = gamma.toByteArray();
        byte[] out2 = phi.toByteArray();
        byte[] output = new byte[this.getOutputBlockSize()];
        if (out1.length > output.length / 2) {
            System.arraycopy(out1, 1, output, output.length / 2 - (out1.length - 1), out1.length - 1);
        } else {
            System.arraycopy(out1, 0, output, output.length / 2 - out1.length, out1.length);
        }
        if (out2.length > output.length / 2) {
            System.arraycopy(out2, 1, output, output.length - (out2.length - 1), out2.length - 1);
        } else {
            System.arraycopy(out2, 0, output, output.length - out2.length, out2.length);
        }
        return output;
    }
}
Also used : ElGamalPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters) DataLengthException(com.github.zhenwei.core.crypto.DataLengthException) BigInteger(java.math.BigInteger) ElGamalPublicKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPublicKeyParameters)

Example 2 with ElGamalPrivateKeyParameters

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

the class ElGamalKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
    ElGamalParameters egp = param.getParameters();
    DHParameters dhp = new DHParameters(egp.getP(), egp.getG(), null, egp.getL());
    BigInteger x = helper.calculatePrivate(dhp, param.getRandom());
    BigInteger y = helper.calculatePublic(dhp, x);
    return new AsymmetricCipherKeyPair(new ElGamalPublicKeyParameters(y, egp), new ElGamalPrivateKeyParameters(x, egp));
}
Also used : ElGamalParameters(com.github.zhenwei.core.crypto.params.ElGamalParameters) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) ElGamalPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters) BigInteger(java.math.BigInteger) ElGamalPublicKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPublicKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 3 with ElGamalPrivateKeyParameters

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

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        DHParameterSpec dhParams = WeGooProvider.CONFIGURATION.getDHDefaultParameters(strength);
        if (dhParams != null) {
            param = new ElGamalKeyGenerationParameters(random, new ElGamalParameters(dhParams.getP(), dhParams.getG(), dhParams.getL()));
        } else {
            ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
            pGen.init(strength, certainty, random);
            param = new ElGamalKeyGenerationParameters(random, pGen.generateParameters());
        }
        engine.init(param);
        initialised = true;
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    ElGamalPublicKeyParameters pub = (ElGamalPublicKeyParameters) pair.getPublic();
    ElGamalPrivateKeyParameters priv = (ElGamalPrivateKeyParameters) pair.getPrivate();
    return new KeyPair(new BCElGamalPublicKey(pub), new BCElGamalPrivateKey(priv));
}
Also used : KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) ElGamalParameters(com.github.zhenwei.core.crypto.params.ElGamalParameters) ElGamalPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters) ElGamalKeyGenerationParameters(com.github.zhenwei.core.crypto.params.ElGamalKeyGenerationParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec) ElGamalPublicKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPublicKeyParameters) ElGamalParametersGenerator(com.github.zhenwei.core.crypto.generators.ElGamalParametersGenerator) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 4 with ElGamalPrivateKeyParameters

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

the class ElGamalEngine method init.

/**
 * initialise the ElGamal engine.
 *
 * @param forEncryption true if we are encrypting, false otherwise.
 * @param param         the necessary ElGamal key parameters.
 */
public void init(boolean forEncryption, CipherParameters param) {
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom p = (ParametersWithRandom) param;
        this.key = (ElGamalKeyParameters) p.getParameters();
        this.random = p.getRandom();
    } else {
        this.key = (ElGamalKeyParameters) param;
        this.random = CryptoServicesRegistrar.getSecureRandom();
    }
    this.forEncryption = forEncryption;
    BigInteger p = key.getParameters().getP();
    bitSize = p.bitLength();
    if (forEncryption) {
        if (!(key instanceof ElGamalPublicKeyParameters)) {
            throw new IllegalArgumentException("ElGamalPublicKeyParameters are required for encryption.");
        }
    } else {
        if (!(key instanceof ElGamalPrivateKeyParameters)) {
            throw new IllegalArgumentException("ElGamalPrivateKeyParameters are required for decryption.");
        }
    }
}
Also used : ElGamalPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) BigInteger(java.math.BigInteger) ElGamalPublicKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPublicKeyParameters)

Example 5 with ElGamalPrivateKeyParameters

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

ElGamalPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters)5 ElGamalPublicKeyParameters (com.github.zhenwei.core.crypto.params.ElGamalPublicKeyParameters)4 BigInteger (java.math.BigInteger)4 ElGamalParameters (com.github.zhenwei.core.crypto.params.ElGamalParameters)3 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)2 DHParameters (com.github.zhenwei.core.crypto.params.DHParameters)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 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)1 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)1 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)1 GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)1 ElGamalParameter (com.github.zhenwei.core.asn1.oiw.ElGamalParameter)1 DHParameter (com.github.zhenwei.core.asn1.pkcs.DHParameter)1 RSAPrivateKey (com.github.zhenwei.core.asn1.pkcs.RSAPrivateKey)1 ECPrivateKey (com.github.zhenwei.core.asn1.sec.ECPrivateKey)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 DSAParameter (com.github.zhenwei.core.asn1.x509.DSAParameter)1 X962Parameters (com.github.zhenwei.core.asn1.x9.X962Parameters)1