Search in sources :

Example 1 with ECPublicKeyParameters

use of org.gudy.bouncycastle.crypto.params.ECPublicKeyParameters 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 ECPublicKeyParameters

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

the class ECDSASigner method verifySignature.

// 5.4 pg 29
/**
 * return true if the value r and s represent a DSA signature for
 * the passed in message (for standard DSA the message should be
 * a SHA-1 hash of the real message to be verified).
 */
@Override
public boolean verifySignature(byte[] message, BigInteger r, BigInteger s) {
    BigInteger e = new BigInteger(1, message);
    BigInteger n = key.getParameters().getN();
    // r in the range [1,n-1]
    if (r.compareTo(ONE) < 0 || r.compareTo(n) >= 0) {
        return false;
    }
    // s in the range [1,n-1]
    if (s.compareTo(ONE) < 0 || s.compareTo(n) >= 0) {
        return false;
    }
    BigInteger c = s.modInverse(n);
    BigInteger u1 = e.multiply(c).mod(n);
    BigInteger u2 = r.multiply(c).mod(n);
    ECPoint G = key.getParameters().getG();
    ECPoint Q = ((ECPublicKeyParameters) key).getQ();
    ECPoint point = G.multiply(u1).add(Q.multiply(u2));
    BigInteger v = point.getX().toBigInteger().mod(n);
    return v.equals(r);
}
Also used : BigInteger(java.math.BigInteger) ECPoint(org.gudy.bouncycastle.math.ec.ECPoint) ECPublicKeyParameters(org.gudy.bouncycastle.crypto.params.ECPublicKeyParameters)

Example 3 with ECPublicKeyParameters

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

the class ECUtil method generatePublicKeyParameter.

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

Example 4 with ECPublicKeyParameters

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

the class ECDHBasicAgreement method calculateAgreement.

@Override
public BigInteger calculateAgreement(CipherParameters pubKey) {
    ECPublicKeyParameters pub = (ECPublicKeyParameters) pubKey;
    ECPoint P = pub.getQ().multiply(key.getD());
    return P.getX().toBigInteger();
}
Also used : ECPoint(org.gudy.bouncycastle.math.ec.ECPoint) ECPublicKeyParameters(org.gudy.bouncycastle.crypto.params.ECPublicKeyParameters)

Aggregations

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