Search in sources :

Example 1 with DHUPrivateParameters

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

the class KeyAgreementSpi method engineInit.

protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (!(key instanceof DHPrivateKey)) {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
    }
    DHPrivateKey privKey = (DHPrivateKey) key;
    if (params != null) {
        if (// p, g override.
        params instanceof DHParameterSpec) {
            DHParameterSpec p = (DHParameterSpec) params;
            this.p = p.getP();
            this.g = p.getG();
            this.dheParameters = null;
            this.ukmParameters = null;
        } else if (params instanceof DHUParameterSpec) {
            if (unifiedAgreement == null) {
                throw new InvalidAlgorithmParameterException("agreement algorithm not DHU based");
            }
            this.p = privKey.getParams().getP();
            this.g = privKey.getParams().getG();
            this.dheParameters = (DHUParameterSpec) params;
            this.ukmParameters = ((DHUParameterSpec) params).getUserKeyingMaterial();
            if (dheParameters.getEphemeralPublicKey() != null) {
                unifiedAgreement.init(new DHUPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(dheParameters.getEphemeralPrivateKey()), generatePublicKeyParameter(dheParameters.getEphemeralPublicKey())));
            } else {
                unifiedAgreement.init(new DHUPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(dheParameters.getEphemeralPrivateKey())));
            }
        } else if (params instanceof MQVParameterSpec) {
            if (mqvAgreement == null) {
                throw new InvalidAlgorithmParameterException("agreement algorithm not MQV based");
            }
            this.p = privKey.getParams().getP();
            this.g = privKey.getParams().getG();
            this.mqvParameters = (MQVParameterSpec) params;
            this.ukmParameters = ((MQVParameterSpec) params).getUserKeyingMaterial();
            if (mqvParameters.getEphemeralPublicKey() != null) {
                mqvAgreement.init(new DHMQVPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(mqvParameters.getEphemeralPrivateKey()), generatePublicKeyParameter(mqvParameters.getEphemeralPublicKey())));
            } else {
                mqvAgreement.init(new DHMQVPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(mqvParameters.getEphemeralPrivateKey())));
            }
        } else if (params instanceof UserKeyingMaterialSpec) {
            if (kdf == null) {
                throw new InvalidAlgorithmParameterException("no KDF specified for UserKeyingMaterialSpec");
            }
            this.p = privKey.getParams().getP();
            this.g = privKey.getParams().getG();
            this.dheParameters = null;
            this.ukmParameters = ((UserKeyingMaterialSpec) params).getUserKeyingMaterial();
        } else {
            throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
        }
    } else {
        this.p = privKey.getParams().getP();
        this.g = privKey.getParams().getG();
    }
    this.x = privKey.getX();
    this.result = bigIntToBytes(x);
}
Also used : DHMQVPrivateParameters(com.github.zhenwei.core.crypto.params.DHMQVPrivateParameters) DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DHUPrivateParameters(com.github.zhenwei.core.crypto.params.DHUPrivateParameters) DHUParameterSpec(com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec) DHParameterSpec(javax.crypto.spec.DHParameterSpec) InvalidKeyException(java.security.InvalidKeyException) MQVParameterSpec(com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec) UserKeyingMaterialSpec(com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec)

Aggregations

DHMQVPrivateParameters (com.github.zhenwei.core.crypto.params.DHMQVPrivateParameters)1 DHUPrivateParameters (com.github.zhenwei.core.crypto.params.DHUPrivateParameters)1 DHUParameterSpec (com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec)1 MQVParameterSpec (com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec)1 UserKeyingMaterialSpec (com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 DHPrivateKey (javax.crypto.interfaces.DHPrivateKey)1 DHParameterSpec (javax.crypto.spec.DHParameterSpec)1