org.bouncycastle.math.ec.ECMultiplier Java Examples

The following examples show how to use org.bouncycastle.math.ec.ECMultiplier. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SM2Signer.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public BigInteger[] generateSignature2() throws CryptoException {
    byte[] eHash = digestDoFinal();

    BigInteger n = ecParams.getN();
    BigInteger e = calculateE(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 = d.add(ONE).modInverse(n);

        s = k.subtract(r.multiply(d)).mod(n);
        s = dPlus1ModN.multiply(s).mod(n);
    } while (s.equals(ZERO));

    return new BigInteger[] {r, s};
}
 
Example #2
Source File: ECDSASigner.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 */
@Override
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 = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n);
    } while (s.equals(ZERO));

    return new BigInteger[] {r, s};
}
 
Example #3
Source File: ECDSASigner.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * The same generateSignature with the temporary variable ECPoint P generated by the signature
 * process is also returned together
 *
 * @param message the message that will be verified later.
 */
public Object[] generateSignature2(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;

    /** */
    ECPoint p;

    ECMultiplier basePointMultiplier = createBasePointMultiplier();

    // 5.3.2
    do // generate s
    {
        BigInteger k;
        do // generate r
        {
            k = kCalculator.nextK();

            p = basePointMultiplier.multiply(ec.getG(), k).normalize();

            // 5.3.3
            r = p.getAffineXCoord().toBigInteger().mod(n);
        } while (r.equals(ZERO));

        s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n);
    } while (s.equals(ZERO));

    return new Object[] {r, s, p};
}
 
Example #4
Source File: SM2PreprocessSigner.java    From gmhelper with Apache License 2.0 4 votes vote down vote up
public byte[] generateSignature(byte[] eHash) throws CryptoException {
    BigInteger n = ecParams.getN();
    BigInteger e = calculateE(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 = d.add(ONE).modInverse(n);

        s = k.subtract(r.multiply(d)).mod(n);
        s = dPlus1ModN.multiply(s).mod(n);
    }
    while (s.equals(ZERO));

    // A7
    try {
        return derEncode(r, s);
    } catch (IOException ex) {
        throw new CryptoException("unable to encode signature: " + ex.getMessage(), ex);
    }
}
 
Example #5
Source File: SM2PreprocessSigner.java    From gmhelper with Apache License 2.0 4 votes vote down vote up
protected ECMultiplier createBasePointMultiplier() {
    return new FixedPointCombMultiplier();
}
 
Example #6
Source File: Signer.java    From evt4j with MIT License 4 votes vote down vote up
protected static ECMultiplier createBasePointMultiplier() {
    return new FixedPointCombMultiplier();
}
 
Example #7
Source File: Signer.java    From evt4j with MIT License 4 votes vote down vote up
/**
 * 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.
 */
@Override
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();

    int sLen = 0;
    // 5.3.2
    do // generate s
    {
        BigInteger k;
        int rLen = 0;
        do // generate r
        {
            k = kCalculator.nextK();

            ECPoint p = basePointMultiplier.multiply(ec.getG(), k).normalize();

            // 5.3.3
            r = p.getAffineXCoord().toBigInteger().mod(n);
            rLen = r.toByteArray().length;
        } while (r.equals(ZERO) || rLen != 32); // make sure that length of r is 32 bytes

        s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n);
        sLen = s.toByteArray().length;
    } while (s.equals(ZERO) || sLen != 32);

    return new BigInteger[] { r, s };
}
 
Example #8
Source File: SM2Signer.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] generateSignature() throws CryptoException {
    byte[] eHash = digestDoFinal();

    BigInteger n = ecParams.getN();
    BigInteger e = calculateE(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 = d.add(ONE).modInverse(n);

        s = k.subtract(r.multiply(d)).mod(n);
        s = dPlus1ModN.multiply(s).mod(n);
    } while (s.equals(ZERO));

    // A7
    try {
        return derEncode(r, s);
    } catch (IOException ex) {
        throw new CryptoException("unable to encode signature: " + ex.getMessage(), ex);
    }
}
 
Example #9
Source File: SM2Signer.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
protected static ECMultiplier createBasePointMultiplier() {
    return new FixedPointCombMultiplier();
}
 
Example #10
Source File: ECDSASigner.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
protected ECMultiplier createBasePointMultiplier() {
    return new FixedPointCombMultiplier();
}
 
Example #11
Source File: SM2Signer.java    From xipki with Apache License 2.0 4 votes vote down vote up
public byte[] generateSignatureForHash(byte[] eHash) throws CryptoException {
  BigInteger n = ecParams.getN();
  BigInteger e = new BigInteger(1, eHash);
  BigInteger d = ((ECPrivateKeyParameters)ecKey).getD();

  BigInteger r;
  BigInteger s;

  ECMultiplier basePointMultiplier = new FixedPointCombMultiplier();

  // 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(ECConstants.ZERO) || r.add(k).equals(n));

    // A6
    // CHECKSTYLE:SKIP
    BigInteger dPlus1ModN = d.add(ECConstants.ONE).modInverse(n);

    s = k.subtract(r.multiply(d)).mod(n);
    s = dPlus1ModN.multiply(s).mod(n);
  } while (s.equals(ECConstants.ZERO));

  // A7
  try {
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(r));
    v.add(new ASN1Integer(s));
    return new DERSequence(v).getEncoded(ASN1Encoding.DER);
  } catch (IOException ex) {
    throw new CryptoException("unable to encode signature: " + ex.getMessage(), ex);
  }
}