Search in sources :

Example 1 with ECPublicKeyParameters

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

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 2 with ECPublicKeyParameters

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

the class ECUtil method generatePublicKeyParameter.

public static AsymmetricKeyParameter generatePublicKeyParameter(PublicKey key) throws InvalidKeyException {
    if (key instanceof ECPublicKey) {
        ECPublicKey k = (ECPublicKey) key;
        ECParameterSpec s = k.getParameters();
        if (s == null) {
            s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
            return new ECPublicKeyParameters(((BCECPublicKey) k).engineGetQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        } else {
            return new ECPublicKeyParameters(k.getQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        }
    } else if (key instanceof java.security.interfaces.ECPublicKey) {
        java.security.interfaces.ECPublicKey pubKey = (java.security.interfaces.ECPublicKey) key;
        ECParameterSpec s = EC5Util.convertSpec(pubKey.getParams(), false);
        return new ECPublicKeyParameters(EC5Util.convertPoint(pubKey.getParams(), pubKey.getW(), false), 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 public key");
            }
            PublicKey publicKey = BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
            if (publicKey instanceof java.security.interfaces.ECPublicKey) {
                return ECUtil.generatePublicKeyParameter(publicKey);
            }
        } catch (Exception e) {
            throw new InvalidKeyException("cannot identify EC public key: " + e.toString());
        }
    }
    throw new InvalidKeyException("cannot identify EC public key.");
}
Also used : ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) PublicKey(java.security.PublicKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) InvalidKeyException(java.security.InvalidKeyException) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters) InvalidKeyException(java.security.InvalidKeyException)

Example 3 with ECPublicKeyParameters

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

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 4 with ECPublicKeyParameters

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

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);
    // components must be bogus.
    if (point.isInfinity()) {
        return false;
    }
    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)

Example 5 with ECPublicKeyParameters

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

Aggregations

ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)12 ECPoint (org.bouncycastle.math.ec.ECPoint)8 BigInteger (java.math.BigInteger)7 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)4 AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)3 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)3 InvalidKeyException (java.security.InvalidKeyException)2 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)2 DEROctetString (org.bouncycastle.asn1.DEROctetString)2 DHParameter (org.bouncycastle.asn1.pkcs.DHParameter)2 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)2 DSAParameter (org.bouncycastle.asn1.x509.DSAParameter)2 DHDomainParameters (org.bouncycastle.asn1.x9.DHDomainParameters)2 DHPublicKey (org.bouncycastle.asn1.x9.DHPublicKey)2 DHValidationParms (org.bouncycastle.asn1.x9.DHValidationParms)2 X962Parameters (org.bouncycastle.asn1.x9.X962Parameters)2 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)2 X9ECPoint (org.bouncycastle.asn1.x9.X9ECPoint)2 DHParameters (org.bouncycastle.crypto.params.DHParameters)2 DHPublicKeyParameters (org.bouncycastle.crypto.params.DHPublicKeyParameters)2