Search in sources :

Example 21 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project XobotOS by xamarin.

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 22 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project robovm by robovm.

the class ECUtil method generatePrivateKeyParameter.

public static AsymmetricKeyParameter generatePrivateKeyParameter(PrivateKey key) throws InvalidKeyException {
    if (key instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) key;
        ECParameterSpec s = k.getParameters();
        if (s == null) {
            s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
        }
        return new ECPrivateKeyParameters(k.getD(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    } else if (key instanceof java.security.interfaces.ECPrivateKey) {
        java.security.interfaces.ECPrivateKey privKey = (java.security.interfaces.ECPrivateKey) key;
        ECParameterSpec s = EC5Util.convertSpec(privKey.getParams(), false);
        return new ECPrivateKeyParameters(privKey.getS(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    } else {
        // see if we can build a key from key.getEncoded()
        try {
            byte[] bytes = key.getEncoded();
            if (bytes == null) {
                throw new InvalidKeyException("no encoding for EC private key");
            }
            PrivateKey privateKey = BouncyCastleProvider.getPrivateKey(PrivateKeyInfo.getInstance(bytes));
            if (privateKey instanceof java.security.interfaces.ECPrivateKey) {
                return ECUtil.generatePrivateKeyParameter(privateKey);
            }
        } catch (Exception e) {
            throw new InvalidKeyException("cannot identify EC private key: " + e.toString());
        }
    }
    throw new InvalidKeyException("can't identify EC private key.");
}
Also used : ECPrivateKey(org.bouncycastle.jce.interfaces.ECPrivateKey) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ECPrivateKey(org.bouncycastle.jce.interfaces.ECPrivateKey) PrivateKey(java.security.PrivateKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) InvalidKeyException(java.security.InvalidKeyException) InvalidKeyException(java.security.InvalidKeyException)

Example 23 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project OsmAnd-tools by osmandapp.

the class SigningUtils method signData.

static String signData(String input, byte[] key) throws BlockIOException {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    X9ECParameters params = SECNamedCurves.getByName("secp256k1");
    ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH());
    BigInteger priv = new BigInteger(1, key);
    ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(priv, ecParams);
    signer.init(true, privKey);
    BigInteger[] sigs = signer.generateSignature(fromHex(input));
    BigInteger r = sigs[0];
    BigInteger s = sigs[1];
    // BIP62: "S must be less than or equal to half of the Group Order N"
    BigInteger overTwo = params.getN().shiftRight(1);
    if (s.compareTo(overTwo) == 1) {
        s = params.getN().subtract(s);
    }
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DERSequenceGenerator seq = new DERSequenceGenerator(bos);
        seq.addObject(new ASN1Integer(r));
        seq.addObject(new ASN1Integer(s));
        seq.close();
        return toHex(bos.toByteArray());
    } catch (IOException e) {
        // Cannot happen.
        throw new BlockIOException("That should never happen... File an issue report.");
    }
}
Also used : ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECDSASigner(org.bouncycastle.crypto.signers.ECDSASigner) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) IOException(java.io.IOException) HMacDSAKCalculator(org.bouncycastle.crypto.signers.HMacDSAKCalculator) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) BigInteger(java.math.BigInteger) DERSequenceGenerator(org.bouncycastle.asn1.DERSequenceGenerator)

Example 24 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters 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);
    }
}
Also used : FixedPointCombMultiplier(org.bouncycastle.math.ec.FixedPointCombMultiplier) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) DERSequence(org.bouncycastle.asn1.DERSequence) BigInteger(java.math.BigInteger) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) IOException(java.io.IOException) ECPoint(org.bouncycastle.math.ec.ECPoint) CryptoException(org.bouncycastle.crypto.CryptoException)

Example 25 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters in project web3sdk by FISCO-BCOS.

the class ECDSASign method sign.

public static ECDSASignature sign(byte[] transactionHash, BigInteger privateKey) {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, CURVE);
    signer.init(true, privKey);
    Object[] components = signer.generateSignature2(transactionHash);
    return new ECDSASignature((BigInteger) components[0], (BigInteger) components[1], (ECPoint) components[2]);
}
Also used : HMacDSAKCalculator(org.bouncycastle.crypto.signers.HMacDSAKCalculator) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest)

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