@@ -465,26 +465,26 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
465465
466466 protected int processEncDecBytes (byte [] input , int inOff , int len , byte [] output , int outOff )
467467 {
468+ boolean forEncryption = checkData (false );
468469 int available , resultLength ;
469- 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 )
470482 {
471- resultLength = 0 ;
472- available = processor .getUpdateOutputSize (len ) + m_bufPos ;
473- ensureSufficientOutputBuffer (output , outOff , available - available % BlockSize );
474483 if (m_bufPos > 0 )
475484 {
476- available = BlockSize - m_bufPos ;
477- // The function is just an operator < or <=
478- if (processor .isLengthWithinAvailableSpace (len , available ))
479- {
480- System .arraycopy (input , inOff , m_buf , m_bufPos , len );
481- m_bufPos += len ;
482- return 0 ;
483- }
484485 System .arraycopy (input , inOff , m_buf , m_bufPos , available );
485486 inOff += available ;
486487 len -= available ;
487-
488488 processBufferEncrypt (m_buf , 0 , output , outOff );
489489 resultLength = BlockSize ;
490490 }
@@ -499,25 +499,30 @@ protected int processEncDecBytes(byte[] input, int inOff, int len, byte[] output
499499 }
500500 else
501501 {
502- available = m_bufferSizeDecrypt - m_bufPos ;
503- 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 ))
504505 {
505- System . arraycopy ( input , inOff , m_buf , m_bufPos , len );
506- m_bufPos += len ;
507- return 0 ;
506+ processBufferDecrypt ( m_buf , resultLength , output , outOff + resultLength );
507+ m_bufPos -= BlockSize ;
508+ resultLength += BlockSize ;
508509 }
509- resultLength = (processor .getUpdateOutputSize (len ) + m_bufPos - MAC_SIZE );
510- resultLength -= resultLength % BlockSize ;
511- ensureSufficientOutputBuffer (output , outOff , resultLength );
512- int originalInOff = inOff ;
513- int originalm_bufPos = m_bufPos ;
514- if ((inOff = processDecryption (input , inOff , len , output , outOff )) == -1 )
510+ if (m_bufPos > 0 )
515511 {
516- 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 ;
517525 }
518- resultLength = inOff - originalInOff ;
519- len -= resultLength ;
520- resultLength += originalm_bufPos ;
521526 while (processor .isLengthExceedingBlockSize (len , m_bufferSizeDecrypt ))
522527 {
523528 processBufferDecrypt (input , inOff , output , outOff + resultLength );
@@ -531,36 +536,6 @@ protected int processEncDecBytes(byte[] input, int inOff, int len, byte[] output
531536 return resultLength ;
532537 }
533538
534- private int processDecryption (byte [] input , int inOff , int len , byte [] output , int outOff )
535- {
536- int resultLength = 0 , available ;
537-
538- // loop will run more than once for the following situation: pb128, ascon80pq, ascon128, ISAP_A_128(A)
539- while (processor .isLengthExceedingBlockSize (m_bufPos , BlockSize )
540- && processor .isLengthExceedingBlockSize (len + m_bufPos , m_bufferSizeDecrypt ))
541- {
542- processBufferDecrypt (m_buf , resultLength , output , outOff + resultLength );
543- m_bufPos -= BlockSize ;
544- resultLength += BlockSize ;
545- }
546-
547- if (m_bufPos > 0 )
548- {
549- System .arraycopy (m_buf , resultLength , m_buf , 0 , m_bufPos );
550- if (processor .isLengthWithinAvailableSpace (m_bufPos + len , m_bufferSizeDecrypt ))
551- {
552- System .arraycopy (input , inOff , m_buf , m_bufPos , len );
553- m_bufPos += len ;
554- return -1 ;
555- }
556- available = Math .max (BlockSize - m_bufPos , 0 );
557- System .arraycopy (input , inOff , m_buf , m_bufPos , available );
558- inOff += available ;
559- processBufferDecrypt (m_buf , 0 , output , outOff + resultLength );
560- }
561- return inOff ;
562- }
563-
564539 @ Override
565540 public int doFinal (byte [] output , int outOff )
566541 throws IllegalStateException , InvalidCipherTextException
@@ -583,10 +558,7 @@ public int doFinal(byte[] output, int outOff)
583558 resultLength = m_bufPos ;
584559 }
585560
586- if (outOff > output .length - resultLength )
587- {
588- throw new OutputLengthException ("output buffer too short" );
589- }
561+ ensureSufficientOutputBuffer (output , outOff , resultLength );
590562 mac = new byte [MAC_SIZE ];
591563 processFinalBlock (output , outOff );
592564 if (forEncryption )
@@ -730,7 +702,7 @@ protected final void bufferReset()
730702
731703 protected final void ensureSufficientOutputBuffer (byte [] output , int outOff , int len )
732704 {
733- if (len >= BlockSize && outOff + len > output .length )
705+ if (outOff + len > output .length )
734706 {
735707 throw new OutputLengthException ("output buffer too short" );
736708 }
0 commit comments