Search in sources :

Example 1 with ECMultiplier

use of org.bouncycastle.math.ec.ECMultiplier in project web3sdk by FISCO-BCOS.

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.
 */
@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 };
}
Also used : ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) BigInteger(java.math.BigInteger) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) ECPoint(org.bouncycastle.math.ec.ECPoint)

Example 2 with ECMultiplier

use of org.bouncycastle.math.ec.ECMultiplier in project web3sdk by FISCO-BCOS.

the class ECDSASigner method generateSignature2.

/**
 * 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 };
}
Also used : ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) BigInteger(java.math.BigInteger) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) ECPoint(org.bouncycastle.math.ec.ECPoint)

Example 3 with ECMultiplier

use of org.bouncycastle.math.ec.ECMultiplier in project web3sdk by FISCO-BCOS.

the class SM2Signer method generateSignature.

@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);
    }
}
Also used : ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) BigInteger(java.math.BigInteger) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) IOException(java.io.IOException) ECPoint(org.bouncycastle.math.ec.ECPoint) CryptoException(org.bouncycastle.crypto.CryptoException)

Example 4 with ECMultiplier

use of org.bouncycastle.math.ec.ECMultiplier in project web3sdk by FISCO-BCOS.

the class SM2Signer method generateSignature2.

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 };
}
Also used : ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) BigInteger(java.math.BigInteger) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) ECPoint(org.bouncycastle.math.ec.ECPoint)

Example 5 with ECMultiplier

use of org.bouncycastle.math.ec.ECMultiplier in project xipki by xipki.

the class SM2Signer method generateSignatureForHash.

// CHECKSTYLE:SKIP
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);
    }
}
Also used : FixedPointCombMultiplier(org.bouncycastle.math.ec.FixedPointCombMultiplier) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) DERSequence(org.bouncycastle.asn1.DERSequence) BigInteger(java.math.BigInteger) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) IOException(java.io.IOException) ECPoint(org.bouncycastle.math.ec.ECPoint) CryptoException(org.bouncycastle.crypto.CryptoException)

Aggregations

ECMultiplier (org.bouncycastle.math.ec.ECMultiplier)6 ECPoint (org.bouncycastle.math.ec.ECPoint)6 BigInteger (java.math.BigInteger)5 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)5 IOException (java.io.IOException)2 CryptoException (org.bouncycastle.crypto.CryptoException)2 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)2 FixedPointCombMultiplier (org.bouncycastle.math.ec.FixedPointCombMultiplier)2 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)1 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)1 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)1 DERSequence (org.bouncycastle.asn1.DERSequence)1 BCECPrivateKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)1 BCECPublicKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey)1 ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)1 ECPublicKeySpec (org.bouncycastle.jce.spec.ECPublicKeySpec)1