use of com.github.zhenwei.core.crypto.params.ECDomainParameters in project LinLong-Java by zhenwei1108.
the class SM2KeyExchange method calculateU.
private ECPoint calculateU(SM2KeyExchangePublicParameters otherPub) {
ECDomainParameters params = staticKey.getParameters();
ECPoint p1 = ECAlgorithms.cleanPoint(params.getCurve(), otherPub.getStaticPublicKey().getQ());
ECPoint p2 = ECAlgorithms.cleanPoint(params.getCurve(), otherPub.getEphemeralPublicKey().getQ());
BigInteger x1 = reduce(ephemeralPubPoint.getAffineXCoord().toBigInteger());
BigInteger x2 = reduce(p2.getAffineXCoord().toBigInteger());
BigInteger tA = staticKey.getD().add(x1.multiply(ephemeralKey.getD()));
BigInteger k1 = ecParams.getH().multiply(tA).mod(ecParams.getN());
BigInteger k2 = k1.multiply(x2).mod(ecParams.getN());
return ECAlgorithms.sumOfTwoMultiplies(p1, k1, p2, k2).normalize();
}
use of com.github.zhenwei.core.crypto.params.ECDomainParameters in project LinLong-Java by zhenwei1108.
the class ECDHBasicAgreement method calculateAgreement.
public BigInteger calculateAgreement(CipherParameters pubKey) {
ECPublicKeyParameters pub = (ECPublicKeyParameters) pubKey;
ECDomainParameters params = key.getParameters();
if (!params.equals(pub.getParameters())) {
throw new IllegalStateException("ECDH public key has wrong domain parameters");
}
BigInteger d = key.getD();
// Always perform calculations on the exact curve specified by our private key's parameters
ECPoint Q = ECAlgorithms.cleanPoint(params.getCurve(), pub.getQ());
if (Q.isInfinity()) {
throw new IllegalStateException("Infinity is not a valid public key for ECDH");
}
BigInteger h = params.getH();
if (!h.equals(ECConstants.ONE)) {
d = params.getHInv().multiply(d).mod(params.getN());
Q = ECAlgorithms.referenceMultiply(Q, h);
}
ECPoint P = Q.multiply(d).normalize();
if (P.isInfinity()) {
throw new IllegalStateException("Infinity is not a valid agreement value for ECDH");
}
return P.getAffineXCoord().toBigInteger();
}
use of com.github.zhenwei.core.crypto.params.ECDomainParameters in project LinLong-Java by zhenwei1108.
the class ECDHCBasicAgreement method calculateAgreement.
public BigInteger calculateAgreement(CipherParameters pubKey) {
ECPublicKeyParameters pub = (ECPublicKeyParameters) pubKey;
ECDomainParameters params = key.getParameters();
if (!params.equals(pub.getParameters())) {
throw new IllegalStateException("ECDHC public key has wrong domain parameters");
}
BigInteger hd = params.getH().multiply(key.getD()).mod(params.getN());
// Always perform calculations on the exact curve specified by our private key's parameters
ECPoint pubPoint = ECAlgorithms.cleanPoint(params.getCurve(), pub.getQ());
if (pubPoint.isInfinity()) {
throw new IllegalStateException("Infinity is not a valid public key for ECDHC");
}
ECPoint P = pubPoint.multiply(hd).normalize();
if (P.isInfinity()) {
throw new IllegalStateException("Infinity is not a valid agreement value for ECDHC");
}
return P.getAffineXCoord().toBigInteger();
}
use of com.github.zhenwei.core.crypto.params.ECDomainParameters in project LinLong-Java by zhenwei1108.
the class ECDHCStagedAgreement method calculateNextPoint.
private ECPoint calculateNextPoint(ECPublicKeyParameters pubKey) {
ECPublicKeyParameters pub = pubKey;
ECDomainParameters params = key.getParameters();
if (!params.equals(pub.getParameters())) {
throw new IllegalStateException("ECDHC public key has wrong domain parameters");
}
BigInteger hd = params.getH().multiply(key.getD()).mod(params.getN());
// Always perform calculations on the exact curve specified by our private key's parameters
ECPoint pubPoint = ECAlgorithms.cleanPoint(params.getCurve(), pub.getQ());
if (pubPoint.isInfinity()) {
throw new IllegalStateException("Infinity is not a valid public key for ECDHC");
}
ECPoint P = pubPoint.multiply(hd).normalize();
if (P.isInfinity()) {
throw new IllegalStateException("Infinity is not a valid agreement value for ECDHC");
}
return P;
}
use of com.github.zhenwei.core.crypto.params.ECDomainParameters in project LinLong-Java by zhenwei1108.
the class ECIESKeyEncapsulation method encrypt.
/**
* Generate and encapsulate a random session key.
*
* @param out the output buffer for the encapsulated key.
* @param outOff the offset for the output buffer.
* @param keyLen the length of the session key.
* @return the random session key.
*/
public CipherParameters encrypt(byte[] out, int outOff, int keyLen) throws IllegalArgumentException {
if (!(key instanceof ECPublicKeyParameters)) {
throw new IllegalArgumentException("Public key required for encryption");
}
ECPublicKeyParameters ecPubKey = (ECPublicKeyParameters) key;
ECDomainParameters ecParams = ecPubKey.getParameters();
ECCurve curve = ecParams.getCurve();
BigInteger n = ecParams.getN();
BigInteger h = ecParams.getH();
// Generate the ephemeral key pair
BigInteger r = BigIntegers.createRandomInRange(ONE, n, rnd);
// Compute the static-ephemeral key agreement
BigInteger rPrime = OldCofactorMode ? r.multiply(h).mod(n) : r;
ECMultiplier basePointMultiplier = createBasePointMultiplier();
ECPoint[] ghTilde = new ECPoint[] { basePointMultiplier.multiply(ecParams.getG(), r), ecPubKey.getQ().multiply(rPrime) };
// NOTE: More efficient than normalizing each individually
curve.normalizeAll(ghTilde);
ECPoint gTilde = ghTilde[0], hTilde = ghTilde[1];
// Encode the ephemeral public key
byte[] C = gTilde.getEncoded(false);
System.arraycopy(C, 0, out, outOff, C.length);
// Encode the shared secret value
byte[] PEH = hTilde.getAffineXCoord().getEncoded();
return deriveKey(keyLen, C, PEH);
}
Aggregations