Skip to content

Commit 24cf9b7

Browse files
author
gefeili
committed
finishAAD. Set BufferBaseDigest not public
1 parent c144727 commit 24cf9b7

File tree

10 files changed

+102
-166
lines changed

10 files changed

+102
-166
lines changed

core/src/main/java/org/bouncycastle/crypto/digests/BufferBaseDigest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.bouncycastle.crypto.OutputLengthException;
66
import org.bouncycastle.util.Arrays;
77

8-
public abstract class BufferBaseDigest
8+
abstract class BufferBaseDigest
99
implements ExtendedDigest
1010
{
1111
protected enum ProcessingBufferType

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

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@ public void reset()
9696
reset(true);
9797
}
9898

99-
public int processByte(byte in, byte[] out, int outOff)
100-
throws DataLengthException
101-
{
102-
return processBytes(new byte[]{in}, 0, 1, out, outOff);
103-
}
104-
10599
public void init(boolean forEncryption, CipherParameters params)
106100
{
107101
this.forEncryption = forEncryption;
@@ -225,6 +219,7 @@ protected void setInnerMembers(ProcessingBufferType type, AADOperatorType aadOpe
225219
aadOperator = new CounterAADOperator();
226220
break;
227221
case Stream:
222+
AADBufferSize = 0;
228223
aadOperator = new StreamAADOperator();
229224
break;
230225
}
@@ -619,6 +614,12 @@ private void processAadBytes(byte[] input, int inOff, int len)
619614
m_aadPos = len;
620615
}
621616

617+
public int processByte(byte in, byte[] out, int outOff)
618+
throws DataLengthException
619+
{
620+
return processBytes(new byte[]{in}, 0, 1, out, outOff);
621+
}
622+
622623
@Override
623624
public int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff)
624625
throws DataLengthException
@@ -858,10 +859,68 @@ protected final void ensureInitialized()
858859
}
859860
}
860861

861-
protected abstract void init(byte[] key, byte[] iv);
862+
protected void finishAAD1(State nextState)
863+
{
864+
switch (m_state)
865+
{
866+
case DecInit:
867+
case DecAad:
868+
case EncInit:
869+
case EncAad:
870+
{
871+
processFinalAAD();
872+
break;
873+
}
874+
default:
875+
break;
876+
}
877+
m_state = nextState;
878+
}
879+
880+
protected void finishAAD2(State nextState)
881+
{
882+
// State indicates whether we ever received AAD
883+
switch (m_state)
884+
{
885+
case DecAad:
886+
case EncAad:
887+
{
888+
processFinalAAD();
889+
break;
890+
}
891+
default:
892+
break;
893+
}
894+
895+
m_aadPos = 0;
896+
m_state = nextState;
897+
}
898+
899+
protected void finishAAD3(State nextState, boolean isDoFinal)
900+
{
901+
// State indicates whether we ever received AAD
902+
switch (m_state)
903+
{
904+
case DecInit:
905+
case DecAad:
906+
if (!isDoFinal && dataOperator.getLen() <= MAC_SIZE)
907+
{
908+
return;
909+
}
910+
case EncInit:
911+
case EncAad:
912+
processFinalAAD();
913+
break;
914+
}
915+
916+
m_aadPos = 0;
917+
m_state = nextState;
918+
}
862919

863920
protected abstract void finishAAD(State nextState, boolean isDoFinal);
864921

922+
protected abstract void init(byte[] key, byte[] iv);
923+
865924
protected abstract void processFinalBlock(byte[] output, int outOff);
866925

867926
protected abstract void processBufferAAD(byte[] input, int inOff);

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,7 @@ public int getOutputSize(int len)
439439

440440
protected void finishAAD(State nextState, boolean isDoFinal)
441441
{
442-
// State indicates whether we ever received AAD
443-
switch (m_state)
444-
{
445-
case DecAad:
446-
case EncAad:
447-
{
448-
processFinalAAD();
449-
break;
450-
}
451-
default:
452-
break;
453-
}
454-
455-
m_aadPos = 0;
456-
m_state = nextState;
442+
finishAAD2(nextState);
457443
}
458444

459445
@Override

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,7 @@ protected void processFinalAAD()
207207
@Override
208208
protected void finishAAD(State nextState, boolean isDoFinal)
209209
{
210-
// State indicates whether we ever received AAD
211-
switch (m_state)
212-
{
213-
case DecInit:
214-
case DecAad:
215-
if (!isDoFinal && dataOperator.getLen() <= MAC_SIZE)
216-
{
217-
return;
218-
}
219-
case EncInit:
220-
case EncAad:
221-
processFinalAAD();
222-
break;
223-
}
224-
225-
m_aadPos = 0;
226-
m_state = nextState;
210+
finishAAD3(nextState, isDoFinal);
227211
}
228212

