Skip to content

Commit f0f6072

Browse files
committed
Java 4 compatibility.
1 parent 0d7dd97 commit f0f6072

File tree

5 files changed

+212
-141
lines changed

5 files changed

+212
-141
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88
abstract class BufferBaseDigest
99
implements ExtendedDigest
1010
{
11-
protected enum ProcessingBufferType
11+
protected static class ProcessingBufferType
1212
{
13-
Buffered,
14-
Immediate,
13+
public static final int BUFFERED = 0;
14+
public static final int IMMEDIATE = 1;
15+
16+
public static final ProcessingBufferType Buffered = new ProcessingBufferType(BUFFERED);
17+
public static final ProcessingBufferType Immediate = new ProcessingBufferType(IMMEDIATE);
18+
19+
private final int ord;
20+
21+
ProcessingBufferType(int ord)
22+
{
23+
this.ord = ord;
24+
}
1525
}
1626

1727
protected int DigestSize;
@@ -25,12 +35,12 @@ protected BufferBaseDigest(ProcessingBufferType type, int BlockSize)
2535
{
2636
this.BlockSize = BlockSize;
2737
m_buf = new byte[BlockSize];
28-
switch (type)
38+
switch (type.ord)
2939
{
30-
case Buffered:
40+
case ProcessingBufferType.BUFFERED:
3141
processor = new BufferedProcessor();
3242
break;
33-
case Immediate:
43+
case ProcessingBufferType.IMMEDIATE:
3444
processor = new ImmediateProcessor();
3545
break;
3646
}

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

Lines changed: 134 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,88 @@
1717
abstract class AEADBaseEngine
1818
implements AEADCipher
1919
{
20-
protected enum ProcessingBufferType
20+
protected static class ProcessingBufferType
2121
{
22-
Buffered, // Store a (aad) block size of input and process after the input size exceeds the buffer size
23-
Immediate, //process the input immediately when the input size is equal or greater than the block size
22+
public static final int BUFFERED = 0; // Store a (aad) block size of input and process after the input size exceeds the buffer size
23+
public static final int IMMEDIATE = 1; //process the input immediately when the input size is equal or greater than the block size
24+
25+
public static final ProcessingBufferType Buffered = new ProcessingBufferType(BUFFERED);
26+
public static final ProcessingBufferType Immediate = new ProcessingBufferType(IMMEDIATE);
27+
28+
private final int ord;
29+
30+
ProcessingBufferType(int ord)
31+
{
32+
this.ord = ord;
33+
}
2434
}
2535

26-
protected enum AADOperatorType
36+
protected static class AADOperatorType
2737
{
28-
Default,
29-
Counter,//add a counter to count the size of AAD
30-
Stream //process AAD data during the process data, used for elephant
38+
public static final int DEFAULT = 0;
39+
public static final int COUNTER = 1;//add a counter to count the size of AAD
40+
public static final int STREAM = 2; //process AAD data during the process data, used for elephant
41+
42+
public static final AADOperatorType Default = new AADOperatorType(DEFAULT);
43+
public static final AADOperatorType Counter = new AADOperatorType(COUNTER);
44+
public static final AADOperatorType Stream = new AADOperatorType(STREAM);
45+
46+
private final int ord;
47+
48+
AADOperatorType(int ord)
49+
{
50+
this.ord = ord;
51+
}
3152
}
3253

33-
protected enum DataOperatorType
54+
protected static class DataOperatorType
3455
{
35-
Default,
36-
Counter,
37-
Stream,
38-
StreamCipher
56+
public static final int DEFAULT = 0;
57+
public static final int COUNTER = 1;
58+
public static final int STREAM = 2;
59+
public static final int STREAM_CIPHER = 3;
60+
61+
public static final DataOperatorType Default = new DataOperatorType(DEFAULT);
62+
public static final DataOperatorType Counter = new DataOperatorType(COUNTER);
63+
public static final DataOperatorType Stream = new DataOperatorType(STREAM);
64+
public static final DataOperatorType StreamCipher = new DataOperatorType(STREAM_CIPHER);
65+
66+
private final int ord;
67+
68+
DataOperatorType(int ord)
69+
{
70+
this.ord = ord;
71+
}
3972
}
4073

41-
protected enum State
74+
protected static class State
4275
{
43-
Uninitialized,
44-
EncInit,
45-
EncAad, // can process AAD
46-
EncData, // cannot process AAD
47-
EncFinal,
48-
DecInit,
49-
DecAad, // can process AAD
50-
DecData, // cannot process AAD
51-
DecFinal,
76+
public static final int UNINITIALIZED = 0;
77+
public static final int ENC_INIT = 1;
78+
public static final int ENC_AAD = 2; // can process AAD
79+
public static final int ENC_DATA = 3; // cannot process AAD
80+
public static final int ENC_FINAL = 4;
81+
public static final int DEC_INIT = 5;
82+
public static final int DEC_AAD = 6; // can process AAD
83+
public static final int DEC_DATA = 7; // cannot process AAD
84+
public static final int DEC_FINAL = 8;
85+
86+
public static final State Uninitialized = new State(UNINITIALIZED);
87+
public static final State EncInit = new State(ENC_INIT);
88+
public static final State EncAad = new State(ENC_AAD);
89+
public static final State EncData = new State(ENC_DATA);
90+
public static final State EncFinal = new State(ENC_FINAL);
91+
public static final State DecInit = new State(DEC_INIT);
92+
public static final State DecAad = new State(DEC_AAD);
93+
public static final State DecData = new State(DEC_DATA);
94+
public static final State DecFinal = new State(DEC_FINAL);
95+
96+
final int ord;
97+
98+
State(int ord)
99+
{
100+
this.ord = ord;
101+
}
52102
}
53103

54104
protected boolean forEncryption;
@@ -174,19 +224,19 @@ protected void reset(boolean clearMac)
174224
Arrays.fill(m_aad, (byte)0);
175225
m_aadPos = 0;
176226
}
177-
switch (m_state)
227+
switch (m_state.ord)
178228
{
179-
case DecInit:
180-
case EncInit:
229+
case State.DEC_INIT:
230+
case State.ENC_INIT:
181231
break;
182-
case DecAad:
183-
case DecData:
184-
case DecFinal:
232+
case State.DEC_AAD:
233+
case State.DEC_DATA:
234+
case State.DEC_FINAL:
185235
m_state = State.DecFinal;
186236
break;
187-
case EncAad:
188-
case EncData:
189-
case EncFinal:
237+
case State.ENC_AAD:
238+
case State.ENC_DATA:
239+
case State.ENC_FINAL:
190240
m_state = State.EncFinal;
191241
return;
192242
default:
@@ -198,49 +248,49 @@ protected void reset(boolean clearMac)
198248

199249
protected void setInnerMembers(ProcessingBufferType type, AADOperatorType aadOperatorType, DataOperatorType dataOperatorType)
200250
{
201-
switch (type)
251+
switch (type.ord)
202252
{
203-
case Buffered:
253+
case ProcessingBufferType.BUFFERED:
204254
processor = new BufferedAADProcessor();
205255
break;
206-
case Immediate:
256+
case ProcessingBufferType.IMMEDIATE:
207257
processor = new ImmediateAADProcessor();
208258
break;
209259
}
210260

211261
m_bufferSizeDecrypt = BlockSize + MAC_SIZE;
212262

213-
switch (aadOperatorType)
263+
switch (aadOperatorType.ord)
214264
{
215-
case Default:
265+
case AADOperatorType.DEFAULT:
216266
m_aad = new byte[AADBufferSize];
217267
aadOperator = new DefaultAADOperator();
218268
break;
219-
case Counter:
269+
case AADOperatorType.COUNTER:
220270
m_aad = new byte[AADBufferSize];
221271
aadOperator = new CounterAADOperator();
222272
break;
223-
case Stream:
273+
case AADOperatorType.STREAM:
224274
AADBufferSize = 0;
225275
aadOperator = new StreamAADOperator();
226276
break;
227277
}
228278

229-
switch (dataOperatorType)
279+
switch (dataOperatorType.ord)
230280
{
231-
case Default:
281+
case DataOperatorType.DEFAULT:
232282
m_buf = new byte[m_bufferSizeDecrypt];
233283
dataOperator = new DefaultDataOperator();
234284
break;
235-
case Counter:
285+
case DataOperatorType.COUNTER:
236286
m_buf = new byte[m_bufferSizeDecrypt];
237287
dataOperator = new CounterDataOperator();
238288
break;
239-
case Stream:
289+
case DataOperatorType.STREAM:
240290
m_buf = new byte[MAC_SIZE];
241291
dataOperator = new StreamDataOperator();
242292
break;
243-
case StreamCipher:
293+
case DataOperatorType.STREAM_CIPHER:
244294
BlockSize = 0;
245295
m_buf = new byte[m_bufferSizeDecrypt];
246296
dataOperator = new StreamCipherOperator();
@@ -862,16 +912,16 @@ public int getUpdateOutputSize(int len)
862912
protected int getTotalBytesForUpdate(int len)
863913
{
864914
int total = processor.getUpdateOutputSize(len);
865-
switch (m_state)
915+
switch (m_state.ord)
866916
{
867-
case DecInit:
868-
case DecAad:
869-
case DecData:
870-
case DecFinal:
917+
case State.DEC_INIT:
918+
case State.DEC_AAD:
919+
case State.DEC_DATA:
920+
case State.DEC_FINAL:
871921
total = Math.max(0, total + m_bufPos - MAC_SIZE);
872922
break;
873-
case EncData:
874-
case EncFinal:
923+
case State.ENC_DATA:
924+
case State.ENC_FINAL:
875925
total = Math.max(0, total + m_bufPos);
876926
break;
877927
default:
@@ -884,15 +934,15 @@ public int getOutputSize(int len)
884934
{
885935
int total = Math.max(0, len);
886936

887-
switch (m_state)
937+
switch (m_state.ord)
888938
{
889-
case DecInit:
890-
case DecAad:
891-
case DecData:
892-
case DecFinal:
939+
case State.DEC_INIT:
940+
case State.DEC_AAD:
941+
case State.DEC_DATA:
942+
case State.DEC_FINAL:
893943
return Math.max(0, total + m_bufPos - MAC_SIZE);
894-
case EncData:
895-
case EncFinal:
944+
case State.ENC_DATA:
945+
case State.ENC_FINAL:
896946
return total + m_bufPos + MAC_SIZE;
897947
default:
898948
return total + MAC_SIZE;
@@ -901,18 +951,18 @@ public int getOutputSize(int len)
901951

902952
protected void checkAAD()
903953
{
904-
switch (m_state)
954+
switch (m_state.ord)
905955
{
906-
case DecInit:
956+
case State.DEC_INIT:
907957
m_state = State.DecAad;
908958
break;
909-
case EncInit:
959+
case State.ENC_INIT:
910960
m_state = State.EncAad;
911961
break;
912-
case DecAad:
913-
case EncAad:
962+
case State.DEC_AAD:
963+
case State.ENC_AAD:
914964
break;
915-
case EncFinal:
965+
case State.ENC_FINAL:
916966
throw new IllegalStateException(getAlgorithmName() + " cannot be reused for encryption");
917967
default:
918968
throw new IllegalStateException(getAlgorithmName() + " needs to be initialized");
@@ -921,21 +971,21 @@ protected void checkAAD()
921971

922972
protected boolean checkData(boolean isDoFinal)
923973
{
924-
switch (m_state)
974+
switch (m_state.ord)
925975
{
926-
case DecInit:
927-
case DecAad:
976+
case State.DEC_INIT:
977+
case State.DEC_AAD:
928978
finishAAD(State.DecData, isDoFinal);
929979
return false;
930-
case EncInit:
931-
case EncAad:
980+
case State.ENC_INIT:
981+
case State.ENC_AAD:
932982
finishAAD(State.EncData, isDoFinal);
933983
return true;
934-
case DecData:
984+
case State.DEC_DATA:
935985
return false;
936-
case EncData:
986+
case State.ENC_DATA:
937987
return true;
938-
case EncFinal:
988+
case State.ENC_FINAL:
939989
throw new IllegalStateException(getAlgorithmName() + " cannot be reused for encryption");
940990
default:
941991
throw new IllegalStateException(getAlgorithmName() + " needs to be initialized");
@@ -969,12 +1019,12 @@ protected final void ensureInitialized()
9691019
// Used for Grain128 AEAD and Romulus Engine
9701020
protected void finishAAD1(State nextState)
9711021
{
972-
switch (m_state)
1022+
switch (m_state.ord)
9731023
{
974-
case DecInit:
975-
case DecAad:
976-
case EncInit:
977-
case EncAad:
1024+
case State.DEC_INIT:
1025+
case State.DEC_AAD:
1026+
case State.ENC_INIT:
1027+
case State.ENC_AAD:
9781028
{
9791029
processFinalAAD();
9801030
break;
@@ -989,10 +1039,10 @@ protected void finishAAD1(State nextState)
9891039
protected void finishAAD2(State nextState)
9901040
{
9911041
// State indicates whether we ever received AAD
992-
switch (m_state)
1042+
switch (m_state.ord)
9931043
{
994-
case DecAad:
995-
case EncAad:
1044+
case State.DEC_AAD:
1045+
case State.ENC_AAD:
9961046
{
9971047
processFinalAAD();
9981048
break;
@@ -1009,16 +1059,16 @@ protected void finishAAD2(State nextState)
10091059
protected void finishAAD3(State nextState, boolean isDoFinal)
10101060
{
10111061
// State indicates whether we ever received AAD
1012-
switch (m_state)
1062+
switch (m_state.ord)
10131063
{
1014-
case DecInit:
1015-
case DecAad:
1064+
case State.DEC_INIT:
1065+
case State.DEC_AAD:
10161066
if (!isDoFinal && dataOperator.getLen() <= MAC_SIZE)
10171067
{
10181068
return;
10191069
}
1020-
case EncInit:
1021-
case EncAad:
1070+
case State.ENC_INIT:
1071+
case State.ENC_AAD:
10221072
processFinalAAD();
10231073
break;
10241074
}

0 commit comments

Comments
 (0)