Skip to content

Commit aca4bd3

Browse files
author
gefeili
committed
Fix the AsconCXof128 based on #2032 code
1 parent e70b6af commit aca4bd3

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ public void reset()
8282
private void initState(byte[] z, int zOff, int zLen)
8383
{
8484
p.set(7445901275803737603L, 4886737088792722364L, -1616759365661982283L, 3076320316797452470L, -8124743304765850554L);
85-
long bitLength = ((long)zLen) << 3;
86-
Pack.longToLittleEndian(bitLength, m_buf, 0);
85+
p.x0 ^= ((long)zLen) << 3;
8786
p.p(12);
8887
update(z, zOff, zLen);
8988
padAndAbsorb();

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public String getName()
4545
public void performTest()
4646
throws Exception
4747
{
48+
testVectorsAsconCXof128_512();
4849
DigestTest.checkXof(new AsconXof128(), 1429, 317, new SecureRandom(), this);
4950
DigestTest.checkXof(new AsconCXof128(), 1429, 317, new SecureRandom(), this);
5051
DigestTest.checkXof(new AsconXof(AsconXof.AsconParameters.AsconXof), 1429, 317, new SecureRandom(), this);
@@ -520,6 +521,12 @@ public void testVectorsXof_AsconXof128()
520521
implTestVectorsXof(new AsconXof128(), "crypto/ascon/asconxof128", "LWC_HASH_KAT_256.txt");
521522
}
522523

524+
public void testVectorsAsconCXof128_512()
525+
throws Exception
526+
{
527+
implTestVectorsAsconCXof128(512 / 8, "crypto/ascon/asconcxof128", "LWC_CXOF_KAT_128_512.txt");
528+
}
529+
523530
public void testVectorsXof_AsconXof()
524531
throws Exception
525532
{
@@ -1172,6 +1179,54 @@ private void implTestVectorsEngine(AEADCipher ascon, String path, String filenam
11721179
}
11731180
}
11741181

1182+
private void implTestVectorsAsconCXof128(int hash_length, String path, String filename)
1183+
throws Exception
1184+
{
1185+
Random random = new Random();
1186+
1187+
InputStream src = TestResourceFinder.findTestResource(path, filename);
1188+
BufferedReader bin = new BufferedReader(new InputStreamReader(src));
1189+
String line;
1190+
HashMap<String, String> map = new HashMap<String, String>();
1191+
while ((line = bin.readLine()) != null)
1192+
{
1193+
int a = line.indexOf('=');
1194+
if (a < 0)
1195+
{
1196+
byte[] zByte = Hex.decode((String)map.get("Z"));
1197+
byte[] ptByte = Hex.decode((String)map.get("Msg"));
1198+
byte[] expected = Hex.decode((String)map.get("MD"));
1199+
1200+
byte[] hash = new byte[hash_length];
1201+
1202+
AsconCXof128 ascon = new AsconCXof128(zByte);
1203+
ascon.update(ptByte, 0, ptByte.length);
1204+
ascon.doFinal(hash, 0, hash_length);
1205+
if (!areEqual(hash, expected))
1206+
{
1207+
mismatch("Keystream " + map.get("Count"), (String)map.get("MD"), hash);
1208+
}
1209+
1210+
if (ptByte.length > 1)
1211+
{
1212+
int split = random.nextInt(ptByte.length - 1) + 1;
1213+
ascon = new AsconCXof128(zByte);
1214+
ascon.update(ptByte, 0, split);
1215+
ascon.update(ptByte, split, ptByte.length - split);
1216+
ascon.doFinal(hash, 0, hash_length);
1217+
if (!areEqual(hash, expected))
1218+
{
1219+
mismatch("Keystream " + map.get("Count"), (String)map.get("MD"), hash);
1220+
}
1221+
}
1222+
}
1223+
else
1224+
{
1225+
map.put(line.substring(0, a).trim(), line.substring(a + 1).trim());
1226+
}
1227+
}
1228+
}
1229+
11751230
private void implTestVectorsXof(Xof ascon, String path, String filename)
11761231
throws Exception
11771232
{

0 commit comments

Comments
 (0)