Skip to content

Commit 8067e71

Browse files
committed
Merge branch '2103-romulusM-output-length' into 'main'
Fix Issue #2103 See merge request root/bc-java!112
2 parents aa379f3 + 704f4a7 commit 8067e71

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ protected class StreamDataOperator
630630

631631
public int processByte(byte input, byte[] output, int outOff)
632632
{
633+
checkData(false);
633634
ensureInitialized();
634635
stream.write(input);
635636
m_bufPos = stream.size();
@@ -639,6 +640,7 @@ public int processByte(byte input, byte[] output, int outOff)
639640
@Override
640641
public int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff)
641642
{
643+
checkData(false);
642644
ensureInitialized();
643645
stream.write(input, inOff, len);
644646
m_bufPos = stream.size();

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

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,117 @@
11
package org.bouncycastle.crypto.test;
22

3+
import java.security.SecureRandom;
4+
5+
import org.bouncycastle.crypto.CipherKeyGenerator;
36
import org.bouncycastle.crypto.CipherParameters;
7+
import org.bouncycastle.crypto.InvalidCipherTextException;
8+
import org.bouncycastle.crypto.KeyGenerationParameters;
49
import org.bouncycastle.crypto.digests.RomulusDigest;
510
import org.bouncycastle.crypto.engines.RomulusEngine;
611
import org.bouncycastle.crypto.modes.AEADCipher;
712
import org.bouncycastle.crypto.params.KeyParameter;
813
import org.bouncycastle.crypto.params.ParametersWithIV;
14+
import org.bouncycastle.util.Arrays;
915
import org.bouncycastle.util.test.SimpleTest;
1016

1117
public class RomulusTest
1218
extends SimpleTest
1319
{
20+
/**
21+
* Data length.
22+
*/
23+
private static final int DATALEN = 1025;
24+
25+
/**
26+
* AEAD length.
27+
*/
28+
private static final int AEADLEN = 10;
29+
30+
31+
/**
32+
* Check cipher.
33+
*
34+
* @param pCipher the cipher
35+
*/
36+
private static void checkCipher(final AEADCipher pCipher,
37+
final int pNonceLen)
38+
throws Exception
39+
{
40+
try
41+
{
42+
/* Obtain some random data */
43+
final byte[] myData = new byte[DATALEN];
44+
final SecureRandom myRandom = new SecureRandom();
45+
myRandom.nextBytes(myData);
46+
47+
/* Obtain some random AEAD */
48+
final byte[] myAEAD = new byte[AEADLEN];
49+
myRandom.nextBytes(myAEAD);
50+
51+
/* Create the Key parameters */
52+
final CipherKeyGenerator myGenerator = new CipherKeyGenerator();
53+
final KeyGenerationParameters myGenParams = new KeyGenerationParameters(myRandom, 128);
54+
myGenerator.init(myGenParams);
55+
final byte[] myKey = myGenerator.generateKey();
56+
final KeyParameter myKeyParams = new KeyParameter(myKey);
57+
58+
/* Create the nonce */
59+
final byte[] myNonce = new byte[pNonceLen];
60+
myRandom.nextBytes(myNonce);
61+
final ParametersWithIV myParams = new ParametersWithIV(myKeyParams, myNonce);
62+
63+
/* Initialise the cipher for encryption */
64+
pCipher.init(true, myParams);
65+
final int myMaxOutLen = pCipher.getOutputSize(DATALEN);
66+
final byte[] myEncrypted = new byte[myMaxOutLen];
67+
//pCipher.processAADBytes(myAEAD, 0, AEADLEN);
68+
int myOutLen = pCipher.processBytes(myData, 0, DATALEN, myEncrypted, 0);
69+
int myRemaining = pCipher.getOutputSize(0);
70+
if (myRemaining + myOutLen < myMaxOutLen)
71+
{
72+
/*********************************************************************/
73+
/* FAILS HERE */
74+
/*********************************************************************/
75+
System.out.println("Bad outputLength on encryption for " + pCipher.getAlgorithmName());
76+
}
77+
int myProcessed = pCipher.doFinal(myEncrypted, myOutLen);
78+
if (myOutLen + myProcessed != myMaxOutLen)
79+
{
80+
System.out.println("Bad total on encryption for " + pCipher.getAlgorithmName());
81+
}
82+
83+
/* Note that myOutLen is too large by DATALEN */
84+
85+
/* Initialise the cipher for decryption */
86+
pCipher.init(false, myParams);
87+
final int myMaxClearLen = pCipher.getOutputSize(myMaxOutLen);
88+
final byte[] myDecrypted = new byte[myMaxClearLen];
89+
//pCipher.processAADBytes(myAEAD, 0, AEADLEN);
90+
int myClearLen = pCipher.processBytes(myEncrypted, 0, myEncrypted.length, myDecrypted, 0);
91+
myRemaining = pCipher.getOutputSize(0);
92+
if (myRemaining + myClearLen < myMaxClearLen)
93+
{
94+
System.out.println("Bad outputLength on decryption for " + pCipher.getAlgorithmName());
95+
}
96+
myProcessed = pCipher.doFinal(myDecrypted, myClearLen);
97+
if (myClearLen + myProcessed != myMaxClearLen)
98+
{
99+
System.out.println("Bad total on decryption for " + pCipher.getAlgorithmName());
100+
}
101+
final byte[] myResult = Arrays.copyOf(myDecrypted, DATALEN);
102+
103+
/* Check that we have the same result */
104+
if (!Arrays.areEqual(myData, myResult))
105+
{
106+
System.out.println("Cipher " + pCipher.getAlgorithmName() + " failed");
107+
}
108+
}
109+
catch (InvalidCipherTextException e)
110+
{
111+
throw new RuntimeException(e);
112+
}
113+
}
114+
14115
public String getName()
15116
{
16117
return "Romulus";
@@ -19,6 +120,7 @@ public String getName()
19120
public void performTest()
20121
throws Exception
21122
{
123+
checkCipher(new RomulusEngine(RomulusEngine.RomulusParameters.RomulusM), 16);
22124
DigestTest.implTestVectorsDigest(this, new RomulusDigest(), "crypto/romulus", "LWC_HASH_KAT_256.txt");
23125
DigestTest.checkDigestReset(this, new RomulusDigest());
24126
DigestTest.implTestExceptionsAndParametersDigest(this, new RomulusDigest(), 32);
@@ -153,10 +255,10 @@ private void implTestParametersEngine(RomulusEngine cipher, int keySize, int ivS
153255
}
154256
}
155257

156-
public static void main(String[] args)
157-
{
158-
runTest(new RomulusTest());
159-
}
258+
// public static void main(String[] args)
259+
// {
260+
// runTest(new RomulusTest());
261+
// }
160262
}
161263

162264

0 commit comments

Comments
 (0)