Skip to content

Commit 4361cd7

Browse files
author
gefeili
committed
Refactor ProcessByte
1 parent 3d93144 commit 4361cd7

File tree

3 files changed

+123
-14
lines changed

3 files changed

+123
-14
lines changed

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

Lines changed: 105 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ public byte[] getMac()
9191
return mac;
9292
}
9393

94-
public void reset()
95-
{
96-
reset(true);
97-
}
98-
94+
@Override
9995
public void init(boolean forEncryption, CipherParameters params)
10096
{
10197
this.forEncryption = forEncryption;
@@ -155,6 +151,12 @@ else if (params instanceof ParametersWithIV)
155151
}
156152
}
157153

154+
@Override
155+
public void reset()
156+
{
157+
reset(true);
158+
}
159+
158160
protected void reset(boolean clearMac)
159161
{
160162
ensureInitialized();
@@ -246,10 +248,12 @@ protected void setInnerMembers(ProcessingBufferType type, AADOperatorType aadOpe
246248
}
247249
}
248250

249-
protected interface AADProcessingBuffer
251+
private interface AADProcessingBuffer
250252
{
251253
void processAADByte(byte input);
252254

255+
int processByte(byte input, byte[] output, int outOff);
256+
253257
int getUpdateOutputSize(int len);
254258

255259
boolean isLengthWithinAvailableSpace(int len, int available);
@@ -270,6 +274,15 @@ public void processAADByte(byte input)
270274
m_aad[m_aadPos++] = input;
271275
}
272276

277+
@Override
278+
public int processByte(byte input, byte[] output, int outOff)
279+
{
280+
checkData(false);
281+
int rlt = processEncDecByte(output, outOff);
282+
m_buf[m_bufPos++] = input;
283+
return rlt;
284+
}
285+
273286
@Override
274287
public boolean isLengthWithinAvailableSpace(int len, int available)
275288
{
@@ -303,6 +316,14 @@ public void processAADByte(byte input)
303316
}
304317
}
305318

319+
@Override
320+
public int processByte(byte input, byte[] output, int outOff)
321+
{
322+
checkData(false);
323+
m_buf[m_bufPos++] = input;
324+
return processEncDecByte(output, outOff);
325+
}
326+
306327
@Override
307328
public int getUpdateOutputSize(int len)
308329
{
@@ -333,7 +354,7 @@ protected interface AADOperator
333354
int getLen();
334355
}
335356

336-
protected class DefaultAADOperator
357+
private class DefaultAADOperator
337358
implements AADOperator
338359
{
339360
@Override
@@ -359,7 +380,7 @@ public int getLen()
359380
}
360381
}
361382

362-
protected class CounterAADOperator
383+
private class CounterAADOperator
363384
implements AADOperator
364385
{
365386
private int aadLen;
@@ -426,16 +447,23 @@ public int getLen()
426447

427448
protected interface DataOperator
428449
{
450+
int processByte(byte input, byte[] output, int outOff);
451+
429452
int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff);
430453

431454
int getLen();
432455

433456
void reset();
434457
}
435458

436-
protected class DefaultDataOperator
459+
private class DefaultDataOperator
437460
implements DataOperator
438461
{
462+
public int processByte(byte input, byte[] output, int outOff)
463+
{
464+
return processor.processByte(input, output, outOff);
465+
}
466+
439467
public int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff)
440468
{
441469
return processEncDecBytes(input, inOff, len, output, outOff);
@@ -453,11 +481,17 @@ public void reset()
453481
}
454482
}
455483

456-
protected class CounterDataOperator
484+
private class CounterDataOperator
457485
implements DataOperator
458486
{
459487
private int messegeLen;
460488

489+
public int processByte(byte input, byte[] output, int outOff)
490+
{
491+
messegeLen++;
492+
return processor.processByte(input, output, outOff);
493+
}
494+
461495
public int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff)
462496
{
463497
messegeLen += len;
@@ -482,6 +516,14 @@ protected class StreamDataOperator
482516
{
483517
private final ErasableOutputStream stream = new ErasableOutputStream();
484518

519+
public int processByte(byte input, byte[] output, int outOff)
520+
{
521+
ensureInitialized();
522+
stream.write(input);
523+
m_bufPos = stream.size();
524+
return 0;
525+
}
526+
485527
@Override
486528
public int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff)
487529
{
@@ -509,11 +551,39 @@ public void reset()
509551
}
510552
}
511553

