use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class ECElGamalEncryptor method init.
/**
* initialise the encryptor.
*
* @param param the necessary EC key parameters.
*/
public void init(CipherParameters param) {
if (param instanceof ParametersWithRandom) {
ParametersWithRandom p = (ParametersWithRandom) param;
if (!(p.getParameters() instanceof ECPublicKeyParameters)) {
throw new IllegalArgumentException("ECPublicKeyParameters are required for encryption.");
}
this.key = (ECPublicKeyParameters) p.getParameters();
this.random = p.getRandom();
} else {
if (!(param instanceof ECPublicKeyParameters)) {
throw new IllegalArgumentException("ECPublicKeyParameters are required for encryption.");
}
this.key = (ECPublicKeyParameters) param;
this.random = CryptoServicesRegistrar.getSecureRandom();
}
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class ECNewPublicKeyTransform method init.
/**
* initialise the EC Elgamal engine.
*
* @param param the necessary EC key parameters.
*/
public void init(CipherParameters param) {
if (param instanceof ParametersWithRandom) {
ParametersWithRandom p = (ParametersWithRandom) param;
if (!(p.getParameters() instanceof ECPublicKeyParameters)) {
throw new IllegalArgumentException("ECPublicKeyParameters are required for new public key transform.");
}
this.key = (ECPublicKeyParameters) p.getParameters();
this.random = p.getRandom();
} else {
if (!(param instanceof ECPublicKeyParameters)) {
throw new IllegalArgumentException("ECPublicKeyParameters are required for new public key transform.");
}
this.key = (ECPublicKeyParameters) param;
this.random = CryptoServicesRegistrar.getSecureRandom();
}
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class ECVKOAgreement method calculateAgreement.
public byte[] calculateAgreement(CipherParameters pubKey) {
ECPublicKeyParameters pub = (ECPublicKeyParameters) pubKey;
ECDomainParameters params = key.getParameters();
if (!params.equals(pub.getParameters())) {
throw new IllegalStateException("ECVKO public key has wrong domain parameters");
}
BigInteger hd = params.getH().multiply(ukm).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 ECVKO");
}
return fromPoint(P);
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class ECNRSigner method verifySignature.
// Section 7.2.6 ECVP-NR, pg 35
/**
* return true if the value r and s represent a signature for the message passed in. Generally,
* the order of the curve should be at least as long as the hash of the message of interest, and
* with ECNR, it *must* be at least as long. But just in case the signer applied mod(n) to the
* longer digest, this implementation will apply mod(n) during verification.
*
* @param digest the digest to be verified.
* @param r the r value of the signature.
* @param s the s value of the signature.
* @throws DataLengthException if the digest is longer than the key allows
*/
public boolean verifySignature(byte[] digest, BigInteger r, BigInteger s) {
if (this.forSigning) {
throw new IllegalStateException("not initialised for verifying");
}
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) key;
BigInteger n = pubKey.getParameters().getN();
int nBitLength = n.bitLength();
BigInteger e = new BigInteger(1, digest);
int eBitLength = e.bitLength();
if (eBitLength > nBitLength) {
throw new DataLengthException("input too large for ECNR key.");
}
BigInteger t = extractT(pubKey, r, s);
return t != null && t.equals(e.mod(n));
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class DSTU4145Signer method verifySignature.
public boolean verifySignature(byte[] message, BigInteger r, BigInteger s) {
if (r.signum() <= 0 || s.signum() <= 0) {
return false;
}
ECDomainParameters parameters = key.getParameters();
BigInteger n = parameters.getN();
if (r.compareTo(n) >= 0 || s.compareTo(n) >= 0) {
return false;
}
ECCurve curve = parameters.getCurve();
ECFieldElement h = hash2FieldElement(curve, message);
if (h.isZero()) {
h = curve.fromBigInteger(ONE);
}
ECPoint R = ECAlgorithms.sumOfTwoMultiplies(parameters.getG(), s, ((ECPublicKeyParameters) key).getQ(), r).normalize();
// components must be bogus.
if (R.isInfinity()) {
return false;
}
ECFieldElement y = h.multiply(R.getAffineXCoord());
return fieldElement2Integer(n, y).compareTo(r) == 0;
}
Aggregations