Skip to content

Commit d26c8c3

Browse files
author
gefeili
committed
Introduce AADOperator and related classes.
1 parent a349902 commit d26c8c3

File tree

11 files changed

+300
-173
lines changed

11 files changed

+300
-173
lines changed

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

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

3+
import java.io.ByteArrayOutputStream;
4+
35
import org.bouncycastle.crypto.DataLengthException;
46
import org.bouncycastle.crypto.InvalidCipherTextException;
57
import org.bouncycastle.crypto.OutputLengthException;
@@ -16,6 +18,13 @@ protected enum ProcessingBufferType
1618
ImmediateLargeMac,
1719
}
1820

21+
protected enum AADOperatorType
22+
{
23+
Default,
24+
Counter,
25+
Stream
26+
}
27+
1928
protected enum State
2029
{
2130
Uninitialized,
@@ -38,6 +47,7 @@ protected enum State
3847
protected State m_state = State.Uninitialized;
3948
protected int m_bufferSizeDecrypt;
4049
protected AADProcessingBuffer processor;
50+
protected AADOperator aadOperator;
4151

4252
protected AEADBufferBaseEngine(ProcessingBufferType type)
4353
{
@@ -58,7 +68,39 @@ protected AEADBufferBaseEngine(ProcessingBufferType type)
5868
}
5969
}
6070

61-
private interface AADProcessingBuffer
71+
protected void setInnerMembers(ProcessingBufferType type, AADOperatorType aadOperatorType)
72+
{
73+
// switch (type)
74+
// {
75+
// case Buffered:
76+
// processor = new BufferedAADProcessor();
77+
// break;
78+
// case BufferedLargeMac:
79+
// processor = new BufferedLargeMacAADProcessor();
80+
// break;
81+
// case Immediate:
82+
// processor = new ImmediateAADProcessor();
83+
// break;
84+
// case ImmediateLargeMac:
85+
// processor = new ImmediateLargeMacAADProcessor();
86+
// break;
87+
// }
88+
89+
switch (aadOperatorType)
90+
{
91+
case Default:
92+
aadOperator = new DefaultAADOperator();
93+
break;
94+
case Counter:
95+
aadOperator = new CounterAADOperator();
96+
break;
97+
case Stream:
98+
aadOperator = new StreamAADOperator();
99+
break;
100+
}
101+
}
102+
103+
protected interface AADProcessingBuffer
62104
{
63105
void processAADByte(byte input);
64106

@@ -176,11 +218,173 @@ public int processDecryptBytes(byte[] input, int inOff, int len, byte[] output,
176218
}
177219
}
178220

221+
protected interface AADOperator
222+
{
223+
void processAADByte(byte input);
224+
225+
void processAADBytes(byte[] input, int inOff, int len);
226+
227+
void reset();
228+
229+
int getAadLen();
230+
}
231+
232+
protected class DefaultAADOperator
233+
implements AADOperator
234+
{
235+
@Override
236+
public void processAADByte(byte input)
237+
{
238+
processor.processAADByte(input);
239+
}
240+
241+
@Override
242+
public void processAADBytes(byte[] input, int inOff, int len)
243+
{
244+
if (m_aadPos > 0)
245+
{
246+
int available = AADBufferSize - m_aadPos;
247+
if (processor.isLengthWithinAvailableSpace(len, available))
248+
{
249+
System.arraycopy(input, inOff, m_aad, m_aadPos, len);
250+
m_aadPos += len;
251+
return;
252+
}
253+
254+
System.arraycopy(input, inOff, m_aad, m_aadPos, available);
255+
inOff += available;
256+
len -= available;
257+
258+
processBufferAAD(m_aad, 0);
259+
}
260+
while (processor.isLengthExceedingBlockSize(len, AADBufferSize))
261+
{
262+
processBufferAAD(input, inOff);
263+
inOff += AADBufferSize;
264+
len -= AADBufferSize;
265+
}
266+
System.arraycopy(input, inOff, m_aad, 0, len);
267+
m_aadPos = len;
268+
}
269+
270+
public void reset()
271+
{
272+
273+
}
274+
275+
@Override
276+
public int getAadLen()
277+
{
278+
return m_aadPos;
279+
}
280+
}
281+
282+
protected class CounterAADOperator
283+
implements AADOperator
284+
{
285+
private int aadLen;
286+
287+
@Override
288+
public void processAADByte(byte input)
289+
{
290+
aadLen++;
291+
processor.processAADByte(input);
292+
}
293+
294+
@Override
295+
public void processAADBytes(byte[] input, int inOff, int len)
296+
{
297+
aadLen += len;
298+
if (m_aadPos > 0)
299+
{
300+
int available = AADBufferSize - m_aadPos;
301+
if (processor.isLengthWithinAvailableSpace(len, available))
302+
{
303+
System.arraycopy(input, inOff, m_aad, m_aadPos, len);
304+
m_aadPos += len;
305+
return;
306+
}
307+
308+
System.arraycopy(input, inOff, m_aad, m_aadPos, available);
309+
inOff += available;
310+
len -= available;
311+
312+
processBufferAAD(m_aad, 0);
313+
}
314+
while (processor.isLengthExceedingBlockSize(len, AADBufferSize))
315+
{
316+
processBufferAAD(input, inOff);
317+
inOff += AADBufferSize;
318+
len -= AADBufferSize;
319+
}
320+
System.arraycopy(input, inOff, m_aad, 0, len);
321+
m_aadPos = len;
322+
}
323+
324+
public int getAadLen()
325+
{
326+
return aadLen;
327+
}
328+
329+
public void reset()
330+
{
331+
aadLen = 0;
332+
}
333+
}
334+
335+
protected class StreamAADOperator
336+
implements AADOperator
337+
{
338+
private ErasableOutputStream stream = new ErasableOutputStream();
339+
340+
@Override
341+
public void processAADByte(byte input)
342+
{
343+
stream.write(input);
344+
}
345+
346+
@Override
347+
public void processAADBytes(byte[] input, int inOff, int len)
348+
{
349+
stream.write(input, inOff, len);
350+
}
351+
352+
public byte[] getBytes()
353+
{
354+
return stream.getBuf();
355+
}
356+
357+
@Override
358+
public void reset()
359+
{
360+
stream.reset();
361+
}
362+
363+
@Override
364+
public int getAadLen()
365+
{
366+
return stream.size();
367+
}
368+
}
369+
370+
protected static final class ErasableOutputStream
371+
extends ByteArrayOutputStream
372+
{
373+
public ErasableOutputStream()
374+
{
375+
}
376+
377+
public byte[] getBuf()
378+
{
379+
return buf;
380+
}
381+
}
382+
179383
@Override
180384
public void processAADByte(byte input)
181385
{
182386
checkAAD();
183-
processor.processAADByte(input);
387+
aadOperator.processAADByte(input);
184388
}
185389

