Search in sources :

Example 11 with ECMultiplier

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

the class ECDSASigner method generateSignature.

// 5.3 pg 28
/**
 * generate a signature for the given message using the key we were initialised with. For
 * conventional DSA the message should be a SHA-1 hash of the message of interest.
 *
 * @param message the message that will be verified later.
 */
public BigInteger[] generateSignature(byte[] message) {
    ECDomainParameters ec = key.getParameters();
    BigInteger n = ec.getN();
    BigInteger e = calculateE(n, message);
    BigInteger d = ((ECPrivateKeyParameters) key).getD();
    if (kCalculator.isDeterministic()) {
        kCalculator.init(n, d, message);
    } else {
        kCalculator.init(n, random);
    }
    BigInteger r, s;
    ECMultiplier basePointMultiplier = createBasePointMultiplier();
    // 5.3.2
    do // generate s
    {
        BigInteger k;
        do // generate r
        {
            k = kCalculator.nextK();
            ECPoint p = basePointMultiplier.multiply(ec.getG(), k).normalize();
            // 5.3.3
            r = p.getAffineXCoord().toBigInteger().mod(n);
        } while (r.equals(ZERO));
        s = BigIntegers.modOddInverse(n, k).multiply(e.add(d.multiply(r))).mod(n);
    } while (s.equals(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 12 with ECMultiplier

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

the class ECGOST3410Signer method generateSignature.

/**
 * generate a signature for the given message using the key we were initialised with. For
 * conventional GOST3410 the message should be a GOST3411 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)

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