Skip to content

Commit 624ab0a

Browse files
author
gefeili
committed
Refactor around Ascon
1 parent ebfa384 commit 624ab0a

File tree

9 files changed

+78
-39
lines changed

9 files changed

+78
-39
lines changed

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

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package org.bouncycastle.crypto.digests;
22

3-
import java.io.ByteArrayOutputStream;
4-
53
import org.bouncycastle.crypto.DataLengthException;
64
import org.bouncycastle.crypto.ExtendedDigest;
75
import org.bouncycastle.crypto.OutputLengthException;
6+
import org.bouncycastle.util.Arrays;
87
import org.bouncycastle.util.Longs;
98

109
abstract class AsconBaseDigest
@@ -18,8 +17,10 @@ abstract class AsconBaseDigest
1817
protected final int CRYPTO_BYTES = 32;
1918
protected final int ASCON_HASH_RATE = 8;
2019
protected int ASCON_PB_ROUNDS = 12;
20+
protected final byte[] m_buf = new byte[ASCON_HASH_RATE];
21+
protected int m_bufPos = 0;
22+
2123

22-
protected final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
2324
private void round(long C)
2425
{
2526
long t0 = x0 ^ x1 ^ x2 ^ x3 ^ C ^ (x1 & (x0 ^ x2 ^ x4 ^ C));
@@ -77,7 +78,13 @@ public int getByteLength()
7778
@Override
7879
public void update(byte in)
7980
{
80-
buffer.write(in);
81+
m_buf[m_bufPos] = in;
82+
if (++m_bufPos == ASCON_HASH_RATE)
83+
{
84+
x0 ^= loadBytes(m_buf, 0, ASCON_HASH_RATE);
85+
p(ASCON_PB_ROUNDS);
86+
m_bufPos = 0;
87+
}
8188
}
8289

8390
@Override
@@ -87,32 +94,62 @@ public void update(byte[] input, int inOff, int len)
8794
{
8895
throw new DataLengthException("input buffer too short");
8996
}
90-
buffer.write(input, inOff, len);
91-
}
92-
93-
protected void absorb(byte[] input, int len)
94-
{
95-
int inOff = 0;
96-
/* absorb full plaintext blocks */
97-
while (len >= ASCON_HASH_RATE)
97+
int available = 8 - m_bufPos;
98+
if (len < available)
9899
{
99-
x0 ^= loadBytes(input, inOff, 8);
100+
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
101+
m_bufPos += len;
102+
return;
103+
}
104+
int inPos = 0;
105+
if (m_bufPos > 0)
106+
{
107+
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
108+
inPos += available;
109+
x0 ^= loadBytes(m_buf, 0, m_buf.length);
100110
p(ASCON_PB_ROUNDS);
101-
inOff += ASCON_HASH_RATE;
102-
len -= ASCON_HASH_RATE;
103111
}
104-
/* absorb final plaintext block */
105-
x0 ^= loadBytes(input, inOff, len);
106-
x0 ^= pad(len);
112+
int remaining;
113+
while ((remaining = len - inPos) >= 8)
114+
{
115+
x0 ^= loadBytes(input, inOff + inPos, m_buf.length);
116+
p(ASCON_PB_ROUNDS);
117+
inPos += 8;
118+
}
119+
System.arraycopy(input, inOff + inPos, m_buf, 0, remaining);
120+
m_bufPos = remaining;
121+
}
122+
123+
protected void finishAbsorbing()
124+
{
125+
x0 ^= loadBytes(m_buf, 0, m_bufPos);
126+
x0 ^= pad(m_bufPos);
107127
p(12);
108128
}
109129

130+
// protected void absorb(byte[] input, int len)
131+
// {
132+
// int inOff = 0;
133+
// /* absorb full plaintext blocks */
134+
// while (len >= ASCON_HASH_RATE)
135+
// {
136+
// x0 ^= loadBytes(input, inOff, 8);
137+
// p(ASCON_PB_ROUNDS);
138+
// inOff += ASCON_HASH_RATE;
139+
// len -= ASCON_HASH_RATE;
140+
// }
141+
// /* absorb final plaintext block */
142+
// x0 ^= loadBytes(input, inOff, len);
143+
// x0 ^= pad(len);
144+
// p(12);
145+
// }
146+
110147
protected void squeeze(byte[] output, int outOff, int len)
111148
{
112149
/* squeeze full output blocks */
113150
while (len > ASCON_HASH_RATE)
114151
{
115-
setBytes(x0, output, outOff, 8);
152+
setBytes(x0, output, outOff, ASCON_HASH_RATE);
116153
p(ASCON_PB_ROUNDS);
117154
outOff += ASCON_HASH_RATE;
118155
len -= ASCON_HASH_RATE;
@@ -128,9 +165,15 @@ protected int hash(byte[] output, int outOff, int outLen)
128165
{
129166
throw new OutputLengthException("output buffer is too short");
130167
}
131-
absorb(buffer.toByteArray(), buffer.size());
168+
finishAbsorbing();
132169
/* squeeze full output blocks */
133170
squeeze(output, outOff, outLen);
134171
return outLen;
135172
}
173+
174+
public void reset()
175+
{
176+
Arrays.clear(m_buf);
177+
m_bufPos = 0;
178+
}
136179
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public AsconCxof128(byte[] s)
3131
}
3232
this.s = Arrays.clone(s);
3333
reset();
34+
update(s, 0, s.length);
3435
}
3536

