Search in sources :

Example 1 with ECMultiplier

use of com.github.zhenwei.core.math.ec.ECMultiplier in project LinLong-Java by zhenwei1108.

the class ECFixedTransform method transform.

/**
 * Transform an existing cipher text pair using the ElGamal algorithm. Note: it is assumed this
 * transform has been initialised with the same public key that was used to create the original
 * cipher text.
 *
 * @param cipherText the EC point to process.
 * @return returns a new ECPair representing the result of the process.
 */
public ECPair transform(ECPair cipherText) {
    if (key == null) {
        throw new IllegalStateException("ECFixedTransform not initialised");
    }
    ECDomainParameters ec = key.getParameters();
    BigInteger n = ec.getN();
    ECMultiplier basePointMultiplier = createBasePointMultiplier();
    BigInteger k = this.k.mod(n);
    ECPoint[] gamma_phi = new ECPoint[] { basePointMultiplier.multiply(ec.getG(), k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getX())), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getY())) };
    ec.getCurve().normalizeAll(gamma_phi);
    return new ECPair(gamma_phi[0], gamma_phi[1]);
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Example 2 with ECMultiplier

use of com.github.zhenwei.core.math.ec.ECMultiplier in project LinLong-Java by zhenwei1108.

the class ECNewRandomnessTransform method transform.

/**
 * Transform an existing cipher test pair using the ElGamal algorithm. Note: it is assumed this
 * transform has been initialised with the same public key that was used to create the original
 * cipher text.
 *
 * @param cipherText the EC point to process.
 * @return returns a new ECPair representing the result of the process.
 */
public ECPair transform(ECPair cipherText) {
    if (key == null) {
        throw new IllegalStateException("ECNewRandomnessTransform not initialised");
    }
    ECDomainParameters ec = key.getParameters();
    BigInteger n = ec.getN();
    ECMultiplier basePointMultiplier = createBasePointMultiplier();
    BigInteger k = ECUtil.generateK(n, random);
    ECPoint[] gamma_phi = new ECPoint[] { basePointMultiplier.multiply(ec.getG(), k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getX())), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getY())) };
    ec.getCurve().normalizeAll(gamma_phi);
    lastK = k;
    return new ECPair(gamma_phi[0], gamma_phi[1]);
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Example 3 with ECMultiplier

use of com.github.zhenwei.core.math.ec.ECMultiplier in project LinLong-Java by zhenwei1108.

the class ECGOST3410_2012Signer method generateSignature.

/**
 * generate a signature for the given message using the key we were initialised with. For
 * conventional GOST3410 2012 the message should be a GOST3411 2012 hash of the message of
 * interest.
 *
 * @param message the message that will be verified later.
 */
public BigInteger[] generateSignature(byte[] message) {
    // conversion is little-endian
    byte[] mRev = Arrays.reverse(message);
    BigInteger e = new BigInteger(1, mRev);
    ECDomainParameters ec = key.getParameters();
    BigInteger n = ec.getN();
    BigInteger d = ((ECPrivateKeyParameters) key).getD();
    BigInteger r, s;
    ECMultiplier basePointMultiplier = createBasePointMultiplier();
    do // generate s
    {
        BigInteger k;
        do // generate r
        {
            do {
                k = BigIntegers.createRandomBigInteger(n.bitLength(), random);
            } while (k.equals(ECConstants.ZERO));
            ECPoint p = basePointMultiplier.multiply(ec.getG(), k).normalize();
            r = p.getAffineXCoord().toBigInteger().mod(n);
        } while (r.equals(ECConstants.ZERO));
        s = (k.multiply(e)).add(d.multiply(r)).mod(n);
    } while (s.equals(ECConstants.ZERO));
    return new BigInteger[] { r, s };
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Example 4 with ECMultiplier

use of com.github.zhenwei.core.math.ec.ECMultiplier in project LinLong-Java by zhenwei1108.

the class SM2Signer method generateSignature.

public byte[] generateSignature() throws CryptoException {
    byte[] eHash = digestDoFinal();
    BigInteger n = ecParams.getN();
    BigInteger e = calculateE(n, eHash);
    BigInteger d = ((ECPrivateKeyParameters) ecKey).getD();
    BigInteger r, s;
    ECMultiplier basePointMultiplier = createBasePointMultiplier();
    // 5.2.1 Draft RFC:  SM2 Public Key Algorithms
    do // generate s
    {
        BigInteger k;
        do // generate r
        {
            // A3
            k = kCalculator.nextK();
            // A4
            ECPoint p = basePointMultiplier.multiply(ecParams.getG(), k).normalize();
            // A5
            r = e.add(p.getAffineXCoord().toBigInteger()).mod(n);
        } while (r.equals(ZERO) || r.add(k).equals(n));
        // A6
        BigInteger dPlus1ModN = BigIntegers.modOddInverse(n, d.add(ONE));
        s = k.subtract(r.multiply(d)).mod(n);
        s = dPlus1ModN.multiply(s).mod(n);
    } while (s.equals(ZERO));
    // A7
    try {
        return encoding.encode(ecParams.getN(), r, s);
    } catch (Exception ex) {
        throw new CryptoException("unable to encode signature: " + ex.getMessage(), ex);
    }
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) CryptoException(com.github.zhenwei.core.crypto.CryptoException) CryptoException(com.github.zhenwei.core.crypto.CryptoException)

Example 5 with ECMultiplier

use of com.github.zhenwei.core.math.ec.ECMultiplier in project LinLong-Java by zhenwei1108.

the class SM2Engine method encrypt.

private byte[] encrypt(byte[] in, int inOff, int inLen) throws InvalidCipherTextException {
    byte[] c2 = new byte[inLen];
    System.arraycopy(in, inOff, c2, 0, c2.length);
    ECMultiplier multiplier = createBasePointMultiplier();
    byte[] c1;
    ECPoint kPB;
    do {
        BigInteger k = nextK();
        ECPoint c1P = multiplier.multiply(ecParams.getG(), k).normalize();
        c1 = c1P.getEncoded(false);
        kPB = ((ECPublicKeyParameters) ecKey).getQ().multiply(k).normalize();
        kdf(digest, kPB, c2);
    } while (notEncrypted(c2, in, inOff));
    byte[] c3 = new byte[digest.getDigestSize()];
    addFieldElement(digest, kPB.getAffineXCoord());
    digest.update(in, inOff, inLen);
    addFieldElement(digest, kPB.getAffineYCoord());
    digest.doFinal(c3, 0);
    switch(mode) {
        case C1C3C2:
            return Arrays.concatenate(c1, c3, c2);
        default:
            return Arrays.concatenate(c1, c2, c3);
    }
}
Also used : BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Aggregations

ECMultiplier (com.github.zhenwei.core.math.ec.ECMultiplier)12 BigInteger (java.math.BigInteger)12 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)11 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)9 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)5 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)2 Sm2Cipher (com.github.zhenwei.core.asn1.pkcs.Sm2Cipher)1 CryptoException (com.github.zhenwei.core.crypto.CryptoException)1 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)1 ECFieldElement (com.github.zhenwei.core.math.ec.ECFieldElement)1