Skip to content

Commit 45c65cf

Browse files
author
gefeili
committed
Refactor around Ascon Digests
1 parent 624ab0a commit 45c65cf

File tree

6 files changed

+57
-20
lines changed

6 files changed

+57
-20
lines changed

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ protected void p(int nr)
5959

6060
protected abstract long pad(int i);
6161

62+
protected abstract long loadBytes(final byte[] bytes);
63+
6264
protected abstract long loadBytes(final byte[] bytes, int inOff, int n);
6365

66+
protected abstract void setBytes(long w, byte[] bytes, int inOff);
67+
6468
protected abstract void setBytes(long w, byte[] bytes, int inOff, int n);
6569

6670
@Override
@@ -81,7 +85,7 @@ public void update(byte in)
8185
m_buf[m_bufPos] = in;
8286
if (++m_bufPos == ASCON_HASH_RATE)
8387
{
84-
x0 ^= loadBytes(m_buf, 0, ASCON_HASH_RATE);
88+
x0 ^= loadBytes(m_buf);
8589
p(ASCON_PB_ROUNDS);
8690
m_bufPos = 0;
8791
}
@@ -106,7 +110,7 @@ public void update(byte[] input, int inOff, int len)
106110
{
107111
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
108112
inPos += available;
109-
x0 ^= loadBytes(m_buf, 0, m_buf.length);
113+
x0 ^= loadBytes(m_buf);
110114
p(ASCON_PB_ROUNDS);
111115
}
112116
int remaining;
@@ -127,29 +131,12 @@ protected void finishAbsorbing()
127131
p(12);
128132
}
129133

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-
147134
protected void squeeze(byte[] output, int outOff, int len)
148135
{
149136
/* squeeze full output blocks */
150137
while (len > ASCON_HASH_RATE)
151138
{
152-
setBytes(x0, output, outOff, ASCON_HASH_RATE);
139+
setBytes(x0, output, outOff);
153140
p(ASCON_PB_ROUNDS);
154141
outOff += ASCON_HASH_RATE;
155142
len -= ASCON_HASH_RATE;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,21 @@ protected long pad(int i)
5959
return 0x01L << (i << 3);
6060
}
6161

62+
protected long loadBytes(final byte[] bytes)
63+
{
64+
return Pack.littleEndianToLong(bytes, 0);
65+
}
66+
6267
protected long loadBytes(final byte[] bytes, int inOff, int n)
6368
{
6469
return Pack.littleEndianToLong(bytes, inOff, n);
6570
}
6671

72+
protected void setBytes(long w, byte[] bytes, int inOff)
73+
{
74+
Pack.longToLittleEndian(w, bytes, inOff);
75+
}
76+
6777
protected void setBytes(long w, byte[] bytes, int inOff, int n)
6878
{
6979
Pack.longToLittleEndian(w, bytes, inOff, n);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,21 @@ protected long pad(int i)
4646
return 0x80L << (56 - (i << 3));
4747
}
4848

49+
protected long loadBytes(final byte[] bytes)
50+
{
51+
return Pack.bigEndianToLong(bytes, 0);
52+
}
53+
4954
protected long loadBytes(final byte[] bytes, int inOff, int n)
5055
{
5156
return Pack.bigEndianToLong(bytes, inOff, n);
5257
}
5358

59+
protected void setBytes(long w, byte[] bytes, int inOff)
60+
{
61+
Pack.longToBigEndian(w, bytes, inOff);
62+
}
63+
5464
protected void setBytes(long w, byte[] bytes, int inOff, int n)
5565
{
5666
Pack.longToBigEndian(w, bytes, inOff, n);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@ protected long pad(int i)
2626
return 0x01L << (i << 3);
2727
}
2828

29+
protected long loadBytes(final byte[] bytes)
30+
{
31+
return Pack.littleEndianToLong(bytes, 0);
32+
}
33+
2934
protected long loadBytes(final byte[] bytes, int inOff, int n)
3035
{
3136
return Pack.littleEndianToLong(bytes, inOff, n);
3237
}
3338

39+
protected void setBytes(long w, byte[] bytes, int inOff)
40+
{
41+
Pack.longToLittleEndian(w, bytes, inOff);
42+
}
43+
3444
protected void setBytes(long w, byte[] bytes, int inOff, int n)
3545
{
3646
Pack.longToLittleEndian(w, bytes, inOff, n);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,21 @@ protected long pad(int i)
4949
return 0x80L << (56 - (i << 3));
5050
}
5151

52+
protected long loadBytes(final byte[] bytes)
53+
{
54+
return Pack.bigEndianToLong(bytes, 0);
55+
}
56+
5257
protected long loadBytes(final byte[] bytes, int inOff, int n)
5358
{
5459
return Pack.bigEndianToLong(bytes, inOff, n);
5560
}
5661

62+
protected void setBytes(long w, byte[] bytes, int inOff)
63+
{
64+
Pack.longToBigEndian(w, bytes, inOff);
65+
}
66+
5767
protected void setBytes(long w, byte[] bytes, int inOff, int n)
5868
{
5969
Pack.longToBigEndian(w, bytes, inOff, n);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,21 @@ protected long pad(int i)
2828
return 0x01L << (i << 3);
2929
}
3030

31+
protected long loadBytes(final byte[] bytes)
32+
{
33+
return Pack.littleEndianToLong(bytes, 0);
34+
}
35+
3136
protected long loadBytes(final byte[] bytes, int inOff, int n)
3237
{
3338
return Pack.littleEndianToLong(bytes, inOff, n);
3439
}
3540

41+
protected void setBytes(long w, byte[] bytes, int inOff)
42+
{
43+
Pack.longToLittleEndian(w, bytes, inOff);
44+
}
45+
3646
protected void setBytes(long w, byte[] bytes, int inOff, int n)
3747
{
3848
Pack.longToLittleEndian(w, bytes, inOff, n);

0 commit comments

Comments
 (0)