@@ -168,27 +168,26 @@ public int getOutputSize(
168168 /**
169169 * process a single byte, producing an output block if necessary.
170170 *
171- * @param in the input byte.
172- * @param out the space for any output that might be produced.
171+ * @param in the input byte.
172+ * @param out the space for any output that might be produced.
173173 * @param outOff the offset from which the output will be copied.
174174 * @return the number of output bytes copied to out.
175- * @throws DataLengthException if there isn't enough space in out.
176- * @throws IllegalStateException if the cipher isn't initialised.
175+ * @exception DataLengthException if there isn't enough space in out.
176+ * @exception IllegalStateException if the cipher isn't initialised.
177177 */
178178 public int processByte (
179- byte in ,
180- byte [] out ,
181- int outOff )
179+ byte in ,
180+ byte [] out ,
181+ int outOff )
182182 throws DataLengthException , IllegalStateException
183183 {
184- int resultLen = 0 ;
184+ int resultLen = 0 ;
185185
186186 buf [bufOff ++] = in ;
187187
188188 if (bufOff == buf .length )
189189 {
190- resultLen = cipher .processBlock (buf , 0 , out , outOff );
191- bufOff = 0 ;
190+ resultLen = processBuffer (out , outOff );
192191 }
193192
194193 return resultLen ;
@@ -197,30 +196,30 @@ public int processByte(
197196 /**
198197 * process an array of bytes, producing output if necessary.
199198 *
200- * @param in the input byte array.
201- * @param inOff the offset at which the input data starts.
202- * @param len the number of bytes to be copied out of the input array.
203- * @param out the space for any output that might be produced.
199+ * @param in the input byte array.
200+ * @param inOff the offset at which the input data starts.
201+ * @param len the number of bytes to be copied out of the input array.
202+ * @param out the space for any output that might be produced.
204203 * @param outOff the offset from which the output will be copied.
205204 * @return the number of output bytes copied to out.
206- * @throws DataLengthException if there isn't enough space in out.
207- * @throws IllegalStateException if the cipher isn't initialised.
205+ * @exception DataLengthException if there isn't enough space in out.
206+ * @exception IllegalStateException if the cipher isn't initialised.
208207 */
209208 public int processBytes (
210- byte [] in ,
211- int inOff ,
212- int len ,
213- byte [] out ,
214- int outOff )
209+ byte [] in ,
210+ int inOff ,
211+ int len ,
212+ byte [] out ,
213+ int outOff )
215214 throws DataLengthException , IllegalStateException
216215 {
217216 if (len < 0 )
218217 {
219218 throw new IllegalArgumentException ("Can't have a negative input length!" );
220219 }
221220
222- int blockSize = getBlockSize ();
223- int length = getUpdateOutputSize (len );
221+ int blockSize = getBlockSize ();
222+ int length = getUpdateOutputSize (len );
224223
225224 if (length > 0 )
226225 {
@@ -235,29 +234,35 @@ public int processBytes(
235234
236235 if (len > gapLen )
237236 {
238- System .arraycopy (in , inOff , buf , bufOff , gapLen );
239- inOff += gapLen ;
240- len -= gapLen ;
237+ if (bufOff != 0 )
238+ {
239+ System .arraycopy (in , inOff , buf , bufOff , gapLen );
240+ inOff += gapLen ;
241+ len -= gapLen ;
242+ }
243+
241244 if (in == out && Arrays .segmentsOverlap (inOff , len , outOff , length ))
242245 {
243246 in = new byte [len ];
244247 System .arraycopy (out , inOff , in , 0 , len );
245248 inOff = 0 ;
246249 }
247250
248- resultLen += cipher .processBlock (buf , 0 , out , outOff );
249-
250- bufOff = 0 ;
251+ // if bufOff non-zero buffer must now be full
252+ if (bufOff != 0 )
253+ {
254+ resultLen += processBuffer (out , outOff );
255+ }
251256
252257 if (mbCipher != null )
253258 {
254- int blockCount = len / mbCipher . getMultiBlockSize () ;
259+ int blockCount = len / blockSize ;
255260
256261 if (blockCount > 0 )
257262 {
258263 resultLen += mbCipher .processBlocks (in , inOff , blockCount , out , outOff + resultLen );
259264
260- int processed = blockCount * mbCipher . getMultiBlockSize () ;
265+ int processed = blockCount * blockSize ;
261266
262267 len -= processed ;
263268 inOff += processed ;
@@ -281,13 +286,25 @@ public int processBytes(
281286
282287 if (bufOff == buf .length )
283288 {
284- resultLen += cipher .processBlock (buf , 0 , out , outOff + resultLen );
285- bufOff = 0 ;
289+ resultLen += processBuffer (out , outOff + resultLen );
286290 }
287291
288292 return resultLen ;
289293 }
290294
295+ private int processBuffer (byte [] out , int outOff )
296+ {
297+ bufOff = 0 ;
298+ if (mbCipher != null )
299+ {
300+ return mbCipher .processBlocks (buf , 0 , buf .length / mbCipher .getBlockSize (), out , outOff );
301+ }
302+ else
303+ {
304+ return cipher .processBlock (buf , 0 , out , outOff );
305+ }
306+ }
307+
291308 /**
292309 * Process the last block in the buffer.
293310 *
@@ -318,15 +335,26 @@ public int doFinal(
318335
319336 if (bufOff != 0 )
320337 {
321- if (!partialBlockOkay )
338+ int index = 0 ;
339+ if (mbCipher != null )
322340 {
323- throw new DataLengthException ("data not block size aligned" );
341+ int nBlocks = bufOff / mbCipher .getBlockSize ();
342+ resultLen += mbCipher .processBlocks (buf , 0 , nBlocks , out , outOff );
343+ index = nBlocks * mbCipher .getBlockSize ();
324344 }
325345
326- cipher .processBlock (buf , 0 , buf , 0 );
327- resultLen = bufOff ;
328- bufOff = 0 ;
329- System .arraycopy (buf , 0 , out , outOff , resultLen );
346+ if (bufOff != index )
347+ {
348+ if (!partialBlockOkay )
349+ {
350+ throw new DataLengthException ("data not block size aligned" );
351+ }
352+
353+ cipher .processBlock (buf , index , buf , index );
354+ resultLen += bufOff - index ;
355+ bufOff = 0 ;
356+ System .arraycopy (buf , index , out , outOff , resultLen );
357+ }
330358 }
331359
332360 return resultLen ;
0 commit comments