Search in sources :

Example 6 with ECDomainParameters

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

Example 7 with ECDomainParameters

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();
}
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 8 with ECDomainParameters

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();
}
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 9 with ECDomainParameters

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;
}
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 10 with ECDomainParameters

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

Aggregations

ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)35 BigInteger (java.math.BigInteger)22 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)21 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)12 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)10 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)10 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)9 ECMultiplier (com.github.zhenwei.core.math.ec.ECMultiplier)9 ECParameterSpec (com.github.zhenwei.provider.jce.spec.ECParameterSpec)9 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)7 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)6 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)6 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)5 X962Parameters (com.github.zhenwei.core.asn1.x9.X962Parameters)5 ECKeyGenerationParameters (com.github.zhenwei.core.crypto.params.ECKeyGenerationParameters)4 IOException (java.io.IOException)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)3 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)3 GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)3