Search in sources :

Example 6 with AsymmetricCipherKeyPair

use of org.bouncycastle.crypto.AsymmetricCipherKeyPair in project robovm by robovm.

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    RSAKeyParameters pub = (RSAKeyParameters) pair.getPublic();
    RSAPrivateCrtKeyParameters priv = (RSAPrivateCrtKeyParameters) pair.getPrivate();
    return new KeyPair(new BCRSAPublicKey(pub), new BCRSAPrivateCrtKey(priv));
}
Also used : KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) RSAKeyParameters(org.bouncycastle.crypto.params.RSAKeyParameters) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) RSAPrivateCrtKeyParameters(org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)

Example 7 with AsymmetricCipherKeyPair

use of org.bouncycastle.crypto.AsymmetricCipherKeyPair in project robovm by robovm.

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        Integer paramStrength = Integers.valueOf(strength);
        if (params.containsKey(paramStrength)) {
            param = (DHKeyGenerationParameters) params.get(paramStrength);
        } else {
            DHParameterSpec dhParams = BouncyCastleProvider.CONFIGURATION.getDHDefaultParameters(strength);
            if (dhParams != null) {
                param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));
            } else {
                synchronized (lock) {
                    // our key size.
                    if (params.containsKey(paramStrength)) {
                        param = (DHKeyGenerationParameters) params.get(paramStrength);
                    } else {
                        DHParametersGenerator pGen = new DHParametersGenerator();
                        pGen.init(strength, certainty, random);
                        param = new DHKeyGenerationParameters(random, pGen.generateParameters());
                        params.put(paramStrength, param);
                    }
                }
            }
        }
        engine.init(param);
        initialised = true;
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic();
    DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate();
    return new KeyPair(new BCDHPublicKey(pub), new BCDHPrivateKey(priv));
}
Also used : KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) DHPublicKeyParameters(org.bouncycastle.crypto.params.DHPublicKeyParameters) DHParameters(org.bouncycastle.crypto.params.DHParameters) DHPrivateKeyParameters(org.bouncycastle.crypto.params.DHPrivateKeyParameters) DHKeyGenerationParameters(org.bouncycastle.crypto.params.DHKeyGenerationParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec) DHParametersGenerator(org.bouncycastle.crypto.generators.DHParametersGenerator) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair)

Example 8 with AsymmetricCipherKeyPair

use of org.bouncycastle.crypto.AsymmetricCipherKeyPair in project robovm by robovm.

the class RSAKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    BigInteger p, q, n, d, e, pSub1, qSub1, phi;
    //
    // p and q values should have a length of half the strength in bits
    //
    int strength = param.getStrength();
    int pbitlength = (strength + 1) / 2;
    int qbitlength = strength - pbitlength;
    int mindiffbits = strength / 3;
    e = param.getPublicExponent();
    //
    for (; ; ) {
        p = new BigInteger(pbitlength, 1, param.getRandom());
        if (p.mod(e).equals(ONE)) {
            continue;
        }
        if (!p.isProbablePrime(param.getCertainty())) {
            continue;
        }
        if (e.gcd(p.subtract(ONE)).equals(ONE)) {
            break;
        }
    }
    //
    for (; ; ) {
        //
        for (; ; ) {
            q = new BigInteger(qbitlength, 1, param.getRandom());
            if (q.subtract(p).abs().bitLength() < mindiffbits) {
                continue;
            }
            if (q.mod(e).equals(ONE)) {
                continue;
            }
            if (!q.isProbablePrime(param.getCertainty())) {
                continue;
            }
            if (e.gcd(q.subtract(ONE)).equals(ONE)) {
                break;
            }
        }
        //
        // calculate the modulus
        //
        n = p.multiply(q);
        if (n.bitLength() == param.getStrength()) {
            break;
        }
        //
        // if we get here our primes aren't big enough, make the largest
        // of the two p and try again
        //
        p = p.max(q);
    }
    if (p.compareTo(q) < 0) {
        phi = p;
        p = q;
        q = phi;
    }
    pSub1 = p.subtract(ONE);
    qSub1 = q.subtract(ONE);
    phi = pSub1.multiply(qSub1);
    //
    // calculate the private exponent
    //
    d = e.modInverse(phi);
    //
    // calculate the CRT factors
    //
    BigInteger dP, dQ, qInv;
    dP = d.remainder(pSub1);
    dQ = d.remainder(qSub1);
    qInv = q.modInverse(p);
    return new AsymmetricCipherKeyPair(new RSAKeyParameters(false, n, e), new RSAPrivateCrtKeyParameters(n, e, d, p, q, dP, dQ, qInv));
}
Also used : BigInteger(java.math.BigInteger) RSAKeyParameters(org.bouncycastle.crypto.params.RSAKeyParameters) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) RSAPrivateCrtKeyParameters(org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)

