use of org.gudy.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters in project BiglyBT by BiglySoftware.
the class RSAEngine 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.
*/
@Override
public byte[] processBlock(byte[] in, int inOff, int inLen) {
if (inLen > (getInputBlockSize() + 1)) {
throw new DataLengthException("input too large for RSA cipher.\n");
} else if (inLen == (getInputBlockSize() + 1) && (in[inOff] & 0x80) != 0) {
throw new DataLengthException("input too large for RSA cipher.\n");
}
byte[] block;
if (inOff != 0 || inLen != in.length) {
block = new byte[inLen];
System.arraycopy(in, inOff, block, 0, inLen);
} else {
block = in;
}
BigInteger input = new BigInteger(1, block);
byte[] output;
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);
output = m.toByteArray();
} else {
output = input.modPow(key.getExponent(), key.getModulus()).toByteArray();
}
if (forEncryption) {
if (// have ended up with an extra zero byte, copy down.
output[0] == 0 && output.length > getOutputBlockSize()) {
byte[] tmp = new byte[output.length - 1];
System.arraycopy(output, 1, tmp, 0, tmp.length);
return tmp;
}
if (// have ended up with less bytes than normal, lengthen
output.length < getOutputBlockSize()) {
byte[] tmp = new byte[getOutputBlockSize()];
System.arraycopy(output, 0, tmp, tmp.length - output.length, output.length);
return tmp;
}
} else {
if (// have ended up with an extra zero byte, copy down.
output[0] == 0) {
byte[] tmp = new byte[output.length - 1];
System.arraycopy(output, 1, tmp, 0, tmp.length);
return tmp;
}
}
return output;
}
use of org.gudy.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters in project BiglyBT by BiglySoftware.
the class RSAKeyPairGenerator method generateKeyPair.
@Override
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 pbitlength = (param.getStrength() + 1) / 2;
int qbitlength = (param.getStrength() - pbitlength);
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.equals(p)) {
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