229213
@Override

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

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bouncycastle.crypto.engines;
22

3+
import org.bouncycastle.crypto.DataLengthException;
4+
import org.bouncycastle.util.Arrays;
35
import org.bouncycastle.util.Pack;
46

57
/**
@@ -19,24 +21,26 @@ public class Grain128AEADEngine
1921
*/
2022
private byte[] workingKey;
2123
private byte[] workingIV;
22-
private int[] lfsr;
23-
private int[] nfsr;
24-
private int[] authAcc;
25-
private int[] authSr;
24+
private final int[] lfsr;
25+
private final int[] nfsr;
26+
private final int[] authAcc;
27+
private final int[] authSr;
2628

2729
public Grain128AEADEngine()
2830
{
2931
algorithmName = "Grain-128 AEAD";
3032
KEY_SIZE = 16;
3133
IV_SIZE = 12;
3234
MAC_SIZE = 8;
35+
lfsr = new int[STATE_SIZE];
36+
nfsr = new int[STATE_SIZE];
37+
authAcc = new int[2];
38+
authSr = new int[2];
3339
setInnerMembers(ProcessingBufferType.Immediate, AADOperatorType.Stream, DataOperatorType.StreamCipher);
3440
}
3541

3642
/**
3743
* Initialize a Grain-128AEAD cipher.
38-
*
39-
* @throws IllegalArgumentException If the params argument is inappropriate.
4044
*/
4145
protected void init(byte[] key, byte[] iv)
4246
throws IllegalArgumentException
@@ -46,11 +50,6 @@ protected void init(byte[] key, byte[] iv)
4650
*/
4751
workingIV = new byte[16];
4852
workingKey = key;
49-
lfsr = new int[STATE_SIZE];
50-
nfsr = new int[STATE_SIZE];
51-
authAcc = new int[2];
52-
authSr = new int[2];
53-
5453
System.arraycopy(iv, 0, workingIV, 0, IV_SIZE);
5554
workingIV[12] = (byte)0xFF;
5655
workingIV[13] = (byte)0xFF;
@@ -158,60 +157,61 @@ private int getOutput()
158157
}
159158

160159
/**
161-
* Shift array 1 bit and add val to index.length - 1.
160+
* Shift array 1 bit and add val to index - 1.
162161
*
163162
* @param array The array to shift.
164163
* @param val The value to shift in.
165-
* @return The shifted array with val added to index.length - 1.
166164
*/
167-
private int[] shift(int[] array, int val)
165+
private void shift(int[] array, int val)
168166
{
169167
array[0] = (array[0] >>> 1) | (array[1] << 31);
170168
array[1] = (array[1] >>> 1) | (array[2] << 31);
171169
array[2] = (array[2] >>> 1) | (array[3] << 31);
172170
array[3] = (array[3] >>> 1) | (val << 31);
173-
return array;
174171
}
175172

176173
private void shift()
177174
{
178-
nfsr = shift(nfsr, (getOutputNFSR() ^ lfsr[0]) & 1);
179-
lfsr = shift(lfsr, (getOutputLFSR()) & 1);
175+
shift(nfsr, (getOutputNFSR() ^ lfsr[0]) & 1);
176+
shift(lfsr, (getOutputLFSR()) & 1);
180177
}
181178

182179
protected void reset(boolean clearMac)
183180
{
184181
super.reset(clearMac);
185182
Pack.littleEndianToInt(workingKey, 0, nfsr);
186183
Pack.littleEndianToInt(workingIV, 0, lfsr);
184+
Arrays.clear(authAcc);
185+
Arrays.clear(authSr);
186+
int output;
187187
// 320 clocks initialization phase.
188188
for (int i = 0; i < 320; ++i)
189189
{
190-
int output = getOutput();
191-
nfsr = shift(nfsr, (getOutputNFSR() ^ lfsr[0] ^ output) & 1);
192-
lfsr = shift(lfsr, (getOutputLFSR() ^ output) & 1);
190+
output = getOutput();
191+
shift(nfsr, (getOutputNFSR() ^ lfsr[0] ^ output) & 1);
192+
shift(lfsr, (getOutputLFSR() ^ output) & 1);
193193
}
194194
for (int quotient = 0; quotient < 8; ++quotient)
195195
{
196196
for (int remainder = 0; remainder < 8; ++remainder)
197197
{
198-
int output = getOutput();
199-
nfsr = shift(nfsr, (getOutputNFSR() ^ lfsr[0] ^ output ^ ((workingKey[quotient]) >> remainder)) & 1);
200-
lfsr = shift(lfsr, (getOutputLFSR() ^ output ^ ((workingKey[quotient + 8]) >> remainder)) & 1);
198+
output = getOutput();
199+
shift(nfsr, (getOutputNFSR() ^ lfsr[0] ^ output ^ ((workingKey[quotient]) >> remainder)) & 1);
200+
shift(lfsr, (getOutputLFSR() ^ output ^ ((workingKey[quotient + 8]) >> remainder)) & 1);
201201
}
202202
}
203203
initGrain(authAcc);
204204
initGrain(authSr);
205205
}
206206

