Skip to content

Commit c26de05

Browse files
author
Administrator
committed
Merge branch '1839-photonbeetle-xoodyak-digests-reset' into 'main'
Fix the issue about doFinal() of PhotonBeetle and Xoodyak digest do not reset properly See merge request root/bc-java!32
2 parents d099702 + a933b7e commit c26de05

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ else if (inlen <= INITIAL_RATE_INBYTES)
137137
System.arraycopy(state, 0, output, outOff, SQUEEZE_RATE_INBYTES);
138138
PHOTON_Permutation();
139139
System.arraycopy(state, 0, output, outOff + SQUEEZE_RATE_INBYTES, TAG_INBYTES - SQUEEZE_RATE_INBYTES);
140+
reset();
140141
return TAG_INBYTES;
141142
}
142143

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public int doFinal(byte[] output, int outOff)
105105
Up(output, outOff, TAGLEN, 0x40);
106106
Down(null, 0, 0, 0);
107107
Up(output, outOff + TAGLEN, TAGLEN, 0);
108+
reset();
108109
return 32;
109110
}
110111

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package org.bouncycastle.crypto.test;
22

3+
import java.security.SecureRandom;
4+
35
import org.bouncycastle.crypto.Digest;
46
import org.bouncycastle.crypto.digests.EncodableDigest;
57
import org.bouncycastle.util.Arrays;
68
import org.bouncycastle.util.Memoable;
79
import org.bouncycastle.util.encoders.Hex;
810
import org.bouncycastle.util.test.SimpleTest;
11+
import org.bouncycastle.util.test.SimpleTestResult;
12+
import org.bouncycastle.util.test.TestFailedException;
913

1014
public abstract class DigestTest
1115
extends SimpleTest
@@ -243,4 +247,31 @@ protected void sixtyFourKTest(
243247
fail("64k test failed", expected, new String(Hex.encode(resBuf)));
244248
}
245249
}
250+
251+
static void checkDigestReset(final SimpleTest test, final Digest pDigest)
252+
{
253+
int DATALEN = 100;
254+
/* Obtain some random data */
255+
final byte[] myData = new byte[DATALEN];
256+
final SecureRandom myRandom = new SecureRandom();
257+
myRandom.nextBytes(myData);
258+
259+
/* Update and finalise digest */
260+
final int myHashLen = pDigest.getDigestSize();
261+
final byte[] myFirst = new byte[myHashLen];
262+
pDigest.update(myData, 0, DATALEN);
263+
pDigest.doFinal(myFirst, 0);
264+
265+
266+
/* Reuse the digest */
267+
final byte[] mySecond = new byte[myHashLen];
268+
pDigest.update(myData, 0, DATALEN);
269+
pDigest.doFinal(mySecond, 0);
270+
271+
/* Check that we have the same result */
272+
if (!java.util.Arrays.equals(myFirst, mySecond))
273+
{
274+
throw new TestFailedException(SimpleTestResult.failed(test,"Digest " + pDigest.getAlgorithmName() + " does not reset properly on doFinal()"));
275+
}
276+
}
246277
}

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public String getName()
3030
public void performTest()
3131
throws Exception
3232
{
33+
DigestTest.checkDigestReset(this, new PhotonBeetleDigest());
3334
testVectorsHash();
3435
PhotonBeetleEngine pb = new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32);
3536
testExceptions(pb, pb.getKeyBytesSize(), pb.getIVBytesSize(), pb.getBlockSize());
@@ -46,22 +47,16 @@ private void testVectorsHash()
4647
throws Exception
4748
{
4849
PhotonBeetleDigest PhotonBeetle = new PhotonBeetleDigest();
49-
CipherParameters params;
5050
InputStream src = TestResourceFinder.findTestResource("crypto/photonbeetle", "LWC_HASH_KAT_256.txt");
5151
BufferedReader bin = new BufferedReader(new InputStreamReader(src));
5252
String line;
53-
byte[] ptByte, adByte;
54-
byte[] rv;
53+
byte[] ptByte;
5554
HashMap<String, String> map = new HashMap<String, String>();
5655
while ((line = bin.readLine()) != null)
5756
{
5857
int a = line.indexOf('=');
5958
if (a < 0)
6059
{
61-
// if (!map.get("Count").equals("3"))
62-
// {
63-
// continue;
64-
// }
6560
PhotonBeetle.reset();
6661
ptByte = Hex.decode((String)map.get("Msg"));
6762
PhotonBeetle.update(ptByte, 0, ptByte.length);
@@ -71,10 +66,6 @@ private void testVectorsHash()
7166
{
7267
mismatch("Keystream " + map.get("Count"), (String)map.get("MD"), hash);
7368
}
74-
// else
75-
// {
76-
// System.out.println("Keystream " + map.get("Count") + " pass");
77-
// }
7869
map.clear();
7970
PhotonBeetle.reset();
8071
}
@@ -83,7 +74,6 @@ private void testVectorsHash()
8374
map.put(line.substring(0, a).trim(), line.substring(a + 1).trim());
8475
}
8576
}
86-
//System.out.print.println("PhotonBeetle Hash pass");
8777
}
8878

