Search in sources :

Example 1 with ECPrivateKeyParameters

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

the class PrivateKeyFactory method createKey.

/**
     * Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
     * 
     * @param keyInfo the PrivateKeyInfo object containing the key material
     * @return a suitable private key parameter
     * @throws IOException on an error decoding the key
     */
public static AsymmetricKeyParameter createKey(PrivateKeyInfo keyInfo) throws IOException {
    AlgorithmIdentifier algId = keyInfo.getAlgorithmId();
    if (algId.getAlgorithm().equals(PKCSObjectIdentifiers.rsaEncryption)) {
        RSAPrivateKeyStructure keyStructure = new RSAPrivateKeyStructure((ASN1Sequence) keyInfo.getPrivateKey());
        return new RSAPrivateCrtKeyParameters(keyStructure.getModulus(), keyStructure.getPublicExponent(), keyStructure.getPrivateExponent(), keyStructure.getPrime1(), keyStructure.getPrime2(), keyStructure.getExponent1(), keyStructure.getExponent2(), keyStructure.getCoefficient());
    } else //      else if (algId.getObjectId().equals(X9ObjectIdentifiers.dhpublicnumber))
    if (algId.getObjectId().equals(PKCSObjectIdentifiers.dhKeyAgreement)) {
        DHParameter params = new DHParameter((ASN1Sequence) keyInfo.getAlgorithmId().getParameters());
        DERInteger derX = (DERInteger) keyInfo.getPrivateKey();
        BigInteger lVal = params.getL();
        int l = lVal == null ? 0 : lVal.intValue();
        DHParameters dhParams = new DHParameters(params.getP(), params.getG(), null, l);
        return new DHPrivateKeyParameters(derX.getValue(), dhParams);
    } else // END android-removed
    if (algId.getObjectId().equals(X9ObjectIdentifiers.id_dsa)) {
        DERInteger derX = (DERInteger) keyInfo.getPrivateKey();
        DEREncodable de = keyInfo.getAlgorithmId().getParameters();
        DSAParameters parameters = null;
        if (de != null) {
            DSAParameter params = DSAParameter.getInstance(de.getDERObject());
            parameters = new DSAParameters(params.getP(), params.getQ(), params.getG());
        }
        return new DSAPrivateKeyParameters(derX.getValue(), parameters);
    } else if (algId.getObjectId().equals(X9ObjectIdentifiers.id_ecPublicKey)) {
        X962Parameters params = new X962Parameters((DERObject) keyInfo.getAlgorithmId().getParameters());
        ECDomainParameters dParams = null;
        if (params.isNamedCurve()) {
            DERObjectIdentifier oid = (DERObjectIdentifier) params.getParameters();
            X9ECParameters ecP = X962NamedCurves.getByOID(oid);
            if (ecP == null) {
                ecP = SECNamedCurves.getByOID(oid);
                if (ecP == null) {
                    ecP = NISTNamedCurves.getByOID(oid);
                // BEGIN android-removed
                // if (ecP == null)
                // {
                //     ecP = TeleTrusTNamedCurves.getByOID(oid);
                // }
                // END android-removed
                }
            }
            dParams = new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
        } else {
            X9ECParameters ecP = new X9ECParameters((ASN1Sequence) params.getParameters());
            dParams = new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
        }
        ECPrivateKeyStructure ec = new ECPrivateKeyStructure((ASN1Sequence) keyInfo.getPrivateKey());
        return new ECPrivateKeyParameters(ec.getKey(), dParams);
    } else {
        throw new RuntimeException("algorithm identifier in key not recognised");
    }
}
Also used : ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) DHParameters(org.bouncycastle.crypto.params.DHParameters) DHPrivateKeyParameters(org.bouncycastle.crypto.params.DHPrivateKeyParameters) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) ECPrivateKeyStructure(org.bouncycastle.asn1.sec.ECPrivateKeyStructure) DERObjectIdentifier(org.bouncycastle.asn1.DERObjectIdentifier) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) DERInteger(org.bouncycastle.asn1.DERInteger) X962Parameters(org.bouncycastle.asn1.x9.X962Parameters) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) DERObject(org.bouncycastle.asn1.DERObject) RSAPrivateKeyStructure(org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure) DEREncodable(org.bouncycastle.asn1.DEREncodable) DSAPrivateKeyParameters(org.bouncycastle.crypto.params.DSAPrivateKeyParameters) BigInteger(java.math.BigInteger) DSAParameter(org.bouncycastle.asn1.x509.DSAParameter) DHParameter(org.bouncycastle.asn1.pkcs.DHParameter) DSAParameters(org.bouncycastle.crypto.params.DSAParameters) RSAPrivateCrtKeyParameters(org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)

Example 2 with ECPrivateKeyParameters

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

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.
     */
public BigInteger[] generateSignature(byte[] message) {
    BigInteger n = key.getParameters().getN();
    BigInteger e = calculateE(n, message);
    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) || k.compareTo(n) >= 0);
            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.bouncycastle.crypto.params.ECPrivateKeyParameters) BigInteger(java.math.BigInteger) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPoint(org.bouncycastle.math.ec.ECPoint)

