Search in sources :

Example 1 with ECPrivateKeyParameters

use of org.gudy.bouncycastle.crypto.params.ECPrivateKeyParameters in project BiglyBT by BiglySoftware.

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.
 */
@Override
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.gudy.bouncycastle.crypto.params.ECPrivateKeyParameters) BigInteger(java.math.BigInteger) ECPoint(org.gudy.bouncycastle.math.ec.ECPoint) ECPublicKeyParameters(org.gudy.bouncycastle.crypto.params.ECPublicKeyParameters) ECPoint(org.gudy.bouncycastle.math.ec.ECPoint) AsymmetricCipherKeyPair(org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair)

Example 2 with ECPrivateKeyParameters

use of org.gudy.bouncycastle.crypto.params.ECPrivateKeyParameters in project BiglyBT by BiglySoftware.

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) {
    BigInteger e = new BigInteger(1, message);
    BigInteger n = key.getParameters().getN();
    BigInteger r = null;
    BigInteger s = null;
    // 5.3.2
    do // generate s
    {
        BigInteger k = null;
        int nBitLength = n.bitLength();
        do // generate r
        {
            do {
                k = new BigInteger(nBitLength, random);
            } while (k.equals(ZERO));
            ECPoint p = key.getParameters().getG().multiply(k);
            // 5.3.3
            BigInteger x = p.getX().toBigInteger();
            r = x.mod(n);
        } while (r.equals(ZERO));
        BigInteger d = ((ECPrivateKeyParameters) key).getD();
        s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n);
    } while (s.equals(ZERO));
    BigInteger[] res = new BigInteger[2];
    res[0] = r;
    res[1] = s;
    return res;
}
Also used : ECPrivateKeyParameters(org.gudy.bouncycastle.crypto.params.ECPrivateKeyParameters) BigInteger(java.math.BigInteger) ECPoint(org.gudy.bouncycastle.math.ec.ECPoint) ECPoint(org.gudy.bouncycastle.math.ec.ECPoint)

Example 3 with ECPrivateKeyParameters

use of org.gudy.bouncycastle.crypto.params.ECPrivateKeyParameters in project BiglyBT by BiglySoftware.

the class ECUtil method generatePrivateKeyParameter.

public static AsymmetricKeyParameter generatePrivateKeyParameter(PrivateKey key) throws InvalidKeyException {
    if (key instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) key;
        ECParameterSpec s = k.getParams();
        return new ECPrivateKeyParameters(k.getD(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN()));
    }
    throw new InvalidKeyException("can't identify EC private key.");
}
Also used : ECPrivateKey(org.gudy.bouncycastle.jce.interfaces.ECPrivateKey) ECPrivateKeyParameters(org.gudy.bouncycastle.crypto.params.ECPrivateKeyParameters) ECDomainParameters(org.gudy.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.gudy.bouncycastle.jce.spec.ECParameterSpec) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

ECPrivateKeyParameters (org.gudy.bouncycastle.crypto.params.ECPrivateKeyParameters)3 BigInteger (java.math.BigInteger)2 ECPoint (org.gudy.bouncycastle.math.ec.ECPoint)2 InvalidKeyException (java.security.InvalidKeyException)1 AsymmetricCipherKeyPair (org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair)1 ECDomainParameters (org.gudy.bouncycastle.crypto.params.ECDomainParameters)1 ECPublicKeyParameters (org.gudy.bouncycastle.crypto.params.ECPublicKeyParameters)1 ECPrivateKey (org.gudy.bouncycastle.jce.interfaces.ECPrivateKey)1 ECParameterSpec (org.gudy.bouncycastle.jce.spec.ECParameterSpec)1