Skip to content

Commit 34ccfb0

Browse files
author
gefeili
committed
Update AsconBaseDigest
1 parent 8a4cc61 commit 34ccfb0

File tree

2 files changed

+19
-68
lines changed

2 files changed

+19
-68
lines changed

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

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package org.bouncycastle.crypto.digests;
22

3-
import org.bouncycastle.crypto.DataLengthException;
4-
import org.bouncycastle.crypto.ExtendedDigest;
53
import org.bouncycastle.crypto.OutputLengthException;
64
import org.bouncycastle.util.Arrays;
75
import org.bouncycastle.util.Longs;
86

97
abstract class AsconBaseDigest
10-
implements ExtendedDigest
8+
extends BufferBaseDigest
119
{
1210
protected long x0;
1311
protected long x1;
1412
protected long x2;
1513
protected long x3;
1614
protected long x4;
17-
protected final int DigestSize = 32;
18-
protected final int BlockSize = 8;
1915
protected int ASCON_PB_ROUNDS = 12;
20-
protected final byte[] m_buf = new byte[BlockSize];
21-
protected int m_bufPos = 0;
16+
17+
protected AsconBaseDigest()
18+
{
19+
DigestSize = 32;
20+
BlockSize = 8;
21+
m_buf = new byte[BlockSize];
22+
}
2223

2324
private void round(long C)
2425
{
@@ -66,67 +67,17 @@ protected void p(int nr)
6667

6768
protected abstract void setBytes(long w, byte[] bytes, int inOff, int n);
6869

69-
@Override
70-
public int getDigestSize()
71-
{
72-
return DigestSize;
73-
}
74-
75-
@Override
76-
public int getByteLength()
77-
{
78-
return BlockSize;
79-
}
80-
81-
@Override
82-
public void update(byte in)
70+
protected void processBytes(byte[] input, int inOff)
8371
{
84-
m_buf[m_bufPos] = in;
85-
if (++m_bufPos == BlockSize)
86-
{
87-
x0 ^= loadBytes(m_buf, 0);
88-
p(ASCON_PB_ROUNDS);
89-
m_bufPos = 0;
90-
}
72+
x0 ^= loadBytes(input, inOff);
73+
p(ASCON_PB_ROUNDS);
9174
}
9275

93-
@Override
94-
public void update(byte[] input, int inOff, int len)
76+
protected void finish(byte[] output, int outOff)
9577
{
96-
if ((inOff + len) > input.length)
97-
{
98-
throw new DataLengthException("input buffer too short");
99-
}
100-
int available = 8 - m_bufPos;
101-
if (len < available)
102-
{
103-
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
104-
m_bufPos += len;
105-
return;
106-
}
107-
int inPos = 0;
108-
if (m_bufPos > 0)
109-
{
110-
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
111-
inPos += available;
112-
x0 ^= loadBytes(m_buf, 0);
113-
p(ASCON_PB_ROUNDS);
114-
}
115-
int remaining;
116-
while ((remaining = len - inPos) >= 8)
117-
{
118-
x0 ^= loadBytes(input, inOff + inPos);
119-
p(ASCON_PB_ROUNDS);
120-
inPos += 8;
121-
}
122-
System.arraycopy(input, inOff + inPos, m_buf, 0, remaining);
123-
m_bufPos = remaining;
124-
}
125-
126-
@Override
127-
public int doFinal(byte[] output, int outOff)
128-
{
129-
return hash(output, outOff, DigestSize);
78+
padAndAbsorb();
79+
/* squeeze full output blocks */
80+
squeeze(output, outOff, DigestSize);
13081
}
13182

13283
protected void padAndAbsorb()

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ public void performTest()
9191
testVectorsXof_AsconXof();
9292
testVectorsXof_AsconXofA();
9393

94-
CipherTest.checkAEADParemeter(this, 16,16, 16, 16, new AsconAEAD128());
95-
CipherTest.checkAEADParemeter(this, 16,16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128));
96-
CipherTest.checkAEADParemeter(this, 16,16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128a));
97-
CipherTest.checkAEADParemeter(this, 20,16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon80pq));
94+
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new AsconAEAD128());
95+
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128));
96+
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128a));
97+
CipherTest.checkAEADParemeter(this, 20, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon80pq));
9898

9999
CipherTest.checkCipher(32, 16, 100, 128, new CipherTest.Instance()
100100
{

0 commit comments

Comments
 (0)