512-
protected class StreamCipherOperator
554+
private class StreamCipherOperator
513555
implements DataOperator
514556
{
557+
//TODO: shift index instead of arraycopy
515558
private int len;
516559

560+
public int processByte(byte input, byte[] output, int outOff)
561+
{
562+
boolean forEncryption = checkData(false);
563+
if (forEncryption)
564+
{
565+
this.len = 1;
566+
processBufferEncrypt(new byte[]{input}, 0, output, outOff);
567+
return 1;
568+
}
569+
else
570+
{
571+
if (m_bufPos == MAC_SIZE)
572+
{
573+
this.len = 1;
574+
processBufferDecrypt(m_buf, 0, output, outOff);
575+
System.arraycopy(m_buf, 1, m_buf, 0, m_bufPos - 1);
576+
m_buf[m_bufPos - 1] = input;
577+
return 1;
578+
}
579+
else
580+
{
581+
m_buf[m_bufPos++] = input;
582+
return 0;
583+
}
584+
}
585+
}
586+
517587
@Override
518588
public int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff)
519589
{
@@ -614,10 +684,33 @@ private void processAadBytes(byte[] input, int inOff, int len)
614684
m_aadPos = len;
615685
}
616686

687+
@Override
617688
public int processByte(byte in, byte[] out, int outOff)
618689
throws DataLengthException
619690
{
620-
return processBytes(new byte[]{in}, 0, 1, out, outOff);
691+
return dataOperator.processByte(in, out, outOff);
692+
}
693+
694+
protected int processEncDecByte(byte[] output, int outOff)
695+
{
696+
int rlt = 0;
697+
int available = (forEncryption ? BlockSize : m_bufferSizeDecrypt) - m_bufPos;
698+
if (available == 0)
699+
{
700+
ensureSufficientOutputBuffer(output, outOff, BlockSize);
701+
if (forEncryption)
702+
{
703+
processBufferEncrypt(m_buf, 0, output, outOff);
704+
}
705+
else
706+
{
707+
processBufferDecrypt(m_buf, 0, output, outOff);
708+
System.arraycopy(m_buf, BlockSize, m_buf, 0, m_bufPos - BlockSize);
709+
}
710+
m_bufPos -= BlockSize;
711+
rlt = BlockSize;
712+
}
713+
return rlt;
621714
}
622715

623716
@Override

core/src/test/java/org/bouncycastle/crypto/test/CipherTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,11 @@ static void checkAEADParemeter(SimpleTest test, int keySize, int ivSize, final i
331331
{
332332
cipher.processAADByte(aad[i]);
333333
}
334-
int len = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext1, 0);
334+
int len = 0;
335+
for (int i = 0; i < plaintext.length; ++i)
336+
{
337+
len += cipher.processByte(plaintext[i], ciphertext1, len);
338+
}
335339
len += cipher.doFinal(ciphertext1, len);
336340
int aadSplit = random.nextInt(aad.length) + 1;
337341
cipher.init(true, new AEADParameters(new KeyParameter(key), macSize * 8, iv, Arrays.copyOf(aad, aadSplit)));
@@ -345,6 +349,18 @@ static void checkAEADParemeter(SimpleTest test, int keySize, int ivSize, final i
345349
len = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext3, 0);
346350
len += cipher.doFinal(ciphertext3, len);
347351
test.isTrue("cipher text check", Arrays.areEqual(ciphertext1, ciphertext2));
352+
cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
353+
for (int i = 0; i < aad.length; ++i)
354+
{
355+
cipher.processAADByte(aad[i]);
356+
}
357+
len = 0;
358+
byte[] plaintext1 = new byte[plaintext.length];
359+
for (int i = 0; i < ciphertext1.length; ++i)
360+
{
361+
len += cipher.processByte(ciphertext1[i], plaintext1, len);
362+
}
363+
len += cipher.doFinal(plaintext1, len);
348364

349365
test.testException("Invalid value for MAC size: ", "IllegalArgumentException", new TestExceptionOperation()
350366
{

core/src/test/java/org/bouncycastle/crypto/test/Grain128AEADTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public AEADCipher createInstance()
3737
CipherTest.checkAEADCipherMultipleBlocks(this, 1024, 7, 100, 128, 12, new Grain128AEADEngine());
3838

3939

40-
CipherTest.checkAEADParemeter(this, 16, 12, 8, 16, new Grain128AEADEngine());
40+
CipherTest.checkAEADParemeter(this, 16, 12, 8, 20, new Grain128AEADEngine());
4141
testSplitUpdate();
4242
testExceptions();
4343
testLongAEAD();

0 commit comments

Comments
 (0)