use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters 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.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class DSTU4145KeyPairGenerator method generateKeyPair.
public AsymmetricCipherKeyPair generateKeyPair() {
AsymmetricCipherKeyPair pair = super.generateKeyPair();
ECPublicKeyParameters pub = (ECPublicKeyParameters) pair.getPublic();
ECPrivateKeyParameters priv = (ECPrivateKeyParameters) pair.getPrivate();
pub = new ECPublicKeyParameters(pub.getQ().negate(), pub.getParameters());
return new AsymmetricCipherKeyPair(pub, priv);
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class ECKeyPairGenerator method generateKeyPair.
/**
* Given the domain parameters this routine generates an EC key pair in accordance with X9.62
* section 5.2.1 pages 26, 27.
*/
public AsymmetricCipherKeyPair generateKeyPair() {
BigInteger n = params.getN();
int nBitLength = n.bitLength();
int minWeight = nBitLength >>> 2;
BigInteger d;
for (; ; ) {
d = BigIntegers.createRandomBigInteger(nBitLength, random);
if (d.compareTo(ONE) < 0 || (d.compareTo(n) >= 0)) {
continue;
}
if (WNafUtil.getNafWeight(d) < minWeight) {
continue;
}
break;
}
ECPoint Q = createBasePointMultiplier().multiply(params.getG(), d);
return new AsymmetricCipherKeyPair(new ECPublicKeyParameters(Q, params), new ECPrivateKeyParameters(d, params));
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters 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);
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class KeyPairGeneratorSpi method generateKeyPair.
public KeyPair generateKeyPair() {
if (!initialised) {
throw new IllegalStateException("EC Key Pair Generator not initialised");
}
AsymmetricCipherKeyPair pair = engine.generateKeyPair();
ECPublicKeyParameters pub = (ECPublicKeyParameters) pair.getPublic();
ECPrivateKeyParameters priv = (ECPrivateKeyParameters) pair.getPrivate();
if (ecParams instanceof ECParameterSpec) {
ECParameterSpec p = (ECParameterSpec) ecParams;
BCECGOST3410PublicKey pubKey = new BCECGOST3410PublicKey(algorithm, pub, p);
return new KeyPair(pubKey, new BCECGOST3410PrivateKey(algorithm, priv, pubKey, p));
} else if (ecParams == null) {
return new KeyPair(new BCECGOST3410PublicKey(algorithm, pub), new BCECGOST3410PrivateKey(algorithm, priv));
} else {
java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;
BCECGOST3410PublicKey pubKey = new BCECGOST3410PublicKey(algorithm, pub, p);
return new KeyPair(pubKey, new BCECGOST3410PrivateKey(algorithm, priv, pubKey, p));
}
}
Aggregations