Example 9 with AsymmetricCipherKeyPair

use of org.bouncycastle.crypto.AsymmetricCipherKeyPair in project robovm by robovm.

the class DHBasicKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
    DHParameters dhp = param.getParameters();
    BigInteger x = helper.calculatePrivate(dhp, param.getRandom());
    BigInteger y = helper.calculatePublic(dhp, x);
    return new AsymmetricCipherKeyPair(new DHPublicKeyParameters(y, dhp), new DHPrivateKeyParameters(x, dhp));
}
Also used : DHPublicKeyParameters(org.bouncycastle.crypto.params.DHPublicKeyParameters) DHParameters(org.bouncycastle.crypto.params.DHParameters) DHPrivateKeyParameters(org.bouncycastle.crypto.params.DHPrivateKeyParameters) BigInteger(java.math.BigInteger) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair)

Example 10 with AsymmetricCipherKeyPair

use of org.bouncycastle.crypto.AsymmetricCipherKeyPair in project robovm by robovm.

the class DSAKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    DSAParameters dsaParams = param.getParameters();
    BigInteger x = generatePrivateKey(dsaParams.getQ(), param.getRandom());
    BigInteger y = calculatePublicKey(dsaParams.getP(), dsaParams.getG(), x);
    return new AsymmetricCipherKeyPair(new DSAPublicKeyParameters(y, dsaParams), new DSAPrivateKeyParameters(x, dsaParams));
}
Also used : DSAPublicKeyParameters(org.bouncycastle.crypto.params.DSAPublicKeyParameters) DSAPrivateKeyParameters(org.bouncycastle.crypto.params.DSAPrivateKeyParameters) BigInteger(java.math.BigInteger) DSAParameters(org.bouncycastle.crypto.params.DSAParameters) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair)

Aggregations

AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)13 BigInteger (java.math.BigInteger)10 DHParameters (org.bouncycastle.crypto.params.DHParameters)4 DHPrivateKeyParameters (org.bouncycastle.crypto.params.DHPrivateKeyParameters)4 DHPublicKeyParameters (org.bouncycastle.crypto.params.DHPublicKeyParameters)4 KeyPair (java.security.KeyPair)3 DSAPrivateKeyParameters (org.bouncycastle.crypto.params.DSAPrivateKeyParameters)3 DSAPublicKeyParameters (org.bouncycastle.crypto.params.DSAPublicKeyParameters)3 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)3 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)3 RSAKeyParameters (org.bouncycastle.crypto.params.RSAKeyParameters)3 RSAPrivateCrtKeyParameters (org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)3 ECPoint (org.bouncycastle.math.ec.ECPoint)3 DSAParameters (org.bouncycastle.crypto.params.DSAParameters)2 DHParameterSpec (javax.crypto.spec.DHParameterSpec)1 DHParametersGenerator (org.bouncycastle.crypto.generators.DHParametersGenerator)1 DSAParametersGenerator (org.bouncycastle.crypto.generators.DSAParametersGenerator)1 DHKeyGenerationParameters (org.bouncycastle.crypto.params.DHKeyGenerationParameters)1 DSAKeyGenerationParameters (org.bouncycastle.crypto.params.DSAKeyGenerationParameters)1