Search in sources :

Example 1 with ECMQVBasicAgreement

use of com.github.zhenwei.core.crypto.agreement.ECMQVBasicAgreement in project LinLong-Java by zhenwei1108.

the class KeyAgreementSpi method engineDoPhase.

protected Key engineDoPhase(Key key, boolean lastPhase) throws InvalidKeyException, IllegalStateException {
    if (parameters == null) {
        throw new IllegalStateException(kaAlgorithm + " not initialised.");
    }
    if (!lastPhase) {
        throw new IllegalStateException(kaAlgorithm + " can only be between two parties.");
    }
    CipherParameters pubKey;
    if (agreement instanceof ECMQVBasicAgreement) {
        if (!(key instanceof MQVPublicKey)) {
            ECPublicKeyParameters staticKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter((PublicKey) key);
            ECPublicKeyParameters ephemKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvParameters.getOtherPartyEphemeralKey());
            pubKey = new MQVPublicParameters(staticKey, ephemKey);
        } else {
            MQVPublicKey mqvPubKey = (MQVPublicKey) key;
            ECPublicKeyParameters staticKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvPubKey.getStaticKey());
            ECPublicKeyParameters ephemKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvPubKey.getEphemeralKey());
            pubKey = new MQVPublicParameters(staticKey, ephemKey);
        }
    } else if (agreement instanceof ECDHCUnifiedAgreement) {
        ECPublicKeyParameters staticKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter((PublicKey) key);
        ECPublicKeyParameters ephemKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(dheParameters.getOtherPartyEphemeralKey());
        pubKey = new ECDHUPublicParameters(staticKey, ephemKey);
    } else {
        if (!(key instanceof PublicKey)) {
            throw new InvalidKeyException(kaAlgorithm + " key agreement requires " + getSimpleName(ECPublicKey.class) + " for doPhase");
        }
        pubKey = ECUtils.generatePublicKeyParameter((PublicKey) key);
    }
    try {
        if (agreement instanceof BasicAgreement) {
            result = bigIntToBytes(((BasicAgreement) agreement).calculateAgreement(pubKey));
        } else {
            result = ((ECDHCUnifiedAgreement) agreement).calculateAgreement(pubKey);
        }
    } catch (final Exception e) {
        throw new InvalidKeyException("calculation failed: " + e.getMessage()) {

            public Throwable getCause() {
                return e;
            }
        };
    }
    return null;
}
Also used : ECDHCUnifiedAgreement(com.github.zhenwei.core.crypto.agreement.ECDHCUnifiedAgreement) ECDHUPublicParameters(com.github.zhenwei.core.crypto.params.ECDHUPublicParameters) MQVPublicKey(com.github.zhenwei.provider.jce.interfaces.MQVPublicKey) MQVPublicKey(com.github.zhenwei.provider.jce.interfaces.MQVPublicKey) PublicKey(java.security.PublicKey) ECPublicKey(com.github.zhenwei.provider.jce.interfaces.ECPublicKey) InvalidKeyException(java.security.InvalidKeyException) ECDHCBasicAgreement(com.github.zhenwei.core.crypto.agreement.ECDHCBasicAgreement) BasicAgreement(com.github.zhenwei.core.crypto.BasicAgreement) ECDHBasicAgreement(com.github.zhenwei.core.crypto.agreement.ECDHBasicAgreement) ECMQVBasicAgreement(com.github.zhenwei.core.crypto.agreement.ECMQVBasicAgreement) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidKeyException(java.security.InvalidKeyException) ECMQVBasicAgreement(com.github.zhenwei.core.crypto.agreement.ECMQVBasicAgreement) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) MQVPublicParameters(com.github.zhenwei.core.crypto.params.MQVPublicParameters)

Example 2 with ECMQVBasicAgreement

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

ECDHCUnifiedAgreement (com.github.zhenwei.core.crypto.agreement.ECDHCUnifiedAgreement)2 ECMQVBasicAgreement (com.github.zhenwei.core.crypto.agreement.ECMQVBasicAgreement)2 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 BasicAgreement (com.github.zhenwei.core.crypto.BasicAgreement)1 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)1 ECDHBasicAgreement (com.github.zhenwei.core.crypto.agreement.ECDHBasicAgreement)1 ECDHCBasicAgreement (com.github.zhenwei.core.crypto.agreement.ECDHCBasicAgreement)1 ECDHUPrivateParameters (com.github.zhenwei.core.crypto.params.ECDHUPrivateParameters)1 ECDHUPublicParameters (com.github.zhenwei.core.crypto.params.ECDHUPublicParameters)1 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)1 MQVPrivateParameters (com.github.zhenwei.core.crypto.params.MQVPrivateParameters)1 MQVPublicParameters (com.github.zhenwei.core.crypto.params.MQVPublicParameters)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 ECPrivateKey (com.github.zhenwei.provider.jce.interfaces.ECPrivateKey)1 ECPublicKey (com.github.zhenwei.provider.jce.interfaces.ECPublicKey)1 MQVPrivateKey (com.github.zhenwei.provider.jce.interfaces.MQVPrivateKey)1