8979
private void testVectors(PhotonBeetleEngine.PhotonBeetleParameters pbp, String filename)
@@ -94,18 +84,13 @@ private void testVectors(PhotonBeetleEngine.PhotonBeetleParameters pbp, String f
9484
InputStream src = TestResourceFinder.findTestResource("crypto/photonbeetle", filename + "_LWC_AEAD_KAT_128_128.txt");
9585
BufferedReader bin = new BufferedReader(new InputStreamReader(src));
9686
String line;
97-
byte[] ptByte, adByte;
9887
byte[] rv;
9988
HashMap<String, String> map = new HashMap<String, String>();
10089
while ((line = bin.readLine()) != null)
10190
{
10291
int a = line.indexOf('=');
10392
if (a < 0)
10493
{
105-
// if (!map.get("Count").equals("133"))
106-
// {
107-
// continue;
108-
// }
10994
byte[] key = Hex.decode(map.get("Key"));
11095
byte[] nonce = Hex.decode(map.get("Nonce"));
11196
byte[] ad = Hex.decode(map.get("AD"));
@@ -121,10 +106,6 @@ private void testVectors(PhotonBeetleEngine.PhotonBeetleParameters pbp, String f
121106
{
122107
mismatch("Keystream " + map.get("Count"), (String)map.get("CT"), rv);
123108
}
124-
// else
125-
// {
126-
// System.out.println("Keystream " + map.get("Count") + " pass");
127-
// }
128109
PhotonBeetle.reset();
129110
PhotonBeetle.init(false, params);
130111
//Decrypt

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public String getName()
3232
public void performTest()
3333
throws Exception
3434
{
35+
DigestTest.checkDigestReset(this, new XoodyakDigest());
3536
testVectorsHash();
3637
testVectors();
3738
XoodyakEngine xoodyak = new XoodyakEngine();
@@ -96,10 +97,6 @@ private void testVectors()
9697
int a = line.indexOf('=');
9798
if (a < 0)
9899
{
99-
// if (!map.get("Count").equals("826"))
100-
// {
101-
// continue;
102-
// }
103100
byte[] key = Hex.decode(map.get("Key"));
104101
byte[] nonce = Hex.decode(map.get("Nonce"));
105102
byte[] ad = Hex.decode(map.get("AD"));
@@ -115,10 +112,6 @@ private void testVectors()
115112
{
116113
mismatch("Keystream " + map.get("Count"), (String)map.get("CT"), rv);
117114
}
118-
// else
119-
// {
120-
// System.out.println("Keystream " + map.get("Count") + " pass");
121-
// }
122115
xoodyak.reset();
123116
xoodyak.init(false, params);
124117
//Decrypt

0 commit comments

Comments
 (0)