Skip to content

Commit 811083a

Browse files
author
gefeili
committed
Add tests to CipherTest, fix the bug in AEADBufferBaseEngine.processBytes when MAC_SIZE>BlockSize
1 parent ff9908f commit 811083a

File tree

7 files changed

+126
-37
lines changed

7 files changed

+126
-37
lines changed

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

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,62 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
135135
m_bufPos += len;
136136
return 0;
137137
}
138-
139-
if (m_bufPos > BlockSize)
138+
if (BlockSize >= MAC_SIZE)
140139
{
141-
validateAndProcessBuffer(m_buf, 0, output, outOff);
142-
m_bufPos -= BlockSize;
143-
System.arraycopy(m_buf, BlockSize, m_buf, 0, m_bufPos);
144-
resultLength = BlockSize;
140+
if (m_bufPos > BlockSize)
141+
{
142+
validateAndProcessBuffer(m_buf, 0, output, outOff);
143+
m_bufPos -= BlockSize;
144+
System.arraycopy(m_buf, BlockSize, m_buf, 0, m_bufPos);
145+
resultLength = BlockSize;
146+
147+
available += BlockSize;
148+
if (len <= available)
149+
{
150+
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
151+
m_bufPos += len;
152+
return resultLength;
153+
}
154+
}
145155

146-
available += BlockSize;
147-
if (len <= available)
156+
available = BlockSize - m_bufPos;
157+
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
158+
inOff += available;
159+
len -= available;
160+
validateAndProcessBuffer(m_buf, 0, output, outOff + resultLength);
161+
resultLength += BlockSize;
162+
//m_bufPos = 0;
163+
}
164+
else
165+
{
166+
while (m_bufPos > BlockSize && len + m_bufPos > BlockSize + MAC_SIZE)
148167
{
149-
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
150-
m_bufPos += len;
151-
return resultLength;
168+
validateAndProcessBuffer(m_buf, resultLength, output, outOff);
169+
m_bufPos -= BlockSize;
170+
resultLength += BlockSize;
171+
outOff += BlockSize;
172+
}
173+
if (m_bufPos != 0)
174+
{
175+
System.arraycopy(m_buf, resultLength, m_buf, 0, m_bufPos);
176+
if (m_bufPos + len > BlockSize + MAC_SIZE)
177+
{
178+
available = Math.max(BlockSize - m_bufPos, 0);
179+
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
180+
inOff += available;
181+
validateAndProcessBuffer(m_buf, 0, output, outOff);
182+
resultLength += BlockSize;
183+
len -= available;
184+
outOff += BlockSize;
185+
}
186+
else
187+
{
188+
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
189+
m_bufPos += len;
190+
return resultLength;
191+
}
152192
}
153193
}
154-
155-
available = BlockSize - m_bufPos;
156-
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
157-
inOff += available;
158-
len -= available;
159-
validateAndProcessBuffer(m_buf, 0, output, outOff + resultLength);
160-
resultLength += BlockSize;
161-
//m_bufPos = 0;
162-
163194
while (len > BlockSize + MAC_SIZE)
164195
{
165196
validateAndProcessBuffer(input, inOff, output, outOff + resultLength);

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,12 @@ protected void processFinalAAD()
185185
}
186186
}
187187

188-
@Override
189-
public void reset()
188+
protected void reset(boolean clearMac)
190189
{
191190
if (!initialised)
192191
{
193192
throw new IllegalArgumentException("Need call init function before encryption/decryption");
194193
}
195-
196-
reset(true);
197-
}
198-
199-
protected void reset(boolean clearMac)
200-
{
201194
bufferReset();
202195
input_empty = true;
203196
aadLen = 0;

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,12 @@ protected void processFinalBlock(byte[] output, int outOff)
130130
Up(mac, MAC_SIZE, 0x40);
131131
}
132132

133-
@Override
134-
public void reset()
133+
protected void reset(boolean clearMac)
135134
{
136135
if (!initialised)
137136
{
138137
throw new IllegalArgumentException("Need call init function before encryption/decryption");
139138
}
140-
reset(true);
141-
}
142-
143-
protected void reset(boolean clearMac)
144-
{
145139
Arrays.fill(state, (byte)0);
146140
aadFinished = false;
147141
encrypted = false;

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ static void isEqualTo(
255255
}
256256
}
257257

