Search in sources :

Example 11 with ECPrivateKeyParameters

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

Example 12 with ECPrivateKeyParameters

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 };
}
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 13 with ECPrivateKeyParameters

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 };
}
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 14 with ECPrivateKeyParameters

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();
}
Also used : HMacDSAKCalculator(org.bouncycastle.crypto.signers.HMacDSAKCalculator) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ECDSASigner(org.bouncycastle.crypto.signers.ECDSASigner) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) BigInteger(java.math.BigInteger)

Example 15 with ECPrivateKeyParameters

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

Aggregations

ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)26 BigInteger (java.math.BigInteger)20 ECPoint (org.bouncycastle.math.ec.ECPoint)11 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)8 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)5 ECDSASigner (org.bouncycastle.crypto.signers.ECDSASigner)5 ECMultiplier (org.bouncycastle.math.ec.ECMultiplier)5 IOException (java.io.IOException)4 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)4 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)4 AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)4 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)4 HMacDSAKCalculator (org.bouncycastle.crypto.signers.HMacDSAKCalculator)4 DSAPrivateKeyParameters (org.bouncycastle.crypto.params.DSAPrivateKeyParameters)3 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)3 InvalidKeyException (java.security.InvalidKeyException)2 PrivateKey (java.security.PrivateKey)2 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)2 DHParameter (org.bouncycastle.asn1.pkcs.DHParameter)2