Search in sources :

Example 1 with DHPrivateKeyParameters

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

the class KeyAgreementSpi method generatePrivateKeyParameter.

private DHPrivateKeyParameters generatePrivateKeyParameter(PrivateKey privKey) throws InvalidKeyException {
    if (privKey instanceof DHPrivateKey) {
        if (privKey instanceof BCDHPrivateKey) {
            return ((BCDHPrivateKey) privKey).engineGetKeyParameters();
        } else {
            DHPrivateKey pub = (DHPrivateKey) privKey;
            DHParameterSpec params = pub.getParams();
            return new DHPrivateKeyParameters(pub.getX(), new DHParameters(params.getP(), params.getG(), null, params.getL()));
        }
    } else {
        throw new InvalidKeyException("private key not a DHPrivateKey");
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) DHPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with DHPrivateKeyParameters

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

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        Integer paramStrength = Integers.valueOf(strength);
        if (params.containsKey(paramStrength)) {
            param = (DHKeyGenerationParameters) params.get(paramStrength);
        } else {
            DHParameterSpec dhParams = WeGooProvider.CONFIGURATION.getDHDefaultParameters(strength);
            if (dhParams != null) {
                param = convertParams(random, dhParams);
            } else {
                synchronized (lock) {
                    // our key size.
                    if (params.containsKey(paramStrength)) {
                        param = (DHKeyGenerationParameters) params.get(paramStrength);
                    } else {
                        DHParametersGenerator pGen = new DHParametersGenerator();
                        pGen.init(strength, PrimeCertaintyCalculator.getDefaultCertainty(strength), random);
                        param = new DHKeyGenerationParameters(random, pGen.generateParameters());
                        params.put(paramStrength, param);
                    }
                }
            }
        }
        engine.init(param);
        initialised = true;
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic();
    DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate();
    return new KeyPair(new BCDHPublicKey(pub), new BCDHPrivateKey(priv));
}
Also used : KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) DHPublicKeyParameters(com.github.zhenwei.core.crypto.params.DHPublicKeyParameters) DHPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters) DHKeyGenerationParameters(com.github.zhenwei.core.crypto.params.DHKeyGenerationParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec) DHParametersGenerator(com.github.zhenwei.core.crypto.generators.DHParametersGenerator) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 3 with DHPrivateKeyParameters

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

the class MQVBasicAgreement method calculateAgreement.

public BigInteger calculateAgreement(CipherParameters pubKey) {
    DHMQVPublicParameters pubParams = (DHMQVPublicParameters) pubKey;
    DHPrivateKeyParameters staticPrivateKey = privParams.getStaticPrivateKey();
    if (!privParams.getStaticPrivateKey().getParameters().equals(pubParams.getStaticPublicKey().getParameters())) {
        throw new IllegalStateException("MQV public key components have wrong domain parameters");
    }
    if (privParams.getStaticPrivateKey().getParameters().getQ() == null) {
        throw new IllegalStateException("MQV key domain parameters do not have Q set");
    }
    BigInteger agreement = calculateDHMQVAgreement(staticPrivateKey.getParameters(), staticPrivateKey, pubParams.getStaticPublicKey(), privParams.getEphemeralPrivateKey(), privParams.getEphemeralPublicKey(), pubParams.getEphemeralPublicKey());
    if (agreement.equals(ONE)) {
        throw new IllegalStateException("1 is not a valid agreement value for MQV");
    }
    return agreement;
}
Also used : DHMQVPublicParameters(com.github.zhenwei.core.crypto.params.DHMQVPublicParameters) DHPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters) BigInteger(java.math.BigInteger)

Example 4 with DHPrivateKeyParameters

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

the class DHKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
    DHParameters dhp = param.getParameters();
    BigInteger x = helper.calculatePrivate(dhp, param.getRandom());
    BigInteger y = helper.calculatePublic(dhp, x);
    return new AsymmetricCipherKeyPair(new DHPublicKeyParameters(y, dhp), new DHPrivateKeyParameters(x, dhp));
}
Also used : DHPublicKeyParameters(com.github.zhenwei.core.crypto.params.DHPublicKeyParameters) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) DHPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters) BigInteger(java.math.BigInteger) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 5 with DHPrivateKeyParameters

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

the class DHBasicKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
    DHParameters dhp = param.getParameters();
    BigInteger x = helper.calculatePrivate(dhp, param.getRandom());
    BigInteger y = helper.calculatePublic(dhp, x);
    return new AsymmetricCipherKeyPair(new DHPublicKeyParameters(y, dhp), new DHPrivateKeyParameters(x, dhp));
}
Also used : DHPublicKeyParameters(com.github.zhenwei.core.crypto.params.DHPublicKeyParameters) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) DHPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters) BigInteger(java.math.BigInteger) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Aggregations

DHPrivateKeyParameters (com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters)8 DHParameters (com.github.zhenwei.core.crypto.params.DHParameters)4 BigInteger (java.math.BigInteger)4 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)3 DHPublicKeyParameters (com.github.zhenwei.core.crypto.params.DHPublicKeyParameters)3 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)2 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)2 DHParameterSpec (javax.crypto.spec.DHParameterSpec)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