Skip to content

Commit 7da1e85

Browse files
author
gefeili
committed
refactor of processDecryption
1 parent 79e98f3 commit 7da1e85

File tree

1 file changed

+24
-55
lines changed

1 file changed

+24
-55
lines changed

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

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ public int getLen()
323323
@Override
324324
public void reset()
325325
{
326-
327326
}
328327
}
329328

@@ -548,60 +547,30 @@ protected int processEncDecBytes(byte[] input, int inOff, int len, byte[] output
548547

549548
private int processDecryption(byte[] input, int inOff, int len, byte[] output, int outOff)
550549
{
551-
int resultLength = 0, available = m_bufferSizeDecrypt - m_bufPos;
552-
if (MAC_SIZE > BlockSize)
550+
int resultLength = 0, available;
551+
552+
// loop will run more than once for the following situation: pb128, ascon80pq, ascon128, ISAP_A_128(A)
553+
while (processor.isLengthExceedingBlockSize(m_bufPos, BlockSize)
554+
&& processor.isLengthExceedingBlockSize(len + m_bufPos, m_bufferSizeDecrypt))
553555
{
554-
// situation: pb128, ascon80pq, ascon128, ISAP_A_128(A)
555-
// If the mac size is greater than the block size, process the data in m_buf in the loop until
556-
// there is nearly mac_size data left
557-
while (processor.isLengthExceedingBlockSize(m_bufPos, BlockSize)
558-
&& processor.isLengthExceedingBlockSize(len + m_bufPos, m_bufferSizeDecrypt))
559-
{
560-
processBufferDecrypt(m_buf, resultLength, output, outOff + resultLength);
561-
m_bufPos -= BlockSize;
562-
resultLength += BlockSize;
563-
}
564-
if (m_bufPos > 0)
565-
{
566-
System.arraycopy(m_buf, resultLength, m_buf, 0, m_bufPos);
567-
if (processor.isLengthExceedingBlockSize(m_bufPos + len, m_bufferSizeDecrypt))
568-
{
569-
available = Math.max(BlockSize - m_bufPos, 0);
570-
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
571-
inOff += available;
572-
processBufferDecrypt(m_buf, 0, output, outOff + resultLength);
573-
}
574-
else
575-
{
576-
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
577-
m_bufPos += len;
578-
return -1;
579-
}
580-
}
556+
processBufferDecrypt(m_buf, resultLength, output, outOff + resultLength);
557+
m_bufPos -= BlockSize;
558+
resultLength += BlockSize;
581559
}
582-
else
560+
561+
if (m_bufPos > 0)
583562
{
584-
if (m_bufPos > 0)
563+
System.arraycopy(m_buf, resultLength, m_buf, 0, m_bufPos);
564+
if (processor.isLengthWithinAvailableSpace(m_bufPos + len, m_bufferSizeDecrypt))
585565
{
586-
if (processor.isLengthExceedingBlockSize(m_bufPos, BlockSize))
587-
{
588-
processBufferDecrypt(m_buf, 0, output, outOff);
589-
m_bufPos -= BlockSize;
590-
System.arraycopy(m_buf, BlockSize, m_buf, 0, m_bufPos);
591-
resultLength = BlockSize;
592-
available += BlockSize;
593-
if (processor.isLengthWithinAvailableSpace(len, available))
594-
{
595-
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
596-
m_bufPos += len;
597-
return -1;
598-
}
599-
}
600-
available = Math.max(BlockSize - m_bufPos, 0);
601-
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
602-
inOff += available;
603-
processBufferDecrypt(m_buf, 0, output, outOff + resultLength);
566+
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
567+
m_bufPos += len;
568+
return -1;
604569
}
570+
available = Math.max(BlockSize - m_bufPos, 0);
571+
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
572+
inOff += available;
573+
processBufferDecrypt(m_buf, 0, output, outOff + resultLength);
605574
}
606575
return inOff;
607576
}
@@ -649,7 +618,7 @@ public int doFinal(byte[] output, int outOff)
649618
return resultLength;
650619
}
651620

652-
public int getBlockSize()
621+
public final int getBlockSize()
653622
{
654623
return BlockSize;
655624
}
@@ -739,7 +708,7 @@ protected boolean checkData(boolean isDoFinal)
739708

740709
protected abstract void finishAAD(State nextState, boolean isDoFinal);
741710

742-
protected void bufferReset()
711+
protected final void bufferReset()
743712
{
744713
if (m_buf != null)
745714
{
@@ -773,23 +742,23 @@ protected void bufferReset()
773742
dataOperator.reset();
774743
}
775744

776-
protected void ensureSufficientOutputBuffer(byte[] output, int outOff, int len)
745+
protected final void ensureSufficientOutputBuffer(byte[] output, int outOff, int len)
777746
{
778747
if (len >= BlockSize && outOff + len > output.length)
779748
{
780749
throw new OutputLengthException("output buffer too short");
781750
}
782751
}
783752

784-
protected void ensureSufficientInputBuffer(byte[] input, int inOff, int len)
753+
protected final void ensureSufficientInputBuffer(byte[] input, int inOff, int len)
785754
{
786755
if (inOff + len > input.length)
787756
{
788757
throw new DataLengthException("input buffer too short");
789758
}
790759
}
791760

792-
protected void ensureInitialized()
761+
protected final void ensureInitialized()
793762
{
794763
if (m_state == State.Uninitialized)
795764
{

0 commit comments

Comments
 (0)