use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project Skein3Fish by wernerd.
the class ECKeyPairGenerator method generateKeyPair.
/**
* Given the domain parameters this routine generates an EC key
* pair in accordance with X9.62 section 5.2.1 pages 26, 27.
*/
public AsymmetricCipherKeyPair generateKeyPair() {
BigInteger n = params.getN();
int nBitLength = n.bitLength();
BigInteger d;
do {
d = new BigInteger(nBitLength, random);
} while (d.equals(ZERO) || (d.compareTo(n) >= 0));
ECPoint Q = params.getG().multiply(d);
return new AsymmetricCipherKeyPair(new ECPublicKeyParameters(Q, params), new ECPrivateKeyParameters(d, params));
}
use of org.bouncycastle.crypto.params.ECPrivateKeyParameters 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.crypto.params.ECPrivateKeyParameters 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.crypto.params.ECPrivateKeyParameters in project web3sdk by FISCO-BCOS.
the class ECKeyPair method sign.
/**
* Sign a hash with the private key of this key pair.
*
* @param hash the hash to sign
* @return An {@link ECDSASignature} of the hash
*/
public ECDSASignature sign(byte[] hash) {
ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE);
signer.init(true, privKey);
BigInteger[] components = signer.generateSignature(hash);
return new ECDSASignature(components[0], components[1]).toCanonicalised();
}
use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project web3sdk by FISCO-BCOS.
the class SM2Signer method initWithCache.
/**
* The same as init method with better performance by adding the cache for the z value
* corresponding to the privateKey value
*
* @param forSigning
* @param param
*/
public void initWithCache(boolean forSigning, CipherParameters param) {
CipherParameters baseParam;
byte[] userID;
if (param instanceof ParametersWithID) {
baseParam = ((ParametersWithID) param).getParameters();
userID = ((ParametersWithID) param).getID();
} else {
baseParam = param;
// the default value
userID = Hex.decode("31323334353637383132333435363738");
}
if (forSigning) {
if (baseParam instanceof ParametersWithRandom) {
ParametersWithRandom rParam = (ParametersWithRandom) baseParam;
ecKey = (ECKeyParameters) rParam.getParameters();
ecParams = ecKey.getParameters();
kCalculator.init(ecParams.getN(), rParam.getRandom());
} else {
ecKey = (ECKeyParameters) baseParam;
ecParams = ecKey.getParameters();
kCalculator.init(ecParams.getN(), CryptoServicesRegistrar.getSecureRandom());
}
BigInteger privateKey = ((ECPrivateKeyParameters) ecKey).getD();
/**
* First find z value from zValueCache
*/
z = zValueCache.get(privateKey);
if (Objects.isNull(z)) {
// z value of privateKey not exist, calculate it and set it to the cache
pubPoint = createBasePointMultiplier().multiply(ecParams.getG(), ((ECPrivateKeyParameters) ecKey).getD()).normalize();
z = getZ(userID);
zValueCache.put(privateKey, z);
logger.info(" privateKey: {} z value not exist, caculate z: {}", privateKey, Hex.toHexString(z));
} else {
if (logger.isDebugEnabled()) {
logger.debug(" privateKey: {} z value, z: {}", privateKey, Hex.toHexString(z));
}
}
digest.update(z, 0, z.length);
} else {
ecKey = (ECKeyParameters) baseParam;
ecParams = ecKey.getParameters();
pubPoint = ((ECPublicKeyParameters) ecKey).getQ();
z = getZ(userID);
digest.update(z, 0, z.length);
}
}
Aggregations