Search in sources :

Example 1 with DHUParameterSpec

use of com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec 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)

Example 2 with DHUParameterSpec

use of com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec in project LinLong-Java by zhenwei1108.

the class KeyAgreementSpi method engineInit.

protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AsymmetricKeyParameter priv = getLwXDHKeyPrivate(key);
    if (priv instanceof X25519PrivateKeyParameters) {
        agreement = getAgreement("X25519");
    } else if (priv instanceof X448PrivateKeyParameters) {
        agreement = getAgreement("X448");
    } else {
        throw new IllegalStateException("unsupported private key type");
    }
    ukmParameters = null;
    if (params instanceof DHUParameterSpec) {
        if (kaAlgorithm.indexOf('U') < 0) {
            throw new InvalidAlgorithmParameterException("agreement algorithm not DHU based");
        }
        dhuSpec = (DHUParameterSpec) params;
        ukmParameters = dhuSpec.getUserKeyingMaterial();
        agreement.init(new XDHUPrivateParameters(priv, ((BCXDHPrivateKey) dhuSpec.getEphemeralPrivateKey()).engineGetKeyParameters(), ((BCXDHPublicKey) dhuSpec.getEphemeralPublicKey()).engineGetKeyParameters()));
    } else {
        agreement.init(priv);
        if (params instanceof UserKeyingMaterialSpec) {
            if (kdf == null) {
                throw new InvalidAlgorithmParameterException("no KDF specified for UserKeyingMaterialSpec");
            }
            this.ukmParameters = ((UserKeyingMaterialSpec) params).getUserKeyingMaterial();
        } else {
            throw new InvalidAlgorithmParameterException("unknown ParameterSpec");
        }
    }
    if (kdf != null && ukmParameters == null) {
        ukmParameters = new byte[0];
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) DHUParameterSpec(com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec) X25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.X25519PrivateKeyParameters) X448PrivateKeyParameters(com.github.zhenwei.core.crypto.params.X448PrivateKeyParameters) XDHUPrivateParameters(com.github.zhenwei.core.crypto.params.XDHUPrivateParameters) UserKeyingMaterialSpec(com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec)

Example 3 with DHUParameterSpec

use of com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec in project LinLong-Java by zhenwei1108.

the class KeyAgreementSpi method initFromKey.

