Search in sources :

Example 1 with DSAKCalculator

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();
}
Also used : BCECPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) ECPoint(org.bouncycastle.math.ec.ECPoint) RandomDSAKCalculator(org.bouncycastle.crypto.signers.RandomDSAKCalculator) DSAKCalculator(org.bouncycastle.crypto.signers.DSAKCalculator) RandomDSAKCalculator(org.bouncycastle.crypto.signers.RandomDSAKCalculator) BCECPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey) BigInteger(java.math.BigInteger) DERSequenceGenerator(org.bouncycastle.asn1.DERSequenceGenerator)

Example 2 with DSAKCalculator

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));
}
Also used : ECKeyPairGenerator(org.bouncycastle.crypto.generators.ECKeyPairGenerator) Digest(org.bouncycastle.crypto.Digest) Blake2bDigest(org.bouncycastle.crypto.digests.Blake2bDigest) ECDSASigner(org.bouncycastle.crypto.signers.ECDSASigner) ArrayList(java.util.ArrayList) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) DSAKCalculator(org.bouncycastle.crypto.signers.DSAKCalculator) HMacDSAKCalculator(org.bouncycastle.crypto.signers.HMacDSAKCalculator) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) HMacDSAKCalculator(org.bouncycastle.crypto.signers.HMacDSAKCalculator) DSADigestSigner(org.bouncycastle.crypto.signers.DSADigestSigner) Blake2bDigest(org.bouncycastle.crypto.digests.Blake2bDigest) ECKeyGenerationParameters(org.bouncycastle.crypto.params.ECKeyGenerationParameters)

Aggregations

DSAKCalculator (org.bouncycastle.crypto.signers.DSAKCalculator)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)1 DERSequenceGenerator (org.bouncycastle.asn1.DERSequenceGenerator)1 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)1 AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)1 Digest (org.bouncycastle.crypto.Digest)1 Blake2bDigest (org.bouncycastle.crypto.digests.Blake2bDigest)1 ECKeyPairGenerator (org.bouncycastle.crypto.generators.ECKeyPairGenerator)1 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)1 ECKeyGenerationParameters (org.bouncycastle.crypto.params.ECKeyGenerationParameters)1 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)1 DSADigestSigner (org.bouncycastle.crypto.signers.DSADigestSigner)1 ECDSASigner (org.bouncycastle.crypto.signers.ECDSASigner)1 HMacDSAKCalculator (org.bouncycastle.crypto.signers.HMacDSAKCalculator)1 RandomDSAKCalculator (org.bouncycastle.crypto.signers.RandomDSAKCalculator)1 BCECPrivateKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)1 ECMultiplier (org.bouncycastle.math.ec.ECMultiplier)1 ECPoint (org.bouncycastle.math.ec.ECPoint)1