Search in sources :

Example 6 with RSAPrivateCrtKeyParameters

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

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

the class RSACoreEngine method processBlock.

public BigInteger processBlock(BigInteger input) {
    if (key instanceof RSAPrivateCrtKeyParameters) {
        //
        // we have the extra factors, use the Chinese Remainder Theorem - the author
        // wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for
        // advice regarding the expression of this.
        //
        RSAPrivateCrtKeyParameters crtKey = (RSAPrivateCrtKeyParameters) key;
        BigInteger p = crtKey.getP();
        BigInteger q = crtKey.getQ();
        BigInteger dP = crtKey.getDP();
        BigInteger dQ = crtKey.getDQ();
        BigInteger qInv = crtKey.getQInv();
        BigInteger mP, mQ, h, m;
        // mP = ((input mod p) ^ dP)) mod p
        mP = (input.remainder(p)).modPow(dP, p);
        // mQ = ((input mod q) ^ dQ)) mod q
        mQ = (input.remainder(q)).modPow(dQ, q);
        // h = qInv * (mP - mQ) mod p
        h = mP.subtract(mQ);
        h = h.multiply(qInv);
        // mod (in Java) returns the positive residual
        h = h.mod(p);
        // m = h * q + mQ
        m = h.multiply(q);
        m = m.add(mQ);
        return m;
    } else {
        return input.modPow(key.getExponent(), key.getModulus());
    }
}
Also used : BigInteger(java.math.BigInteger) RSAPrivateCrtKeyParameters(org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)

Example 8 with RSAPrivateCrtKeyParameters

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

the class RSABlindedEngine method processBlock.

/**
     * Process a single block using the basic RSA algorithm.
     *
     * @param in the input array.
     * @param inOff the offset into the input buffer where the data starts.
     * @param inLen the length of the data to be processed.
     * @return the result of the RSA process.
     * @exception DataLengthException the input block is too large.
     */
public byte[] processBlock(byte[] in, int inOff, int inLen) {
    if (key == null) {
        throw new IllegalStateException("RSA engine not initialised");
    }
    BigInteger input = core.convertInput(in, inOff, inLen);
    BigInteger result;
    if (key instanceof RSAPrivateCrtKeyParameters) {
        RSAPrivateCrtKeyParameters k = (RSAPrivateCrtKeyParameters) key;
        BigInteger e = k.getPublicExponent();
        if (// can't do blinding without a public exponent
        e != null) {
            BigInteger m = k.getModulus();
            BigInteger r = BigIntegers.createRandomInRange(ONE, m.subtract(ONE), random);
            BigInteger blindedInput = r.modPow(e, m).multiply(input).mod(m);
            BigInteger blindedResult = core.processBlock(blindedInput);
            BigInteger rInv = r.modInverse(m);
            result = blindedResult.multiply(rInv).mod(m);
        } else {
            result = core.processBlock(input);
        }
    } else {
        result = core.processBlock(input);
    }
    return core.convertOutput(result);
}
Also used : BigInteger(java.math.BigInteger) RSAPrivateCrtKeyParameters(org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)

Example 9 with RSAPrivateCrtKeyParameters

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

the class RSACoreEngine method processBlock.

public BigInteger processBlock(BigInteger input) {
    if (key instanceof RSAPrivateCrtKeyParameters) {
        //
        // we have the extra factors, use the Chinese Remainder Theorem - the author
        // wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for
        // advice regarding the expression of this.
        //
        RSAPrivateCrtKeyParameters crtKey = (RSAPrivateCrtKeyParameters) key;
        BigInteger p = crtKey.getP();
        BigInteger q = crtKey.getQ();
        BigInteger dP = crtKey.getDP();
        BigInteger dQ = crtKey.getDQ();
        BigInteger qInv = crtKey.getQInv();
        BigInteger mP, mQ, h, m;
        // mP = ((input mod p) ^ dP)) mod p
        mP = (input.remainder(p)).modPow(dP, p);
        // mQ = ((input mod q) ^ dQ)) mod q
        mQ = (input.remainder(q)).modPow(dQ, q);
        // h = qInv * (mP - mQ) mod p
        h = mP.subtract(mQ);
        h = h.multiply(qInv);
        // mod (in Java) returns the positive residual
        h = h.mod(p);
        // m = h * q + mQ
        m = h.multiply(q);
        m = m.add(mQ);
        return m;
    } else {
        return input.modPow(key.getExponent(), key.getModulus());
    }
}
Also used : BigInteger(java.math.BigInteger) RSAPrivateCrtKeyParameters(org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)

Example 10 with RSAPrivateCrtKeyParameters

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

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)

Aggregations

RSAPrivateCrtKeyParameters (org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)10 BigInteger (java.math.BigInteger)9 AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)3 RSAKeyParameters (org.bouncycastle.crypto.params.RSAKeyParameters)3 DHParameter (org.bouncycastle.asn1.pkcs.DHParameter)2 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)2 DSAParameter (org.bouncycastle.asn1.x509.DSAParameter)2 X962Parameters (org.bouncycastle.asn1.x9.X962Parameters)2 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)2 DHParameters (org.bouncycastle.crypto.params.DHParameters)2 DHPrivateKeyParameters (org.bouncycastle.crypto.params.DHPrivateKeyParameters)2 DSAParameters (org.bouncycastle.crypto.params.DSAParameters)2 DSAPrivateKeyParameters (org.bouncycastle.crypto.params.DSAPrivateKeyParameters)2 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)2 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)2 KeyPair (java.security.KeyPair)1 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)1 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)1 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)1 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)1