Skip to content

Commit 9191aec

Browse files
author
gefeili
committed
Fix the issue of Ascon Xofs to ensure that update functions cannot called after doOutput is called.
1 parent 900671b commit 9191aec

File tree

4 files changed

+99
-9
lines changed

4 files changed

+99
-9
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.bouncycastle.crypto.DataLengthException;
44
import org.bouncycastle.crypto.OutputLengthException;
55
import org.bouncycastle.crypto.Xof;
6-
import org.bouncycastle.util.Arrays;
76
import org.bouncycastle.util.Pack;
87

98
/**
@@ -21,7 +20,7 @@ public class AsconCXof128
2120
extends AsconBaseDigest
2221
implements Xof
2322
{
24-
23+
private boolean m_squeezing = false;
2524
private final long z0, z1, z2, z3, z4;
2625

2726
public AsconCXof128()
@@ -53,7 +52,25 @@ public AsconCXof128(byte[] s, int off, int len)
5352
z4 = x4;
5453
}
5554

55+
@Override
56+
public void update(byte in)
57+
{
58+
if (m_squeezing)
59+
{
60+
throw new IllegalArgumentException("attempt to absorb while squeezing");
61+
}
62+
super.update(in);
63+
}
5664

65+
@Override
66+
public void update(byte[] input, int inOff, int len)
67+
{
68+
if (m_squeezing)
69+
{
70+
throw new IllegalArgumentException("attempt to absorb while squeezing");
71+
}
72+
super.update(input, inOff, len);
73+
}
5774

5875
protected long pad(int i)
5976
{
@@ -80,6 +97,12 @@ protected void setBytes(long w, byte[] bytes, int inOff, int n)
8097
Pack.longToLittleEndian(w, bytes, inOff, n);
8198
}
8299

100+
protected void padAndAbsorb()
101+
{
102+
m_squeezing = true;
103+
super.padAndAbsorb();
104+
}
105+
83106
@Override
84107
public String getAlgorithmName()
85108
{
@@ -103,13 +126,16 @@ public int doOutput(byte[] output, int outOff, int outLen)
103126
@Override
104127
public int doFinal(byte[] output, int outOff, int outLen)
105128
{
106-
return doOutput(output, outOff, outLen);
129+
int rlt = doOutput(output, outOff, outLen);
130+
reset();
131+
return rlt;
107132
}
108133

109134
@Override
110135
public void reset()
111136
{
112137
super.reset();
138+
m_squeezing = false;
113139
/* initialize */
114140
x0 = z0;
115141
x1 = z1;
@@ -130,6 +156,7 @@ private void initState(byte[] z, int zOff, int zLen)
130156
p(12);
131157
update(z, zOff, zLen);
132158
padAndAbsorb();
159+
m_squeezing = false;
133160
}
134161
}
135162

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import org.bouncycastle.util.Pack;
44

5-
/** ASCON v1.2 Digest, https://ascon.iaik.tugraz.at/ .
5+
/**
6+
* ASCON v1.2 Digest, https://ascon.iaik.tugraz.at/ .
67
* <p>
78
* https://csrc.nist.gov/CSRC/media/Projects/lightweight-cryptography/documents/finalist-round/updated-spec-doc/ascon-spec-final.pdf
89
* <p>
910
* ASCON v1.2 Digest with reference to C Reference Impl from: https://github.com/ascon/ascon-c .
11+
*
1012
* @deprecated use Ascon Hash 256 Digest
1113
*/
1214
public class AsconDigest

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import org.bouncycastle.crypto.Xof;
44
import org.bouncycastle.util.Pack;
55

6-
/** ASCON v1.2 XOF, https://ascon.iaik.tugraz.at/ .
6+
/**
7+
* ASCON v1.2 XOF, https://ascon.iaik.tugraz.at/ .
78
* <p>
89
* https://csrc.nist.gov/CSRC/media/Projects/lightweight-cryptography/documents/finalist-round/updated-spec-doc/ascon-spec-final.pdf
910
* <p>
1011
* ASCON v1.2 XOF with reference to C Reference Impl from: https://github.com/ascon/ascon-c .
12+
*
1113
* @deprecated Now superseded - please use AsconXof128
1214
*/
1315
public class AsconXof
@@ -42,7 +44,33 @@ public AsconXof(AsconXof.AsconParameters parameters)
4244
}
4345