private void initFromKey(Key key, AlgorithmParameterSpec parameterSpec) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (agreement instanceof ECMQVBasicAgreement) {
        mqvParameters = null;
        if (!(key instanceof MQVPrivateKey) && !(parameterSpec instanceof MQVParameterSpec)) {
            throw new InvalidAlgorithmParameterException(kaAlgorithm + " key agreement requires " + getSimpleName(MQVParameterSpec.class) + " for initialisation");
        }
        ECPrivateKeyParameters staticPrivKey;
        ECPrivateKeyParameters ephemPrivKey;
        ECPublicKeyParameters ephemPubKey;
        if (key instanceof MQVPrivateKey) {
            MQVPrivateKey mqvPrivKey = (MQVPrivateKey) key;
            staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvPrivKey.getStaticPrivateKey());
            ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvPrivKey.getEphemeralPrivateKey());
            ephemPubKey = null;
            if (mqvPrivKey.getEphemeralPublicKey() != null) {
                ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvPrivKey.getEphemeralPublicKey());
            }
        } else {
            MQVParameterSpec mqvParameterSpec = (MQVParameterSpec) parameterSpec;
            staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
            ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvParameterSpec.getEphemeralPrivateKey());
            ephemPubKey = null;
            if (mqvParameterSpec.getEphemeralPublicKey() != null) {
                ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvParameterSpec.getEphemeralPublicKey());
            }
            mqvParameters = mqvParameterSpec;
            ukmParameters = mqvParameterSpec.getUserKeyingMaterial();
        }
        MQVPrivateParameters localParams = new MQVPrivateParameters(staticPrivKey, ephemPrivKey, ephemPubKey);
        this.parameters = staticPrivKey.getParameters();
        // TODO Validate that all the keys are using the same parameters?
        ((ECMQVBasicAgreement) agreement).init(localParams);
    } else if (parameterSpec instanceof DHUParameterSpec) {
        if (!(agreement instanceof ECDHCUnifiedAgreement)) {
            throw new InvalidAlgorithmParameterException(kaAlgorithm + " key agreement cannot be used with " + getSimpleName(DHUParameterSpec.class));
        }
        DHUParameterSpec dheParameterSpec = (DHUParameterSpec) parameterSpec;
        ECPrivateKeyParameters staticPrivKey;
        ECPrivateKeyParameters ephemPrivKey;
        ECPublicKeyParameters ephemPubKey;
        staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
        ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(dheParameterSpec.getEphemeralPrivateKey());
        ephemPubKey = null;
        if (dheParameterSpec.getEphemeralPublicKey() != null) {
            ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(dheParameterSpec.getEphemeralPublicKey());
        }
        dheParameters = dheParameterSpec;
        ukmParameters = dheParameterSpec.getUserKeyingMaterial();
        ECDHUPrivateParameters localParams = new ECDHUPrivateParameters(staticPrivKey, ephemPrivKey, ephemPubKey);
        this.parameters = staticPrivKey.getParameters();
        ((ECDHCUnifiedAgreement) agreement).init(localParams);
    } else {
        if (!(key instanceof PrivateKey)) {
            throw new InvalidKeyException(kaAlgorithm + " key agreement requires " + getSimpleName(ECPrivateKey.class) + " for initialisation");
        }
        if (kdf == null && parameterSpec instanceof UserKeyingMaterialSpec) {
            throw new InvalidAlgorithmParameterException("no KDF specified for UserKeyingMaterialSpec");
        }
        ECPrivateKeyParameters privKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
        this.parameters = privKey.getParameters();
        ukmParameters = (parameterSpec instanceof UserKeyingMaterialSpec) ? ((UserKeyingMaterialSpec) parameterSpec).getUserKeyingMaterial() : null;
        ((BasicAgreement) agreement).init(privKey);
    }
}
Also used : ECDHCUnifiedAgreement(com.github.zhenwei.core.crypto.agreement.ECDHCUnifiedAgreement) ECDHUPrivateParameters(com.github.zhenwei.core.crypto.params.ECDHUPrivateParameters) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ECPrivateKey(com.github.zhenwei.provider.jce.interfaces.ECPrivateKey) MQVPrivateKey(com.github.zhenwei.provider.jce.interfaces.MQVPrivateKey) PrivateKey(java.security.PrivateKey) DHUParameterSpec(com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec) InvalidKeyException(java.security.InvalidKeyException) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) UserKeyingMaterialSpec(com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec) ECMQVBasicAgreement(com.github.zhenwei.core.crypto.agreement.ECMQVBasicAgreement) ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) MQVParameterSpec(com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec) MQVPrivateKey(com.github.zhenwei.provider.jce.interfaces.MQVPrivateKey) MQVPrivateParameters(com.github.zhenwei.core.crypto.params.MQVPrivateParameters)

Aggregations

DHUParameterSpec (com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec)3 UserKeyingMaterialSpec (com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 MQVParameterSpec (com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec)2 InvalidKeyException (java.security.InvalidKeyException)2 ECDHCUnifiedAgreement (com.github.zhenwei.core.crypto.agreement.ECDHCUnifiedAgreement)1 ECMQVBasicAgreement (com.github.zhenwei.core.crypto.agreement.ECMQVBasicAgreement)1 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)1 DHMQVPrivateParameters (com.github.zhenwei.core.crypto.params.DHMQVPrivateParameters)1 DHUPrivateParameters (com.github.zhenwei.core.crypto.params.DHUPrivateParameters)1 ECDHUPrivateParameters (com.github.zhenwei.core.crypto.params.ECDHUPrivateParameters)1 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)1 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)1 MQVPrivateParameters (com.github.zhenwei.core.crypto.params.MQVPrivateParameters)1 X25519PrivateKeyParameters (com.github.zhenwei.core.crypto.params.X25519PrivateKeyParameters)1 X448PrivateKeyParameters (com.github.zhenwei.core.crypto.params.X448PrivateKeyParameters)1 XDHUPrivateParameters (com.github.zhenwei.core.crypto.params.XDHUPrivateParameters)1 ECPrivateKey (com.github.zhenwei.provider.jce.interfaces.ECPrivateKey)1 MQVPrivateKey (com.github.zhenwei.provider.jce.interfaces.MQVPrivateKey)1 PrivateKey (java.security.PrivateKey)1