258-
void checkCipher(final BlockCipher pCipher, final int datalen)
258+
void checkCipher(final BlockCipher pCipher, final int datalen)
259259
throws Exception
260260
{
261261
final SecureRandom random = new SecureRandom();
@@ -346,6 +346,71 @@ public void operation()
346346
cipher.init(true, new AEADParameters(new KeyParameter(key), macSize2, iv, null));
347347
}
348348
});
349+
}
350+
351+
352+
/**
353+
* @param DATALEN Data length
354+
* @param PARTLEN Partial Data length. Must be greater than or equal to internal buffer length to exhibit problem.
355+
* @param AEADLEN AEAD length.
356+
* @param NONCELEN Nonce length.
357+
* */
358+
static void checkAEADCipherMultipleBlocks(SimpleTest test, int DATALEN, int PARTLEN, int AEADLEN, int NONCELEN, final AEADCipher pCipher)
359+
throws InvalidCipherTextException
360+
{
361+
/* Obtain some random data */
362+
final byte[] myData = new byte[DATALEN];
363+
final SecureRandom myRandom = new SecureRandom();
364+
myRandom.nextBytes(myData);
365+
366+
/* Obtain some random AEAD */
367+
final byte[] myAEAD = new byte[AEADLEN];
368+
myRandom.nextBytes(myAEAD);
369+
370+
/* Create the Key parameters */
371+
final CipherKeyGenerator myGenerator = new CipherKeyGenerator();
372+
final KeyGenerationParameters myGenParams = new KeyGenerationParameters(myRandom, 128);
373+
myGenerator.init(myGenParams);
374+
final byte[] myKey = myGenerator.generateKey();
375+
final KeyParameter myKeyParams = new KeyParameter(myKey);
376+
377+
/* Create the nonce */
378+
final byte[] myNonce = new byte[NONCELEN];
379+
myRandom.nextBytes(myNonce);
380+
final ParametersWithIV myParams = new ParametersWithIV(myKeyParams, myNonce);
381+
382+
/* Initialise the cipher for encryption */
383+
pCipher.init(true, myParams);
384+
final int myExpectedOutLen = pCipher.getOutputSize(DATALEN);
385+
final byte[] myEncrypted = new byte[myExpectedOutLen];
386+
pCipher.processAADBytes(myAEAD, 0, AEADLEN);
387+
388+
/* Loop processing partial data */
389+
int myOutLen = 0;
390+
for (int myPos = 0; myPos < DATALEN; myPos += PARTLEN)
391+
{
392+
final int myLen = Math.min(PARTLEN, DATALEN - myPos);
393+
myOutLen += pCipher.processBytes(myData, myPos, myLen, myEncrypted, myOutLen);
394+
}
395+
396+
/* Finish the encryption */
397+
myOutLen += pCipher.doFinal(myEncrypted, myOutLen);
398+
399+
/* Initialise the cipher for decryption */
400+
pCipher.init(false, myParams);
401+
final int myExpectedClearLen = pCipher.getOutputSize(myOutLen);
402+
final byte[] myDecrypted = new byte[myExpectedClearLen];
403+
pCipher.processAADBytes(myAEAD, 0, AEADLEN);
404+
int myClearLen = 0;
405+
for (int myPos = 0; myPos < myOutLen; myPos += PARTLEN)
406+
{
407+
final int myLen = Math.min(PARTLEN, myOutLen - myPos);
408+
myClearLen += pCipher.processBytes(myEncrypted, myPos, myLen, myDecrypted, myClearLen);
409+
}
410+
myClearLen += pCipher.doFinal(myDecrypted, myClearLen);
411+
final byte[] myResult = Arrays.copyOf(myDecrypted, myClearLen);
349412

413+
/* Check that we have the same result */
414+
test.isTrue("cipher text check", Arrays.areEqual(myData, myResult));
350415
}
351416
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public String getName()
2727
public void performTest()
2828
throws Exception
2929
{
30+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 12, new ElephantEngine(ElephantEngine.ElephantParameters.elephant160));
31+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 12, new ElephantEngine(ElephantEngine.ElephantParameters.elephant176));
32+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 12, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));
3033
CipherTest.checkAEADParemeter(this, 16, 12, 8, 20, new ElephantEngine(ElephantEngine.ElephantParameters.elephant160));
3134
CipherTest.checkAEADParemeter(this, 16, 12, 8, 22, new ElephantEngine(ElephantEngine.ElephantParameters.elephant176));
3235
CipherTest.checkAEADParemeter(this, 16, 12, 16, 25, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public String getName()
3131
public void performTest()
3232
throws Exception
3333
{
34+
CipherTest.checkAEADCipherMultipleBlocks(this, 1024, 19, 100, 16 , new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb128));
35+
CipherTest.checkAEADCipherMultipleBlocks(this, 1024, 19, 100, 16 , new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32));
3436
testVectors(PhotonBeetleEngine.PhotonBeetleParameters.pb32, "v32");
3537
testVectors(PhotonBeetleEngine.PhotonBeetleParameters.pb128, "v128");
3638
DigestTest.checkDigestReset(this, new PhotonBeetleDigest());
@@ -97,7 +99,7 @@ private void testVectors(PhotonBeetleEngine.PhotonBeetleParameters pbp, String f
9799
int a = line.indexOf('=');
98100
if (a < 0)
99101
{
100-
// if (map.get("Count").equals("133"))
102+
// if (map.get("Count").equals("298"))
101103
// {
102104
// System.out.println("test");
103105
// }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public String getName()
3333
public void performTest()
3434
throws Exception
3535
{
36+
CipherTest.checkAEADCipherMultipleBlocks(this, 1024, 18, 100, 16 , new XoodyakEngine());
3637
testVectors();
3738
CipherTest.checkCipher(32, 16, 100, 128, new CipherTest.Instance()
3839
{

0 commit comments

Comments
 (0)