Example 3 with ECPrivateKeyParameters

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

the class PrivateKeyFactory method createKey.

/**
     * Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
     * 
     * @param keyInfo the PrivateKeyInfo object containing the key material
     * @return a suitable private key parameter
     * @throws IOException on an error decoding the key
     */
public static AsymmetricKeyParameter createKey(PrivateKeyInfo keyInfo) throws IOException {
    AlgorithmIdentifier algId = keyInfo.getPrivateKeyAlgorithm();
    if (algId.getAlgorithm().equals(PKCSObjectIdentifiers.rsaEncryption)) {
        RSAPrivateKey keyStructure = RSAPrivateKey.getInstance(keyInfo.parsePrivateKey());
        return new RSAPrivateCrtKeyParameters(keyStructure.getModulus(), keyStructure.getPublicExponent(), keyStructure.getPrivateExponent(), keyStructure.getPrime1(), keyStructure.getPrime2(), keyStructure.getExponent1(), keyStructure.getExponent2(), keyStructure.getCoefficient());
    } else //      else if (algId.getObjectId().equals(X9ObjectIdentifiers.dhpublicnumber))
    if (algId.getAlgorithm().equals(PKCSObjectIdentifiers.dhKeyAgreement)) {
        DHParameter params = DHParameter.getInstance(algId.getParameters());
        ASN1Integer derX = (ASN1Integer) keyInfo.parsePrivateKey();
        BigInteger lVal = params.getL();
        int l = lVal == null ? 0 : lVal.intValue();
        DHParameters dhParams = new DHParameters(params.getP(), params.getG(), null, l);
        return new DHPrivateKeyParameters(derX.getValue(), dhParams);
    } else // END android-removed
    if (algId.getAlgorithm().equals(X9ObjectIdentifiers.id_dsa)) {
        ASN1Integer derX = (ASN1Integer) keyInfo.parsePrivateKey();
        ASN1Encodable de = algId.getParameters();
        DSAParameters parameters = null;
        if (de != null) {
            DSAParameter params = DSAParameter.getInstance(de.toASN1Primitive());
            parameters = new DSAParameters(params.getP(), params.getQ(), params.getG());
        }
        return new DSAPrivateKeyParameters(derX.getValue(), parameters);
    } else if (algId.getAlgorithm().equals(X9ObjectIdentifiers.id_ecPublicKey)) {
        X962Parameters params = new X962Parameters((ASN1Primitive) algId.getParameters());
        X9ECParameters x9;
        if (params.isNamedCurve()) {
            ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
            x9 = X962NamedCurves.getByOID(oid);
            if (x9 == null) {
                x9 = SECNamedCurves.getByOID(oid);
                if (x9 == null) {
                    x9 = NISTNamedCurves.getByOID(oid);
                // BEGIN android-removed
                // if (x9 == null)
                // {
                //     x9 = TeleTrusTNamedCurves.getByOID(oid);
                // }
                // END android-removed
                }
            }
        } else {
            x9 = X9ECParameters.getInstance(params.getParameters());
        }
        ECPrivateKey ec = ECPrivateKey.getInstance(keyInfo.parsePrivateKey());
        BigInteger d = ec.getKey();
        // TODO We lose any named parameters here
        ECDomainParameters dParams = new ECDomainParameters(x9.getCurve(), x9.getG(), x9.getN(), x9.getH(), x9.getSeed());
        return new ECPrivateKeyParameters(d, dParams);
    } else {
        throw new RuntimeException("algorithm identifier in key not recognised");
    }
}
Also used : ECPrivateKey(org.bouncycastle.asn1.sec.ECPrivateKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) DHParameters(org.bouncycastle.crypto.params.DHParameters) DHPrivateKeyParameters(org.bouncycastle.crypto.params.DHPrivateKeyParameters) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) X962Parameters(org.bouncycastle.asn1.x9.X962Parameters) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) DSAPrivateKeyParameters(org.bouncycastle.crypto.params.DSAPrivateKeyParameters) BigInteger(java.math.BigInteger) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) DSAParameter(org.bouncycastle.asn1.x509.DSAParameter) RSAPrivateKey(org.bouncycastle.asn1.pkcs.RSAPrivateKey) DHParameter(org.bouncycastle.asn1.pkcs.DHParameter) DSAParameters(org.bouncycastle.crypto.params.DSAParameters) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) RSAPrivateCrtKeyParameters(org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)

Example 4 with ECPrivateKeyParameters

use of org.bouncycastle.crypto.params.ECPrivateKeyParameters 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 5 with ECPrivateKeyParameters

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

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.
     */
public BigInteger[] generateSignature(byte[] message) {
    BigInteger n = key.getParameters().getN();
    BigInteger e = calculateE(n, message);
    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) || k.compareTo(n) >= 0);
            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.bouncycastle.crypto.params.ECPrivateKeyParameters) BigInteger(java.math.BigInteger) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPoint(org.bouncycastle.math.ec.ECPoint)

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