Search in sources :

Example 26 with ECPublicKeyParameters

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();
    }
}
Also used : ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)

Example 27 with ECPublicKeyParameters

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();
    }
}
Also used : ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)

Example 28 with ECPublicKeyParameters

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);
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) BigInteger(java.math.BigInteger) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)

Example 29 with ECPublicKeyParameters

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));
}
Also used : DataLengthException(com.github.zhenwei.core.crypto.DataLengthException) BigInteger(java.math.BigInteger) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Example 30 with ECPublicKeyParameters

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;
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ECFieldElement(com.github.zhenwei.core.math.ec.ECFieldElement) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)

Aggregations

ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)34 BigInteger (java.math.BigInteger)16 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)14 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)12 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)10 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)7 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)6 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)6 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)5 X9ECPoint (com.github.zhenwei.core.asn1.x9.X9ECPoint)5 IOException (java.io.IOException)5 ASN1BitString (com.github.zhenwei.core.asn1.ASN1BitString)4 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)4 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)4 ECParameterSpec (com.github.zhenwei.provider.jce.spec.ECParameterSpec)4 InvalidKeyException (java.security.InvalidKeyException)4 ECPoint (java.security.spec.ECPoint)4 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)3 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)3 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)3