4446
private final String algorithmName;
47+
private boolean m_squeezing = false;
4548

49+
@Override
50+
public void update(byte in)
51+
{
52+
if (m_squeezing)
53+
{
54+
throw new IllegalArgumentException("attempt to absorb while squeezing");
55+
}
56+
super.update(in);
57+
}
58+
59+
@Override
60+
public void update(byte[] input, int inOff, int len)
61+
{
62+
if (m_squeezing)
63+
{
64+
throw new IllegalArgumentException("attempt to absorb while squeezing");
65+
}
66+
super.update(input, inOff, len);
67+
}
68+
69+
protected void padAndAbsorb()
70+
{
71+
m_squeezing = true;
72+
super.padAndAbsorb();
73+
}
4674

4775
protected long pad(int i)
4876
{
@@ -75,7 +103,6 @@ public String getAlgorithmName()
75103
return algorithmName;
76104
}
77105

78-
79106
@Override
80107
public int doOutput(byte[] output, int outOff, int outLen)
81108
{
@@ -85,7 +112,9 @@ public int doOutput(byte[] output, int outOff, int outLen)
85112
@Override
86113
public int doFinal(byte[] output, int outOff, int outLen)
87114
{
88-
return doOutput(output, outOff, outLen);
115+
int rlt = doOutput(output, outOff, outLen);
116+
reset();
117+
return rlt;
89118
}
90119

91120
@Override
@@ -98,6 +127,7 @@ public int getByteLength()
98127
public void reset()
99128
{
100129
super.reset();
130+
m_squeezing = false;
101131
/* initialize */
102132
switch (asconParameters)
103133
{

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
* <a href="https://csrc.nist.gov/pubs/sp/800/232/ipd">NIST SP 800-232 (Initial Public Draft)</a>.
1212
* For reference source code and implementation details, please see:
1313
* <a href="https://github.com/ascon/ascon-c">Reference, highly optimized, masked C and
14-
* ASM implementations of Ascon (NIST SP 800-232)</a>.
14+
* ASM implementations of Ascon (NIST SP 800-232)</a>.
1515
* </p>
1616
*/
1717
public class AsconXof128
1818
extends AsconBaseDigest
1919
implements Xof
2020
{
21+
private boolean m_squeezing = false;
22+
2123
public AsconXof128()
2224
{
2325
reset();
@@ -48,12 +50,38 @@ protected void setBytes(long w, byte[] bytes, int inOff, int n)
4850
Pack.longToLittleEndian(w, bytes, inOff, n);
4951
}
5052

53+
protected void padAndAbsorb()
54+
{
55+
m_squeezing = true;
56+
super.padAndAbsorb();
57+
}
58+
5159
@Override
5260
public String getAlgorithmName()
5361
{
5462
return "Ascon-XOF-128";
5563
}
5664

65+
@Override
66+
public void update(byte in)
67+
{
68+
if (m_squeezing)
69+
{
70+
throw new IllegalArgumentException("attempt to absorb while squeezing");
71+
}
72+
super.update(in);
73+
}
74+
75+
@Override
76+
public void update(byte[] input, int inOff, int len)
77+
{
78+
if (m_squeezing)
79+
{
80+
throw new IllegalArgumentException("attempt to absorb while squeezing");
81+
}
82+
super.update(input, inOff, len);
83+
}
84+
5785
@Override
5886
public int doOutput(byte[] output, int outOff, int outLen)
5987
{
@@ -63,7 +91,9 @@ public int doOutput(byte[] output, int outOff, int outLen)
6391
@Override
6492
public int doFinal(byte[] output, int outOff, int outLen)
6593
{
66-
return doOutput(output, outOff, outLen);
94+
int rlt = doOutput(output, outOff, outLen);
95+
reset();
96+
return rlt;
6797
}
6898

6999
@Override
@@ -75,6 +105,7 @@ public int getByteLength()
75105
@Override
76106
public void reset()
77107
{
108+
m_squeezing = false;
78109
super.reset();
79110
/* initialize */
80111
x0 = -2701369817892108309L;

0 commit comments

Comments
 (0)