Skip to content

Commit 91df749

Browse files
committed
Merge branch 'aead-base-update' into 'main'
Aead base update See merge request root/bc-java!79
2 parents c6ed370 + dd3826a commit 91df749

File tree

7 files changed

+52
-166
lines changed

7 files changed

+52
-166
lines changed

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

Lines changed: 42 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public AsconEngine(AsconParameters asconParameters)
6363
nr = (BlockSize == 8) ? 6 : 8;
6464
AADBufferSize = BlockSize;
6565
dsep = 1L;
66-
setInnerMembers(asconParameters == AsconParameters.ascon128a ? ProcessingBufferType.Immediate : ProcessingBufferType.ImmediateLargeMac, AADOperatorType.Default, DataOperatorType.Default);
66+
setInnerMembers(ProcessingBufferType.Immediate, AADOperatorType.Default, DataOperatorType.Default);
6767
}
6868

6969
protected long pad(int i)

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,29 @@ public enum IsapType
2626
public ISAPEngine(IsapType isapType)
2727
{
2828
KEY_SIZE = IV_SIZE = MAC_SIZE = 16;
29-
ProcessingBufferType bufferType;
3029
switch (isapType)
3130
{
3231
case ISAP_A_128A:
3332
ISAPAEAD = new ISAPAEAD_A_128A();
3433
algorithmName = "ISAP-A-128A AEAD";
35-
bufferType = ProcessingBufferType.ImmediateLargeMac;
3634
break;
3735
case ISAP_K_128A:
3836
ISAPAEAD = new ISAPAEAD_K_128A();
3937
algorithmName = "ISAP-K-128A AEAD";
40-
bufferType = ProcessingBufferType.Immediate;
4138
break;
4239
case ISAP_A_128:
4340
ISAPAEAD = new ISAPAEAD_A_128();
4441
algorithmName = "ISAP-A-128 AEAD";
45-
bufferType = ProcessingBufferType.ImmediateLargeMac;
4642
break;
4743
case ISAP_K_128:
4844
ISAPAEAD = new ISAPAEAD_K_128();
4945
algorithmName = "ISAP-K-128 AEAD";
50-
bufferType = ProcessingBufferType.Immediate;
5146
break;
5247
default:
5348
throw new IllegalArgumentException("Incorrect ISAP parameter");
5449
}
5550
AADBufferSize = BlockSize;
56-
setInnerMembers(bufferType, AADOperatorType.Default, DataOperatorType.Counter);
51+
setInnerMembers(ProcessingBufferType.Immediate, AADOperatorType.Default, DataOperatorType.Counter);
5752
}
5853

5954
private static final int ISAP_STATE_SZ = 40;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public PhotonBeetleEngine(PhotonBeetleParameters pbp)
7272
STATE_INBYTES = (STATE_INBITS + 7) >>> 3;
7373
LAST_THREE_BITS_OFFSET = (STATE_INBITS - ((STATE_INBYTES - 1) << 3) - 3);
7474
algorithmName = "Photon-Beetle AEAD";
75-
setInnerMembers(pbp == PhotonBeetleParameters.pb128 ? ProcessingBufferType.Buffered : ProcessingBufferType.BufferedLargeMac,
76-
AADOperatorType.Counter, DataOperatorType.Counter);
75+
setInnerMembers(ProcessingBufferType.Buffered, AADOperatorType.Counter, DataOperatorType.Counter);
7776
}
7877

7978
@Override
@@ -117,7 +116,7 @@ protected void finishAAD(State nextState, boolean isDoFinal)
117116
m_state = nextState;
118117
}
119118

120-
public void processFinalAAD()
119+
protected void processFinalAAD()
121120
{
122121
int aadLen = aadOperator.getLen();
123122
if (aadLen != 0)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ static void hirose_128_128_256(byte[] h, byte[] g, byte[] m, int mOff)
855855
}
856856

857857
@Override
858-
public void init(byte[] key, byte[] iv)
858+
protected void init(byte[] key, byte[] iv)
859859
throws IllegalArgumentException
860860
{
861861
npub = iv;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public XoodyakEngine()
4343
}
4444

4545
@Override
46-
public void init(byte[] key, byte[] iv)
46+
protected void init(byte[] key, byte[] iv)
4747
throws IllegalArgumentException
4848
{
4949
K = key;

0 commit comments

Comments
 (0)