Skip to content

Commit e7fef7d

Browse files
author
gefeili
committed
Remove aadFinished
1 parent 6869b09 commit e7fef7d

File tree

4 files changed

+85
-40
lines changed

4 files changed

+85
-40
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ private void pho(byte[] Y, byte[] M, int mOff, byte[] X, byte[] C, int cOff, int
182182
private void phoprime(byte[] Y, byte[] C, int cOff, byte[] X, byte[] M, int mOff, int no_of_bytes)
183183
{
184184
Bytes.xor(no_of_bytes, Y, C, cOff, M, mOff);
185-
//xor_block(M, mOff, Y, C, cOff, no_of_bytes);
186185
pho1(X, Y, M, mOff, no_of_bytes);
187186
}
188187

@@ -232,7 +231,6 @@ protected void finishAAD(State nextState, boolean isDoFinal)
232231
case DecAad:
233232
if (!isDoFinal && dataOperator.getLen() <= MAC_SIZE)
234233
{
235-
//m_state = State.DecData;
236234
return;
237235
}
238236
case EncInit:

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,41 @@ public enum IsapType
2727
public ISAPEngine(IsapType isapType)
2828
{
2929
KEY_SIZE = IV_SIZE = MAC_SIZE = 16;
30+
ProcessingBufferType bufferType;
3031
switch (isapType)
3132
{
3233
case ISAP_A_128A:
3334
ISAPAEAD = new ISAPAEAD_A_128A();
3435
algorithmName = "ISAP-A-128A AEAD";
36+
bufferType = ProcessingBufferType.ImmediateLargeMac;
3537
break;
3638
case ISAP_K_128A:
3739
ISAPAEAD = new ISAPAEAD_K_128A();
3840
algorithmName = "ISAP-K-128A AEAD";
41+
bufferType = ProcessingBufferType.Immediate;
3942
break;
4043
case ISAP_A_128:
4144
ISAPAEAD = new ISAPAEAD_A_128();
4245
algorithmName = "ISAP-A-128 AEAD";
46+
bufferType = ProcessingBufferType.ImmediateLargeMac;
4347
break;
4448
case ISAP_K_128:
4549
ISAPAEAD = new ISAPAEAD_K_128();
4650
algorithmName = "ISAP-K-128 AEAD";
51+
bufferType = ProcessingBufferType.Immediate;
4752
break;
53+
default:
54+
throw new IllegalArgumentException("Incorrect ISAP parameter");
4855
}
4956
AADBufferSize = BlockSize;
50-
setInnerMembers(isapType == IsapType.ISAP_K_128A || isapType == IsapType.ISAP_K_128 ? ProcessingBufferType.Immediate :
51-
ProcessingBufferType.ImmediateLargeMac, AADOperatorType.Default, DataOperatorType.Default);
57+
setInnerMembers(bufferType, AADOperatorType.Default, DataOperatorType.Counter);
5258
}
5359

5460
private static final int ISAP_STATE_SZ = 40;
5561
private byte[] k;
5662
private byte[] npub;
5763
private int ISAP_rH;
58-
private boolean aadFinished;
59-
private ISAP_AEAD ISAPAEAD;
64+
private final ISAP_AEAD ISAPAEAD;
6065

6166
private interface ISAP_AEAD
6267
{
@@ -706,32 +711,46 @@ protected void processBufferAAD(byte[] input, int inOff)
706711

707712
protected void processFinalAAD()
708713
{
709-
if (!aadFinished)
714+
ISAPAEAD.absorbFinalAADBlock();
715+
}
716+
717+
@Override
718+
protected void finishAAD(State nextState, boolean isDoFinal)
719+
{
720+
// State indicates whether we ever received AAD
721+
switch (m_state)
710722
{
711-
ISAPAEAD.absorbFinalAADBlock();
712-
m_aadPos = 0;
713-
aadFinished = true;
723+
case DecInit:
724+
case DecAad:
725+
if (!isDoFinal && dataOperator.getLen() <= MAC_SIZE)
726+
{
727+
return;
728+
}
729+
case EncInit:
730+
case EncAad:
731+
processFinalAAD();
732+
break;
714733
}
734+
735+
m_aadPos = 0;
736+
m_state = nextState;
715737
}
716738

717739
protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int outOff)
718740
{
719-
processFinalAAD();
720741
ISAPAEAD.processEncBlock(input, inOff, output, outOff);
721742
ISAPAEAD.absorbMacBlock(output, outOff);
722743
}
723744

724745
protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int outOff)
725746
{
726-
processFinalAAD();
727747
ISAPAEAD.processEncBlock(input, inOff, output, outOff);
728748
ISAPAEAD.absorbMacBlock(input, inOff);
729749
}
730750

731751
@Override
732752
protected void processFinalBlock(byte[] output, int outOff)
733753
{
734-
processFinalAAD();
735754
ISAPAEAD.processEncFinalBlock(output, outOff);
736755
if (forEncryption)
737756
{
@@ -748,7 +767,6 @@ protected void reset(boolean clearMac)
748767
ensureInitialized();
749768
bufferReset();
750769
ISAPAEAD.reset();
751-
aadFinished = false;
752770
super.reset(clearMac);
753771
}
754772
}

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

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public enum PhotonBeetleParameters
2929
private final int STATE_INBYTES;
3030
private final int LAST_THREE_BITS_OFFSET;
3131
private static final int D = 8;
32-
private boolean aadFinished;
3332
private static final byte[][] RC = {
3433
{1, 3, 7, 14, 13, 11, 6, 12, 9, 2, 5, 10},
3534
{0, 2, 6, 15, 12, 10, 7, 13, 8, 3, 4, 11},
@@ -99,27 +98,45 @@ protected void processBufferAAD(byte[] input, int inOff)
9998
Bytes.xorTo(BlockSize, input, inOff, state);
10099
}
101100

