Search in sources :

Example 6 with ECPoint

use of org.bouncycastle.math.ec.ECPoint 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 7 with ECPoint

use of org.bouncycastle.math.ec.ECPoint in project oxAuth by GluuFederation.

the class ECSigner method verifySignature.

@Deprecated
@Override
public boolean verifySignature(String signingInput, String signature) throws Exception {
    if (Strings.isNullOrEmpty(signingInput)) {
        return false;
    }
    if (Strings.isNullOrEmpty(signature)) {
        return false;
    }
    try {
        byte[] sigBytes = Base64Util.base64urldecode(signature);
        byte[] sigInBytes = signingInput.getBytes(Util.UTF8_STRING_ENCODING);
        ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(getSignatureAlgorithm().getCurve().getName());
        BigInteger q = ((ECCurve.Fp) ecSpec.getCurve()).getQ();
        ECFieldElement xFieldElement = new ECFieldElement.Fp(q, ecdsaPublicKey.getX());
        ECFieldElement yFieldElement = new ECFieldElement.Fp(q, ecdsaPublicKey.getY());
        ECPoint pointQ = new ECPoint.Fp(ecSpec.getCurve(), xFieldElement, yFieldElement);
        ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(pointQ, ecSpec);
        KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC");
        PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
        Signature sig = Signature.getInstance(getSignatureAlgorithm().getAlgorithm(), "BC");
        sig.initVerify(publicKey);
        sig.update(sigInBytes);
        return sig.verify(sigBytes);
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("There was a problem in EC verifier", e);
    } catch (UnsupportedEncodingException e) {
        throw new Exception("There was a problem in EC verifier", e);
    } catch (SignatureException e) {
        throw new Exception("There was a problem in EC verifier", e);
    } catch (NoSuchProviderException e) {
        throw new Exception("There was a problem in EC verifier", e);
    } catch (InvalidKeyException e) {
        throw new Exception("There was a problem in EC verifier", e);
    } catch (InvalidKeySpecException e) {
        throw new Exception("There was a problem in EC verifier", e);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) ECFieldElement(org.bouncycastle.math.ec.ECFieldElement)

Example 8 with ECPoint

use of org.bouncycastle.math.ec.ECPoint in project Skein3Fish by wernerd.

the class ECDHBasicAgreement method calculateAgreement.

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

Example 9 with ECPoint

use of org.bouncycastle.math.ec.ECPoint in project XobotOS by xamarin.

the class ECDHBasicAgreement method calculateAgreement.

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

Example 10 with ECPoint

use of org.bouncycastle.math.ec.ECPoint in project XobotOS by xamarin.

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).
     */
public boolean verifySignature(byte[] message, BigInteger r, BigInteger s) {
    BigInteger n = key.getParameters().getN();
    BigInteger e = calculateE(n, message);
    // 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 = ECAlgorithms.sumOfTwoMultiplies(G, u1, Q, u2);
    BigInteger v = point.getX().toBigInteger().mod(n);
    return v.equals(r);
}
Also used : BigInteger(java.math.BigInteger) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters)

Aggregations

ECPoint (org.bouncycastle.math.ec.ECPoint)13 BigInteger (java.math.BigInteger)9 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)8 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)5 AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)3 ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)3 ECPublicKeySpec (org.bouncycastle.jce.spec.ECPublicKeySpec)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 ECFieldElement (org.bouncycastle.math.ec.ECFieldElement)2 GeneralSecurityException (java.security.GeneralSecurityException)1 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)1 ECCurve (org.bouncycastle.math.ec.ECCurve)1 ECDSAPublicKey (org.xdi.oxauth.model.crypto.signature.ECDSAPublicKey)1 SignatureException (org.xdi.oxauth.model.exception.SignatureException)1