Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected void padAndAbsorb()
{
p.x0 ^= loadBytes(m_buf, 0, m_bufPos) ^ pad(m_bufPos);
p.p(12);
m_bufPos = 0;
}

protected void squeeze(byte[] output, int outOff, int len)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ public void reset()
private void initState(byte[] z, int zOff, int zLen)
{
p.set(7445901275803737603L, 4886737088792722364L, -1616759365661982283L, 3076320316797452470L, -8124743304765850554L);
long bitLength = ((long)zLen) << 3;
Pack.longToLittleEndian(bitLength, m_buf, 0);
p.x0 ^= ((long)zLen) << 3;
p.p(12);
update(z, zOff, zLen);
padAndAbsorb();
Expand Down
55 changes: 55 additions & 0 deletions core/src/test/java/org/bouncycastle/crypto/test/AsconTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void performTest()
testVectorsEngine_asconaead128();
testVectorsDigest_AsconHash256();
testVectorsXof_AsconXof128();
testVectorsAsconCXof128_512();

testBufferingEngine_asconaead128();
testBufferingEngine_ascon128();
Expand Down Expand Up @@ -514,6 +515,12 @@ public void testVectorsXof_AsconXof128()
implTestVectorsXof(new AsconXof128(), "crypto/ascon/asconxof128", "LWC_HASH_KAT_256.txt");
}

public void testVectorsAsconCXof128_512()
throws Exception
{
implTestVectorsAsconCXof128(512 / 8, "crypto/ascon/asconcxof128", "LWC_CXOF_KAT_128_512.txt");
}

public void testVectorsXof_AsconXof()
throws Exception
{
Expand Down Expand Up @@ -1211,6 +1218,54 @@ private void implTestVectorsXof(Xof ascon, String path, String filename)
}
}

private void implTestVectorsAsconCXof128(int hash_length, String path, String filename)
throws Exception
{
Random random = new Random();

InputStream src = TestResourceFinder.findTestResource(path, filename);
BufferedReader bin = new BufferedReader(new InputStreamReader(src));
String line;
HashMap<String, String> map = new HashMap<String, String>();
while ((line = bin.readLine()) != null)
{
int a = line.indexOf('=');
if (a < 0)
{
byte[] zByte = Hex.decode((String)map.get("Z"));
byte[] ptByte = Hex.decode((String)map.get("Msg"));
byte[] expected = Hex.decode((String)map.get("MD"));

byte[] hash = new byte[hash_length];

AsconCXof128 ascon = new AsconCXof128(zByte);
ascon.update(ptByte, 0, ptByte.length);
ascon.doFinal(hash, 0, hash_length);
if (!areEqual(hash, expected))
{
mismatch("Keystream " + map.get("Count"), (String)map.get("MD"), hash);
}

if (ptByte.length > 1)
{
int split = random.nextInt(ptByte.length - 1) + 1;
ascon = new AsconCXof128(zByte);
ascon.update(ptByte, 0, split);
ascon.update(ptByte, split, ptByte.length - split);
ascon.doFinal(hash, 0, hash_length);
if (!areEqual(hash, expected))
{
mismatch("Keystream " + map.get("Count"), (String)map.get("MD"), hash);
}
}
}
else
{
map.put(line.substring(0, a).trim(), line.substring(a + 1).trim());
}
}
}

private void mismatch(String name, String expected, byte[] found)
{
fail("mismatch on " + name, expected, new String(Hex.encode(found)));
Expand Down