use of com.github.zhenwei.core.crypto.params.ECDHUPublicParameters in project LinLong-Java by zhenwei1108.
the class ECDHCUnifiedAgreement method calculateAgreement.
public byte[] calculateAgreement(CipherParameters pubKey) {
ECDHUPublicParameters pubParams = (ECDHUPublicParameters) pubKey;
ECDHCBasicAgreement sAgree = new ECDHCBasicAgreement();
ECDHCBasicAgreement eAgree = new ECDHCBasicAgreement();
sAgree.init(privParams.getStaticPrivateKey());
BigInteger sComp = sAgree.calculateAgreement(pubParams.getStaticPublicKey());
eAgree.init(privParams.getEphemeralPrivateKey());
BigInteger eComp = eAgree.calculateAgreement(pubParams.getEphemeralPublicKey());
int fieldSize = getFieldSize();
byte[] result = new byte[fieldSize * 2];
BigIntegers.asUnsignedByteArray(eComp, result, 0, fieldSize);
BigIntegers.asUnsignedByteArray(sComp, result, fieldSize, fieldSize);
return result;
}
use of com.github.zhenwei.core.crypto.params.ECDHUPublicParameters 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;
}
Aggregations