1 package org.bouncycastle.crypto.engines;
3 import java.math.BigInteger;
5 import org.bouncycastle.crypto.CipherParameters;
6 import org.bouncycastle.crypto.DataLengthException;
7 import org.bouncycastle.crypto.AsymmetricBlockCipher;
8 import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
9 import org.bouncycastle.crypto.params.RSAKeyParameters;
10 import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
13 * this does your basic RSA algorithm.
15 public class RSAEngine
16 implements AsymmetricBlockCipher
18 private RSAKeyParameters key;
19 private boolean forEncryption;
22 * initialise the RSA engine.
24 * @param forEncryption true if we are encrypting, false otherwise.
25 * @param param the necessary RSA key parameters.
28 boolean forEncryption,
29 CipherParameters param)
31 this.key = (RSAKeyParameters)param;
32 this.forEncryption = forEncryption;
36 * Return the maximum size for an input block to this engine.
37 * For RSA this is always one byte less than the key size on
38 * encryption, and the same length as the key size on decryption.
40 * @return maximum size for an input block.
42 public int getInputBlockSize()
44 int bitSize = key.getModulus().bitLength();
48 return (bitSize + 7) / 8 - 1;
52 return (bitSize + 7) / 8;
57 * Return the maximum size for an output block to this engine.
58 * For RSA this is always one byte less than the key size on
59 * decryption, and the same length as the key size on encryption.
61 * @return maximum size for an output block.
63 public int getOutputBlockSize()
65 int bitSize = key.getModulus().bitLength();
69 return (bitSize + 7) / 8;
73 return (bitSize + 7) / 8 - 1;
78 * Process a single block using the basic RSA algorithm.
80 * @param in the input array.
81 * @param inOff the offset into the input buffer where the data starts.
82 * @param inLen the length of the data to be processed.
83 * @return the result of the RSA process.
84 * @exception DataLengthException the input block is too large.
86 public byte[] processBlock(
91 if (inLen > (getInputBlockSize() + 1))
93 throw new DataLengthException("input too large for RSA cipher.\n");
95 else if (inLen == (getInputBlockSize() + 1) && (in[inOff] & 0x80) != 0)
97 throw new DataLengthException("input too large for RSA cipher.\n");
102 if (inOff != 0 || inLen != in.length)
104 block = new byte[inLen];
106 System.arraycopy(in, inOff, block, 0, inLen);
113 BigInteger input = new BigInteger(1, block);
116 if (key instanceof RSAPrivateCrtKeyParameters)
119 // we have the extra factors, use the Chinese Remainder Theorem - the author
120 // wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for
121 // advice regarding the expression of this.
123 RSAPrivateCrtKeyParameters crtKey = (RSAPrivateCrtKeyParameters)key;
125 BigInteger d = crtKey.getExponent();
126 BigInteger p = crtKey.getP();
127 BigInteger q = crtKey.getQ();
128 BigInteger dP = crtKey.getDP();
129 BigInteger dQ = crtKey.getDQ();
130 BigInteger qInv = crtKey.getQInv();
132 BigInteger mP, mQ, h, m;
134 // mP = ((input mod p) ^ dP)) mod p
135 mP = (input.remainder(p)).modPow(dP, p);
137 // mQ = ((input mod q) ^ dQ)) mod q
138 mQ = (input.remainder(q)).modPow(dQ, q);
140 // h = qInv * (mP - mQ) mod p
142 h = h.multiply(qInv);
143 h = h.mod(p); // mod (in Java) returns the positive residual
149 output = m.toByteArray();
153 output = input.modPow(
154 key.getExponent(), key.getModulus()).toByteArray();
159 if (output[0] == 0 && output.length > getOutputBlockSize()) // have ended up with an extra zero byte, copy down.
161 byte[] tmp = new byte[output.length - 1];
163 System.arraycopy(output, 1, tmp, 0, tmp.length);
168 if (output.length < getOutputBlockSize()) // have ended up with less bytes than normal, lengthen
170 byte[] tmp = new byte[getOutputBlockSize()];
172 System.arraycopy(output, 0, tmp, tmp.length - output.length, output.length);
179 if (output[0] == 0) // have ended up with an extra zero byte, copy down.
181 byte[] tmp = new byte[output.length - 1];
183 System.arraycopy(output, 1, tmp, 0, tmp.length);