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));
}
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());
}
}
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);
}
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());
}
}
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));
}
Aggregations