207-
private void updateInternalState(int input_i_j)
207+
private void updateInternalState(int mask)
208208
{
209-
int mask = -input_i_j;
209+
mask = -mask;
210210
authAcc[0] ^= authSr[0] & mask;
211211
authAcc[1] ^= authSr[1] & mask;
212-
int val = getByteKeyStream();
212+
mask = getByteKeyStream();
213213
authSr[0] = (authSr[0] >>> 1) | (authSr[1] << 31);
214-
authSr[1] = (authSr[1] >>> 1) | (val << 31);
214+
authSr[1] = (authSr[1] >>> 1) | (mask << 31);
215215
}
216216

217217
public int getUpdateOutputSize(int len)
@@ -222,23 +222,7 @@ public int getUpdateOutputSize(int len)
222222
@Override
223223
protected void finishAAD(State nextState, boolean isDoFinal)
224224
{
225-
// State indicates whether we ever received AAD
226-
switch (m_state)
227-
{
228-
case DecInit:
229-
case DecAad:
230-
case EncInit:
231-
case EncAad:
232-
{
233-
processFinalAAD();
234-
break;
235-
}
236-
default:
237-
break;
238-
}
239-
240-
m_aadPos = 0;
241-
m_state = nextState;
225+
finishAAD1(nextState);
242226
}
243227

244228
@Override
@@ -252,7 +236,6 @@ protected void processFinalBlock(byte[] output, int outOff)
252236
@Override
253237
protected void processBufferAAD(byte[] input, int inOff)
254238
{
255-
256239
}
257240

258241
@Override

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -697,23 +697,7 @@ protected void processFinalAAD()
697697
@Override
698698
protected void finishAAD(State nextState, boolean isDoFinal)
699699
{
700-
// State indicates whether we ever received AAD
701-
switch (m_state)
702-
{
703-
case DecInit:
704-
case DecAad:
705-
if (!isDoFinal && dataOperator.getLen() <= MAC_SIZE)
706-
{
707-
return;
708-
}
709-
case EncInit:
710-
case EncAad:
711-
processFinalAAD();
712-
break;
713-
}
714-
715-
m_aadPos = 0;
716-
m_state = nextState;
700+
finishAAD3(nextState, isDoFinal);
717701
}
718702

719703
protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int outOff)

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,7 @@ protected void processBufferAAD(byte[] input, int inOff)
9393
@Override
9494
protected void finishAAD(State nextState, boolean isDoFinal)
9595
{
96-
// State indicates whether we ever received AAD
97-
switch (m_state)
98-
{
99-
case DecInit:
100-
case DecAad:
101-
if (!isDoFinal && dataOperator.getLen() <= MAC_SIZE)
102-
{
103-
//m_state = State.DecData;
104-
return;
105-
}
106-
case EncInit:
107-
case EncAad:
108-
processFinalAAD();
109-
break;
110-
}
111-
112-
m_aadPos = 0;
113-
m_state = nextState;
96+
finishAAD3(nextState, isDoFinal);
11497
}
11598

11699
protected void processFinalAAD()

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -865,20 +865,7 @@ protected void init(byte[] key, byte[] iv)
865865
protected void finishAAD(State nextState, boolean isDoFinal)
866866
{
867867
// State indicates whether we ever received AAD
868-
switch (m_state)
869-
{
870-
case DecInit:
871-
case DecAad:
872-
case EncInit:
873-
case EncAad:
874-
{
875-
processFinalAAD();
876-
break;
877-
}
878-
default:
879-
break;
880-
}
881-
m_state = nextState;
868+
finishAAD1(nextState);
882869
}
883870

884871
protected void processBufferAAD(byte[] input, int inOff)

0 commit comments

Comments
 (0)