3637
public AsconCxof128(byte[] s, int off, int len)
@@ -45,6 +46,7 @@ public AsconCxof128(byte[] s, int off, int len)
4546
}
4647
this.s = Arrays.copyOfRange(s, off, off + len);
4748
reset();
49+
update(s, 0, s.length);
4850
}
4951

5052
public AsconCxof128()
@@ -81,11 +83,7 @@ public int doOutput(byte[] output, int outOff, int outLen)
8183
{
8284
throw new OutputLengthException("output buffer is too short");
8385
}
84-
if (s != null)
85-
{
86-
absorb(s, s.length);
87-
}
88-
absorb(buffer.toByteArray(), buffer.size());
86+
finishAbsorbing();
8987
/* squeeze full output blocks */
9088
squeeze(output, outOff, outLen);
9189
return outLen;
@@ -106,7 +104,7 @@ public int doFinal(byte[] output, int outOff, int outLen)
106104
@Override
107105
public void reset()
108106
{
109-
buffer.reset();
107+
super.reset();
110108
/* initialize */
111109
x0 = 7445901275803737603L;
112110
x1 = 4886737088792722364L;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public int doFinal(byte[] output, int outOff)
7171
@Override
7272
public void reset()
7373
{
74-
buffer.reset();
74+
super.reset();
7575
/* initialize */
7676
switch (asconParameters)
7777
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public int doFinal(byte[] output, int outOff)
5151
@Override
5252
public void reset()
5353
{
54-
buffer.reset();
54+
super.reset();
5555
/* initialize */
5656
x0 = -7269279749984954751L;
5757
x1 = 5459383224871899602L;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public int getByteLength()
9393
@Override
9494
public void reset()
9595
{
96-
buffer.reset();
96+
super.reset();
9797
/* initialize */
9898
switch (asconParameters)
9999
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public int getByteLength()
7171
@Override
7272
public void reset()
7373
{
74-
buffer.reset();
74+
super.reset();
7575
/* initialize */
7676
x0 = -2701369817892108309L;
7777
x1 = -3711838248891385495L;

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ protected void setBytes(long n, byte[] bs, int off)
5454
Pack.longToLittleEndian(n, bs, off);
5555
}
5656

57-
5857
protected void ascon_aeadinit()
5958
{
6059
/* initialize */
@@ -86,13 +85,11 @@ protected void processFinalDecrypt(byte[] input, int inLen, byte[] output, int o
8685
if (inLen >= 8) // ASCON_AEAD_RATE == 16 is implied
8786
{
8887
long c0 = Pack.littleEndianToLong(input, 0);
89-
long c1 = Pack.littleEndianToLong(input, 8, inLen - 8);
90-
88+
inLen -= 8;
89+
long c1 = Pack.littleEndianToLong(input, 8, inLen);
9190
Pack.longToLittleEndian(x0 ^ c0, output, outOff);
92-
Pack.longToLittleEndian(x1 ^ c1, output, outOff + 8, inLen - 8);
93-
91+
Pack.longToLittleEndian(x1 ^ c1, output, outOff + 8, inLen);
9492
x0 = c0;
95-
inLen -= 8;
9693
x1 &= -(1L << (inLen << 3));
9794
x1 |= c1;
9895
x1 ^= pad(inLen);
@@ -116,10 +113,10 @@ protected void processFinalEncrypt(byte[] input, int inLen, byte[] output, int o
116113
if (inLen >= 8) // ASCON_AEAD_RATE == 16 is implied
117114
{
118115
x0 ^= Pack.littleEndianToLong(input, 0);
119-
x1 ^= Pack.littleEndianToLong(input, 8, inLen - 8);
116+
inLen -= 8;
117+
x1 ^= Pack.littleEndianToLong(input, 8, inLen);
120118
Pack.longToLittleEndian(x0, output, outOff);
121119
Pack.longToLittleEndian(x1, output, outOff + 8);
122-
inLen -= 8;
123120
x1 ^= pad(inLen);
124121
}
125122
else

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public void processAADByte(byte in)
213213
if (++m_bufPos == ASCON_AEAD_RATE)
214214
{
215215
processBufferAAD(m_buf, 0);
216+
m_bufPos = 0;
216217
}
217218
}
218219

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ private void implTestVectorsEngine(AEADCipher ascon, String path, String filenam
11061106
if (a < 0)
11071107
{
11081108
int count = Integer.parseInt(map.get("Count"));
1109-
// if (count != 529)
1109+
// if (count != 34)
11101110
// {
11111111
// continue;
11121112
// }

0 commit comments

Comments
 (0)