186390
@Override
@@ -194,30 +398,7 @@ public void processAADBytes(byte[] input, int inOff, int len)
194398
}
195399

196400
checkAAD();
197-
if (m_aadPos > 0)
198-
{
199-
int available = AADBufferSize - m_aadPos;
200-
if (processor.isLengthWithinAvailableSpace(len, available))
201-
{
202-
System.arraycopy(input, inOff, m_aad, m_aadPos, len);
203-
m_aadPos += len;
204-
return;
205-
}
206-
207-
System.arraycopy(input, inOff, m_aad, m_aadPos, available);
208-
inOff += available;
209-
len -= available;
210-
211-
processBufferAAD(m_aad, 0);
212-
}
213-
while (processor.isLengthExceedingBlockSize(len, AADBufferSize))
214-
{
215-
processBufferAAD(input, inOff);
216-
inOff += AADBufferSize;
217-
len -= AADBufferSize;
218-
}
219-
System.arraycopy(input, inOff, m_aad, 0, len);
220-
m_aadPos = len;
401+
aadOperator.processAADBytes(input, inOff, len);
221402
}
222403

223404
@Override
@@ -520,6 +701,7 @@ protected void bufferReset()
520701
default:
521702
throw new IllegalStateException(getAlgorithmName() + " needs to be initialized");
522703
}
704+
aadOperator.reset();
523705
}
524706

525707
protected void ensureSufficientOutputBuffer(byte[] output, int outOff, int len)

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class AsconBaseEngine
2727
protected AsconBaseEngine(ProcessingBufferType type)
2828
{
2929
super(type);
30+
setInnerMembers(type, AADOperatorType.Default);
3031
}
3132

3233
private void round(long C)
@@ -92,6 +93,7 @@ protected void finishAAD(State nextState, boolean isDofinal)
9293

9394
protected abstract void processFinalEncrypt(byte[] input, int inLen, byte[] output, int outOff);
9495

96+
9597
protected void processBufferAAD(byte[] buffer, int inOff)
9698
{
9799
x0 ^= loadBytes(buffer, inOff);
@@ -102,6 +104,12 @@ protected void processBufferAAD(byte[] buffer, int inOff)
102104
p(nr);
103105
}
104106

107+
protected void processFinalAAD()
108+
{
109+
processFinalAadBlock();
110+
p(nr);
111+
}
112+
105113
@Override
106114
protected void processFinalBlock(byte[] output, int outOff)
107115
{
@@ -118,13 +126,6 @@ protected void processFinalBlock(byte[] output, int outOff)
118126
setBytes(x4, mac, 8);
119127
}
120128

121-
@Override
122-
protected void processFinalAAD()
123-
{
124-
processFinalAadBlock();
125-
p(nr);
126-
}
127-
128129
protected void processBufferDecrypt(byte[] buffer, int bufOff, byte[] output, int outOff)
129130
{
130131
long t0 = loadBytes(buffer, bufOff);

0 commit comments

Comments
 (0)