Skip to content

Commit dcf93ea

Browse files
committed
Fix elephantEngine for multiPart encryption/decryption
1 parent a912b69 commit dcf93ea

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

core/src/main/java/org/bouncycastle/crypto/engines/ElephantEngine.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
364364

365365
if (inputOff + len - (forEncryption ? 0 : CRYPTO_ABYTES) >= BLOCK_SIZE)
366366
{
367-
int mlen = inputOff + len - (forEncryption ? 0 : CRYPTO_ABYTES);
367+
int mlen = inputOff + messageLen + len - (forEncryption ? 0 : CRYPTO_ABYTES);
368368
int adlen = processAADBytes();
369369
int nblocks_c = 1 + mlen / BLOCK_SIZE;
370370
int nblocks_m = ((mlen % BLOCK_SIZE) != 0 ? nblocks_c : nblocks_c - 1);
@@ -374,16 +374,17 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
374374
System.arraycopy(inputMessage, 0, tempInput, 0, inputOff);
375375
System.arraycopy(input, inOff, tempInput, inputOff, Math.min(len, tempInput.length - inputOff));
376376
int rv = processBytes(tempInput, output, outOff, nb_it, nblocks_m, nblocks_c, mlen, nblocks_ad, false);
377-
if (rv >= inputOff)
377+
int copyLen = rv - inputOff;
378+
if (copyLen >= 0)
378379
{
379-
int copyLen = rv - inputOff;
380380
inputOff = inputOff + len - rv;
381381
System.arraycopy(input, inOff + copyLen, inputMessage, 0, inputOff);
382382
}
383383
else
384384
{
385-
System.arraycopy(input, inOff + rv, inputMessage, inputOff, len - rv);
386-
inputOff += len - rv;
385+
System.arraycopy(inputMessage, inputOff + copyLen, inputMessage, 0, -copyLen);
386+
System.arraycopy(input, inOff, inputMessage, -copyLen, len);
387+
inputOff = len - copyLen;
387388
}
388389
messageLen += rv;
389390
return rv;

0 commit comments

Comments
 (0)