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 };
}
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 };
}
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);
}
}
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 };
}
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);
}
}
Aggregations