101+
@Override
102+
protected void finishAAD(State nextState, boolean isDoFinal)
103+
{
104+
// State indicates whether we ever received AAD
105+
switch (m_state)
106+
{
107+
case DecInit:
108+
case DecAad:
109+
if (!isDoFinal && dataOperator.getLen() <= MAC_SIZE)
110+
{
111+
//m_state = State.DecData;
112+
return;
113+
}
114+
case EncInit:
115+
case EncAad:
116+
processFinalAAD();
117+
break;
118+
}
119+
120+
m_aadPos = 0;
121+
m_state = nextState;
122+
}
123+
102124
public void processFinalAAD()
103125
{
104-
if (!aadFinished)
126+
int aadLen = aadOperator.getLen();
127+
if (aadLen != 0)
105128
{
106-
int aadLen = aadOperator.getLen();
107-
if (aadLen != 0)
129+
if (m_aadPos != 0)
108130
{
109-
if (m_aadPos != 0)
131+
PhotonPermutation(state_2d, state);
132+
Bytes.xorTo(m_aadPos, m_aad, state);
133+
if (m_aadPos < BlockSize)
110134
{
111-
PhotonPermutation(state_2d, state);
112-
Bytes.xorTo(m_aadPos, m_aad, state);
113-
if (m_aadPos < BlockSize)
114-
{
115-
state[m_aadPos] ^= 0x01; // ozs
116-
}
135+
state[m_aadPos] ^= 0x01; // ozs
117136
}
118-
state[STATE_INBYTES - 1] ^= select(dataOperator.getLen() - (forEncryption ? 0 : MAC_SIZE) > 0,
119-
((aadLen % BlockSize) == 0), (byte)3, (byte)4) << LAST_THREE_BITS_OFFSET;
120137
}
121-
m_aadPos = 0;
122-
aadFinished = true;
138+
state[STATE_INBYTES - 1] ^= select(dataOperator.getLen() - (forEncryption ? 0 : MAC_SIZE) > 0,
139+
((aadLen % BlockSize) == 0), (byte)3, (byte)4) << LAST_THREE_BITS_OFFSET;
123140
}
124141
}
125142

@@ -183,7 +200,6 @@ protected void reset(boolean clearMac)
183200
ensureInitialized();
184201
bufferReset();
185202
input_empty = true;
186-
aadFinished = false;
187203
System.arraycopy(K, 0, state, 0, K.length);
188204
System.arraycopy(N, 0, state, K.length, N.length);
189205
super.reset(clearMac);

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class XoodyakEngine
2727
0x0000002C, 0x00000380, 0x000000F0, 0x000001A0, 0x00000012};
2828
private boolean encrypted;
2929
private byte aadcd;
30-
private boolean aadFinished;
3130
private static final int ModeKeyed = 0;
3231
private static final int ModeHash = 1;
3332

@@ -39,7 +38,7 @@ public XoodyakEngine()
3938
MAC_SIZE = 16;
4039
BlockSize = 24;
4140
AADBufferSize = 44;
42-
setInnerMembers(ProcessingBufferType.Buffered, AADOperatorType.Default, DataOperatorType.Default);
41+
setInnerMembers(ProcessingBufferType.Buffered, AADOperatorType.Default, DataOperatorType.Counter);
4342
}
4443

4544
@Override
@@ -61,23 +60,39 @@ protected void processBufferAAD(byte[] input, int inOff)
6160

6261
protected void processFinalAAD()
6362
{
64-
if (!aadFinished)
63+
AbsorbAny(m_aad, 0, m_aadPos, aadcd);
64+
m_aadPos = 0;
65+
}
66+
67+
@Override
68+
protected void finishAAD(State nextState, boolean isDoFinal)
69+
{
70+
// State indicates whether we ever received AAD
71+
switch (m_state)
6572
{
66-
AbsorbAny(m_aad, 0, m_aadPos, aadcd);
67-
aadFinished = true;
68-
m_aadPos = 0;
73+
case DecInit:
74+
case DecAad:
75+
if (!isDoFinal && dataOperator.getLen() <= MAC_SIZE)
76+
{
77+
return;
78+
}
79+
case EncInit:
80+
case EncAad:
81+
processFinalAAD();
82+
break;
6983
}
84+
85+
m_aadPos = 0;
86+
m_state = nextState;
7087
}
7188

7289
protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int outOff)
7390
{
74-
processFinalAAD();
7591
encrypt(input, inOff, BlockSize, output, outOff);
7692
}
7793

7894
protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int outOff)
7995
{
80-
processFinalAAD();
8196
decrypt(input, inOff, BlockSize, output, outOff);
8297
}
8398

@@ -124,7 +139,6 @@ private void decrypt(byte[] input, int inOff, int len, byte[] output, int outOff
124139
@Override
125140
protected void processFinalBlock(byte[] output, int outOff)
126141
{
127-
processFinalAAD();
128142
if (forEncryption)
129143
{
130144
Arrays.fill(m_buf, m_bufPos, BlockSize, (byte)0);
@@ -143,7 +157,6 @@ protected void reset(boolean clearMac)
143157
ensureInitialized();
144158
super.reset(clearMac);
145159
Arrays.fill(state, (byte)0);
146-
aadFinished = false;
147160
encrypted = false;
148161
phase = PhaseUp;
149162
aadcd = (byte)0x03;

0 commit comments

Comments
 (0)