use of org.bouncycastle.crypto.signers.DSAKCalculator in project PCNGateway-Java-SDK by BSNDA.
the class Sm2SignUtil method xuperSignature.
public static byte[] xuperSignature(String privateKeyStr, byte[] message) throws Exception {
PrivateKey privateKey = Sm2SignUtil.getPrivateKey(privateKeyStr.getBytes());
BCECPrivateKey ecPrivateKey = (BCECPrivateKey) privateKey;
X9ECParameters sm2ECParameters = GMNamedCurves.getByName("sm2p256v1");
ECDomainParameters ec = new ECDomainParameters(sm2ECParameters.getCurve(), sm2ECParameters.getG(), sm2ECParameters.getN());
// Order n
BigInteger n = ec.getN();
// Base point G
ECPoint G = ec.getG();
BigInteger r, s;
// Get private key d
BigInteger d = ecPrivateKey.getD();
ECMultiplier basePointMultiplier = createBasePointMultiplier();
DSAKCalculator kCalculator = new RandomDSAKCalculator();
// Initialize the random number generator
// if (kCalculator.isDeterministic()) {
// kCalculator.init(n, d, message);
// } else {
kCalculator.init(n, new SecureRandom());
do {
// Calculate s
BigInteger k;
BigInteger e;
BigInteger tmp;
BigInteger tmp2;
do {
// Calculate r,refers to GM/T 0003.2-2012 6.1
k = kCalculator.nextK();
ECPoint p = basePointMultiplier.multiply(G, k).normalize();
e = org.bouncycastle.util.BigIntegers.fromUnsignedByteArray(message);
// r = (e + x) mod n
r = p.getAffineXCoord().toBigInteger().add(e).mod(n);
} while (r.equals(ZERO) || r.add(k).equals(n));
// tmp = (1+d).inverse
tmp = d.add(ONE).modInverse(n);
// tmp2 = k - r*d
tmp2 = k.subtract(r.multiply(d));
s = tmp.multiply(tmp2).mod(n);
} while (s.equals(ZERO));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DERSequenceGenerator seq = new DERSequenceGenerator(bos);
seq.addObject(new ASN1Integer(r));
seq.addObject(new ASN1Integer(s));
seq.close();
return bos.toByteArray();
}
use of org.bouncycastle.crypto.signers.DSAKCalculator in project briar by briar.
the class EllipticCurvePerformanceTest method runTest.
private static void runTest(String name, ECDomainParameters params) {
// Generate two key pairs using the given parameters
ECKeyPairGenerator generator = new ECKeyPairGenerator();
generator.init(new ECKeyGenerationParameters(params, random));
AsymmetricCipherKeyPair keyPair1 = generator.generateKeyPair();
AsymmetricCipherKeyPair keyPair2 = generator.generateKeyPair();
// Time some ECDH and ECDHC key agreements
long agreementMedian = runAgreementTest(keyPair1, keyPair2, false);
long agreementWithCofactorMedian = runAgreementTest(keyPair1, keyPair2, true);
// Time some signatures
List<Long> samples = new ArrayList<>();
List<byte[]> signatures = new ArrayList<>();
for (int i = 0; i < SAMPLES; i++) {
Digest digest = new Blake2bDigest(256);
DSAKCalculator calculator = new HMacDSAKCalculator(digest);
DSADigestSigner signer = new DSADigestSigner(new ECDSASigner(calculator), digest);
long start = System.nanoTime();
signer.init(true, new ParametersWithRandom(keyPair1.getPrivate(), random));
signer.update(new byte[BYTES_TO_SIGN], 0, BYTES_TO_SIGN);
signatures.add(signer.generateSignature());
samples.add(System.nanoTime() - start);
}
long signatureMedian = median(samples);
// Time some signature verifications
samples.clear();
for (int i = 0; i < SAMPLES; i++) {
Digest digest = new Blake2bDigest(256);
DSAKCalculator calculator = new HMacDSAKCalculator(digest);
DSADigestSigner signer = new DSADigestSigner(new ECDSASigner(calculator), digest);
long start = System.nanoTime();
signer.init(false, keyPair1.getPublic());
signer.update(new byte[BYTES_TO_SIGN], 0, BYTES_TO_SIGN);
if (!signer.verifySignature(signatures.get(i)))
throw new AssertionError();
samples.add(System.nanoTime() - start);
}
long verificationMedian = median(samples);
System.out.println(String.format("%s: %,d %,d %,d %,d", name, agreementMedian, agreementWithCofactorMedian, signatureMedian, verificationMedian));
}
Aggregations