@@ -13,9 +13,7 @@ abstract class AEADBufferBaseEngine
1313 protected enum ProcessingBufferType
1414 {
1515 Buffered , // Store a (aad) block size of input and process after the input size exceeds the buffer size
16- BufferedLargeMac , // handle the situation when mac size is larger than the block size, used for pb128
1716 Immediate , //process the input immediately when the input size is equal or greater than the block size
18- ImmediateLargeMac , // handle the situation when mac size is larger than the block size, used for ascon80pq, ascon128, ISAP_A_128(A)
1917 }
2018
2119 protected enum AADOperatorType
@@ -65,15 +63,9 @@ protected void setInnerMembers(ProcessingBufferType type, AADOperatorType aadOpe
6563 case Buffered :
6664 processor = new BufferedAADProcessor ();
6765 break ;
68- case BufferedLargeMac :
69- processor = new BufferedLargeMacAADProcessor ();
70- break ;
7166 case Immediate :
7267 processor = new ImmediateAADProcessor ();
7368 break ;
74- case ImmediateLargeMac :
75- processor = new ImmediateLargeMacAADProcessor ();
76- break ;
7769 }
7870
7971 m_bufferSizeDecrypt = BlockSize + MAC_SIZE ;
@@ -117,16 +109,14 @@ protected interface AADProcessingBuffer
117109 {
118110 void processAADByte (byte input );
119111
120- int processDecryptBytes (byte [] input , int inOff , int len , byte [] output , int outOff );
121-
122112 int getUpdateOutputSize (int len );
123113
124114 boolean isLengthWithinAvailableSpace (int len , int available );
125115
126116 boolean isLengthExceedingBlockSize (int len , int size );
127117 }
128118
129- private abstract class BufferedBaseAADProcessor
119+ private class BufferedAADProcessor
130120 implements AADProcessingBuffer
131121 {
132122 public void processAADByte (byte input )
@@ -159,27 +149,7 @@ public int getUpdateOutputSize(int len)
159149 }
160150 }
161151
162- private class BufferedAADProcessor
163- extends BufferedBaseAADProcessor
164- {
165- @ Override
166- public int processDecryptBytes (byte [] input , int inOff , int len , byte [] output , int outOff )
167- {
168- return processDecryptionWithSmallMacSize (input , inOff , len , output , outOff );
169- }
170- }
171-
172- private class BufferedLargeMacAADProcessor
173- extends BufferedBaseAADProcessor
174- {
175- @ Override
176- public int processDecryptBytes (byte [] input , int inOff , int len , byte [] output , int outOff )
177- {
178- return processDecryptionWithLargeMacSize (input , inOff , len , output , outOff );
179- }
180- }
181-
182- private abstract class ImmediateBaseAADProcessor
152+ private class ImmediateAADProcessor
183153 implements AADProcessingBuffer
184154 {
185155 public void processAADByte (byte input )
@@ -211,26 +181,6 @@ public boolean isLengthExceedingBlockSize(int len, int size)
211181 }
212182 }
213183
214- private class ImmediateAADProcessor
215- extends ImmediateBaseAADProcessor
216- {
217- @ Override
218- public int processDecryptBytes (byte [] input , int inOff , int len , byte [] output , int outOff )
219- {
220- return processDecryptionWithSmallMacSize (input , inOff , len , output , outOff );
221- }
222- }
223-
224- private class ImmediateLargeMacAADProcessor
225- extends ImmediateBaseAADProcessor
226- {
227- @ Override
228- public int processDecryptBytes (byte [] input , int inOff , int len , byte [] output , int outOff )
229- {
230- return processDecryptionWithLargeMacSize (input , inOff , len , output , outOff );
231- }
232- }
233-
234184 protected interface AADOperator
235185 {
236186 void processAADByte (byte input );
@@ -359,7 +309,6 @@ public int getLen()
359309 @ Override
360310 public void reset ()
361311 {
362-
363312 }
364313 }
365314
@@ -516,26 +465,26 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
516465
517466 protected int processEncDecBytes (byte [] input , int inOff , int len , byte [] output , int outOff )
518467 {
468+ boolean forEncryption = checkData (false );
519469 int available , resultLength ;
520- if (checkData (false ))
470+ available = (forEncryption ? BlockSize : m_bufferSizeDecrypt ) - m_bufPos ;
471+ // The function is just an operator < or <=
472+ if (processor .isLengthWithinAvailableSpace (len , available ))
473+ {
474+ System .arraycopy (input , inOff , m_buf , m_bufPos , len );
475+ m_bufPos += len ;
476+ return 0 ;
477+ }
478+ resultLength = processor .getUpdateOutputSize (len ) + m_bufPos - (forEncryption ? 0 : MAC_SIZE );
479+ ensureSufficientOutputBuffer (output , outOff , resultLength - resultLength % BlockSize );
480+ resultLength = 0 ;
481+ if (forEncryption )
521482 {
522- resultLength = 0 ;
523- available = processor .getUpdateOutputSize (len ) + m_bufPos ;
524- ensureSufficientOutputBuffer (output , outOff , available - available % BlockSize );
525483 if (m_bufPos > 0 )
526484 {
527- available = BlockSize - m_bufPos ;
528- // The function is just an operator < or <=
529- if (processor .isLengthWithinAvailableSpace (len , available ))
530- {
531- System .arraycopy (input , inOff , m_buf , m_bufPos , len );
532- m_bufPos += len ;
533- return 0 ;
534- }
535485 System .arraycopy (input , inOff , m_buf , m_bufPos , available );
536486 inOff += available ;
537487 len -= available ;
538-
539488 processBufferEncrypt (m_buf , 0 , output , outOff );
540489 resultLength = BlockSize ;
541490 }
@@ -550,25 +499,30 @@ protected int processEncDecBytes(byte[] input, int inOff, int len, byte[] output
550499 }
551500 else
552501 {
553- available = m_bufferSizeDecrypt - m_bufPos ;
554- if (processor .isLengthWithinAvailableSpace (len , available ))
502+ // loop will run more than once for the following situation: pb128, ascon80pq, ascon128, ISAP_A_128(A)
503+ while (processor .isLengthExceedingBlockSize (m_bufPos , BlockSize )
504+ && processor .isLengthExceedingBlockSize (len + m_bufPos , m_bufferSizeDecrypt ))
555505 {
556- System . arraycopy ( input , inOff , m_buf , m_bufPos , len );
557- m_bufPos += len ;
558- return 0 ;
506+ processBufferDecrypt ( m_buf , resultLength , output , outOff + resultLength );
507+ m_bufPos -= BlockSize ;
508+ resultLength += BlockSize ;
559509 }
560- resultLength = (processor .getUpdateOutputSize (len ) + m_bufPos - MAC_SIZE );
561- resultLength -= resultLength % BlockSize ;
562- ensureSufficientOutputBuffer (output , outOff , resultLength );
563- int originalInOff = inOff ;
564- int originalm_bufPos = m_bufPos ;
565- if ((inOff = processor .processDecryptBytes (input , inOff , len , output , outOff )) == -1 )
510+ if (m_bufPos > 0 )
566511 {
567- return resultLength ;
512+ System .arraycopy (m_buf , resultLength , m_buf , 0 , m_bufPos );
513+ if (processor .isLengthWithinAvailableSpace (m_bufPos + len , m_bufferSizeDecrypt ))
514+ {
515+ System .arraycopy (input , inOff , m_buf , m_bufPos , len );
516+ m_bufPos += len ;
517+ return resultLength ;
518+ }
519+ available = Math .max (BlockSize - m_bufPos , 0 );
520+ System .arraycopy (input , inOff , m_buf , m_bufPos , available );
521+ inOff += available ;
522+ len -= available ;
523+ processBufferDecrypt (m_buf , 0 , output , outOff + resultLength );
524+ resultLength += BlockSize ;
568525 }
569- resultLength = inOff - originalInOff ;
570- len -= resultLength ;
571- resultLength += originalm_bufPos ;
572526 while (processor .isLengthExceedingBlockSize (len , m_bufferSizeDecrypt ))
573527 {
574528 processBufferDecrypt (input , inOff , output , outOff + resultLength );
@@ -582,65 +536,6 @@ protected int processEncDecBytes(byte[] input, int inOff, int len, byte[] output
582536 return resultLength ;
583537 }
584538
585- private int processDecryptionWithLargeMacSize (byte [] input , int inOff , int len , byte [] output , int outOff )
586- {
587- int resultLength = 0 , available ;
588- // If the mac size is greater than the block size, process the data in m_buf in the loop until
589- // there is nearly mac_size data left
590- while (processor .isLengthExceedingBlockSize (m_bufPos , BlockSize )
591- && processor .isLengthExceedingBlockSize (len + m_bufPos , m_bufferSizeDecrypt ))
592- {
593- processBufferDecrypt (m_buf , resultLength , output , outOff + resultLength );
594- m_bufPos -= BlockSize ;
595- resultLength += BlockSize ;
596- }
597- if (m_bufPos > 0 )
598- {
599- System .arraycopy (m_buf , resultLength , m_buf , 0 , m_bufPos );
600- if (processor .isLengthExceedingBlockSize (m_bufPos + len , m_bufferSizeDecrypt ))
601- {
602- available = Math .max (BlockSize - m_bufPos , 0 );
603- System .arraycopy (input , inOff , m_buf , m_bufPos , available );
604- inOff += available ;
605- processBufferDecrypt (m_buf , 0 , output , outOff + resultLength );
606- }
607- else
608- {
609- System .arraycopy (input , inOff , m_buf , m_bufPos , len );
610- m_bufPos += len ;
611- return -1 ;
612- }
613- }
614- return inOff ;
615- }
616-
617- private int processDecryptionWithSmallMacSize (byte [] input , int inOff , int len , byte [] output , int outOff )
618- {
619- int resultLength = 0 , available = m_bufferSizeDecrypt - m_bufPos ;
620- if (m_bufPos > 0 )
621- {
622- if (processor .isLengthExceedingBlockSize (m_bufPos , BlockSize ))
623- {
624- processBufferDecrypt (m_buf , 0 , output , outOff );
625- m_bufPos -= BlockSize ;
626- System .arraycopy (m_buf , BlockSize , m_buf , 0 , m_bufPos );
627- resultLength = BlockSize ;
628- available += BlockSize ;
629- if (processor .isLengthWithinAvailableSpace (len , available ))
630- {
631- System .arraycopy (input , inOff , m_buf , m_bufPos , len );
632- m_bufPos += len ;
633- return -1 ;
634- }
635- }
636- available = Math .max (BlockSize - m_bufPos , 0 );
637- System .arraycopy (input , inOff , m_buf , m_bufPos , available );
638- inOff += available ;
639- processBufferDecrypt (m_buf , 0 , output , outOff + resultLength );
640- }
641- return inOff ;
642- }
643-
644539 @ Override
645540 public int doFinal (byte [] output , int outOff )
646541 throws IllegalStateException , InvalidCipherTextException
@@ -663,10 +558,7 @@ public int doFinal(byte[] output, int outOff)
663558 resultLength = m_bufPos ;
664559 }
665560
666- if (outOff > output .length - resultLength )
667- {
668- throw new OutputLengthException ("output buffer too short" );
669- }
561+ ensureSufficientOutputBuffer (output , outOff , resultLength );
670562 mac = new byte [MAC_SIZE ];
671563 processFinalBlock (output , outOff );
672564 if (forEncryption )
@@ -684,7 +576,7 @@ public int doFinal(byte[] output, int outOff)
684576 return resultLength ;
685577 }
686578
687- public int getBlockSize ()
579+ public final int getBlockSize ()
688580 {
689581 return BlockSize ;
690582 }
@@ -774,7 +666,7 @@ protected boolean checkData(boolean isDoFinal)
774666
775667 protected abstract void finishAAD (State nextState , boolean isDoFinal );
776668
777- protected void bufferReset ()
669+ protected final void bufferReset ()
778670 {
779671 if (m_buf != null )
780672 {
@@ -808,23 +700,23 @@ protected void bufferReset()
808700 dataOperator .reset ();
809701 }
810702
811- protected void ensureSufficientOutputBuffer (byte [] output , int outOff , int len )
703+ protected final void ensureSufficientOutputBuffer (byte [] output , int outOff , int len )
812704 {
813- if (len >= BlockSize && outOff + len > output .length )
705+ if (outOff + len > output .length )
814706 {
815707 throw new OutputLengthException ("output buffer too short" );
816708 }
817709 }
818710
819- protected void ensureSufficientInputBuffer (byte [] input , int inOff , int len )
711+ protected final void ensureSufficientInputBuffer (byte [] input , int inOff , int len )
820712 {
821713 if (inOff + len > input .length )
822714 {
823715 throw new DataLengthException ("input buffer too short" );
824716 }
825717 }
826718
827- protected void ensureInitialized ()
719+ protected final void ensureInitialized ()
828720 {
829721 if (m_state == State .Uninitialized )
830722 {
0 commit comments