Search in sources :

Example 1 with MQVPublicParameters

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

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

the class ECMQVBasicAgreement method calculateAgreement.

public BigInteger calculateAgreement(CipherParameters pubKey) {
    if (Properties.isOverrideSet("org.bouncycastle.ec.disable_mqv")) {
        throw new IllegalStateException("ECMQV explicitly disabled");
    }
    MQVPublicParameters pubParams = (MQVPublicParameters) pubKey;
    ECPrivateKeyParameters staticPrivateKey = privParams.getStaticPrivateKey();
    ECDomainParameters parameters = staticPrivateKey.getParameters();
    if (!parameters.equals(pubParams.getStaticPublicKey().getParameters())) {
        throw new IllegalStateException("ECMQV public key components have wrong domain parameters");
    }
    ECPoint agreement = calculateMqvAgreement(parameters, staticPrivateKey, privParams.getEphemeralPrivateKey(), privParams.getEphemeralPublicKey(), pubParams.getStaticPublicKey(), pubParams.getEphemeralPublicKey()).normalize();
    if (agreement.isInfinity()) {
        throw new IllegalStateException("Infinity is not a valid agreement value for MQV");
    }
    return agreement.getAffineXCoord().toBigInteger();
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) MQVPublicParameters(com.github.zhenwei.core.crypto.params.MQVPublicParameters)

Aggregations

MQVPublicParameters (com.github.zhenwei.core.crypto.params.MQVPublicParameters)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 ECDHCUnifiedAgreement (com.github.zhenwei.core.crypto.agreement.ECDHCUnifiedAgreement)1 ECMQVBasicAgreement (com.github.zhenwei.core.crypto.agreement.ECMQVBasicAgreement)1 ECDHUPublicParameters (com.github.zhenwei.core.crypto.params.ECDHUPublicParameters)1 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)1 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)1 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)1 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)1 ECPublicKey (com.github.zhenwei.provider.jce.interfaces.ECPublicKey)1 MQVPublicKey (com.github.zhenwei.provider.jce.interfaces.MQVPublicKey)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 PublicKey (java.security.PublicKey)1